2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
543 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
605 if (token == lexer->last_token)
611 if (token->type != CPP_PURGED)
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
678 if (tok == lexer->last_token)
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
796 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
804 /* Start emitting debugging information. */
807 cp_lexer_start_debugging (cp_lexer* lexer)
809 lexer->debugging_p = true;
812 /* Stop emitting debugging information. */
815 cp_lexer_stop_debugging (cp_lexer* lexer)
817 lexer->debugging_p = false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
827 cp_token_cache *cache = GGC_NEW (cp_token_cache);
828 cache->first = first;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator *make_call_declarator
852 (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854 (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856 (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858 (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860 (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862 (cp_cv_quals, tree, cp_declarator *);
864 /* An erroneous declarator. */
865 static cp_declarator *cp_error_declarator;
867 /* The obstack on which declarators and related data structures are
869 static struct obstack declarator_obstack;
871 /* Alloc BYTES from the declarator memory pool. */
874 alloc_declarator (size_t bytes)
876 return obstack_alloc (&declarator_obstack, bytes);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
885 cp_declarator *declarator;
887 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888 declarator->kind = kind;
889 declarator->attributes = NULL_TREE;
890 declarator->declarator = NULL;
891 declarator->parameter_pack_p = false;
896 /* Make a declarator for a generalized identifier. If
897 QUALIFYING_SCOPE is non-NULL, the identifier is
898 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904 special_function_kind sfk)
906 cp_declarator *declarator;
908 /* It is valid to write:
910 class C { void f(); };
914 The standard is not clear about whether `typedef const C D' is
915 legal; as of 2002-09-15 the committee is considering that
916 question. EDG 3.0 allows that syntax. Therefore, we do as
918 if (qualifying_scope && TYPE_P (qualifying_scope))
919 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925 declarator = make_declarator (cdk_id);
926 declarator->u.id.qualifying_scope = qualifying_scope;
927 declarator->u.id.unqualified_name = unqualified_name;
928 declarator->u.id.sfk = sfk;
933 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
934 of modifiers such as const or volatile to apply to the pointer
935 type, represented as identifiers. */
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_pointer);
943 declarator->declarator = target;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = NULL_TREE;
948 declarator->parameter_pack_p = target->parameter_pack_p;
949 target->parameter_pack_p = false;
952 declarator->parameter_pack_p = false;
957 /* Like make_pointer_declarator -- but for references. */
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 cp_declarator *declarator;
965 declarator = make_declarator (cdk_reference);
966 declarator->declarator = target;
967 declarator->u.reference.qualifiers = cv_qualifiers;
968 declarator->u.reference.rvalue_ref = rvalue_ref;
971 declarator->parameter_pack_p = target->parameter_pack_p;
972 target->parameter_pack_p = false;
975 declarator->parameter_pack_p = false;
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981 member of CLASS_TYPE. */
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985 cp_declarator *pointee)
987 cp_declarator *declarator;
989 declarator = make_declarator (cdk_ptrmem);
990 declarator->declarator = pointee;
991 declarator->u.pointer.qualifiers = cv_qualifiers;
992 declarator->u.pointer.class_type = class_type;
996 declarator->parameter_pack_p = pointee->parameter_pack_p;
997 pointee->parameter_pack_p = false;
1000 declarator->parameter_pack_p = false;
1005 /* Make a declarator for the function given by TARGET, with the
1006 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1007 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1008 indicates what exceptions can be thrown. */
1011 make_call_declarator (cp_declarator *target,
1013 cp_cv_quals cv_qualifiers,
1014 tree exception_specification,
1015 tree late_return_type)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1024 declarator->u.function.late_return_type = late_return_type;
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1031 declarator->parameter_pack_p = false;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1053 declarator->parameter_pack_p = false;
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1068 while (declarator && !found)
1070 switch ((int)declarator->kind)
1081 declarator = declarator->declarator;
1089 cp_parameter_declarator *no_parameters;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1099 cp_parameter_declarator *parameter;
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1107 clear_decl_specs (¶meter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 function_declarator_p (const cp_declarator *declarator)
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1128 declarator = declarator->declarator;
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, treat user-defined type-names
1199 as non-type identifiers. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201 /* When parsing a type-specifier, do not try to parse a class-specifier
1202 or enum-specifier. */
1203 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206 /* This type is used for parameters and variables which hold
1207 combinations of the above flags. */
1208 typedef int cp_parser_flags;
1210 /* The different kinds of declarators we want to parse. */
1212 typedef enum cp_parser_declarator_kind
1214 /* We want an abstract declarator. */
1215 CP_PARSER_DECLARATOR_ABSTRACT,
1216 /* We want a named declarator. */
1217 CP_PARSER_DECLARATOR_NAMED,
1218 /* We don't mind, but the name must be an unqualified-id. */
1219 CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1222 /* The precedence values used to parse binary expressions. The minimum value
1223 of PREC must be 1, because zero is reserved to quickly discriminate
1224 binary operators from other tokens. */
1229 PREC_LOGICAL_OR_EXPRESSION,
1230 PREC_LOGICAL_AND_EXPRESSION,
1231 PREC_INCLUSIVE_OR_EXPRESSION,
1232 PREC_EXCLUSIVE_OR_EXPRESSION,
1233 PREC_AND_EXPRESSION,
1234 PREC_EQUALITY_EXPRESSION,
1235 PREC_RELATIONAL_EXPRESSION,
1236 PREC_SHIFT_EXPRESSION,
1237 PREC_ADDITIVE_EXPRESSION,
1238 PREC_MULTIPLICATIVE_EXPRESSION,
1240 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244 precedence value. */
1246 typedef struct cp_parser_binary_operations_map_node
1248 /* The token type. */
1249 enum cpp_ttype token_type;
1250 /* The corresponding tree code. */
1251 enum tree_code tree_type;
1252 /* The precedence of this operator. */
1253 enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1256 /* The status of a tentative parse. */
1258 typedef enum cp_parser_status_kind
1260 /* No errors have occurred. */
1261 CP_PARSER_STATUS_KIND_NO_ERROR,
1262 /* An error has occurred. */
1263 CP_PARSER_STATUS_KIND_ERROR,
1264 /* We are committed to this tentative parse, whether or not an error
1266 CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1269 typedef struct cp_parser_expression_stack_entry
1271 /* Left hand side of the binary operation we are currently
1274 /* Original tree code for left hand side, if it was a binary
1275 expression itself (used for -Wparentheses). */
1276 enum tree_code lhs_type;
1277 /* Tree code for the binary operation we are parsing. */
1278 enum tree_code tree_type;
1279 /* Precedence of the binary operation we are parsing. */
1280 enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1283 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1284 entries because precedence levels on the stack are monotonically
1286 typedef struct cp_parser_expression_stack_entry
1287 cp_parser_expression_stack[NUM_PREC_VALUES];
1289 /* Context that is saved and restored when parsing tentatively. */
1290 typedef struct GTY (()) cp_parser_context {
1291 /* If this is a tentative parsing context, the status of the
1293 enum cp_parser_status_kind status;
1294 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1295 that are looked up in this context must be looked up both in the
1296 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297 the context of the containing expression. */
1300 /* The next parsing context in the stack. */
1301 struct cp_parser_context *next;
1302 } cp_parser_context;
1306 /* Constructors and destructors. */
1308 static cp_parser_context *cp_parser_context_new
1309 (cp_parser_context *);
1311 /* Class variables. */
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316 Transformed into an associative array (binops_by_token) by
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 /* The same as binops, but initialized by cp_parser_new so that
1353 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357 /* Constructors and destructors. */
1359 /* Construct a new context. The context below this one on the stack
1360 is given by NEXT. */
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1365 cp_parser_context *context;
1367 /* Allocate the storage. */
1368 if (cp_parser_context_free_list != NULL)
1370 /* Pull the first entry from the free list. */
1371 context = cp_parser_context_free_list;
1372 cp_parser_context_free_list = context->next;
1373 memset (context, 0, sizeof (*context));
1376 context = GGC_CNEW (cp_parser_context);
1378 /* No errors have occurred yet in this context. */
1379 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380 /* If this is not the bottommost context, copy information that we
1381 need from the previous context. */
1384 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385 expression, then we are parsing one in this context, too. */
1386 context->object_type = next->object_type;
1387 /* Thread the stack. */
1388 context->next = next;
1394 /* The cp_parser structure represents the C++ parser. */
1396 typedef struct GTY(()) cp_parser {
1397 /* The lexer from which we are obtaining tokens. */
1400 /* The scope in which names should be looked up. If NULL_TREE, then
1401 we look up names in the scope that is currently open in the
1402 source program. If non-NULL, this is either a TYPE or
1403 NAMESPACE_DECL for the scope in which we should look. It can
1404 also be ERROR_MARK, when we've parsed a bogus scope.
1406 This value is not cleared automatically after a name is looked
1407 up, so we must be careful to clear it before starting a new look
1408 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1409 will look up `Z' in the scope of `X', rather than the current
1410 scope.) Unfortunately, it is difficult to tell when name lookup
1411 is complete, because we sometimes peek at a token, look it up,
1412 and then decide not to consume it. */
1415 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416 last lookup took place. OBJECT_SCOPE is used if an expression
1417 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418 respectively. QUALIFYING_SCOPE is used for an expression of the
1419 form "X::Y"; it refers to X. */
1421 tree qualifying_scope;
1423 /* A stack of parsing contexts. All but the bottom entry on the
1424 stack will be tentative contexts.
1426 We parse tentatively in order to determine which construct is in
1427 use in some situations. For example, in order to determine
1428 whether a statement is an expression-statement or a
1429 declaration-statement we parse it tentatively as a
1430 declaration-statement. If that fails, we then reparse the same
1431 token stream as an expression-statement. */
1432 cp_parser_context *context;
1434 /* True if we are parsing GNU C++. If this flag is not set, then
1435 GNU extensions are not recognized. */
1436 bool allow_gnu_extensions_p;
1438 /* TRUE if the `>' token should be interpreted as the greater-than
1439 operator. FALSE if it is the end of a template-id or
1440 template-parameter-list. In C++0x mode, this flag also applies to
1441 `>>' tokens, which are viewed as two consecutive `>' tokens when
1442 this flag is FALSE. */
1443 bool greater_than_is_operator_p;
1445 /* TRUE if default arguments are allowed within a parameter list
1446 that starts at this point. FALSE if only a gnu extension makes
1447 them permissible. */
1448 bool default_arg_ok_p;
1450 /* TRUE if we are parsing an integral constant-expression. See
1451 [expr.const] for a precise definition. */
1452 bool integral_constant_expression_p;
1454 /* TRUE if we are parsing an integral constant-expression -- but a
1455 non-constant expression should be permitted as well. This flag
1456 is used when parsing an array bound so that GNU variable-length
1457 arrays are tolerated. */
1458 bool allow_non_integral_constant_expression_p;
1460 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461 been seen that makes the expression non-constant. */
1462 bool non_integral_constant_expression_p;
1464 /* TRUE if local variable names and `this' are forbidden in the
1466 bool local_variables_forbidden_p;
1468 /* TRUE if the declaration we are parsing is part of a
1469 linkage-specification of the form `extern string-literal
1471 bool in_unbraced_linkage_specification_p;
1473 /* TRUE if we are presently parsing a declarator, after the
1474 direct-declarator. */
1475 bool in_declarator_p;
1477 /* TRUE if we are presently parsing a template-argument-list. */
1478 bool in_template_argument_list_p;
1480 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481 to IN_OMP_BLOCK if parsing OpenMP structured block and
1482 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1483 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484 iteration-statement, OpenMP block or loop within that switch. */
1485 #define IN_SWITCH_STMT 1
1486 #define IN_ITERATION_STMT 2
1487 #define IN_OMP_BLOCK 4
1488 #define IN_OMP_FOR 8
1489 #define IN_IF_STMT 16
1490 unsigned char in_statement;
1492 /* TRUE if we are presently parsing the body of a switch statement.
1493 Note that this doesn't quite overlap with in_statement above.
1494 The difference relates to giving the right sets of error messages:
1495 "case not in switch" vs "break statement used with OpenMP...". */
1496 bool in_switch_statement_p;
1498 /* TRUE if we are parsing a type-id in an expression context. In
1499 such a situation, both "type (expr)" and "type (type)" are valid
1501 bool in_type_id_in_expr_p;
1503 /* TRUE if we are currently in a header file where declarations are
1504 implicitly extern "C". */
1505 bool implicit_extern_c;
1507 /* TRUE if strings in expressions should be translated to the execution
1509 bool translate_strings_p;
1511 /* TRUE if we are presently parsing the body of a function, but not
1513 bool in_function_body;
1515 /* If non-NULL, then we are parsing a construct where new type
1516 definitions are not permitted. The string stored here will be
1517 issued as an error message if a type is defined. */
1518 const char *type_definition_forbidden_message;
1520 /* A list of lists. The outer list is a stack, used for member
1521 functions of local classes. At each level there are two sub-list,
1522 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524 TREE_VALUE's. The functions are chained in reverse declaration
1527 The TREE_PURPOSE sublist contains those functions with default
1528 arguments that need post processing, and the TREE_VALUE sublist
1529 contains those functions with definitions that need post
1532 These lists can only be processed once the outermost class being
1533 defined is complete. */
1534 tree unparsed_functions_queues;
1536 /* The number of classes whose definitions are currently in
1538 unsigned num_classes_being_defined;
1540 /* The number of template parameter lists that apply directly to the
1541 current declaration. */
1542 unsigned num_template_parameter_lists;
1547 /* Constructors and destructors. */
1549 static cp_parser *cp_parser_new
1552 /* Routines to parse various constructs.
1554 Those that return `tree' will return the error_mark_node (rather
1555 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556 Sometimes, they will return an ordinary node if error-recovery was
1557 attempted, even though a parse error occurred. So, to check
1558 whether or not a parse error occurred, you should always use
1559 cp_parser_error_occurred. If the construct is optional (indicated
1560 either by an `_opt' in the name of the function that does the
1561 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562 the construct is not present. */
1564 /* Lexical conventions [gram.lex] */
1566 static tree cp_parser_identifier
1568 static tree cp_parser_string_literal
1569 (cp_parser *, bool, bool);
1571 /* Basic concepts [gram.basic] */
1573 static bool cp_parser_translation_unit
1576 /* Expressions [gram.expr] */
1578 static tree cp_parser_primary_expression
1579 (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581 (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589 (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591 (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593 (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597 (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599 (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601 (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1604 static tree cp_parser_new_expression
1606 static VEC(tree,gc) *cp_parser_new_placement
1608 static tree cp_parser_new_type_id
1609 (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1612 static cp_declarator *cp_parser_direct_new_declarator
1614 static VEC(tree,gc) *cp_parser_new_initializer
1616 static tree cp_parser_delete_expression
1618 static tree cp_parser_cast_expression
1619 (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623 (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625 (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1628 static tree cp_parser_expression
1629 (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631 (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1634 static tree cp_parser_lambda_expression
1636 static void cp_parser_lambda_introducer
1637 (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641 (cp_parser *, tree);
1643 /* Statements [gram.stmt.stmt] */
1645 static void cp_parser_statement
1646 (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1649 static tree cp_parser_expression_statement
1650 (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652 (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654 (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656 (cp_parser *, bool *);
1657 static tree cp_parser_condition
1659 static tree cp_parser_iteration_statement
1661 static void cp_parser_for_init_statement
1663 static tree cp_parser_jump_statement
1665 static void cp_parser_declaration_statement
1668 static tree cp_parser_implicitly_scoped_statement
1669 (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1673 /* Declarations [gram.dcl.dcl] */
1675 static void cp_parser_declaration_seq_opt
1677 static void cp_parser_declaration
1679 static void cp_parser_block_declaration
1680 (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1687 static tree cp_parser_function_specifier_opt
1688 (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692 static tree cp_parser_simple_type_specifier
1693 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1696 static tree cp_parser_nonclass_name
1697 (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699 (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1702 static void cp_parser_enumerator_list
1703 (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705 (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1708 static void cp_parser_namespace_definition
1710 static void cp_parser_namespace_body
1712 static tree cp_parser_qualified_namespace_specifier
1714 static void cp_parser_namespace_alias_definition
1716 static bool cp_parser_using_declaration
1717 (cp_parser *, bool);
1718 static void cp_parser_using_directive
1720 static void cp_parser_asm_definition
1722 static void cp_parser_linkage_specification
1724 static void cp_parser_static_assert
1725 (cp_parser *, bool);
1726 static tree cp_parser_decltype
1729 /* Declarators [gram.dcl.decl] */
1731 static tree cp_parser_init_declarator
1732 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738 (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741 static tree cp_parser_late_return_type_opt
1743 static tree cp_parser_declarator_id
1744 (cp_parser *, bool);
1745 static tree cp_parser_type_id
1747 static tree cp_parser_template_type_arg
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751 (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1756 static tree cp_parser_parameter_declaration_list
1757 (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759 (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument
1761 (cp_parser *, bool);
1762 static void cp_parser_function_body
1764 static tree cp_parser_initializer
1765 (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767 (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769 (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771 (cp_parser *, bool *);
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1776 /* Classes [gram.class] */
1778 static tree cp_parser_class_name
1779 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1782 static tree cp_parser_class_head
1783 (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1786 static void cp_parser_member_specification_opt
1788 static void cp_parser_member_declaration
1790 static tree cp_parser_pure_specifier
1792 static tree cp_parser_constant_initializer
1795 /* Derived classes [gram.class.derived] */
1797 static tree cp_parser_base_clause
1799 static tree cp_parser_base_specifier
1802 /* Special member functions [gram.special] */
1804 static tree cp_parser_conversion_function_id
1806 static tree cp_parser_conversion_type_id
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1810 static bool cp_parser_ctor_initializer_opt
1812 static void cp_parser_mem_initializer_list
1814 static tree cp_parser_mem_initializer
1816 static tree cp_parser_mem_initializer_id
1819 /* Overloading [gram.over] */
1821 static tree cp_parser_operator_function_id
1823 static tree cp_parser_operator
1826 /* Templates [gram.temp] */
1828 static void cp_parser_template_declaration
1829 (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1832 static tree cp_parser_template_parameter
1833 (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835 (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837 (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839 (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1842 static tree cp_parser_template_argument
1844 static void cp_parser_explicit_instantiation
1846 static void cp_parser_explicit_specialization
1849 /* Exception handling [gram.exception] */
1851 static tree cp_parser_try_block
1853 static bool cp_parser_function_try_block
1855 static void cp_parser_handler_seq
1857 static void cp_parser_handler
1859 static tree cp_parser_exception_declaration
1861 static tree cp_parser_throw_expression
1863 static tree cp_parser_exception_specification_opt
1865 static tree cp_parser_type_id_list
1868 /* GNU Extensions */
1870 static tree cp_parser_asm_specification_opt
1872 static tree cp_parser_asm_operand_list
1874 static tree cp_parser_asm_clobber_list
1876 static tree cp_parser_asm_label_list
1878 static tree cp_parser_attributes_opt
1880 static tree cp_parser_attribute_list
1882 static bool cp_parser_extension_opt
1883 (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889 (cp_parser *, enum pragma_context);
1891 /* Objective-C++ Productions */
1893 static tree cp_parser_objc_message_receiver
1895 static tree cp_parser_objc_message_args
1897 static tree cp_parser_objc_message_expression
1899 static tree cp_parser_objc_encode_expression
1901 static tree cp_parser_objc_defs_expression
1903 static tree cp_parser_objc_protocol_expression
1905 static tree cp_parser_objc_selector_expression
1907 static tree cp_parser_objc_expression
1909 static bool cp_parser_objc_selector_p
1911 static tree cp_parser_objc_selector
1913 static tree cp_parser_objc_protocol_refs_opt
1915 static void cp_parser_objc_declaration
1917 static tree cp_parser_objc_statement
1920 /* Utility Routines */
1922 static tree cp_parser_lookup_name
1923 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925 (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1928 static bool cp_parser_check_declarator_template_parameters
1929 (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931 (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1934 static tree cp_parser_global_scope_opt
1935 (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937 (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941 (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943 (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945 (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949 (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1954 static void cp_parser_save_default_args
1955 (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959 (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961 (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963 (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1966 static void cp_parser_set_storage_class
1967 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969 (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971 (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973 (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975 (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1978 static bool cp_parser_next_token_starts_class_definition_p
1980 static bool cp_parser_next_token_ends_template_argument_p
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983 (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1986 static void cp_parser_check_class_key
1987 (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989 (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1992 static void cp_parser_pre_parsed_nested_name_specifier
1994 static bool cp_parser_cache_group
1995 (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1998 static void cp_parser_commit_to_tentative_parse
2000 static void cp_parser_abort_tentative_parse
2002 static bool cp_parser_parse_definitely
2004 static inline bool cp_parser_parsing_tentatively
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2008 static void cp_parser_error
2009 (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011 (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2014 static bool cp_parser_check_type_definition
2016 static void cp_parser_check_for_definition_in_return_type
2017 (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019 (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021 (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023 (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026 static int cp_parser_skip_to_closing_parenthesis
2027 (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2032 static void cp_parser_skip_to_end_of_block_or_statement
2034 static bool cp_parser_skip_to_closing_brace
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2038 static void cp_parser_skip_to_pragma_eol
2039 (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2042 static bool cp_parser_allow_gnu_extensions_p
2044 static bool cp_parser_is_string_literal
2046 static bool cp_parser_is_keyword
2047 (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049 (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053 /* Returns nonzero if we are parsing tentatively. */
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2058 return parser->context->next != NULL;
2061 /* Returns nonzero if TOKEN is a string literal. */
2064 cp_parser_is_string_literal (cp_token* token)
2066 return (token->type == CPP_STRING ||
2067 token->type == CPP_STRING16 ||
2068 token->type == CPP_STRING32 ||
2069 token->type == CPP_WSTRING ||
2070 token->type == CPP_UTF8STRING);
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 return token->keyword == keyword;
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082 FILE:LINE: MESSAGE before TOKEN
2083 where TOKEN is the next token in the input stream. MESSAGE
2084 (specified by the caller) is usually of the form "expected
2088 cp_parser_error (cp_parser* parser, const char* message)
2090 if (!cp_parser_simulate_error (parser))
2092 cp_token *token = cp_lexer_peek_token (parser->lexer);
2093 /* This diagnostic makes more sense if it is tagged to the line
2094 of the token we just peeked at. */
2095 cp_lexer_set_source_position_from_token (token);
2097 if (token->type == CPP_PRAGMA)
2099 error_at (token->location,
2100 "%<#pragma%> is not allowed here");
2101 cp_parser_skip_to_pragma_eol (parser, token);
2105 c_parse_error (message,
2106 /* Because c_parser_error does not understand
2107 CPP_KEYWORD, keywords are treated like
2109 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110 token->u.value, token->flags);
2114 /* Issue an error about name-lookup failing. NAME is the
2115 IDENTIFIER_NODE DECL is the result of
2116 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2117 the thing that we hoped to find. */
2120 cp_parser_name_lookup_error (cp_parser* parser,
2123 const char* desired,
2124 location_t location)
2126 /* If name lookup completely failed, tell the user that NAME was not
2128 if (decl == error_mark_node)
2130 if (parser->scope && parser->scope != global_namespace)
2131 error_at (location, "%<%E::%E%> has not been declared",
2132 parser->scope, name);
2133 else if (parser->scope == global_namespace)
2134 error_at (location, "%<::%E%> has not been declared", name);
2135 else if (parser->object_scope
2136 && !CLASS_TYPE_P (parser->object_scope))
2137 error_at (location, "request for member %qE in non-class type %qT",
2138 name, parser->object_scope);
2139 else if (parser->object_scope)
2140 error_at (location, "%<%T::%E%> has not been declared",
2141 parser->object_scope, name);
2143 error_at (location, "%qE has not been declared", name);
2145 else if (parser->scope && parser->scope != global_namespace)
2146 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147 else if (parser->scope == global_namespace)
2148 error_at (location, "%<::%E%> %s", name, desired);
2150 error_at (location, "%qE %s", name, desired);
2153 /* If we are parsing tentatively, remember that an error has occurred
2154 during this tentative parse. Returns true if the error was
2155 simulated; false if a message should be issued by the caller. */
2158 cp_parser_simulate_error (cp_parser* parser)
2160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2168 /* Check for repeated decl-specifiers. */
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172 location_t location)
2176 for (ds = ds_first; ds != ds_last; ++ds)
2178 unsigned count = decl_specs->specs[ds];
2181 /* The "long" specifier is a special case because of "long long". */
2185 error_at (location, "%<long long long%> is too long for GCC");
2187 pedwarn_cxx98 (location, OPT_Wlong_long,
2188 "ISO C++ 1998 does not support %<long long%>");
2192 static const char *const decl_spec_names[] = {
2209 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2214 /* This function is called when a type is defined. If type
2215 definitions are forbidden at this point, an error message is
2219 cp_parser_check_type_definition (cp_parser* parser)
2221 /* If types are forbidden here, issue a message. */
2222 if (parser->type_definition_forbidden_message)
2224 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225 in the message need to be interpreted. */
2226 error (parser->type_definition_forbidden_message);
2232 /* This function is called when the DECLARATOR is processed. The TYPE
2233 was a type defined in the decl-specifiers. If it is invalid to
2234 define a type in the decl-specifiers for DECLARATOR, an error is
2235 issued. TYPE_LOCATION is the location of TYPE and is used
2236 for error reporting. */
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240 tree type, location_t type_location)
2242 /* [dcl.fct] forbids type definitions in return types.
2243 Unfortunately, it's not easy to know whether or not we are
2244 processing a return type until after the fact. */
2246 && (declarator->kind == cdk_pointer
2247 || declarator->kind == cdk_reference
2248 || declarator->kind == cdk_ptrmem))
2249 declarator = declarator->declarator;
2251 && declarator->kind == cdk_function)
2253 error_at (type_location,
2254 "new types may not be defined in a return type");
2255 inform (type_location,
2256 "(perhaps a semicolon is missing after the definition of %qT)",
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262 "<" in any valid C++ program. If the next token is indeed "<",
2263 issue a message warning the user about what appears to be an
2264 invalid attempt to form a template-id. LOCATION is the location
2265 of the type-specifier (TYPE) */
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269 tree type, location_t location)
2271 cp_token_position start = 0;
2273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276 error_at (location, "%qT is not a template", type);
2277 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278 error_at (location, "%qE is not a template", type);
2280 error_at (location, "invalid template-id");
2281 /* Remember the location of the invalid "<". */
2282 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283 start = cp_lexer_token_position (parser->lexer, true);
2284 /* Consume the "<". */
2285 cp_lexer_consume_token (parser->lexer);
2286 /* Parse the template arguments. */
2287 cp_parser_enclosed_template_argument_list (parser);
2288 /* Permanently remove the invalid template arguments so that
2289 this error message is not issued again. */
2291 cp_lexer_purge_tokens_after (parser->lexer, start);
2295 /* If parsing an integral constant-expression, issue an error message
2296 about the fact that THING appeared and return true. Otherwise,
2297 return false. In either case, set
2298 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2301 cp_parser_non_integral_constant_expression (cp_parser *parser,
2304 parser->non_integral_constant_expression_p = true;
2305 if (parser->integral_constant_expression_p)
2307 if (!parser->allow_non_integral_constant_expression_p)
2309 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310 in the message need to be interpreted. */
2311 char *message = concat (thing,
2312 " cannot appear in a constant-expression",
2322 /* Emit a diagnostic for an invalid type name. SCOPE is the
2323 qualifying scope (or NULL, if none) for ID. This function commits
2324 to the current active tentative parse, if any. (Otherwise, the
2325 problematic construct might be encountered again later, resulting
2326 in duplicate error messages.) LOCATION is the location of ID. */
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330 tree scope, tree id,
2331 location_t location)
2333 tree decl, old_scope;
2334 /* Try to lookup the identifier. */
2335 old_scope = parser->scope;
2336 parser->scope = scope;
2337 decl = cp_parser_lookup_name_simple (parser, id, location);
2338 parser->scope = old_scope;
2339 /* If the lookup found a template-name, it means that the user forgot
2340 to specify an argument list. Emit a useful error message. */
2341 if (TREE_CODE (decl) == TEMPLATE_DECL)
2343 "invalid use of template-name %qE without an argument list",
2345 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346 error_at (location, "invalid use of destructor %qD as a type", id);
2347 else if (TREE_CODE (decl) == TYPE_DECL)
2348 /* Something like 'unsigned A a;' */
2349 error_at (location, "invalid combination of multiple type-specifiers");
2350 else if (!parser->scope)
2352 /* Issue an error message. */
2353 error_at (location, "%qE does not name a type", id);
2354 /* If we're in a template class, it's possible that the user was
2355 referring to a type from a base class. For example:
2357 template <typename T> struct A { typedef T X; };
2358 template <typename T> struct B : public A<T> { X x; };
2360 The user should have said "typename A<T>::X". */
2361 if (processing_template_decl && current_class_type
2362 && TYPE_BINFO (current_class_type))
2366 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2370 tree base_type = BINFO_TYPE (b);
2371 if (CLASS_TYPE_P (base_type)
2372 && dependent_type_p (base_type))
2375 /* Go from a particular instantiation of the
2376 template (which will have an empty TYPE_FIELDs),
2377 to the main version. */
2378 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379 for (field = TYPE_FIELDS (base_type);
2381 field = TREE_CHAIN (field))
2382 if (TREE_CODE (field) == TYPE_DECL
2383 && DECL_NAME (field) == id)
2386 "(perhaps %<typename %T::%E%> was intended)",
2387 BINFO_TYPE (b), id);
2396 /* Here we diagnose qualified-ids where the scope is actually correct,
2397 but the identifier does not resolve to a valid type name. */
2398 else if (parser->scope != error_mark_node)
2400 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401 error_at (location, "%qE in namespace %qE does not name a type",
2403 else if (CLASS_TYPE_P (parser->scope)
2404 && constructor_name_p (id, parser->scope)
2405 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2407 error_at (location, "invalid use of constructor %<%T::%E%> as "
2408 "template", parser->scope, id);
2409 else if (TYPE_P (parser->scope)
2410 && dependent_scope_p (parser->scope))
2411 error_at (location, "need %<typename%> before %<%T::%E%> because "
2412 "%qT is a dependent scope",
2413 parser->scope, id, parser->scope);
2414 else if (TYPE_P (parser->scope))
2415 error_at (location, "%qE in class %qT does not name a type",
2420 cp_parser_commit_to_tentative_parse (parser);
2423 /* Check for a common situation where a type-name should be present,
2424 but is not, and issue a sensible error message. Returns true if an
2425 invalid type-name was detected.
2427 The situation handled by this function are variable declarations of the
2428 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2429 Usually, `ID' should name a type, but if we got here it means that it
2430 does not. We try to emit the best possible error message depending on
2431 how exactly the id-expression looks like. */
2434 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2437 cp_token *token = cp_lexer_peek_token (parser->lexer);
2439 /* Avoid duplicate error about ambiguous lookup. */
2440 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2442 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2443 if (next->type == CPP_NAME && next->ambiguous_p)
2447 cp_parser_parse_tentatively (parser);
2448 id = cp_parser_id_expression (parser,
2449 /*template_keyword_p=*/false,
2450 /*check_dependency_p=*/true,
2451 /*template_p=*/NULL,
2452 /*declarator_p=*/true,
2453 /*optional_p=*/false);
2454 /* If the next token is a (, this is a function with no explicit return
2455 type, i.e. constructor, destructor or conversion op. */
2456 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2457 || TREE_CODE (id) == TYPE_DECL)
2459 cp_parser_abort_tentative_parse (parser);
2462 if (!cp_parser_parse_definitely (parser))
2465 /* Emit a diagnostic for the invalid type. */
2466 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2467 id, token->location);
2469 /* If we aren't in the middle of a declarator (i.e. in a
2470 parameter-declaration-clause), skip to the end of the declaration;
2471 there's no point in trying to process it. */
2472 if (!parser->in_declarator_p)
2473 cp_parser_skip_to_end_of_block_or_statement (parser);
2477 /* Consume tokens up to, and including, the next non-nested closing `)'.
2478 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2479 are doing error recovery. Returns -1 if OR_COMMA is true and we
2480 found an unnested comma. */
2483 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2488 unsigned paren_depth = 0;
2489 unsigned brace_depth = 0;
2490 unsigned square_depth = 0;
2492 if (recovering && !or_comma
2493 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2498 cp_token * token = cp_lexer_peek_token (parser->lexer);
2500 switch (token->type)
2503 case CPP_PRAGMA_EOL:
2504 /* If we've run out of tokens, then there is no closing `)'. */
2507 /* This is good for lambda expression capture-lists. */
2508 case CPP_OPEN_SQUARE:
2511 case CPP_CLOSE_SQUARE:
2512 if (!square_depth--)
2517 /* This matches the processing in skip_to_end_of_statement. */
2522 case CPP_OPEN_BRACE:
2525 case CPP_CLOSE_BRACE:
2531 if (recovering && or_comma && !brace_depth && !paren_depth
2536 case CPP_OPEN_PAREN:
2541 case CPP_CLOSE_PAREN:
2542 if (!brace_depth && !paren_depth--)
2545 cp_lexer_consume_token (parser->lexer);
2554 /* Consume the token. */
2555 cp_lexer_consume_token (parser->lexer);
2559 /* Consume tokens until we reach the end of the current statement.
2560 Normally, that will be just before consuming a `;'. However, if a
2561 non-nested `}' comes first, then we stop before consuming that. */
2564 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2566 unsigned nesting_depth = 0;
2570 cp_token *token = cp_lexer_peek_token (parser->lexer);
2572 switch (token->type)
2575 case CPP_PRAGMA_EOL:
2576 /* If we've run out of tokens, stop. */
2580 /* If the next token is a `;', we have reached the end of the
2586 case CPP_CLOSE_BRACE:
2587 /* If this is a non-nested '}', stop before consuming it.
2588 That way, when confronted with something like:
2592 we stop before consuming the closing '}', even though we
2593 have not yet reached a `;'. */
2594 if (nesting_depth == 0)
2597 /* If it is the closing '}' for a block that we have
2598 scanned, stop -- but only after consuming the token.
2604 we will stop after the body of the erroneously declared
2605 function, but before consuming the following `typedef'
2607 if (--nesting_depth == 0)
2609 cp_lexer_consume_token (parser->lexer);
2613 case CPP_OPEN_BRACE:
2621 /* Consume the token. */
2622 cp_lexer_consume_token (parser->lexer);
2626 /* This function is called at the end of a statement or declaration.
2627 If the next token is a semicolon, it is consumed; otherwise, error
2628 recovery is attempted. */
2631 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2633 /* Look for the trailing `;'. */
2634 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2636 /* If there is additional (erroneous) input, skip to the end of
2638 cp_parser_skip_to_end_of_statement (parser);
2639 /* If the next token is now a `;', consume it. */
2640 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2641 cp_lexer_consume_token (parser->lexer);
2645 /* Skip tokens until we have consumed an entire block, or until we
2646 have consumed a non-nested `;'. */
2649 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2651 int nesting_depth = 0;
2653 while (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. */
2665 /* Stop if this is an unnested ';'. */
2670 case CPP_CLOSE_BRACE:
2671 /* Stop if this is an unnested '}', or closes the outermost
2674 if (nesting_depth < 0)
2680 case CPP_OPEN_BRACE:
2689 /* Consume the token. */
2690 cp_lexer_consume_token (parser->lexer);
2694 /* Skip tokens until a non-nested closing curly brace is the next
2695 token, or there are no more tokens. Return true in the first case,
2699 cp_parser_skip_to_closing_brace (cp_parser *parser)
2701 unsigned nesting_depth = 0;
2705 cp_token *token = cp_lexer_peek_token (parser->lexer);
2707 switch (token->type)
2710 case CPP_PRAGMA_EOL:
2711 /* If we've run out of tokens, stop. */
2714 case CPP_CLOSE_BRACE:
2715 /* If the next token is a non-nested `}', then we have reached
2716 the end of the current block. */
2717 if (nesting_depth-- == 0)
2721 case CPP_OPEN_BRACE:
2722 /* If it the next token is a `{', then we are entering a new
2723 block. Consume the entire block. */
2731 /* Consume the token. */
2732 cp_lexer_consume_token (parser->lexer);
2736 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2737 parameter is the PRAGMA token, allowing us to purge the entire pragma
2741 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2745 parser->lexer->in_pragma = false;
2748 token = cp_lexer_consume_token (parser->lexer);
2749 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2751 /* Ensure that the pragma is not parsed again. */
2752 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2755 /* Require pragma end of line, resyncing with it as necessary. The
2756 arguments are as for cp_parser_skip_to_pragma_eol. */
2759 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2761 parser->lexer->in_pragma = false;
2762 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2763 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2766 /* This is a simple wrapper around make_typename_type. When the id is
2767 an unresolved identifier node, we can provide a superior diagnostic
2768 using cp_parser_diagnose_invalid_type_name. */
2771 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2772 tree id, location_t id_location)
2775 if (TREE_CODE (id) == IDENTIFIER_NODE)
2777 result = make_typename_type (scope, id, typename_type,
2778 /*complain=*/tf_none);
2779 if (result == error_mark_node)
2780 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2783 return make_typename_type (scope, id, typename_type, tf_error);
2786 /* This is a wrapper around the
2787 make_{pointer,ptrmem,reference}_declarator functions that decides
2788 which one to call based on the CODE and CLASS_TYPE arguments. The
2789 CODE argument should be one of the values returned by
2790 cp_parser_ptr_operator. */
2791 static cp_declarator *
2792 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2793 cp_cv_quals cv_qualifiers,
2794 cp_declarator *target)
2796 if (code == ERROR_MARK)
2797 return cp_error_declarator;
2799 if (code == INDIRECT_REF)
2800 if (class_type == NULL_TREE)
2801 return make_pointer_declarator (cv_qualifiers, target);
2803 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2804 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2805 return make_reference_declarator (cv_qualifiers, target, false);
2806 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2807 return make_reference_declarator (cv_qualifiers, target, true);
2811 /* Create a new C++ parser. */
2814 cp_parser_new (void)
2820 /* cp_lexer_new_main is called before calling ggc_alloc because
2821 cp_lexer_new_main might load a PCH file. */
2822 lexer = cp_lexer_new_main ();
2824 /* Initialize the binops_by_token so that we can get the tree
2825 directly from the token. */
2826 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2827 binops_by_token[binops[i].token_type] = binops[i];
2829 parser = GGC_CNEW (cp_parser);
2830 parser->lexer = lexer;
2831 parser->context = cp_parser_context_new (NULL);
2833 /* For now, we always accept GNU extensions. */
2834 parser->allow_gnu_extensions_p = 1;
2836 /* The `>' token is a greater-than operator, not the end of a
2838 parser->greater_than_is_operator_p = true;
2840 parser->default_arg_ok_p = true;
2842 /* We are not parsing a constant-expression. */
2843 parser->integral_constant_expression_p = false;
2844 parser->allow_non_integral_constant_expression_p = false;
2845 parser->non_integral_constant_expression_p = false;
2847 /* Local variable names are not forbidden. */
2848 parser->local_variables_forbidden_p = false;
2850 /* We are not processing an `extern "C"' declaration. */
2851 parser->in_unbraced_linkage_specification_p = false;
2853 /* We are not processing a declarator. */
2854 parser->in_declarator_p = false;
2856 /* We are not processing a template-argument-list. */
2857 parser->in_template_argument_list_p = false;
2859 /* We are not in an iteration statement. */
2860 parser->in_statement = 0;
2862 /* We are not in a switch statement. */
2863 parser->in_switch_statement_p = false;
2865 /* We are not parsing a type-id inside an expression. */
2866 parser->in_type_id_in_expr_p = false;
2868 /* Declarations aren't implicitly extern "C". */
2869 parser->implicit_extern_c = false;
2871 /* String literals should be translated to the execution character set. */
2872 parser->translate_strings_p = true;
2874 /* We are not parsing a function body. */
2875 parser->in_function_body = false;
2877 /* The unparsed function queue is empty. */
2878 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2880 /* There are no classes being defined. */
2881 parser->num_classes_being_defined = 0;
2883 /* No template parameters apply. */
2884 parser->num_template_parameter_lists = 0;
2889 /* Create a cp_lexer structure which will emit the tokens in CACHE
2890 and push it onto the parser's lexer stack. This is used for delayed
2891 parsing of in-class method bodies and default arguments, and should
2892 not be confused with tentative parsing. */
2894 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2896 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2897 lexer->next = parser->lexer;
2898 parser->lexer = lexer;
2900 /* Move the current source position to that of the first token in the
2902 cp_lexer_set_source_position_from_token (lexer->next_token);
2905 /* Pop the top lexer off the parser stack. This is never used for the
2906 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2908 cp_parser_pop_lexer (cp_parser *parser)
2910 cp_lexer *lexer = parser->lexer;
2911 parser->lexer = lexer->next;
2912 cp_lexer_destroy (lexer);
2914 /* Put the current source position back where it was before this
2915 lexer was pushed. */
2916 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2919 /* Lexical conventions [gram.lex] */
2921 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2925 cp_parser_identifier (cp_parser* parser)
2929 /* Look for the identifier. */
2930 token = cp_parser_require (parser, CPP_NAME, "identifier");
2931 /* Return the value. */
2932 return token ? token->u.value : error_mark_node;
2935 /* Parse a sequence of adjacent string constants. Returns a
2936 TREE_STRING representing the combined, nul-terminated string
2937 constant. If TRANSLATE is true, translate the string to the
2938 execution character set. If WIDE_OK is true, a wide string is
2941 C++98 [lex.string] says that if a narrow string literal token is
2942 adjacent to a wide string literal token, the behavior is undefined.
2943 However, C99 6.4.5p4 says that this results in a wide string literal.
2944 We follow C99 here, for consistency with the C front end.
2946 This code is largely lifted from lex_string() in c-lex.c.
2948 FUTURE: ObjC++ will need to handle @-strings here. */
2950 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2954 struct obstack str_ob;
2955 cpp_string str, istr, *strs;
2957 enum cpp_ttype type;
2959 tok = cp_lexer_peek_token (parser->lexer);
2960 if (!cp_parser_is_string_literal (tok))
2962 cp_parser_error (parser, "expected string-literal");
2963 return error_mark_node;
2968 /* Try to avoid the overhead of creating and destroying an obstack
2969 for the common case of just one string. */
2970 if (!cp_parser_is_string_literal
2971 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2973 cp_lexer_consume_token (parser->lexer);
2975 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2976 str.len = TREE_STRING_LENGTH (tok->u.value);
2983 gcc_obstack_init (&str_ob);
2988 cp_lexer_consume_token (parser->lexer);
2990 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2991 str.len = TREE_STRING_LENGTH (tok->u.value);
2993 if (type != tok->type)
2995 if (type == CPP_STRING)
2997 else if (tok->type != CPP_STRING)
2998 error_at (tok->location,
2999 "unsupported non-standard concatenation "
3000 "of string literals");
3003 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3005 tok = cp_lexer_peek_token (parser->lexer);
3007 while (cp_parser_is_string_literal (tok));
3009 strs = (cpp_string *) obstack_finish (&str_ob);
3012 if (type != CPP_STRING && !wide_ok)
3014 cp_parser_error (parser, "a wide string is invalid in this context");
3018 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3019 (parse_in, strs, count, &istr, type))
3021 value = build_string (istr.len, (const char *)istr.text);
3022 free (CONST_CAST (unsigned char *, istr.text));
3028 case CPP_UTF8STRING:
3029 TREE_TYPE (value) = char_array_type_node;
3032 TREE_TYPE (value) = char16_array_type_node;
3035 TREE_TYPE (value) = char32_array_type_node;
3038 TREE_TYPE (value) = wchar_array_type_node;
3042 value = fix_string_type (value);
3045 /* cpp_interpret_string has issued an error. */
3046 value = error_mark_node;
3049 obstack_free (&str_ob, 0);
3055 /* Basic concepts [gram.basic] */
3057 /* Parse a translation-unit.
3060 declaration-seq [opt]
3062 Returns TRUE if all went well. */
3065 cp_parser_translation_unit (cp_parser* parser)
3067 /* The address of the first non-permanent object on the declarator
3069 static void *declarator_obstack_base;
3073 /* Create the declarator obstack, if necessary. */
3074 if (!cp_error_declarator)
3076 gcc_obstack_init (&declarator_obstack);
3077 /* Create the error declarator. */
3078 cp_error_declarator = make_declarator (cdk_error);
3079 /* Create the empty parameter list. */
3080 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3081 /* Remember where the base of the declarator obstack lies. */
3082 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3085 cp_parser_declaration_seq_opt (parser);
3087 /* If there are no tokens left then all went well. */
3088 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3090 /* Get rid of the token array; we don't need it any more. */
3091 cp_lexer_destroy (parser->lexer);
3092 parser->lexer = NULL;
3094 /* This file might have been a context that's implicitly extern
3095 "C". If so, pop the lang context. (Only relevant for PCH.) */
3096 if (parser->implicit_extern_c)
3098 pop_lang_context ();
3099 parser->implicit_extern_c = false;
3103 finish_translation_unit ();
3109 cp_parser_error (parser, "expected declaration");
3113 /* Make sure the declarator obstack was fully cleaned up. */
3114 gcc_assert (obstack_next_free (&declarator_obstack)
3115 == declarator_obstack_base);
3117 /* All went well. */
3121 /* Expressions [gram.expr] */
3123 /* Parse a primary-expression.
3134 ( compound-statement )
3135 __builtin_va_arg ( assignment-expression , type-id )
3136 __builtin_offsetof ( type-id , offsetof-expression )
3139 __has_nothrow_assign ( type-id )
3140 __has_nothrow_constructor ( type-id )
3141 __has_nothrow_copy ( type-id )
3142 __has_trivial_assign ( type-id )
3143 __has_trivial_constructor ( type-id )
3144 __has_trivial_copy ( type-id )
3145 __has_trivial_destructor ( type-id )
3146 __has_virtual_destructor ( type-id )
3147 __is_abstract ( type-id )
3148 __is_base_of ( type-id , type-id )
3149 __is_class ( type-id )
3150 __is_convertible_to ( type-id , type-id )
3151 __is_empty ( type-id )
3152 __is_enum ( type-id )
3153 __is_pod ( type-id )
3154 __is_polymorphic ( type-id )
3155 __is_union ( type-id )
3157 Objective-C++ Extension:
3165 ADDRESS_P is true iff this expression was immediately preceded by
3166 "&" and therefore might denote a pointer-to-member. CAST_P is true
3167 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3168 true iff this expression is a template argument.
3170 Returns a representation of the expression. Upon return, *IDK
3171 indicates what kind of id-expression (if any) was present. */
3174 cp_parser_primary_expression (cp_parser *parser,
3177 bool template_arg_p,
3180 cp_token *token = NULL;
3182 /* Assume the primary expression is not an id-expression. */
3183 *idk = CP_ID_KIND_NONE;
3185 /* Peek at the next token. */
3186 token = cp_lexer_peek_token (parser->lexer);
3187 switch (token->type)
3200 token = cp_lexer_consume_token (parser->lexer);
3201 if (TREE_CODE (token->u.value) == FIXED_CST)
3203 error_at (token->location,
3204 "fixed-point types not supported in C++");
3205 return error_mark_node;
3207 /* Floating-point literals are only allowed in an integral
3208 constant expression if they are cast to an integral or
3209 enumeration type. */
3210 if (TREE_CODE (token->u.value) == REAL_CST
3211 && parser->integral_constant_expression_p
3214 /* CAST_P will be set even in invalid code like "int(2.7 +
3215 ...)". Therefore, we have to check that the next token
3216 is sure to end the cast. */
3219 cp_token *next_token;
3221 next_token = cp_lexer_peek_token (parser->lexer);
3222 if (/* The comma at the end of an
3223 enumerator-definition. */
3224 next_token->type != CPP_COMMA
3225 /* The curly brace at the end of an enum-specifier. */
3226 && next_token->type != CPP_CLOSE_BRACE
3227 /* The end of a statement. */
3228 && next_token->type != CPP_SEMICOLON
3229 /* The end of the cast-expression. */
3230 && next_token->type != CPP_CLOSE_PAREN
3231 /* The end of an array bound. */
3232 && next_token->type != CPP_CLOSE_SQUARE
3233 /* The closing ">" in a template-argument-list. */
3234 && (next_token->type != CPP_GREATER
3235 || parser->greater_than_is_operator_p)
3236 /* C++0x only: A ">>" treated like two ">" tokens,
3237 in a template-argument-list. */
3238 && (next_token->type != CPP_RSHIFT
3239 || (cxx_dialect == cxx98)
3240 || parser->greater_than_is_operator_p))
3244 /* If we are within a cast, then the constraint that the
3245 cast is to an integral or enumeration type will be
3246 checked at that point. If we are not within a cast, then
3247 this code is invalid. */
3249 cp_parser_non_integral_constant_expression
3250 (parser, "floating-point literal");
3252 return token->u.value;
3258 case CPP_UTF8STRING:
3259 /* ??? Should wide strings be allowed when parser->translate_strings_p
3260 is false (i.e. in attributes)? If not, we can kill the third
3261 argument to cp_parser_string_literal. */
3262 return cp_parser_string_literal (parser,
3263 parser->translate_strings_p,
3266 case CPP_OPEN_PAREN:
3269 bool saved_greater_than_is_operator_p;
3271 /* Consume the `('. */
3272 cp_lexer_consume_token (parser->lexer);
3273 /* Within a parenthesized expression, a `>' token is always
3274 the greater-than operator. */
3275 saved_greater_than_is_operator_p
3276 = parser->greater_than_is_operator_p;
3277 parser->greater_than_is_operator_p = true;
3278 /* If we see `( { ' then we are looking at the beginning of
3279 a GNU statement-expression. */
3280 if (cp_parser_allow_gnu_extensions_p (parser)
3281 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3283 /* Statement-expressions are not allowed by the standard. */
3284 pedwarn (token->location, OPT_pedantic,
3285 "ISO C++ forbids braced-groups within expressions");
3287 /* And they're not allowed outside of a function-body; you
3288 cannot, for example, write:
3290 int i = ({ int j = 3; j + 1; });
3292 at class or namespace scope. */
3293 if (!parser->in_function_body
3294 || parser->in_template_argument_list_p)
3296 error_at (token->location,
3297 "statement-expressions are not allowed outside "
3298 "functions nor in template-argument lists");
3299 cp_parser_skip_to_end_of_block_or_statement (parser);
3300 expr = error_mark_node;
3304 /* Start the statement-expression. */
3305 expr = begin_stmt_expr ();
3306 /* Parse the compound-statement. */
3307 cp_parser_compound_statement (parser, expr, false);
3309 expr = finish_stmt_expr (expr, false);
3314 /* Parse the parenthesized expression. */
3315 expr = cp_parser_expression (parser, cast_p, idk);
3316 /* Let the front end know that this expression was
3317 enclosed in parentheses. This matters in case, for
3318 example, the expression is of the form `A::B', since
3319 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3321 finish_parenthesized_expr (expr);
3323 /* The `>' token might be the end of a template-id or
3324 template-parameter-list now. */
3325 parser->greater_than_is_operator_p
3326 = saved_greater_than_is_operator_p;
3327 /* Consume the `)'. */
3328 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3329 cp_parser_skip_to_end_of_statement (parser);
3334 case CPP_OPEN_SQUARE:
3335 if (c_dialect_objc ())
3336 /* We have an Objective-C++ message. */
3337 return cp_parser_objc_expression (parser);
3338 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3339 return cp_parser_lambda_expression (parser);
3341 case CPP_OBJC_STRING:
3342 if (c_dialect_objc ())
3343 /* We have an Objective-C++ string literal. */
3344 return cp_parser_objc_expression (parser);
3345 cp_parser_error (parser, "expected primary-expression");
3346 return error_mark_node;
3349 switch (token->keyword)
3351 /* These two are the boolean literals. */
3353 cp_lexer_consume_token (parser->lexer);
3354 return boolean_true_node;
3356 cp_lexer_consume_token (parser->lexer);
3357 return boolean_false_node;
3359 /* The `__null' literal. */
3361 cp_lexer_consume_token (parser->lexer);
3364 /* Recognize the `this' keyword. */
3366 cp_lexer_consume_token (parser->lexer);
3367 if (parser->local_variables_forbidden_p)
3369 error_at (token->location,
3370 "%<this%> may not be used in this context");
3371 return error_mark_node;
3373 /* Pointers cannot appear in constant-expressions. */
3374 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3375 return error_mark_node;
3376 return finish_this_expr ();
3378 /* The `operator' keyword can be the beginning of an
3383 case RID_FUNCTION_NAME:
3384 case RID_PRETTY_FUNCTION_NAME:
3385 case RID_C99_FUNCTION_NAME:
3389 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3390 __func__ are the names of variables -- but they are
3391 treated specially. Therefore, they are handled here,
3392 rather than relying on the generic id-expression logic
3393 below. Grammatically, these names are id-expressions.
3395 Consume the token. */
3396 token = cp_lexer_consume_token (parser->lexer);
3398 switch (token->keyword)
3400 case RID_FUNCTION_NAME:
3401 name = "%<__FUNCTION__%>";
3403 case RID_PRETTY_FUNCTION_NAME:
3404 name = "%<__PRETTY_FUNCTION__%>";
3406 case RID_C99_FUNCTION_NAME:
3407 name = "%<__func__%>";
3413 if (cp_parser_non_integral_constant_expression (parser, name))
3414 return error_mark_node;
3416 /* Look up the name. */
3417 return finish_fname (token->u.value);
3425 /* The `__builtin_va_arg' construct is used to handle
3426 `va_arg'. Consume the `__builtin_va_arg' token. */
3427 cp_lexer_consume_token (parser->lexer);
3428 /* Look for the opening `('. */
3429 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3430 /* Now, parse the assignment-expression. */
3431 expression = cp_parser_assignment_expression (parser,
3432 /*cast_p=*/false, NULL);
3433 /* Look for the `,'. */
3434 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3435 /* Parse the type-id. */
3436 type = cp_parser_type_id (parser);
3437 /* Look for the closing `)'. */
3438 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3439 /* Using `va_arg' in a constant-expression is not
3441 if (cp_parser_non_integral_constant_expression (parser,
3443 return error_mark_node;
3444 return build_x_va_arg (expression, type);
3448 return cp_parser_builtin_offsetof (parser);
3450 case RID_HAS_NOTHROW_ASSIGN:
3451 case RID_HAS_NOTHROW_CONSTRUCTOR:
3452 case RID_HAS_NOTHROW_COPY:
3453 case RID_HAS_TRIVIAL_ASSIGN:
3454 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3455 case RID_HAS_TRIVIAL_COPY:
3456 case RID_HAS_TRIVIAL_DESTRUCTOR:
3457 case RID_HAS_VIRTUAL_DESTRUCTOR:
3458 case RID_IS_ABSTRACT:
3459 case RID_IS_BASE_OF:
3461 case RID_IS_CONVERTIBLE_TO:
3465 case RID_IS_POLYMORPHIC:
3466 case RID_IS_STD_LAYOUT:
3467 case RID_IS_TRIVIAL:
3469 return cp_parser_trait_expr (parser, token->keyword);
3471 /* Objective-C++ expressions. */
3473 case RID_AT_PROTOCOL:
3474 case RID_AT_SELECTOR:
3475 return cp_parser_objc_expression (parser);
3478 cp_parser_error (parser, "expected primary-expression");
3479 return error_mark_node;
3482 /* An id-expression can start with either an identifier, a
3483 `::' as the beginning of a qualified-id, or the "operator"
3487 case CPP_TEMPLATE_ID:
3488 case CPP_NESTED_NAME_SPECIFIER:
3492 const char *error_msg;
3495 cp_token *id_expr_token;
3498 /* Parse the id-expression. */
3500 = cp_parser_id_expression (parser,
3501 /*template_keyword_p=*/false,
3502 /*check_dependency_p=*/true,
3504 /*declarator_p=*/false,
3505 /*optional_p=*/false);
3506 if (id_expression == error_mark_node)
3507 return error_mark_node;
3508 id_expr_token = token;
3509 token = cp_lexer_peek_token (parser->lexer);
3510 done = (token->type != CPP_OPEN_SQUARE
3511 && token->type != CPP_OPEN_PAREN
3512 && token->type != CPP_DOT
3513 && token->type != CPP_DEREF
3514 && token->type != CPP_PLUS_PLUS
3515 && token->type != CPP_MINUS_MINUS);
3516 /* If we have a template-id, then no further lookup is
3517 required. If the template-id was for a template-class, we
3518 will sometimes have a TYPE_DECL at this point. */
3519 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3520 || TREE_CODE (id_expression) == TYPE_DECL)
3521 decl = id_expression;
3522 /* Look up the name. */
3525 tree ambiguous_decls;
3527 /* If we already know that this lookup is ambiguous, then
3528 we've already issued an error message; there's no reason
3530 if (id_expr_token->type == CPP_NAME
3531 && id_expr_token->ambiguous_p)
3533 cp_parser_simulate_error (parser);
3534 return error_mark_node;
3537 decl = cp_parser_lookup_name (parser, id_expression,
3540 /*is_namespace=*/false,
3541 /*check_dependency=*/true,
3543 id_expr_token->location);
3544 /* If the lookup was ambiguous, an error will already have
3546 if (ambiguous_decls)
3547 return error_mark_node;
3549 /* In Objective-C++, an instance variable (ivar) may be preferred
3550 to whatever cp_parser_lookup_name() found. */
3551 decl = objc_lookup_ivar (decl, id_expression);
3553 /* If name lookup gives us a SCOPE_REF, then the
3554 qualifying scope was dependent. */
3555 if (TREE_CODE (decl) == SCOPE_REF)
3557 /* At this point, we do not know if DECL is a valid
3558 integral constant expression. We assume that it is
3559 in fact such an expression, so that code like:
3561 template <int N> struct A {
3565 is accepted. At template-instantiation time, we
3566 will check that B<N>::i is actually a constant. */
3569 /* Check to see if DECL is a local variable in a context
3570 where that is forbidden. */
3571 if (parser->local_variables_forbidden_p
3572 && local_variable_p (decl))
3574 /* It might be that we only found DECL because we are
3575 trying to be generous with pre-ISO scoping rules.
3576 For example, consider:
3580 for (int i = 0; i < 10; ++i) {}
3581 extern void f(int j = i);
3584 Here, name look up will originally find the out
3585 of scope `i'. We need to issue a warning message,
3586 but then use the global `i'. */
3587 decl = check_for_out_of_scope_variable (decl);
3588 if (local_variable_p (decl))
3590 error_at (id_expr_token->location,
3591 "local variable %qD may not appear in this context",
3593 return error_mark_node;
3598 decl = (finish_id_expression
3599 (id_expression, decl, parser->scope,
3601 parser->integral_constant_expression_p,
3602 parser->allow_non_integral_constant_expression_p,
3603 &parser->non_integral_constant_expression_p,
3604 template_p, done, address_p,
3607 id_expr_token->location));
3609 cp_parser_error (parser, error_msg);
3613 /* Anything else is an error. */
3615 cp_parser_error (parser, "expected primary-expression");
3616 return error_mark_node;
3620 /* Parse an id-expression.
3627 :: [opt] nested-name-specifier template [opt] unqualified-id
3629 :: operator-function-id
3632 Return a representation of the unqualified portion of the
3633 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3634 a `::' or nested-name-specifier.
3636 Often, if the id-expression was a qualified-id, the caller will
3637 want to make a SCOPE_REF to represent the qualified-id. This
3638 function does not do this in order to avoid wastefully creating
3639 SCOPE_REFs when they are not required.
3641 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3644 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3645 uninstantiated templates.
3647 If *TEMPLATE_P is non-NULL, it is set to true iff the
3648 `template' keyword is used to explicitly indicate that the entity
3649 named is a template.
3651 If DECLARATOR_P is true, the id-expression is appearing as part of
3652 a declarator, rather than as part of an expression. */
3655 cp_parser_id_expression (cp_parser *parser,
3656 bool template_keyword_p,
3657 bool check_dependency_p,
3662 bool global_scope_p;
3663 bool nested_name_specifier_p;
3665 /* Assume the `template' keyword was not used. */
3667 *template_p = template_keyword_p;
3669 /* Look for the optional `::' operator. */
3671 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3673 /* Look for the optional nested-name-specifier. */
3674 nested_name_specifier_p
3675 = (cp_parser_nested_name_specifier_opt (parser,
3676 /*typename_keyword_p=*/false,
3681 /* If there is a nested-name-specifier, then we are looking at
3682 the first qualified-id production. */
3683 if (nested_name_specifier_p)
3686 tree saved_object_scope;
3687 tree saved_qualifying_scope;
3688 tree unqualified_id;
3691 /* See if the next token is the `template' keyword. */
3693 template_p = &is_template;
3694 *template_p = cp_parser_optional_template_keyword (parser);
3695 /* Name lookup we do during the processing of the
3696 unqualified-id might obliterate SCOPE. */
3697 saved_scope = parser->scope;
3698 saved_object_scope = parser->object_scope;
3699 saved_qualifying_scope = parser->qualifying_scope;
3700 /* Process the final unqualified-id. */
3701 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3704 /*optional_p=*/false);
3705 /* Restore the SAVED_SCOPE for our caller. */
3706 parser->scope = saved_scope;
3707 parser->object_scope = saved_object_scope;
3708 parser->qualifying_scope = saved_qualifying_scope;
3710 return unqualified_id;
3712 /* Otherwise, if we are in global scope, then we are looking at one
3713 of the other qualified-id productions. */
3714 else if (global_scope_p)
3719 /* Peek at the next token. */
3720 token = cp_lexer_peek_token (parser->lexer);
3722 /* If it's an identifier, and the next token is not a "<", then
3723 we can avoid the template-id case. This is an optimization
3724 for this common case. */
3725 if (token->type == CPP_NAME
3726 && !cp_parser_nth_token_starts_template_argument_list_p
3728 return cp_parser_identifier (parser);
3730 cp_parser_parse_tentatively (parser);
3731 /* Try a template-id. */
3732 id = cp_parser_template_id (parser,
3733 /*template_keyword_p=*/false,
3734 /*check_dependency_p=*/true,
3736 /* If that worked, we're done. */
3737 if (cp_parser_parse_definitely (parser))
3740 /* Peek at the next token. (Changes in the token buffer may
3741 have invalidated the pointer obtained above.) */
3742 token = cp_lexer_peek_token (parser->lexer);
3744 switch (token->type)
3747 return cp_parser_identifier (parser);
3750 if (token->keyword == RID_OPERATOR)
3751 return cp_parser_operator_function_id (parser);
3755 cp_parser_error (parser, "expected id-expression");
3756 return error_mark_node;
3760 return cp_parser_unqualified_id (parser, template_keyword_p,
3761 /*check_dependency_p=*/true,
3766 /* Parse an unqualified-id.
3770 operator-function-id
3771 conversion-function-id
3775 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3776 keyword, in a construct like `A::template ...'.
3778 Returns a representation of unqualified-id. For the `identifier'
3779 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3780 production a BIT_NOT_EXPR is returned; the operand of the
3781 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3782 other productions, see the documentation accompanying the
3783 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3784 names are looked up in uninstantiated templates. If DECLARATOR_P
3785 is true, the unqualified-id is appearing as part of a declarator,
3786 rather than as part of an expression. */
3789 cp_parser_unqualified_id (cp_parser* parser,
3790 bool template_keyword_p,
3791 bool check_dependency_p,
3797 /* Peek at the next token. */
3798 token = cp_lexer_peek_token (parser->lexer);
3800 switch (token->type)
3806 /* We don't know yet whether or not this will be a
3808 cp_parser_parse_tentatively (parser);
3809 /* Try a template-id. */
3810 id = cp_parser_template_id (parser, template_keyword_p,
3813 /* If it worked, we're done. */
3814 if (cp_parser_parse_definitely (parser))
3816 /* Otherwise, it's an ordinary identifier. */
3817 return cp_parser_identifier (parser);
3820 case CPP_TEMPLATE_ID:
3821 return cp_parser_template_id (parser, template_keyword_p,
3828 tree qualifying_scope;
3833 /* Consume the `~' token. */
3834 cp_lexer_consume_token (parser->lexer);
3835 /* Parse the class-name. The standard, as written, seems to
3838 template <typename T> struct S { ~S (); };
3839 template <typename T> S<T>::~S() {}
3841 is invalid, since `~' must be followed by a class-name, but
3842 `S<T>' is dependent, and so not known to be a class.
3843 That's not right; we need to look in uninstantiated
3844 templates. A further complication arises from:
3846 template <typename T> void f(T t) {
3850 Here, it is not possible to look up `T' in the scope of `T'
3851 itself. We must look in both the current scope, and the
3852 scope of the containing complete expression.
3854 Yet another issue is:
3863 The standard does not seem to say that the `S' in `~S'
3864 should refer to the type `S' and not the data member
3867 /* DR 244 says that we look up the name after the "~" in the
3868 same scope as we looked up the qualifying name. That idea
3869 isn't fully worked out; it's more complicated than that. */
3870 scope = parser->scope;
3871 object_scope = parser->object_scope;
3872 qualifying_scope = parser->qualifying_scope;
3874 /* Check for invalid scopes. */
3875 if (scope == error_mark_node)
3877 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3878 cp_lexer_consume_token (parser->lexer);
3879 return error_mark_node;
3881 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3883 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3884 error_at (token->location,
3885 "scope %qT before %<~%> is not a class-name",
3887 cp_parser_simulate_error (parser);
3888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3889 cp_lexer_consume_token (parser->lexer);
3890 return error_mark_node;
3892 gcc_assert (!scope || TYPE_P (scope));
3894 /* If the name is of the form "X::~X" it's OK. */
3895 token = cp_lexer_peek_token (parser->lexer);
3897 && token->type == CPP_NAME
3898 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3900 && constructor_name_p (token->u.value, scope))
3902 cp_lexer_consume_token (parser->lexer);
3903 return build_nt (BIT_NOT_EXPR, scope);
3906 /* If there was an explicit qualification (S::~T), first look
3907 in the scope given by the qualification (i.e., S).
3909 Note: in the calls to cp_parser_class_name below we pretend that
3910 the lookup had an explicit 'class' tag so that lookup finds the
3911 injected-class-name rather than the constructor. */
3913 type_decl = NULL_TREE;
3916 cp_parser_parse_tentatively (parser);
3917 type_decl = cp_parser_class_name (parser,
3918 /*typename_keyword_p=*/false,
3919 /*template_keyword_p=*/false,
3921 /*check_dependency=*/false,
3922 /*class_head_p=*/false,
3924 if (cp_parser_parse_definitely (parser))
3927 /* In "N::S::~S", look in "N" as well. */
3928 if (!done && scope && qualifying_scope)
3930 cp_parser_parse_tentatively (parser);
3931 parser->scope = qualifying_scope;
3932 parser->object_scope = NULL_TREE;
3933 parser->qualifying_scope = NULL_TREE;
3935 = cp_parser_class_name (parser,
3936 /*typename_keyword_p=*/false,
3937 /*template_keyword_p=*/false,
3939 /*check_dependency=*/false,
3940 /*class_head_p=*/false,
3942 if (cp_parser_parse_definitely (parser))
3945 /* In "p->S::~T", look in the scope given by "*p" as well. */
3946 else if (!done && object_scope)
3948 cp_parser_parse_tentatively (parser);
3949 parser->scope = object_scope;
3950 parser->object_scope = NULL_TREE;
3951 parser->qualifying_scope = NULL_TREE;
3953 = cp_parser_class_name (parser,
3954 /*typename_keyword_p=*/false,
3955 /*template_keyword_p=*/false,
3957 /*check_dependency=*/false,
3958 /*class_head_p=*/false,
3960 if (cp_parser_parse_definitely (parser))
3963 /* Look in the surrounding context. */
3966 parser->scope = NULL_TREE;
3967 parser->object_scope = NULL_TREE;
3968 parser->qualifying_scope = NULL_TREE;
3969 if (processing_template_decl)
3970 cp_parser_parse_tentatively (parser);
3972 = cp_parser_class_name (parser,
3973 /*typename_keyword_p=*/false,
3974 /*template_keyword_p=*/false,
3976 /*check_dependency=*/false,
3977 /*class_head_p=*/false,
3979 if (processing_template_decl
3980 && ! cp_parser_parse_definitely (parser))
3982 /* We couldn't find a type with this name, so just accept
3983 it and check for a match at instantiation time. */
3984 type_decl = cp_parser_identifier (parser);
3985 if (type_decl != error_mark_node)
3986 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3990 /* If an error occurred, assume that the name of the
3991 destructor is the same as the name of the qualifying
3992 class. That allows us to keep parsing after running
3993 into ill-formed destructor names. */
3994 if (type_decl == error_mark_node && scope)
3995 return build_nt (BIT_NOT_EXPR, scope);
3996 else if (type_decl == error_mark_node)
3997 return error_mark_node;
3999 /* Check that destructor name and scope match. */
4000 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4002 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4003 error_at (token->location,
4004 "declaration of %<~%T%> as member of %qT",
4006 cp_parser_simulate_error (parser);
4007 return error_mark_node;
4012 A typedef-name that names a class shall not be used as the
4013 identifier in the declarator for a destructor declaration. */
4015 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4016 && !DECL_SELF_REFERENCE_P (type_decl)
4017 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4018 error_at (token->location,
4019 "typedef-name %qD used as destructor declarator",
4022 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4026 if (token->keyword == RID_OPERATOR)
4030 /* This could be a template-id, so we try that first. */
4031 cp_parser_parse_tentatively (parser);
4032 /* Try a template-id. */
4033 id = cp_parser_template_id (parser, template_keyword_p,
4034 /*check_dependency_p=*/true,
4036 /* If that worked, we're done. */
4037 if (cp_parser_parse_definitely (parser))
4039 /* We still don't know whether we're looking at an
4040 operator-function-id or a conversion-function-id. */
4041 cp_parser_parse_tentatively (parser);
4042 /* Try an operator-function-id. */
4043 id = cp_parser_operator_function_id (parser);
4044 /* If that didn't work, try a conversion-function-id. */
4045 if (!cp_parser_parse_definitely (parser))
4046 id = cp_parser_conversion_function_id (parser);
4055 cp_parser_error (parser, "expected unqualified-id");
4056 return error_mark_node;
4060 /* Parse an (optional) nested-name-specifier.
4062 nested-name-specifier: [C++98]
4063 class-or-namespace-name :: nested-name-specifier [opt]
4064 class-or-namespace-name :: template nested-name-specifier [opt]
4066 nested-name-specifier: [C++0x]
4069 nested-name-specifier identifier ::
4070 nested-name-specifier template [opt] simple-template-id ::
4072 PARSER->SCOPE should be set appropriately before this function is
4073 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4074 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4077 Sets PARSER->SCOPE to the class (TYPE) or namespace
4078 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4079 it unchanged if there is no nested-name-specifier. Returns the new
4080 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4082 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4083 part of a declaration and/or decl-specifier. */
4086 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4087 bool typename_keyword_p,
4088 bool check_dependency_p,
4090 bool is_declaration)
4092 bool success = false;
4093 cp_token_position start = 0;
4096 /* Remember where the nested-name-specifier starts. */
4097 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4099 start = cp_lexer_token_position (parser->lexer, false);
4100 push_deferring_access_checks (dk_deferred);
4107 tree saved_qualifying_scope;
4108 bool template_keyword_p;
4110 /* Spot cases that cannot be the beginning of a
4111 nested-name-specifier. */
4112 token = cp_lexer_peek_token (parser->lexer);
4114 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4115 the already parsed nested-name-specifier. */
4116 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4118 /* Grab the nested-name-specifier and continue the loop. */
4119 cp_parser_pre_parsed_nested_name_specifier (parser);
4120 /* If we originally encountered this nested-name-specifier
4121 with IS_DECLARATION set to false, we will not have
4122 resolved TYPENAME_TYPEs, so we must do so here. */
4124 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4126 new_scope = resolve_typename_type (parser->scope,
4127 /*only_current_p=*/false);
4128 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4129 parser->scope = new_scope;
4135 /* Spot cases that cannot be the beginning of a
4136 nested-name-specifier. On the second and subsequent times
4137 through the loop, we look for the `template' keyword. */
4138 if (success && token->keyword == RID_TEMPLATE)
4140 /* A template-id can start a nested-name-specifier. */
4141 else if (token->type == CPP_TEMPLATE_ID)
4145 /* If the next token is not an identifier, then it is
4146 definitely not a type-name or namespace-name. */
4147 if (token->type != CPP_NAME)
4149 /* If the following token is neither a `<' (to begin a
4150 template-id), nor a `::', then we are not looking at a
4151 nested-name-specifier. */
4152 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4153 if (token->type != CPP_SCOPE
4154 && !cp_parser_nth_token_starts_template_argument_list_p
4159 /* The nested-name-specifier is optional, so we parse
4161 cp_parser_parse_tentatively (parser);
4163 /* Look for the optional `template' keyword, if this isn't the
4164 first time through the loop. */
4166 template_keyword_p = cp_parser_optional_template_keyword (parser);
4168 template_keyword_p = false;
4170 /* Save the old scope since the name lookup we are about to do
4171 might destroy it. */
4172 old_scope = parser->scope;
4173 saved_qualifying_scope = parser->qualifying_scope;
4174 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4175 look up names in "X<T>::I" in order to determine that "Y" is
4176 a template. So, if we have a typename at this point, we make
4177 an effort to look through it. */
4179 && !typename_keyword_p
4181 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4182 parser->scope = resolve_typename_type (parser->scope,
4183 /*only_current_p=*/false);
4184 /* Parse the qualifying entity. */
4186 = cp_parser_qualifying_entity (parser,
4192 /* Look for the `::' token. */
4193 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4195 /* If we found what we wanted, we keep going; otherwise, we're
4197 if (!cp_parser_parse_definitely (parser))
4199 bool error_p = false;
4201 /* Restore the OLD_SCOPE since it was valid before the
4202 failed attempt at finding the last
4203 class-or-namespace-name. */
4204 parser->scope = old_scope;
4205 parser->qualifying_scope = saved_qualifying_scope;
4206 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4208 /* If the next token is an identifier, and the one after
4209 that is a `::', then any valid interpretation would have
4210 found a class-or-namespace-name. */
4211 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4212 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4214 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4217 token = cp_lexer_consume_token (parser->lexer);
4220 if (!token->ambiguous_p)
4223 tree ambiguous_decls;
4225 decl = cp_parser_lookup_name (parser, token->u.value,
4227 /*is_template=*/false,
4228 /*is_namespace=*/false,
4229 /*check_dependency=*/true,
4232 if (TREE_CODE (decl) == TEMPLATE_DECL)
4233 error_at (token->location,
4234 "%qD used without template parameters",
4236 else if (ambiguous_decls)
4238 error_at (token->location,
4239 "reference to %qD is ambiguous",
4241 print_candidates (ambiguous_decls);
4242 decl = error_mark_node;
4246 const char* msg = "is not a class or namespace";
4247 if (cxx_dialect != cxx98)
4248 msg = "is not a class, namespace, or enumeration";
4249 cp_parser_name_lookup_error
4250 (parser, token->u.value, decl, msg,
4254 parser->scope = error_mark_node;
4256 /* Treat this as a successful nested-name-specifier
4261 If the name found is not a class-name (clause
4262 _class_) or namespace-name (_namespace.def_), the
4263 program is ill-formed. */
4266 cp_lexer_consume_token (parser->lexer);
4270 /* We've found one valid nested-name-specifier. */
4272 /* Name lookup always gives us a DECL. */
4273 if (TREE_CODE (new_scope) == TYPE_DECL)
4274 new_scope = TREE_TYPE (new_scope);
4275 /* Uses of "template" must be followed by actual templates. */
4276 if (template_keyword_p
4277 && !(CLASS_TYPE_P (new_scope)
4278 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4279 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4280 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4281 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4282 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4283 == TEMPLATE_ID_EXPR)))
4284 permerror (input_location, TYPE_P (new_scope)
4285 ? "%qT is not a template"
4286 : "%qD is not a template",
4288 /* If it is a class scope, try to complete it; we are about to
4289 be looking up names inside the class. */
4290 if (TYPE_P (new_scope)
4291 /* Since checking types for dependency can be expensive,
4292 avoid doing it if the type is already complete. */
4293 && !COMPLETE_TYPE_P (new_scope)
4294 /* Do not try to complete dependent types. */
4295 && !dependent_type_p (new_scope))
4297 new_scope = complete_type (new_scope);
4298 /* If it is a typedef to current class, use the current
4299 class instead, as the typedef won't have any names inside
4301 if (!COMPLETE_TYPE_P (new_scope)
4302 && currently_open_class (new_scope))
4303 new_scope = TYPE_MAIN_VARIANT (new_scope);
4305 /* Make sure we look in the right scope the next time through
4307 parser->scope = new_scope;
4310 /* If parsing tentatively, replace the sequence of tokens that makes
4311 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4312 token. That way, should we re-parse the token stream, we will
4313 not have to repeat the effort required to do the parse, nor will
4314 we issue duplicate error messages. */
4315 if (success && start)
4319 token = cp_lexer_token_at (parser->lexer, start);
4320 /* Reset the contents of the START token. */
4321 token->type = CPP_NESTED_NAME_SPECIFIER;
4322 /* Retrieve any deferred checks. Do not pop this access checks yet
4323 so the memory will not be reclaimed during token replacing below. */
4324 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4325 token->u.tree_check_value->value = parser->scope;
4326 token->u.tree_check_value->checks = get_deferred_access_checks ();
4327 token->u.tree_check_value->qualifying_scope =
4328 parser->qualifying_scope;
4329 token->keyword = RID_MAX;
4331 /* Purge all subsequent tokens. */
4332 cp_lexer_purge_tokens_after (parser->lexer, start);
4336 pop_to_parent_deferring_access_checks ();
4338 return success ? parser->scope : NULL_TREE;
4341 /* Parse a nested-name-specifier. See
4342 cp_parser_nested_name_specifier_opt for details. This function
4343 behaves identically, except that it will an issue an error if no
4344 nested-name-specifier is present. */
4347 cp_parser_nested_name_specifier (cp_parser *parser,
4348 bool typename_keyword_p,
4349 bool check_dependency_p,
4351 bool is_declaration)
4355 /* Look for the nested-name-specifier. */
4356 scope = cp_parser_nested_name_specifier_opt (parser,
4361 /* If it was not present, issue an error message. */
4364 cp_parser_error (parser, "expected nested-name-specifier");
4365 parser->scope = NULL_TREE;
4371 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4372 this is either a class-name or a namespace-name (which corresponds
4373 to the class-or-namespace-name production in the grammar). For
4374 C++0x, it can also be a type-name that refers to an enumeration
4377 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4378 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4379 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4380 TYPE_P is TRUE iff the next name should be taken as a class-name,
4381 even the same name is declared to be another entity in the same
4384 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4385 specified by the class-or-namespace-name. If neither is found the
4386 ERROR_MARK_NODE is returned. */
4389 cp_parser_qualifying_entity (cp_parser *parser,
4390 bool typename_keyword_p,
4391 bool template_keyword_p,
4392 bool check_dependency_p,
4394 bool is_declaration)
4397 tree saved_qualifying_scope;
4398 tree saved_object_scope;
4401 bool successful_parse_p;
4403 /* Before we try to parse the class-name, we must save away the
4404 current PARSER->SCOPE since cp_parser_class_name will destroy
4406 saved_scope = parser->scope;
4407 saved_qualifying_scope = parser->qualifying_scope;
4408 saved_object_scope = parser->object_scope;
4409 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4410 there is no need to look for a namespace-name. */
4411 only_class_p = template_keyword_p
4412 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4414 cp_parser_parse_tentatively (parser);
4415 scope = cp_parser_class_name (parser,
4418 type_p ? class_type : none_type,
4420 /*class_head_p=*/false,
4422 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4423 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4425 && cxx_dialect != cxx98
4426 && !successful_parse_p)
4428 /* Restore the saved scope. */
4429 parser->scope = saved_scope;
4430 parser->qualifying_scope = saved_qualifying_scope;
4431 parser->object_scope = saved_object_scope;
4433 /* Parse tentatively. */
4434 cp_parser_parse_tentatively (parser);
4436 /* Parse a typedef-name or enum-name. */
4437 scope = cp_parser_nonclass_name (parser);
4438 successful_parse_p = cp_parser_parse_definitely (parser);
4440 /* If that didn't work, try for a namespace-name. */
4441 if (!only_class_p && !successful_parse_p)
4443 /* Restore the saved scope. */
4444 parser->scope = saved_scope;
4445 parser->qualifying_scope = saved_qualifying_scope;
4446 parser->object_scope = saved_object_scope;
4447 /* If we are not looking at an identifier followed by the scope
4448 resolution operator, then this is not part of a
4449 nested-name-specifier. (Note that this function is only used
4450 to parse the components of a nested-name-specifier.) */
4451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4452 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4453 return error_mark_node;
4454 scope = cp_parser_namespace_name (parser);
4460 /* Parse a postfix-expression.
4464 postfix-expression [ expression ]
4465 postfix-expression ( expression-list [opt] )
4466 simple-type-specifier ( expression-list [opt] )
4467 typename :: [opt] nested-name-specifier identifier
4468 ( expression-list [opt] )
4469 typename :: [opt] nested-name-specifier template [opt] template-id
4470 ( expression-list [opt] )
4471 postfix-expression . template [opt] id-expression
4472 postfix-expression -> template [opt] id-expression
4473 postfix-expression . pseudo-destructor-name
4474 postfix-expression -> pseudo-destructor-name
4475 postfix-expression ++
4476 postfix-expression --
4477 dynamic_cast < type-id > ( expression )
4478 static_cast < type-id > ( expression )
4479 reinterpret_cast < type-id > ( expression )
4480 const_cast < type-id > ( expression )
4481 typeid ( expression )
4487 ( type-id ) { initializer-list , [opt] }
4489 This extension is a GNU version of the C99 compound-literal
4490 construct. (The C99 grammar uses `type-name' instead of `type-id',
4491 but they are essentially the same concept.)
4493 If ADDRESS_P is true, the postfix expression is the operand of the
4494 `&' operator. CAST_P is true if this expression is the target of a
4497 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4498 class member access expressions [expr.ref].
4500 Returns a representation of the expression. */
4503 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4504 bool member_access_only_p,
4505 cp_id_kind * pidk_return)
4509 cp_id_kind idk = CP_ID_KIND_NONE;
4510 tree postfix_expression = NULL_TREE;
4511 bool is_member_access = false;
4513 /* Peek at the next token. */
4514 token = cp_lexer_peek_token (parser->lexer);
4515 /* Some of the productions are determined by keywords. */
4516 keyword = token->keyword;
4526 const char *saved_message;
4528 /* All of these can be handled in the same way from the point
4529 of view of parsing. Begin by consuming the token
4530 identifying the cast. */
4531 cp_lexer_consume_token (parser->lexer);
4533 /* New types cannot be defined in the cast. */
4534 saved_message = parser->type_definition_forbidden_message;
4535 parser->type_definition_forbidden_message
4536 = "types may not be defined in casts";
4538 /* Look for the opening `<'. */
4539 cp_parser_require (parser, CPP_LESS, "%<<%>");
4540 /* Parse the type to which we are casting. */
4541 type = cp_parser_type_id (parser);
4542 /* Look for the closing `>'. */
4543 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4544 /* Restore the old message. */
4545 parser->type_definition_forbidden_message = saved_message;
4547 /* And the expression which is being cast. */
4548 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4549 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4550 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4552 /* Only type conversions to integral or enumeration types
4553 can be used in constant-expressions. */
4554 if (!cast_valid_in_integral_constant_expression_p (type)
4555 && (cp_parser_non_integral_constant_expression
4557 "a cast to a type other than an integral or "
4558 "enumeration type")))
4559 return error_mark_node;
4565 = build_dynamic_cast (type, expression, tf_warning_or_error);
4569 = build_static_cast (type, expression, tf_warning_or_error);
4573 = build_reinterpret_cast (type, expression,
4574 tf_warning_or_error);
4578 = build_const_cast (type, expression, tf_warning_or_error);
4589 const char *saved_message;
4590 bool saved_in_type_id_in_expr_p;
4592 /* Consume the `typeid' token. */
4593 cp_lexer_consume_token (parser->lexer);
4594 /* Look for the `(' token. */
4595 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4596 /* Types cannot be defined in a `typeid' expression. */
4597 saved_message = parser->type_definition_forbidden_message;
4598 parser->type_definition_forbidden_message
4599 = "types may not be defined in a %<typeid%> expression";
4600 /* We can't be sure yet whether we're looking at a type-id or an
4602 cp_parser_parse_tentatively (parser);
4603 /* Try a type-id first. */
4604 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4605 parser->in_type_id_in_expr_p = true;
4606 type = cp_parser_type_id (parser);
4607 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4608 /* Look for the `)' token. Otherwise, we can't be sure that
4609 we're not looking at an expression: consider `typeid (int
4610 (3))', for example. */
4611 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4612 /* If all went well, simply lookup the type-id. */
4613 if (cp_parser_parse_definitely (parser))
4614 postfix_expression = get_typeid (type);
4615 /* Otherwise, fall back to the expression variant. */
4620 /* Look for an expression. */
4621 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4622 /* Compute its typeid. */
4623 postfix_expression = build_typeid (expression);
4624 /* Look for the `)' token. */
4625 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4627 /* Restore the saved message. */
4628 parser->type_definition_forbidden_message = saved_message;
4629 /* `typeid' may not appear in an integral constant expression. */
4630 if (cp_parser_non_integral_constant_expression(parser,
4631 "%<typeid%> operator"))
4632 return error_mark_node;
4639 /* The syntax permitted here is the same permitted for an
4640 elaborated-type-specifier. */
4641 type = cp_parser_elaborated_type_specifier (parser,
4642 /*is_friend=*/false,
4643 /*is_declaration=*/false);
4644 postfix_expression = cp_parser_functional_cast (parser, type);
4652 /* If the next thing is a simple-type-specifier, we may be
4653 looking at a functional cast. We could also be looking at
4654 an id-expression. So, we try the functional cast, and if
4655 that doesn't work we fall back to the primary-expression. */
4656 cp_parser_parse_tentatively (parser);
4657 /* Look for the simple-type-specifier. */
4658 type = cp_parser_simple_type_specifier (parser,
4659 /*decl_specs=*/NULL,
4660 CP_PARSER_FLAGS_NONE);
4661 /* Parse the cast itself. */
4662 if (!cp_parser_error_occurred (parser))
4664 = cp_parser_functional_cast (parser, type);
4665 /* If that worked, we're done. */
4666 if (cp_parser_parse_definitely (parser))
4669 /* If the functional-cast didn't work out, try a
4670 compound-literal. */
4671 if (cp_parser_allow_gnu_extensions_p (parser)
4672 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4674 VEC(constructor_elt,gc) *initializer_list = NULL;
4675 bool saved_in_type_id_in_expr_p;
4677 cp_parser_parse_tentatively (parser);
4678 /* Consume the `('. */
4679 cp_lexer_consume_token (parser->lexer);
4680 /* Parse the type. */
4681 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4682 parser->in_type_id_in_expr_p = true;
4683 type = cp_parser_type_id (parser);
4684 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4685 /* Look for the `)'. */
4686 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4687 /* Look for the `{'. */
4688 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4689 /* If things aren't going well, there's no need to
4691 if (!cp_parser_error_occurred (parser))
4693 bool non_constant_p;
4694 /* Parse the initializer-list. */
4696 = cp_parser_initializer_list (parser, &non_constant_p);
4697 /* Allow a trailing `,'. */
4698 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4699 cp_lexer_consume_token (parser->lexer);
4700 /* Look for the final `}'. */
4701 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4703 /* If that worked, we're definitely looking at a
4704 compound-literal expression. */
4705 if (cp_parser_parse_definitely (parser))
4707 /* Warn the user that a compound literal is not
4708 allowed in standard C++. */
4709 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4710 /* For simplicity, we disallow compound literals in
4711 constant-expressions. We could
4712 allow compound literals of integer type, whose
4713 initializer was a constant, in constant
4714 expressions. Permitting that usage, as a further
4715 extension, would not change the meaning of any
4716 currently accepted programs. (Of course, as
4717 compound literals are not part of ISO C++, the
4718 standard has nothing to say.) */
4719 if (cp_parser_non_integral_constant_expression
4720 (parser, "non-constant compound literals"))
4722 postfix_expression = error_mark_node;
4725 /* Form the representation of the compound-literal. */
4727 = (finish_compound_literal
4728 (type, build_constructor (init_list_type_node,
4729 initializer_list)));
4734 /* It must be a primary-expression. */
4736 = cp_parser_primary_expression (parser, address_p, cast_p,
4737 /*template_arg_p=*/false,
4743 /* Keep looping until the postfix-expression is complete. */
4746 if (idk == CP_ID_KIND_UNQUALIFIED
4747 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4748 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4749 /* It is not a Koenig lookup function call. */
4751 = unqualified_name_lookup_error (postfix_expression);
4753 /* Peek at the next token. */
4754 token = cp_lexer_peek_token (parser->lexer);
4756 switch (token->type)
4758 case CPP_OPEN_SQUARE:
4760 = cp_parser_postfix_open_square_expression (parser,
4763 idk = CP_ID_KIND_NONE;
4764 is_member_access = false;
4767 case CPP_OPEN_PAREN:
4768 /* postfix-expression ( expression-list [opt] ) */
4771 bool is_builtin_constant_p;
4772 bool saved_integral_constant_expression_p = false;
4773 bool saved_non_integral_constant_expression_p = false;
4776 is_member_access = false;
4778 is_builtin_constant_p
4779 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4780 if (is_builtin_constant_p)
4782 /* The whole point of __builtin_constant_p is to allow
4783 non-constant expressions to appear as arguments. */
4784 saved_integral_constant_expression_p
4785 = parser->integral_constant_expression_p;
4786 saved_non_integral_constant_expression_p
4787 = parser->non_integral_constant_expression_p;
4788 parser->integral_constant_expression_p = false;
4790 args = (cp_parser_parenthesized_expression_list
4791 (parser, /*is_attribute_list=*/false,
4792 /*cast_p=*/false, /*allow_expansion_p=*/true,
4793 /*non_constant_p=*/NULL));
4794 if (is_builtin_constant_p)
4796 parser->integral_constant_expression_p
4797 = saved_integral_constant_expression_p;
4798 parser->non_integral_constant_expression_p
4799 = saved_non_integral_constant_expression_p;
4804 postfix_expression = error_mark_node;
4808 /* Function calls are not permitted in
4809 constant-expressions. */
4810 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4811 && cp_parser_non_integral_constant_expression (parser,
4814 postfix_expression = error_mark_node;
4815 release_tree_vector (args);
4820 if (idk == CP_ID_KIND_UNQUALIFIED
4821 || idk == CP_ID_KIND_TEMPLATE_ID)
4823 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4825 if (!VEC_empty (tree, args))
4828 if (!any_type_dependent_arguments_p (args))
4830 = perform_koenig_lookup (postfix_expression, args);
4834 = unqualified_fn_lookup_error (postfix_expression);
4836 /* We do not perform argument-dependent lookup if
4837 normal lookup finds a non-function, in accordance
4838 with the expected resolution of DR 218. */
4839 else if (!VEC_empty (tree, args)
4840 && is_overloaded_fn (postfix_expression))
4842 tree fn = get_first_fn (postfix_expression);
4844 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4845 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4847 /* Only do argument dependent lookup if regular
4848 lookup does not find a set of member functions.
4849 [basic.lookup.koenig]/2a */
4850 if (!DECL_FUNCTION_MEMBER_P (fn))
4853 if (!any_type_dependent_arguments_p (args))
4855 = perform_koenig_lookup (postfix_expression, args);
4860 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4862 tree instance = TREE_OPERAND (postfix_expression, 0);
4863 tree fn = TREE_OPERAND (postfix_expression, 1);
4865 if (processing_template_decl
4866 && (type_dependent_expression_p (instance)
4867 || (!BASELINK_P (fn)
4868 && TREE_CODE (fn) != FIELD_DECL)
4869 || type_dependent_expression_p (fn)
4870 || any_type_dependent_arguments_p (args)))
4873 = build_nt_call_vec (postfix_expression, args);
4874 release_tree_vector (args);
4878 if (BASELINK_P (fn))
4881 = (build_new_method_call
4882 (instance, fn, &args, NULL_TREE,
4883 (idk == CP_ID_KIND_QUALIFIED
4884 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4886 tf_warning_or_error));
4890 = finish_call_expr (postfix_expression, &args,
4891 /*disallow_virtual=*/false,
4893 tf_warning_or_error);
4895 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4896 || TREE_CODE (postfix_expression) == MEMBER_REF
4897 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4898 postfix_expression = (build_offset_ref_call_from_tree
4899 (postfix_expression, &args));
4900 else if (idk == CP_ID_KIND_QUALIFIED)
4901 /* A call to a static class member, or a namespace-scope
4904 = finish_call_expr (postfix_expression, &args,
4905 /*disallow_virtual=*/true,
4907 tf_warning_or_error);
4909 /* All other function calls. */
4911 = finish_call_expr (postfix_expression, &args,
4912 /*disallow_virtual=*/false,
4914 tf_warning_or_error);
4916 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4917 idk = CP_ID_KIND_NONE;
4919 release_tree_vector (args);
4925 /* postfix-expression . template [opt] id-expression
4926 postfix-expression . pseudo-destructor-name
4927 postfix-expression -> template [opt] id-expression
4928 postfix-expression -> pseudo-destructor-name */
4930 /* Consume the `.' or `->' operator. */
4931 cp_lexer_consume_token (parser->lexer);
4934 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4939 is_member_access = true;
4943 /* postfix-expression ++ */
4944 /* Consume the `++' token. */
4945 cp_lexer_consume_token (parser->lexer);
4946 /* Generate a representation for the complete expression. */
4948 = finish_increment_expr (postfix_expression,
4949 POSTINCREMENT_EXPR);
4950 /* Increments may not appear in constant-expressions. */
4951 if (cp_parser_non_integral_constant_expression (parser,
4953 postfix_expression = error_mark_node;
4954 idk = CP_ID_KIND_NONE;
4955 is_member_access = false;
4958 case CPP_MINUS_MINUS:
4959 /* postfix-expression -- */
4960 /* Consume the `--' token. */
4961 cp_lexer_consume_token (parser->lexer);
4962 /* Generate a representation for the complete expression. */
4964 = finish_increment_expr (postfix_expression,
4965 POSTDECREMENT_EXPR);
4966 /* Decrements may not appear in constant-expressions. */
4967 if (cp_parser_non_integral_constant_expression (parser,
4969 postfix_expression = error_mark_node;
4970 idk = CP_ID_KIND_NONE;
4971 is_member_access = false;
4975 if (pidk_return != NULL)
4976 * pidk_return = idk;
4977 if (member_access_only_p)
4978 return is_member_access? postfix_expression : error_mark_node;
4980 return postfix_expression;
4984 /* We should never get here. */
4986 return error_mark_node;
4989 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4990 by cp_parser_builtin_offsetof. We're looking for
4992 postfix-expression [ expression ]
4994 FOR_OFFSETOF is set if we're being called in that context, which
4995 changes how we deal with integer constant expressions. */
4998 cp_parser_postfix_open_square_expression (cp_parser *parser,
4999 tree postfix_expression,
5004 /* Consume the `[' token. */
5005 cp_lexer_consume_token (parser->lexer);
5007 /* Parse the index expression. */
5008 /* ??? For offsetof, there is a question of what to allow here. If
5009 offsetof is not being used in an integral constant expression context,
5010 then we *could* get the right answer by computing the value at runtime.
5011 If we are in an integral constant expression context, then we might
5012 could accept any constant expression; hard to say without analysis.
5013 Rather than open the barn door too wide right away, allow only integer
5014 constant expressions here. */
5016 index = cp_parser_constant_expression (parser, false, NULL);
5018 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5020 /* Look for the closing `]'. */
5021 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5023 /* Build the ARRAY_REF. */
5024 postfix_expression = grok_array_decl (postfix_expression, index);
5026 /* When not doing offsetof, array references are not permitted in
5027 constant-expressions. */
5029 && (cp_parser_non_integral_constant_expression
5030 (parser, "an array reference")))
5031 postfix_expression = error_mark_node;
5033 return postfix_expression;
5036 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5037 by cp_parser_builtin_offsetof. We're looking for
5039 postfix-expression . template [opt] id-expression
5040 postfix-expression . pseudo-destructor-name
5041 postfix-expression -> template [opt] id-expression
5042 postfix-expression -> pseudo-destructor-name
5044 FOR_OFFSETOF is set if we're being called in that context. That sorta
5045 limits what of the above we'll actually accept, but nevermind.
5046 TOKEN_TYPE is the "." or "->" token, which will already have been
5047 removed from the stream. */
5050 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5051 enum cpp_ttype token_type,
5052 tree postfix_expression,
5053 bool for_offsetof, cp_id_kind *idk,
5054 location_t location)
5058 bool pseudo_destructor_p;
5059 tree scope = NULL_TREE;
5061 /* If this is a `->' operator, dereference the pointer. */
5062 if (token_type == CPP_DEREF)
5063 postfix_expression = build_x_arrow (postfix_expression);
5064 /* Check to see whether or not the expression is type-dependent. */
5065 dependent_p = type_dependent_expression_p (postfix_expression);
5066 /* The identifier following the `->' or `.' is not qualified. */
5067 parser->scope = NULL_TREE;
5068 parser->qualifying_scope = NULL_TREE;
5069 parser->object_scope = NULL_TREE;
5070 *idk = CP_ID_KIND_NONE;
5072 /* Enter the scope corresponding to the type of the object
5073 given by the POSTFIX_EXPRESSION. */
5074 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5076 scope = TREE_TYPE (postfix_expression);
5077 /* According to the standard, no expression should ever have
5078 reference type. Unfortunately, we do not currently match
5079 the standard in this respect in that our internal representation
5080 of an expression may have reference type even when the standard
5081 says it does not. Therefore, we have to manually obtain the
5082 underlying type here. */
5083 scope = non_reference (scope);
5084 /* The type of the POSTFIX_EXPRESSION must be complete. */
5085 if (scope == unknown_type_node)
5087 error_at (location, "%qE does not have class type",
5088 postfix_expression);
5092 scope = complete_type_or_else (scope, NULL_TREE);
5093 /* Let the name lookup machinery know that we are processing a
5094 class member access expression. */
5095 parser->context->object_type = scope;
5096 /* If something went wrong, we want to be able to discern that case,
5097 as opposed to the case where there was no SCOPE due to the type
5098 of expression being dependent. */
5100 scope = error_mark_node;
5101 /* If the SCOPE was erroneous, make the various semantic analysis
5102 functions exit quickly -- and without issuing additional error
5104 if (scope == error_mark_node)
5105 postfix_expression = error_mark_node;
5108 /* Assume this expression is not a pseudo-destructor access. */
5109 pseudo_destructor_p = false;
5111 /* If the SCOPE is a scalar type, then, if this is a valid program,
5112 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5113 is type dependent, it can be pseudo-destructor-name or something else.
5114 Try to parse it as pseudo-destructor-name first. */
5115 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5120 cp_parser_parse_tentatively (parser);
5121 /* Parse the pseudo-destructor-name. */
5123 cp_parser_pseudo_destructor_name (parser, &s, &type);
5125 && (cp_parser_error_occurred (parser)
5126 || TREE_CODE (type) != TYPE_DECL
5127 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5128 cp_parser_abort_tentative_parse (parser);
5129 else if (cp_parser_parse_definitely (parser))
5131 pseudo_destructor_p = true;
5133 = finish_pseudo_destructor_expr (postfix_expression,
5134 s, TREE_TYPE (type));
5138 if (!pseudo_destructor_p)
5140 /* If the SCOPE is not a scalar type, we are looking at an
5141 ordinary class member access expression, rather than a
5142 pseudo-destructor-name. */
5144 cp_token *token = cp_lexer_peek_token (parser->lexer);
5145 /* Parse the id-expression. */
5146 name = (cp_parser_id_expression
5148 cp_parser_optional_template_keyword (parser),
5149 /*check_dependency_p=*/true,
5151 /*declarator_p=*/false,
5152 /*optional_p=*/false));
5153 /* In general, build a SCOPE_REF if the member name is qualified.
5154 However, if the name was not dependent and has already been
5155 resolved; there is no need to build the SCOPE_REF. For example;
5157 struct X { void f(); };
5158 template <typename T> void f(T* t) { t->X::f(); }
5160 Even though "t" is dependent, "X::f" is not and has been resolved
5161 to a BASELINK; there is no need to include scope information. */
5163 /* But we do need to remember that there was an explicit scope for
5164 virtual function calls. */
5166 *idk = CP_ID_KIND_QUALIFIED;
5168 /* If the name is a template-id that names a type, we will get a
5169 TYPE_DECL here. That is invalid code. */
5170 if (TREE_CODE (name) == TYPE_DECL)
5172 error_at (token->location, "invalid use of %qD", name);
5173 postfix_expression = error_mark_node;
5177 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5179 name = build_qualified_name (/*type=*/NULL_TREE,
5183 parser->scope = NULL_TREE;
5184 parser->qualifying_scope = NULL_TREE;
5185 parser->object_scope = NULL_TREE;
5187 if (scope && name && BASELINK_P (name))
5188 adjust_result_of_qualified_name_lookup
5189 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5191 = finish_class_member_access_expr (postfix_expression, name,
5193 tf_warning_or_error);
5197 /* We no longer need to look up names in the scope of the object on
5198 the left-hand side of the `.' or `->' operator. */
5199 parser->context->object_type = NULL_TREE;
5201 /* Outside of offsetof, these operators may not appear in
5202 constant-expressions. */
5204 && (cp_parser_non_integral_constant_expression
5205 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5206 postfix_expression = error_mark_node;
5208 return postfix_expression;
5211 /* Parse a parenthesized expression-list.
5214 assignment-expression
5215 expression-list, assignment-expression
5220 identifier, expression-list
5222 CAST_P is true if this expression is the target of a cast.
5224 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5227 Returns a vector of trees. Each element is a representation of an
5228 assignment-expression. NULL is returned if the ( and or ) are
5229 missing. An empty, but allocated, vector is returned on no
5230 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5231 if this is really an attribute list being parsed. If
5232 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5233 not all of the expressions in the list were constant. */
5235 static VEC(tree,gc) *
5236 cp_parser_parenthesized_expression_list (cp_parser* parser,
5237 bool is_attribute_list,
5239 bool allow_expansion_p,
5240 bool *non_constant_p)
5242 VEC(tree,gc) *expression_list;
5243 bool fold_expr_p = is_attribute_list;
5244 tree identifier = NULL_TREE;
5245 bool saved_greater_than_is_operator_p;
5247 /* Assume all the expressions will be constant. */
5249 *non_constant_p = false;
5251 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5254 expression_list = make_tree_vector ();
5256 /* Within a parenthesized expression, a `>' token is always
5257 the greater-than operator. */
5258 saved_greater_than_is_operator_p
5259 = parser->greater_than_is_operator_p;
5260 parser->greater_than_is_operator_p = true;
5262 /* Consume expressions until there are no more. */
5263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5268 /* At the beginning of attribute lists, check to see if the
5269 next token is an identifier. */
5270 if (is_attribute_list
5271 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5275 /* Consume the identifier. */
5276 token = cp_lexer_consume_token (parser->lexer);
5277 /* Save the identifier. */
5278 identifier = token->u.value;
5282 bool expr_non_constant_p;
5284 /* Parse the next assignment-expression. */
5285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5287 /* A braced-init-list. */
5288 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5289 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5290 if (non_constant_p && expr_non_constant_p)
5291 *non_constant_p = true;
5293 else if (non_constant_p)
5295 expr = (cp_parser_constant_expression
5296 (parser, /*allow_non_constant_p=*/true,
5297 &expr_non_constant_p));
5298 if (expr_non_constant_p)
5299 *non_constant_p = true;
5302 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5305 expr = fold_non_dependent_expr (expr);
5307 /* If we have an ellipsis, then this is an expression
5309 if (allow_expansion_p
5310 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5312 /* Consume the `...'. */
5313 cp_lexer_consume_token (parser->lexer);
5315 /* Build the argument pack. */
5316 expr = make_pack_expansion (expr);
5319 /* Add it to the list. We add error_mark_node
5320 expressions to the list, so that we can still tell if
5321 the correct form for a parenthesized expression-list
5322 is found. That gives better errors. */
5323 VEC_safe_push (tree, gc, expression_list, expr);
5325 if (expr == error_mark_node)
5329 /* After the first item, attribute lists look the same as
5330 expression lists. */
5331 is_attribute_list = false;
5334 /* If the next token isn't a `,', then we are done. */
5335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5338 /* Otherwise, consume the `,' and keep going. */
5339 cp_lexer_consume_token (parser->lexer);
5342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5347 /* We try and resync to an unnested comma, as that will give the
5348 user better diagnostics. */
5349 ending = cp_parser_skip_to_closing_parenthesis (parser,
5350 /*recovering=*/true,
5352 /*consume_paren=*/true);
5357 parser->greater_than_is_operator_p
5358 = saved_greater_than_is_operator_p;
5363 parser->greater_than_is_operator_p
5364 = saved_greater_than_is_operator_p;
5367 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5369 return expression_list;
5372 /* Parse a pseudo-destructor-name.
5374 pseudo-destructor-name:
5375 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5376 :: [opt] nested-name-specifier template template-id :: ~ type-name
5377 :: [opt] nested-name-specifier [opt] ~ type-name
5379 If either of the first two productions is used, sets *SCOPE to the
5380 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5381 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5382 or ERROR_MARK_NODE if the parse fails. */
5385 cp_parser_pseudo_destructor_name (cp_parser* parser,
5389 bool nested_name_specifier_p;
5391 /* Assume that things will not work out. */
5392 *type = error_mark_node;
5394 /* Look for the optional `::' operator. */
5395 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5396 /* Look for the optional nested-name-specifier. */
5397 nested_name_specifier_p
5398 = (cp_parser_nested_name_specifier_opt (parser,
5399 /*typename_keyword_p=*/false,
5400 /*check_dependency_p=*/true,
5402 /*is_declaration=*/false)
5404 /* Now, if we saw a nested-name-specifier, we might be doing the
5405 second production. */
5406 if (nested_name_specifier_p
5407 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5409 /* Consume the `template' keyword. */
5410 cp_lexer_consume_token (parser->lexer);
5411 /* Parse the template-id. */
5412 cp_parser_template_id (parser,
5413 /*template_keyword_p=*/true,
5414 /*check_dependency_p=*/false,
5415 /*is_declaration=*/true);
5416 /* Look for the `::' token. */
5417 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5419 /* If the next token is not a `~', then there might be some
5420 additional qualification. */
5421 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5423 /* At this point, we're looking for "type-name :: ~". The type-name
5424 must not be a class-name, since this is a pseudo-destructor. So,
5425 it must be either an enum-name, or a typedef-name -- both of which
5426 are just identifiers. So, we peek ahead to check that the "::"
5427 and "~" tokens are present; if they are not, then we can avoid
5428 calling type_name. */
5429 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5430 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5431 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5433 cp_parser_error (parser, "non-scalar type");
5437 /* Look for the type-name. */
5438 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5439 if (*scope == error_mark_node)
5442 /* Look for the `::' token. */
5443 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5448 /* Look for the `~'. */
5449 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5450 /* Look for the type-name again. We are not responsible for
5451 checking that it matches the first type-name. */
5452 *type = cp_parser_nonclass_name (parser);
5455 /* Parse a unary-expression.
5461 unary-operator cast-expression
5462 sizeof unary-expression
5470 __extension__ cast-expression
5471 __alignof__ unary-expression
5472 __alignof__ ( type-id )
5473 __real__ cast-expression
5474 __imag__ cast-expression
5477 ADDRESS_P is true iff the unary-expression is appearing as the
5478 operand of the `&' operator. CAST_P is true if this expression is
5479 the target of a cast.
5481 Returns a representation of the expression. */
5484 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5488 enum tree_code unary_operator;
5490 /* Peek at the next token. */
5491 token = cp_lexer_peek_token (parser->lexer);
5492 /* Some keywords give away the kind of expression. */
5493 if (token->type == CPP_KEYWORD)
5495 enum rid keyword = token->keyword;
5505 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5506 /* Consume the token. */
5507 cp_lexer_consume_token (parser->lexer);
5508 /* Parse the operand. */
5509 operand = cp_parser_sizeof_operand (parser, keyword);
5511 if (TYPE_P (operand))
5512 return cxx_sizeof_or_alignof_type (operand, op, true);
5514 return cxx_sizeof_or_alignof_expr (operand, op, true);
5518 return cp_parser_new_expression (parser);
5521 return cp_parser_delete_expression (parser);
5525 /* The saved value of the PEDANTIC flag. */
5529 /* Save away the PEDANTIC flag. */
5530 cp_parser_extension_opt (parser, &saved_pedantic);
5531 /* Parse the cast-expression. */
5532 expr = cp_parser_simple_cast_expression (parser);
5533 /* Restore the PEDANTIC flag. */
5534 pedantic = saved_pedantic;
5544 /* Consume the `__real__' or `__imag__' token. */
5545 cp_lexer_consume_token (parser->lexer);
5546 /* Parse the cast-expression. */
5547 expression = cp_parser_simple_cast_expression (parser);
5548 /* Create the complete representation. */
5549 return build_x_unary_op ((keyword == RID_REALPART
5550 ? REALPART_EXPR : IMAGPART_EXPR),
5552 tf_warning_or_error);
5561 /* Look for the `:: new' and `:: delete', which also signal the
5562 beginning of a new-expression, or delete-expression,
5563 respectively. If the next token is `::', then it might be one of
5565 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5569 /* See if the token after the `::' is one of the keywords in
5570 which we're interested. */
5571 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5572 /* If it's `new', we have a new-expression. */
5573 if (keyword == RID_NEW)
5574 return cp_parser_new_expression (parser);
5575 /* Similarly, for `delete'. */
5576 else if (keyword == RID_DELETE)
5577 return cp_parser_delete_expression (parser);
5580 /* Look for a unary operator. */
5581 unary_operator = cp_parser_unary_operator (token);
5582 /* The `++' and `--' operators can be handled similarly, even though
5583 they are not technically unary-operators in the grammar. */
5584 if (unary_operator == ERROR_MARK)
5586 if (token->type == CPP_PLUS_PLUS)
5587 unary_operator = PREINCREMENT_EXPR;
5588 else if (token->type == CPP_MINUS_MINUS)
5589 unary_operator = PREDECREMENT_EXPR;
5590 /* Handle the GNU address-of-label extension. */
5591 else if (cp_parser_allow_gnu_extensions_p (parser)
5592 && token->type == CPP_AND_AND)
5596 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5598 /* Consume the '&&' token. */
5599 cp_lexer_consume_token (parser->lexer);
5600 /* Look for the identifier. */
5601 identifier = cp_parser_identifier (parser);
5602 /* Create an expression representing the address. */
5603 expression = finish_label_address_expr (identifier, loc);
5604 if (cp_parser_non_integral_constant_expression (parser,
5605 "the address of a label"))
5606 expression = error_mark_node;
5610 if (unary_operator != ERROR_MARK)
5612 tree cast_expression;
5613 tree expression = error_mark_node;
5614 const char *non_constant_p = NULL;
5616 /* Consume the operator token. */
5617 token = cp_lexer_consume_token (parser->lexer);
5618 /* Parse the cast-expression. */
5620 = cp_parser_cast_expression (parser,
5621 unary_operator == ADDR_EXPR,
5622 /*cast_p=*/false, pidk);
5623 /* Now, build an appropriate representation. */
5624 switch (unary_operator)
5627 non_constant_p = "%<*%>";
5628 expression = build_x_indirect_ref (cast_expression, "unary *",
5629 tf_warning_or_error);
5633 non_constant_p = "%<&%>";
5636 expression = build_x_unary_op (unary_operator, cast_expression,
5637 tf_warning_or_error);
5640 case PREINCREMENT_EXPR:
5641 case PREDECREMENT_EXPR:
5642 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5643 ? "%<++%>" : "%<--%>");
5645 case UNARY_PLUS_EXPR:
5647 case TRUTH_NOT_EXPR:
5648 expression = finish_unary_op_expr (unary_operator, cast_expression);
5656 && cp_parser_non_integral_constant_expression (parser,
5658 expression = error_mark_node;
5663 return cp_parser_postfix_expression (parser, address_p, cast_p,
5664 /*member_access_only_p=*/false,
5668 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5669 unary-operator, the corresponding tree code is returned. */
5671 static enum tree_code
5672 cp_parser_unary_operator (cp_token* token)
5674 switch (token->type)
5677 return INDIRECT_REF;
5683 return UNARY_PLUS_EXPR;
5689 return TRUTH_NOT_EXPR;
5692 return BIT_NOT_EXPR;
5699 /* Parse a new-expression.
5702 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5703 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5705 Returns a representation of the expression. */
5708 cp_parser_new_expression (cp_parser* parser)
5710 bool global_scope_p;
5711 VEC(tree,gc) *placement;
5713 VEC(tree,gc) *initializer;
5717 /* Look for the optional `::' operator. */
5719 = (cp_parser_global_scope_opt (parser,
5720 /*current_scope_valid_p=*/false)
5722 /* Look for the `new' operator. */
5723 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5724 /* There's no easy way to tell a new-placement from the
5725 `( type-id )' construct. */
5726 cp_parser_parse_tentatively (parser);
5727 /* Look for a new-placement. */
5728 placement = cp_parser_new_placement (parser);
5729 /* If that didn't work out, there's no new-placement. */
5730 if (!cp_parser_parse_definitely (parser))
5732 if (placement != NULL)
5733 release_tree_vector (placement);
5737 /* If the next token is a `(', then we have a parenthesized
5739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5742 /* Consume the `('. */
5743 cp_lexer_consume_token (parser->lexer);
5744 /* Parse the type-id. */
5745 type = cp_parser_type_id (parser);
5746 /* Look for the closing `)'. */
5747 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5748 token = cp_lexer_peek_token (parser->lexer);
5749 /* There should not be a direct-new-declarator in this production,
5750 but GCC used to allowed this, so we check and emit a sensible error
5751 message for this case. */
5752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5754 error_at (token->location,
5755 "array bound forbidden after parenthesized type-id");
5756 inform (token->location,
5757 "try removing the parentheses around the type-id");
5758 cp_parser_direct_new_declarator (parser);
5762 /* Otherwise, there must be a new-type-id. */
5764 type = cp_parser_new_type_id (parser, &nelts);
5766 /* If the next token is a `(' or '{', then we have a new-initializer. */
5767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5768 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5769 initializer = cp_parser_new_initializer (parser);
5773 /* A new-expression may not appear in an integral constant
5775 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5776 ret = error_mark_node;
5779 /* Create a representation of the new-expression. */
5780 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5781 tf_warning_or_error);
5784 if (placement != NULL)
5785 release_tree_vector (placement);
5786 if (initializer != NULL)
5787 release_tree_vector (initializer);
5792 /* Parse a new-placement.
5797 Returns the same representation as for an expression-list. */
5799 static VEC(tree,gc) *
5800 cp_parser_new_placement (cp_parser* parser)
5802 VEC(tree,gc) *expression_list;
5804 /* Parse the expression-list. */
5805 expression_list = (cp_parser_parenthesized_expression_list
5806 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5807 /*non_constant_p=*/NULL));
5809 return expression_list;
5812 /* Parse a new-type-id.
5815 type-specifier-seq new-declarator [opt]
5817 Returns the TYPE allocated. If the new-type-id indicates an array
5818 type, *NELTS is set to the number of elements in the last array
5819 bound; the TYPE will not include the last array bound. */
5822 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5824 cp_decl_specifier_seq type_specifier_seq;
5825 cp_declarator *new_declarator;
5826 cp_declarator *declarator;
5827 cp_declarator *outer_declarator;
5828 const char *saved_message;
5831 /* The type-specifier sequence must not contain type definitions.
5832 (It cannot contain declarations of new types either, but if they
5833 are not definitions we will catch that because they are not
5835 saved_message = parser->type_definition_forbidden_message;
5836 parser->type_definition_forbidden_message
5837 = "types may not be defined in a new-type-id";
5838 /* Parse the type-specifier-seq. */
5839 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5840 /*is_trailing_return=*/false,
5841 &type_specifier_seq);
5842 /* Restore the old message. */
5843 parser->type_definition_forbidden_message = saved_message;
5844 /* Parse the new-declarator. */
5845 new_declarator = cp_parser_new_declarator_opt (parser);
5847 /* Determine the number of elements in the last array dimension, if
5850 /* Skip down to the last array dimension. */
5851 declarator = new_declarator;
5852 outer_declarator = NULL;
5853 while (declarator && (declarator->kind == cdk_pointer
5854 || declarator->kind == cdk_ptrmem))
5856 outer_declarator = declarator;
5857 declarator = declarator->declarator;
5860 && declarator->kind == cdk_array
5861 && declarator->declarator
5862 && declarator->declarator->kind == cdk_array)
5864 outer_declarator = declarator;
5865 declarator = declarator->declarator;
5868 if (declarator && declarator->kind == cdk_array)
5870 *nelts = declarator->u.array.bounds;
5871 if (*nelts == error_mark_node)
5872 *nelts = integer_one_node;
5874 if (outer_declarator)
5875 outer_declarator->declarator = declarator->declarator;
5877 new_declarator = NULL;
5880 type = groktypename (&type_specifier_seq, new_declarator, false);
5884 /* Parse an (optional) new-declarator.
5887 ptr-operator new-declarator [opt]
5888 direct-new-declarator
5890 Returns the declarator. */
5892 static cp_declarator *
5893 cp_parser_new_declarator_opt (cp_parser* parser)
5895 enum tree_code code;
5897 cp_cv_quals cv_quals;
5899 /* We don't know if there's a ptr-operator next, or not. */
5900 cp_parser_parse_tentatively (parser);
5901 /* Look for a ptr-operator. */
5902 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5903 /* If that worked, look for more new-declarators. */
5904 if (cp_parser_parse_definitely (parser))
5906 cp_declarator *declarator;
5908 /* Parse another optional declarator. */
5909 declarator = cp_parser_new_declarator_opt (parser);
5911 return cp_parser_make_indirect_declarator
5912 (code, type, cv_quals, declarator);
5915 /* If the next token is a `[', there is a direct-new-declarator. */
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5917 return cp_parser_direct_new_declarator (parser);
5922 /* Parse a direct-new-declarator.
5924 direct-new-declarator:
5926 direct-new-declarator [constant-expression]
5930 static cp_declarator *
5931 cp_parser_direct_new_declarator (cp_parser* parser)
5933 cp_declarator *declarator = NULL;
5939 /* Look for the opening `['. */
5940 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5941 /* The first expression is not required to be constant. */
5944 cp_token *token = cp_lexer_peek_token (parser->lexer);
5945 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5946 /* The standard requires that the expression have integral
5947 type. DR 74 adds enumeration types. We believe that the
5948 real intent is that these expressions be handled like the
5949 expression in a `switch' condition, which also allows
5950 classes with a single conversion to integral or
5951 enumeration type. */
5952 if (!processing_template_decl)
5955 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5960 error_at (token->location,
5961 "expression in new-declarator must have integral "
5962 "or enumeration type");
5963 expression = error_mark_node;
5967 /* But all the other expressions must be. */
5970 = cp_parser_constant_expression (parser,
5971 /*allow_non_constant=*/false,
5973 /* Look for the closing `]'. */
5974 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5976 /* Add this bound to the declarator. */
5977 declarator = make_array_declarator (declarator, expression);
5979 /* If the next token is not a `[', then there are no more
5981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5988 /* Parse a new-initializer.
5991 ( expression-list [opt] )
5994 Returns a representation of the expression-list. */
5996 static VEC(tree,gc) *
5997 cp_parser_new_initializer (cp_parser* parser)
5999 VEC(tree,gc) *expression_list;
6001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6004 bool expr_non_constant_p;
6005 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6006 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6007 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6008 expression_list = make_tree_vector_single (t);
6011 expression_list = (cp_parser_parenthesized_expression_list
6012 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6013 /*non_constant_p=*/NULL));
6015 return expression_list;
6018 /* Parse a delete-expression.
6021 :: [opt] delete cast-expression
6022 :: [opt] delete [ ] cast-expression
6024 Returns a representation of the expression. */
6027 cp_parser_delete_expression (cp_parser* parser)
6029 bool global_scope_p;
6033 /* Look for the optional `::' operator. */
6035 = (cp_parser_global_scope_opt (parser,
6036 /*current_scope_valid_p=*/false)
6038 /* Look for the `delete' keyword. */
6039 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6040 /* See if the array syntax is in use. */
6041 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6043 /* Consume the `[' token. */
6044 cp_lexer_consume_token (parser->lexer);
6045 /* Look for the `]' token. */
6046 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6047 /* Remember that this is the `[]' construct. */
6053 /* Parse the cast-expression. */
6054 expression = cp_parser_simple_cast_expression (parser);
6056 /* A delete-expression may not appear in an integral constant
6058 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6059 return error_mark_node;
6061 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6064 /* Returns true if TOKEN may start a cast-expression and false
6068 cp_parser_token_starts_cast_expression (cp_token *token)
6070 switch (token->type)
6076 case CPP_CLOSE_SQUARE:
6077 case CPP_CLOSE_PAREN:
6078 case CPP_CLOSE_BRACE:
6082 case CPP_DEREF_STAR:
6090 case CPP_GREATER_EQ:
6110 /* '[' may start a primary-expression in obj-c++. */
6111 case CPP_OPEN_SQUARE:
6112 return c_dialect_objc ();
6119 /* Parse a cast-expression.
6123 ( type-id ) cast-expression
6125 ADDRESS_P is true iff the unary-expression is appearing as the
6126 operand of the `&' operator. CAST_P is true if this expression is
6127 the target of a cast.
6129 Returns a representation of the expression. */
6132 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6135 /* If it's a `(', then we might be looking at a cast. */
6136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6138 tree type = NULL_TREE;
6139 tree expr = NULL_TREE;
6140 bool compound_literal_p;
6141 const char *saved_message;
6143 /* There's no way to know yet whether or not this is a cast.
6144 For example, `(int (3))' is a unary-expression, while `(int)
6145 3' is a cast. So, we resort to parsing tentatively. */
6146 cp_parser_parse_tentatively (parser);
6147 /* Types may not be defined in a cast. */
6148 saved_message = parser->type_definition_forbidden_message;
6149 parser->type_definition_forbidden_message
6150 = "types may not be defined in casts";
6151 /* Consume the `('. */
6152 cp_lexer_consume_token (parser->lexer);
6153 /* A very tricky bit is that `(struct S) { 3 }' is a
6154 compound-literal (which we permit in C++ as an extension).
6155 But, that construct is not a cast-expression -- it is a
6156 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6157 is legal; if the compound-literal were a cast-expression,
6158 you'd need an extra set of parentheses.) But, if we parse
6159 the type-id, and it happens to be a class-specifier, then we
6160 will commit to the parse at that point, because we cannot
6161 undo the action that is done when creating a new class. So,
6162 then we cannot back up and do a postfix-expression.
6164 Therefore, we scan ahead to the closing `)', and check to see
6165 if the token after the `)' is a `{'. If so, we are not
6166 looking at a cast-expression.
6168 Save tokens so that we can put them back. */
6169 cp_lexer_save_tokens (parser->lexer);
6170 /* Skip tokens until the next token is a closing parenthesis.
6171 If we find the closing `)', and the next token is a `{', then
6172 we are looking at a compound-literal. */
6174 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6175 /*consume_paren=*/true)
6176 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6177 /* Roll back the tokens we skipped. */
6178 cp_lexer_rollback_tokens (parser->lexer);
6179 /* If we were looking at a compound-literal, simulate an error
6180 so that the call to cp_parser_parse_definitely below will
6182 if (compound_literal_p)
6183 cp_parser_simulate_error (parser);
6186 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6187 parser->in_type_id_in_expr_p = true;
6188 /* Look for the type-id. */
6189 type = cp_parser_type_id (parser);
6190 /* Look for the closing `)'. */
6191 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6192 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6195 /* Restore the saved message. */
6196 parser->type_definition_forbidden_message = saved_message;
6198 /* At this point this can only be either a cast or a
6199 parenthesized ctor such as `(T ())' that looks like a cast to
6200 function returning T. */
6201 if (!cp_parser_error_occurred (parser)
6202 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6205 cp_parser_parse_definitely (parser);
6206 expr = cp_parser_cast_expression (parser,
6207 /*address_p=*/false,
6208 /*cast_p=*/true, pidk);
6210 /* Warn about old-style casts, if so requested. */
6211 if (warn_old_style_cast
6212 && !in_system_header
6213 && !VOID_TYPE_P (type)
6214 && current_lang_name != lang_name_c)
6215 warning (OPT_Wold_style_cast, "use of old-style cast");
6217 /* Only type conversions to integral or enumeration types
6218 can be used in constant-expressions. */
6219 if (!cast_valid_in_integral_constant_expression_p (type)
6220 && (cp_parser_non_integral_constant_expression
6222 "a cast to a type other than an integral or "
6223 "enumeration type")))
6224 return error_mark_node;
6226 /* Perform the cast. */
6227 expr = build_c_cast (input_location, type, expr);
6231 cp_parser_abort_tentative_parse (parser);
6234 /* If we get here, then it's not a cast, so it must be a
6235 unary-expression. */
6236 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6239 /* Parse a binary expression of the general form:
6243 pm-expression .* cast-expression
6244 pm-expression ->* cast-expression
6246 multiplicative-expression:
6248 multiplicative-expression * pm-expression
6249 multiplicative-expression / pm-expression
6250 multiplicative-expression % pm-expression
6252 additive-expression:
6253 multiplicative-expression
6254 additive-expression + multiplicative-expression
6255 additive-expression - multiplicative-expression
6259 shift-expression << additive-expression
6260 shift-expression >> additive-expression
6262 relational-expression:
6264 relational-expression < shift-expression
6265 relational-expression > shift-expression
6266 relational-expression <= shift-expression
6267 relational-expression >= shift-expression
6271 relational-expression:
6272 relational-expression <? shift-expression
6273 relational-expression >? shift-expression
6275 equality-expression:
6276 relational-expression
6277 equality-expression == relational-expression
6278 equality-expression != relational-expression
6282 and-expression & equality-expression
6284 exclusive-or-expression:
6286 exclusive-or-expression ^ and-expression
6288 inclusive-or-expression:
6289 exclusive-or-expression
6290 inclusive-or-expression | exclusive-or-expression
6292 logical-and-expression:
6293 inclusive-or-expression
6294 logical-and-expression && inclusive-or-expression
6296 logical-or-expression:
6297 logical-and-expression
6298 logical-or-expression || logical-and-expression
6300 All these are implemented with a single function like:
6303 simple-cast-expression
6304 binary-expression <token> binary-expression
6306 CAST_P is true if this expression is the target of a cast.
6308 The binops_by_token map is used to get the tree codes for each <token> type.
6309 binary-expressions are associated according to a precedence table. */
6311 #define TOKEN_PRECEDENCE(token) \
6312 (((token->type == CPP_GREATER \
6313 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6314 && !parser->greater_than_is_operator_p) \
6315 ? PREC_NOT_OPERATOR \
6316 : binops_by_token[token->type].prec)
6319 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6320 bool no_toplevel_fold_p,
6321 enum cp_parser_prec prec,
6324 cp_parser_expression_stack stack;
6325 cp_parser_expression_stack_entry *sp = &stack[0];
6328 enum tree_code tree_type, lhs_type, rhs_type;
6329 enum cp_parser_prec new_prec, lookahead_prec;
6332 /* Parse the first expression. */
6333 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6334 lhs_type = ERROR_MARK;
6338 /* Get an operator token. */
6339 token = cp_lexer_peek_token (parser->lexer);
6341 if (warn_cxx0x_compat
6342 && token->type == CPP_RSHIFT
6343 && !parser->greater_than_is_operator_p)
6345 if (warning_at (token->location, OPT_Wc__0x_compat,
6346 "%<>>%> operator will be treated as"
6347 " two right angle brackets in C++0x"))
6348 inform (token->location,
6349 "suggest parentheses around %<>>%> expression");
6352 new_prec = TOKEN_PRECEDENCE (token);
6354 /* Popping an entry off the stack means we completed a subexpression:
6355 - either we found a token which is not an operator (`>' where it is not
6356 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6357 will happen repeatedly;
6358 - or, we found an operator which has lower priority. This is the case
6359 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6361 if (new_prec <= prec)
6370 tree_type = binops_by_token[token->type].tree_type;
6372 /* We used the operator token. */
6373 cp_lexer_consume_token (parser->lexer);
6375 /* For "false && x" or "true || x", x will never be executed;
6376 disable warnings while evaluating it. */
6377 if (tree_type == TRUTH_ANDIF_EXPR)
6378 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6379 else if (tree_type == TRUTH_ORIF_EXPR)
6380 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6382 /* Extract another operand. It may be the RHS of this expression
6383 or the LHS of a new, higher priority expression. */
6384 rhs = cp_parser_simple_cast_expression (parser);
6385 rhs_type = ERROR_MARK;
6387 /* Get another operator token. Look up its precedence to avoid
6388 building a useless (immediately popped) stack entry for common
6389 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6390 token = cp_lexer_peek_token (parser->lexer);
6391 lookahead_prec = TOKEN_PRECEDENCE (token);
6392 if (lookahead_prec > new_prec)
6394 /* ... and prepare to parse the RHS of the new, higher priority
6395 expression. Since precedence levels on the stack are
6396 monotonically increasing, we do not have to care about
6399 sp->tree_type = tree_type;
6401 sp->lhs_type = lhs_type;
6404 lhs_type = rhs_type;
6406 new_prec = lookahead_prec;
6410 lookahead_prec = new_prec;
6411 /* If the stack is not empty, we have parsed into LHS the right side
6412 (`4' in the example above) of an expression we had suspended.
6413 We can use the information on the stack to recover the LHS (`3')
6414 from the stack together with the tree code (`MULT_EXPR'), and
6415 the precedence of the higher level subexpression
6416 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6417 which will be used to actually build the additive expression. */
6420 tree_type = sp->tree_type;
6422 rhs_type = lhs_type;
6424 lhs_type = sp->lhs_type;
6427 /* Undo the disabling of warnings done above. */
6428 if (tree_type == TRUTH_ANDIF_EXPR)
6429 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6430 else if (tree_type == TRUTH_ORIF_EXPR)
6431 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6433 overloaded_p = false;
6434 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6435 ERROR_MARK for everything that is not a binary expression.
6436 This makes warn_about_parentheses miss some warnings that
6437 involve unary operators. For unary expressions we should
6438 pass the correct tree_code unless the unary expression was
6439 surrounded by parentheses.
6441 if (no_toplevel_fold_p
6442 && lookahead_prec <= prec
6444 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6445 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6447 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6448 &overloaded_p, tf_warning_or_error);
6449 lhs_type = tree_type;
6451 /* If the binary operator required the use of an overloaded operator,
6452 then this expression cannot be an integral constant-expression.
6453 An overloaded operator can be used even if both operands are
6454 otherwise permissible in an integral constant-expression if at
6455 least one of the operands is of enumeration type. */
6458 && (cp_parser_non_integral_constant_expression
6459 (parser, "calls to overloaded operators")))
6460 return error_mark_node;
6467 /* Parse the `? expression : assignment-expression' part of a
6468 conditional-expression. The LOGICAL_OR_EXPR is the
6469 logical-or-expression that started the conditional-expression.
6470 Returns a representation of the entire conditional-expression.
6472 This routine is used by cp_parser_assignment_expression.
6474 ? expression : assignment-expression
6478 ? : assignment-expression */
6481 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6484 tree assignment_expr;
6486 /* Consume the `?' token. */
6487 cp_lexer_consume_token (parser->lexer);
6488 if (cp_parser_allow_gnu_extensions_p (parser)
6489 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6491 /* Implicit true clause. */
6493 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6497 /* Parse the expression. */
6498 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6499 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6500 c_inhibit_evaluation_warnings +=
6501 ((logical_or_expr == truthvalue_true_node)
6502 - (logical_or_expr == truthvalue_false_node));
6505 /* The next token should be a `:'. */
6506 cp_parser_require (parser, CPP_COLON, "%<:%>");
6507 /* Parse the assignment-expression. */
6508 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6509 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6511 /* Build the conditional-expression. */
6512 return build_x_conditional_expr (logical_or_expr,
6515 tf_warning_or_error);
6518 /* Parse an assignment-expression.
6520 assignment-expression:
6521 conditional-expression
6522 logical-or-expression assignment-operator assignment_expression
6525 CAST_P is true if this expression is the target of a cast.
6527 Returns a representation for the expression. */
6530 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6535 /* If the next token is the `throw' keyword, then we're looking at
6536 a throw-expression. */
6537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6538 expr = cp_parser_throw_expression (parser);
6539 /* Otherwise, it must be that we are looking at a
6540 logical-or-expression. */
6543 /* Parse the binary expressions (logical-or-expression). */
6544 expr = cp_parser_binary_expression (parser, cast_p, false,
6545 PREC_NOT_OPERATOR, pidk);
6546 /* If the next token is a `?' then we're actually looking at a
6547 conditional-expression. */
6548 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6549 return cp_parser_question_colon_clause (parser, expr);
6552 enum tree_code assignment_operator;
6554 /* If it's an assignment-operator, we're using the second
6557 = cp_parser_assignment_operator_opt (parser);
6558 if (assignment_operator != ERROR_MARK)
6560 bool non_constant_p;
6562 /* Parse the right-hand side of the assignment. */
6563 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6565 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6566 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6568 /* An assignment may not appear in a
6569 constant-expression. */
6570 if (cp_parser_non_integral_constant_expression (parser,
6572 return error_mark_node;
6573 /* Build the assignment expression. */
6574 expr = build_x_modify_expr (expr,
6575 assignment_operator,
6577 tf_warning_or_error);
6585 /* Parse an (optional) assignment-operator.
6587 assignment-operator: one of
6588 = *= /= %= += -= >>= <<= &= ^= |=
6592 assignment-operator: one of
6595 If the next token is an assignment operator, the corresponding tree
6596 code is returned, and the token is consumed. For example, for
6597 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6598 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6599 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6600 operator, ERROR_MARK is returned. */
6602 static enum tree_code
6603 cp_parser_assignment_operator_opt (cp_parser* parser)
6608 /* Peek at the next token. */
6609 token = cp_lexer_peek_token (parser->lexer);
6611 switch (token->type)
6622 op = TRUNC_DIV_EXPR;
6626 op = TRUNC_MOD_EXPR;
6658 /* Nothing else is an assignment operator. */
6662 /* If it was an assignment operator, consume it. */
6663 if (op != ERROR_MARK)
6664 cp_lexer_consume_token (parser->lexer);
6669 /* Parse an expression.
6672 assignment-expression
6673 expression , assignment-expression
6675 CAST_P is true if this expression is the target of a cast.
6677 Returns a representation of the expression. */
6680 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6682 tree expression = NULL_TREE;
6686 tree assignment_expression;
6688 /* Parse the next assignment-expression. */
6689 assignment_expression
6690 = cp_parser_assignment_expression (parser, cast_p, pidk);
6691 /* If this is the first assignment-expression, we can just
6694 expression = assignment_expression;
6696 expression = build_x_compound_expr (expression,
6697 assignment_expression,
6698 tf_warning_or_error);
6699 /* If the next token is not a comma, then we are done with the
6701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6703 /* Consume the `,'. */
6704 cp_lexer_consume_token (parser->lexer);
6705 /* A comma operator cannot appear in a constant-expression. */
6706 if (cp_parser_non_integral_constant_expression (parser,
6707 "a comma operator"))
6708 expression = error_mark_node;
6714 /* Parse a constant-expression.
6716 constant-expression:
6717 conditional-expression
6719 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6720 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6721 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6722 is false, NON_CONSTANT_P should be NULL. */
6725 cp_parser_constant_expression (cp_parser* parser,
6726 bool allow_non_constant_p,
6727 bool *non_constant_p)
6729 bool saved_integral_constant_expression_p;
6730 bool saved_allow_non_integral_constant_expression_p;
6731 bool saved_non_integral_constant_expression_p;
6734 /* It might seem that we could simply parse the
6735 conditional-expression, and then check to see if it were
6736 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6737 one that the compiler can figure out is constant, possibly after
6738 doing some simplifications or optimizations. The standard has a
6739 precise definition of constant-expression, and we must honor
6740 that, even though it is somewhat more restrictive.
6746 is not a legal declaration, because `(2, 3)' is not a
6747 constant-expression. The `,' operator is forbidden in a
6748 constant-expression. However, GCC's constant-folding machinery
6749 will fold this operation to an INTEGER_CST for `3'. */
6751 /* Save the old settings. */
6752 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6753 saved_allow_non_integral_constant_expression_p
6754 = parser->allow_non_integral_constant_expression_p;
6755 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6756 /* We are now parsing a constant-expression. */
6757 parser->integral_constant_expression_p = true;
6758 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6759 parser->non_integral_constant_expression_p = false;
6760 /* Although the grammar says "conditional-expression", we parse an
6761 "assignment-expression", which also permits "throw-expression"
6762 and the use of assignment operators. In the case that
6763 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6764 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6765 actually essential that we look for an assignment-expression.
6766 For example, cp_parser_initializer_clauses uses this function to
6767 determine whether a particular assignment-expression is in fact
6769 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6770 /* Restore the old settings. */
6771 parser->integral_constant_expression_p
6772 = saved_integral_constant_expression_p;
6773 parser->allow_non_integral_constant_expression_p
6774 = saved_allow_non_integral_constant_expression_p;
6775 if (allow_non_constant_p)
6776 *non_constant_p = parser->non_integral_constant_expression_p;
6777 else if (parser->non_integral_constant_expression_p)
6778 expression = error_mark_node;
6779 parser->non_integral_constant_expression_p
6780 = saved_non_integral_constant_expression_p;
6785 /* Parse __builtin_offsetof.
6787 offsetof-expression:
6788 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6790 offsetof-member-designator:
6792 | offsetof-member-designator "." id-expression
6793 | offsetof-member-designator "[" expression "]"
6794 | offsetof-member-designator "->" id-expression */
6797 cp_parser_builtin_offsetof (cp_parser *parser)
6799 int save_ice_p, save_non_ice_p;
6804 /* We're about to accept non-integral-constant things, but will
6805 definitely yield an integral constant expression. Save and
6806 restore these values around our local parsing. */
6807 save_ice_p = parser->integral_constant_expression_p;
6808 save_non_ice_p = parser->non_integral_constant_expression_p;
6810 /* Consume the "__builtin_offsetof" token. */
6811 cp_lexer_consume_token (parser->lexer);
6812 /* Consume the opening `('. */
6813 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6814 /* Parse the type-id. */
6815 type = cp_parser_type_id (parser);
6816 /* Look for the `,'. */
6817 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6818 token = cp_lexer_peek_token (parser->lexer);
6820 /* Build the (type *)null that begins the traditional offsetof macro. */
6821 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6822 tf_warning_or_error);
6824 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6825 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6826 true, &dummy, token->location);
6829 token = cp_lexer_peek_token (parser->lexer);
6830 switch (token->type)
6832 case CPP_OPEN_SQUARE:
6833 /* offsetof-member-designator "[" expression "]" */
6834 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6838 /* offsetof-member-designator "->" identifier */
6839 expr = grok_array_decl (expr, integer_zero_node);
6843 /* offsetof-member-designator "." identifier */
6844 cp_lexer_consume_token (parser->lexer);
6845 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6850 case CPP_CLOSE_PAREN:
6851 /* Consume the ")" token. */
6852 cp_lexer_consume_token (parser->lexer);
6856 /* Error. We know the following require will fail, but
6857 that gives the proper error message. */
6858 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6859 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6860 expr = error_mark_node;
6866 /* If we're processing a template, we can't finish the semantics yet.
6867 Otherwise we can fold the entire expression now. */
6868 if (processing_template_decl)
6869 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6871 expr = finish_offsetof (expr);
6874 parser->integral_constant_expression_p = save_ice_p;
6875 parser->non_integral_constant_expression_p = save_non_ice_p;
6880 /* Parse a trait expression. */
6883 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6886 tree type1, type2 = NULL_TREE;
6887 bool binary = false;
6888 cp_decl_specifier_seq decl_specs;
6892 case RID_HAS_NOTHROW_ASSIGN:
6893 kind = CPTK_HAS_NOTHROW_ASSIGN;
6895 case RID_HAS_NOTHROW_CONSTRUCTOR:
6896 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6898 case RID_HAS_NOTHROW_COPY:
6899 kind = CPTK_HAS_NOTHROW_COPY;
6901 case RID_HAS_TRIVIAL_ASSIGN:
6902 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6904 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6905 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6907 case RID_HAS_TRIVIAL_COPY:
6908 kind = CPTK_HAS_TRIVIAL_COPY;
6910 case RID_HAS_TRIVIAL_DESTRUCTOR:
6911 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6913 case RID_HAS_VIRTUAL_DESTRUCTOR:
6914 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6916 case RID_IS_ABSTRACT:
6917 kind = CPTK_IS_ABSTRACT;
6919 case RID_IS_BASE_OF:
6920 kind = CPTK_IS_BASE_OF;
6924 kind = CPTK_IS_CLASS;
6926 case RID_IS_CONVERTIBLE_TO:
6927 kind = CPTK_IS_CONVERTIBLE_TO;
6931 kind = CPTK_IS_EMPTY;
6934 kind = CPTK_IS_ENUM;
6939 case RID_IS_POLYMORPHIC:
6940 kind = CPTK_IS_POLYMORPHIC;
6942 case RID_IS_STD_LAYOUT:
6943 kind = CPTK_IS_STD_LAYOUT;
6945 case RID_IS_TRIVIAL:
6946 kind = CPTK_IS_TRIVIAL;
6949 kind = CPTK_IS_UNION;
6955 /* Consume the token. */
6956 cp_lexer_consume_token (parser->lexer);
6958 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6960 type1 = cp_parser_type_id (parser);
6962 if (type1 == error_mark_node)
6963 return error_mark_node;
6965 /* Build a trivial decl-specifier-seq. */
6966 clear_decl_specs (&decl_specs);
6967 decl_specs.type = type1;
6969 /* Call grokdeclarator to figure out what type this is. */
6970 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6971 /*initialized=*/0, /*attrlist=*/NULL);
6975 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6977 type2 = cp_parser_type_id (parser);
6979 if (type2 == error_mark_node)
6980 return error_mark_node;
6982 /* Build a trivial decl-specifier-seq. */
6983 clear_decl_specs (&decl_specs);
6984 decl_specs.type = type2;
6986 /* Call grokdeclarator to figure out what type this is. */
6987 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6988 /*initialized=*/0, /*attrlist=*/NULL);
6991 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6993 /* Complete the trait expression, which may mean either processing
6994 the trait expr now or saving it for template instantiation. */
6995 return finish_trait_expr (kind, type1, type2);
6998 /* Lambdas that appear in variable initializer or default argument scope
6999 get that in their mangling, so we need to record it. We might as well
7000 use the count for function and namespace scopes as well. */
7001 static GTY(()) tree lambda_scope;
7002 static GTY(()) int lambda_count;
7003 typedef struct GTY(()) tree_int
7008 DEF_VEC_O(tree_int);
7009 DEF_VEC_ALLOC_O(tree_int,gc);
7010 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7013 start_lambda_scope (tree decl)
7017 /* Once we're inside a function, we ignore other scopes and just push
7018 the function again so that popping works properly. */
7019 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7020 decl = current_function_decl;
7021 ti.t = lambda_scope;
7022 ti.i = lambda_count;
7023 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7024 if (lambda_scope != decl)
7026 /* Don't reset the count if we're still in the same function. */
7027 lambda_scope = decl;
7033 record_lambda_scope (tree lambda)
7035 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7036 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7040 finish_lambda_scope (void)
7042 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7043 if (lambda_scope != p->t)
7045 lambda_scope = p->t;
7046 lambda_count = p->i;
7048 VEC_pop (tree_int, lambda_scope_stack);
7051 /* Parse a lambda expression.
7054 lambda-introducer lambda-declarator [opt] compound-statement
7056 Returns a representation of the expression. */
7059 cp_parser_lambda_expression (cp_parser* parser)
7061 tree lambda_expr = build_lambda_expr ();
7064 LAMBDA_EXPR_LOCATION (lambda_expr)
7065 = cp_lexer_peek_token (parser->lexer)->location;
7067 /* We may be in the middle of deferred access check. Disable
7069 push_deferring_access_checks (dk_no_deferred);
7071 type = begin_lambda_type (lambda_expr);
7073 record_lambda_scope (lambda_expr);
7075 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7076 determine_visibility (TYPE_NAME (type));
7079 /* Inside the class, surrounding template-parameter-lists do not apply. */
7080 unsigned int saved_num_template_parameter_lists
7081 = parser->num_template_parameter_lists;
7083 parser->num_template_parameter_lists = 0;
7085 cp_parser_lambda_introducer (parser, lambda_expr);
7087 /* By virtue of defining a local class, a lambda expression has access to
7088 the private variables of enclosing classes. */
7090 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7092 cp_parser_lambda_body (parser, lambda_expr);
7094 /* The capture list was built up in reverse order; fix that now. */
7096 tree newlist = NULL_TREE;
7099 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7102 tree field = TREE_PURPOSE (elt);
7105 next = TREE_CHAIN (elt);
7106 TREE_CHAIN (elt) = newlist;
7109 /* Also add __ to the beginning of the field name so that code
7110 outside the lambda body can't see the captured name. We could
7111 just remove the name entirely, but this is more useful for
7113 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7114 /* The 'this' capture already starts with __. */
7117 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7118 buf[1] = buf[0] = '_';
7119 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7120 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7121 DECL_NAME (field) = get_identifier (buf);
7123 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7126 maybe_add_lambda_conv_op (type);
7128 type = finish_struct (type, /*attributes=*/NULL_TREE);
7130 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7133 pop_deferring_access_checks ();
7135 return build_lambda_object (lambda_expr);
7138 /* Parse the beginning of a lambda expression.
7141 [ lambda-capture [opt] ]
7143 LAMBDA_EXPR is the current representation of the lambda expression. */
7146 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7148 /* Need commas after the first capture. */
7151 /* Eat the leading `['. */
7152 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7154 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7155 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7156 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7157 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7158 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7159 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7161 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7163 cp_lexer_consume_token (parser->lexer);
7167 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7169 cp_token* capture_token;
7171 tree capture_init_expr;
7172 cp_id_kind idk = CP_ID_KIND_NONE;
7173 bool explicit_init_p = false;
7175 enum capture_kind_type
7180 enum capture_kind_type capture_kind = BY_COPY;
7182 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7184 error ("expected end of capture-list");
7191 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7193 /* Possibly capture `this'. */
7194 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7196 cp_lexer_consume_token (parser->lexer);
7197 add_capture (lambda_expr,
7198 /*id=*/get_identifier ("__this"),
7199 /*initializer=*/finish_this_expr(),
7200 /*by_reference_p=*/false,
7205 /* Remember whether we want to capture as a reference or not. */
7206 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7208 capture_kind = BY_REFERENCE;
7209 cp_lexer_consume_token (parser->lexer);
7212 /* Get the identifier. */
7213 capture_token = cp_lexer_peek_token (parser->lexer);
7214 capture_id = cp_parser_identifier (parser);
7216 if (capture_id == error_mark_node)
7217 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7218 delimiters, but I modified this to stop on unnested ']' as well. It
7219 was already changed to stop on unnested '}', so the
7220 "closing_parenthesis" name is no more misleading with my change. */
7222 cp_parser_skip_to_closing_parenthesis (parser,
7223 /*recovering=*/true,
7225 /*consume_paren=*/true);
7229 /* Find the initializer for this capture. */
7230 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7232 /* An explicit expression exists. */
7233 cp_lexer_consume_token (parser->lexer);
7234 pedwarn (input_location, OPT_pedantic,
7235 "ISO C++ does not allow initializers "
7236 "in lambda expression capture lists");
7237 capture_init_expr = cp_parser_assignment_expression (parser,
7240 explicit_init_p = true;
7244 const char* error_msg;
7246 /* Turn the identifier into an id-expression. */
7248 = cp_parser_lookup_name
7252 /*is_template=*/false,
7253 /*is_namespace=*/false,
7254 /*check_dependency=*/true,
7255 /*ambiguous_decls=*/NULL,
7256 capture_token->location);
7259 = finish_id_expression
7264 /*integral_constant_expression_p=*/false,
7265 /*allow_non_integral_constant_expression_p=*/false,
7266 /*non_integral_constant_expression_p=*/NULL,
7267 /*template_p=*/false,
7269 /*address_p=*/false,
7270 /*template_arg_p=*/false,
7272 capture_token->location);
7275 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7277 = unqualified_name_lookup_error (capture_init_expr);
7279 add_capture (lambda_expr,
7282 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7286 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7289 /* Parse the (optional) middle of a lambda expression.
7292 ( parameter-declaration-clause [opt] )
7293 attribute-specifier [opt]
7295 exception-specification [opt]
7296 lambda-return-type-clause [opt]
7298 LAMBDA_EXPR is the current representation of the lambda expression. */
7301 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7303 /* 5.1.1.4 of the standard says:
7304 If a lambda-expression does not include a lambda-declarator, it is as if
7305 the lambda-declarator were ().
7306 This means an empty parameter list, no attributes, and no exception
7308 tree param_list = void_list_node;
7309 tree attributes = NULL_TREE;
7310 tree exception_spec = NULL_TREE;
7313 /* The lambda-declarator is optional, but must begin with an opening
7314 parenthesis if present. */
7315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7317 cp_lexer_consume_token (parser->lexer);
7319 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7321 /* Parse parameters. */
7322 param_list = cp_parser_parameter_declaration_clause (parser);
7324 /* Default arguments shall not be specified in the
7325 parameter-declaration-clause of a lambda-declarator. */
7326 for (t = param_list; t; t = TREE_CHAIN (t))
7327 if (TREE_PURPOSE (t))
7328 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7329 "default argument specified for lambda parameter");
7331 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7333 attributes = cp_parser_attributes_opt (parser);
7335 /* Parse optional `mutable' keyword. */
7336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7338 cp_lexer_consume_token (parser->lexer);
7339 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7342 /* Parse optional exception specification. */
7343 exception_spec = cp_parser_exception_specification_opt (parser);
7345 /* Parse optional trailing return type. */
7346 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7348 cp_lexer_consume_token (parser->lexer);
7349 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7352 /* The function parameters must be in scope all the way until after the
7353 trailing-return-type in case of decltype. */
7354 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7355 pop_binding (DECL_NAME (t), t);
7360 /* Create the function call operator.
7362 Messing with declarators like this is no uglier than building up the
7363 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7366 cp_decl_specifier_seq return_type_specs;
7367 cp_declarator* declarator;
7372 clear_decl_specs (&return_type_specs);
7373 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7374 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7376 /* Maybe we will deduce the return type later, but we can use void
7377 as a placeholder return type anyways. */
7378 return_type_specs.type = void_type_node;
7380 p = obstack_alloc (&declarator_obstack, 0);
7382 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7385 quals = TYPE_UNQUALIFIED;
7386 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7387 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7389 /* A lambda with no captures has a static op() and a conversion op
7390 to function type. */
7391 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7392 error ("lambda expression with no captures declared mutable");
7393 return_type_specs.storage_class = sc_static;
7395 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7396 quals = TYPE_QUAL_CONST;
7397 declarator = make_call_declarator (declarator, param_list, quals,
7399 /*late_return_type=*/NULL_TREE);
7401 fco = grokmethod (&return_type_specs,
7404 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7405 DECL_ARTIFICIAL (fco) = 1;
7407 finish_member_declaration (fco);
7409 obstack_free (&declarator_obstack, p);
7413 /* Parse the body of a lambda expression, which is simply
7417 but which requires special handling.
7418 LAMBDA_EXPR is the current representation of the lambda expression. */
7421 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7423 bool nested = (current_function_decl != NULL_TREE);
7425 push_function_context ();
7427 /* Finish the function call operator
7429 + late_parsing_for_member
7430 + function_definition_after_declarator
7431 + ctor_initializer_opt_and_function_body */
7433 tree fco = lambda_function (lambda_expr);
7437 /* Let the front end know that we are going to be defining this
7439 start_preparsed_function (fco,
7441 SF_PRE_PARSED | SF_INCLASS_INLINE);
7443 start_lambda_scope (fco);
7444 body = begin_function_body ();
7446 /* 5.1.1.4 of the standard says:
7447 If a lambda-expression does not include a trailing-return-type, it
7448 is as if the trailing-return-type denotes the following type:
7449 * if the compound-statement is of the form
7450 { return attribute-specifier [opt] expression ; }
7451 the type of the returned expression after lvalue-to-rvalue
7452 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7453 (_conv.array_ 4.2), and function-to-pointer conversion
7455 * otherwise, void. */
7457 /* In a lambda that has neither a lambda-return-type-clause
7458 nor a deducible form, errors should be reported for return statements
7459 in the body. Since we used void as the placeholder return type, parsing
7460 the body as usual will give such desired behavior. */
7461 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7462 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7463 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7464 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7467 tree expr = NULL_TREE;
7468 cp_id_kind idk = CP_ID_KIND_NONE;
7470 /* Parse tentatively in case there's more after the initial return
7472 cp_parser_parse_tentatively (parser);
7474 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7475 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7477 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7479 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7480 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7482 if (cp_parser_parse_definitely (parser))
7484 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7486 compound_stmt = begin_compound_stmt (0);
7487 /* Will get error here if type not deduced yet. */
7488 finish_return_stmt (expr);
7489 finish_compound_stmt (compound_stmt);
7497 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7498 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7499 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7500 cp_parser_compound_stmt does not pass it. */
7501 cp_parser_function_body (parser);
7502 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7505 finish_function_body (body);
7506 finish_lambda_scope ();
7508 /* Finish the function and generate code for it if necessary. */
7509 expand_or_defer_fn (finish_function (/*inline*/2));
7513 pop_function_context();
7516 /* Statements [gram.stmt.stmt] */
7518 /* Parse a statement.
7522 expression-statement
7527 declaration-statement
7530 IN_COMPOUND is true when the statement is nested inside a
7531 cp_parser_compound_statement; this matters for certain pragmas.
7533 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7534 is a (possibly labeled) if statement which is not enclosed in braces
7535 and has an else clause. This is used to implement -Wparentheses. */
7538 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7539 bool in_compound, bool *if_p)
7543 location_t statement_location;
7548 /* There is no statement yet. */
7549 statement = NULL_TREE;
7550 /* Peek at the next token. */
7551 token = cp_lexer_peek_token (parser->lexer);
7552 /* Remember the location of the first token in the statement. */
7553 statement_location = token->location;
7554 /* If this is a keyword, then that will often determine what kind of
7555 statement we have. */
7556 if (token->type == CPP_KEYWORD)
7558 enum rid keyword = token->keyword;
7564 /* Looks like a labeled-statement with a case label.
7565 Parse the label, and then use tail recursion to parse
7567 cp_parser_label_for_labeled_statement (parser);
7572 statement = cp_parser_selection_statement (parser, if_p);
7578 statement = cp_parser_iteration_statement (parser);
7585 statement = cp_parser_jump_statement (parser);
7588 /* Objective-C++ exception-handling constructs. */
7591 case RID_AT_FINALLY:
7592 case RID_AT_SYNCHRONIZED:
7594 statement = cp_parser_objc_statement (parser);
7598 statement = cp_parser_try_block (parser);
7602 /* This must be a namespace alias definition. */
7603 cp_parser_declaration_statement (parser);
7607 /* It might be a keyword like `int' that can start a
7608 declaration-statement. */
7612 else if (token->type == CPP_NAME)
7614 /* If the next token is a `:', then we are looking at a
7615 labeled-statement. */
7616 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7617 if (token->type == CPP_COLON)
7619 /* Looks like a labeled-statement with an ordinary label.
7620 Parse the label, and then use tail recursion to parse
7622 cp_parser_label_for_labeled_statement (parser);
7626 /* Anything that starts with a `{' must be a compound-statement. */
7627 else if (token->type == CPP_OPEN_BRACE)
7628 statement = cp_parser_compound_statement (parser, NULL, false);
7629 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7630 a statement all its own. */
7631 else if (token->type == CPP_PRAGMA)
7633 /* Only certain OpenMP pragmas are attached to statements, and thus
7634 are considered statements themselves. All others are not. In
7635 the context of a compound, accept the pragma as a "statement" and
7636 return so that we can check for a close brace. Otherwise we
7637 require a real statement and must go back and read one. */
7639 cp_parser_pragma (parser, pragma_compound);
7640 else if (!cp_parser_pragma (parser, pragma_stmt))
7644 else if (token->type == CPP_EOF)
7646 cp_parser_error (parser, "expected statement");
7650 /* Everything else must be a declaration-statement or an
7651 expression-statement. Try for the declaration-statement
7652 first, unless we are looking at a `;', in which case we know that
7653 we have an expression-statement. */
7656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7658 cp_parser_parse_tentatively (parser);
7659 /* Try to parse the declaration-statement. */
7660 cp_parser_declaration_statement (parser);
7661 /* If that worked, we're done. */
7662 if (cp_parser_parse_definitely (parser))
7665 /* Look for an expression-statement instead. */
7666 statement = cp_parser_expression_statement (parser, in_statement_expr);
7669 /* Set the line number for the statement. */
7670 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7671 SET_EXPR_LOCATION (statement, statement_location);
7674 /* Parse the label for a labeled-statement, i.e.
7677 case constant-expression :
7681 case constant-expression ... constant-expression : statement
7683 When a label is parsed without errors, the label is added to the
7684 parse tree by the finish_* functions, so this function doesn't
7685 have to return the label. */
7688 cp_parser_label_for_labeled_statement (cp_parser* parser)
7691 tree label = NULL_TREE;
7693 /* The next token should be an identifier. */
7694 token = cp_lexer_peek_token (parser->lexer);
7695 if (token->type != CPP_NAME
7696 && token->type != CPP_KEYWORD)
7698 cp_parser_error (parser, "expected labeled-statement");
7702 switch (token->keyword)
7709 /* Consume the `case' token. */
7710 cp_lexer_consume_token (parser->lexer);
7711 /* Parse the constant-expression. */
7712 expr = cp_parser_constant_expression (parser,
7713 /*allow_non_constant_p=*/false,
7716 ellipsis = cp_lexer_peek_token (parser->lexer);
7717 if (ellipsis->type == CPP_ELLIPSIS)
7719 /* Consume the `...' token. */
7720 cp_lexer_consume_token (parser->lexer);
7722 cp_parser_constant_expression (parser,
7723 /*allow_non_constant_p=*/false,
7725 /* We don't need to emit warnings here, as the common code
7726 will do this for us. */
7729 expr_hi = NULL_TREE;
7731 if (parser->in_switch_statement_p)
7732 finish_case_label (token->location, expr, expr_hi);
7734 error_at (token->location,
7735 "case label %qE not within a switch statement",
7741 /* Consume the `default' token. */
7742 cp_lexer_consume_token (parser->lexer);
7744 if (parser->in_switch_statement_p)
7745 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7747 error_at (token->location, "case label not within a switch statement");
7751 /* Anything else must be an ordinary label. */
7752 label = finish_label_stmt (cp_parser_identifier (parser));
7756 /* Require the `:' token. */
7757 cp_parser_require (parser, CPP_COLON, "%<:%>");
7759 /* An ordinary label may optionally be followed by attributes.
7760 However, this is only permitted if the attributes are then
7761 followed by a semicolon. This is because, for backward
7762 compatibility, when parsing
7763 lab: __attribute__ ((unused)) int i;
7764 we want the attribute to attach to "i", not "lab". */
7765 if (label != NULL_TREE
7766 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7770 cp_parser_parse_tentatively (parser);
7771 attrs = cp_parser_attributes_opt (parser);
7772 if (attrs == NULL_TREE
7773 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7774 cp_parser_abort_tentative_parse (parser);
7775 else if (!cp_parser_parse_definitely (parser))
7778 cplus_decl_attributes (&label, attrs, 0);
7782 /* Parse an expression-statement.
7784 expression-statement:
7787 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7788 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7789 indicates whether this expression-statement is part of an
7790 expression statement. */
7793 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7795 tree statement = NULL_TREE;
7796 cp_token *token = cp_lexer_peek_token (parser->lexer);
7798 /* If the next token is a ';', then there is no expression
7800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7801 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7803 /* Give a helpful message for "A<T>::type t;" */
7804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7805 && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7806 && TREE_CODE (statement) == SCOPE_REF)
7807 error_at (token->location, "need %<typename%> before %qE because "
7808 "%qT is a dependent scope",
7809 statement, TREE_OPERAND (statement, 0));
7811 /* Consume the final `;'. */
7812 cp_parser_consume_semicolon_at_end_of_statement (parser);
7814 if (in_statement_expr
7815 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7816 /* This is the final expression statement of a statement
7818 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7820 statement = finish_expr_stmt (statement);
7827 /* Parse a compound-statement.
7830 { statement-seq [opt] }
7835 { label-declaration-seq [opt] statement-seq [opt] }
7837 label-declaration-seq:
7839 label-declaration-seq label-declaration
7841 Returns a tree representing the statement. */
7844 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7849 /* Consume the `{'. */
7850 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7851 return error_mark_node;
7852 /* Begin the compound-statement. */
7853 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7854 /* If the next keyword is `__label__' we have a label declaration. */
7855 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7856 cp_parser_label_declaration (parser);
7857 /* Parse an (optional) statement-seq. */
7858 cp_parser_statement_seq_opt (parser, in_statement_expr);
7859 /* Finish the compound-statement. */
7860 finish_compound_stmt (compound_stmt);
7861 /* Consume the `}'. */
7862 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7864 return compound_stmt;
7867 /* Parse an (optional) statement-seq.
7871 statement-seq [opt] statement */
7874 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7876 /* Scan statements until there aren't any more. */
7879 cp_token *token = cp_lexer_peek_token (parser->lexer);
7881 /* If we're looking at a `}', then we've run out of statements. */
7882 if (token->type == CPP_CLOSE_BRACE
7883 || token->type == CPP_EOF
7884 || token->type == CPP_PRAGMA_EOL)
7887 /* If we are in a compound statement and find 'else' then
7888 something went wrong. */
7889 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7891 if (parser->in_statement & IN_IF_STMT)
7895 token = cp_lexer_consume_token (parser->lexer);
7896 error_at (token->location, "%<else%> without a previous %<if%>");
7900 /* Parse the statement. */
7901 cp_parser_statement (parser, in_statement_expr, true, NULL);
7905 /* Parse a selection-statement.
7907 selection-statement:
7908 if ( condition ) statement
7909 if ( condition ) statement else statement
7910 switch ( condition ) statement
7912 Returns the new IF_STMT or SWITCH_STMT.
7914 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7915 is a (possibly labeled) if statement which is not enclosed in
7916 braces and has an else clause. This is used to implement
7920 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7928 /* Peek at the next token. */
7929 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7931 /* See what kind of keyword it is. */
7932 keyword = token->keyword;
7941 /* Look for the `('. */
7942 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7944 cp_parser_skip_to_end_of_statement (parser);
7945 return error_mark_node;
7948 /* Begin the selection-statement. */
7949 if (keyword == RID_IF)
7950 statement = begin_if_stmt ();
7952 statement = begin_switch_stmt ();
7954 /* Parse the condition. */
7955 condition = cp_parser_condition (parser);
7956 /* Look for the `)'. */
7957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7958 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7959 /*consume_paren=*/true);
7961 if (keyword == RID_IF)
7964 unsigned char in_statement;
7966 /* Add the condition. */
7967 finish_if_stmt_cond (condition, statement);
7969 /* Parse the then-clause. */
7970 in_statement = parser->in_statement;
7971 parser->in_statement |= IN_IF_STMT;
7972 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7975 add_stmt (build_empty_stmt (loc));
7976 cp_lexer_consume_token (parser->lexer);
7977 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7978 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7979 "empty body in an %<if%> statement");
7983 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7984 parser->in_statement = in_statement;
7986 finish_then_clause (statement);
7988 /* If the next token is `else', parse the else-clause. */
7989 if (cp_lexer_next_token_is_keyword (parser->lexer,
7992 /* Consume the `else' keyword. */
7993 cp_lexer_consume_token (parser->lexer);
7994 begin_else_clause (statement);
7995 /* Parse the else-clause. */
7996 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7999 loc = cp_lexer_peek_token (parser->lexer)->location;
8001 OPT_Wempty_body, "suggest braces around "
8002 "empty body in an %<else%> statement");
8003 add_stmt (build_empty_stmt (loc));
8004 cp_lexer_consume_token (parser->lexer);
8007 cp_parser_implicitly_scoped_statement (parser, NULL);
8009 finish_else_clause (statement);
8011 /* If we are currently parsing a then-clause, then
8012 IF_P will not be NULL. We set it to true to
8013 indicate that this if statement has an else clause.
8014 This may trigger the Wparentheses warning below
8015 when we get back up to the parent if statement. */
8021 /* This if statement does not have an else clause. If
8022 NESTED_IF is true, then the then-clause is an if
8023 statement which does have an else clause. We warn
8024 about the potential ambiguity. */
8026 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8027 "suggest explicit braces to avoid ambiguous"
8031 /* Now we're all done with the if-statement. */
8032 finish_if_stmt (statement);
8036 bool in_switch_statement_p;
8037 unsigned char in_statement;
8039 /* Add the condition. */
8040 finish_switch_cond (condition, statement);
8042 /* Parse the body of the switch-statement. */
8043 in_switch_statement_p = parser->in_switch_statement_p;
8044 in_statement = parser->in_statement;
8045 parser->in_switch_statement_p = true;
8046 parser->in_statement |= IN_SWITCH_STMT;
8047 cp_parser_implicitly_scoped_statement (parser, NULL);
8048 parser->in_switch_statement_p = in_switch_statement_p;
8049 parser->in_statement = in_statement;
8051 /* Now we're all done with the switch-statement. */
8052 finish_switch_stmt (statement);
8060 cp_parser_error (parser, "expected selection-statement");
8061 return error_mark_node;
8065 /* Parse a condition.
8069 type-specifier-seq declarator = initializer-clause
8070 type-specifier-seq declarator braced-init-list
8075 type-specifier-seq declarator asm-specification [opt]
8076 attributes [opt] = assignment-expression
8078 Returns the expression that should be tested. */
8081 cp_parser_condition (cp_parser* parser)
8083 cp_decl_specifier_seq type_specifiers;
8084 const char *saved_message;
8086 /* Try the declaration first. */
8087 cp_parser_parse_tentatively (parser);
8088 /* New types are not allowed in the type-specifier-seq for a
8090 saved_message = parser->type_definition_forbidden_message;
8091 parser->type_definition_forbidden_message
8092 = "types may not be defined in conditions";
8093 /* Parse the type-specifier-seq. */
8094 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8095 /*is_trailing_return=*/false,
8097 /* Restore the saved message. */
8098 parser->type_definition_forbidden_message = saved_message;
8099 /* If all is well, we might be looking at a declaration. */
8100 if (!cp_parser_error_occurred (parser))
8103 tree asm_specification;
8105 cp_declarator *declarator;
8106 tree initializer = NULL_TREE;
8108 /* Parse the declarator. */
8109 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8110 /*ctor_dtor_or_conv_p=*/NULL,
8111 /*parenthesized_p=*/NULL,
8112 /*member_p=*/false);
8113 /* Parse the attributes. */
8114 attributes = cp_parser_attributes_opt (parser);
8115 /* Parse the asm-specification. */
8116 asm_specification = cp_parser_asm_specification_opt (parser);
8117 /* If the next token is not an `=' or '{', then we might still be
8118 looking at an expression. For example:
8122 looks like a decl-specifier-seq and a declarator -- but then
8123 there is no `=', so this is an expression. */
8124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8125 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8126 cp_parser_simulate_error (parser);
8128 /* If we did see an `=' or '{', then we are looking at a declaration
8130 if (cp_parser_parse_definitely (parser))
8133 bool non_constant_p;
8134 bool flags = LOOKUP_ONLYCONVERTING;
8136 /* Create the declaration. */
8137 decl = start_decl (declarator, &type_specifiers,
8138 /*initialized_p=*/true,
8139 attributes, /*prefix_attributes=*/NULL_TREE,
8142 /* Parse the initializer. */
8143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8145 initializer = cp_parser_braced_list (parser, &non_constant_p);
8146 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8151 /* Consume the `='. */
8152 cp_parser_require (parser, CPP_EQ, "%<=%>");
8153 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8155 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8156 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8158 if (!non_constant_p)
8159 initializer = fold_non_dependent_expr (initializer);
8161 /* Process the initializer. */
8162 cp_finish_decl (decl,
8163 initializer, !non_constant_p,
8168 pop_scope (pushed_scope);
8170 return convert_from_reference (decl);
8173 /* If we didn't even get past the declarator successfully, we are
8174 definitely not looking at a declaration. */
8176 cp_parser_abort_tentative_parse (parser);
8178 /* Otherwise, we are looking at an expression. */
8179 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8182 /* Parse an iteration-statement.
8184 iteration-statement:
8185 while ( condition ) statement
8186 do statement while ( expression ) ;
8187 for ( for-init-statement condition [opt] ; expression [opt] )
8190 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8193 cp_parser_iteration_statement (cp_parser* parser)
8198 unsigned char in_statement;
8200 /* Peek at the next token. */
8201 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8203 return error_mark_node;
8205 /* Remember whether or not we are already within an iteration
8207 in_statement = parser->in_statement;
8209 /* See what kind of keyword it is. */
8210 keyword = token->keyword;
8217 /* Begin the while-statement. */
8218 statement = begin_while_stmt ();
8219 /* Look for the `('. */
8220 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8221 /* Parse the condition. */
8222 condition = cp_parser_condition (parser);
8223 finish_while_stmt_cond (condition, statement);
8224 /* Look for the `)'. */
8225 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8226 /* Parse the dependent statement. */
8227 parser->in_statement = IN_ITERATION_STMT;
8228 cp_parser_already_scoped_statement (parser);
8229 parser->in_statement = in_statement;
8230 /* We're done with the while-statement. */
8231 finish_while_stmt (statement);
8239 /* Begin the do-statement. */
8240 statement = begin_do_stmt ();
8241 /* Parse the body of the do-statement. */
8242 parser->in_statement = IN_ITERATION_STMT;
8243 cp_parser_implicitly_scoped_statement (parser, NULL);
8244 parser->in_statement = in_statement;
8245 finish_do_body (statement);
8246 /* Look for the `while' keyword. */
8247 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8248 /* Look for the `('. */
8249 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8250 /* Parse the expression. */
8251 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8252 /* We're done with the do-statement. */
8253 finish_do_stmt (expression, statement);
8254 /* Look for the `)'. */
8255 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8256 /* Look for the `;'. */
8257 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8263 tree condition = NULL_TREE;
8264 tree expression = NULL_TREE;
8266 /* Begin the for-statement. */
8267 statement = begin_for_stmt ();
8268 /* Look for the `('. */
8269 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8270 /* Parse the initialization. */
8271 cp_parser_for_init_statement (parser);
8272 finish_for_init_stmt (statement);
8274 /* If there's a condition, process it. */
8275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8276 condition = cp_parser_condition (parser);
8277 finish_for_cond (condition, statement);
8278 /* Look for the `;'. */
8279 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8281 /* If there's an expression, process it. */
8282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8283 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8284 finish_for_expr (expression, statement);
8285 /* Look for the `)'. */
8286 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8288 /* Parse the body of the for-statement. */
8289 parser->in_statement = IN_ITERATION_STMT;
8290 cp_parser_already_scoped_statement (parser);
8291 parser->in_statement = in_statement;
8293 /* We're done with the for-statement. */
8294 finish_for_stmt (statement);
8299 cp_parser_error (parser, "expected iteration-statement");
8300 statement = error_mark_node;
8307 /* Parse a for-init-statement.
8310 expression-statement
8311 simple-declaration */
8314 cp_parser_for_init_statement (cp_parser* parser)
8316 /* If the next token is a `;', then we have an empty
8317 expression-statement. Grammatically, this is also a
8318 simple-declaration, but an invalid one, because it does not
8319 declare anything. Therefore, if we did not handle this case
8320 specially, we would issue an error message about an invalid
8322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8324 /* We're going to speculatively look for a declaration, falling back
8325 to an expression, if necessary. */
8326 cp_parser_parse_tentatively (parser);
8327 /* Parse the declaration. */
8328 cp_parser_simple_declaration (parser,
8329 /*function_definition_allowed_p=*/false);
8330 /* If the tentative parse failed, then we shall need to look for an
8331 expression-statement. */
8332 if (cp_parser_parse_definitely (parser))
8336 cp_parser_expression_statement (parser, false);
8339 /* Parse a jump-statement.
8344 return expression [opt] ;
8345 return braced-init-list ;
8353 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8356 cp_parser_jump_statement (cp_parser* parser)
8358 tree statement = error_mark_node;
8361 unsigned char in_statement;
8363 /* Peek at the next token. */
8364 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8366 return error_mark_node;
8368 /* See what kind of keyword it is. */
8369 keyword = token->keyword;
8373 in_statement = parser->in_statement & ~IN_IF_STMT;
8374 switch (in_statement)
8377 error_at (token->location, "break statement not within loop or switch");
8380 gcc_assert ((in_statement & IN_SWITCH_STMT)
8381 || in_statement == IN_ITERATION_STMT);
8382 statement = finish_break_stmt ();
8385 error_at (token->location, "invalid exit from OpenMP structured block");
8388 error_at (token->location, "break statement used with OpenMP for loop");
8391 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8395 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8398 error_at (token->location, "continue statement not within a loop");
8400 case IN_ITERATION_STMT:
8402 statement = finish_continue_stmt ();
8405 error_at (token->location, "invalid exit from OpenMP structured block");
8410 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8416 bool expr_non_constant_p;
8418 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8421 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8423 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8424 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8426 /* If the next token is a `;', then there is no
8429 /* Build the return-statement. */
8430 statement = finish_return_stmt (expr);
8431 /* Look for the final `;'. */
8432 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8437 /* Create the goto-statement. */
8438 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8440 /* Issue a warning about this use of a GNU extension. */
8441 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8442 /* Consume the '*' token. */
8443 cp_lexer_consume_token (parser->lexer);
8444 /* Parse the dependent expression. */
8445 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8448 finish_goto_stmt (cp_parser_identifier (parser));
8449 /* Look for the final `;'. */
8450 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8454 cp_parser_error (parser, "expected jump-statement");
8461 /* Parse a declaration-statement.
8463 declaration-statement:
8464 block-declaration */
8467 cp_parser_declaration_statement (cp_parser* parser)
8471 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8472 p = obstack_alloc (&declarator_obstack, 0);
8474 /* Parse the block-declaration. */
8475 cp_parser_block_declaration (parser, /*statement_p=*/true);
8477 /* Free any declarators allocated. */
8478 obstack_free (&declarator_obstack, p);
8480 /* Finish off the statement. */
8484 /* Some dependent statements (like `if (cond) statement'), are
8485 implicitly in their own scope. In other words, if the statement is
8486 a single statement (as opposed to a compound-statement), it is
8487 none-the-less treated as if it were enclosed in braces. Any
8488 declarations appearing in the dependent statement are out of scope
8489 after control passes that point. This function parses a statement,
8490 but ensures that is in its own scope, even if it is not a
8493 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8494 is a (possibly labeled) if statement which is not enclosed in
8495 braces and has an else clause. This is used to implement
8498 Returns the new statement. */
8501 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8508 /* Mark if () ; with a special NOP_EXPR. */
8509 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8512 cp_lexer_consume_token (parser->lexer);
8513 statement = add_stmt (build_empty_stmt (loc));
8515 /* if a compound is opened, we simply parse the statement directly. */
8516 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8517 statement = cp_parser_compound_statement (parser, NULL, false);
8518 /* If the token is not a `{', then we must take special action. */
8521 /* Create a compound-statement. */
8522 statement = begin_compound_stmt (0);
8523 /* Parse the dependent-statement. */
8524 cp_parser_statement (parser, NULL_TREE, false, if_p);
8525 /* Finish the dummy compound-statement. */
8526 finish_compound_stmt (statement);
8529 /* Return the statement. */
8533 /* For some dependent statements (like `while (cond) statement'), we
8534 have already created a scope. Therefore, even if the dependent
8535 statement is a compound-statement, we do not want to create another
8539 cp_parser_already_scoped_statement (cp_parser* parser)
8541 /* If the token is a `{', then we must take special action. */
8542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8543 cp_parser_statement (parser, NULL_TREE, false, NULL);
8546 /* Avoid calling cp_parser_compound_statement, so that we
8547 don't create a new scope. Do everything else by hand. */
8548 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8549 /* If the next keyword is `__label__' we have a label declaration. */
8550 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8551 cp_parser_label_declaration (parser);
8552 /* Parse an (optional) statement-seq. */
8553 cp_parser_statement_seq_opt (parser, NULL_TREE);
8554 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8558 /* Declarations [gram.dcl.dcl] */
8560 /* Parse an optional declaration-sequence.
8564 declaration-seq declaration */
8567 cp_parser_declaration_seq_opt (cp_parser* parser)
8573 token = cp_lexer_peek_token (parser->lexer);
8575 if (token->type == CPP_CLOSE_BRACE
8576 || token->type == CPP_EOF
8577 || token->type == CPP_PRAGMA_EOL)
8580 if (token->type == CPP_SEMICOLON)
8582 /* A declaration consisting of a single semicolon is
8583 invalid. Allow it unless we're being pedantic. */
8584 cp_lexer_consume_token (parser->lexer);
8585 if (!in_system_header)
8586 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8590 /* If we're entering or exiting a region that's implicitly
8591 extern "C", modify the lang context appropriately. */
8592 if (!parser->implicit_extern_c && token->implicit_extern_c)
8594 push_lang_context (lang_name_c);
8595 parser->implicit_extern_c = true;
8597 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8599 pop_lang_context ();
8600 parser->implicit_extern_c = false;
8603 if (token->type == CPP_PRAGMA)
8605 /* A top-level declaration can consist solely of a #pragma.
8606 A nested declaration cannot, so this is done here and not
8607 in cp_parser_declaration. (A #pragma at block scope is
8608 handled in cp_parser_statement.) */
8609 cp_parser_pragma (parser, pragma_external);
8613 /* Parse the declaration itself. */
8614 cp_parser_declaration (parser);
8618 /* Parse a declaration.
8623 template-declaration
8624 explicit-instantiation
8625 explicit-specialization
8626 linkage-specification
8627 namespace-definition
8632 __extension__ declaration */
8635 cp_parser_declaration (cp_parser* parser)
8642 /* Check for the `__extension__' keyword. */
8643 if (cp_parser_extension_opt (parser, &saved_pedantic))
8645 /* Parse the qualified declaration. */
8646 cp_parser_declaration (parser);
8647 /* Restore the PEDANTIC flag. */
8648 pedantic = saved_pedantic;
8653 /* Try to figure out what kind of declaration is present. */
8654 token1 = *cp_lexer_peek_token (parser->lexer);
8656 if (token1.type != CPP_EOF)
8657 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8660 token2.type = CPP_EOF;
8661 token2.keyword = RID_MAX;
8664 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8665 p = obstack_alloc (&declarator_obstack, 0);
8667 /* If the next token is `extern' and the following token is a string
8668 literal, then we have a linkage specification. */
8669 if (token1.keyword == RID_EXTERN
8670 && cp_parser_is_string_literal (&token2))
8671 cp_parser_linkage_specification (parser);
8672 /* If the next token is `template', then we have either a template
8673 declaration, an explicit instantiation, or an explicit
8675 else if (token1.keyword == RID_TEMPLATE)
8677 /* `template <>' indicates a template specialization. */
8678 if (token2.type == CPP_LESS
8679 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8680 cp_parser_explicit_specialization (parser);
8681 /* `template <' indicates a template declaration. */
8682 else if (token2.type == CPP_LESS)
8683 cp_parser_template_declaration (parser, /*member_p=*/false);
8684 /* Anything else must be an explicit instantiation. */
8686 cp_parser_explicit_instantiation (parser);
8688 /* If the next token is `export', then we have a template
8690 else if (token1.keyword == RID_EXPORT)
8691 cp_parser_template_declaration (parser, /*member_p=*/false);
8692 /* If the next token is `extern', 'static' or 'inline' and the one
8693 after that is `template', we have a GNU extended explicit
8694 instantiation directive. */
8695 else if (cp_parser_allow_gnu_extensions_p (parser)
8696 && (token1.keyword == RID_EXTERN
8697 || token1.keyword == RID_STATIC
8698 || token1.keyword == RID_INLINE)
8699 && token2.keyword == RID_TEMPLATE)
8700 cp_parser_explicit_instantiation (parser);
8701 /* If the next token is `namespace', check for a named or unnamed
8702 namespace definition. */
8703 else if (token1.keyword == RID_NAMESPACE
8704 && (/* A named namespace definition. */
8705 (token2.type == CPP_NAME
8706 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8708 /* An unnamed namespace definition. */
8709 || token2.type == CPP_OPEN_BRACE
8710 || token2.keyword == RID_ATTRIBUTE))
8711 cp_parser_namespace_definition (parser);
8712 /* An inline (associated) namespace definition. */
8713 else if (token1.keyword == RID_INLINE
8714 && token2.keyword == RID_NAMESPACE)
8715 cp_parser_namespace_definition (parser);
8716 /* Objective-C++ declaration/definition. */
8717 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8718 cp_parser_objc_declaration (parser);
8719 /* We must have either a block declaration or a function
8722 /* Try to parse a block-declaration, or a function-definition. */
8723 cp_parser_block_declaration (parser, /*statement_p=*/false);
8725 /* Free any declarators allocated. */
8726 obstack_free (&declarator_obstack, p);
8729 /* Parse a block-declaration.
8734 namespace-alias-definition
8741 __extension__ block-declaration
8746 static_assert-declaration
8748 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8749 part of a declaration-statement. */
8752 cp_parser_block_declaration (cp_parser *parser,
8758 /* Check for the `__extension__' keyword. */
8759 if (cp_parser_extension_opt (parser, &saved_pedantic))
8761 /* Parse the qualified declaration. */
8762 cp_parser_block_declaration (parser, statement_p);
8763 /* Restore the PEDANTIC flag. */
8764 pedantic = saved_pedantic;
8769 /* Peek at the next token to figure out which kind of declaration is
8771 token1 = cp_lexer_peek_token (parser->lexer);
8773 /* If the next keyword is `asm', we have an asm-definition. */
8774 if (token1->keyword == RID_ASM)
8777 cp_parser_commit_to_tentative_parse (parser);
8778 cp_parser_asm_definition (parser);
8780 /* If the next keyword is `namespace', we have a
8781 namespace-alias-definition. */
8782 else if (token1->keyword == RID_NAMESPACE)
8783 cp_parser_namespace_alias_definition (parser);
8784 /* If the next keyword is `using', we have either a
8785 using-declaration or a using-directive. */
8786 else if (token1->keyword == RID_USING)
8791 cp_parser_commit_to_tentative_parse (parser);
8792 /* If the token after `using' is `namespace', then we have a
8794 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8795 if (token2->keyword == RID_NAMESPACE)
8796 cp_parser_using_directive (parser);
8797 /* Otherwise, it's a using-declaration. */
8799 cp_parser_using_declaration (parser,
8800 /*access_declaration_p=*/false);
8802 /* If the next keyword is `__label__' we have a misplaced label
8804 else if (token1->keyword == RID_LABEL)
8806 cp_lexer_consume_token (parser->lexer);
8807 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8808 cp_parser_skip_to_end_of_statement (parser);
8809 /* If the next token is now a `;', consume it. */
8810 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8811 cp_lexer_consume_token (parser->lexer);
8813 /* If the next token is `static_assert' we have a static assertion. */
8814 else if (token1->keyword == RID_STATIC_ASSERT)
8815 cp_parser_static_assert (parser, /*member_p=*/false);
8816 /* Anything else must be a simple-declaration. */
8818 cp_parser_simple_declaration (parser, !statement_p);
8821 /* Parse a simple-declaration.
8824 decl-specifier-seq [opt] init-declarator-list [opt] ;
8826 init-declarator-list:
8828 init-declarator-list , init-declarator
8830 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8831 function-definition as a simple-declaration. */
8834 cp_parser_simple_declaration (cp_parser* parser,
8835 bool function_definition_allowed_p)
8837 cp_decl_specifier_seq decl_specifiers;
8838 int declares_class_or_enum;
8839 bool saw_declarator;
8841 /* Defer access checks until we know what is being declared; the
8842 checks for names appearing in the decl-specifier-seq should be
8843 done as if we were in the scope of the thing being declared. */
8844 push_deferring_access_checks (dk_deferred);
8846 /* Parse the decl-specifier-seq. We have to keep track of whether
8847 or not the decl-specifier-seq declares a named class or
8848 enumeration type, since that is the only case in which the
8849 init-declarator-list is allowed to be empty.
8853 In a simple-declaration, the optional init-declarator-list can be
8854 omitted only when declaring a class or enumeration, that is when
8855 the decl-specifier-seq contains either a class-specifier, an
8856 elaborated-type-specifier, or an enum-specifier. */
8857 cp_parser_decl_specifier_seq (parser,
8858 CP_PARSER_FLAGS_OPTIONAL,
8860 &declares_class_or_enum);
8861 /* We no longer need to defer access checks. */
8862 stop_deferring_access_checks ();
8864 /* In a block scope, a valid declaration must always have a
8865 decl-specifier-seq. By not trying to parse declarators, we can
8866 resolve the declaration/expression ambiguity more quickly. */
8867 if (!function_definition_allowed_p
8868 && !decl_specifiers.any_specifiers_p)
8870 cp_parser_error (parser, "expected declaration");
8874 /* If the next two tokens are both identifiers, the code is
8875 erroneous. The usual cause of this situation is code like:
8879 where "T" should name a type -- but does not. */
8880 if (!decl_specifiers.any_type_specifiers_p
8881 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8883 /* If parsing tentatively, we should commit; we really are
8884 looking at a declaration. */
8885 cp_parser_commit_to_tentative_parse (parser);
8890 /* If we have seen at least one decl-specifier, and the next token
8891 is not a parenthesis, then we must be looking at a declaration.
8892 (After "int (" we might be looking at a functional cast.) */
8893 if (decl_specifiers.any_specifiers_p
8894 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8895 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8896 && !cp_parser_error_occurred (parser))
8897 cp_parser_commit_to_tentative_parse (parser);
8899 /* Keep going until we hit the `;' at the end of the simple
8901 saw_declarator = false;
8902 while (cp_lexer_next_token_is_not (parser->lexer,
8906 bool function_definition_p;
8911 /* If we are processing next declarator, coma is expected */
8912 token = cp_lexer_peek_token (parser->lexer);
8913 gcc_assert (token->type == CPP_COMMA);
8914 cp_lexer_consume_token (parser->lexer);
8917 saw_declarator = true;
8919 /* Parse the init-declarator. */
8920 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8922 function_definition_allowed_p,
8924 declares_class_or_enum,
8925 &function_definition_p);
8926 /* If an error occurred while parsing tentatively, exit quickly.
8927 (That usually happens when in the body of a function; each
8928 statement is treated as a declaration-statement until proven
8930 if (cp_parser_error_occurred (parser))
8932 /* Handle function definitions specially. */
8933 if (function_definition_p)
8935 /* If the next token is a `,', then we are probably
8936 processing something like:
8940 which is erroneous. */
8941 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8943 cp_token *token = cp_lexer_peek_token (parser->lexer);
8944 error_at (token->location,
8946 " declarations and function-definitions is forbidden");
8948 /* Otherwise, we're done with the list of declarators. */
8951 pop_deferring_access_checks ();
8955 /* The next token should be either a `,' or a `;'. */
8956 token = cp_lexer_peek_token (parser->lexer);
8957 /* If it's a `,', there are more declarators to come. */
8958 if (token->type == CPP_COMMA)
8959 /* will be consumed next time around */;
8960 /* If it's a `;', we are done. */
8961 else if (token->type == CPP_SEMICOLON)
8963 /* Anything else is an error. */
8966 /* If we have already issued an error message we don't need
8967 to issue another one. */
8968 if (decl != error_mark_node
8969 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8970 cp_parser_error (parser, "expected %<,%> or %<;%>");
8971 /* Skip tokens until we reach the end of the statement. */
8972 cp_parser_skip_to_end_of_statement (parser);
8973 /* If the next token is now a `;', consume it. */
8974 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8975 cp_lexer_consume_token (parser->lexer);
8978 /* After the first time around, a function-definition is not
8979 allowed -- even if it was OK at first. For example:
8984 function_definition_allowed_p = false;
8987 /* Issue an error message if no declarators are present, and the
8988 decl-specifier-seq does not itself declare a class or
8990 if (!saw_declarator)
8992 if (cp_parser_declares_only_class_p (parser))
8993 shadow_tag (&decl_specifiers);
8994 /* Perform any deferred access checks. */
8995 perform_deferred_access_checks ();
8998 /* Consume the `;'. */
8999 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9002 pop_deferring_access_checks ();
9005 /* Parse a decl-specifier-seq.
9008 decl-specifier-seq [opt] decl-specifier
9011 storage-class-specifier
9022 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9024 The parser flags FLAGS is used to control type-specifier parsing.
9026 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9029 1: one of the decl-specifiers is an elaborated-type-specifier
9030 (i.e., a type declaration)
9031 2: one of the decl-specifiers is an enum-specifier or a
9032 class-specifier (i.e., a type definition)
9037 cp_parser_decl_specifier_seq (cp_parser* parser,
9038 cp_parser_flags flags,
9039 cp_decl_specifier_seq *decl_specs,
9040 int* declares_class_or_enum)
9042 bool constructor_possible_p = !parser->in_declarator_p;
9043 cp_token *start_token = NULL;
9045 /* Clear DECL_SPECS. */
9046 clear_decl_specs (decl_specs);
9048 /* Assume no class or enumeration type is declared. */
9049 *declares_class_or_enum = 0;
9051 /* Keep reading specifiers until there are no more to read. */
9055 bool found_decl_spec;
9058 /* Peek at the next token. */
9059 token = cp_lexer_peek_token (parser->lexer);
9061 /* Save the first token of the decl spec list for error
9064 start_token = token;
9065 /* Handle attributes. */
9066 if (token->keyword == RID_ATTRIBUTE)
9068 /* Parse the attributes. */
9069 decl_specs->attributes
9070 = chainon (decl_specs->attributes,
9071 cp_parser_attributes_opt (parser));
9074 /* Assume we will find a decl-specifier keyword. */
9075 found_decl_spec = true;
9076 /* If the next token is an appropriate keyword, we can simply
9077 add it to the list. */
9078 switch (token->keyword)
9084 if (!at_class_scope_p ())
9086 error_at (token->location, "%<friend%> used outside of class");
9087 cp_lexer_purge_token (parser->lexer);
9091 ++decl_specs->specs[(int) ds_friend];
9092 /* Consume the token. */
9093 cp_lexer_consume_token (parser->lexer);
9098 ++decl_specs->specs[(int) ds_constexpr];
9099 cp_lexer_consume_token (parser->lexer);
9102 /* function-specifier:
9109 cp_parser_function_specifier_opt (parser, decl_specs);
9115 ++decl_specs->specs[(int) ds_typedef];
9116 /* Consume the token. */
9117 cp_lexer_consume_token (parser->lexer);
9118 /* A constructor declarator cannot appear in a typedef. */
9119 constructor_possible_p = false;
9120 /* The "typedef" keyword can only occur in a declaration; we
9121 may as well commit at this point. */
9122 cp_parser_commit_to_tentative_parse (parser);
9124 if (decl_specs->storage_class != sc_none)
9125 decl_specs->conflicting_specifiers_p = true;
9128 /* storage-class-specifier:
9138 if (cxx_dialect == cxx98)
9140 /* Consume the token. */
9141 cp_lexer_consume_token (parser->lexer);
9143 /* Complain about `auto' as a storage specifier, if
9144 we're complaining about C++0x compatibility. */
9145 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9146 " will change meaning in C++0x; please remove it");
9148 /* Set the storage class anyway. */
9149 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9153 /* C++0x auto type-specifier. */
9154 found_decl_spec = false;
9161 /* Consume the token. */
9162 cp_lexer_consume_token (parser->lexer);
9163 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9167 /* Consume the token. */
9168 cp_lexer_consume_token (parser->lexer);
9169 ++decl_specs->specs[(int) ds_thread];
9173 /* We did not yet find a decl-specifier yet. */
9174 found_decl_spec = false;
9178 /* Constructors are a special case. The `S' in `S()' is not a
9179 decl-specifier; it is the beginning of the declarator. */
9182 && constructor_possible_p
9183 && (cp_parser_constructor_declarator_p
9184 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9186 /* If we don't have a DECL_SPEC yet, then we must be looking at
9187 a type-specifier. */
9188 if (!found_decl_spec && !constructor_p)
9190 int decl_spec_declares_class_or_enum;
9191 bool is_cv_qualifier;
9195 = cp_parser_type_specifier (parser, flags,
9197 /*is_declaration=*/true,
9198 &decl_spec_declares_class_or_enum,
9200 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9202 /* If this type-specifier referenced a user-defined type
9203 (a typedef, class-name, etc.), then we can't allow any
9204 more such type-specifiers henceforth.
9208 The longest sequence of decl-specifiers that could
9209 possibly be a type name is taken as the
9210 decl-specifier-seq of a declaration. The sequence shall
9211 be self-consistent as described below.
9215 As a general rule, at most one type-specifier is allowed
9216 in the complete decl-specifier-seq of a declaration. The
9217 only exceptions are the following:
9219 -- const or volatile can be combined with any other
9222 -- signed or unsigned can be combined with char, long,
9230 void g (const int Pc);
9232 Here, Pc is *not* part of the decl-specifier seq; it's
9233 the declarator. Therefore, once we see a type-specifier
9234 (other than a cv-qualifier), we forbid any additional
9235 user-defined types. We *do* still allow things like `int
9236 int' to be considered a decl-specifier-seq, and issue the
9237 error message later. */
9238 if (type_spec && !is_cv_qualifier)
9239 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9240 /* A constructor declarator cannot follow a type-specifier. */
9243 constructor_possible_p = false;
9244 found_decl_spec = true;
9245 if (!is_cv_qualifier)
9246 decl_specs->any_type_specifiers_p = true;
9250 /* If we still do not have a DECL_SPEC, then there are no more
9252 if (!found_decl_spec)
9255 decl_specs->any_specifiers_p = true;
9256 /* After we see one decl-specifier, further decl-specifiers are
9258 flags |= CP_PARSER_FLAGS_OPTIONAL;
9261 cp_parser_check_decl_spec (decl_specs, start_token->location);
9263 /* Don't allow a friend specifier with a class definition. */
9264 if (decl_specs->specs[(int) ds_friend] != 0
9265 && (*declares_class_or_enum & 2))
9266 error_at (start_token->location,
9267 "class definition may not be declared a friend");
9270 /* Parse an (optional) storage-class-specifier.
9272 storage-class-specifier:
9281 storage-class-specifier:
9284 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9287 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9289 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9292 if (cxx_dialect != cxx98)
9294 /* Fall through for C++98. */
9301 /* Consume the token. */
9302 return cp_lexer_consume_token (parser->lexer)->u.value;
9309 /* Parse an (optional) function-specifier.
9316 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9317 Updates DECL_SPECS, if it is non-NULL. */
9320 cp_parser_function_specifier_opt (cp_parser* parser,
9321 cp_decl_specifier_seq *decl_specs)
9323 cp_token *token = cp_lexer_peek_token (parser->lexer);
9324 switch (token->keyword)
9328 ++decl_specs->specs[(int) ds_inline];
9332 /* 14.5.2.3 [temp.mem]
9334 A member function template shall not be virtual. */
9335 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9336 error_at (token->location, "templates may not be %<virtual%>");
9337 else if (decl_specs)
9338 ++decl_specs->specs[(int) ds_virtual];
9343 ++decl_specs->specs[(int) ds_explicit];
9350 /* Consume the token. */
9351 return cp_lexer_consume_token (parser->lexer)->u.value;
9354 /* Parse a linkage-specification.
9356 linkage-specification:
9357 extern string-literal { declaration-seq [opt] }
9358 extern string-literal declaration */
9361 cp_parser_linkage_specification (cp_parser* parser)
9365 /* Look for the `extern' keyword. */
9366 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9368 /* Look for the string-literal. */
9369 linkage = cp_parser_string_literal (parser, false, false);
9371 /* Transform the literal into an identifier. If the literal is a
9372 wide-character string, or contains embedded NULs, then we can't
9373 handle it as the user wants. */
9374 if (strlen (TREE_STRING_POINTER (linkage))
9375 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9377 cp_parser_error (parser, "invalid linkage-specification");
9378 /* Assume C++ linkage. */
9379 linkage = lang_name_cplusplus;
9382 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9384 /* We're now using the new linkage. */
9385 push_lang_context (linkage);
9387 /* If the next token is a `{', then we're using the first
9389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9391 /* Consume the `{' token. */
9392 cp_lexer_consume_token (parser->lexer);
9393 /* Parse the declarations. */
9394 cp_parser_declaration_seq_opt (parser);
9395 /* Look for the closing `}'. */
9396 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9398 /* Otherwise, there's just one declaration. */
9401 bool saved_in_unbraced_linkage_specification_p;
9403 saved_in_unbraced_linkage_specification_p
9404 = parser->in_unbraced_linkage_specification_p;
9405 parser->in_unbraced_linkage_specification_p = true;
9406 cp_parser_declaration (parser);
9407 parser->in_unbraced_linkage_specification_p
9408 = saved_in_unbraced_linkage_specification_p;
9411 /* We're done with the linkage-specification. */
9412 pop_lang_context ();
9415 /* Parse a static_assert-declaration.
9417 static_assert-declaration:
9418 static_assert ( constant-expression , string-literal ) ;
9420 If MEMBER_P, this static_assert is a class member. */
9423 cp_parser_static_assert(cp_parser *parser, bool member_p)
9428 location_t saved_loc;
9430 /* Peek at the `static_assert' token so we can keep track of exactly
9431 where the static assertion started. */
9432 token = cp_lexer_peek_token (parser->lexer);
9433 saved_loc = token->location;
9435 /* Look for the `static_assert' keyword. */
9436 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9437 "%<static_assert%>"))
9440 /* We know we are in a static assertion; commit to any tentative
9442 if (cp_parser_parsing_tentatively (parser))
9443 cp_parser_commit_to_tentative_parse (parser);
9445 /* Parse the `(' starting the static assertion condition. */
9446 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9448 /* Parse the constant-expression. */
9450 cp_parser_constant_expression (parser,
9451 /*allow_non_constant_p=*/false,
9452 /*non_constant_p=*/NULL);
9454 /* Parse the separating `,'. */
9455 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9457 /* Parse the string-literal message. */
9458 message = cp_parser_string_literal (parser,
9459 /*translate=*/false,
9462 /* A `)' completes the static assertion. */
9463 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9464 cp_parser_skip_to_closing_parenthesis (parser,
9465 /*recovering=*/true,
9467 /*consume_paren=*/true);
9469 /* A semicolon terminates the declaration. */
9470 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9472 /* Complete the static assertion, which may mean either processing
9473 the static assert now or saving it for template instantiation. */
9474 finish_static_assert (condition, message, saved_loc, member_p);
9477 /* Parse a `decltype' type. Returns the type.
9479 simple-type-specifier:
9480 decltype ( expression ) */
9483 cp_parser_decltype (cp_parser *parser)
9486 bool id_expression_or_member_access_p = false;
9487 const char *saved_message;
9488 bool saved_integral_constant_expression_p;
9489 bool saved_non_integral_constant_expression_p;
9490 cp_token *id_expr_start_token;
9492 /* Look for the `decltype' token. */
9493 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9494 return error_mark_node;
9496 /* Types cannot be defined in a `decltype' expression. Save away the
9498 saved_message = parser->type_definition_forbidden_message;
9500 /* And create the new one. */
9501 parser->type_definition_forbidden_message
9502 = "types may not be defined in %<decltype%> expressions";
9504 /* The restrictions on constant-expressions do not apply inside
9505 decltype expressions. */
9506 saved_integral_constant_expression_p
9507 = parser->integral_constant_expression_p;
9508 saved_non_integral_constant_expression_p
9509 = parser->non_integral_constant_expression_p;
9510 parser->integral_constant_expression_p = false;
9512 /* Do not actually evaluate the expression. */
9513 ++cp_unevaluated_operand;
9515 /* Do not warn about problems with the expression. */
9516 ++c_inhibit_evaluation_warnings;
9518 /* Parse the opening `('. */
9519 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9520 return error_mark_node;
9522 /* First, try parsing an id-expression. */
9523 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9524 cp_parser_parse_tentatively (parser);
9525 expr = cp_parser_id_expression (parser,
9526 /*template_keyword_p=*/false,
9527 /*check_dependency_p=*/true,
9528 /*template_p=*/NULL,
9529 /*declarator_p=*/false,
9530 /*optional_p=*/false);
9532 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9534 bool non_integral_constant_expression_p = false;
9535 tree id_expression = expr;
9537 const char *error_msg;
9539 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9540 /* Lookup the name we got back from the id-expression. */
9541 expr = cp_parser_lookup_name (parser, expr,
9543 /*is_template=*/false,
9544 /*is_namespace=*/false,
9545 /*check_dependency=*/true,
9546 /*ambiguous_decls=*/NULL,
9547 id_expr_start_token->location);
9550 && expr != error_mark_node
9551 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9552 && TREE_CODE (expr) != TYPE_DECL
9553 && (TREE_CODE (expr) != BIT_NOT_EXPR
9554 || !TYPE_P (TREE_OPERAND (expr, 0)))
9555 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9557 /* Complete lookup of the id-expression. */
9558 expr = (finish_id_expression
9559 (id_expression, expr, parser->scope, &idk,
9560 /*integral_constant_expression_p=*/false,
9561 /*allow_non_integral_constant_expression_p=*/true,
9562 &non_integral_constant_expression_p,
9563 /*template_p=*/false,
9565 /*address_p=*/false,
9566 /*template_arg_p=*/false,
9568 id_expr_start_token->location));
9570 if (expr == error_mark_node)
9571 /* We found an id-expression, but it was something that we
9572 should not have found. This is an error, not something
9573 we can recover from, so note that we found an
9574 id-expression and we'll recover as gracefully as
9576 id_expression_or_member_access_p = true;
9580 && expr != error_mark_node
9581 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9582 /* We have an id-expression. */
9583 id_expression_or_member_access_p = true;
9586 if (!id_expression_or_member_access_p)
9588 /* Abort the id-expression parse. */
9589 cp_parser_abort_tentative_parse (parser);
9591 /* Parsing tentatively, again. */
9592 cp_parser_parse_tentatively (parser);
9594 /* Parse a class member access. */
9595 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9597 /*member_access_only_p=*/true, NULL);
9600 && expr != error_mark_node
9601 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9602 /* We have an id-expression. */
9603 id_expression_or_member_access_p = true;
9606 if (id_expression_or_member_access_p)
9607 /* We have parsed the complete id-expression or member access. */
9608 cp_parser_parse_definitely (parser);
9611 bool saved_greater_than_is_operator_p;
9613 /* Abort our attempt to parse an id-expression or member access
9615 cp_parser_abort_tentative_parse (parser);
9617 /* Within a parenthesized expression, a `>' token is always
9618 the greater-than operator. */
9619 saved_greater_than_is_operator_p
9620 = parser->greater_than_is_operator_p;
9621 parser->greater_than_is_operator_p = true;
9623 /* Parse a full expression. */
9624 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9626 /* The `>' token might be the end of a template-id or
9627 template-parameter-list now. */
9628 parser->greater_than_is_operator_p
9629 = saved_greater_than_is_operator_p;
9632 /* Go back to evaluating expressions. */
9633 --cp_unevaluated_operand;
9634 --c_inhibit_evaluation_warnings;
9636 /* Restore the old message and the integral constant expression
9638 parser->type_definition_forbidden_message = saved_message;
9639 parser->integral_constant_expression_p
9640 = saved_integral_constant_expression_p;
9641 parser->non_integral_constant_expression_p
9642 = saved_non_integral_constant_expression_p;
9644 if (expr == error_mark_node)
9646 /* Skip everything up to the closing `)'. */
9647 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9648 /*consume_paren=*/true);
9649 return error_mark_node;
9652 /* Parse to the closing `)'. */
9653 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9655 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9656 /*consume_paren=*/true);
9657 return error_mark_node;
9660 return finish_decltype_type (expr, id_expression_or_member_access_p);
9663 /* Special member functions [gram.special] */
9665 /* Parse a conversion-function-id.
9667 conversion-function-id:
9668 operator conversion-type-id
9670 Returns an IDENTIFIER_NODE representing the operator. */
9673 cp_parser_conversion_function_id (cp_parser* parser)
9677 tree saved_qualifying_scope;
9678 tree saved_object_scope;
9679 tree pushed_scope = NULL_TREE;
9681 /* Look for the `operator' token. */
9682 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9683 return error_mark_node;
9684 /* When we parse the conversion-type-id, the current scope will be
9685 reset. However, we need that information in able to look up the
9686 conversion function later, so we save it here. */
9687 saved_scope = parser->scope;
9688 saved_qualifying_scope = parser->qualifying_scope;
9689 saved_object_scope = parser->object_scope;
9690 /* We must enter the scope of the class so that the names of
9691 entities declared within the class are available in the
9692 conversion-type-id. For example, consider:
9699 S::operator I() { ... }
9701 In order to see that `I' is a type-name in the definition, we
9702 must be in the scope of `S'. */
9704 pushed_scope = push_scope (saved_scope);
9705 /* Parse the conversion-type-id. */
9706 type = cp_parser_conversion_type_id (parser);
9707 /* Leave the scope of the class, if any. */
9709 pop_scope (pushed_scope);
9710 /* Restore the saved scope. */
9711 parser->scope = saved_scope;
9712 parser->qualifying_scope = saved_qualifying_scope;
9713 parser->object_scope = saved_object_scope;
9714 /* If the TYPE is invalid, indicate failure. */
9715 if (type == error_mark_node)
9716 return error_mark_node;
9717 return mangle_conv_op_name_for_type (type);
9720 /* Parse a conversion-type-id:
9723 type-specifier-seq conversion-declarator [opt]
9725 Returns the TYPE specified. */
9728 cp_parser_conversion_type_id (cp_parser* parser)
9731 cp_decl_specifier_seq type_specifiers;
9732 cp_declarator *declarator;
9733 tree type_specified;
9735 /* Parse the attributes. */
9736 attributes = cp_parser_attributes_opt (parser);
9737 /* Parse the type-specifiers. */
9738 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9739 /*is_trailing_return=*/false,
9741 /* If that didn't work, stop. */
9742 if (type_specifiers.type == error_mark_node)
9743 return error_mark_node;
9744 /* Parse the conversion-declarator. */
9745 declarator = cp_parser_conversion_declarator_opt (parser);
9747 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9748 /*initialized=*/0, &attributes);
9750 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9752 /* Don't give this error when parsing tentatively. This happens to
9753 work because we always parse this definitively once. */
9754 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9755 && type_uses_auto (type_specified))
9757 error ("invalid use of %<auto%> in conversion operator");
9758 return error_mark_node;
9761 return type_specified;
9764 /* Parse an (optional) conversion-declarator.
9766 conversion-declarator:
9767 ptr-operator conversion-declarator [opt]
9771 static cp_declarator *
9772 cp_parser_conversion_declarator_opt (cp_parser* parser)
9774 enum tree_code code;
9776 cp_cv_quals cv_quals;
9778 /* We don't know if there's a ptr-operator next, or not. */
9779 cp_parser_parse_tentatively (parser);
9780 /* Try the ptr-operator. */
9781 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9782 /* If it worked, look for more conversion-declarators. */
9783 if (cp_parser_parse_definitely (parser))
9785 cp_declarator *declarator;
9787 /* Parse another optional declarator. */
9788 declarator = cp_parser_conversion_declarator_opt (parser);
9790 return cp_parser_make_indirect_declarator
9791 (code, class_type, cv_quals, declarator);
9797 /* Parse an (optional) ctor-initializer.
9800 : mem-initializer-list
9802 Returns TRUE iff the ctor-initializer was actually present. */
9805 cp_parser_ctor_initializer_opt (cp_parser* parser)
9807 /* If the next token is not a `:', then there is no
9808 ctor-initializer. */
9809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9811 /* Do default initialization of any bases and members. */
9812 if (DECL_CONSTRUCTOR_P (current_function_decl))
9813 finish_mem_initializers (NULL_TREE);
9818 /* Consume the `:' token. */
9819 cp_lexer_consume_token (parser->lexer);
9820 /* And the mem-initializer-list. */
9821 cp_parser_mem_initializer_list (parser);
9826 /* Parse a mem-initializer-list.
9828 mem-initializer-list:
9829 mem-initializer ... [opt]
9830 mem-initializer ... [opt] , mem-initializer-list */
9833 cp_parser_mem_initializer_list (cp_parser* parser)
9835 tree mem_initializer_list = NULL_TREE;
9836 cp_token *token = cp_lexer_peek_token (parser->lexer);
9838 /* Let the semantic analysis code know that we are starting the
9839 mem-initializer-list. */
9840 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9841 error_at (token->location,
9842 "only constructors take base initializers");
9844 /* Loop through the list. */
9847 tree mem_initializer;
9849 token = cp_lexer_peek_token (parser->lexer);
9850 /* Parse the mem-initializer. */
9851 mem_initializer = cp_parser_mem_initializer (parser);
9852 /* If the next token is a `...', we're expanding member initializers. */
9853 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9855 /* Consume the `...'. */
9856 cp_lexer_consume_token (parser->lexer);
9858 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9859 can be expanded but members cannot. */
9860 if (mem_initializer != error_mark_node
9861 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9863 error_at (token->location,
9864 "cannot expand initializer for member %<%D%>",
9865 TREE_PURPOSE (mem_initializer));
9866 mem_initializer = error_mark_node;
9869 /* Construct the pack expansion type. */
9870 if (mem_initializer != error_mark_node)
9871 mem_initializer = make_pack_expansion (mem_initializer);
9873 /* Add it to the list, unless it was erroneous. */
9874 if (mem_initializer != error_mark_node)
9876 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9877 mem_initializer_list = mem_initializer;
9879 /* If the next token is not a `,', we're done. */
9880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9882 /* Consume the `,' token. */
9883 cp_lexer_consume_token (parser->lexer);
9886 /* Perform semantic analysis. */
9887 if (DECL_CONSTRUCTOR_P (current_function_decl))
9888 finish_mem_initializers (mem_initializer_list);
9891 /* Parse a mem-initializer.
9894 mem-initializer-id ( expression-list [opt] )
9895 mem-initializer-id braced-init-list
9900 ( expression-list [opt] )
9902 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9903 class) or FIELD_DECL (for a non-static data member) to initialize;
9904 the TREE_VALUE is the expression-list. An empty initialization
9905 list is represented by void_list_node. */
9908 cp_parser_mem_initializer (cp_parser* parser)
9910 tree mem_initializer_id;
9911 tree expression_list;
9913 cp_token *token = cp_lexer_peek_token (parser->lexer);
9915 /* Find out what is being initialized. */
9916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9918 permerror (token->location,
9919 "anachronistic old-style base class initializer");
9920 mem_initializer_id = NULL_TREE;
9924 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9925 if (mem_initializer_id == error_mark_node)
9926 return mem_initializer_id;
9928 member = expand_member_init (mem_initializer_id);
9929 if (member && !DECL_P (member))
9930 in_base_initializer = 1;
9932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9934 bool expr_non_constant_p;
9935 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9936 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9937 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9938 expression_list = build_tree_list (NULL_TREE, expression_list);
9943 vec = cp_parser_parenthesized_expression_list (parser, false,
9945 /*allow_expansion_p=*/true,
9946 /*non_constant_p=*/NULL);
9948 return error_mark_node;
9949 expression_list = build_tree_list_vec (vec);
9950 release_tree_vector (vec);
9953 if (expression_list == error_mark_node)
9954 return error_mark_node;
9955 if (!expression_list)
9956 expression_list = void_type_node;
9958 in_base_initializer = 0;
9960 return member ? build_tree_list (member, expression_list) : error_mark_node;
9963 /* Parse a mem-initializer-id.
9966 :: [opt] nested-name-specifier [opt] class-name
9969 Returns a TYPE indicating the class to be initializer for the first
9970 production. Returns an IDENTIFIER_NODE indicating the data member
9971 to be initialized for the second production. */
9974 cp_parser_mem_initializer_id (cp_parser* parser)
9976 bool global_scope_p;
9977 bool nested_name_specifier_p;
9978 bool template_p = false;
9981 cp_token *token = cp_lexer_peek_token (parser->lexer);
9983 /* `typename' is not allowed in this context ([temp.res]). */
9984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9986 error_at (token->location,
9987 "keyword %<typename%> not allowed in this context (a qualified "
9988 "member initializer is implicitly a type)");
9989 cp_lexer_consume_token (parser->lexer);
9991 /* Look for the optional `::' operator. */
9993 = (cp_parser_global_scope_opt (parser,
9994 /*current_scope_valid_p=*/false)
9996 /* Look for the optional nested-name-specifier. The simplest way to
10001 The keyword `typename' is not permitted in a base-specifier or
10002 mem-initializer; in these contexts a qualified name that
10003 depends on a template-parameter is implicitly assumed to be a
10006 is to assume that we have seen the `typename' keyword at this
10008 nested_name_specifier_p
10009 = (cp_parser_nested_name_specifier_opt (parser,
10010 /*typename_keyword_p=*/true,
10011 /*check_dependency_p=*/true,
10013 /*is_declaration=*/true)
10015 if (nested_name_specifier_p)
10016 template_p = cp_parser_optional_template_keyword (parser);
10017 /* If there is a `::' operator or a nested-name-specifier, then we
10018 are definitely looking for a class-name. */
10019 if (global_scope_p || nested_name_specifier_p)
10020 return cp_parser_class_name (parser,
10021 /*typename_keyword_p=*/true,
10022 /*template_keyword_p=*/template_p,
10024 /*check_dependency_p=*/true,
10025 /*class_head_p=*/false,
10026 /*is_declaration=*/true);
10027 /* Otherwise, we could also be looking for an ordinary identifier. */
10028 cp_parser_parse_tentatively (parser);
10029 /* Try a class-name. */
10030 id = cp_parser_class_name (parser,
10031 /*typename_keyword_p=*/true,
10032 /*template_keyword_p=*/false,
10034 /*check_dependency_p=*/true,
10035 /*class_head_p=*/false,
10036 /*is_declaration=*/true);
10037 /* If we found one, we're done. */
10038 if (cp_parser_parse_definitely (parser))
10040 /* Otherwise, look for an ordinary identifier. */
10041 return cp_parser_identifier (parser);
10044 /* Overloading [gram.over] */
10046 /* Parse an operator-function-id.
10048 operator-function-id:
10051 Returns an IDENTIFIER_NODE for the operator which is a
10052 human-readable spelling of the identifier, e.g., `operator +'. */
10055 cp_parser_operator_function_id (cp_parser* parser)
10057 /* Look for the `operator' keyword. */
10058 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10059 return error_mark_node;
10060 /* And then the name of the operator itself. */
10061 return cp_parser_operator (parser);
10064 /* Parse an operator.
10067 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10068 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10069 || ++ -- , ->* -> () []
10076 Returns an IDENTIFIER_NODE for the operator which is a
10077 human-readable spelling of the identifier, e.g., `operator +'. */
10080 cp_parser_operator (cp_parser* parser)
10082 tree id = NULL_TREE;
10085 /* Peek at the next token. */
10086 token = cp_lexer_peek_token (parser->lexer);
10087 /* Figure out which operator we have. */
10088 switch (token->type)
10094 /* The keyword should be either `new' or `delete'. */
10095 if (token->keyword == RID_NEW)
10097 else if (token->keyword == RID_DELETE)
10102 /* Consume the `new' or `delete' token. */
10103 cp_lexer_consume_token (parser->lexer);
10105 /* Peek at the next token. */
10106 token = cp_lexer_peek_token (parser->lexer);
10107 /* If it's a `[' token then this is the array variant of the
10109 if (token->type == CPP_OPEN_SQUARE)
10111 /* Consume the `[' token. */
10112 cp_lexer_consume_token (parser->lexer);
10113 /* Look for the `]' token. */
10114 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10115 id = ansi_opname (op == NEW_EXPR
10116 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10118 /* Otherwise, we have the non-array variant. */
10120 id = ansi_opname (op);
10126 id = ansi_opname (PLUS_EXPR);
10130 id = ansi_opname (MINUS_EXPR);
10134 id = ansi_opname (MULT_EXPR);
10138 id = ansi_opname (TRUNC_DIV_EXPR);
10142 id = ansi_opname (TRUNC_MOD_EXPR);
10146 id = ansi_opname (BIT_XOR_EXPR);
10150 id = ansi_opname (BIT_AND_EXPR);
10154 id = ansi_opname (BIT_IOR_EXPR);
10158 id = ansi_opname (BIT_NOT_EXPR);
10162 id = ansi_opname (TRUTH_NOT_EXPR);
10166 id = ansi_assopname (NOP_EXPR);
10170 id = ansi_opname (LT_EXPR);
10174 id = ansi_opname (GT_EXPR);
10178 id = ansi_assopname (PLUS_EXPR);
10182 id = ansi_assopname (MINUS_EXPR);
10186 id = ansi_assopname (MULT_EXPR);
10190 id = ansi_assopname (TRUNC_DIV_EXPR);
10194 id = ansi_assopname (TRUNC_MOD_EXPR);
10198 id = ansi_assopname (BIT_XOR_EXPR);
10202 id = ansi_assopname (BIT_AND_EXPR);
10206 id = ansi_assopname (BIT_IOR_EXPR);
10210 id = ansi_opname (LSHIFT_EXPR);
10214 id = ansi_opname (RSHIFT_EXPR);
10217 case CPP_LSHIFT_EQ:
10218 id = ansi_assopname (LSHIFT_EXPR);
10221 case CPP_RSHIFT_EQ:
10222 id = ansi_assopname (RSHIFT_EXPR);
10226 id = ansi_opname (EQ_EXPR);
10230 id = ansi_opname (NE_EXPR);
10234 id = ansi_opname (LE_EXPR);
10237 case CPP_GREATER_EQ:
10238 id = ansi_opname (GE_EXPR);
10242 id = ansi_opname (TRUTH_ANDIF_EXPR);
10246 id = ansi_opname (TRUTH_ORIF_EXPR);
10249 case CPP_PLUS_PLUS:
10250 id = ansi_opname (POSTINCREMENT_EXPR);
10253 case CPP_MINUS_MINUS:
10254 id = ansi_opname (PREDECREMENT_EXPR);
10258 id = ansi_opname (COMPOUND_EXPR);
10261 case CPP_DEREF_STAR:
10262 id = ansi_opname (MEMBER_REF);
10266 id = ansi_opname (COMPONENT_REF);
10269 case CPP_OPEN_PAREN:
10270 /* Consume the `('. */
10271 cp_lexer_consume_token (parser->lexer);
10272 /* Look for the matching `)'. */
10273 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10274 return ansi_opname (CALL_EXPR);
10276 case CPP_OPEN_SQUARE:
10277 /* Consume the `['. */
10278 cp_lexer_consume_token (parser->lexer);
10279 /* Look for the matching `]'. */
10280 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10281 return ansi_opname (ARRAY_REF);
10284 /* Anything else is an error. */
10288 /* If we have selected an identifier, we need to consume the
10291 cp_lexer_consume_token (parser->lexer);
10292 /* Otherwise, no valid operator name was present. */
10295 cp_parser_error (parser, "expected operator");
10296 id = error_mark_node;
10302 /* Parse a template-declaration.
10304 template-declaration:
10305 export [opt] template < template-parameter-list > declaration
10307 If MEMBER_P is TRUE, this template-declaration occurs within a
10310 The grammar rule given by the standard isn't correct. What
10311 is really meant is:
10313 template-declaration:
10314 export [opt] template-parameter-list-seq
10315 decl-specifier-seq [opt] init-declarator [opt] ;
10316 export [opt] template-parameter-list-seq
10317 function-definition
10319 template-parameter-list-seq:
10320 template-parameter-list-seq [opt]
10321 template < template-parameter-list > */
10324 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10326 /* Check for `export'. */
10327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10329 /* Consume the `export' token. */
10330 cp_lexer_consume_token (parser->lexer);
10331 /* Warn that we do not support `export'. */
10332 warning (0, "keyword %<export%> not implemented, and will be ignored");
10335 cp_parser_template_declaration_after_export (parser, member_p);
10338 /* Parse a template-parameter-list.
10340 template-parameter-list:
10342 template-parameter-list , template-parameter
10344 Returns a TREE_LIST. Each node represents a template parameter.
10345 The nodes are connected via their TREE_CHAINs. */
10348 cp_parser_template_parameter_list (cp_parser* parser)
10350 tree parameter_list = NULL_TREE;
10352 begin_template_parm_list ();
10357 bool is_parameter_pack;
10358 location_t parm_loc;
10360 /* Parse the template-parameter. */
10361 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10362 parameter = cp_parser_template_parameter (parser,
10364 &is_parameter_pack);
10365 /* Add it to the list. */
10366 if (parameter != error_mark_node)
10367 parameter_list = process_template_parm (parameter_list,
10371 is_parameter_pack);
10374 tree err_parm = build_tree_list (parameter, parameter);
10375 TREE_VALUE (err_parm) = error_mark_node;
10376 parameter_list = chainon (parameter_list, err_parm);
10379 /* If the next token is not a `,', we're done. */
10380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10382 /* Otherwise, consume the `,' token. */
10383 cp_lexer_consume_token (parser->lexer);
10386 return end_template_parm_list (parameter_list);
10389 /* Parse a template-parameter.
10391 template-parameter:
10393 parameter-declaration
10395 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10396 the parameter. The TREE_PURPOSE is the default value, if any.
10397 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10398 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10399 set to true iff this parameter is a parameter pack. */
10402 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10403 bool *is_parameter_pack)
10406 cp_parameter_declarator *parameter_declarator;
10407 cp_declarator *id_declarator;
10410 /* Assume it is a type parameter or a template parameter. */
10411 *is_non_type = false;
10412 /* Assume it not a parameter pack. */
10413 *is_parameter_pack = false;
10414 /* Peek at the next token. */
10415 token = cp_lexer_peek_token (parser->lexer);
10416 /* If it is `class' or `template', we have a type-parameter. */
10417 if (token->keyword == RID_TEMPLATE)
10418 return cp_parser_type_parameter (parser, is_parameter_pack);
10419 /* If it is `class' or `typename' we do not know yet whether it is a
10420 type parameter or a non-type parameter. Consider:
10422 template <typename T, typename T::X X> ...
10426 template <class C, class D*> ...
10428 Here, the first parameter is a type parameter, and the second is
10429 a non-type parameter. We can tell by looking at the token after
10430 the identifier -- if it is a `,', `=', or `>' then we have a type
10432 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10434 /* Peek at the token after `class' or `typename'. */
10435 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10436 /* If it's an ellipsis, we have a template type parameter
10438 if (token->type == CPP_ELLIPSIS)
10439 return cp_parser_type_parameter (parser, is_parameter_pack);
10440 /* If it's an identifier, skip it. */
10441 if (token->type == CPP_NAME)
10442 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10443 /* Now, see if the token looks like the end of a template
10445 if (token->type == CPP_COMMA
10446 || token->type == CPP_EQ
10447 || token->type == CPP_GREATER)
10448 return cp_parser_type_parameter (parser, is_parameter_pack);
10451 /* Otherwise, it is a non-type parameter.
10455 When parsing a default template-argument for a non-type
10456 template-parameter, the first non-nested `>' is taken as the end
10457 of the template parameter-list rather than a greater-than
10459 *is_non_type = true;
10460 parameter_declarator
10461 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10462 /*parenthesized_p=*/NULL);
10464 /* If the parameter declaration is marked as a parameter pack, set
10465 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10466 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10468 if (parameter_declarator
10469 && parameter_declarator->declarator
10470 && parameter_declarator->declarator->parameter_pack_p)
10472 *is_parameter_pack = true;
10473 parameter_declarator->declarator->parameter_pack_p = false;
10476 /* If the next token is an ellipsis, and we don't already have it
10477 marked as a parameter pack, then we have a parameter pack (that
10478 has no declarator). */
10479 if (!*is_parameter_pack
10480 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10481 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10483 /* Consume the `...'. */
10484 cp_lexer_consume_token (parser->lexer);
10485 maybe_warn_variadic_templates ();
10487 *is_parameter_pack = true;
10489 /* We might end up with a pack expansion as the type of the non-type
10490 template parameter, in which case this is a non-type template
10492 else if (parameter_declarator
10493 && parameter_declarator->decl_specifiers.type
10494 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10496 *is_parameter_pack = true;
10497 parameter_declarator->decl_specifiers.type =
10498 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10501 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10503 /* Parameter packs cannot have default arguments. However, a
10504 user may try to do so, so we'll parse them and give an
10505 appropriate diagnostic here. */
10507 /* Consume the `='. */
10508 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10509 cp_lexer_consume_token (parser->lexer);
10511 /* Find the name of the parameter pack. */
10512 id_declarator = parameter_declarator->declarator;
10513 while (id_declarator && id_declarator->kind != cdk_id)
10514 id_declarator = id_declarator->declarator;
10516 if (id_declarator && id_declarator->kind == cdk_id)
10517 error_at (start_token->location,
10518 "template parameter pack %qD cannot have a default argument",
10519 id_declarator->u.id.unqualified_name);
10521 error_at (start_token->location,
10522 "template parameter pack cannot have a default argument");
10524 /* Parse the default argument, but throw away the result. */
10525 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10528 parm = grokdeclarator (parameter_declarator->declarator,
10529 ¶meter_declarator->decl_specifiers,
10530 TPARM, /*initialized=*/0,
10531 /*attrlist=*/NULL);
10532 if (parm == error_mark_node)
10533 return error_mark_node;
10535 return build_tree_list (parameter_declarator->default_argument, parm);
10538 /* Parse a type-parameter.
10541 class identifier [opt]
10542 class identifier [opt] = type-id
10543 typename identifier [opt]
10544 typename identifier [opt] = type-id
10545 template < template-parameter-list > class identifier [opt]
10546 template < template-parameter-list > class identifier [opt]
10549 GNU Extension (variadic templates):
10552 class ... identifier [opt]
10553 typename ... identifier [opt]
10555 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10556 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10557 the declaration of the parameter.
10559 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10562 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10567 /* Look for a keyword to tell us what kind of parameter this is. */
10568 token = cp_parser_require (parser, CPP_KEYWORD,
10569 "%<class%>, %<typename%>, or %<template%>");
10571 return error_mark_node;
10573 switch (token->keyword)
10579 tree default_argument;
10581 /* If the next token is an ellipsis, we have a template
10583 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10585 /* Consume the `...' token. */
10586 cp_lexer_consume_token (parser->lexer);
10587 maybe_warn_variadic_templates ();
10589 *is_parameter_pack = true;
10592 /* If the next token is an identifier, then it names the
10594 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10595 identifier = cp_parser_identifier (parser);
10597 identifier = NULL_TREE;
10599 /* Create the parameter. */
10600 parameter = finish_template_type_parm (class_type_node, identifier);
10602 /* If the next token is an `=', we have a default argument. */
10603 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10605 /* Consume the `=' token. */
10606 cp_lexer_consume_token (parser->lexer);
10607 /* Parse the default-argument. */
10608 push_deferring_access_checks (dk_no_deferred);
10609 default_argument = cp_parser_type_id (parser);
10611 /* Template parameter packs cannot have default
10613 if (*is_parameter_pack)
10616 error_at (token->location,
10617 "template parameter pack %qD cannot have a "
10618 "default argument", identifier);
10620 error_at (token->location,
10621 "template parameter packs cannot have "
10622 "default arguments");
10623 default_argument = NULL_TREE;
10625 pop_deferring_access_checks ();
10628 default_argument = NULL_TREE;
10630 /* Create the combined representation of the parameter and the
10631 default argument. */
10632 parameter = build_tree_list (default_argument, parameter);
10638 tree parameter_list;
10640 tree default_argument;
10642 /* Look for the `<'. */
10643 cp_parser_require (parser, CPP_LESS, "%<<%>");
10644 /* Parse the template-parameter-list. */
10645 parameter_list = cp_parser_template_parameter_list (parser);
10646 /* Look for the `>'. */
10647 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10648 /* Look for the `class' keyword. */
10649 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10650 /* If the next token is an ellipsis, we have a template
10652 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10654 /* Consume the `...' token. */
10655 cp_lexer_consume_token (parser->lexer);
10656 maybe_warn_variadic_templates ();
10658 *is_parameter_pack = true;
10660 /* If the next token is an `=', then there is a
10661 default-argument. If the next token is a `>', we are at
10662 the end of the parameter-list. If the next token is a `,',
10663 then we are at the end of this parameter. */
10664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10665 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10666 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10668 identifier = cp_parser_identifier (parser);
10669 /* Treat invalid names as if the parameter were nameless. */
10670 if (identifier == error_mark_node)
10671 identifier = NULL_TREE;
10674 identifier = NULL_TREE;
10676 /* Create the template parameter. */
10677 parameter = finish_template_template_parm (class_type_node,
10680 /* If the next token is an `=', then there is a
10681 default-argument. */
10682 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10686 /* Consume the `='. */
10687 cp_lexer_consume_token (parser->lexer);
10688 /* Parse the id-expression. */
10689 push_deferring_access_checks (dk_no_deferred);
10690 /* save token before parsing the id-expression, for error
10692 token = cp_lexer_peek_token (parser->lexer);
10694 = cp_parser_id_expression (parser,
10695 /*template_keyword_p=*/false,
10696 /*check_dependency_p=*/true,
10697 /*template_p=*/&is_template,
10698 /*declarator_p=*/false,
10699 /*optional_p=*/false);
10700 if (TREE_CODE (default_argument) == TYPE_DECL)
10701 /* If the id-expression was a template-id that refers to
10702 a template-class, we already have the declaration here,
10703 so no further lookup is needed. */
10706 /* Look up the name. */
10708 = cp_parser_lookup_name (parser, default_argument,
10710 /*is_template=*/is_template,
10711 /*is_namespace=*/false,
10712 /*check_dependency=*/true,
10713 /*ambiguous_decls=*/NULL,
10715 /* See if the default argument is valid. */
10717 = check_template_template_default_arg (default_argument);
10719 /* Template parameter packs cannot have default
10721 if (*is_parameter_pack)
10724 error_at (token->location,
10725 "template parameter pack %qD cannot "
10726 "have a default argument",
10729 error_at (token->location, "template parameter packs cannot "
10730 "have default arguments");
10731 default_argument = NULL_TREE;
10733 pop_deferring_access_checks ();
10736 default_argument = NULL_TREE;
10738 /* Create the combined representation of the parameter and the
10739 default argument. */
10740 parameter = build_tree_list (default_argument, parameter);
10745 gcc_unreachable ();
10752 /* Parse a template-id.
10755 template-name < template-argument-list [opt] >
10757 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10758 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10759 returned. Otherwise, if the template-name names a function, or set
10760 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10761 names a class, returns a TYPE_DECL for the specialization.
10763 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10764 uninstantiated templates. */
10767 cp_parser_template_id (cp_parser *parser,
10768 bool template_keyword_p,
10769 bool check_dependency_p,
10770 bool is_declaration)
10776 cp_token_position start_of_id = 0;
10777 deferred_access_check *chk;
10778 VEC (deferred_access_check,gc) *access_check;
10779 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10780 bool is_identifier;
10782 /* If the next token corresponds to a template-id, there is no need
10784 next_token = cp_lexer_peek_token (parser->lexer);
10785 if (next_token->type == CPP_TEMPLATE_ID)
10787 struct tree_check *check_value;
10789 /* Get the stored value. */
10790 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10791 /* Perform any access checks that were deferred. */
10792 access_check = check_value->checks;
10796 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10799 perform_or_defer_access_check (chk->binfo,
10804 /* Return the stored value. */
10805 return check_value->value;
10808 /* Avoid performing name lookup if there is no possibility of
10809 finding a template-id. */
10810 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10811 || (next_token->type == CPP_NAME
10812 && !cp_parser_nth_token_starts_template_argument_list_p
10815 cp_parser_error (parser, "expected template-id");
10816 return error_mark_node;
10819 /* Remember where the template-id starts. */
10820 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10821 start_of_id = cp_lexer_token_position (parser->lexer, false);
10823 push_deferring_access_checks (dk_deferred);
10825 /* Parse the template-name. */
10826 is_identifier = false;
10827 token = cp_lexer_peek_token (parser->lexer);
10828 templ = cp_parser_template_name (parser, template_keyword_p,
10829 check_dependency_p,
10832 if (templ == error_mark_node || is_identifier)
10834 pop_deferring_access_checks ();
10838 /* If we find the sequence `[:' after a template-name, it's probably
10839 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10840 parse correctly the argument list. */
10841 next_token = cp_lexer_peek_token (parser->lexer);
10842 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10843 if (next_token->type == CPP_OPEN_SQUARE
10844 && next_token->flags & DIGRAPH
10845 && next_token_2->type == CPP_COLON
10846 && !(next_token_2->flags & PREV_WHITE))
10848 cp_parser_parse_tentatively (parser);
10849 /* Change `:' into `::'. */
10850 next_token_2->type = CPP_SCOPE;
10851 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10853 cp_lexer_consume_token (parser->lexer);
10855 /* Parse the arguments. */
10856 arguments = cp_parser_enclosed_template_argument_list (parser);
10857 if (!cp_parser_parse_definitely (parser))
10859 /* If we couldn't parse an argument list, then we revert our changes
10860 and return simply an error. Maybe this is not a template-id
10862 next_token_2->type = CPP_COLON;
10863 cp_parser_error (parser, "expected %<<%>");
10864 pop_deferring_access_checks ();
10865 return error_mark_node;
10867 /* Otherwise, emit an error about the invalid digraph, but continue
10868 parsing because we got our argument list. */
10869 if (permerror (next_token->location,
10870 "%<<::%> cannot begin a template-argument list"))
10872 static bool hint = false;
10873 inform (next_token->location,
10874 "%<<:%> is an alternate spelling for %<[%>."
10875 " Insert whitespace between %<<%> and %<::%>");
10876 if (!hint && !flag_permissive)
10878 inform (next_token->location, "(if you use %<-fpermissive%>"
10879 " G++ will accept your code)");
10886 /* Look for the `<' that starts the template-argument-list. */
10887 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10889 pop_deferring_access_checks ();
10890 return error_mark_node;
10892 /* Parse the arguments. */
10893 arguments = cp_parser_enclosed_template_argument_list (parser);
10896 /* Build a representation of the specialization. */
10897 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10898 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10899 else if (DECL_CLASS_TEMPLATE_P (templ)
10900 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10902 bool entering_scope;
10903 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10904 template (rather than some instantiation thereof) only if
10905 is not nested within some other construct. For example, in
10906 "template <typename T> void f(T) { A<T>::", A<T> is just an
10907 instantiation of A. */
10908 entering_scope = (template_parm_scope_p ()
10909 && cp_lexer_next_token_is (parser->lexer,
10912 = finish_template_type (templ, arguments, entering_scope);
10916 /* If it's not a class-template or a template-template, it should be
10917 a function-template. */
10918 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10919 || TREE_CODE (templ) == OVERLOAD
10920 || BASELINK_P (templ)));
10922 template_id = lookup_template_function (templ, arguments);
10925 /* If parsing tentatively, replace the sequence of tokens that makes
10926 up the template-id with a CPP_TEMPLATE_ID token. That way,
10927 should we re-parse the token stream, we will not have to repeat
10928 the effort required to do the parse, nor will we issue duplicate
10929 error messages about problems during instantiation of the
10933 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10935 /* Reset the contents of the START_OF_ID token. */
10936 token->type = CPP_TEMPLATE_ID;
10937 /* Retrieve any deferred checks. Do not pop this access checks yet
10938 so the memory will not be reclaimed during token replacing below. */
10939 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10940 token->u.tree_check_value->value = template_id;
10941 token->u.tree_check_value->checks = get_deferred_access_checks ();
10942 token->keyword = RID_MAX;
10944 /* Purge all subsequent tokens. */
10945 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10947 /* ??? Can we actually assume that, if template_id ==
10948 error_mark_node, we will have issued a diagnostic to the
10949 user, as opposed to simply marking the tentative parse as
10951 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10952 error_at (token->location, "parse error in template argument list");
10955 pop_deferring_access_checks ();
10956 return template_id;
10959 /* Parse a template-name.
10964 The standard should actually say:
10968 operator-function-id
10970 A defect report has been filed about this issue.
10972 A conversion-function-id cannot be a template name because they cannot
10973 be part of a template-id. In fact, looking at this code:
10975 a.operator K<int>()
10977 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10978 It is impossible to call a templated conversion-function-id with an
10979 explicit argument list, since the only allowed template parameter is
10980 the type to which it is converting.
10982 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10983 `template' keyword, in a construction like:
10987 In that case `f' is taken to be a template-name, even though there
10988 is no way of knowing for sure.
10990 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10991 name refers to a set of overloaded functions, at least one of which
10992 is a template, or an IDENTIFIER_NODE with the name of the template,
10993 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10994 names are looked up inside uninstantiated templates. */
10997 cp_parser_template_name (cp_parser* parser,
10998 bool template_keyword_p,
10999 bool check_dependency_p,
11000 bool is_declaration,
11001 bool *is_identifier)
11006 cp_token *token = cp_lexer_peek_token (parser->lexer);
11008 /* If the next token is `operator', then we have either an
11009 operator-function-id or a conversion-function-id. */
11010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11012 /* We don't know whether we're looking at an
11013 operator-function-id or a conversion-function-id. */
11014 cp_parser_parse_tentatively (parser);
11015 /* Try an operator-function-id. */
11016 identifier = cp_parser_operator_function_id (parser);
11017 /* If that didn't work, try a conversion-function-id. */
11018 if (!cp_parser_parse_definitely (parser))
11020 cp_parser_error (parser, "expected template-name");
11021 return error_mark_node;
11024 /* Look for the identifier. */
11026 identifier = cp_parser_identifier (parser);
11028 /* If we didn't find an identifier, we don't have a template-id. */
11029 if (identifier == error_mark_node)
11030 return error_mark_node;
11032 /* If the name immediately followed the `template' keyword, then it
11033 is a template-name. However, if the next token is not `<', then
11034 we do not treat it as a template-name, since it is not being used
11035 as part of a template-id. This enables us to handle constructs
11038 template <typename T> struct S { S(); };
11039 template <typename T> S<T>::S();
11041 correctly. We would treat `S' as a template -- if it were `S<T>'
11042 -- but we do not if there is no `<'. */
11044 if (processing_template_decl
11045 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11047 /* In a declaration, in a dependent context, we pretend that the
11048 "template" keyword was present in order to improve error
11049 recovery. For example, given:
11051 template <typename T> void f(T::X<int>);
11053 we want to treat "X<int>" as a template-id. */
11055 && !template_keyword_p
11056 && parser->scope && TYPE_P (parser->scope)
11057 && check_dependency_p
11058 && dependent_scope_p (parser->scope)
11059 /* Do not do this for dtors (or ctors), since they never
11060 need the template keyword before their name. */
11061 && !constructor_name_p (identifier, parser->scope))
11063 cp_token_position start = 0;
11065 /* Explain what went wrong. */
11066 error_at (token->location, "non-template %qD used as template",
11068 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11069 parser->scope, identifier);
11070 /* If parsing tentatively, find the location of the "<" token. */
11071 if (cp_parser_simulate_error (parser))
11072 start = cp_lexer_token_position (parser->lexer, true);
11073 /* Parse the template arguments so that we can issue error
11074 messages about them. */
11075 cp_lexer_consume_token (parser->lexer);
11076 cp_parser_enclosed_template_argument_list (parser);
11077 /* Skip tokens until we find a good place from which to
11078 continue parsing. */
11079 cp_parser_skip_to_closing_parenthesis (parser,
11080 /*recovering=*/true,
11082 /*consume_paren=*/false);
11083 /* If parsing tentatively, permanently remove the
11084 template argument list. That will prevent duplicate
11085 error messages from being issued about the missing
11086 "template" keyword. */
11088 cp_lexer_purge_tokens_after (parser->lexer, start);
11090 *is_identifier = true;
11094 /* If the "template" keyword is present, then there is generally
11095 no point in doing name-lookup, so we just return IDENTIFIER.
11096 But, if the qualifying scope is non-dependent then we can
11097 (and must) do name-lookup normally. */
11098 if (template_keyword_p
11100 || (TYPE_P (parser->scope)
11101 && dependent_type_p (parser->scope))))
11105 /* Look up the name. */
11106 decl = cp_parser_lookup_name (parser, identifier,
11108 /*is_template=*/true,
11109 /*is_namespace=*/false,
11110 check_dependency_p,
11111 /*ambiguous_decls=*/NULL,
11114 /* If DECL is a template, then the name was a template-name. */
11115 if (TREE_CODE (decl) == TEMPLATE_DECL)
11119 tree fn = NULL_TREE;
11121 /* The standard does not explicitly indicate whether a name that
11122 names a set of overloaded declarations, some of which are
11123 templates, is a template-name. However, such a name should
11124 be a template-name; otherwise, there is no way to form a
11125 template-id for the overloaded templates. */
11126 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11127 if (TREE_CODE (fns) == OVERLOAD)
11128 for (fn = fns; fn; fn = OVL_NEXT (fn))
11129 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11134 /* The name does not name a template. */
11135 cp_parser_error (parser, "expected template-name");
11136 return error_mark_node;
11140 /* If DECL is dependent, and refers to a function, then just return
11141 its name; we will look it up again during template instantiation. */
11142 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11144 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11145 if (TYPE_P (scope) && dependent_type_p (scope))
11152 /* Parse a template-argument-list.
11154 template-argument-list:
11155 template-argument ... [opt]
11156 template-argument-list , template-argument ... [opt]
11158 Returns a TREE_VEC containing the arguments. */
11161 cp_parser_template_argument_list (cp_parser* parser)
11163 tree fixed_args[10];
11164 unsigned n_args = 0;
11165 unsigned alloced = 10;
11166 tree *arg_ary = fixed_args;
11168 bool saved_in_template_argument_list_p;
11170 bool saved_non_ice_p;
11172 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11173 parser->in_template_argument_list_p = true;
11174 /* Even if the template-id appears in an integral
11175 constant-expression, the contents of the argument list do
11177 saved_ice_p = parser->integral_constant_expression_p;
11178 parser->integral_constant_expression_p = false;
11179 saved_non_ice_p = parser->non_integral_constant_expression_p;
11180 parser->non_integral_constant_expression_p = false;
11181 /* Parse the arguments. */
11187 /* Consume the comma. */
11188 cp_lexer_consume_token (parser->lexer);
11190 /* Parse the template-argument. */
11191 argument = cp_parser_template_argument (parser);
11193 /* If the next token is an ellipsis, we're expanding a template
11195 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11197 if (argument == error_mark_node)
11199 cp_token *token = cp_lexer_peek_token (parser->lexer);
11200 error_at (token->location,
11201 "expected parameter pack before %<...%>");
11203 /* Consume the `...' token. */
11204 cp_lexer_consume_token (parser->lexer);
11206 /* Make the argument into a TYPE_PACK_EXPANSION or
11207 EXPR_PACK_EXPANSION. */
11208 argument = make_pack_expansion (argument);
11211 if (n_args == alloced)
11215 if (arg_ary == fixed_args)
11217 arg_ary = XNEWVEC (tree, alloced);
11218 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11221 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11223 arg_ary[n_args++] = argument;
11225 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11227 vec = make_tree_vec (n_args);
11230 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11232 if (arg_ary != fixed_args)
11234 parser->non_integral_constant_expression_p = saved_non_ice_p;
11235 parser->integral_constant_expression_p = saved_ice_p;
11236 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11240 /* Parse a template-argument.
11243 assignment-expression
11247 The representation is that of an assignment-expression, type-id, or
11248 id-expression -- except that the qualified id-expression is
11249 evaluated, so that the value returned is either a DECL or an
11252 Although the standard says "assignment-expression", it forbids
11253 throw-expressions or assignments in the template argument.
11254 Therefore, we use "conditional-expression" instead. */
11257 cp_parser_template_argument (cp_parser* parser)
11262 bool maybe_type_id = false;
11263 cp_token *token = NULL, *argument_start_token = NULL;
11266 /* There's really no way to know what we're looking at, so we just
11267 try each alternative in order.
11271 In a template-argument, an ambiguity between a type-id and an
11272 expression is resolved to a type-id, regardless of the form of
11273 the corresponding template-parameter.
11275 Therefore, we try a type-id first. */
11276 cp_parser_parse_tentatively (parser);
11277 argument = cp_parser_template_type_arg (parser);
11278 /* If there was no error parsing the type-id but the next token is a
11279 '>>', our behavior depends on which dialect of C++ we're
11280 parsing. In C++98, we probably found a typo for '> >'. But there
11281 are type-id which are also valid expressions. For instance:
11283 struct X { int operator >> (int); };
11284 template <int V> struct Foo {};
11287 Here 'X()' is a valid type-id of a function type, but the user just
11288 wanted to write the expression "X() >> 5". Thus, we remember that we
11289 found a valid type-id, but we still try to parse the argument as an
11290 expression to see what happens.
11292 In C++0x, the '>>' will be considered two separate '>'
11294 if (!cp_parser_error_occurred (parser)
11295 && cxx_dialect == cxx98
11296 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11298 maybe_type_id = true;
11299 cp_parser_abort_tentative_parse (parser);
11303 /* If the next token isn't a `,' or a `>', then this argument wasn't
11304 really finished. This means that the argument is not a valid
11306 if (!cp_parser_next_token_ends_template_argument_p (parser))
11307 cp_parser_error (parser, "expected template-argument");
11308 /* If that worked, we're done. */
11309 if (cp_parser_parse_definitely (parser))
11312 /* We're still not sure what the argument will be. */
11313 cp_parser_parse_tentatively (parser);
11314 /* Try a template. */
11315 argument_start_token = cp_lexer_peek_token (parser->lexer);
11316 argument = cp_parser_id_expression (parser,
11317 /*template_keyword_p=*/false,
11318 /*check_dependency_p=*/true,
11320 /*declarator_p=*/false,
11321 /*optional_p=*/false);
11322 /* If the next token isn't a `,' or a `>', then this argument wasn't
11323 really finished. */
11324 if (!cp_parser_next_token_ends_template_argument_p (parser))
11325 cp_parser_error (parser, "expected template-argument");
11326 if (!cp_parser_error_occurred (parser))
11328 /* Figure out what is being referred to. If the id-expression
11329 was for a class template specialization, then we will have a
11330 TYPE_DECL at this point. There is no need to do name lookup
11331 at this point in that case. */
11332 if (TREE_CODE (argument) != TYPE_DECL)
11333 argument = cp_parser_lookup_name (parser, argument,
11335 /*is_template=*/template_p,
11336 /*is_namespace=*/false,
11337 /*check_dependency=*/true,
11338 /*ambiguous_decls=*/NULL,
11339 argument_start_token->location);
11340 if (TREE_CODE (argument) != TEMPLATE_DECL
11341 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11342 cp_parser_error (parser, "expected template-name");
11344 if (cp_parser_parse_definitely (parser))
11346 /* It must be a non-type argument. There permitted cases are given
11347 in [temp.arg.nontype]:
11349 -- an integral constant-expression of integral or enumeration
11352 -- the name of a non-type template-parameter; or
11354 -- the name of an object or function with external linkage...
11356 -- the address of an object or function with external linkage...
11358 -- a pointer to member... */
11359 /* Look for a non-type template parameter. */
11360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11362 cp_parser_parse_tentatively (parser);
11363 argument = cp_parser_primary_expression (parser,
11364 /*address_p=*/false,
11366 /*template_arg_p=*/true,
11368 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11369 || !cp_parser_next_token_ends_template_argument_p (parser))
11370 cp_parser_simulate_error (parser);
11371 if (cp_parser_parse_definitely (parser))
11375 /* If the next token is "&", the argument must be the address of an
11376 object or function with external linkage. */
11377 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11379 cp_lexer_consume_token (parser->lexer);
11380 /* See if we might have an id-expression. */
11381 token = cp_lexer_peek_token (parser->lexer);
11382 if (token->type == CPP_NAME
11383 || token->keyword == RID_OPERATOR
11384 || token->type == CPP_SCOPE
11385 || token->type == CPP_TEMPLATE_ID
11386 || token->type == CPP_NESTED_NAME_SPECIFIER)
11388 cp_parser_parse_tentatively (parser);
11389 argument = cp_parser_primary_expression (parser,
11392 /*template_arg_p=*/true,
11394 if (cp_parser_error_occurred (parser)
11395 || !cp_parser_next_token_ends_template_argument_p (parser))
11396 cp_parser_abort_tentative_parse (parser);
11401 if (TREE_CODE (argument) == INDIRECT_REF)
11403 gcc_assert (REFERENCE_REF_P (argument));
11404 argument = TREE_OPERAND (argument, 0);
11407 /* If we're in a template, we represent a qualified-id referring
11408 to a static data member as a SCOPE_REF even if the scope isn't
11409 dependent so that we can check access control later. */
11411 if (TREE_CODE (probe) == SCOPE_REF)
11412 probe = TREE_OPERAND (probe, 1);
11413 if (TREE_CODE (probe) == VAR_DECL)
11415 /* A variable without external linkage might still be a
11416 valid constant-expression, so no error is issued here
11417 if the external-linkage check fails. */
11418 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11419 cp_parser_simulate_error (parser);
11421 else if (is_overloaded_fn (argument))
11422 /* All overloaded functions are allowed; if the external
11423 linkage test does not pass, an error will be issued
11427 && (TREE_CODE (argument) == OFFSET_REF
11428 || TREE_CODE (argument) == SCOPE_REF))
11429 /* A pointer-to-member. */
11431 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11434 cp_parser_simulate_error (parser);
11436 if (cp_parser_parse_definitely (parser))
11439 argument = build_x_unary_op (ADDR_EXPR, argument,
11440 tf_warning_or_error);
11445 /* If the argument started with "&", there are no other valid
11446 alternatives at this point. */
11449 cp_parser_error (parser, "invalid non-type template argument");
11450 return error_mark_node;
11453 /* If the argument wasn't successfully parsed as a type-id followed
11454 by '>>', the argument can only be a constant expression now.
11455 Otherwise, we try parsing the constant-expression tentatively,
11456 because the argument could really be a type-id. */
11458 cp_parser_parse_tentatively (parser);
11459 argument = cp_parser_constant_expression (parser,
11460 /*allow_non_constant_p=*/false,
11461 /*non_constant_p=*/NULL);
11462 argument = fold_non_dependent_expr (argument);
11463 if (!maybe_type_id)
11465 if (!cp_parser_next_token_ends_template_argument_p (parser))
11466 cp_parser_error (parser, "expected template-argument");
11467 if (cp_parser_parse_definitely (parser))
11469 /* We did our best to parse the argument as a non type-id, but that
11470 was the only alternative that matched (albeit with a '>' after
11471 it). We can assume it's just a typo from the user, and a
11472 diagnostic will then be issued. */
11473 return cp_parser_template_type_arg (parser);
11476 /* Parse an explicit-instantiation.
11478 explicit-instantiation:
11479 template declaration
11481 Although the standard says `declaration', what it really means is:
11483 explicit-instantiation:
11484 template decl-specifier-seq [opt] declarator [opt] ;
11486 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11487 supposed to be allowed. A defect report has been filed about this
11492 explicit-instantiation:
11493 storage-class-specifier template
11494 decl-specifier-seq [opt] declarator [opt] ;
11495 function-specifier template
11496 decl-specifier-seq [opt] declarator [opt] ; */
11499 cp_parser_explicit_instantiation (cp_parser* parser)
11501 int declares_class_or_enum;
11502 cp_decl_specifier_seq decl_specifiers;
11503 tree extension_specifier = NULL_TREE;
11506 /* Look for an (optional) storage-class-specifier or
11507 function-specifier. */
11508 if (cp_parser_allow_gnu_extensions_p (parser))
11510 extension_specifier
11511 = cp_parser_storage_class_specifier_opt (parser);
11512 if (!extension_specifier)
11513 extension_specifier
11514 = cp_parser_function_specifier_opt (parser,
11515 /*decl_specs=*/NULL);
11518 /* Look for the `template' keyword. */
11519 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11520 /* Let the front end know that we are processing an explicit
11522 begin_explicit_instantiation ();
11523 /* [temp.explicit] says that we are supposed to ignore access
11524 control while processing explicit instantiation directives. */
11525 push_deferring_access_checks (dk_no_check);
11526 /* Parse a decl-specifier-seq. */
11527 token = cp_lexer_peek_token (parser->lexer);
11528 cp_parser_decl_specifier_seq (parser,
11529 CP_PARSER_FLAGS_OPTIONAL,
11531 &declares_class_or_enum);
11532 /* If there was exactly one decl-specifier, and it declared a class,
11533 and there's no declarator, then we have an explicit type
11535 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11539 type = check_tag_decl (&decl_specifiers);
11540 /* Turn access control back on for names used during
11541 template instantiation. */
11542 pop_deferring_access_checks ();
11544 do_type_instantiation (type, extension_specifier,
11545 /*complain=*/tf_error);
11549 cp_declarator *declarator;
11552 /* Parse the declarator. */
11554 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11555 /*ctor_dtor_or_conv_p=*/NULL,
11556 /*parenthesized_p=*/NULL,
11557 /*member_p=*/false);
11558 if (declares_class_or_enum & 2)
11559 cp_parser_check_for_definition_in_return_type (declarator,
11560 decl_specifiers.type,
11561 decl_specifiers.type_location);
11562 if (declarator != cp_error_declarator)
11564 decl = grokdeclarator (declarator, &decl_specifiers,
11565 NORMAL, 0, &decl_specifiers.attributes);
11566 /* Turn access control back on for names used during
11567 template instantiation. */
11568 pop_deferring_access_checks ();
11569 /* Do the explicit instantiation. */
11570 do_decl_instantiation (decl, extension_specifier);
11574 pop_deferring_access_checks ();
11575 /* Skip the body of the explicit instantiation. */
11576 cp_parser_skip_to_end_of_statement (parser);
11579 /* We're done with the instantiation. */
11580 end_explicit_instantiation ();
11582 cp_parser_consume_semicolon_at_end_of_statement (parser);
11585 /* Parse an explicit-specialization.
11587 explicit-specialization:
11588 template < > declaration
11590 Although the standard says `declaration', what it really means is:
11592 explicit-specialization:
11593 template <> decl-specifier [opt] init-declarator [opt] ;
11594 template <> function-definition
11595 template <> explicit-specialization
11596 template <> template-declaration */
11599 cp_parser_explicit_specialization (cp_parser* parser)
11601 bool need_lang_pop;
11602 cp_token *token = cp_lexer_peek_token (parser->lexer);
11604 /* Look for the `template' keyword. */
11605 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11606 /* Look for the `<'. */
11607 cp_parser_require (parser, CPP_LESS, "%<<%>");
11608 /* Look for the `>'. */
11609 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11610 /* We have processed another parameter list. */
11611 ++parser->num_template_parameter_lists;
11614 A template ... explicit specialization ... shall not have C
11616 if (current_lang_name == lang_name_c)
11618 error_at (token->location, "template specialization with C linkage");
11619 /* Give it C++ linkage to avoid confusing other parts of the
11621 push_lang_context (lang_name_cplusplus);
11622 need_lang_pop = true;
11625 need_lang_pop = false;
11626 /* Let the front end know that we are beginning a specialization. */
11627 if (!begin_specialization ())
11629 end_specialization ();
11633 /* If the next keyword is `template', we need to figure out whether
11634 or not we're looking a template-declaration. */
11635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11637 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11638 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11639 cp_parser_template_declaration_after_export (parser,
11640 /*member_p=*/false);
11642 cp_parser_explicit_specialization (parser);
11645 /* Parse the dependent declaration. */
11646 cp_parser_single_declaration (parser,
11648 /*member_p=*/false,
11649 /*explicit_specialization_p=*/true,
11650 /*friend_p=*/NULL);
11651 /* We're done with the specialization. */
11652 end_specialization ();
11653 /* For the erroneous case of a template with C linkage, we pushed an
11654 implicit C++ linkage scope; exit that scope now. */
11656 pop_lang_context ();
11657 /* We're done with this parameter list. */
11658 --parser->num_template_parameter_lists;
11661 /* Parse a type-specifier.
11664 simple-type-specifier
11667 elaborated-type-specifier
11675 Returns a representation of the type-specifier. For a
11676 class-specifier, enum-specifier, or elaborated-type-specifier, a
11677 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11679 The parser flags FLAGS is used to control type-specifier parsing.
11681 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11682 in a decl-specifier-seq.
11684 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11685 class-specifier, enum-specifier, or elaborated-type-specifier, then
11686 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11687 if a type is declared; 2 if it is defined. Otherwise, it is set to
11690 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11691 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11692 is set to FALSE. */
11695 cp_parser_type_specifier (cp_parser* parser,
11696 cp_parser_flags flags,
11697 cp_decl_specifier_seq *decl_specs,
11698 bool is_declaration,
11699 int* declares_class_or_enum,
11700 bool* is_cv_qualifier)
11702 tree type_spec = NULL_TREE;
11705 cp_decl_spec ds = ds_last;
11707 /* Assume this type-specifier does not declare a new type. */
11708 if (declares_class_or_enum)
11709 *declares_class_or_enum = 0;
11710 /* And that it does not specify a cv-qualifier. */
11711 if (is_cv_qualifier)
11712 *is_cv_qualifier = false;
11713 /* Peek at the next token. */
11714 token = cp_lexer_peek_token (parser->lexer);
11716 /* If we're looking at a keyword, we can use that to guide the
11717 production we choose. */
11718 keyword = token->keyword;
11722 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11723 goto elaborated_type_specifier;
11725 /* Look for the enum-specifier. */
11726 type_spec = cp_parser_enum_specifier (parser);
11727 /* If that worked, we're done. */
11730 if (declares_class_or_enum)
11731 *declares_class_or_enum = 2;
11733 cp_parser_set_decl_spec_type (decl_specs,
11736 /*user_defined_p=*/true);
11740 goto elaborated_type_specifier;
11742 /* Any of these indicate either a class-specifier, or an
11743 elaborated-type-specifier. */
11747 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11748 goto elaborated_type_specifier;
11750 /* Parse tentatively so that we can back up if we don't find a
11751 class-specifier. */
11752 cp_parser_parse_tentatively (parser);
11753 /* Look for the class-specifier. */
11754 type_spec = cp_parser_class_specifier (parser);
11755 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11756 /* If that worked, we're done. */
11757 if (cp_parser_parse_definitely (parser))
11759 if (declares_class_or_enum)
11760 *declares_class_or_enum = 2;
11762 cp_parser_set_decl_spec_type (decl_specs,
11765 /*user_defined_p=*/true);
11769 /* Fall through. */
11770 elaborated_type_specifier:
11771 /* We're declaring (not defining) a class or enum. */
11772 if (declares_class_or_enum)
11773 *declares_class_or_enum = 1;
11775 /* Fall through. */
11777 /* Look for an elaborated-type-specifier. */
11779 = (cp_parser_elaborated_type_specifier
11781 decl_specs && decl_specs->specs[(int) ds_friend],
11784 cp_parser_set_decl_spec_type (decl_specs,
11787 /*user_defined_p=*/true);
11792 if (is_cv_qualifier)
11793 *is_cv_qualifier = true;
11798 if (is_cv_qualifier)
11799 *is_cv_qualifier = true;
11804 if (is_cv_qualifier)
11805 *is_cv_qualifier = true;
11809 /* The `__complex__' keyword is a GNU extension. */
11817 /* Handle simple keywords. */
11822 ++decl_specs->specs[(int)ds];
11823 decl_specs->any_specifiers_p = true;
11825 return cp_lexer_consume_token (parser->lexer)->u.value;
11828 /* If we do not already have a type-specifier, assume we are looking
11829 at a simple-type-specifier. */
11830 type_spec = cp_parser_simple_type_specifier (parser,
11834 /* If we didn't find a type-specifier, and a type-specifier was not
11835 optional in this context, issue an error message. */
11836 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11838 cp_parser_error (parser, "expected type specifier");
11839 return error_mark_node;
11845 /* Parse a simple-type-specifier.
11847 simple-type-specifier:
11848 :: [opt] nested-name-specifier [opt] type-name
11849 :: [opt] nested-name-specifier template template-id
11864 simple-type-specifier:
11866 decltype ( expression )
11872 simple-type-specifier:
11873 __typeof__ unary-expression
11874 __typeof__ ( type-id )
11876 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11877 appropriately updated. */
11880 cp_parser_simple_type_specifier (cp_parser* parser,
11881 cp_decl_specifier_seq *decl_specs,
11882 cp_parser_flags flags)
11884 tree type = NULL_TREE;
11887 /* Peek at the next token. */
11888 token = cp_lexer_peek_token (parser->lexer);
11890 /* If we're looking at a keyword, things are easy. */
11891 switch (token->keyword)
11895 decl_specs->explicit_char_p = true;
11896 type = char_type_node;
11899 type = char16_type_node;
11902 type = char32_type_node;
11905 type = wchar_type_node;
11908 type = boolean_type_node;
11912 ++decl_specs->specs[(int) ds_short];
11913 type = short_integer_type_node;
11917 decl_specs->explicit_int_p = true;
11918 type = integer_type_node;
11922 ++decl_specs->specs[(int) ds_long];
11923 type = long_integer_type_node;
11927 ++decl_specs->specs[(int) ds_signed];
11928 type = integer_type_node;
11932 ++decl_specs->specs[(int) ds_unsigned];
11933 type = unsigned_type_node;
11936 type = float_type_node;
11939 type = double_type_node;
11942 type = void_type_node;
11946 maybe_warn_cpp0x (CPP0X_AUTO);
11947 type = make_auto ();
11951 /* Parse the `decltype' type. */
11952 type = cp_parser_decltype (parser);
11955 cp_parser_set_decl_spec_type (decl_specs, type,
11957 /*user_defined_p=*/true);
11962 /* Consume the `typeof' token. */
11963 cp_lexer_consume_token (parser->lexer);
11964 /* Parse the operand to `typeof'. */
11965 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11966 /* If it is not already a TYPE, take its type. */
11967 if (!TYPE_P (type))
11968 type = finish_typeof (type);
11971 cp_parser_set_decl_spec_type (decl_specs, type,
11973 /*user_defined_p=*/true);
11981 /* If the type-specifier was for a built-in type, we're done. */
11986 /* Record the type. */
11988 && (token->keyword != RID_SIGNED
11989 && token->keyword != RID_UNSIGNED
11990 && token->keyword != RID_SHORT
11991 && token->keyword != RID_LONG))
11992 cp_parser_set_decl_spec_type (decl_specs,
11995 /*user_defined=*/false);
11997 decl_specs->any_specifiers_p = true;
11999 /* Consume the token. */
12000 id = cp_lexer_consume_token (parser->lexer)->u.value;
12002 /* There is no valid C++ program where a non-template type is
12003 followed by a "<". That usually indicates that the user thought
12004 that the type was a template. */
12005 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12007 return TYPE_NAME (type);
12010 /* The type-specifier must be a user-defined type. */
12011 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12016 /* Don't gobble tokens or issue error messages if this is an
12017 optional type-specifier. */
12018 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12019 cp_parser_parse_tentatively (parser);
12021 /* Look for the optional `::' operator. */
12023 = (cp_parser_global_scope_opt (parser,
12024 /*current_scope_valid_p=*/false)
12026 /* Look for the nested-name specifier. */
12028 = (cp_parser_nested_name_specifier_opt (parser,
12029 /*typename_keyword_p=*/false,
12030 /*check_dependency_p=*/true,
12032 /*is_declaration=*/false)
12034 token = cp_lexer_peek_token (parser->lexer);
12035 /* If we have seen a nested-name-specifier, and the next token
12036 is `template', then we are using the template-id production. */
12038 && cp_parser_optional_template_keyword (parser))
12040 /* Look for the template-id. */
12041 type = cp_parser_template_id (parser,
12042 /*template_keyword_p=*/true,
12043 /*check_dependency_p=*/true,
12044 /*is_declaration=*/false);
12045 /* If the template-id did not name a type, we are out of
12047 if (TREE_CODE (type) != TYPE_DECL)
12049 cp_parser_error (parser, "expected template-id for type");
12053 /* Otherwise, look for a type-name. */
12055 type = cp_parser_type_name (parser);
12056 /* Keep track of all name-lookups performed in class scopes. */
12060 && TREE_CODE (type) == TYPE_DECL
12061 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12062 maybe_note_name_used_in_class (DECL_NAME (type), type);
12063 /* If it didn't work out, we don't have a TYPE. */
12064 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12065 && !cp_parser_parse_definitely (parser))
12067 if (type && decl_specs)
12068 cp_parser_set_decl_spec_type (decl_specs, type,
12070 /*user_defined=*/true);
12073 /* If we didn't get a type-name, issue an error message. */
12074 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12076 cp_parser_error (parser, "expected type-name");
12077 return error_mark_node;
12080 /* There is no valid C++ program where a non-template type is
12081 followed by a "<". That usually indicates that the user thought
12082 that the type was a template. */
12083 if (type && type != error_mark_node)
12085 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12086 If it is, then the '<'...'>' enclose protocol names rather than
12087 template arguments, and so everything is fine. */
12088 if (c_dialect_objc ()
12089 && (objc_is_id (type) || objc_is_class_name (type)))
12091 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12092 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12094 /* Clobber the "unqualified" type previously entered into
12095 DECL_SPECS with the new, improved protocol-qualified version. */
12097 decl_specs->type = qual_type;
12102 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12109 /* Parse a type-name.
12122 Returns a TYPE_DECL for the type. */
12125 cp_parser_type_name (cp_parser* parser)
12129 /* We can't know yet whether it is a class-name or not. */
12130 cp_parser_parse_tentatively (parser);
12131 /* Try a class-name. */
12132 type_decl = cp_parser_class_name (parser,
12133 /*typename_keyword_p=*/false,
12134 /*template_keyword_p=*/false,
12136 /*check_dependency_p=*/true,
12137 /*class_head_p=*/false,
12138 /*is_declaration=*/false);
12139 /* If it's not a class-name, keep looking. */
12140 if (!cp_parser_parse_definitely (parser))
12142 /* It must be a typedef-name or an enum-name. */
12143 return cp_parser_nonclass_name (parser);
12149 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12157 Returns a TYPE_DECL for the type. */
12160 cp_parser_nonclass_name (cp_parser* parser)
12165 cp_token *token = cp_lexer_peek_token (parser->lexer);
12166 identifier = cp_parser_identifier (parser);
12167 if (identifier == error_mark_node)
12168 return error_mark_node;
12170 /* Look up the type-name. */
12171 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12173 if (TREE_CODE (type_decl) != TYPE_DECL
12174 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12176 /* See if this is an Objective-C type. */
12177 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12178 tree type = objc_get_protocol_qualified_type (identifier, protos);
12180 type_decl = TYPE_NAME (type);
12183 /* Issue an error if we did not find a type-name. */
12184 if (TREE_CODE (type_decl) != TYPE_DECL)
12186 if (!cp_parser_simulate_error (parser))
12187 cp_parser_name_lookup_error (parser, identifier, type_decl,
12188 "is not a type", token->location);
12189 return error_mark_node;
12191 /* Remember that the name was used in the definition of the
12192 current class so that we can check later to see if the
12193 meaning would have been different after the class was
12194 entirely defined. */
12195 else if (type_decl != error_mark_node
12197 maybe_note_name_used_in_class (identifier, type_decl);
12202 /* Parse an elaborated-type-specifier. Note that the grammar given
12203 here incorporates the resolution to DR68.
12205 elaborated-type-specifier:
12206 class-key :: [opt] nested-name-specifier [opt] identifier
12207 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12208 enum-key :: [opt] nested-name-specifier [opt] identifier
12209 typename :: [opt] nested-name-specifier identifier
12210 typename :: [opt] nested-name-specifier template [opt]
12215 elaborated-type-specifier:
12216 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12217 class-key attributes :: [opt] nested-name-specifier [opt]
12218 template [opt] template-id
12219 enum attributes :: [opt] nested-name-specifier [opt] identifier
12221 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12222 declared `friend'. If IS_DECLARATION is TRUE, then this
12223 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12224 something is being declared.
12226 Returns the TYPE specified. */
12229 cp_parser_elaborated_type_specifier (cp_parser* parser,
12231 bool is_declaration)
12233 enum tag_types tag_type;
12235 tree type = NULL_TREE;
12236 tree attributes = NULL_TREE;
12238 cp_token *token = NULL;
12240 /* See if we're looking at the `enum' keyword. */
12241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12243 /* Consume the `enum' token. */
12244 cp_lexer_consume_token (parser->lexer);
12245 /* Remember that it's an enumeration type. */
12246 tag_type = enum_type;
12247 /* Parse the optional `struct' or `class' key (for C++0x scoped
12249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12250 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12252 if (cxx_dialect == cxx98)
12253 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12255 /* Consume the `struct' or `class'. */
12256 cp_lexer_consume_token (parser->lexer);
12258 /* Parse the attributes. */
12259 attributes = cp_parser_attributes_opt (parser);
12261 /* Or, it might be `typename'. */
12262 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12265 /* Consume the `typename' token. */
12266 cp_lexer_consume_token (parser->lexer);
12267 /* Remember that it's a `typename' type. */
12268 tag_type = typename_type;
12270 /* Otherwise it must be a class-key. */
12273 tag_type = cp_parser_class_key (parser);
12274 if (tag_type == none_type)
12275 return error_mark_node;
12276 /* Parse the attributes. */
12277 attributes = cp_parser_attributes_opt (parser);
12280 /* Look for the `::' operator. */
12281 globalscope = cp_parser_global_scope_opt (parser,
12282 /*current_scope_valid_p=*/false);
12283 /* Look for the nested-name-specifier. */
12284 if (tag_type == typename_type && !globalscope)
12286 if (!cp_parser_nested_name_specifier (parser,
12287 /*typename_keyword_p=*/true,
12288 /*check_dependency_p=*/true,
12291 return error_mark_node;
12294 /* Even though `typename' is not present, the proposed resolution
12295 to Core Issue 180 says that in `class A<T>::B', `B' should be
12296 considered a type-name, even if `A<T>' is dependent. */
12297 cp_parser_nested_name_specifier_opt (parser,
12298 /*typename_keyword_p=*/true,
12299 /*check_dependency_p=*/true,
12302 /* For everything but enumeration types, consider a template-id.
12303 For an enumeration type, consider only a plain identifier. */
12304 if (tag_type != enum_type)
12306 bool template_p = false;
12309 /* Allow the `template' keyword. */
12310 template_p = cp_parser_optional_template_keyword (parser);
12311 /* If we didn't see `template', we don't know if there's a
12312 template-id or not. */
12314 cp_parser_parse_tentatively (parser);
12315 /* Parse the template-id. */
12316 token = cp_lexer_peek_token (parser->lexer);
12317 decl = cp_parser_template_id (parser, template_p,
12318 /*check_dependency_p=*/true,
12320 /* If we didn't find a template-id, look for an ordinary
12322 if (!template_p && !cp_parser_parse_definitely (parser))
12324 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12325 in effect, then we must assume that, upon instantiation, the
12326 template will correspond to a class. */
12327 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12328 && tag_type == typename_type)
12329 type = make_typename_type (parser->scope, decl,
12331 /*complain=*/tf_error);
12332 /* If the `typename' keyword is in effect and DECL is not a type
12333 decl. Then type is non existant. */
12334 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12337 type = TREE_TYPE (decl);
12342 token = cp_lexer_peek_token (parser->lexer);
12343 identifier = cp_parser_identifier (parser);
12345 if (identifier == error_mark_node)
12347 parser->scope = NULL_TREE;
12348 return error_mark_node;
12351 /* For a `typename', we needn't call xref_tag. */
12352 if (tag_type == typename_type
12353 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12354 return cp_parser_make_typename_type (parser, parser->scope,
12357 /* Look up a qualified name in the usual way. */
12361 tree ambiguous_decls;
12363 decl = cp_parser_lookup_name (parser, identifier,
12365 /*is_template=*/false,
12366 /*is_namespace=*/false,
12367 /*check_dependency=*/true,
12371 /* If the lookup was ambiguous, an error will already have been
12373 if (ambiguous_decls)
12374 return error_mark_node;
12376 /* If we are parsing friend declaration, DECL may be a
12377 TEMPLATE_DECL tree node here. However, we need to check
12378 whether this TEMPLATE_DECL results in valid code. Consider
12379 the following example:
12382 template <class T> class C {};
12385 template <class T> friend class N::C; // #1, valid code
12387 template <class T> class Y {
12388 friend class N::C; // #2, invalid code
12391 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12392 name lookup of `N::C'. We see that friend declaration must
12393 be template for the code to be valid. Note that
12394 processing_template_decl does not work here since it is
12395 always 1 for the above two cases. */
12397 decl = (cp_parser_maybe_treat_template_as_class
12398 (decl, /*tag_name_p=*/is_friend
12399 && parser->num_template_parameter_lists));
12401 if (TREE_CODE (decl) != TYPE_DECL)
12403 cp_parser_diagnose_invalid_type_name (parser,
12407 return error_mark_node;
12410 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12412 bool allow_template = (parser->num_template_parameter_lists
12413 || DECL_SELF_REFERENCE_P (decl));
12414 type = check_elaborated_type_specifier (tag_type, decl,
12417 if (type == error_mark_node)
12418 return error_mark_node;
12421 /* Forward declarations of nested types, such as
12426 are invalid unless all components preceding the final '::'
12427 are complete. If all enclosing types are complete, these
12428 declarations become merely pointless.
12430 Invalid forward declarations of nested types are errors
12431 caught elsewhere in parsing. Those that are pointless arrive
12434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12435 && !is_friend && !processing_explicit_instantiation)
12436 warning (0, "declaration %qD does not declare anything", decl);
12438 type = TREE_TYPE (decl);
12442 /* An elaborated-type-specifier sometimes introduces a new type and
12443 sometimes names an existing type. Normally, the rule is that it
12444 introduces a new type only if there is not an existing type of
12445 the same name already in scope. For example, given:
12448 void f() { struct S s; }
12450 the `struct S' in the body of `f' is the same `struct S' as in
12451 the global scope; the existing definition is used. However, if
12452 there were no global declaration, this would introduce a new
12453 local class named `S'.
12455 An exception to this rule applies to the following code:
12457 namespace N { struct S; }
12459 Here, the elaborated-type-specifier names a new type
12460 unconditionally; even if there is already an `S' in the
12461 containing scope this declaration names a new type.
12462 This exception only applies if the elaborated-type-specifier
12463 forms the complete declaration:
12467 A declaration consisting solely of `class-key identifier ;' is
12468 either a redeclaration of the name in the current scope or a
12469 forward declaration of the identifier as a class name. It
12470 introduces the name into the current scope.
12472 We are in this situation precisely when the next token is a `;'.
12474 An exception to the exception is that a `friend' declaration does
12475 *not* name a new type; i.e., given:
12477 struct S { friend struct T; };
12479 `T' is not a new type in the scope of `S'.
12481 Also, `new struct S' or `sizeof (struct S)' never results in the
12482 definition of a new type; a new type can only be declared in a
12483 declaration context. */
12489 /* Friends have special name lookup rules. */
12490 ts = ts_within_enclosing_non_class;
12491 else if (is_declaration
12492 && cp_lexer_next_token_is (parser->lexer,
12494 /* This is a `class-key identifier ;' */
12500 (parser->num_template_parameter_lists
12501 && (cp_parser_next_token_starts_class_definition_p (parser)
12502 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12503 /* An unqualified name was used to reference this type, so
12504 there were no qualifying templates. */
12505 if (!cp_parser_check_template_parameters (parser,
12506 /*num_templates=*/0,
12508 /*declarator=*/NULL))
12509 return error_mark_node;
12510 type = xref_tag (tag_type, identifier, ts, template_p);
12514 if (type == error_mark_node)
12515 return error_mark_node;
12517 /* Allow attributes on forward declarations of classes. */
12520 if (TREE_CODE (type) == TYPENAME_TYPE)
12521 warning (OPT_Wattributes,
12522 "attributes ignored on uninstantiated type");
12523 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12524 && ! processing_explicit_instantiation)
12525 warning (OPT_Wattributes,
12526 "attributes ignored on template instantiation");
12527 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12528 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12530 warning (OPT_Wattributes,
12531 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12534 if (tag_type != enum_type)
12535 cp_parser_check_class_key (tag_type, type);
12537 /* A "<" cannot follow an elaborated type specifier. If that
12538 happens, the user was probably trying to form a template-id. */
12539 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12544 /* Parse an enum-specifier.
12547 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12552 enum struct [C++0x]
12555 : type-specifier-seq
12558 enum-key attributes[opt] identifier [opt] enum-base [opt]
12559 { enumerator-list [opt] }attributes[opt]
12561 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12562 if the token stream isn't an enum-specifier after all. */
12565 cp_parser_enum_specifier (cp_parser* parser)
12570 bool scoped_enum_p = false;
12571 bool has_underlying_type = false;
12572 tree underlying_type = NULL_TREE;
12574 /* Parse tentatively so that we can back up if we don't find a
12576 cp_parser_parse_tentatively (parser);
12578 /* Caller guarantees that the current token is 'enum', an identifier
12579 possibly follows, and the token after that is an opening brace.
12580 If we don't have an identifier, fabricate an anonymous name for
12581 the enumeration being defined. */
12582 cp_lexer_consume_token (parser->lexer);
12584 /* Parse the "class" or "struct", which indicates a scoped
12585 enumeration type in C++0x. */
12586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12587 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12589 if (cxx_dialect == cxx98)
12590 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12592 /* Consume the `struct' or `class' token. */
12593 cp_lexer_consume_token (parser->lexer);
12595 scoped_enum_p = true;
12598 attributes = cp_parser_attributes_opt (parser);
12600 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12601 identifier = cp_parser_identifier (parser);
12603 identifier = make_anon_name ();
12605 /* Check for the `:' that denotes a specified underlying type in C++0x.
12606 Note that a ':' could also indicate a bitfield width, however. */
12607 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12609 cp_decl_specifier_seq type_specifiers;
12611 /* Consume the `:'. */
12612 cp_lexer_consume_token (parser->lexer);
12614 /* Parse the type-specifier-seq. */
12615 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12616 /*is_trailing_return=*/false,
12619 /* At this point this is surely not elaborated type specifier. */
12620 if (!cp_parser_parse_definitely (parser))
12623 if (cxx_dialect == cxx98)
12624 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12626 has_underlying_type = true;
12628 /* If that didn't work, stop. */
12629 if (type_specifiers.type != error_mark_node)
12631 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12632 /*initialized=*/0, NULL);
12633 if (underlying_type == error_mark_node)
12634 underlying_type = NULL_TREE;
12638 /* Look for the `{' but don't consume it yet. */
12639 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12641 cp_parser_error (parser, "expected %<{%>");
12642 if (has_underlying_type)
12646 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12649 /* Issue an error message if type-definitions are forbidden here. */
12650 if (!cp_parser_check_type_definition (parser))
12651 type = error_mark_node;
12653 /* Create the new type. We do this before consuming the opening
12654 brace so the enum will be recorded as being on the line of its
12655 tag (or the 'enum' keyword, if there is no tag). */
12656 type = start_enum (identifier, underlying_type, scoped_enum_p);
12658 /* Consume the opening brace. */
12659 cp_lexer_consume_token (parser->lexer);
12661 if (type == error_mark_node)
12663 cp_parser_skip_to_end_of_block_or_statement (parser);
12664 return error_mark_node;
12667 /* If the next token is not '}', then there are some enumerators. */
12668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12669 cp_parser_enumerator_list (parser, type);
12671 /* Consume the final '}'. */
12672 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12674 /* Look for trailing attributes to apply to this enumeration, and
12675 apply them if appropriate. */
12676 if (cp_parser_allow_gnu_extensions_p (parser))
12678 tree trailing_attr = cp_parser_attributes_opt (parser);
12679 trailing_attr = chainon (trailing_attr, attributes);
12680 cplus_decl_attributes (&type,
12682 (int) ATTR_FLAG_TYPE_IN_PLACE);
12685 /* Finish up the enumeration. */
12686 finish_enum (type);
12691 /* Parse an enumerator-list. The enumerators all have the indicated
12695 enumerator-definition
12696 enumerator-list , enumerator-definition */
12699 cp_parser_enumerator_list (cp_parser* parser, tree type)
12703 /* Parse an enumerator-definition. */
12704 cp_parser_enumerator_definition (parser, type);
12706 /* If the next token is not a ',', we've reached the end of
12708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12710 /* Otherwise, consume the `,' and keep going. */
12711 cp_lexer_consume_token (parser->lexer);
12712 /* If the next token is a `}', there is a trailing comma. */
12713 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12715 if (!in_system_header)
12716 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12722 /* Parse an enumerator-definition. The enumerator has the indicated
12725 enumerator-definition:
12727 enumerator = constant-expression
12733 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12738 /* Look for the identifier. */
12739 identifier = cp_parser_identifier (parser);
12740 if (identifier == error_mark_node)
12743 /* If the next token is an '=', then there is an explicit value. */
12744 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12746 /* Consume the `=' token. */
12747 cp_lexer_consume_token (parser->lexer);
12748 /* Parse the value. */
12749 value = cp_parser_constant_expression (parser,
12750 /*allow_non_constant_p=*/false,
12756 /* If we are processing a template, make sure the initializer of the
12757 enumerator doesn't contain any bare template parameter pack. */
12758 if (check_for_bare_parameter_packs (value))
12759 value = error_mark_node;
12761 /* Create the enumerator. */
12762 build_enumerator (identifier, value, type);
12765 /* Parse a namespace-name.
12768 original-namespace-name
12771 Returns the NAMESPACE_DECL for the namespace. */
12774 cp_parser_namespace_name (cp_parser* parser)
12777 tree namespace_decl;
12779 cp_token *token = cp_lexer_peek_token (parser->lexer);
12781 /* Get the name of the namespace. */
12782 identifier = cp_parser_identifier (parser);
12783 if (identifier == error_mark_node)
12784 return error_mark_node;
12786 /* Look up the identifier in the currently active scope. Look only
12787 for namespaces, due to:
12789 [basic.lookup.udir]
12791 When looking up a namespace-name in a using-directive or alias
12792 definition, only namespace names are considered.
12796 [basic.lookup.qual]
12798 During the lookup of a name preceding the :: scope resolution
12799 operator, object, function, and enumerator names are ignored.
12801 (Note that cp_parser_qualifying_entity only calls this
12802 function if the token after the name is the scope resolution
12804 namespace_decl = cp_parser_lookup_name (parser, identifier,
12806 /*is_template=*/false,
12807 /*is_namespace=*/true,
12808 /*check_dependency=*/true,
12809 /*ambiguous_decls=*/NULL,
12811 /* If it's not a namespace, issue an error. */
12812 if (namespace_decl == error_mark_node
12813 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12815 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12816 error_at (token->location, "%qD is not a namespace-name", identifier);
12817 cp_parser_error (parser, "expected namespace-name");
12818 namespace_decl = error_mark_node;
12821 return namespace_decl;
12824 /* Parse a namespace-definition.
12826 namespace-definition:
12827 named-namespace-definition
12828 unnamed-namespace-definition
12830 named-namespace-definition:
12831 original-namespace-definition
12832 extension-namespace-definition
12834 original-namespace-definition:
12835 namespace identifier { namespace-body }
12837 extension-namespace-definition:
12838 namespace original-namespace-name { namespace-body }
12840 unnamed-namespace-definition:
12841 namespace { namespace-body } */
12844 cp_parser_namespace_definition (cp_parser* parser)
12846 tree identifier, attribs;
12847 bool has_visibility;
12850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12853 cp_lexer_consume_token (parser->lexer);
12858 /* Look for the `namespace' keyword. */
12859 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12861 /* Get the name of the namespace. We do not attempt to distinguish
12862 between an original-namespace-definition and an
12863 extension-namespace-definition at this point. The semantic
12864 analysis routines are responsible for that. */
12865 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12866 identifier = cp_parser_identifier (parser);
12868 identifier = NULL_TREE;
12870 /* Parse any specified attributes. */
12871 attribs = cp_parser_attributes_opt (parser);
12873 /* Look for the `{' to start the namespace. */
12874 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12875 /* Start the namespace. */
12876 push_namespace (identifier);
12878 /* "inline namespace" is equivalent to a stub namespace definition
12879 followed by a strong using directive. */
12882 tree name_space = current_namespace;
12883 /* Set up namespace association. */
12884 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12885 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12886 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12887 /* Import the contents of the inline namespace. */
12889 do_using_directive (name_space);
12890 push_namespace (identifier);
12893 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12895 /* Parse the body of the namespace. */
12896 cp_parser_namespace_body (parser);
12898 #ifdef HANDLE_PRAGMA_VISIBILITY
12899 if (has_visibility)
12900 pop_visibility (1);
12903 /* Finish the namespace. */
12905 /* Look for the final `}'. */
12906 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12909 /* Parse a namespace-body.
12912 declaration-seq [opt] */
12915 cp_parser_namespace_body (cp_parser* parser)
12917 cp_parser_declaration_seq_opt (parser);
12920 /* Parse a namespace-alias-definition.
12922 namespace-alias-definition:
12923 namespace identifier = qualified-namespace-specifier ; */
12926 cp_parser_namespace_alias_definition (cp_parser* parser)
12929 tree namespace_specifier;
12931 cp_token *token = cp_lexer_peek_token (parser->lexer);
12933 /* Look for the `namespace' keyword. */
12934 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12935 /* Look for the identifier. */
12936 identifier = cp_parser_identifier (parser);
12937 if (identifier == error_mark_node)
12939 /* Look for the `=' token. */
12940 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12941 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12943 error_at (token->location, "%<namespace%> definition is not allowed here");
12944 /* Skip the definition. */
12945 cp_lexer_consume_token (parser->lexer);
12946 if (cp_parser_skip_to_closing_brace (parser))
12947 cp_lexer_consume_token (parser->lexer);
12950 cp_parser_require (parser, CPP_EQ, "%<=%>");
12951 /* Look for the qualified-namespace-specifier. */
12952 namespace_specifier
12953 = cp_parser_qualified_namespace_specifier (parser);
12954 /* Look for the `;' token. */
12955 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12957 /* Register the alias in the symbol table. */
12958 do_namespace_alias (identifier, namespace_specifier);
12961 /* Parse a qualified-namespace-specifier.
12963 qualified-namespace-specifier:
12964 :: [opt] nested-name-specifier [opt] namespace-name
12966 Returns a NAMESPACE_DECL corresponding to the specified
12970 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12972 /* Look for the optional `::'. */
12973 cp_parser_global_scope_opt (parser,
12974 /*current_scope_valid_p=*/false);
12976 /* Look for the optional nested-name-specifier. */
12977 cp_parser_nested_name_specifier_opt (parser,
12978 /*typename_keyword_p=*/false,
12979 /*check_dependency_p=*/true,
12981 /*is_declaration=*/true);
12983 return cp_parser_namespace_name (parser);
12986 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12987 access declaration.
12990 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12991 using :: unqualified-id ;
12993 access-declaration:
12999 cp_parser_using_declaration (cp_parser* parser,
13000 bool access_declaration_p)
13003 bool typename_p = false;
13004 bool global_scope_p;
13009 if (access_declaration_p)
13010 cp_parser_parse_tentatively (parser);
13013 /* Look for the `using' keyword. */
13014 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13016 /* Peek at the next token. */
13017 token = cp_lexer_peek_token (parser->lexer);
13018 /* See if it's `typename'. */
13019 if (token->keyword == RID_TYPENAME)
13021 /* Remember that we've seen it. */
13023 /* Consume the `typename' token. */
13024 cp_lexer_consume_token (parser->lexer);
13028 /* Look for the optional global scope qualification. */
13030 = (cp_parser_global_scope_opt (parser,
13031 /*current_scope_valid_p=*/false)
13034 /* If we saw `typename', or didn't see `::', then there must be a
13035 nested-name-specifier present. */
13036 if (typename_p || !global_scope_p)
13037 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13038 /*check_dependency_p=*/true,
13040 /*is_declaration=*/true);
13041 /* Otherwise, we could be in either of the two productions. In that
13042 case, treat the nested-name-specifier as optional. */
13044 qscope = cp_parser_nested_name_specifier_opt (parser,
13045 /*typename_keyword_p=*/false,
13046 /*check_dependency_p=*/true,
13048 /*is_declaration=*/true);
13050 qscope = global_namespace;
13052 if (access_declaration_p && cp_parser_error_occurred (parser))
13053 /* Something has already gone wrong; there's no need to parse
13054 further. Since an error has occurred, the return value of
13055 cp_parser_parse_definitely will be false, as required. */
13056 return cp_parser_parse_definitely (parser);
13058 token = cp_lexer_peek_token (parser->lexer);
13059 /* Parse the unqualified-id. */
13060 identifier = cp_parser_unqualified_id (parser,
13061 /*template_keyword_p=*/false,
13062 /*check_dependency_p=*/true,
13063 /*declarator_p=*/true,
13064 /*optional_p=*/false);
13066 if (access_declaration_p)
13068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13069 cp_parser_simulate_error (parser);
13070 if (!cp_parser_parse_definitely (parser))
13074 /* The function we call to handle a using-declaration is different
13075 depending on what scope we are in. */
13076 if (qscope == error_mark_node || identifier == error_mark_node)
13078 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13079 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13080 /* [namespace.udecl]
13082 A using declaration shall not name a template-id. */
13083 error_at (token->location,
13084 "a template-id may not appear in a using-declaration");
13087 if (at_class_scope_p ())
13089 /* Create the USING_DECL. */
13090 decl = do_class_using_decl (parser->scope, identifier);
13092 if (check_for_bare_parameter_packs (decl))
13095 /* Add it to the list of members in this class. */
13096 finish_member_declaration (decl);
13100 decl = cp_parser_lookup_name_simple (parser,
13103 if (decl == error_mark_node)
13104 cp_parser_name_lookup_error (parser, identifier,
13107 else if (check_for_bare_parameter_packs (decl))
13109 else if (!at_namespace_scope_p ())
13110 do_local_using_decl (decl, qscope, identifier);
13112 do_toplevel_using_decl (decl, qscope, identifier);
13116 /* Look for the final `;'. */
13117 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13122 /* Parse a using-directive.
13125 using namespace :: [opt] nested-name-specifier [opt]
13126 namespace-name ; */
13129 cp_parser_using_directive (cp_parser* parser)
13131 tree namespace_decl;
13134 /* Look for the `using' keyword. */
13135 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13136 /* And the `namespace' keyword. */
13137 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13138 /* Look for the optional `::' operator. */
13139 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13140 /* And the optional nested-name-specifier. */
13141 cp_parser_nested_name_specifier_opt (parser,
13142 /*typename_keyword_p=*/false,
13143 /*check_dependency_p=*/true,
13145 /*is_declaration=*/true);
13146 /* Get the namespace being used. */
13147 namespace_decl = cp_parser_namespace_name (parser);
13148 /* And any specified attributes. */
13149 attribs = cp_parser_attributes_opt (parser);
13150 /* Update the symbol table. */
13151 parse_using_directive (namespace_decl, attribs);
13152 /* Look for the final `;'. */
13153 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13156 /* Parse an asm-definition.
13159 asm ( string-literal ) ;
13164 asm volatile [opt] ( string-literal ) ;
13165 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13166 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13167 : asm-operand-list [opt] ) ;
13168 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13169 : asm-operand-list [opt]
13170 : asm-clobber-list [opt] ) ;
13171 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13172 : asm-clobber-list [opt]
13173 : asm-goto-list ) ; */
13176 cp_parser_asm_definition (cp_parser* parser)
13179 tree outputs = NULL_TREE;
13180 tree inputs = NULL_TREE;
13181 tree clobbers = NULL_TREE;
13182 tree labels = NULL_TREE;
13184 bool volatile_p = false;
13185 bool extended_p = false;
13186 bool invalid_inputs_p = false;
13187 bool invalid_outputs_p = false;
13188 bool goto_p = false;
13189 const char *missing = NULL;
13191 /* Look for the `asm' keyword. */
13192 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13193 /* See if the next token is `volatile'. */
13194 if (cp_parser_allow_gnu_extensions_p (parser)
13195 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13197 /* Remember that we saw the `volatile' keyword. */
13199 /* Consume the token. */
13200 cp_lexer_consume_token (parser->lexer);
13202 if (cp_parser_allow_gnu_extensions_p (parser)
13203 && parser->in_function_body
13204 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13206 /* Remember that we saw the `goto' keyword. */
13208 /* Consume the token. */
13209 cp_lexer_consume_token (parser->lexer);
13211 /* Look for the opening `('. */
13212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13214 /* Look for the string. */
13215 string = cp_parser_string_literal (parser, false, false);
13216 if (string == error_mark_node)
13218 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13219 /*consume_paren=*/true);
13223 /* If we're allowing GNU extensions, check for the extended assembly
13224 syntax. Unfortunately, the `:' tokens need not be separated by
13225 a space in C, and so, for compatibility, we tolerate that here
13226 too. Doing that means that we have to treat the `::' operator as
13228 if (cp_parser_allow_gnu_extensions_p (parser)
13229 && parser->in_function_body
13230 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13231 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13233 bool inputs_p = false;
13234 bool clobbers_p = false;
13235 bool labels_p = false;
13237 /* The extended syntax was used. */
13240 /* Look for outputs. */
13241 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13243 /* Consume the `:'. */
13244 cp_lexer_consume_token (parser->lexer);
13245 /* Parse the output-operands. */
13246 if (cp_lexer_next_token_is_not (parser->lexer,
13248 && cp_lexer_next_token_is_not (parser->lexer,
13250 && cp_lexer_next_token_is_not (parser->lexer,
13253 outputs = cp_parser_asm_operand_list (parser);
13255 if (outputs == error_mark_node)
13256 invalid_outputs_p = true;
13258 /* If the next token is `::', there are no outputs, and the
13259 next token is the beginning of the inputs. */
13260 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13261 /* The inputs are coming next. */
13264 /* Look for inputs. */
13266 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13268 /* Consume the `:' or `::'. */
13269 cp_lexer_consume_token (parser->lexer);
13270 /* Parse the output-operands. */
13271 if (cp_lexer_next_token_is_not (parser->lexer,
13273 && cp_lexer_next_token_is_not (parser->lexer,
13275 && cp_lexer_next_token_is_not (parser->lexer,
13277 inputs = cp_parser_asm_operand_list (parser);
13279 if (inputs == error_mark_node)
13280 invalid_inputs_p = true;
13282 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13283 /* The clobbers are coming next. */
13286 /* Look for clobbers. */
13288 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13291 /* Consume the `:' or `::'. */
13292 cp_lexer_consume_token (parser->lexer);
13293 /* Parse the clobbers. */
13294 if (cp_lexer_next_token_is_not (parser->lexer,
13296 && cp_lexer_next_token_is_not (parser->lexer,
13298 clobbers = cp_parser_asm_clobber_list (parser);
13301 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13302 /* The labels are coming next. */
13305 /* Look for labels. */
13307 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13310 /* Consume the `:' or `::'. */
13311 cp_lexer_consume_token (parser->lexer);
13312 /* Parse the labels. */
13313 labels = cp_parser_asm_label_list (parser);
13316 if (goto_p && !labels_p)
13317 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13320 missing = "%<:%> or %<::%>";
13322 /* Look for the closing `)'. */
13323 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13324 missing ? missing : "%<)%>"))
13325 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13326 /*consume_paren=*/true);
13327 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13329 if (!invalid_inputs_p && !invalid_outputs_p)
13331 /* Create the ASM_EXPR. */
13332 if (parser->in_function_body)
13334 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13335 inputs, clobbers, labels);
13336 /* If the extended syntax was not used, mark the ASM_EXPR. */
13339 tree temp = asm_stmt;
13340 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13341 temp = TREE_OPERAND (temp, 0);
13343 ASM_INPUT_P (temp) = 1;
13347 cgraph_add_asm_node (string);
13351 /* Declarators [gram.dcl.decl] */
13353 /* Parse an init-declarator.
13356 declarator initializer [opt]
13361 declarator asm-specification [opt] attributes [opt] initializer [opt]
13363 function-definition:
13364 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13366 decl-specifier-seq [opt] declarator function-try-block
13370 function-definition:
13371 __extension__ function-definition
13373 The DECL_SPECIFIERS apply to this declarator. Returns a
13374 representation of the entity declared. If MEMBER_P is TRUE, then
13375 this declarator appears in a class scope. The new DECL created by
13376 this declarator is returned.
13378 The CHECKS are access checks that should be performed once we know
13379 what entity is being declared (and, therefore, what classes have
13382 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13383 for a function-definition here as well. If the declarator is a
13384 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13385 be TRUE upon return. By that point, the function-definition will
13386 have been completely parsed.
13388 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13392 cp_parser_init_declarator (cp_parser* parser,
13393 cp_decl_specifier_seq *decl_specifiers,
13394 VEC (deferred_access_check,gc)* checks,
13395 bool function_definition_allowed_p,
13397 int declares_class_or_enum,
13398 bool* function_definition_p)
13400 cp_token *token = NULL, *asm_spec_start_token = NULL,
13401 *attributes_start_token = NULL;
13402 cp_declarator *declarator;
13403 tree prefix_attributes;
13405 tree asm_specification;
13407 tree decl = NULL_TREE;
13409 int is_initialized;
13410 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13411 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13413 enum cpp_ttype initialization_kind;
13414 bool is_direct_init = false;
13415 bool is_non_constant_init;
13416 int ctor_dtor_or_conv_p;
13418 tree pushed_scope = NULL;
13420 /* Gather the attributes that were provided with the
13421 decl-specifiers. */
13422 prefix_attributes = decl_specifiers->attributes;
13424 /* Assume that this is not the declarator for a function
13426 if (function_definition_p)
13427 *function_definition_p = false;
13429 /* Defer access checks while parsing the declarator; we cannot know
13430 what names are accessible until we know what is being
13432 resume_deferring_access_checks ();
13434 /* Parse the declarator. */
13435 token = cp_lexer_peek_token (parser->lexer);
13437 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13438 &ctor_dtor_or_conv_p,
13439 /*parenthesized_p=*/NULL,
13440 /*member_p=*/false);
13441 /* Gather up the deferred checks. */
13442 stop_deferring_access_checks ();
13444 /* If the DECLARATOR was erroneous, there's no need to go
13446 if (declarator == cp_error_declarator)
13447 return error_mark_node;
13449 /* Check that the number of template-parameter-lists is OK. */
13450 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13452 return error_mark_node;
13454 if (declares_class_or_enum & 2)
13455 cp_parser_check_for_definition_in_return_type (declarator,
13456 decl_specifiers->type,
13457 decl_specifiers->type_location);
13459 /* Figure out what scope the entity declared by the DECLARATOR is
13460 located in. `grokdeclarator' sometimes changes the scope, so
13461 we compute it now. */
13462 scope = get_scope_of_declarator (declarator);
13464 /* If we're allowing GNU extensions, look for an asm-specification
13466 if (cp_parser_allow_gnu_extensions_p (parser))
13468 /* Look for an asm-specification. */
13469 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13470 asm_specification = cp_parser_asm_specification_opt (parser);
13471 /* And attributes. */
13472 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13473 attributes = cp_parser_attributes_opt (parser);
13477 asm_specification = NULL_TREE;
13478 attributes = NULL_TREE;
13481 /* Peek at the next token. */
13482 token = cp_lexer_peek_token (parser->lexer);
13483 /* Check to see if the token indicates the start of a
13484 function-definition. */
13485 if (function_declarator_p (declarator)
13486 && cp_parser_token_starts_function_definition_p (token))
13488 if (!function_definition_allowed_p)
13490 /* If a function-definition should not appear here, issue an
13492 cp_parser_error (parser,
13493 "a function-definition is not allowed here");
13494 return error_mark_node;
13498 location_t func_brace_location
13499 = cp_lexer_peek_token (parser->lexer)->location;
13501 /* Neither attributes nor an asm-specification are allowed
13502 on a function-definition. */
13503 if (asm_specification)
13504 error_at (asm_spec_start_token->location,
13505 "an asm-specification is not allowed "
13506 "on a function-definition");
13508 error_at (attributes_start_token->location,
13509 "attributes are not allowed on a function-definition");
13510 /* This is a function-definition. */
13511 *function_definition_p = true;
13513 /* Parse the function definition. */
13515 decl = cp_parser_save_member_function_body (parser,
13518 prefix_attributes);
13521 = (cp_parser_function_definition_from_specifiers_and_declarator
13522 (parser, decl_specifiers, prefix_attributes, declarator));
13524 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13526 /* This is where the prologue starts... */
13527 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13528 = func_brace_location;
13537 Only in function declarations for constructors, destructors, and
13538 type conversions can the decl-specifier-seq be omitted.
13540 We explicitly postpone this check past the point where we handle
13541 function-definitions because we tolerate function-definitions
13542 that are missing their return types in some modes. */
13543 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13545 cp_parser_error (parser,
13546 "expected constructor, destructor, or type conversion");
13547 return error_mark_node;
13550 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13551 if (token->type == CPP_EQ
13552 || token->type == CPP_OPEN_PAREN
13553 || token->type == CPP_OPEN_BRACE)
13555 is_initialized = SD_INITIALIZED;
13556 initialization_kind = token->type;
13558 if (token->type == CPP_EQ
13559 && function_declarator_p (declarator))
13561 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13562 if (t2->keyword == RID_DEFAULT)
13563 is_initialized = SD_DEFAULTED;
13564 else if (t2->keyword == RID_DELETE)
13565 is_initialized = SD_DELETED;
13570 /* If the init-declarator isn't initialized and isn't followed by a
13571 `,' or `;', it's not a valid init-declarator. */
13572 if (token->type != CPP_COMMA
13573 && token->type != CPP_SEMICOLON)
13575 cp_parser_error (parser, "expected initializer");
13576 return error_mark_node;
13578 is_initialized = SD_UNINITIALIZED;
13579 initialization_kind = CPP_EOF;
13582 /* Because start_decl has side-effects, we should only call it if we
13583 know we're going ahead. By this point, we know that we cannot
13584 possibly be looking at any other construct. */
13585 cp_parser_commit_to_tentative_parse (parser);
13587 /* If the decl specifiers were bad, issue an error now that we're
13588 sure this was intended to be a declarator. Then continue
13589 declaring the variable(s), as int, to try to cut down on further
13591 if (decl_specifiers->any_specifiers_p
13592 && decl_specifiers->type == error_mark_node)
13594 cp_parser_error (parser, "invalid type in declaration");
13595 decl_specifiers->type = integer_type_node;
13598 /* Check to see whether or not this declaration is a friend. */
13599 friend_p = cp_parser_friend_p (decl_specifiers);
13601 /* Enter the newly declared entry in the symbol table. If we're
13602 processing a declaration in a class-specifier, we wait until
13603 after processing the initializer. */
13606 if (parser->in_unbraced_linkage_specification_p)
13607 decl_specifiers->storage_class = sc_extern;
13608 decl = start_decl (declarator, decl_specifiers,
13609 is_initialized, attributes, prefix_attributes,
13613 /* Enter the SCOPE. That way unqualified names appearing in the
13614 initializer will be looked up in SCOPE. */
13615 pushed_scope = push_scope (scope);
13617 /* Perform deferred access control checks, now that we know in which
13618 SCOPE the declared entity resides. */
13619 if (!member_p && decl)
13621 tree saved_current_function_decl = NULL_TREE;
13623 /* If the entity being declared is a function, pretend that we
13624 are in its scope. If it is a `friend', it may have access to
13625 things that would not otherwise be accessible. */
13626 if (TREE_CODE (decl) == FUNCTION_DECL)
13628 saved_current_function_decl = current_function_decl;
13629 current_function_decl = decl;
13632 /* Perform access checks for template parameters. */
13633 cp_parser_perform_template_parameter_access_checks (checks);
13635 /* Perform the access control checks for the declarator and the
13636 decl-specifiers. */
13637 perform_deferred_access_checks ();
13639 /* Restore the saved value. */
13640 if (TREE_CODE (decl) == FUNCTION_DECL)
13641 current_function_decl = saved_current_function_decl;
13644 /* Parse the initializer. */
13645 initializer = NULL_TREE;
13646 is_direct_init = false;
13647 is_non_constant_init = true;
13648 if (is_initialized)
13650 if (function_declarator_p (declarator))
13652 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13653 if (initialization_kind == CPP_EQ)
13654 initializer = cp_parser_pure_specifier (parser);
13657 /* If the declaration was erroneous, we don't really
13658 know what the user intended, so just silently
13659 consume the initializer. */
13660 if (decl != error_mark_node)
13661 error_at (initializer_start_token->location,
13662 "initializer provided for function");
13663 cp_parser_skip_to_closing_parenthesis (parser,
13664 /*recovering=*/true,
13665 /*or_comma=*/false,
13666 /*consume_paren=*/true);
13671 /* We want to record the extra mangling scope for in-class
13672 initializers of class members and initializers of static data
13673 member templates. The former is a C++0x feature which isn't
13674 implemented yet, and I expect it will involve deferring
13675 parsing of the initializer until end of class as with default
13676 arguments. So right here we only handle the latter. */
13677 if (!member_p && processing_template_decl)
13678 start_lambda_scope (decl);
13679 initializer = cp_parser_initializer (parser,
13681 &is_non_constant_init);
13682 if (!member_p && processing_template_decl)
13683 finish_lambda_scope ();
13687 /* The old parser allows attributes to appear after a parenthesized
13688 initializer. Mark Mitchell proposed removing this functionality
13689 on the GCC mailing lists on 2002-08-13. This parser accepts the
13690 attributes -- but ignores them. */
13691 if (cp_parser_allow_gnu_extensions_p (parser)
13692 && initialization_kind == CPP_OPEN_PAREN)
13693 if (cp_parser_attributes_opt (parser))
13694 warning (OPT_Wattributes,
13695 "attributes after parenthesized initializer ignored");
13697 /* For an in-class declaration, use `grokfield' to create the
13703 pop_scope (pushed_scope);
13704 pushed_scope = false;
13706 decl = grokfield (declarator, decl_specifiers,
13707 initializer, !is_non_constant_init,
13708 /*asmspec=*/NULL_TREE,
13709 prefix_attributes);
13710 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13711 cp_parser_save_default_args (parser, decl);
13714 /* Finish processing the declaration. But, skip friend
13716 if (!friend_p && decl && decl != error_mark_node)
13718 cp_finish_decl (decl,
13719 initializer, !is_non_constant_init,
13721 /* If the initializer is in parentheses, then this is
13722 a direct-initialization, which means that an
13723 `explicit' constructor is OK. Otherwise, an
13724 `explicit' constructor cannot be used. */
13725 ((is_direct_init || !is_initialized)
13726 ? 0 : LOOKUP_ONLYCONVERTING));
13728 else if ((cxx_dialect != cxx98) && friend_p
13729 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13730 /* Core issue #226 (C++0x only): A default template-argument
13731 shall not be specified in a friend class template
13733 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13734 /*is_partial=*/0, /*is_friend_decl=*/1);
13736 if (!friend_p && pushed_scope)
13737 pop_scope (pushed_scope);
13742 /* Parse a declarator.
13746 ptr-operator declarator
13748 abstract-declarator:
13749 ptr-operator abstract-declarator [opt]
13750 direct-abstract-declarator
13755 attributes [opt] direct-declarator
13756 attributes [opt] ptr-operator declarator
13758 abstract-declarator:
13759 attributes [opt] ptr-operator abstract-declarator [opt]
13760 attributes [opt] direct-abstract-declarator
13762 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13763 detect constructor, destructor or conversion operators. It is set
13764 to -1 if the declarator is a name, and +1 if it is a
13765 function. Otherwise it is set to zero. Usually you just want to
13766 test for >0, but internally the negative value is used.
13768 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13769 a decl-specifier-seq unless it declares a constructor, destructor,
13770 or conversion. It might seem that we could check this condition in
13771 semantic analysis, rather than parsing, but that makes it difficult
13772 to handle something like `f()'. We want to notice that there are
13773 no decl-specifiers, and therefore realize that this is an
13774 expression, not a declaration.)
13776 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13777 the declarator is a direct-declarator of the form "(...)".
13779 MEMBER_P is true iff this declarator is a member-declarator. */
13781 static cp_declarator *
13782 cp_parser_declarator (cp_parser* parser,
13783 cp_parser_declarator_kind dcl_kind,
13784 int* ctor_dtor_or_conv_p,
13785 bool* parenthesized_p,
13789 cp_declarator *declarator;
13790 enum tree_code code;
13791 cp_cv_quals cv_quals;
13793 tree attributes = NULL_TREE;
13795 /* Assume this is not a constructor, destructor, or type-conversion
13797 if (ctor_dtor_or_conv_p)
13798 *ctor_dtor_or_conv_p = 0;
13800 if (cp_parser_allow_gnu_extensions_p (parser))
13801 attributes = cp_parser_attributes_opt (parser);
13803 /* Peek at the next token. */
13804 token = cp_lexer_peek_token (parser->lexer);
13806 /* Check for the ptr-operator production. */
13807 cp_parser_parse_tentatively (parser);
13808 /* Parse the ptr-operator. */
13809 code = cp_parser_ptr_operator (parser,
13812 /* If that worked, then we have a ptr-operator. */
13813 if (cp_parser_parse_definitely (parser))
13815 /* If a ptr-operator was found, then this declarator was not
13817 if (parenthesized_p)
13818 *parenthesized_p = true;
13819 /* The dependent declarator is optional if we are parsing an
13820 abstract-declarator. */
13821 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13822 cp_parser_parse_tentatively (parser);
13824 /* Parse the dependent declarator. */
13825 declarator = cp_parser_declarator (parser, dcl_kind,
13826 /*ctor_dtor_or_conv_p=*/NULL,
13827 /*parenthesized_p=*/NULL,
13828 /*member_p=*/false);
13830 /* If we are parsing an abstract-declarator, we must handle the
13831 case where the dependent declarator is absent. */
13832 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13833 && !cp_parser_parse_definitely (parser))
13836 declarator = cp_parser_make_indirect_declarator
13837 (code, class_type, cv_quals, declarator);
13839 /* Everything else is a direct-declarator. */
13842 if (parenthesized_p)
13843 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13845 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13846 ctor_dtor_or_conv_p,
13850 if (attributes && declarator && declarator != cp_error_declarator)
13851 declarator->attributes = attributes;
13856 /* Parse a direct-declarator or direct-abstract-declarator.
13860 direct-declarator ( parameter-declaration-clause )
13861 cv-qualifier-seq [opt]
13862 exception-specification [opt]
13863 direct-declarator [ constant-expression [opt] ]
13866 direct-abstract-declarator:
13867 direct-abstract-declarator [opt]
13868 ( parameter-declaration-clause )
13869 cv-qualifier-seq [opt]
13870 exception-specification [opt]
13871 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13872 ( abstract-declarator )
13874 Returns a representation of the declarator. DCL_KIND is
13875 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13876 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13877 we are parsing a direct-declarator. It is
13878 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13879 of ambiguity we prefer an abstract declarator, as per
13880 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13881 cp_parser_declarator. */
13883 static cp_declarator *
13884 cp_parser_direct_declarator (cp_parser* parser,
13885 cp_parser_declarator_kind dcl_kind,
13886 int* ctor_dtor_or_conv_p,
13890 cp_declarator *declarator = NULL;
13891 tree scope = NULL_TREE;
13892 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13893 bool saved_in_declarator_p = parser->in_declarator_p;
13895 tree pushed_scope = NULL_TREE;
13899 /* Peek at the next token. */
13900 token = cp_lexer_peek_token (parser->lexer);
13901 if (token->type == CPP_OPEN_PAREN)
13903 /* This is either a parameter-declaration-clause, or a
13904 parenthesized declarator. When we know we are parsing a
13905 named declarator, it must be a parenthesized declarator
13906 if FIRST is true. For instance, `(int)' is a
13907 parameter-declaration-clause, with an omitted
13908 direct-abstract-declarator. But `((*))', is a
13909 parenthesized abstract declarator. Finally, when T is a
13910 template parameter `(T)' is a
13911 parameter-declaration-clause, and not a parenthesized
13914 We first try and parse a parameter-declaration-clause,
13915 and then try a nested declarator (if FIRST is true).
13917 It is not an error for it not to be a
13918 parameter-declaration-clause, even when FIRST is
13924 The first is the declaration of a function while the
13925 second is the definition of a variable, including its
13928 Having seen only the parenthesis, we cannot know which of
13929 these two alternatives should be selected. Even more
13930 complex are examples like:
13935 The former is a function-declaration; the latter is a
13936 variable initialization.
13938 Thus again, we try a parameter-declaration-clause, and if
13939 that fails, we back out and return. */
13941 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13944 unsigned saved_num_template_parameter_lists;
13945 bool is_declarator = false;
13948 /* In a member-declarator, the only valid interpretation
13949 of a parenthesis is the start of a
13950 parameter-declaration-clause. (It is invalid to
13951 initialize a static data member with a parenthesized
13952 initializer; only the "=" form of initialization is
13955 cp_parser_parse_tentatively (parser);
13957 /* Consume the `('. */
13958 cp_lexer_consume_token (parser->lexer);
13961 /* If this is going to be an abstract declarator, we're
13962 in a declarator and we can't have default args. */
13963 parser->default_arg_ok_p = false;
13964 parser->in_declarator_p = true;
13967 /* Inside the function parameter list, surrounding
13968 template-parameter-lists do not apply. */
13969 saved_num_template_parameter_lists
13970 = parser->num_template_parameter_lists;
13971 parser->num_template_parameter_lists = 0;
13973 begin_scope (sk_function_parms, NULL_TREE);
13975 /* Parse the parameter-declaration-clause. */
13976 params = cp_parser_parameter_declaration_clause (parser);
13978 parser->num_template_parameter_lists
13979 = saved_num_template_parameter_lists;
13981 /* If all went well, parse the cv-qualifier-seq and the
13982 exception-specification. */
13983 if (member_p || cp_parser_parse_definitely (parser))
13985 cp_cv_quals cv_quals;
13986 tree exception_specification;
13989 is_declarator = true;
13991 if (ctor_dtor_or_conv_p)
13992 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13994 /* Consume the `)'. */
13995 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13997 /* Parse the cv-qualifier-seq. */
13998 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13999 /* And the exception-specification. */
14000 exception_specification
14001 = cp_parser_exception_specification_opt (parser);
14004 = cp_parser_late_return_type_opt (parser);
14006 /* Create the function-declarator. */
14007 declarator = make_call_declarator (declarator,
14010 exception_specification,
14012 /* Any subsequent parameter lists are to do with
14013 return type, so are not those of the declared
14015 parser->default_arg_ok_p = false;
14018 /* Remove the function parms from scope. */
14019 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14020 pop_binding (DECL_NAME (t), t);
14024 /* Repeat the main loop. */
14028 /* If this is the first, we can try a parenthesized
14032 bool saved_in_type_id_in_expr_p;
14034 parser->default_arg_ok_p = saved_default_arg_ok_p;
14035 parser->in_declarator_p = saved_in_declarator_p;
14037 /* Consume the `('. */
14038 cp_lexer_consume_token (parser->lexer);
14039 /* Parse the nested declarator. */
14040 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14041 parser->in_type_id_in_expr_p = true;
14043 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14044 /*parenthesized_p=*/NULL,
14046 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14048 /* Expect a `)'. */
14049 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14050 declarator = cp_error_declarator;
14051 if (declarator == cp_error_declarator)
14054 goto handle_declarator;
14056 /* Otherwise, we must be done. */
14060 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14061 && token->type == CPP_OPEN_SQUARE)
14063 /* Parse an array-declarator. */
14066 if (ctor_dtor_or_conv_p)
14067 *ctor_dtor_or_conv_p = 0;
14070 parser->default_arg_ok_p = false;
14071 parser->in_declarator_p = true;
14072 /* Consume the `['. */
14073 cp_lexer_consume_token (parser->lexer);
14074 /* Peek at the next token. */
14075 token = cp_lexer_peek_token (parser->lexer);
14076 /* If the next token is `]', then there is no
14077 constant-expression. */
14078 if (token->type != CPP_CLOSE_SQUARE)
14080 bool non_constant_p;
14083 = cp_parser_constant_expression (parser,
14084 /*allow_non_constant=*/true,
14086 if (!non_constant_p)
14087 bounds = fold_non_dependent_expr (bounds);
14088 /* Normally, the array bound must be an integral constant
14089 expression. However, as an extension, we allow VLAs
14090 in function scopes. */
14091 else if (!parser->in_function_body)
14093 error_at (token->location,
14094 "array bound is not an integer constant");
14095 bounds = error_mark_node;
14097 else if (processing_template_decl && !error_operand_p (bounds))
14099 /* Remember this wasn't a constant-expression. */
14100 bounds = build_nop (TREE_TYPE (bounds), bounds);
14101 TREE_SIDE_EFFECTS (bounds) = 1;
14105 bounds = NULL_TREE;
14106 /* Look for the closing `]'. */
14107 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14109 declarator = cp_error_declarator;
14113 declarator = make_array_declarator (declarator, bounds);
14115 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14118 tree qualifying_scope;
14119 tree unqualified_name;
14120 special_function_kind sfk;
14122 bool pack_expansion_p = false;
14123 cp_token *declarator_id_start_token;
14125 /* Parse a declarator-id */
14126 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14129 cp_parser_parse_tentatively (parser);
14131 /* If we see an ellipsis, we should be looking at a
14133 if (token->type == CPP_ELLIPSIS)
14135 /* Consume the `...' */
14136 cp_lexer_consume_token (parser->lexer);
14138 pack_expansion_p = true;
14142 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14144 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14145 qualifying_scope = parser->scope;
14150 if (!unqualified_name && pack_expansion_p)
14152 /* Check whether an error occurred. */
14153 okay = !cp_parser_error_occurred (parser);
14155 /* We already consumed the ellipsis to mark a
14156 parameter pack, but we have no way to report it,
14157 so abort the tentative parse. We will be exiting
14158 immediately anyway. */
14159 cp_parser_abort_tentative_parse (parser);
14162 okay = cp_parser_parse_definitely (parser);
14165 unqualified_name = error_mark_node;
14166 else if (unqualified_name
14167 && (qualifying_scope
14168 || (TREE_CODE (unqualified_name)
14169 != IDENTIFIER_NODE)))
14171 cp_parser_error (parser, "expected unqualified-id");
14172 unqualified_name = error_mark_node;
14176 if (!unqualified_name)
14178 if (unqualified_name == error_mark_node)
14180 declarator = cp_error_declarator;
14181 pack_expansion_p = false;
14182 declarator->parameter_pack_p = false;
14186 if (qualifying_scope && at_namespace_scope_p ()
14187 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14189 /* In the declaration of a member of a template class
14190 outside of the class itself, the SCOPE will sometimes
14191 be a TYPENAME_TYPE. For example, given:
14193 template <typename T>
14194 int S<T>::R::i = 3;
14196 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14197 this context, we must resolve S<T>::R to an ordinary
14198 type, rather than a typename type.
14200 The reason we normally avoid resolving TYPENAME_TYPEs
14201 is that a specialization of `S' might render
14202 `S<T>::R' not a type. However, if `S' is
14203 specialized, then this `i' will not be used, so there
14204 is no harm in resolving the types here. */
14207 /* Resolve the TYPENAME_TYPE. */
14208 type = resolve_typename_type (qualifying_scope,
14209 /*only_current_p=*/false);
14210 /* If that failed, the declarator is invalid. */
14211 if (TREE_CODE (type) == TYPENAME_TYPE)
14213 if (typedef_variant_p (type))
14214 error_at (declarator_id_start_token->location,
14215 "cannot define member of dependent typedef "
14218 error_at (declarator_id_start_token->location,
14219 "%<%T::%E%> is not a type",
14220 TYPE_CONTEXT (qualifying_scope),
14221 TYPE_IDENTIFIER (qualifying_scope));
14223 qualifying_scope = type;
14228 if (unqualified_name)
14232 if (qualifying_scope
14233 && CLASS_TYPE_P (qualifying_scope))
14234 class_type = qualifying_scope;
14236 class_type = current_class_type;
14238 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14240 tree name_type = TREE_TYPE (unqualified_name);
14241 if (class_type && same_type_p (name_type, class_type))
14243 if (qualifying_scope
14244 && CLASSTYPE_USE_TEMPLATE (name_type))
14246 error_at (declarator_id_start_token->location,
14247 "invalid use of constructor as a template");
14248 inform (declarator_id_start_token->location,
14249 "use %<%T::%D%> instead of %<%T::%D%> to "
14250 "name the constructor in a qualified name",
14252 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14253 class_type, name_type);
14254 declarator = cp_error_declarator;
14258 unqualified_name = constructor_name (class_type);
14262 /* We do not attempt to print the declarator
14263 here because we do not have enough
14264 information about its original syntactic
14266 cp_parser_error (parser, "invalid declarator");
14267 declarator = cp_error_declarator;
14274 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14275 sfk = sfk_destructor;
14276 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14277 sfk = sfk_conversion;
14278 else if (/* There's no way to declare a constructor
14279 for an anonymous type, even if the type
14280 got a name for linkage purposes. */
14281 !TYPE_WAS_ANONYMOUS (class_type)
14282 && constructor_name_p (unqualified_name,
14285 unqualified_name = constructor_name (class_type);
14286 sfk = sfk_constructor;
14288 else if (is_overloaded_fn (unqualified_name)
14289 && DECL_CONSTRUCTOR_P (get_first_fn
14290 (unqualified_name)))
14291 sfk = sfk_constructor;
14293 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14294 *ctor_dtor_or_conv_p = -1;
14297 declarator = make_id_declarator (qualifying_scope,
14300 declarator->id_loc = token->location;
14301 declarator->parameter_pack_p = pack_expansion_p;
14303 if (pack_expansion_p)
14304 maybe_warn_variadic_templates ();
14307 handle_declarator:;
14308 scope = get_scope_of_declarator (declarator);
14310 /* Any names that appear after the declarator-id for a
14311 member are looked up in the containing scope. */
14312 pushed_scope = push_scope (scope);
14313 parser->in_declarator_p = true;
14314 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14315 || (declarator && declarator->kind == cdk_id))
14316 /* Default args are only allowed on function
14318 parser->default_arg_ok_p = saved_default_arg_ok_p;
14320 parser->default_arg_ok_p = false;
14329 /* For an abstract declarator, we might wind up with nothing at this
14330 point. That's an error; the declarator is not optional. */
14332 cp_parser_error (parser, "expected declarator");
14334 /* If we entered a scope, we must exit it now. */
14336 pop_scope (pushed_scope);
14338 parser->default_arg_ok_p = saved_default_arg_ok_p;
14339 parser->in_declarator_p = saved_in_declarator_p;
14344 /* Parse a ptr-operator.
14347 * cv-qualifier-seq [opt]
14349 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14354 & cv-qualifier-seq [opt]
14356 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14357 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14358 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14359 filled in with the TYPE containing the member. *CV_QUALS is
14360 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14361 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14362 Note that the tree codes returned by this function have nothing
14363 to do with the types of trees that will be eventually be created
14364 to represent the pointer or reference type being parsed. They are
14365 just constants with suggestive names. */
14366 static enum tree_code
14367 cp_parser_ptr_operator (cp_parser* parser,
14369 cp_cv_quals *cv_quals)
14371 enum tree_code code = ERROR_MARK;
14374 /* Assume that it's not a pointer-to-member. */
14376 /* And that there are no cv-qualifiers. */
14377 *cv_quals = TYPE_UNQUALIFIED;
14379 /* Peek at the next token. */
14380 token = cp_lexer_peek_token (parser->lexer);
14382 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14383 if (token->type == CPP_MULT)
14384 code = INDIRECT_REF;
14385 else if (token->type == CPP_AND)
14387 else if ((cxx_dialect != cxx98) &&
14388 token->type == CPP_AND_AND) /* C++0x only */
14389 code = NON_LVALUE_EXPR;
14391 if (code != ERROR_MARK)
14393 /* Consume the `*', `&' or `&&'. */
14394 cp_lexer_consume_token (parser->lexer);
14396 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14397 `&', if we are allowing GNU extensions. (The only qualifier
14398 that can legally appear after `&' is `restrict', but that is
14399 enforced during semantic analysis. */
14400 if (code == INDIRECT_REF
14401 || cp_parser_allow_gnu_extensions_p (parser))
14402 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14406 /* Try the pointer-to-member case. */
14407 cp_parser_parse_tentatively (parser);
14408 /* Look for the optional `::' operator. */
14409 cp_parser_global_scope_opt (parser,
14410 /*current_scope_valid_p=*/false);
14411 /* Look for the nested-name specifier. */
14412 token = cp_lexer_peek_token (parser->lexer);
14413 cp_parser_nested_name_specifier (parser,
14414 /*typename_keyword_p=*/false,
14415 /*check_dependency_p=*/true,
14417 /*is_declaration=*/false);
14418 /* If we found it, and the next token is a `*', then we are
14419 indeed looking at a pointer-to-member operator. */
14420 if (!cp_parser_error_occurred (parser)
14421 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14423 /* Indicate that the `*' operator was used. */
14424 code = INDIRECT_REF;
14426 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14427 error_at (token->location, "%qD is a namespace", parser->scope);
14430 /* The type of which the member is a member is given by the
14432 *type = parser->scope;
14433 /* The next name will not be qualified. */
14434 parser->scope = NULL_TREE;
14435 parser->qualifying_scope = NULL_TREE;
14436 parser->object_scope = NULL_TREE;
14437 /* Look for the optional cv-qualifier-seq. */
14438 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14441 /* If that didn't work we don't have a ptr-operator. */
14442 if (!cp_parser_parse_definitely (parser))
14443 cp_parser_error (parser, "expected ptr-operator");
14449 /* Parse an (optional) cv-qualifier-seq.
14452 cv-qualifier cv-qualifier-seq [opt]
14463 Returns a bitmask representing the cv-qualifiers. */
14466 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14468 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14473 cp_cv_quals cv_qualifier;
14475 /* Peek at the next token. */
14476 token = cp_lexer_peek_token (parser->lexer);
14477 /* See if it's a cv-qualifier. */
14478 switch (token->keyword)
14481 cv_qualifier = TYPE_QUAL_CONST;
14485 cv_qualifier = TYPE_QUAL_VOLATILE;
14489 cv_qualifier = TYPE_QUAL_RESTRICT;
14493 cv_qualifier = TYPE_UNQUALIFIED;
14500 if (cv_quals & cv_qualifier)
14502 error_at (token->location, "duplicate cv-qualifier");
14503 cp_lexer_purge_token (parser->lexer);
14507 cp_lexer_consume_token (parser->lexer);
14508 cv_quals |= cv_qualifier;
14515 /* Parse a late-specified return type, if any. This is not a separate
14516 non-terminal, but part of a function declarator, which looks like
14518 -> trailing-type-specifier-seq abstract-declarator(opt)
14520 Returns the type indicated by the type-id. */
14523 cp_parser_late_return_type_opt (cp_parser* parser)
14527 /* Peek at the next token. */
14528 token = cp_lexer_peek_token (parser->lexer);
14529 /* A late-specified return type is indicated by an initial '->'. */
14530 if (token->type != CPP_DEREF)
14533 /* Consume the ->. */
14534 cp_lexer_consume_token (parser->lexer);
14536 return cp_parser_trailing_type_id (parser);
14539 /* Parse a declarator-id.
14543 :: [opt] nested-name-specifier [opt] type-name
14545 In the `id-expression' case, the value returned is as for
14546 cp_parser_id_expression if the id-expression was an unqualified-id.
14547 If the id-expression was a qualified-id, then a SCOPE_REF is
14548 returned. The first operand is the scope (either a NAMESPACE_DECL
14549 or TREE_TYPE), but the second is still just a representation of an
14553 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14556 /* The expression must be an id-expression. Assume that qualified
14557 names are the names of types so that:
14560 int S<T>::R::i = 3;
14562 will work; we must treat `S<T>::R' as the name of a type.
14563 Similarly, assume that qualified names are templates, where
14567 int S<T>::R<T>::i = 3;
14570 id = cp_parser_id_expression (parser,
14571 /*template_keyword_p=*/false,
14572 /*check_dependency_p=*/false,
14573 /*template_p=*/NULL,
14574 /*declarator_p=*/true,
14576 if (id && BASELINK_P (id))
14577 id = BASELINK_FUNCTIONS (id);
14581 /* Parse a type-id.
14584 type-specifier-seq abstract-declarator [opt]
14586 Returns the TYPE specified. */
14589 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14590 bool is_trailing_return)
14592 cp_decl_specifier_seq type_specifier_seq;
14593 cp_declarator *abstract_declarator;
14595 /* Parse the type-specifier-seq. */
14596 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14597 is_trailing_return,
14598 &type_specifier_seq);
14599 if (type_specifier_seq.type == error_mark_node)
14600 return error_mark_node;
14602 /* There might or might not be an abstract declarator. */
14603 cp_parser_parse_tentatively (parser);
14604 /* Look for the declarator. */
14605 abstract_declarator
14606 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14607 /*parenthesized_p=*/NULL,
14608 /*member_p=*/false);
14609 /* Check to see if there really was a declarator. */
14610 if (!cp_parser_parse_definitely (parser))
14611 abstract_declarator = NULL;
14613 if (type_specifier_seq.type
14614 && type_uses_auto (type_specifier_seq.type))
14616 /* A type-id with type 'auto' is only ok if the abstract declarator
14617 is a function declarator with a late-specified return type. */
14618 if (abstract_declarator
14619 && abstract_declarator->kind == cdk_function
14620 && abstract_declarator->u.function.late_return_type)
14624 error ("invalid use of %<auto%>");
14625 return error_mark_node;
14629 return groktypename (&type_specifier_seq, abstract_declarator,
14633 static tree cp_parser_type_id (cp_parser *parser)
14635 return cp_parser_type_id_1 (parser, false, false);
14638 static tree cp_parser_template_type_arg (cp_parser *parser)
14640 return cp_parser_type_id_1 (parser, true, false);
14643 static tree cp_parser_trailing_type_id (cp_parser *parser)
14645 return cp_parser_type_id_1 (parser, false, true);
14648 /* Parse a type-specifier-seq.
14650 type-specifier-seq:
14651 type-specifier type-specifier-seq [opt]
14655 type-specifier-seq:
14656 attributes type-specifier-seq [opt]
14658 If IS_DECLARATION is true, we are at the start of a "condition" or
14659 exception-declaration, so we might be followed by a declarator-id.
14661 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14662 i.e. we've just seen "->".
14664 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14667 cp_parser_type_specifier_seq (cp_parser* parser,
14668 bool is_declaration,
14669 bool is_trailing_return,
14670 cp_decl_specifier_seq *type_specifier_seq)
14672 bool seen_type_specifier = false;
14673 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14674 cp_token *start_token = NULL;
14676 /* Clear the TYPE_SPECIFIER_SEQ. */
14677 clear_decl_specs (type_specifier_seq);
14679 /* In the context of a trailing return type, enum E { } is an
14680 elaborated-type-specifier followed by a function-body, not an
14682 if (is_trailing_return)
14683 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14685 /* Parse the type-specifiers and attributes. */
14688 tree type_specifier;
14689 bool is_cv_qualifier;
14691 /* Check for attributes first. */
14692 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14694 type_specifier_seq->attributes =
14695 chainon (type_specifier_seq->attributes,
14696 cp_parser_attributes_opt (parser));
14700 /* record the token of the beginning of the type specifier seq,
14701 for error reporting purposes*/
14703 start_token = cp_lexer_peek_token (parser->lexer);
14705 /* Look for the type-specifier. */
14706 type_specifier = cp_parser_type_specifier (parser,
14708 type_specifier_seq,
14709 /*is_declaration=*/false,
14712 if (!type_specifier)
14714 /* If the first type-specifier could not be found, this is not a
14715 type-specifier-seq at all. */
14716 if (!seen_type_specifier)
14718 cp_parser_error (parser, "expected type-specifier");
14719 type_specifier_seq->type = error_mark_node;
14722 /* If subsequent type-specifiers could not be found, the
14723 type-specifier-seq is complete. */
14727 seen_type_specifier = true;
14728 /* The standard says that a condition can be:
14730 type-specifier-seq declarator = assignment-expression
14737 we should treat the "S" as a declarator, not as a
14738 type-specifier. The standard doesn't say that explicitly for
14739 type-specifier-seq, but it does say that for
14740 decl-specifier-seq in an ordinary declaration. Perhaps it
14741 would be clearer just to allow a decl-specifier-seq here, and
14742 then add a semantic restriction that if any decl-specifiers
14743 that are not type-specifiers appear, the program is invalid. */
14744 if (is_declaration && !is_cv_qualifier)
14745 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14748 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14751 /* Parse a parameter-declaration-clause.
14753 parameter-declaration-clause:
14754 parameter-declaration-list [opt] ... [opt]
14755 parameter-declaration-list , ...
14757 Returns a representation for the parameter declarations. A return
14758 value of NULL indicates a parameter-declaration-clause consisting
14759 only of an ellipsis. */
14762 cp_parser_parameter_declaration_clause (cp_parser* parser)
14769 /* Peek at the next token. */
14770 token = cp_lexer_peek_token (parser->lexer);
14771 /* Check for trivial parameter-declaration-clauses. */
14772 if (token->type == CPP_ELLIPSIS)
14774 /* Consume the `...' token. */
14775 cp_lexer_consume_token (parser->lexer);
14778 else if (token->type == CPP_CLOSE_PAREN)
14779 /* There are no parameters. */
14781 #ifndef NO_IMPLICIT_EXTERN_C
14782 if (in_system_header && current_class_type == NULL
14783 && current_lang_name == lang_name_c)
14787 return void_list_node;
14789 /* Check for `(void)', too, which is a special case. */
14790 else if (token->keyword == RID_VOID
14791 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14792 == CPP_CLOSE_PAREN))
14794 /* Consume the `void' token. */
14795 cp_lexer_consume_token (parser->lexer);
14796 /* There are no parameters. */
14797 return void_list_node;
14800 /* Parse the parameter-declaration-list. */
14801 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14802 /* If a parse error occurred while parsing the
14803 parameter-declaration-list, then the entire
14804 parameter-declaration-clause is erroneous. */
14808 /* Peek at the next token. */
14809 token = cp_lexer_peek_token (parser->lexer);
14810 /* If it's a `,', the clause should terminate with an ellipsis. */
14811 if (token->type == CPP_COMMA)
14813 /* Consume the `,'. */
14814 cp_lexer_consume_token (parser->lexer);
14815 /* Expect an ellipsis. */
14817 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14819 /* It might also be `...' if the optional trailing `,' was
14821 else if (token->type == CPP_ELLIPSIS)
14823 /* Consume the `...' token. */
14824 cp_lexer_consume_token (parser->lexer);
14825 /* And remember that we saw it. */
14829 ellipsis_p = false;
14831 /* Finish the parameter list. */
14833 parameters = chainon (parameters, void_list_node);
14838 /* Parse a parameter-declaration-list.
14840 parameter-declaration-list:
14841 parameter-declaration
14842 parameter-declaration-list , parameter-declaration
14844 Returns a representation of the parameter-declaration-list, as for
14845 cp_parser_parameter_declaration_clause. However, the
14846 `void_list_node' is never appended to the list. Upon return,
14847 *IS_ERROR will be true iff an error occurred. */
14850 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14852 tree parameters = NULL_TREE;
14853 tree *tail = ¶meters;
14854 bool saved_in_unbraced_linkage_specification_p;
14857 /* Assume all will go well. */
14859 /* The special considerations that apply to a function within an
14860 unbraced linkage specifications do not apply to the parameters
14861 to the function. */
14862 saved_in_unbraced_linkage_specification_p
14863 = parser->in_unbraced_linkage_specification_p;
14864 parser->in_unbraced_linkage_specification_p = false;
14866 /* Look for more parameters. */
14869 cp_parameter_declarator *parameter;
14870 tree decl = error_mark_node;
14871 bool parenthesized_p;
14872 /* Parse the parameter. */
14874 = cp_parser_parameter_declaration (parser,
14875 /*template_parm_p=*/false,
14878 /* We don't know yet if the enclosing context is deprecated, so wait
14879 and warn in grokparms if appropriate. */
14880 deprecated_state = DEPRECATED_SUPPRESS;
14883 decl = grokdeclarator (parameter->declarator,
14884 ¶meter->decl_specifiers,
14886 parameter->default_argument != NULL_TREE,
14887 ¶meter->decl_specifiers.attributes);
14889 deprecated_state = DEPRECATED_NORMAL;
14891 /* If a parse error occurred parsing the parameter declaration,
14892 then the entire parameter-declaration-list is erroneous. */
14893 if (decl == error_mark_node)
14896 parameters = error_mark_node;
14900 if (parameter->decl_specifiers.attributes)
14901 cplus_decl_attributes (&decl,
14902 parameter->decl_specifiers.attributes,
14904 if (DECL_NAME (decl))
14905 decl = pushdecl (decl);
14907 if (decl != error_mark_node)
14909 retrofit_lang_decl (decl);
14910 DECL_PARM_INDEX (decl) = ++index;
14913 /* Add the new parameter to the list. */
14914 *tail = build_tree_list (parameter->default_argument, decl);
14915 tail = &TREE_CHAIN (*tail);
14917 /* Peek at the next token. */
14918 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14919 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14920 /* These are for Objective-C++ */
14921 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14922 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14923 /* The parameter-declaration-list is complete. */
14925 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14929 /* Peek at the next token. */
14930 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14931 /* If it's an ellipsis, then the list is complete. */
14932 if (token->type == CPP_ELLIPSIS)
14934 /* Otherwise, there must be more parameters. Consume the
14936 cp_lexer_consume_token (parser->lexer);
14937 /* When parsing something like:
14939 int i(float f, double d)
14941 we can tell after seeing the declaration for "f" that we
14942 are not looking at an initialization of a variable "i",
14943 but rather at the declaration of a function "i".
14945 Due to the fact that the parsing of template arguments
14946 (as specified to a template-id) requires backtracking we
14947 cannot use this technique when inside a template argument
14949 if (!parser->in_template_argument_list_p
14950 && !parser->in_type_id_in_expr_p
14951 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14952 /* However, a parameter-declaration of the form
14953 "foat(f)" (which is a valid declaration of a
14954 parameter "f") can also be interpreted as an
14955 expression (the conversion of "f" to "float"). */
14956 && !parenthesized_p)
14957 cp_parser_commit_to_tentative_parse (parser);
14961 cp_parser_error (parser, "expected %<,%> or %<...%>");
14962 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14963 cp_parser_skip_to_closing_parenthesis (parser,
14964 /*recovering=*/true,
14965 /*or_comma=*/false,
14966 /*consume_paren=*/false);
14971 parser->in_unbraced_linkage_specification_p
14972 = saved_in_unbraced_linkage_specification_p;
14977 /* Parse a parameter declaration.
14979 parameter-declaration:
14980 decl-specifier-seq ... [opt] declarator
14981 decl-specifier-seq declarator = assignment-expression
14982 decl-specifier-seq ... [opt] abstract-declarator [opt]
14983 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14985 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14986 declares a template parameter. (In that case, a non-nested `>'
14987 token encountered during the parsing of the assignment-expression
14988 is not interpreted as a greater-than operator.)
14990 Returns a representation of the parameter, or NULL if an error
14991 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14992 true iff the declarator is of the form "(p)". */
14994 static cp_parameter_declarator *
14995 cp_parser_parameter_declaration (cp_parser *parser,
14996 bool template_parm_p,
14997 bool *parenthesized_p)
14999 int declares_class_or_enum;
15000 bool greater_than_is_operator_p;
15001 cp_decl_specifier_seq decl_specifiers;
15002 cp_declarator *declarator;
15003 tree default_argument;
15004 cp_token *token = NULL, *declarator_token_start = NULL;
15005 const char *saved_message;
15007 /* In a template parameter, `>' is not an operator.
15011 When parsing a default template-argument for a non-type
15012 template-parameter, the first non-nested `>' is taken as the end
15013 of the template parameter-list rather than a greater-than
15015 greater_than_is_operator_p = !template_parm_p;
15017 /* Type definitions may not appear in parameter types. */
15018 saved_message = parser->type_definition_forbidden_message;
15019 parser->type_definition_forbidden_message
15020 = "types may not be defined in parameter types";
15022 /* Parse the declaration-specifiers. */
15023 cp_parser_decl_specifier_seq (parser,
15024 CP_PARSER_FLAGS_NONE,
15026 &declares_class_or_enum);
15028 /* Complain about missing 'typename' or other invalid type names. */
15029 if (!decl_specifiers.any_type_specifiers_p)
15030 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15032 /* If an error occurred, there's no reason to attempt to parse the
15033 rest of the declaration. */
15034 if (cp_parser_error_occurred (parser))
15036 parser->type_definition_forbidden_message = saved_message;
15040 /* Peek at the next token. */
15041 token = cp_lexer_peek_token (parser->lexer);
15043 /* If the next token is a `)', `,', `=', `>', or `...', then there
15044 is no declarator. However, when variadic templates are enabled,
15045 there may be a declarator following `...'. */
15046 if (token->type == CPP_CLOSE_PAREN
15047 || token->type == CPP_COMMA
15048 || token->type == CPP_EQ
15049 || token->type == CPP_GREATER)
15052 if (parenthesized_p)
15053 *parenthesized_p = false;
15055 /* Otherwise, there should be a declarator. */
15058 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15059 parser->default_arg_ok_p = false;
15061 /* After seeing a decl-specifier-seq, if the next token is not a
15062 "(", there is no possibility that the code is a valid
15063 expression. Therefore, if parsing tentatively, we commit at
15065 if (!parser->in_template_argument_list_p
15066 /* In an expression context, having seen:
15070 we cannot be sure whether we are looking at a
15071 function-type (taking a "char" as a parameter) or a cast
15072 of some object of type "char" to "int". */
15073 && !parser->in_type_id_in_expr_p
15074 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15075 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15076 cp_parser_commit_to_tentative_parse (parser);
15077 /* Parse the declarator. */
15078 declarator_token_start = token;
15079 declarator = cp_parser_declarator (parser,
15080 CP_PARSER_DECLARATOR_EITHER,
15081 /*ctor_dtor_or_conv_p=*/NULL,
15083 /*member_p=*/false);
15084 parser->default_arg_ok_p = saved_default_arg_ok_p;
15085 /* After the declarator, allow more attributes. */
15086 decl_specifiers.attributes
15087 = chainon (decl_specifiers.attributes,
15088 cp_parser_attributes_opt (parser));
15091 /* If the next token is an ellipsis, and we have not seen a
15092 declarator name, and the type of the declarator contains parameter
15093 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15094 a parameter pack expansion expression. Otherwise, leave the
15095 ellipsis for a C-style variadic function. */
15096 token = cp_lexer_peek_token (parser->lexer);
15097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15099 tree type = decl_specifiers.type;
15101 if (type && DECL_P (type))
15102 type = TREE_TYPE (type);
15105 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15106 && declarator_can_be_parameter_pack (declarator)
15107 && (!declarator || !declarator->parameter_pack_p)
15108 && uses_parameter_packs (type))
15110 /* Consume the `...'. */
15111 cp_lexer_consume_token (parser->lexer);
15112 maybe_warn_variadic_templates ();
15114 /* Build a pack expansion type */
15116 declarator->parameter_pack_p = true;
15118 decl_specifiers.type = make_pack_expansion (type);
15122 /* The restriction on defining new types applies only to the type
15123 of the parameter, not to the default argument. */
15124 parser->type_definition_forbidden_message = saved_message;
15126 /* If the next token is `=', then process a default argument. */
15127 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15129 /* Consume the `='. */
15130 cp_lexer_consume_token (parser->lexer);
15132 /* If we are defining a class, then the tokens that make up the
15133 default argument must be saved and processed later. */
15134 if (!template_parm_p && at_class_scope_p ()
15135 && TYPE_BEING_DEFINED (current_class_type)
15136 && !LAMBDA_TYPE_P (current_class_type))
15138 unsigned depth = 0;
15139 int maybe_template_id = 0;
15140 cp_token *first_token;
15143 /* Add tokens until we have processed the entire default
15144 argument. We add the range [first_token, token). */
15145 first_token = cp_lexer_peek_token (parser->lexer);
15150 /* Peek at the next token. */
15151 token = cp_lexer_peek_token (parser->lexer);
15152 /* What we do depends on what token we have. */
15153 switch (token->type)
15155 /* In valid code, a default argument must be
15156 immediately followed by a `,' `)', or `...'. */
15158 if (depth == 0 && maybe_template_id)
15160 /* If we've seen a '<', we might be in a
15161 template-argument-list. Until Core issue 325 is
15162 resolved, we don't know how this situation ought
15163 to be handled, so try to DTRT. We check whether
15164 what comes after the comma is a valid parameter
15165 declaration list. If it is, then the comma ends
15166 the default argument; otherwise the default
15167 argument continues. */
15168 bool error = false;
15170 /* Set ITALP so cp_parser_parameter_declaration_list
15171 doesn't decide to commit to this parse. */
15172 bool saved_italp = parser->in_template_argument_list_p;
15173 parser->in_template_argument_list_p = true;
15175 cp_parser_parse_tentatively (parser);
15176 cp_lexer_consume_token (parser->lexer);
15177 cp_parser_parameter_declaration_list (parser, &error);
15178 if (!cp_parser_error_occurred (parser) && !error)
15180 cp_parser_abort_tentative_parse (parser);
15182 parser->in_template_argument_list_p = saved_italp;
15185 case CPP_CLOSE_PAREN:
15187 /* If we run into a non-nested `;', `}', or `]',
15188 then the code is invalid -- but the default
15189 argument is certainly over. */
15190 case CPP_SEMICOLON:
15191 case CPP_CLOSE_BRACE:
15192 case CPP_CLOSE_SQUARE:
15195 /* Update DEPTH, if necessary. */
15196 else if (token->type == CPP_CLOSE_PAREN
15197 || token->type == CPP_CLOSE_BRACE
15198 || token->type == CPP_CLOSE_SQUARE)
15202 case CPP_OPEN_PAREN:
15203 case CPP_OPEN_SQUARE:
15204 case CPP_OPEN_BRACE:
15210 /* This might be the comparison operator, or it might
15211 start a template argument list. */
15212 ++maybe_template_id;
15216 if (cxx_dialect == cxx98)
15218 /* Fall through for C++0x, which treats the `>>'
15219 operator like two `>' tokens in certain
15225 /* This might be an operator, or it might close a
15226 template argument list. But if a previous '<'
15227 started a template argument list, this will have
15228 closed it, so we can't be in one anymore. */
15229 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15230 if (maybe_template_id < 0)
15231 maybe_template_id = 0;
15235 /* If we run out of tokens, issue an error message. */
15237 case CPP_PRAGMA_EOL:
15238 error_at (token->location, "file ends in default argument");
15244 /* In these cases, we should look for template-ids.
15245 For example, if the default argument is
15246 `X<int, double>()', we need to do name lookup to
15247 figure out whether or not `X' is a template; if
15248 so, the `,' does not end the default argument.
15250 That is not yet done. */
15257 /* If we've reached the end, stop. */
15261 /* Add the token to the token block. */
15262 token = cp_lexer_consume_token (parser->lexer);
15265 /* Create a DEFAULT_ARG to represent the unparsed default
15267 default_argument = make_node (DEFAULT_ARG);
15268 DEFARG_TOKENS (default_argument)
15269 = cp_token_cache_new (first_token, token);
15270 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15272 /* Outside of a class definition, we can just parse the
15273 assignment-expression. */
15276 token = cp_lexer_peek_token (parser->lexer);
15278 = cp_parser_default_argument (parser, template_parm_p);
15281 if (!parser->default_arg_ok_p)
15283 if (flag_permissive)
15284 warning (0, "deprecated use of default argument for parameter of non-function");
15287 error_at (token->location,
15288 "default arguments are only "
15289 "permitted for function parameters");
15290 default_argument = NULL_TREE;
15293 else if ((declarator && declarator->parameter_pack_p)
15294 || (decl_specifiers.type
15295 && PACK_EXPANSION_P (decl_specifiers.type)))
15297 /* Find the name of the parameter pack. */
15298 cp_declarator *id_declarator = declarator;
15299 while (id_declarator && id_declarator->kind != cdk_id)
15300 id_declarator = id_declarator->declarator;
15302 if (id_declarator && id_declarator->kind == cdk_id)
15303 error_at (declarator_token_start->location,
15305 ? "template parameter pack %qD"
15306 " cannot have a default argument"
15307 : "parameter pack %qD cannot have a default argument",
15308 id_declarator->u.id.unqualified_name);
15310 error_at (declarator_token_start->location,
15312 ? "template parameter pack cannot have a default argument"
15313 : "parameter pack cannot have a default argument");
15315 default_argument = NULL_TREE;
15319 default_argument = NULL_TREE;
15321 return make_parameter_declarator (&decl_specifiers,
15326 /* Parse a default argument and return it.
15328 TEMPLATE_PARM_P is true if this is a default argument for a
15329 non-type template parameter. */
15331 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15333 tree default_argument = NULL_TREE;
15334 bool saved_greater_than_is_operator_p;
15335 bool saved_local_variables_forbidden_p;
15337 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15339 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15340 parser->greater_than_is_operator_p = !template_parm_p;
15341 /* Local variable names (and the `this' keyword) may not
15342 appear in a default argument. */
15343 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15344 parser->local_variables_forbidden_p = true;
15345 /* Parse the assignment-expression. */
15346 if (template_parm_p)
15347 push_deferring_access_checks (dk_no_deferred);
15349 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15350 if (template_parm_p)
15351 pop_deferring_access_checks ();
15352 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15353 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15355 return default_argument;
15358 /* Parse a function-body.
15361 compound_statement */
15364 cp_parser_function_body (cp_parser *parser)
15366 cp_parser_compound_statement (parser, NULL, false);
15369 /* Parse a ctor-initializer-opt followed by a function-body. Return
15370 true if a ctor-initializer was present. */
15373 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15376 bool ctor_initializer_p;
15378 /* Begin the function body. */
15379 body = begin_function_body ();
15380 /* Parse the optional ctor-initializer. */
15381 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15382 /* Parse the function-body. */
15383 cp_parser_function_body (parser);
15384 /* Finish the function body. */
15385 finish_function_body (body);
15387 return ctor_initializer_p;
15390 /* Parse an initializer.
15393 = initializer-clause
15394 ( expression-list )
15396 Returns an expression representing the initializer. If no
15397 initializer is present, NULL_TREE is returned.
15399 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15400 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15401 set to TRUE if there is no initializer present. If there is an
15402 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15403 is set to true; otherwise it is set to false. */
15406 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15407 bool* non_constant_p)
15412 /* Peek at the next token. */
15413 token = cp_lexer_peek_token (parser->lexer);
15415 /* Let our caller know whether or not this initializer was
15417 *is_direct_init = (token->type != CPP_EQ);
15418 /* Assume that the initializer is constant. */
15419 *non_constant_p = false;
15421 if (token->type == CPP_EQ)
15423 /* Consume the `='. */
15424 cp_lexer_consume_token (parser->lexer);
15425 /* Parse the initializer-clause. */
15426 init = cp_parser_initializer_clause (parser, non_constant_p);
15428 else if (token->type == CPP_OPEN_PAREN)
15431 vec = cp_parser_parenthesized_expression_list (parser, false,
15433 /*allow_expansion_p=*/true,
15436 return error_mark_node;
15437 init = build_tree_list_vec (vec);
15438 release_tree_vector (vec);
15440 else if (token->type == CPP_OPEN_BRACE)
15442 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15443 init = cp_parser_braced_list (parser, non_constant_p);
15444 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15448 /* Anything else is an error. */
15449 cp_parser_error (parser, "expected initializer");
15450 init = error_mark_node;
15456 /* Parse an initializer-clause.
15458 initializer-clause:
15459 assignment-expression
15462 Returns an expression representing the initializer.
15464 If the `assignment-expression' production is used the value
15465 returned is simply a representation for the expression.
15467 Otherwise, calls cp_parser_braced_list. */
15470 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15474 /* Assume the expression is constant. */
15475 *non_constant_p = false;
15477 /* If it is not a `{', then we are looking at an
15478 assignment-expression. */
15479 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15482 = cp_parser_constant_expression (parser,
15483 /*allow_non_constant_p=*/true,
15485 if (!*non_constant_p)
15486 initializer = fold_non_dependent_expr (initializer);
15489 initializer = cp_parser_braced_list (parser, non_constant_p);
15491 return initializer;
15494 /* Parse a brace-enclosed initializer list.
15497 { initializer-list , [opt] }
15500 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15501 the elements of the initializer-list (or NULL, if the last
15502 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15503 NULL_TREE. There is no way to detect whether or not the optional
15504 trailing `,' was provided. NON_CONSTANT_P is as for
15505 cp_parser_initializer. */
15508 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15512 /* Consume the `{' token. */
15513 cp_lexer_consume_token (parser->lexer);
15514 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15515 initializer = make_node (CONSTRUCTOR);
15516 /* If it's not a `}', then there is a non-trivial initializer. */
15517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15519 /* Parse the initializer list. */
15520 CONSTRUCTOR_ELTS (initializer)
15521 = cp_parser_initializer_list (parser, non_constant_p);
15522 /* A trailing `,' token is allowed. */
15523 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15524 cp_lexer_consume_token (parser->lexer);
15526 /* Now, there should be a trailing `}'. */
15527 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15528 TREE_TYPE (initializer) = init_list_type_node;
15529 return initializer;
15532 /* Parse an initializer-list.
15535 initializer-clause ... [opt]
15536 initializer-list , initializer-clause ... [opt]
15541 identifier : initializer-clause
15542 initializer-list, identifier : initializer-clause
15544 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15545 for the initializer. If the INDEX of the elt is non-NULL, it is the
15546 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15547 as for cp_parser_initializer. */
15549 static VEC(constructor_elt,gc) *
15550 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15552 VEC(constructor_elt,gc) *v = NULL;
15554 /* Assume all of the expressions are constant. */
15555 *non_constant_p = false;
15557 /* Parse the rest of the list. */
15563 bool clause_non_constant_p;
15565 /* If the next token is an identifier and the following one is a
15566 colon, we are looking at the GNU designated-initializer
15568 if (cp_parser_allow_gnu_extensions_p (parser)
15569 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15570 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15572 /* Warn the user that they are using an extension. */
15573 pedwarn (input_location, OPT_pedantic,
15574 "ISO C++ does not allow designated initializers");
15575 /* Consume the identifier. */
15576 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15577 /* Consume the `:'. */
15578 cp_lexer_consume_token (parser->lexer);
15581 identifier = NULL_TREE;
15583 /* Parse the initializer. */
15584 initializer = cp_parser_initializer_clause (parser,
15585 &clause_non_constant_p);
15586 /* If any clause is non-constant, so is the entire initializer. */
15587 if (clause_non_constant_p)
15588 *non_constant_p = true;
15590 /* If we have an ellipsis, this is an initializer pack
15592 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15594 /* Consume the `...'. */
15595 cp_lexer_consume_token (parser->lexer);
15597 /* Turn the initializer into an initializer expansion. */
15598 initializer = make_pack_expansion (initializer);
15601 /* Add it to the vector. */
15602 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15604 /* If the next token is not a comma, we have reached the end of
15606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15609 /* Peek at the next token. */
15610 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15611 /* If the next token is a `}', then we're still done. An
15612 initializer-clause can have a trailing `,' after the
15613 initializer-list and before the closing `}'. */
15614 if (token->type == CPP_CLOSE_BRACE)
15617 /* Consume the `,' token. */
15618 cp_lexer_consume_token (parser->lexer);
15624 /* Classes [gram.class] */
15626 /* Parse a class-name.
15632 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15633 to indicate that names looked up in dependent types should be
15634 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15635 keyword has been used to indicate that the name that appears next
15636 is a template. TAG_TYPE indicates the explicit tag given before
15637 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15638 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15639 is the class being defined in a class-head.
15641 Returns the TYPE_DECL representing the class. */
15644 cp_parser_class_name (cp_parser *parser,
15645 bool typename_keyword_p,
15646 bool template_keyword_p,
15647 enum tag_types tag_type,
15648 bool check_dependency_p,
15650 bool is_declaration)
15656 tree identifier = NULL_TREE;
15658 /* All class-names start with an identifier. */
15659 token = cp_lexer_peek_token (parser->lexer);
15660 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15662 cp_parser_error (parser, "expected class-name");
15663 return error_mark_node;
15666 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15667 to a template-id, so we save it here. */
15668 scope = parser->scope;
15669 if (scope == error_mark_node)
15670 return error_mark_node;
15672 /* Any name names a type if we're following the `typename' keyword
15673 in a qualified name where the enclosing scope is type-dependent. */
15674 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15675 && dependent_type_p (scope));
15676 /* Handle the common case (an identifier, but not a template-id)
15678 if (token->type == CPP_NAME
15679 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15681 cp_token *identifier_token;
15684 /* Look for the identifier. */
15685 identifier_token = cp_lexer_peek_token (parser->lexer);
15686 ambiguous_p = identifier_token->ambiguous_p;
15687 identifier = cp_parser_identifier (parser);
15688 /* If the next token isn't an identifier, we are certainly not
15689 looking at a class-name. */
15690 if (identifier == error_mark_node)
15691 decl = error_mark_node;
15692 /* If we know this is a type-name, there's no need to look it
15694 else if (typename_p)
15698 tree ambiguous_decls;
15699 /* If we already know that this lookup is ambiguous, then
15700 we've already issued an error message; there's no reason
15704 cp_parser_simulate_error (parser);
15705 return error_mark_node;
15707 /* If the next token is a `::', then the name must be a type
15710 [basic.lookup.qual]
15712 During the lookup for a name preceding the :: scope
15713 resolution operator, object, function, and enumerator
15714 names are ignored. */
15715 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15716 tag_type = typename_type;
15717 /* Look up the name. */
15718 decl = cp_parser_lookup_name (parser, identifier,
15720 /*is_template=*/false,
15721 /*is_namespace=*/false,
15722 check_dependency_p,
15724 identifier_token->location);
15725 if (ambiguous_decls)
15727 error_at (identifier_token->location,
15728 "reference to %qD is ambiguous", identifier);
15729 print_candidates (ambiguous_decls);
15730 if (cp_parser_parsing_tentatively (parser))
15732 identifier_token->ambiguous_p = true;
15733 cp_parser_simulate_error (parser);
15735 return error_mark_node;
15741 /* Try a template-id. */
15742 decl = cp_parser_template_id (parser, template_keyword_p,
15743 check_dependency_p,
15745 if (decl == error_mark_node)
15746 return error_mark_node;
15749 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15751 /* If this is a typename, create a TYPENAME_TYPE. */
15752 if (typename_p && decl != error_mark_node)
15754 decl = make_typename_type (scope, decl, typename_type,
15755 /*complain=*/tf_error);
15756 if (decl != error_mark_node)
15757 decl = TYPE_NAME (decl);
15760 /* Check to see that it is really the name of a class. */
15761 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15762 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15763 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15764 /* Situations like this:
15766 template <typename T> struct A {
15767 typename T::template X<int>::I i;
15770 are problematic. Is `T::template X<int>' a class-name? The
15771 standard does not seem to be definitive, but there is no other
15772 valid interpretation of the following `::'. Therefore, those
15773 names are considered class-names. */
15775 decl = make_typename_type (scope, decl, tag_type, tf_error);
15776 if (decl != error_mark_node)
15777 decl = TYPE_NAME (decl);
15779 else if (TREE_CODE (decl) != TYPE_DECL
15780 || TREE_TYPE (decl) == error_mark_node
15781 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15782 decl = error_mark_node;
15784 if (decl == error_mark_node)
15785 cp_parser_error (parser, "expected class-name");
15786 else if (identifier && !parser->scope)
15787 maybe_note_name_used_in_class (identifier, decl);
15792 /* Parse a class-specifier.
15795 class-head { member-specification [opt] }
15797 Returns the TREE_TYPE representing the class. */
15800 cp_parser_class_specifier (cp_parser* parser)
15803 tree attributes = NULL_TREE;
15804 bool nested_name_specifier_p;
15805 unsigned saved_num_template_parameter_lists;
15806 bool saved_in_function_body;
15807 bool saved_in_unbraced_linkage_specification_p;
15808 tree old_scope = NULL_TREE;
15809 tree scope = NULL_TREE;
15812 push_deferring_access_checks (dk_no_deferred);
15814 /* Parse the class-head. */
15815 type = cp_parser_class_head (parser,
15816 &nested_name_specifier_p,
15819 /* If the class-head was a semantic disaster, skip the entire body
15823 cp_parser_skip_to_end_of_block_or_statement (parser);
15824 pop_deferring_access_checks ();
15825 return error_mark_node;
15828 /* Look for the `{'. */
15829 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15831 pop_deferring_access_checks ();
15832 return error_mark_node;
15835 /* Process the base classes. If they're invalid, skip the
15836 entire class body. */
15837 if (!xref_basetypes (type, bases))
15839 /* Consuming the closing brace yields better error messages
15841 if (cp_parser_skip_to_closing_brace (parser))
15842 cp_lexer_consume_token (parser->lexer);
15843 pop_deferring_access_checks ();
15844 return error_mark_node;
15847 /* Issue an error message if type-definitions are forbidden here. */
15848 cp_parser_check_type_definition (parser);
15849 /* Remember that we are defining one more class. */
15850 ++parser->num_classes_being_defined;
15851 /* Inside the class, surrounding template-parameter-lists do not
15853 saved_num_template_parameter_lists
15854 = parser->num_template_parameter_lists;
15855 parser->num_template_parameter_lists = 0;
15856 /* We are not in a function body. */
15857 saved_in_function_body = parser->in_function_body;
15858 parser->in_function_body = false;
15859 /* We are not immediately inside an extern "lang" block. */
15860 saved_in_unbraced_linkage_specification_p
15861 = parser->in_unbraced_linkage_specification_p;
15862 parser->in_unbraced_linkage_specification_p = false;
15864 /* Start the class. */
15865 if (nested_name_specifier_p)
15867 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15868 old_scope = push_inner_scope (scope);
15870 type = begin_class_definition (type, attributes);
15872 if (type == error_mark_node)
15873 /* If the type is erroneous, skip the entire body of the class. */
15874 cp_parser_skip_to_closing_brace (parser);
15876 /* Parse the member-specification. */
15877 cp_parser_member_specification_opt (parser);
15879 /* Look for the trailing `}'. */
15880 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15881 /* Look for trailing attributes to apply to this class. */
15882 if (cp_parser_allow_gnu_extensions_p (parser))
15883 attributes = cp_parser_attributes_opt (parser);
15884 if (type != error_mark_node)
15885 type = finish_struct (type, attributes);
15886 if (nested_name_specifier_p)
15887 pop_inner_scope (old_scope, scope);
15888 /* If this class is not itself within the scope of another class,
15889 then we need to parse the bodies of all of the queued function
15890 definitions. Note that the queued functions defined in a class
15891 are not always processed immediately following the
15892 class-specifier for that class. Consider:
15895 struct B { void f() { sizeof (A); } };
15898 If `f' were processed before the processing of `A' were
15899 completed, there would be no way to compute the size of `A'.
15900 Note that the nesting we are interested in here is lexical --
15901 not the semantic nesting given by TYPE_CONTEXT. In particular,
15904 struct A { struct B; };
15905 struct A::B { void f() { } };
15907 there is no need to delay the parsing of `A::B::f'. */
15908 if (--parser->num_classes_being_defined == 0)
15912 tree class_type = NULL_TREE;
15913 tree pushed_scope = NULL_TREE;
15915 /* In a first pass, parse default arguments to the functions.
15916 Then, in a second pass, parse the bodies of the functions.
15917 This two-phased approach handles cases like:
15925 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15926 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15927 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15928 TREE_PURPOSE (parser->unparsed_functions_queues)
15929 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15931 fn = TREE_VALUE (queue_entry);
15932 /* If there are default arguments that have not yet been processed,
15933 take care of them now. */
15934 if (class_type != TREE_PURPOSE (queue_entry))
15937 pop_scope (pushed_scope);
15938 class_type = TREE_PURPOSE (queue_entry);
15939 pushed_scope = push_scope (class_type);
15941 /* Make sure that any template parameters are in scope. */
15942 maybe_begin_member_template_processing (fn);
15943 /* Parse the default argument expressions. */
15944 cp_parser_late_parsing_default_args (parser, fn);
15945 /* Remove any template parameters from the symbol table. */
15946 maybe_end_member_template_processing ();
15949 pop_scope (pushed_scope);
15950 /* Now parse the body of the functions. */
15951 for (TREE_VALUE (parser->unparsed_functions_queues)
15952 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15953 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15954 TREE_VALUE (parser->unparsed_functions_queues)
15955 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15957 /* Figure out which function we need to process. */
15958 fn = TREE_VALUE (queue_entry);
15959 /* Parse the function. */
15960 cp_parser_late_parsing_for_member (parser, fn);
15964 /* Put back any saved access checks. */
15965 pop_deferring_access_checks ();
15967 /* Restore saved state. */
15968 parser->in_function_body = saved_in_function_body;
15969 parser->num_template_parameter_lists
15970 = saved_num_template_parameter_lists;
15971 parser->in_unbraced_linkage_specification_p
15972 = saved_in_unbraced_linkage_specification_p;
15977 /* Parse a class-head.
15980 class-key identifier [opt] base-clause [opt]
15981 class-key nested-name-specifier identifier base-clause [opt]
15982 class-key nested-name-specifier [opt] template-id
15986 class-key attributes identifier [opt] base-clause [opt]
15987 class-key attributes nested-name-specifier identifier base-clause [opt]
15988 class-key attributes nested-name-specifier [opt] template-id
15991 Upon return BASES is initialized to the list of base classes (or
15992 NULL, if there are none) in the same form returned by
15993 cp_parser_base_clause.
15995 Returns the TYPE of the indicated class. Sets
15996 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15997 involving a nested-name-specifier was used, and FALSE otherwise.
15999 Returns error_mark_node if this is not a class-head.
16001 Returns NULL_TREE if the class-head is syntactically valid, but
16002 semantically invalid in a way that means we should skip the entire
16003 body of the class. */
16006 cp_parser_class_head (cp_parser* parser,
16007 bool* nested_name_specifier_p,
16008 tree *attributes_p,
16011 tree nested_name_specifier;
16012 enum tag_types class_key;
16013 tree id = NULL_TREE;
16014 tree type = NULL_TREE;
16016 bool template_id_p = false;
16017 bool qualified_p = false;
16018 bool invalid_nested_name_p = false;
16019 bool invalid_explicit_specialization_p = false;
16020 tree pushed_scope = NULL_TREE;
16021 unsigned num_templates;
16022 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16023 /* Assume no nested-name-specifier will be present. */
16024 *nested_name_specifier_p = false;
16025 /* Assume no template parameter lists will be used in defining the
16029 *bases = NULL_TREE;
16031 /* Look for the class-key. */
16032 class_key = cp_parser_class_key (parser);
16033 if (class_key == none_type)
16034 return error_mark_node;
16036 /* Parse the attributes. */
16037 attributes = cp_parser_attributes_opt (parser);
16039 /* If the next token is `::', that is invalid -- but sometimes
16040 people do try to write:
16044 Handle this gracefully by accepting the extra qualifier, and then
16045 issuing an error about it later if this really is a
16046 class-head. If it turns out just to be an elaborated type
16047 specifier, remain silent. */
16048 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16049 qualified_p = true;
16051 push_deferring_access_checks (dk_no_check);
16053 /* Determine the name of the class. Begin by looking for an
16054 optional nested-name-specifier. */
16055 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16056 nested_name_specifier
16057 = cp_parser_nested_name_specifier_opt (parser,
16058 /*typename_keyword_p=*/false,
16059 /*check_dependency_p=*/false,
16061 /*is_declaration=*/false);
16062 /* If there was a nested-name-specifier, then there *must* be an
16064 if (nested_name_specifier)
16066 type_start_token = cp_lexer_peek_token (parser->lexer);
16067 /* Although the grammar says `identifier', it really means
16068 `class-name' or `template-name'. You are only allowed to
16069 define a class that has already been declared with this
16072 The proposed resolution for Core Issue 180 says that wherever
16073 you see `class T::X' you should treat `X' as a type-name.
16075 It is OK to define an inaccessible class; for example:
16077 class A { class B; };
16080 We do not know if we will see a class-name, or a
16081 template-name. We look for a class-name first, in case the
16082 class-name is a template-id; if we looked for the
16083 template-name first we would stop after the template-name. */
16084 cp_parser_parse_tentatively (parser);
16085 type = cp_parser_class_name (parser,
16086 /*typename_keyword_p=*/false,
16087 /*template_keyword_p=*/false,
16089 /*check_dependency_p=*/false,
16090 /*class_head_p=*/true,
16091 /*is_declaration=*/false);
16092 /* If that didn't work, ignore the nested-name-specifier. */
16093 if (!cp_parser_parse_definitely (parser))
16095 invalid_nested_name_p = true;
16096 type_start_token = cp_lexer_peek_token (parser->lexer);
16097 id = cp_parser_identifier (parser);
16098 if (id == error_mark_node)
16101 /* If we could not find a corresponding TYPE, treat this
16102 declaration like an unqualified declaration. */
16103 if (type == error_mark_node)
16104 nested_name_specifier = NULL_TREE;
16105 /* Otherwise, count the number of templates used in TYPE and its
16106 containing scopes. */
16111 for (scope = TREE_TYPE (type);
16112 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16113 scope = (TYPE_P (scope)
16114 ? TYPE_CONTEXT (scope)
16115 : DECL_CONTEXT (scope)))
16117 && CLASS_TYPE_P (scope)
16118 && CLASSTYPE_TEMPLATE_INFO (scope)
16119 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16120 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16124 /* Otherwise, the identifier is optional. */
16127 /* We don't know whether what comes next is a template-id,
16128 an identifier, or nothing at all. */
16129 cp_parser_parse_tentatively (parser);
16130 /* Check for a template-id. */
16131 type_start_token = cp_lexer_peek_token (parser->lexer);
16132 id = cp_parser_template_id (parser,
16133 /*template_keyword_p=*/false,
16134 /*check_dependency_p=*/true,
16135 /*is_declaration=*/true);
16136 /* If that didn't work, it could still be an identifier. */
16137 if (!cp_parser_parse_definitely (parser))
16139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16141 type_start_token = cp_lexer_peek_token (parser->lexer);
16142 id = cp_parser_identifier (parser);
16149 template_id_p = true;
16154 pop_deferring_access_checks ();
16157 cp_parser_check_for_invalid_template_id (parser, id,
16158 type_start_token->location);
16160 /* If it's not a `:' or a `{' then we can't really be looking at a
16161 class-head, since a class-head only appears as part of a
16162 class-specifier. We have to detect this situation before calling
16163 xref_tag, since that has irreversible side-effects. */
16164 if (!cp_parser_next_token_starts_class_definition_p (parser))
16166 cp_parser_error (parser, "expected %<{%> or %<:%>");
16167 return error_mark_node;
16170 /* At this point, we're going ahead with the class-specifier, even
16171 if some other problem occurs. */
16172 cp_parser_commit_to_tentative_parse (parser);
16173 /* Issue the error about the overly-qualified name now. */
16176 cp_parser_error (parser,
16177 "global qualification of class name is invalid");
16178 return error_mark_node;
16180 else if (invalid_nested_name_p)
16182 cp_parser_error (parser,
16183 "qualified name does not name a class");
16184 return error_mark_node;
16186 else if (nested_name_specifier)
16190 /* Reject typedef-names in class heads. */
16191 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16193 error_at (type_start_token->location,
16194 "invalid class name in declaration of %qD",
16200 /* Figure out in what scope the declaration is being placed. */
16201 scope = current_scope ();
16202 /* If that scope does not contain the scope in which the
16203 class was originally declared, the program is invalid. */
16204 if (scope && !is_ancestor (scope, nested_name_specifier))
16206 if (at_namespace_scope_p ())
16207 error_at (type_start_token->location,
16208 "declaration of %qD in namespace %qD which does not "
16210 type, scope, nested_name_specifier);
16212 error_at (type_start_token->location,
16213 "declaration of %qD in %qD which does not enclose %qD",
16214 type, scope, nested_name_specifier);
16220 A declarator-id shall not be qualified except for the
16221 definition of a ... nested class outside of its class
16222 ... [or] the definition or explicit instantiation of a
16223 class member of a namespace outside of its namespace. */
16224 if (scope == nested_name_specifier)
16226 permerror (nested_name_specifier_token_start->location,
16227 "extra qualification not allowed");
16228 nested_name_specifier = NULL_TREE;
16232 /* An explicit-specialization must be preceded by "template <>". If
16233 it is not, try to recover gracefully. */
16234 if (at_namespace_scope_p ()
16235 && parser->num_template_parameter_lists == 0
16238 error_at (type_start_token->location,
16239 "an explicit specialization must be preceded by %<template <>%>");
16240 invalid_explicit_specialization_p = true;
16241 /* Take the same action that would have been taken by
16242 cp_parser_explicit_specialization. */
16243 ++parser->num_template_parameter_lists;
16244 begin_specialization ();
16246 /* There must be no "return" statements between this point and the
16247 end of this function; set "type "to the correct return value and
16248 use "goto done;" to return. */
16249 /* Make sure that the right number of template parameters were
16251 if (!cp_parser_check_template_parameters (parser, num_templates,
16252 type_start_token->location,
16253 /*declarator=*/NULL))
16255 /* If something went wrong, there is no point in even trying to
16256 process the class-definition. */
16261 /* Look up the type. */
16264 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16265 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16266 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16268 error_at (type_start_token->location,
16269 "function template %qD redeclared as a class template", id);
16270 type = error_mark_node;
16274 type = TREE_TYPE (id);
16275 type = maybe_process_partial_specialization (type);
16277 if (nested_name_specifier)
16278 pushed_scope = push_scope (nested_name_specifier);
16280 else if (nested_name_specifier)
16286 template <typename T> struct S { struct T };
16287 template <typename T> struct S<T>::T { };
16289 we will get a TYPENAME_TYPE when processing the definition of
16290 `S::T'. We need to resolve it to the actual type before we
16291 try to define it. */
16292 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16294 class_type = resolve_typename_type (TREE_TYPE (type),
16295 /*only_current_p=*/false);
16296 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16297 type = TYPE_NAME (class_type);
16300 cp_parser_error (parser, "could not resolve typename type");
16301 type = error_mark_node;
16305 if (maybe_process_partial_specialization (TREE_TYPE (type))
16306 == error_mark_node)
16312 class_type = current_class_type;
16313 /* Enter the scope indicated by the nested-name-specifier. */
16314 pushed_scope = push_scope (nested_name_specifier);
16315 /* Get the canonical version of this type. */
16316 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16317 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16318 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16320 type = push_template_decl (type);
16321 if (type == error_mark_node)
16328 type = TREE_TYPE (type);
16329 *nested_name_specifier_p = true;
16331 else /* The name is not a nested name. */
16333 /* If the class was unnamed, create a dummy name. */
16335 id = make_anon_name ();
16336 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16337 parser->num_template_parameter_lists);
16340 /* Indicate whether this class was declared as a `class' or as a
16342 if (TREE_CODE (type) == RECORD_TYPE)
16343 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16344 cp_parser_check_class_key (class_key, type);
16346 /* If this type was already complete, and we see another definition,
16347 that's an error. */
16348 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16350 error_at (type_start_token->location, "redefinition of %q#T",
16352 error_at (type_start_token->location, "previous definition of %q+#T",
16357 else if (type == error_mark_node)
16360 /* We will have entered the scope containing the class; the names of
16361 base classes should be looked up in that context. For example:
16363 struct A { struct B {}; struct C; };
16364 struct A::C : B {};
16368 /* Get the list of base-classes, if there is one. */
16369 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16370 *bases = cp_parser_base_clause (parser);
16373 /* Leave the scope given by the nested-name-specifier. We will
16374 enter the class scope itself while processing the members. */
16376 pop_scope (pushed_scope);
16378 if (invalid_explicit_specialization_p)
16380 end_specialization ();
16381 --parser->num_template_parameter_lists;
16383 *attributes_p = attributes;
16387 /* Parse a class-key.
16394 Returns the kind of class-key specified, or none_type to indicate
16397 static enum tag_types
16398 cp_parser_class_key (cp_parser* parser)
16401 enum tag_types tag_type;
16403 /* Look for the class-key. */
16404 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16408 /* Check to see if the TOKEN is a class-key. */
16409 tag_type = cp_parser_token_is_class_key (token);
16411 cp_parser_error (parser, "expected class-key");
16415 /* Parse an (optional) member-specification.
16417 member-specification:
16418 member-declaration member-specification [opt]
16419 access-specifier : member-specification [opt] */
16422 cp_parser_member_specification_opt (cp_parser* parser)
16429 /* Peek at the next token. */
16430 token = cp_lexer_peek_token (parser->lexer);
16431 /* If it's a `}', or EOF then we've seen all the members. */
16432 if (token->type == CPP_CLOSE_BRACE
16433 || token->type == CPP_EOF
16434 || token->type == CPP_PRAGMA_EOL)
16437 /* See if this token is a keyword. */
16438 keyword = token->keyword;
16442 case RID_PROTECTED:
16444 /* Consume the access-specifier. */
16445 cp_lexer_consume_token (parser->lexer);
16446 /* Remember which access-specifier is active. */
16447 current_access_specifier = token->u.value;
16448 /* Look for the `:'. */
16449 cp_parser_require (parser, CPP_COLON, "%<:%>");
16453 /* Accept #pragmas at class scope. */
16454 if (token->type == CPP_PRAGMA)
16456 cp_parser_pragma (parser, pragma_external);
16460 /* Otherwise, the next construction must be a
16461 member-declaration. */
16462 cp_parser_member_declaration (parser);
16467 /* Parse a member-declaration.
16469 member-declaration:
16470 decl-specifier-seq [opt] member-declarator-list [opt] ;
16471 function-definition ; [opt]
16472 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16474 template-declaration
16476 member-declarator-list:
16478 member-declarator-list , member-declarator
16481 declarator pure-specifier [opt]
16482 declarator constant-initializer [opt]
16483 identifier [opt] : constant-expression
16487 member-declaration:
16488 __extension__ member-declaration
16491 declarator attributes [opt] pure-specifier [opt]
16492 declarator attributes [opt] constant-initializer [opt]
16493 identifier [opt] attributes [opt] : constant-expression
16497 member-declaration:
16498 static_assert-declaration */
16501 cp_parser_member_declaration (cp_parser* parser)
16503 cp_decl_specifier_seq decl_specifiers;
16504 tree prefix_attributes;
16506 int declares_class_or_enum;
16508 cp_token *token = NULL;
16509 cp_token *decl_spec_token_start = NULL;
16510 cp_token *initializer_token_start = NULL;
16511 int saved_pedantic;
16513 /* Check for the `__extension__' keyword. */
16514 if (cp_parser_extension_opt (parser, &saved_pedantic))
16517 cp_parser_member_declaration (parser);
16518 /* Restore the old value of the PEDANTIC flag. */
16519 pedantic = saved_pedantic;
16524 /* Check for a template-declaration. */
16525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16527 /* An explicit specialization here is an error condition, and we
16528 expect the specialization handler to detect and report this. */
16529 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16530 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16531 cp_parser_explicit_specialization (parser);
16533 cp_parser_template_declaration (parser, /*member_p=*/true);
16538 /* Check for a using-declaration. */
16539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16541 /* Parse the using-declaration. */
16542 cp_parser_using_declaration (parser,
16543 /*access_declaration_p=*/false);
16547 /* Check for @defs. */
16548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16551 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16552 ivar = ivar_chains;
16556 ivar = TREE_CHAIN (member);
16557 TREE_CHAIN (member) = NULL_TREE;
16558 finish_member_declaration (member);
16563 /* If the next token is `static_assert' we have a static assertion. */
16564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16566 cp_parser_static_assert (parser, /*member_p=*/true);
16570 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16573 /* Parse the decl-specifier-seq. */
16574 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16575 cp_parser_decl_specifier_seq (parser,
16576 CP_PARSER_FLAGS_OPTIONAL,
16578 &declares_class_or_enum);
16579 prefix_attributes = decl_specifiers.attributes;
16580 decl_specifiers.attributes = NULL_TREE;
16581 /* Check for an invalid type-name. */
16582 if (!decl_specifiers.any_type_specifiers_p
16583 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16585 /* If there is no declarator, then the decl-specifier-seq should
16587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16589 /* If there was no decl-specifier-seq, and the next token is a
16590 `;', then we have something like:
16596 Each member-declaration shall declare at least one member
16597 name of the class. */
16598 if (!decl_specifiers.any_specifiers_p)
16600 cp_token *token = cp_lexer_peek_token (parser->lexer);
16601 if (!in_system_header_at (token->location))
16602 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16608 /* See if this declaration is a friend. */
16609 friend_p = cp_parser_friend_p (&decl_specifiers);
16610 /* If there were decl-specifiers, check to see if there was
16611 a class-declaration. */
16612 type = check_tag_decl (&decl_specifiers);
16613 /* Nested classes have already been added to the class, but
16614 a `friend' needs to be explicitly registered. */
16617 /* If the `friend' keyword was present, the friend must
16618 be introduced with a class-key. */
16619 if (!declares_class_or_enum)
16620 error_at (decl_spec_token_start->location,
16621 "a class-key must be used when declaring a friend");
16624 template <typename T> struct A {
16625 friend struct A<T>::B;
16628 A<T>::B will be represented by a TYPENAME_TYPE, and
16629 therefore not recognized by check_tag_decl. */
16631 && decl_specifiers.type
16632 && TYPE_P (decl_specifiers.type))
16633 type = decl_specifiers.type;
16634 if (!type || !TYPE_P (type))
16635 error_at (decl_spec_token_start->location,
16636 "friend declaration does not name a class or "
16639 make_friend_class (current_class_type, type,
16640 /*complain=*/true);
16642 /* If there is no TYPE, an error message will already have
16644 else if (!type || type == error_mark_node)
16646 /* An anonymous aggregate has to be handled specially; such
16647 a declaration really declares a data member (with a
16648 particular type), as opposed to a nested class. */
16649 else if (ANON_AGGR_TYPE_P (type))
16651 /* Remove constructors and such from TYPE, now that we
16652 know it is an anonymous aggregate. */
16653 fixup_anonymous_aggr (type);
16654 /* And make the corresponding data member. */
16655 decl = build_decl (decl_spec_token_start->location,
16656 FIELD_DECL, NULL_TREE, type);
16657 /* Add it to the class. */
16658 finish_member_declaration (decl);
16661 cp_parser_check_access_in_redeclaration
16663 decl_spec_token_start->location);
16668 /* See if these declarations will be friends. */
16669 friend_p = cp_parser_friend_p (&decl_specifiers);
16671 /* Keep going until we hit the `;' at the end of the
16673 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16675 tree attributes = NULL_TREE;
16676 tree first_attribute;
16678 /* Peek at the next token. */
16679 token = cp_lexer_peek_token (parser->lexer);
16681 /* Check for a bitfield declaration. */
16682 if (token->type == CPP_COLON
16683 || (token->type == CPP_NAME
16684 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16690 /* Get the name of the bitfield. Note that we cannot just
16691 check TOKEN here because it may have been invalidated by
16692 the call to cp_lexer_peek_nth_token above. */
16693 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16694 identifier = cp_parser_identifier (parser);
16696 identifier = NULL_TREE;
16698 /* Consume the `:' token. */
16699 cp_lexer_consume_token (parser->lexer);
16700 /* Get the width of the bitfield. */
16702 = cp_parser_constant_expression (parser,
16703 /*allow_non_constant=*/false,
16706 /* Look for attributes that apply to the bitfield. */
16707 attributes = cp_parser_attributes_opt (parser);
16708 /* Remember which attributes are prefix attributes and
16710 first_attribute = attributes;
16711 /* Combine the attributes. */
16712 attributes = chainon (prefix_attributes, attributes);
16714 /* Create the bitfield declaration. */
16715 decl = grokbitfield (identifier
16716 ? make_id_declarator (NULL_TREE,
16726 cp_declarator *declarator;
16728 tree asm_specification;
16729 int ctor_dtor_or_conv_p;
16731 /* Parse the declarator. */
16733 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16734 &ctor_dtor_or_conv_p,
16735 /*parenthesized_p=*/NULL,
16736 /*member_p=*/true);
16738 /* If something went wrong parsing the declarator, make sure
16739 that we at least consume some tokens. */
16740 if (declarator == cp_error_declarator)
16742 /* Skip to the end of the statement. */
16743 cp_parser_skip_to_end_of_statement (parser);
16744 /* If the next token is not a semicolon, that is
16745 probably because we just skipped over the body of
16746 a function. So, we consume a semicolon if
16747 present, but do not issue an error message if it
16749 if (cp_lexer_next_token_is (parser->lexer,
16751 cp_lexer_consume_token (parser->lexer);
16755 if (declares_class_or_enum & 2)
16756 cp_parser_check_for_definition_in_return_type
16757 (declarator, decl_specifiers.type,
16758 decl_specifiers.type_location);
16760 /* Look for an asm-specification. */
16761 asm_specification = cp_parser_asm_specification_opt (parser);
16762 /* Look for attributes that apply to the declaration. */
16763 attributes = cp_parser_attributes_opt (parser);
16764 /* Remember which attributes are prefix attributes and
16766 first_attribute = attributes;
16767 /* Combine the attributes. */
16768 attributes = chainon (prefix_attributes, attributes);
16770 /* If it's an `=', then we have a constant-initializer or a
16771 pure-specifier. It is not correct to parse the
16772 initializer before registering the member declaration
16773 since the member declaration should be in scope while
16774 its initializer is processed. However, the rest of the
16775 front end does not yet provide an interface that allows
16776 us to handle this correctly. */
16777 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16781 A pure-specifier shall be used only in the declaration of
16782 a virtual function.
16784 A member-declarator can contain a constant-initializer
16785 only if it declares a static member of integral or
16788 Therefore, if the DECLARATOR is for a function, we look
16789 for a pure-specifier; otherwise, we look for a
16790 constant-initializer. When we call `grokfield', it will
16791 perform more stringent semantics checks. */
16792 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16793 if (function_declarator_p (declarator))
16794 initializer = cp_parser_pure_specifier (parser);
16796 /* Parse the initializer. */
16797 initializer = cp_parser_constant_initializer (parser);
16799 /* Otherwise, there is no initializer. */
16801 initializer = NULL_TREE;
16803 /* See if we are probably looking at a function
16804 definition. We are certainly not looking at a
16805 member-declarator. Calling `grokfield' has
16806 side-effects, so we must not do it unless we are sure
16807 that we are looking at a member-declarator. */
16808 if (cp_parser_token_starts_function_definition_p
16809 (cp_lexer_peek_token (parser->lexer)))
16811 /* The grammar does not allow a pure-specifier to be
16812 used when a member function is defined. (It is
16813 possible that this fact is an oversight in the
16814 standard, since a pure function may be defined
16815 outside of the class-specifier. */
16817 error_at (initializer_token_start->location,
16818 "pure-specifier on function-definition");
16819 decl = cp_parser_save_member_function_body (parser,
16823 /* If the member was not a friend, declare it here. */
16825 finish_member_declaration (decl);
16826 /* Peek at the next token. */
16827 token = cp_lexer_peek_token (parser->lexer);
16828 /* If the next token is a semicolon, consume it. */
16829 if (token->type == CPP_SEMICOLON)
16830 cp_lexer_consume_token (parser->lexer);
16834 if (declarator->kind == cdk_function)
16835 declarator->id_loc = token->location;
16836 /* Create the declaration. */
16837 decl = grokfield (declarator, &decl_specifiers,
16838 initializer, /*init_const_expr_p=*/true,
16843 /* Reset PREFIX_ATTRIBUTES. */
16844 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16845 attributes = TREE_CHAIN (attributes);
16847 TREE_CHAIN (attributes) = NULL_TREE;
16849 /* If there is any qualification still in effect, clear it
16850 now; we will be starting fresh with the next declarator. */
16851 parser->scope = NULL_TREE;
16852 parser->qualifying_scope = NULL_TREE;
16853 parser->object_scope = NULL_TREE;
16854 /* If it's a `,', then there are more declarators. */
16855 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16856 cp_lexer_consume_token (parser->lexer);
16857 /* If the next token isn't a `;', then we have a parse error. */
16858 else if (cp_lexer_next_token_is_not (parser->lexer,
16861 cp_parser_error (parser, "expected %<;%>");
16862 /* Skip tokens until we find a `;'. */
16863 cp_parser_skip_to_end_of_statement (parser);
16870 /* Add DECL to the list of members. */
16872 finish_member_declaration (decl);
16874 if (TREE_CODE (decl) == FUNCTION_DECL)
16875 cp_parser_save_default_args (parser, decl);
16880 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16883 /* Parse a pure-specifier.
16888 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16889 Otherwise, ERROR_MARK_NODE is returned. */
16892 cp_parser_pure_specifier (cp_parser* parser)
16896 /* Look for the `=' token. */
16897 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16898 return error_mark_node;
16899 /* Look for the `0' token. */
16900 token = cp_lexer_peek_token (parser->lexer);
16902 if (token->type == CPP_EOF
16903 || token->type == CPP_PRAGMA_EOL)
16904 return error_mark_node;
16906 cp_lexer_consume_token (parser->lexer);
16908 /* Accept = default or = delete in c++0x mode. */
16909 if (token->keyword == RID_DEFAULT
16910 || token->keyword == RID_DELETE)
16912 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16913 return token->u.value;
16916 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16917 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16919 cp_parser_error (parser,
16920 "invalid pure specifier (only %<= 0%> is allowed)");
16921 cp_parser_skip_to_end_of_statement (parser);
16922 return error_mark_node;
16924 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16926 error_at (token->location, "templates may not be %<virtual%>");
16927 return error_mark_node;
16930 return integer_zero_node;
16933 /* Parse a constant-initializer.
16935 constant-initializer:
16936 = constant-expression
16938 Returns a representation of the constant-expression. */
16941 cp_parser_constant_initializer (cp_parser* parser)
16943 /* Look for the `=' token. */
16944 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16945 return error_mark_node;
16947 /* It is invalid to write:
16949 struct S { static const int i = { 7 }; };
16952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16954 cp_parser_error (parser,
16955 "a brace-enclosed initializer is not allowed here");
16956 /* Consume the opening brace. */
16957 cp_lexer_consume_token (parser->lexer);
16958 /* Skip the initializer. */
16959 cp_parser_skip_to_closing_brace (parser);
16960 /* Look for the trailing `}'. */
16961 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16963 return error_mark_node;
16966 return cp_parser_constant_expression (parser,
16967 /*allow_non_constant=*/false,
16971 /* Derived classes [gram.class.derived] */
16973 /* Parse a base-clause.
16976 : base-specifier-list
16978 base-specifier-list:
16979 base-specifier ... [opt]
16980 base-specifier-list , base-specifier ... [opt]
16982 Returns a TREE_LIST representing the base-classes, in the order in
16983 which they were declared. The representation of each node is as
16984 described by cp_parser_base_specifier.
16986 In the case that no bases are specified, this function will return
16987 NULL_TREE, not ERROR_MARK_NODE. */
16990 cp_parser_base_clause (cp_parser* parser)
16992 tree bases = NULL_TREE;
16994 /* Look for the `:' that begins the list. */
16995 cp_parser_require (parser, CPP_COLON, "%<:%>");
16997 /* Scan the base-specifier-list. */
17002 bool pack_expansion_p = false;
17004 /* Look for the base-specifier. */
17005 base = cp_parser_base_specifier (parser);
17006 /* Look for the (optional) ellipsis. */
17007 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17009 /* Consume the `...'. */
17010 cp_lexer_consume_token (parser->lexer);
17012 pack_expansion_p = true;
17015 /* Add BASE to the front of the list. */
17016 if (base != error_mark_node)
17018 if (pack_expansion_p)
17019 /* Make this a pack expansion type. */
17020 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17023 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17025 TREE_CHAIN (base) = bases;
17029 /* Peek at the next token. */
17030 token = cp_lexer_peek_token (parser->lexer);
17031 /* If it's not a comma, then the list is complete. */
17032 if (token->type != CPP_COMMA)
17034 /* Consume the `,'. */
17035 cp_lexer_consume_token (parser->lexer);
17038 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17039 base class had a qualified name. However, the next name that
17040 appears is certainly not qualified. */
17041 parser->scope = NULL_TREE;
17042 parser->qualifying_scope = NULL_TREE;
17043 parser->object_scope = NULL_TREE;
17045 return nreverse (bases);
17048 /* Parse a base-specifier.
17051 :: [opt] nested-name-specifier [opt] class-name
17052 virtual access-specifier [opt] :: [opt] nested-name-specifier
17054 access-specifier virtual [opt] :: [opt] nested-name-specifier
17057 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17058 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17059 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17060 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17063 cp_parser_base_specifier (cp_parser* parser)
17067 bool virtual_p = false;
17068 bool duplicate_virtual_error_issued_p = false;
17069 bool duplicate_access_error_issued_p = false;
17070 bool class_scope_p, template_p;
17071 tree access = access_default_node;
17074 /* Process the optional `virtual' and `access-specifier'. */
17077 /* Peek at the next token. */
17078 token = cp_lexer_peek_token (parser->lexer);
17079 /* Process `virtual'. */
17080 switch (token->keyword)
17083 /* If `virtual' appears more than once, issue an error. */
17084 if (virtual_p && !duplicate_virtual_error_issued_p)
17086 cp_parser_error (parser,
17087 "%<virtual%> specified more than once in base-specified");
17088 duplicate_virtual_error_issued_p = true;
17093 /* Consume the `virtual' token. */
17094 cp_lexer_consume_token (parser->lexer);
17099 case RID_PROTECTED:
17101 /* If more than one access specifier appears, issue an
17103 if (access != access_default_node
17104 && !duplicate_access_error_issued_p)
17106 cp_parser_error (parser,
17107 "more than one access specifier in base-specified");
17108 duplicate_access_error_issued_p = true;
17111 access = ridpointers[(int) token->keyword];
17113 /* Consume the access-specifier. */
17114 cp_lexer_consume_token (parser->lexer);
17123 /* It is not uncommon to see programs mechanically, erroneously, use
17124 the 'typename' keyword to denote (dependent) qualified types
17125 as base classes. */
17126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17128 token = cp_lexer_peek_token (parser->lexer);
17129 if (!processing_template_decl)
17130 error_at (token->location,
17131 "keyword %<typename%> not allowed outside of templates");
17133 error_at (token->location,
17134 "keyword %<typename%> not allowed in this context "
17135 "(the base class is implicitly a type)");
17136 cp_lexer_consume_token (parser->lexer);
17139 /* Look for the optional `::' operator. */
17140 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17141 /* Look for the nested-name-specifier. The simplest way to
17146 The keyword `typename' is not permitted in a base-specifier or
17147 mem-initializer; in these contexts a qualified name that
17148 depends on a template-parameter is implicitly assumed to be a
17151 is to pretend that we have seen the `typename' keyword at this
17153 cp_parser_nested_name_specifier_opt (parser,
17154 /*typename_keyword_p=*/true,
17155 /*check_dependency_p=*/true,
17157 /*is_declaration=*/true);
17158 /* If the base class is given by a qualified name, assume that names
17159 we see are type names or templates, as appropriate. */
17160 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17161 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17163 /* Finally, look for the class-name. */
17164 type = cp_parser_class_name (parser,
17168 /*check_dependency_p=*/true,
17169 /*class_head_p=*/false,
17170 /*is_declaration=*/true);
17172 if (type == error_mark_node)
17173 return error_mark_node;
17175 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17178 /* Exception handling [gram.exception] */
17180 /* Parse an (optional) exception-specification.
17182 exception-specification:
17183 throw ( type-id-list [opt] )
17185 Returns a TREE_LIST representing the exception-specification. The
17186 TREE_VALUE of each node is a type. */
17189 cp_parser_exception_specification_opt (cp_parser* parser)
17194 /* Peek at the next token. */
17195 token = cp_lexer_peek_token (parser->lexer);
17196 /* If it's not `throw', then there's no exception-specification. */
17197 if (!cp_parser_is_keyword (token, RID_THROW))
17200 /* Consume the `throw'. */
17201 cp_lexer_consume_token (parser->lexer);
17203 /* Look for the `('. */
17204 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17206 /* Peek at the next token. */
17207 token = cp_lexer_peek_token (parser->lexer);
17208 /* If it's not a `)', then there is a type-id-list. */
17209 if (token->type != CPP_CLOSE_PAREN)
17211 const char *saved_message;
17213 /* Types may not be defined in an exception-specification. */
17214 saved_message = parser->type_definition_forbidden_message;
17215 parser->type_definition_forbidden_message
17216 = "types may not be defined in an exception-specification";
17217 /* Parse the type-id-list. */
17218 type_id_list = cp_parser_type_id_list (parser);
17219 /* Restore the saved message. */
17220 parser->type_definition_forbidden_message = saved_message;
17223 type_id_list = empty_except_spec;
17225 /* Look for the `)'. */
17226 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17228 return type_id_list;
17231 /* Parse an (optional) type-id-list.
17235 type-id-list , type-id ... [opt]
17237 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17238 in the order that the types were presented. */
17241 cp_parser_type_id_list (cp_parser* parser)
17243 tree types = NULL_TREE;
17250 /* Get the next type-id. */
17251 type = cp_parser_type_id (parser);
17252 /* Parse the optional ellipsis. */
17253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17255 /* Consume the `...'. */
17256 cp_lexer_consume_token (parser->lexer);
17258 /* Turn the type into a pack expansion expression. */
17259 type = make_pack_expansion (type);
17261 /* Add it to the list. */
17262 types = add_exception_specifier (types, type, /*complain=*/1);
17263 /* Peek at the next token. */
17264 token = cp_lexer_peek_token (parser->lexer);
17265 /* If it is not a `,', we are done. */
17266 if (token->type != CPP_COMMA)
17268 /* Consume the `,'. */
17269 cp_lexer_consume_token (parser->lexer);
17272 return nreverse (types);
17275 /* Parse a try-block.
17278 try compound-statement handler-seq */
17281 cp_parser_try_block (cp_parser* parser)
17285 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17286 try_block = begin_try_block ();
17287 cp_parser_compound_statement (parser, NULL, true);
17288 finish_try_block (try_block);
17289 cp_parser_handler_seq (parser);
17290 finish_handler_sequence (try_block);
17295 /* Parse a function-try-block.
17297 function-try-block:
17298 try ctor-initializer [opt] function-body handler-seq */
17301 cp_parser_function_try_block (cp_parser* parser)
17303 tree compound_stmt;
17305 bool ctor_initializer_p;
17307 /* Look for the `try' keyword. */
17308 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17310 /* Let the rest of the front end know where we are. */
17311 try_block = begin_function_try_block (&compound_stmt);
17312 /* Parse the function-body. */
17314 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17315 /* We're done with the `try' part. */
17316 finish_function_try_block (try_block);
17317 /* Parse the handlers. */
17318 cp_parser_handler_seq (parser);
17319 /* We're done with the handlers. */
17320 finish_function_handler_sequence (try_block, compound_stmt);
17322 return ctor_initializer_p;
17325 /* Parse a handler-seq.
17328 handler handler-seq [opt] */
17331 cp_parser_handler_seq (cp_parser* parser)
17337 /* Parse the handler. */
17338 cp_parser_handler (parser);
17339 /* Peek at the next token. */
17340 token = cp_lexer_peek_token (parser->lexer);
17341 /* If it's not `catch' then there are no more handlers. */
17342 if (!cp_parser_is_keyword (token, RID_CATCH))
17347 /* Parse a handler.
17350 catch ( exception-declaration ) compound-statement */
17353 cp_parser_handler (cp_parser* parser)
17358 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17359 handler = begin_handler ();
17360 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17361 declaration = cp_parser_exception_declaration (parser);
17362 finish_handler_parms (declaration, handler);
17363 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17364 cp_parser_compound_statement (parser, NULL, false);
17365 finish_handler (handler);
17368 /* Parse an exception-declaration.
17370 exception-declaration:
17371 type-specifier-seq declarator
17372 type-specifier-seq abstract-declarator
17376 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17377 ellipsis variant is used. */
17380 cp_parser_exception_declaration (cp_parser* parser)
17382 cp_decl_specifier_seq type_specifiers;
17383 cp_declarator *declarator;
17384 const char *saved_message;
17386 /* If it's an ellipsis, it's easy to handle. */
17387 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17389 /* Consume the `...' token. */
17390 cp_lexer_consume_token (parser->lexer);
17394 /* Types may not be defined in exception-declarations. */
17395 saved_message = parser->type_definition_forbidden_message;
17396 parser->type_definition_forbidden_message
17397 = "types may not be defined in exception-declarations";
17399 /* Parse the type-specifier-seq. */
17400 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17401 /*is_trailing_return=*/false,
17403 /* If it's a `)', then there is no declarator. */
17404 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17407 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17408 /*ctor_dtor_or_conv_p=*/NULL,
17409 /*parenthesized_p=*/NULL,
17410 /*member_p=*/false);
17412 /* Restore the saved message. */
17413 parser->type_definition_forbidden_message = saved_message;
17415 if (!type_specifiers.any_specifiers_p)
17416 return error_mark_node;
17418 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17421 /* Parse a throw-expression.
17424 throw assignment-expression [opt]
17426 Returns a THROW_EXPR representing the throw-expression. */
17429 cp_parser_throw_expression (cp_parser* parser)
17434 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17435 token = cp_lexer_peek_token (parser->lexer);
17436 /* Figure out whether or not there is an assignment-expression
17437 following the "throw" keyword. */
17438 if (token->type == CPP_COMMA
17439 || token->type == CPP_SEMICOLON
17440 || token->type == CPP_CLOSE_PAREN
17441 || token->type == CPP_CLOSE_SQUARE
17442 || token->type == CPP_CLOSE_BRACE
17443 || token->type == CPP_COLON)
17444 expression = NULL_TREE;
17446 expression = cp_parser_assignment_expression (parser,
17447 /*cast_p=*/false, NULL);
17449 return build_throw (expression);
17452 /* GNU Extensions */
17454 /* Parse an (optional) asm-specification.
17457 asm ( string-literal )
17459 If the asm-specification is present, returns a STRING_CST
17460 corresponding to the string-literal. Otherwise, returns
17464 cp_parser_asm_specification_opt (cp_parser* parser)
17467 tree asm_specification;
17469 /* Peek at the next token. */
17470 token = cp_lexer_peek_token (parser->lexer);
17471 /* If the next token isn't the `asm' keyword, then there's no
17472 asm-specification. */
17473 if (!cp_parser_is_keyword (token, RID_ASM))
17476 /* Consume the `asm' token. */
17477 cp_lexer_consume_token (parser->lexer);
17478 /* Look for the `('. */
17479 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17481 /* Look for the string-literal. */
17482 asm_specification = cp_parser_string_literal (parser, false, false);
17484 /* Look for the `)'. */
17485 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17487 return asm_specification;
17490 /* Parse an asm-operand-list.
17494 asm-operand-list , asm-operand
17497 string-literal ( expression )
17498 [ string-literal ] string-literal ( expression )
17500 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17501 each node is the expression. The TREE_PURPOSE is itself a
17502 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17503 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17504 is a STRING_CST for the string literal before the parenthesis. Returns
17505 ERROR_MARK_NODE if any of the operands are invalid. */
17508 cp_parser_asm_operand_list (cp_parser* parser)
17510 tree asm_operands = NULL_TREE;
17511 bool invalid_operands = false;
17515 tree string_literal;
17519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17521 /* Consume the `[' token. */
17522 cp_lexer_consume_token (parser->lexer);
17523 /* Read the operand name. */
17524 name = cp_parser_identifier (parser);
17525 if (name != error_mark_node)
17526 name = build_string (IDENTIFIER_LENGTH (name),
17527 IDENTIFIER_POINTER (name));
17528 /* Look for the closing `]'. */
17529 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17533 /* Look for the string-literal. */
17534 string_literal = cp_parser_string_literal (parser, false, false);
17536 /* Look for the `('. */
17537 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17538 /* Parse the expression. */
17539 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17540 /* Look for the `)'. */
17541 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17543 if (name == error_mark_node
17544 || string_literal == error_mark_node
17545 || expression == error_mark_node)
17546 invalid_operands = true;
17548 /* Add this operand to the list. */
17549 asm_operands = tree_cons (build_tree_list (name, string_literal),
17552 /* If the next token is not a `,', there are no more
17554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17556 /* Consume the `,'. */
17557 cp_lexer_consume_token (parser->lexer);
17560 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17563 /* Parse an asm-clobber-list.
17567 asm-clobber-list , string-literal
17569 Returns a TREE_LIST, indicating the clobbers in the order that they
17570 appeared. The TREE_VALUE of each node is a STRING_CST. */
17573 cp_parser_asm_clobber_list (cp_parser* parser)
17575 tree clobbers = NULL_TREE;
17579 tree string_literal;
17581 /* Look for the string literal. */
17582 string_literal = cp_parser_string_literal (parser, false, false);
17583 /* Add it to the list. */
17584 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17585 /* If the next token is not a `,', then the list is
17587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17589 /* Consume the `,' token. */
17590 cp_lexer_consume_token (parser->lexer);
17596 /* Parse an asm-label-list.
17600 asm-label-list , identifier
17602 Returns a TREE_LIST, indicating the labels in the order that they
17603 appeared. The TREE_VALUE of each node is a label. */
17606 cp_parser_asm_label_list (cp_parser* parser)
17608 tree labels = NULL_TREE;
17612 tree identifier, label, name;
17614 /* Look for the identifier. */
17615 identifier = cp_parser_identifier (parser);
17616 if (!error_operand_p (identifier))
17618 label = lookup_label (identifier);
17619 if (TREE_CODE (label) == LABEL_DECL)
17621 TREE_USED (label) = 1;
17622 check_goto (label);
17623 name = build_string (IDENTIFIER_LENGTH (identifier),
17624 IDENTIFIER_POINTER (identifier));
17625 labels = tree_cons (name, label, labels);
17628 /* If the next token is not a `,', then the list is
17630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17632 /* Consume the `,' token. */
17633 cp_lexer_consume_token (parser->lexer);
17636 return nreverse (labels);
17639 /* Parse an (optional) series of attributes.
17642 attributes attribute
17645 __attribute__ (( attribute-list [opt] ))
17647 The return value is as for cp_parser_attribute_list. */
17650 cp_parser_attributes_opt (cp_parser* parser)
17652 tree attributes = NULL_TREE;
17657 tree attribute_list;
17659 /* Peek at the next token. */
17660 token = cp_lexer_peek_token (parser->lexer);
17661 /* If it's not `__attribute__', then we're done. */
17662 if (token->keyword != RID_ATTRIBUTE)
17665 /* Consume the `__attribute__' keyword. */
17666 cp_lexer_consume_token (parser->lexer);
17667 /* Look for the two `(' tokens. */
17668 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17669 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17671 /* Peek at the next token. */
17672 token = cp_lexer_peek_token (parser->lexer);
17673 if (token->type != CPP_CLOSE_PAREN)
17674 /* Parse the attribute-list. */
17675 attribute_list = cp_parser_attribute_list (parser);
17677 /* If the next token is a `)', then there is no attribute
17679 attribute_list = NULL;
17681 /* Look for the two `)' tokens. */
17682 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17683 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17685 /* Add these new attributes to the list. */
17686 attributes = chainon (attributes, attribute_list);
17692 /* Parse an attribute-list.
17696 attribute-list , attribute
17700 identifier ( identifier )
17701 identifier ( identifier , expression-list )
17702 identifier ( expression-list )
17704 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17705 to an attribute. The TREE_PURPOSE of each node is the identifier
17706 indicating which attribute is in use. The TREE_VALUE represents
17707 the arguments, if any. */
17710 cp_parser_attribute_list (cp_parser* parser)
17712 tree attribute_list = NULL_TREE;
17713 bool save_translate_strings_p = parser->translate_strings_p;
17715 parser->translate_strings_p = false;
17722 /* Look for the identifier. We also allow keywords here; for
17723 example `__attribute__ ((const))' is legal. */
17724 token = cp_lexer_peek_token (parser->lexer);
17725 if (token->type == CPP_NAME
17726 || token->type == CPP_KEYWORD)
17728 tree arguments = NULL_TREE;
17730 /* Consume the token. */
17731 token = cp_lexer_consume_token (parser->lexer);
17733 /* Save away the identifier that indicates which attribute
17735 identifier = (token->type == CPP_KEYWORD)
17736 /* For keywords, use the canonical spelling, not the
17737 parsed identifier. */
17738 ? ridpointers[(int) token->keyword]
17741 attribute = build_tree_list (identifier, NULL_TREE);
17743 /* Peek at the next token. */
17744 token = cp_lexer_peek_token (parser->lexer);
17745 /* If it's an `(', then parse the attribute arguments. */
17746 if (token->type == CPP_OPEN_PAREN)
17749 vec = cp_parser_parenthesized_expression_list
17750 (parser, true, /*cast_p=*/false,
17751 /*allow_expansion_p=*/false,
17752 /*non_constant_p=*/NULL);
17754 arguments = error_mark_node;
17757 arguments = build_tree_list_vec (vec);
17758 release_tree_vector (vec);
17760 /* Save the arguments away. */
17761 TREE_VALUE (attribute) = arguments;
17764 if (arguments != error_mark_node)
17766 /* Add this attribute to the list. */
17767 TREE_CHAIN (attribute) = attribute_list;
17768 attribute_list = attribute;
17771 token = cp_lexer_peek_token (parser->lexer);
17773 /* Now, look for more attributes. If the next token isn't a
17774 `,', we're done. */
17775 if (token->type != CPP_COMMA)
17778 /* Consume the comma and keep going. */
17779 cp_lexer_consume_token (parser->lexer);
17781 parser->translate_strings_p = save_translate_strings_p;
17783 /* We built up the list in reverse order. */
17784 return nreverse (attribute_list);
17787 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17788 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17789 current value of the PEDANTIC flag, regardless of whether or not
17790 the `__extension__' keyword is present. The caller is responsible
17791 for restoring the value of the PEDANTIC flag. */
17794 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17796 /* Save the old value of the PEDANTIC flag. */
17797 *saved_pedantic = pedantic;
17799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17801 /* Consume the `__extension__' token. */
17802 cp_lexer_consume_token (parser->lexer);
17803 /* We're not being pedantic while the `__extension__' keyword is
17813 /* Parse a label declaration.
17816 __label__ label-declarator-seq ;
17818 label-declarator-seq:
17819 identifier , label-declarator-seq
17823 cp_parser_label_declaration (cp_parser* parser)
17825 /* Look for the `__label__' keyword. */
17826 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17832 /* Look for an identifier. */
17833 identifier = cp_parser_identifier (parser);
17834 /* If we failed, stop. */
17835 if (identifier == error_mark_node)
17837 /* Declare it as a label. */
17838 finish_label_decl (identifier);
17839 /* If the next token is a `;', stop. */
17840 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17842 /* Look for the `,' separating the label declarations. */
17843 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17846 /* Look for the final `;'. */
17847 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17850 /* Support Functions */
17852 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17853 NAME should have one of the representations used for an
17854 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17855 is returned. If PARSER->SCOPE is a dependent type, then a
17856 SCOPE_REF is returned.
17858 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17859 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17860 was formed. Abstractly, such entities should not be passed to this
17861 function, because they do not need to be looked up, but it is
17862 simpler to check for this special case here, rather than at the
17865 In cases not explicitly covered above, this function returns a
17866 DECL, OVERLOAD, or baselink representing the result of the lookup.
17867 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17870 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17871 (e.g., "struct") that was used. In that case bindings that do not
17872 refer to types are ignored.
17874 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17877 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17880 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17883 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17884 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17885 NULL_TREE otherwise. */
17888 cp_parser_lookup_name (cp_parser *parser, tree name,
17889 enum tag_types tag_type,
17892 bool check_dependency,
17893 tree *ambiguous_decls,
17894 location_t name_location)
17898 tree object_type = parser->context->object_type;
17900 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17901 flags |= LOOKUP_COMPLAIN;
17903 /* Assume that the lookup will be unambiguous. */
17904 if (ambiguous_decls)
17905 *ambiguous_decls = NULL_TREE;
17907 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17908 no longer valid. Note that if we are parsing tentatively, and
17909 the parse fails, OBJECT_TYPE will be automatically restored. */
17910 parser->context->object_type = NULL_TREE;
17912 if (name == error_mark_node)
17913 return error_mark_node;
17915 /* A template-id has already been resolved; there is no lookup to
17917 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17919 if (BASELINK_P (name))
17921 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17922 == TEMPLATE_ID_EXPR);
17926 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17927 it should already have been checked to make sure that the name
17928 used matches the type being destroyed. */
17929 if (TREE_CODE (name) == BIT_NOT_EXPR)
17933 /* Figure out to which type this destructor applies. */
17935 type = parser->scope;
17936 else if (object_type)
17937 type = object_type;
17939 type = current_class_type;
17940 /* If that's not a class type, there is no destructor. */
17941 if (!type || !CLASS_TYPE_P (type))
17942 return error_mark_node;
17943 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17944 lazily_declare_fn (sfk_destructor, type);
17945 if (!CLASSTYPE_DESTRUCTORS (type))
17946 return error_mark_node;
17947 /* If it was a class type, return the destructor. */
17948 return CLASSTYPE_DESTRUCTORS (type);
17951 /* By this point, the NAME should be an ordinary identifier. If
17952 the id-expression was a qualified name, the qualifying scope is
17953 stored in PARSER->SCOPE at this point. */
17954 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17956 /* Perform the lookup. */
17961 if (parser->scope == error_mark_node)
17962 return error_mark_node;
17964 /* If the SCOPE is dependent, the lookup must be deferred until
17965 the template is instantiated -- unless we are explicitly
17966 looking up names in uninstantiated templates. Even then, we
17967 cannot look up the name if the scope is not a class type; it
17968 might, for example, be a template type parameter. */
17969 dependent_p = (TYPE_P (parser->scope)
17970 && dependent_scope_p (parser->scope));
17971 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17973 /* Defer lookup. */
17974 decl = error_mark_node;
17977 tree pushed_scope = NULL_TREE;
17979 /* If PARSER->SCOPE is a dependent type, then it must be a
17980 class type, and we must not be checking dependencies;
17981 otherwise, we would have processed this lookup above. So
17982 that PARSER->SCOPE is not considered a dependent base by
17983 lookup_member, we must enter the scope here. */
17985 pushed_scope = push_scope (parser->scope);
17987 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17988 lookup result and the nested-name-specifier nominates a class C:
17989 * if the name specified after the nested-name-specifier, when
17990 looked up in C, is the injected-class-name of C (Clause 9), or
17991 * if the name specified after the nested-name-specifier is the
17992 same as the identifier or the simple-template-id's template-
17993 name in the last component of the nested-name-specifier,
17994 the name is instead considered to name the constructor of
17995 class C. [ Note: for example, the constructor is not an
17996 acceptable lookup result in an elaborated-type-specifier so
17997 the constructor would not be used in place of the
17998 injected-class-name. --end note ] Such a constructor name
17999 shall be used only in the declarator-id of a declaration that
18000 names a constructor or in a using-declaration. */
18001 if (tag_type == none_type
18002 && CLASS_TYPE_P (parser->scope)
18003 && constructor_name_p (name, parser->scope))
18004 name = ctor_identifier;
18006 /* If the PARSER->SCOPE is a template specialization, it
18007 may be instantiated during name lookup. In that case,
18008 errors may be issued. Even if we rollback the current
18009 tentative parse, those errors are valid. */
18010 decl = lookup_qualified_name (parser->scope, name,
18011 tag_type != none_type,
18012 /*complain=*/true);
18014 /* If we have a single function from a using decl, pull it out. */
18015 if (TREE_CODE (decl) == OVERLOAD
18016 && !really_overloaded_fn (decl))
18017 decl = OVL_FUNCTION (decl);
18020 pop_scope (pushed_scope);
18023 /* If the scope is a dependent type and either we deferred lookup or
18024 we did lookup but didn't find the name, rememeber the name. */
18025 if (decl == error_mark_node && TYPE_P (parser->scope)
18026 && dependent_type_p (parser->scope))
18032 /* The resolution to Core Issue 180 says that `struct
18033 A::B' should be considered a type-name, even if `A'
18035 type = make_typename_type (parser->scope, name, tag_type,
18036 /*complain=*/tf_error);
18037 decl = TYPE_NAME (type);
18039 else if (is_template
18040 && (cp_parser_next_token_ends_template_argument_p (parser)
18041 || cp_lexer_next_token_is (parser->lexer,
18043 decl = make_unbound_class_template (parser->scope,
18045 /*complain=*/tf_error);
18047 decl = build_qualified_name (/*type=*/NULL_TREE,
18048 parser->scope, name,
18051 parser->qualifying_scope = parser->scope;
18052 parser->object_scope = NULL_TREE;
18054 else if (object_type)
18056 tree object_decl = NULL_TREE;
18057 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18058 OBJECT_TYPE is not a class. */
18059 if (CLASS_TYPE_P (object_type))
18060 /* If the OBJECT_TYPE is a template specialization, it may
18061 be instantiated during name lookup. In that case, errors
18062 may be issued. Even if we rollback the current tentative
18063 parse, those errors are valid. */
18064 object_decl = lookup_member (object_type,
18067 tag_type != none_type);
18068 /* Look it up in the enclosing context, too. */
18069 decl = lookup_name_real (name, tag_type != none_type,
18071 /*block_p=*/true, is_namespace, flags);
18072 parser->object_scope = object_type;
18073 parser->qualifying_scope = NULL_TREE;
18075 decl = object_decl;
18079 decl = lookup_name_real (name, tag_type != none_type,
18081 /*block_p=*/true, is_namespace, flags);
18082 parser->qualifying_scope = NULL_TREE;
18083 parser->object_scope = NULL_TREE;
18086 /* If the lookup failed, let our caller know. */
18087 if (!decl || decl == error_mark_node)
18088 return error_mark_node;
18090 /* Pull out the template from an injected-class-name (or multiple). */
18092 decl = maybe_get_template_decl_from_type_decl (decl);
18094 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18095 if (TREE_CODE (decl) == TREE_LIST)
18097 if (ambiguous_decls)
18098 *ambiguous_decls = decl;
18099 /* The error message we have to print is too complicated for
18100 cp_parser_error, so we incorporate its actions directly. */
18101 if (!cp_parser_simulate_error (parser))
18103 error_at (name_location, "reference to %qD is ambiguous",
18105 print_candidates (decl);
18107 return error_mark_node;
18110 gcc_assert (DECL_P (decl)
18111 || TREE_CODE (decl) == OVERLOAD
18112 || TREE_CODE (decl) == SCOPE_REF
18113 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18114 || BASELINK_P (decl));
18116 /* If we have resolved the name of a member declaration, check to
18117 see if the declaration is accessible. When the name resolves to
18118 set of overloaded functions, accessibility is checked when
18119 overload resolution is done.
18121 During an explicit instantiation, access is not checked at all,
18122 as per [temp.explicit]. */
18124 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18129 /* Like cp_parser_lookup_name, but for use in the typical case where
18130 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18131 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18134 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18136 return cp_parser_lookup_name (parser, name,
18138 /*is_template=*/false,
18139 /*is_namespace=*/false,
18140 /*check_dependency=*/true,
18141 /*ambiguous_decls=*/NULL,
18145 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18146 the current context, return the TYPE_DECL. If TAG_NAME_P is
18147 true, the DECL indicates the class being defined in a class-head,
18148 or declared in an elaborated-type-specifier.
18150 Otherwise, return DECL. */
18153 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18155 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18156 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18159 template <typename T> struct B;
18162 template <typename T> struct A::B {};
18164 Similarly, in an elaborated-type-specifier:
18166 namespace N { struct X{}; }
18169 template <typename T> friend struct N::X;
18172 However, if the DECL refers to a class type, and we are in
18173 the scope of the class, then the name lookup automatically
18174 finds the TYPE_DECL created by build_self_reference rather
18175 than a TEMPLATE_DECL. For example, in:
18177 template <class T> struct S {
18181 there is no need to handle such case. */
18183 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18184 return DECL_TEMPLATE_RESULT (decl);
18189 /* If too many, or too few, template-parameter lists apply to the
18190 declarator, issue an error message. Returns TRUE if all went well,
18191 and FALSE otherwise. */
18194 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18195 cp_declarator *declarator,
18196 location_t declarator_location)
18198 unsigned num_templates;
18200 /* We haven't seen any classes that involve template parameters yet. */
18203 switch (declarator->kind)
18206 if (declarator->u.id.qualifying_scope)
18211 scope = declarator->u.id.qualifying_scope;
18212 member = declarator->u.id.unqualified_name;
18214 while (scope && CLASS_TYPE_P (scope))
18216 /* You're supposed to have one `template <...>'
18217 for every template class, but you don't need one
18218 for a full specialization. For example:
18220 template <class T> struct S{};
18221 template <> struct S<int> { void f(); };
18222 void S<int>::f () {}
18224 is correct; there shouldn't be a `template <>' for
18225 the definition of `S<int>::f'. */
18226 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18227 /* If SCOPE does not have template information of any
18228 kind, then it is not a template, nor is it nested
18229 within a template. */
18231 if (explicit_class_specialization_p (scope))
18233 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18236 scope = TYPE_CONTEXT (scope);
18239 else if (TREE_CODE (declarator->u.id.unqualified_name)
18240 == TEMPLATE_ID_EXPR)
18241 /* If the DECLARATOR has the form `X<y>' then it uses one
18242 additional level of template parameters. */
18245 return cp_parser_check_template_parameters
18246 (parser, num_templates, declarator_location, declarator);
18252 case cdk_reference:
18254 return (cp_parser_check_declarator_template_parameters
18255 (parser, declarator->declarator, declarator_location));
18261 gcc_unreachable ();
18266 /* NUM_TEMPLATES were used in the current declaration. If that is
18267 invalid, return FALSE and issue an error messages. Otherwise,
18268 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18269 declarator and we can print more accurate diagnostics. */
18272 cp_parser_check_template_parameters (cp_parser* parser,
18273 unsigned num_templates,
18274 location_t location,
18275 cp_declarator *declarator)
18277 /* If there are the same number of template classes and parameter
18278 lists, that's OK. */
18279 if (parser->num_template_parameter_lists == num_templates)
18281 /* If there are more, but only one more, then we are referring to a
18282 member template. That's OK too. */
18283 if (parser->num_template_parameter_lists == num_templates + 1)
18285 /* If there are more template classes than parameter lists, we have
18288 template <class T> void S<T>::R<T>::f (); */
18289 if (parser->num_template_parameter_lists < num_templates)
18291 if (declarator && !current_function_decl)
18292 error_at (location, "specializing member %<%T::%E%> "
18293 "requires %<template<>%> syntax",
18294 declarator->u.id.qualifying_scope,
18295 declarator->u.id.unqualified_name);
18296 else if (declarator)
18297 error_at (location, "invalid declaration of %<%T::%E%>",
18298 declarator->u.id.qualifying_scope,
18299 declarator->u.id.unqualified_name);
18301 error_at (location, "too few template-parameter-lists");
18304 /* Otherwise, there are too many template parameter lists. We have
18307 template <class T> template <class U> void S::f(); */
18308 error_at (location, "too many template-parameter-lists");
18312 /* Parse an optional `::' token indicating that the following name is
18313 from the global namespace. If so, PARSER->SCOPE is set to the
18314 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18315 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18316 Returns the new value of PARSER->SCOPE, if the `::' token is
18317 present, and NULL_TREE otherwise. */
18320 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18324 /* Peek at the next token. */
18325 token = cp_lexer_peek_token (parser->lexer);
18326 /* If we're looking at a `::' token then we're starting from the
18327 global namespace, not our current location. */
18328 if (token->type == CPP_SCOPE)
18330 /* Consume the `::' token. */
18331 cp_lexer_consume_token (parser->lexer);
18332 /* Set the SCOPE so that we know where to start the lookup. */
18333 parser->scope = global_namespace;
18334 parser->qualifying_scope = global_namespace;
18335 parser->object_scope = NULL_TREE;
18337 return parser->scope;
18339 else if (!current_scope_valid_p)
18341 parser->scope = NULL_TREE;
18342 parser->qualifying_scope = NULL_TREE;
18343 parser->object_scope = NULL_TREE;
18349 /* Returns TRUE if the upcoming token sequence is the start of a
18350 constructor declarator. If FRIEND_P is true, the declarator is
18351 preceded by the `friend' specifier. */
18354 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18356 bool constructor_p;
18357 tree nested_name_specifier;
18358 cp_token *next_token;
18360 /* The common case is that this is not a constructor declarator, so
18361 try to avoid doing lots of work if at all possible. It's not
18362 valid declare a constructor at function scope. */
18363 if (parser->in_function_body)
18365 /* And only certain tokens can begin a constructor declarator. */
18366 next_token = cp_lexer_peek_token (parser->lexer);
18367 if (next_token->type != CPP_NAME
18368 && next_token->type != CPP_SCOPE
18369 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18370 && next_token->type != CPP_TEMPLATE_ID)
18373 /* Parse tentatively; we are going to roll back all of the tokens
18375 cp_parser_parse_tentatively (parser);
18376 /* Assume that we are looking at a constructor declarator. */
18377 constructor_p = true;
18379 /* Look for the optional `::' operator. */
18380 cp_parser_global_scope_opt (parser,
18381 /*current_scope_valid_p=*/false);
18382 /* Look for the nested-name-specifier. */
18383 nested_name_specifier
18384 = (cp_parser_nested_name_specifier_opt (parser,
18385 /*typename_keyword_p=*/false,
18386 /*check_dependency_p=*/false,
18388 /*is_declaration=*/false));
18389 /* Outside of a class-specifier, there must be a
18390 nested-name-specifier. */
18391 if (!nested_name_specifier &&
18392 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18394 constructor_p = false;
18395 else if (nested_name_specifier == error_mark_node)
18396 constructor_p = false;
18398 /* If we have a class scope, this is easy; DR 147 says that S::S always
18399 names the constructor, and no other qualified name could. */
18400 if (constructor_p && nested_name_specifier
18401 && TYPE_P (nested_name_specifier))
18403 tree id = cp_parser_unqualified_id (parser,
18404 /*template_keyword_p=*/false,
18405 /*check_dependency_p=*/false,
18406 /*declarator_p=*/true,
18407 /*optional_p=*/false);
18408 if (is_overloaded_fn (id))
18409 id = DECL_NAME (get_first_fn (id));
18410 if (!constructor_name_p (id, nested_name_specifier))
18411 constructor_p = false;
18413 /* If we still think that this might be a constructor-declarator,
18414 look for a class-name. */
18415 else if (constructor_p)
18419 template <typename T> struct S {
18423 we must recognize that the nested `S' names a class. */
18425 type_decl = cp_parser_class_name (parser,
18426 /*typename_keyword_p=*/false,
18427 /*template_keyword_p=*/false,
18429 /*check_dependency_p=*/false,
18430 /*class_head_p=*/false,
18431 /*is_declaration=*/false);
18432 /* If there was no class-name, then this is not a constructor. */
18433 constructor_p = !cp_parser_error_occurred (parser);
18435 /* If we're still considering a constructor, we have to see a `(',
18436 to begin the parameter-declaration-clause, followed by either a
18437 `)', an `...', or a decl-specifier. We need to check for a
18438 type-specifier to avoid being fooled into thinking that:
18442 is a constructor. (It is actually a function named `f' that
18443 takes one parameter (of type `int') and returns a value of type
18446 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18447 constructor_p = false;
18450 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18451 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18452 /* A parameter declaration begins with a decl-specifier,
18453 which is either the "attribute" keyword, a storage class
18454 specifier, or (usually) a type-specifier. */
18455 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18458 tree pushed_scope = NULL_TREE;
18459 unsigned saved_num_template_parameter_lists;
18461 /* Names appearing in the type-specifier should be looked up
18462 in the scope of the class. */
18463 if (current_class_type)
18467 type = TREE_TYPE (type_decl);
18468 if (TREE_CODE (type) == TYPENAME_TYPE)
18470 type = resolve_typename_type (type,
18471 /*only_current_p=*/false);
18472 if (TREE_CODE (type) == TYPENAME_TYPE)
18474 cp_parser_abort_tentative_parse (parser);
18478 pushed_scope = push_scope (type);
18481 /* Inside the constructor parameter list, surrounding
18482 template-parameter-lists do not apply. */
18483 saved_num_template_parameter_lists
18484 = parser->num_template_parameter_lists;
18485 parser->num_template_parameter_lists = 0;
18487 /* Look for the type-specifier. */
18488 cp_parser_type_specifier (parser,
18489 CP_PARSER_FLAGS_NONE,
18490 /*decl_specs=*/NULL,
18491 /*is_declarator=*/true,
18492 /*declares_class_or_enum=*/NULL,
18493 /*is_cv_qualifier=*/NULL);
18495 parser->num_template_parameter_lists
18496 = saved_num_template_parameter_lists;
18498 /* Leave the scope of the class. */
18500 pop_scope (pushed_scope);
18502 constructor_p = !cp_parser_error_occurred (parser);
18506 /* We did not really want to consume any tokens. */
18507 cp_parser_abort_tentative_parse (parser);
18509 return constructor_p;
18512 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18513 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18514 they must be performed once we are in the scope of the function.
18516 Returns the function defined. */
18519 cp_parser_function_definition_from_specifiers_and_declarator
18520 (cp_parser* parser,
18521 cp_decl_specifier_seq *decl_specifiers,
18523 const cp_declarator *declarator)
18528 /* Begin the function-definition. */
18529 success_p = start_function (decl_specifiers, declarator, attributes);
18531 /* The things we're about to see are not directly qualified by any
18532 template headers we've seen thus far. */
18533 reset_specialization ();
18535 /* If there were names looked up in the decl-specifier-seq that we
18536 did not check, check them now. We must wait until we are in the
18537 scope of the function to perform the checks, since the function
18538 might be a friend. */
18539 perform_deferred_access_checks ();
18543 /* Skip the entire function. */
18544 cp_parser_skip_to_end_of_block_or_statement (parser);
18545 fn = error_mark_node;
18547 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18549 /* Seen already, skip it. An error message has already been output. */
18550 cp_parser_skip_to_end_of_block_or_statement (parser);
18551 fn = current_function_decl;
18552 current_function_decl = NULL_TREE;
18553 /* If this is a function from a class, pop the nested class. */
18554 if (current_class_name)
18555 pop_nested_class ();
18558 fn = cp_parser_function_definition_after_declarator (parser,
18559 /*inline_p=*/false);
18564 /* Parse the part of a function-definition that follows the
18565 declarator. INLINE_P is TRUE iff this function is an inline
18566 function defined within a class-specifier.
18568 Returns the function defined. */
18571 cp_parser_function_definition_after_declarator (cp_parser* parser,
18575 bool ctor_initializer_p = false;
18576 bool saved_in_unbraced_linkage_specification_p;
18577 bool saved_in_function_body;
18578 unsigned saved_num_template_parameter_lists;
18581 saved_in_function_body = parser->in_function_body;
18582 parser->in_function_body = true;
18583 /* If the next token is `return', then the code may be trying to
18584 make use of the "named return value" extension that G++ used to
18586 token = cp_lexer_peek_token (parser->lexer);
18587 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18589 /* Consume the `return' keyword. */
18590 cp_lexer_consume_token (parser->lexer);
18591 /* Look for the identifier that indicates what value is to be
18593 cp_parser_identifier (parser);
18594 /* Issue an error message. */
18595 error_at (token->location,
18596 "named return values are no longer supported");
18597 /* Skip tokens until we reach the start of the function body. */
18600 cp_token *token = cp_lexer_peek_token (parser->lexer);
18601 if (token->type == CPP_OPEN_BRACE
18602 || token->type == CPP_EOF
18603 || token->type == CPP_PRAGMA_EOL)
18605 cp_lexer_consume_token (parser->lexer);
18608 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18609 anything declared inside `f'. */
18610 saved_in_unbraced_linkage_specification_p
18611 = parser->in_unbraced_linkage_specification_p;
18612 parser->in_unbraced_linkage_specification_p = false;
18613 /* Inside the function, surrounding template-parameter-lists do not
18615 saved_num_template_parameter_lists
18616 = parser->num_template_parameter_lists;
18617 parser->num_template_parameter_lists = 0;
18619 start_lambda_scope (current_function_decl);
18621 /* If the next token is `try', then we are looking at a
18622 function-try-block. */
18623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18624 ctor_initializer_p = cp_parser_function_try_block (parser);
18625 /* A function-try-block includes the function-body, so we only do
18626 this next part if we're not processing a function-try-block. */
18629 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18631 finish_lambda_scope ();
18633 /* Finish the function. */
18634 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18635 (inline_p ? 2 : 0));
18636 /* Generate code for it, if necessary. */
18637 expand_or_defer_fn (fn);
18638 /* Restore the saved values. */
18639 parser->in_unbraced_linkage_specification_p
18640 = saved_in_unbraced_linkage_specification_p;
18641 parser->num_template_parameter_lists
18642 = saved_num_template_parameter_lists;
18643 parser->in_function_body = saved_in_function_body;
18648 /* Parse a template-declaration, assuming that the `export' (and
18649 `extern') keywords, if present, has already been scanned. MEMBER_P
18650 is as for cp_parser_template_declaration. */
18653 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18655 tree decl = NULL_TREE;
18656 VEC (deferred_access_check,gc) *checks;
18657 tree parameter_list;
18658 bool friend_p = false;
18659 bool need_lang_pop;
18662 /* Look for the `template' keyword. */
18663 token = cp_lexer_peek_token (parser->lexer);
18664 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18668 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18670 if (at_class_scope_p () && current_function_decl)
18672 /* 14.5.2.2 [temp.mem]
18674 A local class shall not have member templates. */
18675 error_at (token->location,
18676 "invalid declaration of member template in local class");
18677 cp_parser_skip_to_end_of_block_or_statement (parser);
18682 A template ... shall not have C linkage. */
18683 if (current_lang_name == lang_name_c)
18685 error_at (token->location, "template with C linkage");
18686 /* Give it C++ linkage to avoid confusing other parts of the
18688 push_lang_context (lang_name_cplusplus);
18689 need_lang_pop = true;
18692 need_lang_pop = false;
18694 /* We cannot perform access checks on the template parameter
18695 declarations until we know what is being declared, just as we
18696 cannot check the decl-specifier list. */
18697 push_deferring_access_checks (dk_deferred);
18699 /* If the next token is `>', then we have an invalid
18700 specialization. Rather than complain about an invalid template
18701 parameter, issue an error message here. */
18702 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18704 cp_parser_error (parser, "invalid explicit specialization");
18705 begin_specialization ();
18706 parameter_list = NULL_TREE;
18709 /* Parse the template parameters. */
18710 parameter_list = cp_parser_template_parameter_list (parser);
18712 /* Get the deferred access checks from the parameter list. These
18713 will be checked once we know what is being declared, as for a
18714 member template the checks must be performed in the scope of the
18715 class containing the member. */
18716 checks = get_deferred_access_checks ();
18718 /* Look for the `>'. */
18719 cp_parser_skip_to_end_of_template_parameter_list (parser);
18720 /* We just processed one more parameter list. */
18721 ++parser->num_template_parameter_lists;
18722 /* If the next token is `template', there are more template
18724 if (cp_lexer_next_token_is_keyword (parser->lexer,
18726 cp_parser_template_declaration_after_export (parser, member_p);
18729 /* There are no access checks when parsing a template, as we do not
18730 know if a specialization will be a friend. */
18731 push_deferring_access_checks (dk_no_check);
18732 token = cp_lexer_peek_token (parser->lexer);
18733 decl = cp_parser_single_declaration (parser,
18736 /*explicit_specialization_p=*/false,
18738 pop_deferring_access_checks ();
18740 /* If this is a member template declaration, let the front
18742 if (member_p && !friend_p && decl)
18744 if (TREE_CODE (decl) == TYPE_DECL)
18745 cp_parser_check_access_in_redeclaration (decl, token->location);
18747 decl = finish_member_template_decl (decl);
18749 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18750 make_friend_class (current_class_type, TREE_TYPE (decl),
18751 /*complain=*/true);
18753 /* We are done with the current parameter list. */
18754 --parser->num_template_parameter_lists;
18756 pop_deferring_access_checks ();
18759 finish_template_decl (parameter_list);
18761 /* Register member declarations. */
18762 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18763 finish_member_declaration (decl);
18764 /* For the erroneous case of a template with C linkage, we pushed an
18765 implicit C++ linkage scope; exit that scope now. */
18767 pop_lang_context ();
18768 /* If DECL is a function template, we must return to parse it later.
18769 (Even though there is no definition, there might be default
18770 arguments that need handling.) */
18771 if (member_p && decl
18772 && (TREE_CODE (decl) == FUNCTION_DECL
18773 || DECL_FUNCTION_TEMPLATE_P (decl)))
18774 TREE_VALUE (parser->unparsed_functions_queues)
18775 = tree_cons (NULL_TREE, decl,
18776 TREE_VALUE (parser->unparsed_functions_queues));
18779 /* Perform the deferred access checks from a template-parameter-list.
18780 CHECKS is a TREE_LIST of access checks, as returned by
18781 get_deferred_access_checks. */
18784 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18786 ++processing_template_parmlist;
18787 perform_access_checks (checks);
18788 --processing_template_parmlist;
18791 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18792 `function-definition' sequence. MEMBER_P is true, this declaration
18793 appears in a class scope.
18795 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18796 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18799 cp_parser_single_declaration (cp_parser* parser,
18800 VEC (deferred_access_check,gc)* checks,
18802 bool explicit_specialization_p,
18805 int declares_class_or_enum;
18806 tree decl = NULL_TREE;
18807 cp_decl_specifier_seq decl_specifiers;
18808 bool function_definition_p = false;
18809 cp_token *decl_spec_token_start;
18811 /* This function is only used when processing a template
18813 gcc_assert (innermost_scope_kind () == sk_template_parms
18814 || innermost_scope_kind () == sk_template_spec);
18816 /* Defer access checks until we know what is being declared. */
18817 push_deferring_access_checks (dk_deferred);
18819 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18821 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18822 cp_parser_decl_specifier_seq (parser,
18823 CP_PARSER_FLAGS_OPTIONAL,
18825 &declares_class_or_enum);
18827 *friend_p = cp_parser_friend_p (&decl_specifiers);
18829 /* There are no template typedefs. */
18830 if (decl_specifiers.specs[(int) ds_typedef])
18832 error_at (decl_spec_token_start->location,
18833 "template declaration of %<typedef%>");
18834 decl = error_mark_node;
18837 /* Gather up the access checks that occurred the
18838 decl-specifier-seq. */
18839 stop_deferring_access_checks ();
18841 /* Check for the declaration of a template class. */
18842 if (declares_class_or_enum)
18844 if (cp_parser_declares_only_class_p (parser))
18846 decl = shadow_tag (&decl_specifiers);
18851 friend template <typename T> struct A<T>::B;
18854 A<T>::B will be represented by a TYPENAME_TYPE, and
18855 therefore not recognized by shadow_tag. */
18856 if (friend_p && *friend_p
18858 && decl_specifiers.type
18859 && TYPE_P (decl_specifiers.type))
18860 decl = decl_specifiers.type;
18862 if (decl && decl != error_mark_node)
18863 decl = TYPE_NAME (decl);
18865 decl = error_mark_node;
18867 /* Perform access checks for template parameters. */
18868 cp_parser_perform_template_parameter_access_checks (checks);
18872 /* Complain about missing 'typename' or other invalid type names. */
18873 if (!decl_specifiers.any_type_specifiers_p)
18874 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18876 /* If it's not a template class, try for a template function. If
18877 the next token is a `;', then this declaration does not declare
18878 anything. But, if there were errors in the decl-specifiers, then
18879 the error might well have come from an attempted class-specifier.
18880 In that case, there's no need to warn about a missing declarator. */
18882 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18883 || decl_specifiers.type != error_mark_node))
18885 decl = cp_parser_init_declarator (parser,
18888 /*function_definition_allowed_p=*/true,
18890 declares_class_or_enum,
18891 &function_definition_p);
18893 /* 7.1.1-1 [dcl.stc]
18895 A storage-class-specifier shall not be specified in an explicit
18896 specialization... */
18898 && explicit_specialization_p
18899 && decl_specifiers.storage_class != sc_none)
18901 error_at (decl_spec_token_start->location,
18902 "explicit template specialization cannot have a storage class");
18903 decl = error_mark_node;
18907 pop_deferring_access_checks ();
18909 /* Clear any current qualification; whatever comes next is the start
18910 of something new. */
18911 parser->scope = NULL_TREE;
18912 parser->qualifying_scope = NULL_TREE;
18913 parser->object_scope = NULL_TREE;
18914 /* Look for a trailing `;' after the declaration. */
18915 if (!function_definition_p
18916 && (decl == error_mark_node
18917 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18918 cp_parser_skip_to_end_of_block_or_statement (parser);
18923 /* Parse a cast-expression that is not the operand of a unary "&". */
18926 cp_parser_simple_cast_expression (cp_parser *parser)
18928 return cp_parser_cast_expression (parser, /*address_p=*/false,
18929 /*cast_p=*/false, NULL);
18932 /* Parse a functional cast to TYPE. Returns an expression
18933 representing the cast. */
18936 cp_parser_functional_cast (cp_parser* parser, tree type)
18939 tree expression_list;
18943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18945 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18946 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18947 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18948 if (TREE_CODE (type) == TYPE_DECL)
18949 type = TREE_TYPE (type);
18950 return finish_compound_literal (type, expression_list);
18954 vec = cp_parser_parenthesized_expression_list (parser, false,
18956 /*allow_expansion_p=*/true,
18957 /*non_constant_p=*/NULL);
18959 expression_list = error_mark_node;
18962 expression_list = build_tree_list_vec (vec);
18963 release_tree_vector (vec);
18966 cast = build_functional_cast (type, expression_list,
18967 tf_warning_or_error);
18968 /* [expr.const]/1: In an integral constant expression "only type
18969 conversions to integral or enumeration type can be used". */
18970 if (TREE_CODE (type) == TYPE_DECL)
18971 type = TREE_TYPE (type);
18972 if (cast != error_mark_node
18973 && !cast_valid_in_integral_constant_expression_p (type)
18974 && (cp_parser_non_integral_constant_expression
18975 (parser, "a call to a constructor")))
18976 return error_mark_node;
18980 /* Save the tokens that make up the body of a member function defined
18981 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18982 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18983 specifiers applied to the declaration. Returns the FUNCTION_DECL
18984 for the member function. */
18987 cp_parser_save_member_function_body (cp_parser* parser,
18988 cp_decl_specifier_seq *decl_specifiers,
18989 cp_declarator *declarator,
18996 /* Create the FUNCTION_DECL. */
18997 fn = grokmethod (decl_specifiers, declarator, attributes);
18998 /* If something went badly wrong, bail out now. */
18999 if (fn == error_mark_node)
19001 /* If there's a function-body, skip it. */
19002 if (cp_parser_token_starts_function_definition_p
19003 (cp_lexer_peek_token (parser->lexer)))
19004 cp_parser_skip_to_end_of_block_or_statement (parser);
19005 return error_mark_node;
19008 /* Remember it, if there default args to post process. */
19009 cp_parser_save_default_args (parser, fn);
19011 /* Save away the tokens that make up the body of the
19013 first = parser->lexer->next_token;
19014 /* We can have braced-init-list mem-initializers before the fn body. */
19015 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19017 cp_lexer_consume_token (parser->lexer);
19018 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19019 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19021 /* cache_group will stop after an un-nested { } pair, too. */
19022 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19025 /* variadic mem-inits have ... after the ')'. */
19026 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19027 cp_lexer_consume_token (parser->lexer);
19030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19031 /* Handle function try blocks. */
19032 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19033 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19034 last = parser->lexer->next_token;
19036 /* Save away the inline definition; we will process it when the
19037 class is complete. */
19038 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19039 DECL_PENDING_INLINE_P (fn) = 1;
19041 /* We need to know that this was defined in the class, so that
19042 friend templates are handled correctly. */
19043 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19045 /* Add FN to the queue of functions to be parsed later. */
19046 TREE_VALUE (parser->unparsed_functions_queues)
19047 = tree_cons (NULL_TREE, fn,
19048 TREE_VALUE (parser->unparsed_functions_queues));
19053 /* Parse a template-argument-list, as well as the trailing ">" (but
19054 not the opening ">"). See cp_parser_template_argument_list for the
19058 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19062 tree saved_qualifying_scope;
19063 tree saved_object_scope;
19064 bool saved_greater_than_is_operator_p;
19065 int saved_unevaluated_operand;
19066 int saved_inhibit_evaluation_warnings;
19070 When parsing a template-id, the first non-nested `>' is taken as
19071 the end of the template-argument-list rather than a greater-than
19073 saved_greater_than_is_operator_p
19074 = parser->greater_than_is_operator_p;
19075 parser->greater_than_is_operator_p = false;
19076 /* Parsing the argument list may modify SCOPE, so we save it
19078 saved_scope = parser->scope;
19079 saved_qualifying_scope = parser->qualifying_scope;
19080 saved_object_scope = parser->object_scope;
19081 /* We need to evaluate the template arguments, even though this
19082 template-id may be nested within a "sizeof". */
19083 saved_unevaluated_operand = cp_unevaluated_operand;
19084 cp_unevaluated_operand = 0;
19085 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19086 c_inhibit_evaluation_warnings = 0;
19087 /* Parse the template-argument-list itself. */
19088 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19089 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19090 arguments = NULL_TREE;
19092 arguments = cp_parser_template_argument_list (parser);
19093 /* Look for the `>' that ends the template-argument-list. If we find
19094 a '>>' instead, it's probably just a typo. */
19095 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19097 if (cxx_dialect != cxx98)
19099 /* In C++0x, a `>>' in a template argument list or cast
19100 expression is considered to be two separate `>'
19101 tokens. So, change the current token to a `>', but don't
19102 consume it: it will be consumed later when the outer
19103 template argument list (or cast expression) is parsed.
19104 Note that this replacement of `>' for `>>' is necessary
19105 even if we are parsing tentatively: in the tentative
19106 case, after calling
19107 cp_parser_enclosed_template_argument_list we will always
19108 throw away all of the template arguments and the first
19109 closing `>', either because the template argument list
19110 was erroneous or because we are replacing those tokens
19111 with a CPP_TEMPLATE_ID token. The second `>' (which will
19112 not have been thrown away) is needed either to close an
19113 outer template argument list or to complete a new-style
19115 cp_token *token = cp_lexer_peek_token (parser->lexer);
19116 token->type = CPP_GREATER;
19118 else if (!saved_greater_than_is_operator_p)
19120 /* If we're in a nested template argument list, the '>>' has
19121 to be a typo for '> >'. We emit the error message, but we
19122 continue parsing and we push a '>' as next token, so that
19123 the argument list will be parsed correctly. Note that the
19124 global source location is still on the token before the
19125 '>>', so we need to say explicitly where we want it. */
19126 cp_token *token = cp_lexer_peek_token (parser->lexer);
19127 error_at (token->location, "%<>>%> should be %<> >%> "
19128 "within a nested template argument list");
19130 token->type = CPP_GREATER;
19134 /* If this is not a nested template argument list, the '>>'
19135 is a typo for '>'. Emit an error message and continue.
19136 Same deal about the token location, but here we can get it
19137 right by consuming the '>>' before issuing the diagnostic. */
19138 cp_token *token = cp_lexer_consume_token (parser->lexer);
19139 error_at (token->location,
19140 "spurious %<>>%>, use %<>%> to terminate "
19141 "a template argument list");
19145 cp_parser_skip_to_end_of_template_parameter_list (parser);
19146 /* The `>' token might be a greater-than operator again now. */
19147 parser->greater_than_is_operator_p
19148 = saved_greater_than_is_operator_p;
19149 /* Restore the SAVED_SCOPE. */
19150 parser->scope = saved_scope;
19151 parser->qualifying_scope = saved_qualifying_scope;
19152 parser->object_scope = saved_object_scope;
19153 cp_unevaluated_operand = saved_unevaluated_operand;
19154 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19159 /* MEMBER_FUNCTION is a member function, or a friend. If default
19160 arguments, or the body of the function have not yet been parsed,
19164 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19166 /* If this member is a template, get the underlying
19168 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19169 member_function = DECL_TEMPLATE_RESULT (member_function);
19171 /* There should not be any class definitions in progress at this
19172 point; the bodies of members are only parsed outside of all class
19174 gcc_assert (parser->num_classes_being_defined == 0);
19175 /* While we're parsing the member functions we might encounter more
19176 classes. We want to handle them right away, but we don't want
19177 them getting mixed up with functions that are currently in the
19179 parser->unparsed_functions_queues
19180 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19182 /* Make sure that any template parameters are in scope. */
19183 maybe_begin_member_template_processing (member_function);
19185 /* If the body of the function has not yet been parsed, parse it
19187 if (DECL_PENDING_INLINE_P (member_function))
19189 tree function_scope;
19190 cp_token_cache *tokens;
19192 /* The function is no longer pending; we are processing it. */
19193 tokens = DECL_PENDING_INLINE_INFO (member_function);
19194 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19195 DECL_PENDING_INLINE_P (member_function) = 0;
19197 /* If this is a local class, enter the scope of the containing
19199 function_scope = current_function_decl;
19200 if (function_scope)
19201 push_function_context ();
19203 /* Push the body of the function onto the lexer stack. */
19204 cp_parser_push_lexer_for_tokens (parser, tokens);
19206 /* Let the front end know that we going to be defining this
19208 start_preparsed_function (member_function, NULL_TREE,
19209 SF_PRE_PARSED | SF_INCLASS_INLINE);
19211 /* Don't do access checking if it is a templated function. */
19212 if (processing_template_decl)
19213 push_deferring_access_checks (dk_no_check);
19215 /* Now, parse the body of the function. */
19216 cp_parser_function_definition_after_declarator (parser,
19217 /*inline_p=*/true);
19219 if (processing_template_decl)
19220 pop_deferring_access_checks ();
19222 /* Leave the scope of the containing function. */
19223 if (function_scope)
19224 pop_function_context ();
19225 cp_parser_pop_lexer (parser);
19228 /* Remove any template parameters from the symbol table. */
19229 maybe_end_member_template_processing ();
19231 /* Restore the queue. */
19232 parser->unparsed_functions_queues
19233 = TREE_CHAIN (parser->unparsed_functions_queues);
19236 /* If DECL contains any default args, remember it on the unparsed
19237 functions queue. */
19240 cp_parser_save_default_args (cp_parser* parser, tree decl)
19244 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19246 probe = TREE_CHAIN (probe))
19247 if (TREE_PURPOSE (probe))
19249 TREE_PURPOSE (parser->unparsed_functions_queues)
19250 = tree_cons (current_class_type, decl,
19251 TREE_PURPOSE (parser->unparsed_functions_queues));
19256 /* FN is a FUNCTION_DECL which may contains a parameter with an
19257 unparsed DEFAULT_ARG. Parse the default args now. This function
19258 assumes that the current scope is the scope in which the default
19259 argument should be processed. */
19262 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19264 bool saved_local_variables_forbidden_p;
19265 tree parm, parmdecl;
19267 /* While we're parsing the default args, we might (due to the
19268 statement expression extension) encounter more classes. We want
19269 to handle them right away, but we don't want them getting mixed
19270 up with default args that are currently in the queue. */
19271 parser->unparsed_functions_queues
19272 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19274 /* Local variable names (and the `this' keyword) may not appear
19275 in a default argument. */
19276 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19277 parser->local_variables_forbidden_p = true;
19279 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19280 parmdecl = DECL_ARGUMENTS (fn);
19281 parm && parm != void_list_node;
19282 parm = TREE_CHAIN (parm),
19283 parmdecl = TREE_CHAIN (parmdecl))
19285 cp_token_cache *tokens;
19286 tree default_arg = TREE_PURPOSE (parm);
19288 VEC(tree,gc) *insts;
19295 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19296 /* This can happen for a friend declaration for a function
19297 already declared with default arguments. */
19300 /* Push the saved tokens for the default argument onto the parser's
19302 tokens = DEFARG_TOKENS (default_arg);
19303 cp_parser_push_lexer_for_tokens (parser, tokens);
19305 start_lambda_scope (parmdecl);
19307 /* Parse the assignment-expression. */
19308 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19309 if (parsed_arg == error_mark_node)
19311 cp_parser_pop_lexer (parser);
19315 if (!processing_template_decl)
19316 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19318 TREE_PURPOSE (parm) = parsed_arg;
19320 /* Update any instantiations we've already created. */
19321 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19322 VEC_iterate (tree, insts, ix, copy); ix++)
19323 TREE_PURPOSE (copy) = parsed_arg;
19325 finish_lambda_scope ();
19327 /* If the token stream has not been completely used up, then
19328 there was extra junk after the end of the default
19330 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19331 cp_parser_error (parser, "expected %<,%>");
19333 /* Revert to the main lexer. */
19334 cp_parser_pop_lexer (parser);
19337 /* Make sure no default arg is missing. */
19338 check_default_args (fn);
19340 /* Restore the state of local_variables_forbidden_p. */
19341 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19343 /* Restore the queue. */
19344 parser->unparsed_functions_queues
19345 = TREE_CHAIN (parser->unparsed_functions_queues);
19348 /* Parse the operand of `sizeof' (or a similar operator). Returns
19349 either a TYPE or an expression, depending on the form of the
19350 input. The KEYWORD indicates which kind of expression we have
19354 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19356 tree expr = NULL_TREE;
19357 const char *saved_message;
19359 bool saved_integral_constant_expression_p;
19360 bool saved_non_integral_constant_expression_p;
19361 bool pack_expansion_p = false;
19363 /* Types cannot be defined in a `sizeof' expression. Save away the
19365 saved_message = parser->type_definition_forbidden_message;
19366 /* And create the new one. */
19367 tmp = concat ("types may not be defined in %<",
19368 IDENTIFIER_POINTER (ridpointers[keyword]),
19369 "%> expressions", NULL);
19370 parser->type_definition_forbidden_message = tmp;
19372 /* The restrictions on constant-expressions do not apply inside
19373 sizeof expressions. */
19374 saved_integral_constant_expression_p
19375 = parser->integral_constant_expression_p;
19376 saved_non_integral_constant_expression_p
19377 = parser->non_integral_constant_expression_p;
19378 parser->integral_constant_expression_p = false;
19380 /* If it's a `...', then we are computing the length of a parameter
19382 if (keyword == RID_SIZEOF
19383 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19385 /* Consume the `...'. */
19386 cp_lexer_consume_token (parser->lexer);
19387 maybe_warn_variadic_templates ();
19389 /* Note that this is an expansion. */
19390 pack_expansion_p = true;
19393 /* Do not actually evaluate the expression. */
19394 ++cp_unevaluated_operand;
19395 ++c_inhibit_evaluation_warnings;
19396 /* If it's a `(', then we might be looking at the type-id
19398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19401 bool saved_in_type_id_in_expr_p;
19403 /* We can't be sure yet whether we're looking at a type-id or an
19405 cp_parser_parse_tentatively (parser);
19406 /* Consume the `('. */
19407 cp_lexer_consume_token (parser->lexer);
19408 /* Parse the type-id. */
19409 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19410 parser->in_type_id_in_expr_p = true;
19411 type = cp_parser_type_id (parser);
19412 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19413 /* Now, look for the trailing `)'. */
19414 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19415 /* If all went well, then we're done. */
19416 if (cp_parser_parse_definitely (parser))
19418 cp_decl_specifier_seq decl_specs;
19420 /* Build a trivial decl-specifier-seq. */
19421 clear_decl_specs (&decl_specs);
19422 decl_specs.type = type;
19424 /* Call grokdeclarator to figure out what type this is. */
19425 expr = grokdeclarator (NULL,
19429 /*attrlist=*/NULL);
19433 /* If the type-id production did not work out, then we must be
19434 looking at the unary-expression production. */
19436 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19437 /*cast_p=*/false, NULL);
19439 if (pack_expansion_p)
19440 /* Build a pack expansion. */
19441 expr = make_pack_expansion (expr);
19443 /* Go back to evaluating expressions. */
19444 --cp_unevaluated_operand;
19445 --c_inhibit_evaluation_warnings;
19447 /* Free the message we created. */
19449 /* And restore the old one. */
19450 parser->type_definition_forbidden_message = saved_message;
19451 parser->integral_constant_expression_p
19452 = saved_integral_constant_expression_p;
19453 parser->non_integral_constant_expression_p
19454 = saved_non_integral_constant_expression_p;
19459 /* If the current declaration has no declarator, return true. */
19462 cp_parser_declares_only_class_p (cp_parser *parser)
19464 /* If the next token is a `;' or a `,' then there is no
19466 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19467 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19470 /* Update the DECL_SPECS to reflect the storage class indicated by
19474 cp_parser_set_storage_class (cp_parser *parser,
19475 cp_decl_specifier_seq *decl_specs,
19477 location_t location)
19479 cp_storage_class storage_class;
19481 if (parser->in_unbraced_linkage_specification_p)
19483 error_at (location, "invalid use of %qD in linkage specification",
19484 ridpointers[keyword]);
19487 else if (decl_specs->storage_class != sc_none)
19489 decl_specs->conflicting_specifiers_p = true;
19493 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19494 && decl_specs->specs[(int) ds_thread])
19496 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19497 decl_specs->specs[(int) ds_thread] = 0;
19503 storage_class = sc_auto;
19506 storage_class = sc_register;
19509 storage_class = sc_static;
19512 storage_class = sc_extern;
19515 storage_class = sc_mutable;
19518 gcc_unreachable ();
19520 decl_specs->storage_class = storage_class;
19522 /* A storage class specifier cannot be applied alongside a typedef
19523 specifier. If there is a typedef specifier present then set
19524 conflicting_specifiers_p which will trigger an error later
19525 on in grokdeclarator. */
19526 if (decl_specs->specs[(int)ds_typedef])
19527 decl_specs->conflicting_specifiers_p = true;
19530 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19531 is true, the type is a user-defined type; otherwise it is a
19532 built-in type specified by a keyword. */
19535 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19537 location_t location,
19538 bool user_defined_p)
19540 decl_specs->any_specifiers_p = true;
19542 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19543 (with, for example, in "typedef int wchar_t;") we remember that
19544 this is what happened. In system headers, we ignore these
19545 declarations so that G++ can work with system headers that are not
19547 if (decl_specs->specs[(int) ds_typedef]
19549 && (type_spec == boolean_type_node
19550 || type_spec == char16_type_node
19551 || type_spec == char32_type_node
19552 || type_spec == wchar_type_node)
19553 && (decl_specs->type
19554 || decl_specs->specs[(int) ds_long]
19555 || decl_specs->specs[(int) ds_short]
19556 || decl_specs->specs[(int) ds_unsigned]
19557 || decl_specs->specs[(int) ds_signed]))
19559 decl_specs->redefined_builtin_type = type_spec;
19560 if (!decl_specs->type)
19562 decl_specs->type = type_spec;
19563 decl_specs->user_defined_type_p = false;
19564 decl_specs->type_location = location;
19567 else if (decl_specs->type)
19568 decl_specs->multiple_types_p = true;
19571 decl_specs->type = type_spec;
19572 decl_specs->user_defined_type_p = user_defined_p;
19573 decl_specs->redefined_builtin_type = NULL_TREE;
19574 decl_specs->type_location = location;
19578 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19579 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19582 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19584 return decl_specifiers->specs[(int) ds_friend] != 0;
19587 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19588 issue an error message indicating that TOKEN_DESC was expected.
19590 Returns the token consumed, if the token had the appropriate type.
19591 Otherwise, returns NULL. */
19594 cp_parser_require (cp_parser* parser,
19595 enum cpp_ttype type,
19596 const char* token_desc)
19598 if (cp_lexer_next_token_is (parser->lexer, type))
19599 return cp_lexer_consume_token (parser->lexer);
19602 /* Output the MESSAGE -- unless we're parsing tentatively. */
19603 if (!cp_parser_simulate_error (parser))
19605 char *message = concat ("expected ", token_desc, NULL);
19606 cp_parser_error (parser, message);
19613 /* An error message is produced if the next token is not '>'.
19614 All further tokens are skipped until the desired token is
19615 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19618 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19620 /* Current level of '< ... >'. */
19621 unsigned level = 0;
19622 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19623 unsigned nesting_depth = 0;
19625 /* Are we ready, yet? If not, issue error message. */
19626 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19629 /* Skip tokens until the desired token is found. */
19632 /* Peek at the next token. */
19633 switch (cp_lexer_peek_token (parser->lexer)->type)
19636 if (!nesting_depth)
19641 if (cxx_dialect == cxx98)
19642 /* C++0x views the `>>' operator as two `>' tokens, but
19645 else if (!nesting_depth && level-- == 0)
19647 /* We've hit a `>>' where the first `>' closes the
19648 template argument list, and the second `>' is
19649 spurious. Just consume the `>>' and stop; we've
19650 already produced at least one error. */
19651 cp_lexer_consume_token (parser->lexer);
19654 /* Fall through for C++0x, so we handle the second `>' in
19658 if (!nesting_depth && level-- == 0)
19660 /* We've reached the token we want, consume it and stop. */
19661 cp_lexer_consume_token (parser->lexer);
19666 case CPP_OPEN_PAREN:
19667 case CPP_OPEN_SQUARE:
19671 case CPP_CLOSE_PAREN:
19672 case CPP_CLOSE_SQUARE:
19673 if (nesting_depth-- == 0)
19678 case CPP_PRAGMA_EOL:
19679 case CPP_SEMICOLON:
19680 case CPP_OPEN_BRACE:
19681 case CPP_CLOSE_BRACE:
19682 /* The '>' was probably forgotten, don't look further. */
19689 /* Consume this token. */
19690 cp_lexer_consume_token (parser->lexer);
19694 /* If the next token is the indicated keyword, consume it. Otherwise,
19695 issue an error message indicating that TOKEN_DESC was expected.
19697 Returns the token consumed, if the token had the appropriate type.
19698 Otherwise, returns NULL. */
19701 cp_parser_require_keyword (cp_parser* parser,
19703 const char* token_desc)
19705 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19707 if (token && token->keyword != keyword)
19709 dyn_string_t error_msg;
19711 /* Format the error message. */
19712 error_msg = dyn_string_new (0);
19713 dyn_string_append_cstr (error_msg, "expected ");
19714 dyn_string_append_cstr (error_msg, token_desc);
19715 cp_parser_error (parser, error_msg->s);
19716 dyn_string_delete (error_msg);
19723 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19724 function-definition. */
19727 cp_parser_token_starts_function_definition_p (cp_token* token)
19729 return (/* An ordinary function-body begins with an `{'. */
19730 token->type == CPP_OPEN_BRACE
19731 /* A ctor-initializer begins with a `:'. */
19732 || token->type == CPP_COLON
19733 /* A function-try-block begins with `try'. */
19734 || token->keyword == RID_TRY
19735 /* The named return value extension begins with `return'. */
19736 || token->keyword == RID_RETURN);
19739 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19743 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19747 token = cp_lexer_peek_token (parser->lexer);
19748 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19751 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19752 C++0x) ending a template-argument. */
19755 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19759 token = cp_lexer_peek_token (parser->lexer);
19760 return (token->type == CPP_COMMA
19761 || token->type == CPP_GREATER
19762 || token->type == CPP_ELLIPSIS
19763 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19766 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19767 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19770 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19775 token = cp_lexer_peek_nth_token (parser->lexer, n);
19776 if (token->type == CPP_LESS)
19778 /* Check for the sequence `<::' in the original code. It would be lexed as
19779 `[:', where `[' is a digraph, and there is no whitespace before
19781 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19784 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19785 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19791 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19792 or none_type otherwise. */
19794 static enum tag_types
19795 cp_parser_token_is_class_key (cp_token* token)
19797 switch (token->keyword)
19802 return record_type;
19811 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19814 cp_parser_check_class_key (enum tag_types class_key, tree type)
19816 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19817 permerror (input_location, "%qs tag used in naming %q#T",
19818 class_key == union_type ? "union"
19819 : class_key == record_type ? "struct" : "class",
19823 /* Issue an error message if DECL is redeclared with different
19824 access than its original declaration [class.access.spec/3].
19825 This applies to nested classes and nested class templates.
19829 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19831 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19834 if ((TREE_PRIVATE (decl)
19835 != (current_access_specifier == access_private_node))
19836 || (TREE_PROTECTED (decl)
19837 != (current_access_specifier == access_protected_node)))
19838 error_at (location, "%qD redeclared with different access", decl);
19841 /* Look for the `template' keyword, as a syntactic disambiguator.
19842 Return TRUE iff it is present, in which case it will be
19846 cp_parser_optional_template_keyword (cp_parser *parser)
19848 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19850 /* The `template' keyword can only be used within templates;
19851 outside templates the parser can always figure out what is a
19852 template and what is not. */
19853 if (!processing_template_decl)
19855 cp_token *token = cp_lexer_peek_token (parser->lexer);
19856 error_at (token->location,
19857 "%<template%> (as a disambiguator) is only allowed "
19858 "within templates");
19859 /* If this part of the token stream is rescanned, the same
19860 error message would be generated. So, we purge the token
19861 from the stream. */
19862 cp_lexer_purge_token (parser->lexer);
19867 /* Consume the `template' keyword. */
19868 cp_lexer_consume_token (parser->lexer);
19876 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19877 set PARSER->SCOPE, and perform other related actions. */
19880 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19883 struct tree_check *check_value;
19884 deferred_access_check *chk;
19885 VEC (deferred_access_check,gc) *checks;
19887 /* Get the stored value. */
19888 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19889 /* Perform any access checks that were deferred. */
19890 checks = check_value->checks;
19894 VEC_iterate (deferred_access_check, checks, i, chk) ;
19897 perform_or_defer_access_check (chk->binfo,
19902 /* Set the scope from the stored value. */
19903 parser->scope = check_value->value;
19904 parser->qualifying_scope = check_value->qualifying_scope;
19905 parser->object_scope = NULL_TREE;
19908 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19909 encounter the end of a block before what we were looking for. */
19912 cp_parser_cache_group (cp_parser *parser,
19913 enum cpp_ttype end,
19918 cp_token *token = cp_lexer_peek_token (parser->lexer);
19920 /* Abort a parenthesized expression if we encounter a semicolon. */
19921 if ((end == CPP_CLOSE_PAREN || depth == 0)
19922 && token->type == CPP_SEMICOLON)
19924 /* If we've reached the end of the file, stop. */
19925 if (token->type == CPP_EOF
19926 || (end != CPP_PRAGMA_EOL
19927 && token->type == CPP_PRAGMA_EOL))
19929 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19930 /* We've hit the end of an enclosing block, so there's been some
19931 kind of syntax error. */
19934 /* Consume the token. */
19935 cp_lexer_consume_token (parser->lexer);
19936 /* See if it starts a new group. */
19937 if (token->type == CPP_OPEN_BRACE)
19939 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19940 /* In theory this should probably check end == '}', but
19941 cp_parser_save_member_function_body needs it to exit
19942 after either '}' or ')' when called with ')'. */
19946 else if (token->type == CPP_OPEN_PAREN)
19948 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19949 if (depth == 0 && end == CPP_CLOSE_PAREN)
19952 else if (token->type == CPP_PRAGMA)
19953 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19954 else if (token->type == end)
19959 /* Begin parsing tentatively. We always save tokens while parsing
19960 tentatively so that if the tentative parsing fails we can restore the
19964 cp_parser_parse_tentatively (cp_parser* parser)
19966 /* Enter a new parsing context. */
19967 parser->context = cp_parser_context_new (parser->context);
19968 /* Begin saving tokens. */
19969 cp_lexer_save_tokens (parser->lexer);
19970 /* In order to avoid repetitive access control error messages,
19971 access checks are queued up until we are no longer parsing
19973 push_deferring_access_checks (dk_deferred);
19976 /* Commit to the currently active tentative parse. */
19979 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19981 cp_parser_context *context;
19984 /* Mark all of the levels as committed. */
19985 lexer = parser->lexer;
19986 for (context = parser->context; context->next; context = context->next)
19988 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19990 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19991 while (!cp_lexer_saving_tokens (lexer))
19992 lexer = lexer->next;
19993 cp_lexer_commit_tokens (lexer);
19997 /* Abort the currently active tentative parse. All consumed tokens
19998 will be rolled back, and no diagnostics will be issued. */
20001 cp_parser_abort_tentative_parse (cp_parser* parser)
20003 cp_parser_simulate_error (parser);
20004 /* Now, pretend that we want to see if the construct was
20005 successfully parsed. */
20006 cp_parser_parse_definitely (parser);
20009 /* Stop parsing tentatively. If a parse error has occurred, restore the
20010 token stream. Otherwise, commit to the tokens we have consumed.
20011 Returns true if no error occurred; false otherwise. */
20014 cp_parser_parse_definitely (cp_parser* parser)
20016 bool error_occurred;
20017 cp_parser_context *context;
20019 /* Remember whether or not an error occurred, since we are about to
20020 destroy that information. */
20021 error_occurred = cp_parser_error_occurred (parser);
20022 /* Remove the topmost context from the stack. */
20023 context = parser->context;
20024 parser->context = context->next;
20025 /* If no parse errors occurred, commit to the tentative parse. */
20026 if (!error_occurred)
20028 /* Commit to the tokens read tentatively, unless that was
20030 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20031 cp_lexer_commit_tokens (parser->lexer);
20033 pop_to_parent_deferring_access_checks ();
20035 /* Otherwise, if errors occurred, roll back our state so that things
20036 are just as they were before we began the tentative parse. */
20039 cp_lexer_rollback_tokens (parser->lexer);
20040 pop_deferring_access_checks ();
20042 /* Add the context to the front of the free list. */
20043 context->next = cp_parser_context_free_list;
20044 cp_parser_context_free_list = context;
20046 return !error_occurred;
20049 /* Returns true if we are parsing tentatively and are not committed to
20050 this tentative parse. */
20053 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20055 return (cp_parser_parsing_tentatively (parser)
20056 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20059 /* Returns nonzero iff an error has occurred during the most recent
20060 tentative parse. */
20063 cp_parser_error_occurred (cp_parser* parser)
20065 return (cp_parser_parsing_tentatively (parser)
20066 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20069 /* Returns nonzero if GNU extensions are allowed. */
20072 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20074 return parser->allow_gnu_extensions_p;
20077 /* Objective-C++ Productions */
20080 /* Parse an Objective-C expression, which feeds into a primary-expression
20084 objc-message-expression
20085 objc-string-literal
20086 objc-encode-expression
20087 objc-protocol-expression
20088 objc-selector-expression
20090 Returns a tree representation of the expression. */
20093 cp_parser_objc_expression (cp_parser* parser)
20095 /* Try to figure out what kind of declaration is present. */
20096 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20100 case CPP_OPEN_SQUARE:
20101 return cp_parser_objc_message_expression (parser);
20103 case CPP_OBJC_STRING:
20104 kwd = cp_lexer_consume_token (parser->lexer);
20105 return objc_build_string_object (kwd->u.value);
20108 switch (kwd->keyword)
20110 case RID_AT_ENCODE:
20111 return cp_parser_objc_encode_expression (parser);
20113 case RID_AT_PROTOCOL:
20114 return cp_parser_objc_protocol_expression (parser);
20116 case RID_AT_SELECTOR:
20117 return cp_parser_objc_selector_expression (parser);
20123 error_at (kwd->location,
20124 "misplaced %<@%D%> Objective-C++ construct",
20126 cp_parser_skip_to_end_of_block_or_statement (parser);
20129 return error_mark_node;
20132 /* Parse an Objective-C message expression.
20134 objc-message-expression:
20135 [ objc-message-receiver objc-message-args ]
20137 Returns a representation of an Objective-C message. */
20140 cp_parser_objc_message_expression (cp_parser* parser)
20142 tree receiver, messageargs;
20144 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20145 receiver = cp_parser_objc_message_receiver (parser);
20146 messageargs = cp_parser_objc_message_args (parser);
20147 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20149 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20152 /* Parse an objc-message-receiver.
20154 objc-message-receiver:
20156 simple-type-specifier
20158 Returns a representation of the type or expression. */
20161 cp_parser_objc_message_receiver (cp_parser* parser)
20165 /* An Objective-C message receiver may be either (1) a type
20166 or (2) an expression. */
20167 cp_parser_parse_tentatively (parser);
20168 rcv = cp_parser_expression (parser, false, NULL);
20170 if (cp_parser_parse_definitely (parser))
20173 rcv = cp_parser_simple_type_specifier (parser,
20174 /*decl_specs=*/NULL,
20175 CP_PARSER_FLAGS_NONE);
20177 return objc_get_class_reference (rcv);
20180 /* Parse the arguments and selectors comprising an Objective-C message.
20185 objc-selector-args , objc-comma-args
20187 objc-selector-args:
20188 objc-selector [opt] : assignment-expression
20189 objc-selector-args objc-selector [opt] : assignment-expression
20192 assignment-expression
20193 objc-comma-args , assignment-expression
20195 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20196 selector arguments and TREE_VALUE containing a list of comma
20200 cp_parser_objc_message_args (cp_parser* parser)
20202 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20203 bool maybe_unary_selector_p = true;
20204 cp_token *token = cp_lexer_peek_token (parser->lexer);
20206 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20208 tree selector = NULL_TREE, arg;
20210 if (token->type != CPP_COLON)
20211 selector = cp_parser_objc_selector (parser);
20213 /* Detect if we have a unary selector. */
20214 if (maybe_unary_selector_p
20215 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20216 return build_tree_list (selector, NULL_TREE);
20218 maybe_unary_selector_p = false;
20219 cp_parser_require (parser, CPP_COLON, "%<:%>");
20220 arg = cp_parser_assignment_expression (parser, false, NULL);
20223 = chainon (sel_args,
20224 build_tree_list (selector, arg));
20226 token = cp_lexer_peek_token (parser->lexer);
20229 /* Handle non-selector arguments, if any. */
20230 while (token->type == CPP_COMMA)
20234 cp_lexer_consume_token (parser->lexer);
20235 arg = cp_parser_assignment_expression (parser, false, NULL);
20238 = chainon (addl_args,
20239 build_tree_list (NULL_TREE, arg));
20241 token = cp_lexer_peek_token (parser->lexer);
20244 return build_tree_list (sel_args, addl_args);
20247 /* Parse an Objective-C encode expression.
20249 objc-encode-expression:
20250 @encode objc-typename
20252 Returns an encoded representation of the type argument. */
20255 cp_parser_objc_encode_expression (cp_parser* parser)
20260 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20261 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20262 token = cp_lexer_peek_token (parser->lexer);
20263 type = complete_type (cp_parser_type_id (parser));
20264 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20268 error_at (token->location,
20269 "%<@encode%> must specify a type as an argument");
20270 return error_mark_node;
20273 return objc_build_encode_expr (type);
20276 /* Parse an Objective-C @defs expression. */
20279 cp_parser_objc_defs_expression (cp_parser *parser)
20283 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20284 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20285 name = cp_parser_identifier (parser);
20286 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20288 return objc_get_class_ivars (name);
20291 /* Parse an Objective-C protocol expression.
20293 objc-protocol-expression:
20294 @protocol ( identifier )
20296 Returns a representation of the protocol expression. */
20299 cp_parser_objc_protocol_expression (cp_parser* parser)
20303 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20304 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20305 proto = cp_parser_identifier (parser);
20306 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20308 return objc_build_protocol_expr (proto);
20311 /* Parse an Objective-C selector expression.
20313 objc-selector-expression:
20314 @selector ( objc-method-signature )
20316 objc-method-signature:
20322 objc-selector-seq objc-selector :
20324 Returns a representation of the method selector. */
20327 cp_parser_objc_selector_expression (cp_parser* parser)
20329 tree sel_seq = NULL_TREE;
20330 bool maybe_unary_selector_p = true;
20332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20334 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20335 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20336 token = cp_lexer_peek_token (parser->lexer);
20338 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20339 || token->type == CPP_SCOPE)
20341 tree selector = NULL_TREE;
20343 if (token->type != CPP_COLON
20344 || token->type == CPP_SCOPE)
20345 selector = cp_parser_objc_selector (parser);
20347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20348 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20350 /* Detect if we have a unary selector. */
20351 if (maybe_unary_selector_p)
20353 sel_seq = selector;
20354 goto finish_selector;
20358 cp_parser_error (parser, "expected %<:%>");
20361 maybe_unary_selector_p = false;
20362 token = cp_lexer_consume_token (parser->lexer);
20364 if (token->type == CPP_SCOPE)
20367 = chainon (sel_seq,
20368 build_tree_list (selector, NULL_TREE));
20370 = chainon (sel_seq,
20371 build_tree_list (NULL_TREE, NULL_TREE));
20375 = chainon (sel_seq,
20376 build_tree_list (selector, NULL_TREE));
20378 token = cp_lexer_peek_token (parser->lexer);
20382 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20384 return objc_build_selector_expr (loc, sel_seq);
20387 /* Parse a list of identifiers.
20389 objc-identifier-list:
20391 objc-identifier-list , identifier
20393 Returns a TREE_LIST of identifier nodes. */
20396 cp_parser_objc_identifier_list (cp_parser* parser)
20398 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20399 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20401 while (sep->type == CPP_COMMA)
20403 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20404 list = chainon (list,
20405 build_tree_list (NULL_TREE,
20406 cp_parser_identifier (parser)));
20407 sep = cp_lexer_peek_token (parser->lexer);
20413 /* Parse an Objective-C alias declaration.
20415 objc-alias-declaration:
20416 @compatibility_alias identifier identifier ;
20418 This function registers the alias mapping with the Objective-C front end.
20419 It returns nothing. */
20422 cp_parser_objc_alias_declaration (cp_parser* parser)
20426 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20427 alias = cp_parser_identifier (parser);
20428 orig = cp_parser_identifier (parser);
20429 objc_declare_alias (alias, orig);
20430 cp_parser_consume_semicolon_at_end_of_statement (parser);
20433 /* Parse an Objective-C class forward-declaration.
20435 objc-class-declaration:
20436 @class objc-identifier-list ;
20438 The function registers the forward declarations with the Objective-C
20439 front end. It returns nothing. */
20442 cp_parser_objc_class_declaration (cp_parser* parser)
20444 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20445 objc_declare_class (cp_parser_objc_identifier_list (parser));
20446 cp_parser_consume_semicolon_at_end_of_statement (parser);
20449 /* Parse a list of Objective-C protocol references.
20451 objc-protocol-refs-opt:
20452 objc-protocol-refs [opt]
20454 objc-protocol-refs:
20455 < objc-identifier-list >
20457 Returns a TREE_LIST of identifiers, if any. */
20460 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20462 tree protorefs = NULL_TREE;
20464 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20466 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20467 protorefs = cp_parser_objc_identifier_list (parser);
20468 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20474 /* Parse a Objective-C visibility specification. */
20477 cp_parser_objc_visibility_spec (cp_parser* parser)
20479 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20481 switch (vis->keyword)
20483 case RID_AT_PRIVATE:
20484 objc_set_visibility (2);
20486 case RID_AT_PROTECTED:
20487 objc_set_visibility (0);
20489 case RID_AT_PUBLIC:
20490 objc_set_visibility (1);
20496 /* Eat '@private'/'@protected'/'@public'. */
20497 cp_lexer_consume_token (parser->lexer);
20500 /* Parse an Objective-C method type. */
20503 cp_parser_objc_method_type (cp_parser* parser)
20505 objc_set_method_type
20506 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20511 /* Parse an Objective-C protocol qualifier. */
20514 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20516 tree quals = NULL_TREE, node;
20517 cp_token *token = cp_lexer_peek_token (parser->lexer);
20519 node = token->u.value;
20521 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20522 && (node == ridpointers [(int) RID_IN]
20523 || node == ridpointers [(int) RID_OUT]
20524 || node == ridpointers [(int) RID_INOUT]
20525 || node == ridpointers [(int) RID_BYCOPY]
20526 || node == ridpointers [(int) RID_BYREF]
20527 || node == ridpointers [(int) RID_ONEWAY]))
20529 quals = tree_cons (NULL_TREE, node, quals);
20530 cp_lexer_consume_token (parser->lexer);
20531 token = cp_lexer_peek_token (parser->lexer);
20532 node = token->u.value;
20538 /* Parse an Objective-C typename. */
20541 cp_parser_objc_typename (cp_parser* parser)
20543 tree type_name = NULL_TREE;
20545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20547 tree proto_quals, cp_type = NULL_TREE;
20549 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20550 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20552 /* An ObjC type name may consist of just protocol qualifiers, in which
20553 case the type shall default to 'id'. */
20554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20555 cp_type = cp_parser_type_id (parser);
20557 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20558 type_name = build_tree_list (proto_quals, cp_type);
20564 /* Check to see if TYPE refers to an Objective-C selector name. */
20567 cp_parser_objc_selector_p (enum cpp_ttype type)
20569 return (type == CPP_NAME || type == CPP_KEYWORD
20570 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20571 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20572 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20573 || type == CPP_XOR || type == CPP_XOR_EQ);
20576 /* Parse an Objective-C selector. */
20579 cp_parser_objc_selector (cp_parser* parser)
20581 cp_token *token = cp_lexer_consume_token (parser->lexer);
20583 if (!cp_parser_objc_selector_p (token->type))
20585 error_at (token->location, "invalid Objective-C++ selector name");
20586 return error_mark_node;
20589 /* C++ operator names are allowed to appear in ObjC selectors. */
20590 switch (token->type)
20592 case CPP_AND_AND: return get_identifier ("and");
20593 case CPP_AND_EQ: return get_identifier ("and_eq");
20594 case CPP_AND: return get_identifier ("bitand");
20595 case CPP_OR: return get_identifier ("bitor");
20596 case CPP_COMPL: return get_identifier ("compl");
20597 case CPP_NOT: return get_identifier ("not");
20598 case CPP_NOT_EQ: return get_identifier ("not_eq");
20599 case CPP_OR_OR: return get_identifier ("or");
20600 case CPP_OR_EQ: return get_identifier ("or_eq");
20601 case CPP_XOR: return get_identifier ("xor");
20602 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20603 default: return token->u.value;
20607 /* Parse an Objective-C params list. */
20610 cp_parser_objc_method_keyword_params (cp_parser* parser)
20612 tree params = NULL_TREE;
20613 bool maybe_unary_selector_p = true;
20614 cp_token *token = cp_lexer_peek_token (parser->lexer);
20616 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20618 tree selector = NULL_TREE, type_name, identifier;
20620 if (token->type != CPP_COLON)
20621 selector = cp_parser_objc_selector (parser);
20623 /* Detect if we have a unary selector. */
20624 if (maybe_unary_selector_p
20625 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20628 maybe_unary_selector_p = false;
20629 cp_parser_require (parser, CPP_COLON, "%<:%>");
20630 type_name = cp_parser_objc_typename (parser);
20631 identifier = cp_parser_identifier (parser);
20635 objc_build_keyword_decl (selector,
20639 token = cp_lexer_peek_token (parser->lexer);
20645 /* Parse the non-keyword Objective-C params. */
20648 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20650 tree params = make_node (TREE_LIST);
20651 cp_token *token = cp_lexer_peek_token (parser->lexer);
20652 *ellipsisp = false; /* Initially, assume no ellipsis. */
20654 while (token->type == CPP_COMMA)
20656 cp_parameter_declarator *parmdecl;
20659 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20660 token = cp_lexer_peek_token (parser->lexer);
20662 if (token->type == CPP_ELLIPSIS)
20664 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20669 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20670 parm = grokdeclarator (parmdecl->declarator,
20671 &parmdecl->decl_specifiers,
20672 PARM, /*initialized=*/0,
20673 /*attrlist=*/NULL);
20675 chainon (params, build_tree_list (NULL_TREE, parm));
20676 token = cp_lexer_peek_token (parser->lexer);
20682 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20685 cp_parser_objc_interstitial_code (cp_parser* parser)
20687 cp_token *token = cp_lexer_peek_token (parser->lexer);
20689 /* If the next token is `extern' and the following token is a string
20690 literal, then we have a linkage specification. */
20691 if (token->keyword == RID_EXTERN
20692 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20693 cp_parser_linkage_specification (parser);
20694 /* Handle #pragma, if any. */
20695 else if (token->type == CPP_PRAGMA)
20696 cp_parser_pragma (parser, pragma_external);
20697 /* Allow stray semicolons. */
20698 else if (token->type == CPP_SEMICOLON)
20699 cp_lexer_consume_token (parser->lexer);
20700 /* Finally, try to parse a block-declaration, or a function-definition. */
20702 cp_parser_block_declaration (parser, /*statement_p=*/false);
20705 /* Parse a method signature. */
20708 cp_parser_objc_method_signature (cp_parser* parser)
20710 tree rettype, kwdparms, optparms;
20711 bool ellipsis = false;
20713 cp_parser_objc_method_type (parser);
20714 rettype = cp_parser_objc_typename (parser);
20715 kwdparms = cp_parser_objc_method_keyword_params (parser);
20716 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20718 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20721 /* Pars an Objective-C method prototype list. */
20724 cp_parser_objc_method_prototype_list (cp_parser* parser)
20726 cp_token *token = cp_lexer_peek_token (parser->lexer);
20728 while (token->keyword != RID_AT_END)
20730 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20732 objc_add_method_declaration
20733 (cp_parser_objc_method_signature (parser));
20734 cp_parser_consume_semicolon_at_end_of_statement (parser);
20737 /* Allow for interspersed non-ObjC++ code. */
20738 cp_parser_objc_interstitial_code (parser);
20740 token = cp_lexer_peek_token (parser->lexer);
20743 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20744 objc_finish_interface ();
20747 /* Parse an Objective-C method definition list. */
20750 cp_parser_objc_method_definition_list (cp_parser* parser)
20752 cp_token *token = cp_lexer_peek_token (parser->lexer);
20754 while (token->keyword != RID_AT_END)
20758 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20760 push_deferring_access_checks (dk_deferred);
20761 objc_start_method_definition
20762 (cp_parser_objc_method_signature (parser));
20764 /* For historical reasons, we accept an optional semicolon. */
20765 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20766 cp_lexer_consume_token (parser->lexer);
20768 perform_deferred_access_checks ();
20769 stop_deferring_access_checks ();
20770 meth = cp_parser_function_definition_after_declarator (parser,
20772 pop_deferring_access_checks ();
20773 objc_finish_method_definition (meth);
20776 /* Allow for interspersed non-ObjC++ code. */
20777 cp_parser_objc_interstitial_code (parser);
20779 token = cp_lexer_peek_token (parser->lexer);
20782 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20783 objc_finish_implementation ();
20786 /* Parse Objective-C ivars. */
20789 cp_parser_objc_class_ivars (cp_parser* parser)
20791 cp_token *token = cp_lexer_peek_token (parser->lexer);
20793 if (token->type != CPP_OPEN_BRACE)
20794 return; /* No ivars specified. */
20796 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20797 token = cp_lexer_peek_token (parser->lexer);
20799 while (token->type != CPP_CLOSE_BRACE)
20801 cp_decl_specifier_seq declspecs;
20802 int decl_class_or_enum_p;
20803 tree prefix_attributes;
20805 cp_parser_objc_visibility_spec (parser);
20807 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20810 cp_parser_decl_specifier_seq (parser,
20811 CP_PARSER_FLAGS_OPTIONAL,
20813 &decl_class_or_enum_p);
20814 prefix_attributes = declspecs.attributes;
20815 declspecs.attributes = NULL_TREE;
20817 /* Keep going until we hit the `;' at the end of the
20819 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20821 tree width = NULL_TREE, attributes, first_attribute, decl;
20822 cp_declarator *declarator = NULL;
20823 int ctor_dtor_or_conv_p;
20825 /* Check for a (possibly unnamed) bitfield declaration. */
20826 token = cp_lexer_peek_token (parser->lexer);
20827 if (token->type == CPP_COLON)
20830 if (token->type == CPP_NAME
20831 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20834 /* Get the name of the bitfield. */
20835 declarator = make_id_declarator (NULL_TREE,
20836 cp_parser_identifier (parser),
20840 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20841 /* Get the width of the bitfield. */
20843 = cp_parser_constant_expression (parser,
20844 /*allow_non_constant=*/false,
20849 /* Parse the declarator. */
20851 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20852 &ctor_dtor_or_conv_p,
20853 /*parenthesized_p=*/NULL,
20854 /*member_p=*/false);
20857 /* Look for attributes that apply to the ivar. */
20858 attributes = cp_parser_attributes_opt (parser);
20859 /* Remember which attributes are prefix attributes and
20861 first_attribute = attributes;
20862 /* Combine the attributes. */
20863 attributes = chainon (prefix_attributes, attributes);
20866 /* Create the bitfield declaration. */
20867 decl = grokbitfield (declarator, &declspecs,
20871 decl = grokfield (declarator, &declspecs,
20872 NULL_TREE, /*init_const_expr_p=*/false,
20873 NULL_TREE, attributes);
20875 /* Add the instance variable. */
20876 objc_add_instance_variable (decl);
20878 /* Reset PREFIX_ATTRIBUTES. */
20879 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20880 attributes = TREE_CHAIN (attributes);
20882 TREE_CHAIN (attributes) = NULL_TREE;
20884 token = cp_lexer_peek_token (parser->lexer);
20886 if (token->type == CPP_COMMA)
20888 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20894 cp_parser_consume_semicolon_at_end_of_statement (parser);
20895 token = cp_lexer_peek_token (parser->lexer);
20898 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20899 /* For historical reasons, we accept an optional semicolon. */
20900 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20901 cp_lexer_consume_token (parser->lexer);
20904 /* Parse an Objective-C protocol declaration. */
20907 cp_parser_objc_protocol_declaration (cp_parser* parser)
20909 tree proto, protorefs;
20912 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20915 tok = cp_lexer_peek_token (parser->lexer);
20916 error_at (tok->location, "identifier expected after %<@protocol%>");
20920 /* See if we have a forward declaration or a definition. */
20921 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20923 /* Try a forward declaration first. */
20924 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20926 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20928 cp_parser_consume_semicolon_at_end_of_statement (parser);
20931 /* Ok, we got a full-fledged definition (or at least should). */
20934 proto = cp_parser_identifier (parser);
20935 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20936 objc_start_protocol (proto, protorefs);
20937 cp_parser_objc_method_prototype_list (parser);
20941 /* Parse an Objective-C superclass or category. */
20944 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20947 cp_token *next = cp_lexer_peek_token (parser->lexer);
20949 *super = *categ = NULL_TREE;
20950 if (next->type == CPP_COLON)
20952 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20953 *super = cp_parser_identifier (parser);
20955 else if (next->type == CPP_OPEN_PAREN)
20957 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20958 *categ = cp_parser_identifier (parser);
20959 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20963 /* Parse an Objective-C class interface. */
20966 cp_parser_objc_class_interface (cp_parser* parser)
20968 tree name, super, categ, protos;
20970 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20971 name = cp_parser_identifier (parser);
20972 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20973 protos = cp_parser_objc_protocol_refs_opt (parser);
20975 /* We have either a class or a category on our hands. */
20977 objc_start_category_interface (name, categ, protos);
20980 objc_start_class_interface (name, super, protos);
20981 /* Handle instance variable declarations, if any. */
20982 cp_parser_objc_class_ivars (parser);
20983 objc_continue_interface ();
20986 cp_parser_objc_method_prototype_list (parser);
20989 /* Parse an Objective-C class implementation. */
20992 cp_parser_objc_class_implementation (cp_parser* parser)
20994 tree name, super, categ;
20996 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20997 name = cp_parser_identifier (parser);
20998 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21000 /* We have either a class or a category on our hands. */
21002 objc_start_category_implementation (name, categ);
21005 objc_start_class_implementation (name, super);
21006 /* Handle instance variable declarations, if any. */
21007 cp_parser_objc_class_ivars (parser);
21008 objc_continue_implementation ();
21011 cp_parser_objc_method_definition_list (parser);
21014 /* Consume the @end token and finish off the implementation. */
21017 cp_parser_objc_end_implementation (cp_parser* parser)
21019 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21020 objc_finish_implementation ();
21023 /* Parse an Objective-C declaration. */
21026 cp_parser_objc_declaration (cp_parser* parser)
21028 /* Try to figure out what kind of declaration is present. */
21029 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21031 switch (kwd->keyword)
21034 cp_parser_objc_alias_declaration (parser);
21037 cp_parser_objc_class_declaration (parser);
21039 case RID_AT_PROTOCOL:
21040 cp_parser_objc_protocol_declaration (parser);
21042 case RID_AT_INTERFACE:
21043 cp_parser_objc_class_interface (parser);
21045 case RID_AT_IMPLEMENTATION:
21046 cp_parser_objc_class_implementation (parser);
21049 cp_parser_objc_end_implementation (parser);
21052 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21054 cp_parser_skip_to_end_of_block_or_statement (parser);
21058 /* Parse an Objective-C try-catch-finally statement.
21060 objc-try-catch-finally-stmt:
21061 @try compound-statement objc-catch-clause-seq [opt]
21062 objc-finally-clause [opt]
21064 objc-catch-clause-seq:
21065 objc-catch-clause objc-catch-clause-seq [opt]
21068 @catch ( exception-declaration ) compound-statement
21070 objc-finally-clause
21071 @finally compound-statement
21073 Returns NULL_TREE. */
21076 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21077 location_t location;
21080 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21081 location = cp_lexer_peek_token (parser->lexer)->location;
21082 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21083 node, lest it get absorbed into the surrounding block. */
21084 stmt = push_stmt_list ();
21085 cp_parser_compound_statement (parser, NULL, false);
21086 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21088 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21090 cp_parameter_declarator *parmdecl;
21093 cp_lexer_consume_token (parser->lexer);
21094 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21095 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21096 parm = grokdeclarator (parmdecl->declarator,
21097 &parmdecl->decl_specifiers,
21098 PARM, /*initialized=*/0,
21099 /*attrlist=*/NULL);
21100 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21101 objc_begin_catch_clause (parm);
21102 cp_parser_compound_statement (parser, NULL, false);
21103 objc_finish_catch_clause ();
21106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21108 cp_lexer_consume_token (parser->lexer);
21109 location = cp_lexer_peek_token (parser->lexer)->location;
21110 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21111 node, lest it get absorbed into the surrounding block. */
21112 stmt = push_stmt_list ();
21113 cp_parser_compound_statement (parser, NULL, false);
21114 objc_build_finally_clause (location, pop_stmt_list (stmt));
21117 return objc_finish_try_stmt ();
21120 /* Parse an Objective-C synchronized statement.
21122 objc-synchronized-stmt:
21123 @synchronized ( expression ) compound-statement
21125 Returns NULL_TREE. */
21128 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21129 location_t location;
21132 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21134 location = cp_lexer_peek_token (parser->lexer)->location;
21135 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21136 lock = cp_parser_expression (parser, false, NULL);
21137 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21139 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21140 node, lest it get absorbed into the surrounding block. */
21141 stmt = push_stmt_list ();
21142 cp_parser_compound_statement (parser, NULL, false);
21144 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21147 /* Parse an Objective-C throw statement.
21150 @throw assignment-expression [opt] ;
21152 Returns a constructed '@throw' statement. */
21155 cp_parser_objc_throw_statement (cp_parser *parser) {
21156 tree expr = NULL_TREE;
21157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21159 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21162 expr = cp_parser_assignment_expression (parser, false, NULL);
21164 cp_parser_consume_semicolon_at_end_of_statement (parser);
21166 return objc_build_throw_stmt (loc, expr);
21169 /* Parse an Objective-C statement. */
21172 cp_parser_objc_statement (cp_parser * parser) {
21173 /* Try to figure out what kind of declaration is present. */
21174 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21176 switch (kwd->keyword)
21179 return cp_parser_objc_try_catch_finally_statement (parser);
21180 case RID_AT_SYNCHRONIZED:
21181 return cp_parser_objc_synchronized_statement (parser);
21183 return cp_parser_objc_throw_statement (parser);
21185 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21187 cp_parser_skip_to_end_of_block_or_statement (parser);
21190 return error_mark_node;
21193 /* OpenMP 2.5 parsing routines. */
21195 /* Returns name of the next clause.
21196 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21197 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21198 returned and the token is consumed. */
21200 static pragma_omp_clause
21201 cp_parser_omp_clause_name (cp_parser *parser)
21203 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21206 result = PRAGMA_OMP_CLAUSE_IF;
21207 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21208 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21209 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21210 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21211 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21213 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21214 const char *p = IDENTIFIER_POINTER (id);
21219 if (!strcmp ("collapse", p))
21220 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21221 else if (!strcmp ("copyin", p))
21222 result = PRAGMA_OMP_CLAUSE_COPYIN;
21223 else if (!strcmp ("copyprivate", p))
21224 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21227 if (!strcmp ("firstprivate", p))
21228 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21231 if (!strcmp ("lastprivate", p))
21232 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21235 if (!strcmp ("nowait", p))
21236 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21237 else if (!strcmp ("num_threads", p))
21238 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21241 if (!strcmp ("ordered", p))
21242 result = PRAGMA_OMP_CLAUSE_ORDERED;
21245 if (!strcmp ("reduction", p))
21246 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21249 if (!strcmp ("schedule", p))
21250 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21251 else if (!strcmp ("shared", p))
21252 result = PRAGMA_OMP_CLAUSE_SHARED;
21255 if (!strcmp ("untied", p))
21256 result = PRAGMA_OMP_CLAUSE_UNTIED;
21261 if (result != PRAGMA_OMP_CLAUSE_NONE)
21262 cp_lexer_consume_token (parser->lexer);
21267 /* Validate that a clause of the given type does not already exist. */
21270 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21271 const char *name, location_t location)
21275 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21276 if (OMP_CLAUSE_CODE (c) == code)
21278 error_at (location, "too many %qs clauses", name);
21286 variable-list , identifier
21288 In addition, we match a closing parenthesis. An opening parenthesis
21289 will have been consumed by the caller.
21291 If KIND is nonzero, create the appropriate node and install the decl
21292 in OMP_CLAUSE_DECL and add the node to the head of the list.
21294 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21295 return the list created. */
21298 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21306 token = cp_lexer_peek_token (parser->lexer);
21307 name = cp_parser_id_expression (parser, /*template_p=*/false,
21308 /*check_dependency_p=*/true,
21309 /*template_p=*/NULL,
21310 /*declarator_p=*/false,
21311 /*optional_p=*/false);
21312 if (name == error_mark_node)
21315 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21316 if (decl == error_mark_node)
21317 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21318 else if (kind != 0)
21320 tree u = build_omp_clause (token->location, kind);
21321 OMP_CLAUSE_DECL (u) = decl;
21322 OMP_CLAUSE_CHAIN (u) = list;
21326 list = tree_cons (decl, NULL_TREE, list);
21329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21331 cp_lexer_consume_token (parser->lexer);
21334 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21338 /* Try to resync to an unnested comma. Copied from
21339 cp_parser_parenthesized_expression_list. */
21341 ending = cp_parser_skip_to_closing_parenthesis (parser,
21342 /*recovering=*/true,
21344 /*consume_paren=*/true);
21352 /* Similarly, but expect leading and trailing parenthesis. This is a very
21353 common case for omp clauses. */
21356 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21358 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21359 return cp_parser_omp_var_list_no_open (parser, kind, list);
21364 collapse ( constant-expression ) */
21367 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21373 loc = cp_lexer_peek_token (parser->lexer)->location;
21374 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21377 num = cp_parser_constant_expression (parser, false, NULL);
21379 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21380 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21381 /*or_comma=*/false,
21382 /*consume_paren=*/true);
21384 if (num == error_mark_node)
21386 num = fold_non_dependent_expr (num);
21387 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21388 || !host_integerp (num, 0)
21389 || (n = tree_low_cst (num, 0)) <= 0
21392 error_at (loc, "collapse argument needs positive constant integer expression");
21396 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21397 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21398 OMP_CLAUSE_CHAIN (c) = list;
21399 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21405 default ( shared | none ) */
21408 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21410 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21413 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21417 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21418 const char *p = IDENTIFIER_POINTER (id);
21423 if (strcmp ("none", p) != 0)
21425 kind = OMP_CLAUSE_DEFAULT_NONE;
21429 if (strcmp ("shared", p) != 0)
21431 kind = OMP_CLAUSE_DEFAULT_SHARED;
21438 cp_lexer_consume_token (parser->lexer);
21443 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21448 /*or_comma=*/false,
21449 /*consume_paren=*/true);
21451 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21454 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21455 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21456 OMP_CLAUSE_CHAIN (c) = list;
21457 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21463 if ( expression ) */
21466 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21470 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21473 t = cp_parser_condition (parser);
21475 if (t == error_mark_node
21476 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21478 /*or_comma=*/false,
21479 /*consume_paren=*/true);
21481 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21483 c = build_omp_clause (location, OMP_CLAUSE_IF);
21484 OMP_CLAUSE_IF_EXPR (c) = t;
21485 OMP_CLAUSE_CHAIN (c) = list;
21494 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21495 tree list, location_t location)
21499 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21501 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21502 OMP_CLAUSE_CHAIN (c) = list;
21507 num_threads ( expression ) */
21510 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21511 location_t location)
21515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21518 t = cp_parser_expression (parser, false, NULL);
21520 if (t == error_mark_node
21521 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21523 /*or_comma=*/false,
21524 /*consume_paren=*/true);
21526 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21527 "num_threads", location);
21529 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21530 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21531 OMP_CLAUSE_CHAIN (c) = list;
21540 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21541 tree list, location_t location)
21545 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21546 "ordered", location);
21548 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21549 OMP_CLAUSE_CHAIN (c) = list;
21554 reduction ( reduction-operator : variable-list )
21556 reduction-operator:
21557 One of: + * - & ^ | && || */
21560 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21562 enum tree_code code;
21565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21568 switch (cp_lexer_peek_token (parser->lexer)->type)
21580 code = BIT_AND_EXPR;
21583 code = BIT_XOR_EXPR;
21586 code = BIT_IOR_EXPR;
21589 code = TRUTH_ANDIF_EXPR;
21592 code = TRUTH_ORIF_EXPR;
21595 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21596 "%<|%>, %<&&%>, or %<||%>");
21598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21599 /*or_comma=*/false,
21600 /*consume_paren=*/true);
21603 cp_lexer_consume_token (parser->lexer);
21605 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21608 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21609 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21610 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21616 schedule ( schedule-kind )
21617 schedule ( schedule-kind , expression )
21620 static | dynamic | guided | runtime | auto */
21623 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21627 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21630 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21634 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21635 const char *p = IDENTIFIER_POINTER (id);
21640 if (strcmp ("dynamic", p) != 0)
21642 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21646 if (strcmp ("guided", p) != 0)
21648 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21652 if (strcmp ("runtime", p) != 0)
21654 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21661 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21662 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21663 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21664 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21667 cp_lexer_consume_token (parser->lexer);
21669 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21672 cp_lexer_consume_token (parser->lexer);
21674 token = cp_lexer_peek_token (parser->lexer);
21675 t = cp_parser_assignment_expression (parser, false, NULL);
21677 if (t == error_mark_node)
21679 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21680 error_at (token->location, "schedule %<runtime%> does not take "
21681 "a %<chunk_size%> parameter");
21682 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21683 error_at (token->location, "schedule %<auto%> does not take "
21684 "a %<chunk_size%> parameter");
21686 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21688 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21691 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21694 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21695 OMP_CLAUSE_CHAIN (c) = list;
21699 cp_parser_error (parser, "invalid schedule kind");
21701 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21702 /*or_comma=*/false,
21703 /*consume_paren=*/true);
21711 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21712 tree list, location_t location)
21716 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21718 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21719 OMP_CLAUSE_CHAIN (c) = list;
21723 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21724 is a bitmask in MASK. Return the list of clauses found; the result
21725 of clause default goes in *pdefault. */
21728 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21729 const char *where, cp_token *pragma_tok)
21731 tree clauses = NULL;
21733 cp_token *token = NULL;
21735 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21737 pragma_omp_clause c_kind;
21738 const char *c_name;
21739 tree prev = clauses;
21741 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21742 cp_lexer_consume_token (parser->lexer);
21744 token = cp_lexer_peek_token (parser->lexer);
21745 c_kind = cp_parser_omp_clause_name (parser);
21750 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21751 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21753 c_name = "collapse";
21755 case PRAGMA_OMP_CLAUSE_COPYIN:
21756 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21759 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21760 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21762 c_name = "copyprivate";
21764 case PRAGMA_OMP_CLAUSE_DEFAULT:
21765 clauses = cp_parser_omp_clause_default (parser, clauses,
21767 c_name = "default";
21769 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21772 c_name = "firstprivate";
21774 case PRAGMA_OMP_CLAUSE_IF:
21775 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21778 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21779 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21781 c_name = "lastprivate";
21783 case PRAGMA_OMP_CLAUSE_NOWAIT:
21784 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21787 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21788 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21790 c_name = "num_threads";
21792 case PRAGMA_OMP_CLAUSE_ORDERED:
21793 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21795 c_name = "ordered";
21797 case PRAGMA_OMP_CLAUSE_PRIVATE:
21798 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21800 c_name = "private";
21802 case PRAGMA_OMP_CLAUSE_REDUCTION:
21803 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21804 c_name = "reduction";
21806 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21807 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21809 c_name = "schedule";
21811 case PRAGMA_OMP_CLAUSE_SHARED:
21812 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21816 case PRAGMA_OMP_CLAUSE_UNTIED:
21817 clauses = cp_parser_omp_clause_untied (parser, clauses,
21822 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21826 if (((mask >> c_kind) & 1) == 0)
21828 /* Remove the invalid clause(s) from the list to avoid
21829 confusing the rest of the compiler. */
21831 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21835 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21836 return finish_omp_clauses (clauses);
21843 In practice, we're also interested in adding the statement to an
21844 outer node. So it is convenient if we work around the fact that
21845 cp_parser_statement calls add_stmt. */
21848 cp_parser_begin_omp_structured_block (cp_parser *parser)
21850 unsigned save = parser->in_statement;
21852 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21853 This preserves the "not within loop or switch" style error messages
21854 for nonsense cases like
21860 if (parser->in_statement)
21861 parser->in_statement = IN_OMP_BLOCK;
21867 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21869 parser->in_statement = save;
21873 cp_parser_omp_structured_block (cp_parser *parser)
21875 tree stmt = begin_omp_structured_block ();
21876 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21878 cp_parser_statement (parser, NULL_TREE, false, NULL);
21880 cp_parser_end_omp_structured_block (parser, save);
21881 return finish_omp_structured_block (stmt);
21885 # pragma omp atomic new-line
21889 x binop= expr | x++ | ++x | x-- | --x
21891 +, *, -, /, &, ^, |, <<, >>
21893 where x is an lvalue expression with scalar type. */
21896 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21899 enum tree_code code;
21901 cp_parser_require_pragma_eol (parser, pragma_tok);
21903 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21904 /*cast_p=*/false, NULL);
21905 switch (TREE_CODE (lhs))
21910 case PREINCREMENT_EXPR:
21911 case POSTINCREMENT_EXPR:
21912 lhs = TREE_OPERAND (lhs, 0);
21914 rhs = integer_one_node;
21917 case PREDECREMENT_EXPR:
21918 case POSTDECREMENT_EXPR:
21919 lhs = TREE_OPERAND (lhs, 0);
21921 rhs = integer_one_node;
21925 switch (cp_lexer_peek_token (parser->lexer)->type)
21931 code = TRUNC_DIV_EXPR;
21939 case CPP_LSHIFT_EQ:
21940 code = LSHIFT_EXPR;
21942 case CPP_RSHIFT_EQ:
21943 code = RSHIFT_EXPR;
21946 code = BIT_AND_EXPR;
21949 code = BIT_IOR_EXPR;
21952 code = BIT_XOR_EXPR;
21955 cp_parser_error (parser,
21956 "invalid operator for %<#pragma omp atomic%>");
21959 cp_lexer_consume_token (parser->lexer);
21961 rhs = cp_parser_expression (parser, false, NULL);
21962 if (rhs == error_mark_node)
21966 finish_omp_atomic (code, lhs, rhs);
21967 cp_parser_consume_semicolon_at_end_of_statement (parser);
21971 cp_parser_skip_to_end_of_block_or_statement (parser);
21976 # pragma omp barrier new-line */
21979 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21981 cp_parser_require_pragma_eol (parser, pragma_tok);
21982 finish_omp_barrier ();
21986 # pragma omp critical [(name)] new-line
21987 structured-block */
21990 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21992 tree stmt, name = NULL;
21994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21996 cp_lexer_consume_token (parser->lexer);
21998 name = cp_parser_identifier (parser);
22000 if (name == error_mark_node
22001 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22002 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22003 /*or_comma=*/false,
22004 /*consume_paren=*/true);
22005 if (name == error_mark_node)
22008 cp_parser_require_pragma_eol (parser, pragma_tok);
22010 stmt = cp_parser_omp_structured_block (parser);
22011 return c_finish_omp_critical (input_location, stmt, name);
22015 # pragma omp flush flush-vars[opt] new-line
22018 ( variable-list ) */
22021 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22024 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22025 cp_parser_require_pragma_eol (parser, pragma_tok);
22027 finish_omp_flush ();
22030 /* Helper function, to parse omp for increment expression. */
22033 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22035 tree cond = cp_parser_binary_expression (parser, false, true,
22036 PREC_NOT_OPERATOR, NULL);
22039 if (cond == error_mark_node
22040 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22042 cp_parser_skip_to_end_of_statement (parser);
22043 return error_mark_node;
22046 switch (TREE_CODE (cond))
22054 return error_mark_node;
22057 /* If decl is an iterator, preserve LHS and RHS of the relational
22058 expr until finish_omp_for. */
22060 && (type_dependent_expression_p (decl)
22061 || CLASS_TYPE_P (TREE_TYPE (decl))))
22064 return build_x_binary_op (TREE_CODE (cond),
22065 TREE_OPERAND (cond, 0), ERROR_MARK,
22066 TREE_OPERAND (cond, 1), ERROR_MARK,
22067 &overloaded_p, tf_warning_or_error);
22070 /* Helper function, to parse omp for increment expression. */
22073 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22075 cp_token *token = cp_lexer_peek_token (parser->lexer);
22081 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22083 op = (token->type == CPP_PLUS_PLUS
22084 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22085 cp_lexer_consume_token (parser->lexer);
22086 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22088 return error_mark_node;
22089 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22092 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22094 return error_mark_node;
22096 token = cp_lexer_peek_token (parser->lexer);
22097 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22099 op = (token->type == CPP_PLUS_PLUS
22100 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22101 cp_lexer_consume_token (parser->lexer);
22102 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22105 op = cp_parser_assignment_operator_opt (parser);
22106 if (op == ERROR_MARK)
22107 return error_mark_node;
22109 if (op != NOP_EXPR)
22111 rhs = cp_parser_assignment_expression (parser, false, NULL);
22112 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22113 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22116 lhs = cp_parser_binary_expression (parser, false, false,
22117 PREC_ADDITIVE_EXPRESSION, NULL);
22118 token = cp_lexer_peek_token (parser->lexer);
22119 decl_first = lhs == decl;
22122 if (token->type != CPP_PLUS
22123 && token->type != CPP_MINUS)
22124 return error_mark_node;
22128 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22129 cp_lexer_consume_token (parser->lexer);
22130 rhs = cp_parser_binary_expression (parser, false, false,
22131 PREC_ADDITIVE_EXPRESSION, NULL);
22132 token = cp_lexer_peek_token (parser->lexer);
22133 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22135 if (lhs == NULL_TREE)
22137 if (op == PLUS_EXPR)
22140 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22143 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22144 NULL, tf_warning_or_error);
22147 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22151 if (rhs != decl || op == MINUS_EXPR)
22152 return error_mark_node;
22153 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22156 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22158 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22161 /* Parse the restricted form of the for statement allowed by OpenMP. */
22164 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22166 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22167 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22168 tree this_pre_body, cl;
22169 location_t loc_first;
22170 bool collapse_err = false;
22171 int i, collapse = 1, nbraces = 0;
22173 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22174 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22175 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22177 gcc_assert (collapse >= 1);
22179 declv = make_tree_vec (collapse);
22180 initv = make_tree_vec (collapse);
22181 condv = make_tree_vec (collapse);
22182 incrv = make_tree_vec (collapse);
22184 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22186 for (i = 0; i < collapse; i++)
22188 int bracecount = 0;
22189 bool add_private_clause = false;
22192 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22194 cp_parser_error (parser, "for statement expected");
22197 loc = cp_lexer_consume_token (parser->lexer)->location;
22199 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22202 init = decl = real_decl = NULL;
22203 this_pre_body = push_stmt_list ();
22204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22206 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22210 integer-type var = lb
22211 random-access-iterator-type var = lb
22212 pointer-type var = lb
22214 cp_decl_specifier_seq type_specifiers;
22216 /* First, try to parse as an initialized declaration. See
22217 cp_parser_condition, from whence the bulk of this is copied. */
22219 cp_parser_parse_tentatively (parser);
22220 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22221 /*is_trailing_return=*/false,
22223 if (cp_parser_parse_definitely (parser))
22225 /* If parsing a type specifier seq succeeded, then this
22226 MUST be a initialized declaration. */
22227 tree asm_specification, attributes;
22228 cp_declarator *declarator;
22230 declarator = cp_parser_declarator (parser,
22231 CP_PARSER_DECLARATOR_NAMED,
22232 /*ctor_dtor_or_conv_p=*/NULL,
22233 /*parenthesized_p=*/NULL,
22234 /*member_p=*/false);
22235 attributes = cp_parser_attributes_opt (parser);
22236 asm_specification = cp_parser_asm_specification_opt (parser);
22238 if (declarator == cp_error_declarator)
22239 cp_parser_skip_to_end_of_statement (parser);
22243 tree pushed_scope, auto_node;
22245 decl = start_decl (declarator, &type_specifiers,
22246 SD_INITIALIZED, attributes,
22247 /*prefix_attributes=*/NULL_TREE,
22250 auto_node = type_uses_auto (TREE_TYPE (decl));
22251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22253 if (cp_lexer_next_token_is (parser->lexer,
22255 error ("parenthesized initialization is not allowed in "
22256 "OpenMP %<for%> loop");
22258 /* Trigger an error. */
22259 cp_parser_require (parser, CPP_EQ, "%<=%>");
22261 init = error_mark_node;
22262 cp_parser_skip_to_end_of_statement (parser);
22264 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22265 || type_dependent_expression_p (decl)
22268 bool is_direct_init, is_non_constant_init;
22270 init = cp_parser_initializer (parser,
22272 &is_non_constant_init);
22274 if (auto_node && describable_type (init))
22277 = do_auto_deduction (TREE_TYPE (decl), init,
22280 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22281 && !type_dependent_expression_p (decl))
22285 cp_finish_decl (decl, init, !is_non_constant_init,
22287 LOOKUP_ONLYCONVERTING);
22288 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22291 = tree_cons (NULL, this_pre_body, for_block);
22295 init = pop_stmt_list (this_pre_body);
22296 this_pre_body = NULL_TREE;
22301 cp_lexer_consume_token (parser->lexer);
22302 init = cp_parser_assignment_expression (parser, false, NULL);
22305 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22306 init = error_mark_node;
22308 cp_finish_decl (decl, NULL_TREE,
22309 /*init_const_expr_p=*/false,
22311 LOOKUP_ONLYCONVERTING);
22315 pop_scope (pushed_scope);
22321 /* If parsing a type specifier sequence failed, then
22322 this MUST be a simple expression. */
22323 cp_parser_parse_tentatively (parser);
22324 decl = cp_parser_primary_expression (parser, false, false,
22326 if (!cp_parser_error_occurred (parser)
22329 && CLASS_TYPE_P (TREE_TYPE (decl)))
22333 cp_parser_parse_definitely (parser);
22334 cp_parser_require (parser, CPP_EQ, "%<=%>");
22335 rhs = cp_parser_assignment_expression (parser, false, NULL);
22336 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22338 tf_warning_or_error));
22339 add_private_clause = true;
22344 cp_parser_abort_tentative_parse (parser);
22345 init = cp_parser_expression (parser, false, NULL);
22348 if (TREE_CODE (init) == MODIFY_EXPR
22349 || TREE_CODE (init) == MODOP_EXPR)
22350 real_decl = TREE_OPERAND (init, 0);
22355 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22358 this_pre_body = pop_stmt_list (this_pre_body);
22362 pre_body = push_stmt_list ();
22364 add_stmt (this_pre_body);
22365 pre_body = pop_stmt_list (pre_body);
22368 pre_body = this_pre_body;
22373 if (par_clauses != NULL && real_decl != NULL_TREE)
22376 for (c = par_clauses; *c ; )
22377 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22378 && OMP_CLAUSE_DECL (*c) == real_decl)
22380 error_at (loc, "iteration variable %qD"
22381 " should not be firstprivate", real_decl);
22382 *c = OMP_CLAUSE_CHAIN (*c);
22384 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22385 && OMP_CLAUSE_DECL (*c) == real_decl)
22387 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22388 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22389 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22390 OMP_CLAUSE_DECL (l) = real_decl;
22391 OMP_CLAUSE_CHAIN (l) = clauses;
22392 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22394 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22395 CP_OMP_CLAUSE_INFO (*c) = NULL;
22396 add_private_clause = false;
22400 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22401 && OMP_CLAUSE_DECL (*c) == real_decl)
22402 add_private_clause = false;
22403 c = &OMP_CLAUSE_CHAIN (*c);
22407 if (add_private_clause)
22410 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22412 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22413 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22414 && OMP_CLAUSE_DECL (c) == decl)
22416 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22417 && OMP_CLAUSE_DECL (c) == decl)
22418 error_at (loc, "iteration variable %qD "
22419 "should not be firstprivate",
22421 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22422 && OMP_CLAUSE_DECL (c) == decl)
22423 error_at (loc, "iteration variable %qD should not be reduction",
22428 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22429 OMP_CLAUSE_DECL (c) = decl;
22430 c = finish_omp_clauses (c);
22433 OMP_CLAUSE_CHAIN (c) = clauses;
22440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22441 cond = cp_parser_omp_for_cond (parser, decl);
22442 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22447 /* If decl is an iterator, preserve the operator on decl
22448 until finish_omp_for. */
22450 && (type_dependent_expression_p (decl)
22451 || CLASS_TYPE_P (TREE_TYPE (decl))))
22452 incr = cp_parser_omp_for_incr (parser, decl);
22454 incr = cp_parser_expression (parser, false, NULL);
22457 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22459 /*or_comma=*/false,
22460 /*consume_paren=*/true);
22462 TREE_VEC_ELT (declv, i) = decl;
22463 TREE_VEC_ELT (initv, i) = init;
22464 TREE_VEC_ELT (condv, i) = cond;
22465 TREE_VEC_ELT (incrv, i) = incr;
22467 if (i == collapse - 1)
22470 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22471 in between the collapsed for loops to be still considered perfectly
22472 nested. Hopefully the final version clarifies this.
22473 For now handle (multiple) {'s and empty statements. */
22474 cp_parser_parse_tentatively (parser);
22477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22479 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22481 cp_lexer_consume_token (parser->lexer);
22484 else if (bracecount
22485 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22486 cp_lexer_consume_token (parser->lexer);
22489 loc = cp_lexer_peek_token (parser->lexer)->location;
22490 error_at (loc, "not enough collapsed for loops");
22491 collapse_err = true;
22492 cp_parser_abort_tentative_parse (parser);
22501 cp_parser_parse_definitely (parser);
22502 nbraces += bracecount;
22506 /* Note that we saved the original contents of this flag when we entered
22507 the structured block, and so we don't need to re-save it here. */
22508 parser->in_statement = IN_OMP_FOR;
22510 /* Note that the grammar doesn't call for a structured block here,
22511 though the loop as a whole is a structured block. */
22512 body = push_stmt_list ();
22513 cp_parser_statement (parser, NULL_TREE, false, NULL);
22514 body = pop_stmt_list (body);
22516 if (declv == NULL_TREE)
22519 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22520 pre_body, clauses);
22524 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22526 cp_lexer_consume_token (parser->lexer);
22529 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22530 cp_lexer_consume_token (parser->lexer);
22535 error_at (cp_lexer_peek_token (parser->lexer)->location,
22536 "collapsed loops not perfectly nested");
22538 collapse_err = true;
22539 cp_parser_statement_seq_opt (parser, NULL);
22540 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22547 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22548 for_block = TREE_CHAIN (for_block);
22555 #pragma omp for for-clause[optseq] new-line
22558 #define OMP_FOR_CLAUSE_MASK \
22559 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22560 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22561 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22562 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22563 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22564 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22565 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22566 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22569 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22571 tree clauses, sb, ret;
22574 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22575 "#pragma omp for", pragma_tok);
22577 sb = begin_omp_structured_block ();
22578 save = cp_parser_begin_omp_structured_block (parser);
22580 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22582 cp_parser_end_omp_structured_block (parser, save);
22583 add_stmt (finish_omp_structured_block (sb));
22589 # pragma omp master new-line
22590 structured-block */
22593 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22595 cp_parser_require_pragma_eol (parser, pragma_tok);
22596 return c_finish_omp_master (input_location,
22597 cp_parser_omp_structured_block (parser));
22601 # pragma omp ordered new-line
22602 structured-block */
22605 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22607 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22608 cp_parser_require_pragma_eol (parser, pragma_tok);
22609 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22615 { section-sequence }
22618 section-directive[opt] structured-block
22619 section-sequence section-directive structured-block */
22622 cp_parser_omp_sections_scope (cp_parser *parser)
22624 tree stmt, substmt;
22625 bool error_suppress = false;
22628 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22631 stmt = push_stmt_list ();
22633 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22637 substmt = begin_omp_structured_block ();
22638 save = cp_parser_begin_omp_structured_block (parser);
22642 cp_parser_statement (parser, NULL_TREE, false, NULL);
22644 tok = cp_lexer_peek_token (parser->lexer);
22645 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22647 if (tok->type == CPP_CLOSE_BRACE)
22649 if (tok->type == CPP_EOF)
22653 cp_parser_end_omp_structured_block (parser, save);
22654 substmt = finish_omp_structured_block (substmt);
22655 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22656 add_stmt (substmt);
22661 tok = cp_lexer_peek_token (parser->lexer);
22662 if (tok->type == CPP_CLOSE_BRACE)
22664 if (tok->type == CPP_EOF)
22667 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22669 cp_lexer_consume_token (parser->lexer);
22670 cp_parser_require_pragma_eol (parser, tok);
22671 error_suppress = false;
22673 else if (!error_suppress)
22675 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22676 error_suppress = true;
22679 substmt = cp_parser_omp_structured_block (parser);
22680 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22681 add_stmt (substmt);
22683 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22685 substmt = pop_stmt_list (stmt);
22687 stmt = make_node (OMP_SECTIONS);
22688 TREE_TYPE (stmt) = void_type_node;
22689 OMP_SECTIONS_BODY (stmt) = substmt;
22696 # pragma omp sections sections-clause[optseq] newline
22699 #define OMP_SECTIONS_CLAUSE_MASK \
22700 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22701 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22702 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22703 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22704 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22707 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22711 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22712 "#pragma omp sections", pragma_tok);
22714 ret = cp_parser_omp_sections_scope (parser);
22716 OMP_SECTIONS_CLAUSES (ret) = clauses;
22722 # pragma parallel parallel-clause new-line
22723 # pragma parallel for parallel-for-clause new-line
22724 # pragma parallel sections parallel-sections-clause new-line */
22726 #define OMP_PARALLEL_CLAUSE_MASK \
22727 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22728 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22729 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22730 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22731 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22732 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22733 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22734 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22737 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22739 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22740 const char *p_name = "#pragma omp parallel";
22741 tree stmt, clauses, par_clause, ws_clause, block;
22742 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22748 cp_lexer_consume_token (parser->lexer);
22749 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22750 p_name = "#pragma omp parallel for";
22751 mask |= OMP_FOR_CLAUSE_MASK;
22752 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22754 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22757 const char *p = IDENTIFIER_POINTER (id);
22758 if (strcmp (p, "sections") == 0)
22760 cp_lexer_consume_token (parser->lexer);
22761 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22762 p_name = "#pragma omp parallel sections";
22763 mask |= OMP_SECTIONS_CLAUSE_MASK;
22764 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22768 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22769 block = begin_omp_parallel ();
22770 save = cp_parser_begin_omp_structured_block (parser);
22774 case PRAGMA_OMP_PARALLEL:
22775 cp_parser_statement (parser, NULL_TREE, false, NULL);
22776 par_clause = clauses;
22779 case PRAGMA_OMP_PARALLEL_FOR:
22780 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22781 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22784 case PRAGMA_OMP_PARALLEL_SECTIONS:
22785 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22786 stmt = cp_parser_omp_sections_scope (parser);
22788 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22792 gcc_unreachable ();
22795 cp_parser_end_omp_structured_block (parser, save);
22796 stmt = finish_omp_parallel (par_clause, block);
22797 if (p_kind != PRAGMA_OMP_PARALLEL)
22798 OMP_PARALLEL_COMBINED (stmt) = 1;
22803 # pragma omp single single-clause[optseq] new-line
22804 structured-block */
22806 #define OMP_SINGLE_CLAUSE_MASK \
22807 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22808 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22809 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22810 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22813 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22815 tree stmt = make_node (OMP_SINGLE);
22816 TREE_TYPE (stmt) = void_type_node;
22818 OMP_SINGLE_CLAUSES (stmt)
22819 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22820 "#pragma omp single", pragma_tok);
22821 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22823 return add_stmt (stmt);
22827 # pragma omp task task-clause[optseq] new-line
22828 structured-block */
22830 #define OMP_TASK_CLAUSE_MASK \
22831 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22832 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22833 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22834 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22835 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22836 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22839 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22841 tree clauses, block;
22844 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22845 "#pragma omp task", pragma_tok);
22846 block = begin_omp_task ();
22847 save = cp_parser_begin_omp_structured_block (parser);
22848 cp_parser_statement (parser, NULL_TREE, false, NULL);
22849 cp_parser_end_omp_structured_block (parser, save);
22850 return finish_omp_task (clauses, block);
22854 # pragma omp taskwait new-line */
22857 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22859 cp_parser_require_pragma_eol (parser, pragma_tok);
22860 finish_omp_taskwait ();
22864 # pragma omp threadprivate (variable-list) */
22867 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22871 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22872 cp_parser_require_pragma_eol (parser, pragma_tok);
22874 finish_omp_threadprivate (vars);
22877 /* Main entry point to OpenMP statement pragmas. */
22880 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22884 switch (pragma_tok->pragma_kind)
22886 case PRAGMA_OMP_ATOMIC:
22887 cp_parser_omp_atomic (parser, pragma_tok);
22889 case PRAGMA_OMP_CRITICAL:
22890 stmt = cp_parser_omp_critical (parser, pragma_tok);
22892 case PRAGMA_OMP_FOR:
22893 stmt = cp_parser_omp_for (parser, pragma_tok);
22895 case PRAGMA_OMP_MASTER:
22896 stmt = cp_parser_omp_master (parser, pragma_tok);
22898 case PRAGMA_OMP_ORDERED:
22899 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22901 case PRAGMA_OMP_PARALLEL:
22902 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22904 case PRAGMA_OMP_SECTIONS:
22905 stmt = cp_parser_omp_sections (parser, pragma_tok);
22907 case PRAGMA_OMP_SINGLE:
22908 stmt = cp_parser_omp_single (parser, pragma_tok);
22910 case PRAGMA_OMP_TASK:
22911 stmt = cp_parser_omp_task (parser, pragma_tok);
22914 gcc_unreachable ();
22918 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22923 static GTY (()) cp_parser *the_parser;
22926 /* Special handling for the first token or line in the file. The first
22927 thing in the file might be #pragma GCC pch_preprocess, which loads a
22928 PCH file, which is a GC collection point. So we need to handle this
22929 first pragma without benefit of an existing lexer structure.
22931 Always returns one token to the caller in *FIRST_TOKEN. This is
22932 either the true first token of the file, or the first token after
22933 the initial pragma. */
22936 cp_parser_initial_pragma (cp_token *first_token)
22940 cp_lexer_get_preprocessor_token (NULL, first_token);
22941 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22944 cp_lexer_get_preprocessor_token (NULL, first_token);
22945 if (first_token->type == CPP_STRING)
22947 name = first_token->u.value;
22949 cp_lexer_get_preprocessor_token (NULL, first_token);
22950 if (first_token->type != CPP_PRAGMA_EOL)
22951 error_at (first_token->location,
22952 "junk at end of %<#pragma GCC pch_preprocess%>");
22955 error_at (first_token->location, "expected string literal");
22957 /* Skip to the end of the pragma. */
22958 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22959 cp_lexer_get_preprocessor_token (NULL, first_token);
22961 /* Now actually load the PCH file. */
22963 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22965 /* Read one more token to return to our caller. We have to do this
22966 after reading the PCH file in, since its pointers have to be
22968 cp_lexer_get_preprocessor_token (NULL, first_token);
22971 /* Normal parsing of a pragma token. Here we can (and must) use the
22975 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22977 cp_token *pragma_tok;
22980 pragma_tok = cp_lexer_consume_token (parser->lexer);
22981 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22982 parser->lexer->in_pragma = true;
22984 id = pragma_tok->pragma_kind;
22987 case PRAGMA_GCC_PCH_PREPROCESS:
22988 error_at (pragma_tok->location,
22989 "%<#pragma GCC pch_preprocess%> must be first");
22992 case PRAGMA_OMP_BARRIER:
22995 case pragma_compound:
22996 cp_parser_omp_barrier (parser, pragma_tok);
22999 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23000 "used in compound statements");
23007 case PRAGMA_OMP_FLUSH:
23010 case pragma_compound:
23011 cp_parser_omp_flush (parser, pragma_tok);
23014 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23015 "used in compound statements");
23022 case PRAGMA_OMP_TASKWAIT:
23025 case pragma_compound:
23026 cp_parser_omp_taskwait (parser, pragma_tok);
23029 error_at (pragma_tok->location,
23030 "%<#pragma omp taskwait%> may only be "
23031 "used in compound statements");
23038 case PRAGMA_OMP_THREADPRIVATE:
23039 cp_parser_omp_threadprivate (parser, pragma_tok);
23042 case PRAGMA_OMP_ATOMIC:
23043 case PRAGMA_OMP_CRITICAL:
23044 case PRAGMA_OMP_FOR:
23045 case PRAGMA_OMP_MASTER:
23046 case PRAGMA_OMP_ORDERED:
23047 case PRAGMA_OMP_PARALLEL:
23048 case PRAGMA_OMP_SECTIONS:
23049 case PRAGMA_OMP_SINGLE:
23050 case PRAGMA_OMP_TASK:
23051 if (context == pragma_external)
23053 cp_parser_omp_construct (parser, pragma_tok);
23056 case PRAGMA_OMP_SECTION:
23057 error_at (pragma_tok->location,
23058 "%<#pragma omp section%> may only be used in "
23059 "%<#pragma omp sections%> construct");
23063 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23064 c_invoke_pragma_handler (id);
23068 cp_parser_error (parser, "expected declaration specifiers");
23072 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23076 /* The interface the pragma parsers have to the lexer. */
23079 pragma_lex (tree *value)
23082 enum cpp_ttype ret;
23084 tok = cp_lexer_peek_token (the_parser->lexer);
23087 *value = tok->u.value;
23089 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23091 else if (ret == CPP_STRING)
23092 *value = cp_parser_string_literal (the_parser, false, false);
23095 cp_lexer_consume_token (the_parser->lexer);
23096 if (ret == CPP_KEYWORD)
23104 /* External interface. */
23106 /* Parse one entire translation unit. */
23109 c_parse_file (void)
23111 bool error_occurred;
23112 static bool already_called = false;
23114 if (already_called)
23116 sorry ("inter-module optimizations not implemented for C++");
23119 already_called = true;
23121 the_parser = cp_parser_new ();
23122 push_deferring_access_checks (flag_access_control
23123 ? dk_no_deferred : dk_no_check);
23124 error_occurred = cp_parser_translation_unit (the_parser);
23128 #include "gt-cp-parser.h"