2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
543 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
605 if (token == lexer->last_token)
611 if (token->type != CPP_PURGED)
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
678 if (tok == lexer->last_token)
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
796 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
804 /* Start emitting debugging information. */
807 cp_lexer_start_debugging (cp_lexer* lexer)
809 lexer->debugging_p = true;
812 /* Stop emitting debugging information. */
815 cp_lexer_stop_debugging (cp_lexer* lexer)
817 lexer->debugging_p = false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
827 cp_token_cache *cache = GGC_NEW (cp_token_cache);
828 cache->first = first;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator *make_call_declarator
852 (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854 (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856 (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858 (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860 (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862 (cp_cv_quals, tree, cp_declarator *);
864 /* An erroneous declarator. */
865 static cp_declarator *cp_error_declarator;
867 /* The obstack on which declarators and related data structures are
869 static struct obstack declarator_obstack;
871 /* Alloc BYTES from the declarator memory pool. */
874 alloc_declarator (size_t bytes)
876 return obstack_alloc (&declarator_obstack, bytes);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
885 cp_declarator *declarator;
887 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888 declarator->kind = kind;
889 declarator->attributes = NULL_TREE;
890 declarator->declarator = NULL;
891 declarator->parameter_pack_p = false;
896 /* Make a declarator for a generalized identifier. If
897 QUALIFYING_SCOPE is non-NULL, the identifier is
898 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904 special_function_kind sfk)
906 cp_declarator *declarator;
908 /* It is valid to write:
910 class C { void f(); };
914 The standard is not clear about whether `typedef const C D' is
915 legal; as of 2002-09-15 the committee is considering that
916 question. EDG 3.0 allows that syntax. Therefore, we do as
918 if (qualifying_scope && TYPE_P (qualifying_scope))
919 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925 declarator = make_declarator (cdk_id);
926 declarator->u.id.qualifying_scope = qualifying_scope;
927 declarator->u.id.unqualified_name = unqualified_name;
928 declarator->u.id.sfk = sfk;
933 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
934 of modifiers such as const or volatile to apply to the pointer
935 type, represented as identifiers. */
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_pointer);
943 declarator->declarator = target;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = NULL_TREE;
948 declarator->parameter_pack_p = target->parameter_pack_p;
949 target->parameter_pack_p = false;
952 declarator->parameter_pack_p = false;
957 /* Like make_pointer_declarator -- but for references. */
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 cp_declarator *declarator;
965 declarator = make_declarator (cdk_reference);
966 declarator->declarator = target;
967 declarator->u.reference.qualifiers = cv_qualifiers;
968 declarator->u.reference.rvalue_ref = rvalue_ref;
971 declarator->parameter_pack_p = target->parameter_pack_p;
972 target->parameter_pack_p = false;
975 declarator->parameter_pack_p = false;
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981 member of CLASS_TYPE. */
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985 cp_declarator *pointee)
987 cp_declarator *declarator;
989 declarator = make_declarator (cdk_ptrmem);
990 declarator->declarator = pointee;
991 declarator->u.pointer.qualifiers = cv_qualifiers;
992 declarator->u.pointer.class_type = class_type;
996 declarator->parameter_pack_p = pointee->parameter_pack_p;
997 pointee->parameter_pack_p = false;
1000 declarator->parameter_pack_p = false;
1005 /* Make a declarator for the function given by TARGET, with the
1006 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1007 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1008 indicates what exceptions can be thrown. */
1011 make_call_declarator (cp_declarator *target,
1013 cp_cv_quals cv_qualifiers,
1014 tree exception_specification,
1015 tree late_return_type)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1024 declarator->u.function.late_return_type = late_return_type;
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1031 declarator->parameter_pack_p = false;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1053 declarator->parameter_pack_p = false;
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1068 while (declarator && !found)
1070 switch ((int)declarator->kind)
1081 declarator = declarator->declarator;
1089 cp_parameter_declarator *no_parameters;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1099 cp_parameter_declarator *parameter;
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1107 clear_decl_specs (¶meter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 function_declarator_p (const cp_declarator *declarator)
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1128 declarator = declarator->declarator;
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, treat user-defined type-names
1199 as non-type identifiers. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201 /* When parsing a type-specifier, do not try to parse a class-specifier
1202 or enum-specifier. */
1203 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206 /* This type is used for parameters and variables which hold
1207 combinations of the above flags. */
1208 typedef int cp_parser_flags;
1210 /* The different kinds of declarators we want to parse. */
1212 typedef enum cp_parser_declarator_kind
1214 /* We want an abstract declarator. */
1215 CP_PARSER_DECLARATOR_ABSTRACT,
1216 /* We want a named declarator. */
1217 CP_PARSER_DECLARATOR_NAMED,
1218 /* We don't mind, but the name must be an unqualified-id. */
1219 CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1222 /* The precedence values used to parse binary expressions. The minimum value
1223 of PREC must be 1, because zero is reserved to quickly discriminate
1224 binary operators from other tokens. */
1229 PREC_LOGICAL_OR_EXPRESSION,
1230 PREC_LOGICAL_AND_EXPRESSION,
1231 PREC_INCLUSIVE_OR_EXPRESSION,
1232 PREC_EXCLUSIVE_OR_EXPRESSION,
1233 PREC_AND_EXPRESSION,
1234 PREC_EQUALITY_EXPRESSION,
1235 PREC_RELATIONAL_EXPRESSION,
1236 PREC_SHIFT_EXPRESSION,
1237 PREC_ADDITIVE_EXPRESSION,
1238 PREC_MULTIPLICATIVE_EXPRESSION,
1240 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244 precedence value. */
1246 typedef struct cp_parser_binary_operations_map_node
1248 /* The token type. */
1249 enum cpp_ttype token_type;
1250 /* The corresponding tree code. */
1251 enum tree_code tree_type;
1252 /* The precedence of this operator. */
1253 enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1256 /* The status of a tentative parse. */
1258 typedef enum cp_parser_status_kind
1260 /* No errors have occurred. */
1261 CP_PARSER_STATUS_KIND_NO_ERROR,
1262 /* An error has occurred. */
1263 CP_PARSER_STATUS_KIND_ERROR,
1264 /* We are committed to this tentative parse, whether or not an error
1266 CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1269 typedef struct cp_parser_expression_stack_entry
1271 /* Left hand side of the binary operation we are currently
1274 /* Original tree code for left hand side, if it was a binary
1275 expression itself (used for -Wparentheses). */
1276 enum tree_code lhs_type;
1277 /* Tree code for the binary operation we are parsing. */
1278 enum tree_code tree_type;
1279 /* Precedence of the binary operation we are parsing. */
1280 enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1283 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1284 entries because precedence levels on the stack are monotonically
1286 typedef struct cp_parser_expression_stack_entry
1287 cp_parser_expression_stack[NUM_PREC_VALUES];
1289 /* Context that is saved and restored when parsing tentatively. */
1290 typedef struct GTY (()) cp_parser_context {
1291 /* If this is a tentative parsing context, the status of the
1293 enum cp_parser_status_kind status;
1294 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1295 that are looked up in this context must be looked up both in the
1296 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297 the context of the containing expression. */
1300 /* The next parsing context in the stack. */
1301 struct cp_parser_context *next;
1302 } cp_parser_context;
1306 /* Constructors and destructors. */
1308 static cp_parser_context *cp_parser_context_new
1309 (cp_parser_context *);
1311 /* Class variables. */
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316 Transformed into an associative array (binops_by_token) by
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 /* The same as binops, but initialized by cp_parser_new so that
1353 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357 /* Constructors and destructors. */
1359 /* Construct a new context. The context below this one on the stack
1360 is given by NEXT. */
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1365 cp_parser_context *context;
1367 /* Allocate the storage. */
1368 if (cp_parser_context_free_list != NULL)
1370 /* Pull the first entry from the free list. */
1371 context = cp_parser_context_free_list;
1372 cp_parser_context_free_list = context->next;
1373 memset (context, 0, sizeof (*context));
1376 context = GGC_CNEW (cp_parser_context);
1378 /* No errors have occurred yet in this context. */
1379 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380 /* If this is not the bottommost context, copy information that we
1381 need from the previous context. */
1384 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385 expression, then we are parsing one in this context, too. */
1386 context->object_type = next->object_type;
1387 /* Thread the stack. */
1388 context->next = next;
1394 /* The cp_parser structure represents the C++ parser. */
1396 typedef struct GTY(()) cp_parser {
1397 /* The lexer from which we are obtaining tokens. */
1400 /* The scope in which names should be looked up. If NULL_TREE, then
1401 we look up names in the scope that is currently open in the
1402 source program. If non-NULL, this is either a TYPE or
1403 NAMESPACE_DECL for the scope in which we should look. It can
1404 also be ERROR_MARK, when we've parsed a bogus scope.
1406 This value is not cleared automatically after a name is looked
1407 up, so we must be careful to clear it before starting a new look
1408 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1409 will look up `Z' in the scope of `X', rather than the current
1410 scope.) Unfortunately, it is difficult to tell when name lookup
1411 is complete, because we sometimes peek at a token, look it up,
1412 and then decide not to consume it. */
1415 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416 last lookup took place. OBJECT_SCOPE is used if an expression
1417 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418 respectively. QUALIFYING_SCOPE is used for an expression of the
1419 form "X::Y"; it refers to X. */
1421 tree qualifying_scope;
1423 /* A stack of parsing contexts. All but the bottom entry on the
1424 stack will be tentative contexts.
1426 We parse tentatively in order to determine which construct is in
1427 use in some situations. For example, in order to determine
1428 whether a statement is an expression-statement or a
1429 declaration-statement we parse it tentatively as a
1430 declaration-statement. If that fails, we then reparse the same
1431 token stream as an expression-statement. */
1432 cp_parser_context *context;
1434 /* True if we are parsing GNU C++. If this flag is not set, then
1435 GNU extensions are not recognized. */
1436 bool allow_gnu_extensions_p;
1438 /* TRUE if the `>' token should be interpreted as the greater-than
1439 operator. FALSE if it is the end of a template-id or
1440 template-parameter-list. In C++0x mode, this flag also applies to
1441 `>>' tokens, which are viewed as two consecutive `>' tokens when
1442 this flag is FALSE. */
1443 bool greater_than_is_operator_p;
1445 /* TRUE if default arguments are allowed within a parameter list
1446 that starts at this point. FALSE if only a gnu extension makes
1447 them permissible. */
1448 bool default_arg_ok_p;
1450 /* TRUE if we are parsing an integral constant-expression. See
1451 [expr.const] for a precise definition. */
1452 bool integral_constant_expression_p;
1454 /* TRUE if we are parsing an integral constant-expression -- but a
1455 non-constant expression should be permitted as well. This flag
1456 is used when parsing an array bound so that GNU variable-length
1457 arrays are tolerated. */
1458 bool allow_non_integral_constant_expression_p;
1460 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461 been seen that makes the expression non-constant. */
1462 bool non_integral_constant_expression_p;
1464 /* TRUE if local variable names and `this' are forbidden in the
1466 bool local_variables_forbidden_p;
1468 /* TRUE if the declaration we are parsing is part of a
1469 linkage-specification of the form `extern string-literal
1471 bool in_unbraced_linkage_specification_p;
1473 /* TRUE if we are presently parsing a declarator, after the
1474 direct-declarator. */
1475 bool in_declarator_p;
1477 /* TRUE if we are presently parsing a template-argument-list. */
1478 bool in_template_argument_list_p;
1480 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481 to IN_OMP_BLOCK if parsing OpenMP structured block and
1482 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1483 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484 iteration-statement, OpenMP block or loop within that switch. */
1485 #define IN_SWITCH_STMT 1
1486 #define IN_ITERATION_STMT 2
1487 #define IN_OMP_BLOCK 4
1488 #define IN_OMP_FOR 8
1489 #define IN_IF_STMT 16
1490 unsigned char in_statement;
1492 /* TRUE if we are presently parsing the body of a switch statement.
1493 Note that this doesn't quite overlap with in_statement above.
1494 The difference relates to giving the right sets of error messages:
1495 "case not in switch" vs "break statement used with OpenMP...". */
1496 bool in_switch_statement_p;
1498 /* TRUE if we are parsing a type-id in an expression context. In
1499 such a situation, both "type (expr)" and "type (type)" are valid
1501 bool in_type_id_in_expr_p;
1503 /* TRUE if we are currently in a header file where declarations are
1504 implicitly extern "C". */
1505 bool implicit_extern_c;
1507 /* TRUE if strings in expressions should be translated to the execution
1509 bool translate_strings_p;
1511 /* TRUE if we are presently parsing the body of a function, but not
1513 bool in_function_body;
1515 /* If non-NULL, then we are parsing a construct where new type
1516 definitions are not permitted. The string stored here will be
1517 issued as an error message if a type is defined. */
1518 const char *type_definition_forbidden_message;
1520 /* A list of lists. The outer list is a stack, used for member
1521 functions of local classes. At each level there are two sub-list,
1522 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524 TREE_VALUE's. The functions are chained in reverse declaration
1527 The TREE_PURPOSE sublist contains those functions with default
1528 arguments that need post processing, and the TREE_VALUE sublist
1529 contains those functions with definitions that need post
1532 These lists can only be processed once the outermost class being
1533 defined is complete. */
1534 tree unparsed_functions_queues;
1536 /* The number of classes whose definitions are currently in
1538 unsigned num_classes_being_defined;
1540 /* The number of template parameter lists that apply directly to the
1541 current declaration. */
1542 unsigned num_template_parameter_lists;
1547 /* Constructors and destructors. */
1549 static cp_parser *cp_parser_new
1552 /* Routines to parse various constructs.
1554 Those that return `tree' will return the error_mark_node (rather
1555 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556 Sometimes, they will return an ordinary node if error-recovery was
1557 attempted, even though a parse error occurred. So, to check
1558 whether or not a parse error occurred, you should always use
1559 cp_parser_error_occurred. If the construct is optional (indicated
1560 either by an `_opt' in the name of the function that does the
1561 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562 the construct is not present. */
1564 /* Lexical conventions [gram.lex] */
1566 static tree cp_parser_identifier
1568 static tree cp_parser_string_literal
1569 (cp_parser *, bool, bool);
1571 /* Basic concepts [gram.basic] */
1573 static bool cp_parser_translation_unit
1576 /* Expressions [gram.expr] */
1578 static tree cp_parser_primary_expression
1579 (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581 (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589 (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591 (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593 (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597 (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599 (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601 (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1604 static tree cp_parser_new_expression
1606 static VEC(tree,gc) *cp_parser_new_placement
1608 static tree cp_parser_new_type_id
1609 (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1612 static cp_declarator *cp_parser_direct_new_declarator
1614 static VEC(tree,gc) *cp_parser_new_initializer
1616 static tree cp_parser_delete_expression
1618 static tree cp_parser_cast_expression
1619 (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623 (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625 (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1628 static tree cp_parser_expression
1629 (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631 (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1634 static tree cp_parser_lambda_expression
1636 static void cp_parser_lambda_introducer
1637 (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641 (cp_parser *, tree);
1643 /* Statements [gram.stmt.stmt] */
1645 static void cp_parser_statement
1646 (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1649 static tree cp_parser_expression_statement
1650 (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652 (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654 (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656 (cp_parser *, bool *);
1657 static tree cp_parser_condition
1659 static tree cp_parser_iteration_statement
1661 static void cp_parser_for_init_statement
1663 static tree cp_parser_jump_statement
1665 static void cp_parser_declaration_statement
1668 static tree cp_parser_implicitly_scoped_statement
1669 (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1673 /* Declarations [gram.dcl.dcl] */
1675 static void cp_parser_declaration_seq_opt
1677 static void cp_parser_declaration
1679 static void cp_parser_block_declaration
1680 (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1687 static tree cp_parser_function_specifier_opt
1688 (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692 static tree cp_parser_simple_type_specifier
1693 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1696 static tree cp_parser_nonclass_name
1697 (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699 (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1702 static void cp_parser_enumerator_list
1703 (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705 (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1708 static void cp_parser_namespace_definition
1710 static void cp_parser_namespace_body
1712 static tree cp_parser_qualified_namespace_specifier
1714 static void cp_parser_namespace_alias_definition
1716 static bool cp_parser_using_declaration
1717 (cp_parser *, bool);
1718 static void cp_parser_using_directive
1720 static void cp_parser_asm_definition
1722 static void cp_parser_linkage_specification
1724 static void cp_parser_static_assert
1725 (cp_parser *, bool);
1726 static tree cp_parser_decltype
1729 /* Declarators [gram.dcl.decl] */
1731 static tree cp_parser_init_declarator
1732 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738 (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741 static tree cp_parser_late_return_type_opt
1743 static tree cp_parser_declarator_id
1744 (cp_parser *, bool);
1745 static tree cp_parser_type_id
1747 static tree cp_parser_template_type_arg
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751 (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1756 static tree cp_parser_parameter_declaration_list
1757 (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759 (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument
1761 (cp_parser *, bool);
1762 static void cp_parser_function_body
1764 static tree cp_parser_initializer
1765 (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767 (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769 (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771 (cp_parser *, bool *);
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1776 /* Classes [gram.class] */
1778 static tree cp_parser_class_name
1779 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1782 static tree cp_parser_class_head
1783 (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1786 static void cp_parser_member_specification_opt
1788 static void cp_parser_member_declaration
1790 static tree cp_parser_pure_specifier
1792 static tree cp_parser_constant_initializer
1795 /* Derived classes [gram.class.derived] */
1797 static tree cp_parser_base_clause
1799 static tree cp_parser_base_specifier
1802 /* Special member functions [gram.special] */
1804 static tree cp_parser_conversion_function_id
1806 static tree cp_parser_conversion_type_id
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1810 static bool cp_parser_ctor_initializer_opt
1812 static void cp_parser_mem_initializer_list
1814 static tree cp_parser_mem_initializer
1816 static tree cp_parser_mem_initializer_id
1819 /* Overloading [gram.over] */
1821 static tree cp_parser_operator_function_id
1823 static tree cp_parser_operator
1826 /* Templates [gram.temp] */
1828 static void cp_parser_template_declaration
1829 (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1832 static tree cp_parser_template_parameter
1833 (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835 (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837 (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839 (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1842 static tree cp_parser_template_argument
1844 static void cp_parser_explicit_instantiation
1846 static void cp_parser_explicit_specialization
1849 /* Exception handling [gram.exception] */
1851 static tree cp_parser_try_block
1853 static bool cp_parser_function_try_block
1855 static void cp_parser_handler_seq
1857 static void cp_parser_handler
1859 static tree cp_parser_exception_declaration
1861 static tree cp_parser_throw_expression
1863 static tree cp_parser_exception_specification_opt
1865 static tree cp_parser_type_id_list
1868 /* GNU Extensions */
1870 static tree cp_parser_asm_specification_opt
1872 static tree cp_parser_asm_operand_list
1874 static tree cp_parser_asm_clobber_list
1876 static tree cp_parser_asm_label_list
1878 static tree cp_parser_attributes_opt
1880 static tree cp_parser_attribute_list
1882 static bool cp_parser_extension_opt
1883 (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889 (cp_parser *, enum pragma_context);
1891 /* Objective-C++ Productions */
1893 static tree cp_parser_objc_message_receiver
1895 static tree cp_parser_objc_message_args
1897 static tree cp_parser_objc_message_expression
1899 static tree cp_parser_objc_encode_expression
1901 static tree cp_parser_objc_defs_expression
1903 static tree cp_parser_objc_protocol_expression
1905 static tree cp_parser_objc_selector_expression
1907 static tree cp_parser_objc_expression
1909 static bool cp_parser_objc_selector_p
1911 static tree cp_parser_objc_selector
1913 static tree cp_parser_objc_protocol_refs_opt
1915 static void cp_parser_objc_declaration
1917 static tree cp_parser_objc_statement
1920 /* Utility Routines */
1922 static tree cp_parser_lookup_name
1923 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925 (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1928 static bool cp_parser_check_declarator_template_parameters
1929 (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931 (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1934 static tree cp_parser_global_scope_opt
1935 (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937 (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941 (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943 (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945 (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949 (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1954 static void cp_parser_save_default_args
1955 (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959 (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961 (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963 (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1966 static void cp_parser_set_storage_class
1967 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969 (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971 (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973 (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975 (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1978 static bool cp_parser_next_token_starts_class_definition_p
1980 static bool cp_parser_next_token_ends_template_argument_p
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983 (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1986 static void cp_parser_check_class_key
1987 (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989 (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1992 static void cp_parser_pre_parsed_nested_name_specifier
1994 static bool cp_parser_cache_group
1995 (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1998 static void cp_parser_commit_to_tentative_parse
2000 static void cp_parser_abort_tentative_parse
2002 static bool cp_parser_parse_definitely
2004 static inline bool cp_parser_parsing_tentatively
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2008 static void cp_parser_error
2009 (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011 (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2014 static bool cp_parser_check_type_definition
2016 static void cp_parser_check_for_definition_in_return_type
2017 (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019 (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021 (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023 (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026 static int cp_parser_skip_to_closing_parenthesis
2027 (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2032 static void cp_parser_skip_to_end_of_block_or_statement
2034 static bool cp_parser_skip_to_closing_brace
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2038 static void cp_parser_skip_to_pragma_eol
2039 (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2042 static bool cp_parser_allow_gnu_extensions_p
2044 static bool cp_parser_is_string_literal
2046 static bool cp_parser_is_keyword
2047 (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049 (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053 /* Returns nonzero if we are parsing tentatively. */
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2058 return parser->context->next != NULL;
2061 /* Returns nonzero if TOKEN is a string literal. */
2064 cp_parser_is_string_literal (cp_token* token)
2066 return (token->type == CPP_STRING ||
2067 token->type == CPP_STRING16 ||
2068 token->type == CPP_STRING32 ||
2069 token->type == CPP_WSTRING ||
2070 token->type == CPP_UTF8STRING);
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 return token->keyword == keyword;
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082 FILE:LINE: MESSAGE before TOKEN
2083 where TOKEN is the next token in the input stream. MESSAGE
2084 (specified by the caller) is usually of the form "expected
2088 cp_parser_error (cp_parser* parser, const char* message)
2090 if (!cp_parser_simulate_error (parser))
2092 cp_token *token = cp_lexer_peek_token (parser->lexer);
2093 /* This diagnostic makes more sense if it is tagged to the line
2094 of the token we just peeked at. */
2095 cp_lexer_set_source_position_from_token (token);
2097 if (token->type == CPP_PRAGMA)
2099 error_at (token->location,
2100 "%<#pragma%> is not allowed here");
2101 cp_parser_skip_to_pragma_eol (parser, token);
2105 c_parse_error (message,
2106 /* Because c_parser_error does not understand
2107 CPP_KEYWORD, keywords are treated like
2109 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110 token->u.value, token->flags);
2114 /* Issue an error about name-lookup failing. NAME is the
2115 IDENTIFIER_NODE DECL is the result of
2116 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2117 the thing that we hoped to find. */
2120 cp_parser_name_lookup_error (cp_parser* parser,
2123 const char* desired,
2124 location_t location)
2126 /* If name lookup completely failed, tell the user that NAME was not
2128 if (decl == error_mark_node)
2130 if (parser->scope && parser->scope != global_namespace)
2131 error_at (location, "%<%E::%E%> has not been declared",
2132 parser->scope, name);
2133 else if (parser->scope == global_namespace)
2134 error_at (location, "%<::%E%> has not been declared", name);
2135 else if (parser->object_scope
2136 && !CLASS_TYPE_P (parser->object_scope))
2137 error_at (location, "request for member %qE in non-class type %qT",
2138 name, parser->object_scope);
2139 else if (parser->object_scope)
2140 error_at (location, "%<%T::%E%> has not been declared",
2141 parser->object_scope, name);
2143 error_at (location, "%qE has not been declared", name);
2145 else if (parser->scope && parser->scope != global_namespace)
2146 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147 else if (parser->scope == global_namespace)
2148 error_at (location, "%<::%E%> %s", name, desired);
2150 error_at (location, "%qE %s", name, desired);
2153 /* If we are parsing tentatively, remember that an error has occurred
2154 during this tentative parse. Returns true if the error was
2155 simulated; false if a message should be issued by the caller. */
2158 cp_parser_simulate_error (cp_parser* parser)
2160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2168 /* Check for repeated decl-specifiers. */
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172 location_t location)
2176 for (ds = ds_first; ds != ds_last; ++ds)
2178 unsigned count = decl_specs->specs[ds];
2181 /* The "long" specifier is a special case because of "long long". */
2185 error_at (location, "%<long long long%> is too long for GCC");
2187 pedwarn_cxx98 (location, OPT_Wlong_long,
2188 "ISO C++ 1998 does not support %<long long%>");
2192 static const char *const decl_spec_names[] = {
2209 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2214 /* This function is called when a type is defined. If type
2215 definitions are forbidden at this point, an error message is
2219 cp_parser_check_type_definition (cp_parser* parser)
2221 /* If types are forbidden here, issue a message. */
2222 if (parser->type_definition_forbidden_message)
2224 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225 in the message need to be interpreted. */
2226 error (parser->type_definition_forbidden_message);
2232 /* This function is called when the DECLARATOR is processed. The TYPE
2233 was a type defined in the decl-specifiers. If it is invalid to
2234 define a type in the decl-specifiers for DECLARATOR, an error is
2235 issued. TYPE_LOCATION is the location of TYPE and is used
2236 for error reporting. */
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240 tree type, location_t type_location)
2242 /* [dcl.fct] forbids type definitions in return types.
2243 Unfortunately, it's not easy to know whether or not we are
2244 processing a return type until after the fact. */
2246 && (declarator->kind == cdk_pointer
2247 || declarator->kind == cdk_reference
2248 || declarator->kind == cdk_ptrmem))
2249 declarator = declarator->declarator;
2251 && declarator->kind == cdk_function)
2253 error_at (type_location,
2254 "new types may not be defined in a return type");
2255 inform (type_location,
2256 "(perhaps a semicolon is missing after the definition of %qT)",
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262 "<" in any valid C++ program. If the next token is indeed "<",
2263 issue a message warning the user about what appears to be an
2264 invalid attempt to form a template-id. LOCATION is the location
2265 of the type-specifier (TYPE) */
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269 tree type, location_t location)
2271 cp_token_position start = 0;
2273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276 error_at (location, "%qT is not a template", type);
2277 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278 error_at (location, "%qE is not a template", type);
2280 error_at (location, "invalid template-id");
2281 /* Remember the location of the invalid "<". */
2282 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283 start = cp_lexer_token_position (parser->lexer, true);
2284 /* Consume the "<". */
2285 cp_lexer_consume_token (parser->lexer);
2286 /* Parse the template arguments. */
2287 cp_parser_enclosed_template_argument_list (parser);
2288 /* Permanently remove the invalid template arguments so that
2289 this error message is not issued again. */
2291 cp_lexer_purge_tokens_after (parser->lexer, start);
2295 /* If parsing an integral constant-expression, issue an error message
2296 about the fact that THING appeared and return true. Otherwise,
2297 return false. In either case, set
2298 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2301 cp_parser_non_integral_constant_expression (cp_parser *parser,
2304 parser->non_integral_constant_expression_p = true;
2305 if (parser->integral_constant_expression_p)
2307 if (!parser->allow_non_integral_constant_expression_p)
2309 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310 in the message need to be interpreted. */
2311 char *message = concat (thing,
2312 " cannot appear in a constant-expression",
2322 /* Emit a diagnostic for an invalid type name. SCOPE is the
2323 qualifying scope (or NULL, if none) for ID. This function commits
2324 to the current active tentative parse, if any. (Otherwise, the
2325 problematic construct might be encountered again later, resulting
2326 in duplicate error messages.) LOCATION is the location of ID. */
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330 tree scope, tree id,
2331 location_t location)
2333 tree decl, old_scope;
2334 /* Try to lookup the identifier. */
2335 old_scope = parser->scope;
2336 parser->scope = scope;
2337 decl = cp_parser_lookup_name_simple (parser, id, location);
2338 parser->scope = old_scope;
2339 /* If the lookup found a template-name, it means that the user forgot
2340 to specify an argument list. Emit a useful error message. */
2341 if (TREE_CODE (decl) == TEMPLATE_DECL)
2343 "invalid use of template-name %qE without an argument list",
2345 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346 error_at (location, "invalid use of destructor %qD as a type", id);
2347 else if (TREE_CODE (decl) == TYPE_DECL)
2348 /* Something like 'unsigned A a;' */
2349 error_at (location, "invalid combination of multiple type-specifiers");
2350 else if (!parser->scope)
2352 /* Issue an error message. */
2353 error_at (location, "%qE does not name a type", id);
2354 /* If we're in a template class, it's possible that the user was
2355 referring to a type from a base class. For example:
2357 template <typename T> struct A { typedef T X; };
2358 template <typename T> struct B : public A<T> { X x; };
2360 The user should have said "typename A<T>::X". */
2361 if (processing_template_decl && current_class_type
2362 && TYPE_BINFO (current_class_type))
2366 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2370 tree base_type = BINFO_TYPE (b);
2371 if (CLASS_TYPE_P (base_type)
2372 && dependent_type_p (base_type))
2375 /* Go from a particular instantiation of the
2376 template (which will have an empty TYPE_FIELDs),
2377 to the main version. */
2378 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379 for (field = TYPE_FIELDS (base_type);
2381 field = TREE_CHAIN (field))
2382 if (TREE_CODE (field) == TYPE_DECL
2383 && DECL_NAME (field) == id)
2386 "(perhaps %<typename %T::%E%> was intended)",
2387 BINFO_TYPE (b), id);
2396 /* Here we diagnose qualified-ids where the scope is actually correct,
2397 but the identifier does not resolve to a valid type name. */
2398 else if (parser->scope != error_mark_node)
2400 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401 error_at (location, "%qE in namespace %qE does not name a type",
2403 else if (TYPE_P (parser->scope)
2404 && dependent_scope_p (parser->scope))
2405 error_at (location, "need %<typename%> before %<%T::%E%> to name "
2406 "a type in dependent scope %qT",
2407 parser->scope, id, parser->scope);
2408 else if (TYPE_P (parser->scope))
2409 error_at (location, "%qE in class %qT does not name a type",
2414 cp_parser_commit_to_tentative_parse (parser);
2417 /* Check for a common situation where a type-name should be present,
2418 but is not, and issue a sensible error message. Returns true if an
2419 invalid type-name was detected.
2421 The situation handled by this function are variable declarations of the
2422 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2423 Usually, `ID' should name a type, but if we got here it means that it
2424 does not. We try to emit the best possible error message depending on
2425 how exactly the id-expression looks like. */
2428 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2431 cp_token *token = cp_lexer_peek_token (parser->lexer);
2433 cp_parser_parse_tentatively (parser);
2434 id = cp_parser_id_expression (parser,
2435 /*template_keyword_p=*/false,
2436 /*check_dependency_p=*/true,
2437 /*template_p=*/NULL,
2438 /*declarator_p=*/true,
2439 /*optional_p=*/false);
2440 /* After the id-expression, there should be a plain identifier,
2441 otherwise this is not a simple variable declaration. */
2442 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2443 || TREE_CODE (id) == TYPE_DECL)
2445 cp_parser_abort_tentative_parse (parser);
2448 if (!cp_parser_parse_definitely (parser))
2451 /* Emit a diagnostic for the invalid type. */
2452 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2453 id, token->location);
2454 /* Skip to the end of the declaration; there's no point in
2455 trying to process it. */
2456 cp_parser_skip_to_end_of_block_or_statement (parser);
2460 /* Consume tokens up to, and including, the next non-nested closing `)'.
2461 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2462 are doing error recovery. Returns -1 if OR_COMMA is true and we
2463 found an unnested comma. */
2466 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2471 unsigned paren_depth = 0;
2472 unsigned brace_depth = 0;
2473 unsigned square_depth = 0;
2475 if (recovering && !or_comma
2476 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2481 cp_token * token = cp_lexer_peek_token (parser->lexer);
2483 switch (token->type)
2486 case CPP_PRAGMA_EOL:
2487 /* If we've run out of tokens, then there is no closing `)'. */
2490 /* This is good for lambda expression capture-lists. */
2491 case CPP_OPEN_SQUARE:
2494 case CPP_CLOSE_SQUARE:
2495 if (!square_depth--)
2500 /* This matches the processing in skip_to_end_of_statement. */
2505 case CPP_OPEN_BRACE:
2508 case CPP_CLOSE_BRACE:
2514 if (recovering && or_comma && !brace_depth && !paren_depth
2519 case CPP_OPEN_PAREN:
2524 case CPP_CLOSE_PAREN:
2525 if (!brace_depth && !paren_depth--)
2528 cp_lexer_consume_token (parser->lexer);
2537 /* Consume the token. */
2538 cp_lexer_consume_token (parser->lexer);
2542 /* Consume tokens until we reach the end of the current statement.
2543 Normally, that will be just before consuming a `;'. However, if a
2544 non-nested `}' comes first, then we stop before consuming that. */
2547 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2549 unsigned nesting_depth = 0;
2553 cp_token *token = cp_lexer_peek_token (parser->lexer);
2555 switch (token->type)
2558 case CPP_PRAGMA_EOL:
2559 /* If we've run out of tokens, stop. */
2563 /* If the next token is a `;', we have reached the end of the
2569 case CPP_CLOSE_BRACE:
2570 /* If this is a non-nested '}', stop before consuming it.
2571 That way, when confronted with something like:
2575 we stop before consuming the closing '}', even though we
2576 have not yet reached a `;'. */
2577 if (nesting_depth == 0)
2580 /* If it is the closing '}' for a block that we have
2581 scanned, stop -- but only after consuming the token.
2587 we will stop after the body of the erroneously declared
2588 function, but before consuming the following `typedef'
2590 if (--nesting_depth == 0)
2592 cp_lexer_consume_token (parser->lexer);
2596 case CPP_OPEN_BRACE:
2604 /* Consume the token. */
2605 cp_lexer_consume_token (parser->lexer);
2609 /* This function is called at the end of a statement or declaration.
2610 If the next token is a semicolon, it is consumed; otherwise, error
2611 recovery is attempted. */
2614 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2616 /* Look for the trailing `;'. */
2617 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2619 /* If there is additional (erroneous) input, skip to the end of
2621 cp_parser_skip_to_end_of_statement (parser);
2622 /* If the next token is now a `;', consume it. */
2623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2624 cp_lexer_consume_token (parser->lexer);
2628 /* Skip tokens until we have consumed an entire block, or until we
2629 have consumed a non-nested `;'. */
2632 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2634 int nesting_depth = 0;
2636 while (nesting_depth >= 0)
2638 cp_token *token = cp_lexer_peek_token (parser->lexer);
2640 switch (token->type)
2643 case CPP_PRAGMA_EOL:
2644 /* If we've run out of tokens, stop. */
2648 /* Stop if this is an unnested ';'. */
2653 case CPP_CLOSE_BRACE:
2654 /* Stop if this is an unnested '}', or closes the outermost
2657 if (nesting_depth < 0)
2663 case CPP_OPEN_BRACE:
2672 /* Consume the token. */
2673 cp_lexer_consume_token (parser->lexer);
2677 /* Skip tokens until a non-nested closing curly brace is the next
2678 token, or there are no more tokens. Return true in the first case,
2682 cp_parser_skip_to_closing_brace (cp_parser *parser)
2684 unsigned nesting_depth = 0;
2688 cp_token *token = cp_lexer_peek_token (parser->lexer);
2690 switch (token->type)
2693 case CPP_PRAGMA_EOL:
2694 /* If we've run out of tokens, stop. */
2697 case CPP_CLOSE_BRACE:
2698 /* If the next token is a non-nested `}', then we have reached
2699 the end of the current block. */
2700 if (nesting_depth-- == 0)
2704 case CPP_OPEN_BRACE:
2705 /* If it the next token is a `{', then we are entering a new
2706 block. Consume the entire block. */
2714 /* Consume the token. */
2715 cp_lexer_consume_token (parser->lexer);
2719 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2720 parameter is the PRAGMA token, allowing us to purge the entire pragma
2724 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2728 parser->lexer->in_pragma = false;
2731 token = cp_lexer_consume_token (parser->lexer);
2732 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2734 /* Ensure that the pragma is not parsed again. */
2735 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2738 /* Require pragma end of line, resyncing with it as necessary. The
2739 arguments are as for cp_parser_skip_to_pragma_eol. */
2742 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2744 parser->lexer->in_pragma = false;
2745 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2746 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2749 /* This is a simple wrapper around make_typename_type. When the id is
2750 an unresolved identifier node, we can provide a superior diagnostic
2751 using cp_parser_diagnose_invalid_type_name. */
2754 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2755 tree id, location_t id_location)
2758 if (TREE_CODE (id) == IDENTIFIER_NODE)
2760 result = make_typename_type (scope, id, typename_type,
2761 /*complain=*/tf_none);
2762 if (result == error_mark_node)
2763 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2766 return make_typename_type (scope, id, typename_type, tf_error);
2769 /* This is a wrapper around the
2770 make_{pointer,ptrmem,reference}_declarator functions that decides
2771 which one to call based on the CODE and CLASS_TYPE arguments. The
2772 CODE argument should be one of the values returned by
2773 cp_parser_ptr_operator. */
2774 static cp_declarator *
2775 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2776 cp_cv_quals cv_qualifiers,
2777 cp_declarator *target)
2779 if (code == ERROR_MARK)
2780 return cp_error_declarator;
2782 if (code == INDIRECT_REF)
2783 if (class_type == NULL_TREE)
2784 return make_pointer_declarator (cv_qualifiers, target);
2786 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2787 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2788 return make_reference_declarator (cv_qualifiers, target, false);
2789 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2790 return make_reference_declarator (cv_qualifiers, target, true);
2794 /* Create a new C++ parser. */
2797 cp_parser_new (void)
2803 /* cp_lexer_new_main is called before calling ggc_alloc because
2804 cp_lexer_new_main might load a PCH file. */
2805 lexer = cp_lexer_new_main ();
2807 /* Initialize the binops_by_token so that we can get the tree
2808 directly from the token. */
2809 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2810 binops_by_token[binops[i].token_type] = binops[i];
2812 parser = GGC_CNEW (cp_parser);
2813 parser->lexer = lexer;
2814 parser->context = cp_parser_context_new (NULL);
2816 /* For now, we always accept GNU extensions. */
2817 parser->allow_gnu_extensions_p = 1;
2819 /* The `>' token is a greater-than operator, not the end of a
2821 parser->greater_than_is_operator_p = true;
2823 parser->default_arg_ok_p = true;
2825 /* We are not parsing a constant-expression. */
2826 parser->integral_constant_expression_p = false;
2827 parser->allow_non_integral_constant_expression_p = false;
2828 parser->non_integral_constant_expression_p = false;
2830 /* Local variable names are not forbidden. */
2831 parser->local_variables_forbidden_p = false;
2833 /* We are not processing an `extern "C"' declaration. */
2834 parser->in_unbraced_linkage_specification_p = false;
2836 /* We are not processing a declarator. */
2837 parser->in_declarator_p = false;
2839 /* We are not processing a template-argument-list. */
2840 parser->in_template_argument_list_p = false;
2842 /* We are not in an iteration statement. */
2843 parser->in_statement = 0;
2845 /* We are not in a switch statement. */
2846 parser->in_switch_statement_p = false;
2848 /* We are not parsing a type-id inside an expression. */
2849 parser->in_type_id_in_expr_p = false;
2851 /* Declarations aren't implicitly extern "C". */
2852 parser->implicit_extern_c = false;
2854 /* String literals should be translated to the execution character set. */
2855 parser->translate_strings_p = true;
2857 /* We are not parsing a function body. */
2858 parser->in_function_body = false;
2860 /* The unparsed function queue is empty. */
2861 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2863 /* There are no classes being defined. */
2864 parser->num_classes_being_defined = 0;
2866 /* No template parameters apply. */
2867 parser->num_template_parameter_lists = 0;
2872 /* Create a cp_lexer structure which will emit the tokens in CACHE
2873 and push it onto the parser's lexer stack. This is used for delayed
2874 parsing of in-class method bodies and default arguments, and should
2875 not be confused with tentative parsing. */
2877 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2879 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2880 lexer->next = parser->lexer;
2881 parser->lexer = lexer;
2883 /* Move the current source position to that of the first token in the
2885 cp_lexer_set_source_position_from_token (lexer->next_token);
2888 /* Pop the top lexer off the parser stack. This is never used for the
2889 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2891 cp_parser_pop_lexer (cp_parser *parser)
2893 cp_lexer *lexer = parser->lexer;
2894 parser->lexer = lexer->next;
2895 cp_lexer_destroy (lexer);
2897 /* Put the current source position back where it was before this
2898 lexer was pushed. */
2899 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2902 /* Lexical conventions [gram.lex] */
2904 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2908 cp_parser_identifier (cp_parser* parser)
2912 /* Look for the identifier. */
2913 token = cp_parser_require (parser, CPP_NAME, "identifier");
2914 /* Return the value. */
2915 return token ? token->u.value : error_mark_node;
2918 /* Parse a sequence of adjacent string constants. Returns a
2919 TREE_STRING representing the combined, nul-terminated string
2920 constant. If TRANSLATE is true, translate the string to the
2921 execution character set. If WIDE_OK is true, a wide string is
2924 C++98 [lex.string] says that if a narrow string literal token is
2925 adjacent to a wide string literal token, the behavior is undefined.
2926 However, C99 6.4.5p4 says that this results in a wide string literal.
2927 We follow C99 here, for consistency with the C front end.
2929 This code is largely lifted from lex_string() in c-lex.c.
2931 FUTURE: ObjC++ will need to handle @-strings here. */
2933 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2937 struct obstack str_ob;
2938 cpp_string str, istr, *strs;
2940 enum cpp_ttype type;
2942 tok = cp_lexer_peek_token (parser->lexer);
2943 if (!cp_parser_is_string_literal (tok))
2945 cp_parser_error (parser, "expected string-literal");
2946 return error_mark_node;
2951 /* Try to avoid the overhead of creating and destroying an obstack
2952 for the common case of just one string. */
2953 if (!cp_parser_is_string_literal
2954 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2956 cp_lexer_consume_token (parser->lexer);
2958 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2959 str.len = TREE_STRING_LENGTH (tok->u.value);
2966 gcc_obstack_init (&str_ob);
2971 cp_lexer_consume_token (parser->lexer);
2973 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2974 str.len = TREE_STRING_LENGTH (tok->u.value);
2976 if (type != tok->type)
2978 if (type == CPP_STRING)
2980 else if (tok->type != CPP_STRING)
2981 error_at (tok->location,
2982 "unsupported non-standard concatenation "
2983 "of string literals");
2986 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2988 tok = cp_lexer_peek_token (parser->lexer);
2990 while (cp_parser_is_string_literal (tok));
2992 strs = (cpp_string *) obstack_finish (&str_ob);
2995 if (type != CPP_STRING && !wide_ok)
2997 cp_parser_error (parser, "a wide string is invalid in this context");
3001 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3002 (parse_in, strs, count, &istr, type))
3004 value = build_string (istr.len, (const char *)istr.text);
3005 free (CONST_CAST (unsigned char *, istr.text));
3011 case CPP_UTF8STRING:
3012 TREE_TYPE (value) = char_array_type_node;
3015 TREE_TYPE (value) = char16_array_type_node;
3018 TREE_TYPE (value) = char32_array_type_node;
3021 TREE_TYPE (value) = wchar_array_type_node;
3025 value = fix_string_type (value);
3028 /* cpp_interpret_string has issued an error. */
3029 value = error_mark_node;
3032 obstack_free (&str_ob, 0);
3038 /* Basic concepts [gram.basic] */
3040 /* Parse a translation-unit.
3043 declaration-seq [opt]
3045 Returns TRUE if all went well. */
3048 cp_parser_translation_unit (cp_parser* parser)
3050 /* The address of the first non-permanent object on the declarator
3052 static void *declarator_obstack_base;
3056 /* Create the declarator obstack, if necessary. */
3057 if (!cp_error_declarator)
3059 gcc_obstack_init (&declarator_obstack);
3060 /* Create the error declarator. */
3061 cp_error_declarator = make_declarator (cdk_error);
3062 /* Create the empty parameter list. */
3063 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3064 /* Remember where the base of the declarator obstack lies. */
3065 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3068 cp_parser_declaration_seq_opt (parser);
3070 /* If there are no tokens left then all went well. */
3071 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3073 /* Get rid of the token array; we don't need it any more. */
3074 cp_lexer_destroy (parser->lexer);
3075 parser->lexer = NULL;
3077 /* This file might have been a context that's implicitly extern
3078 "C". If so, pop the lang context. (Only relevant for PCH.) */
3079 if (parser->implicit_extern_c)
3081 pop_lang_context ();
3082 parser->implicit_extern_c = false;
3086 finish_translation_unit ();
3092 cp_parser_error (parser, "expected declaration");
3096 /* Make sure the declarator obstack was fully cleaned up. */
3097 gcc_assert (obstack_next_free (&declarator_obstack)
3098 == declarator_obstack_base);
3100 /* All went well. */
3104 /* Expressions [gram.expr] */
3106 /* Parse a primary-expression.
3117 ( compound-statement )
3118 __builtin_va_arg ( assignment-expression , type-id )
3119 __builtin_offsetof ( type-id , offsetof-expression )
3122 __has_nothrow_assign ( type-id )
3123 __has_nothrow_constructor ( type-id )
3124 __has_nothrow_copy ( type-id )
3125 __has_trivial_assign ( type-id )
3126 __has_trivial_constructor ( type-id )
3127 __has_trivial_copy ( type-id )
3128 __has_trivial_destructor ( type-id )
3129 __has_virtual_destructor ( type-id )
3130 __is_abstract ( type-id )
3131 __is_base_of ( type-id , type-id )
3132 __is_class ( type-id )
3133 __is_convertible_to ( type-id , type-id )
3134 __is_empty ( type-id )
3135 __is_enum ( type-id )
3136 __is_pod ( type-id )
3137 __is_polymorphic ( type-id )
3138 __is_union ( type-id )
3140 Objective-C++ Extension:
3148 ADDRESS_P is true iff this expression was immediately preceded by
3149 "&" and therefore might denote a pointer-to-member. CAST_P is true
3150 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3151 true iff this expression is a template argument.
3153 Returns a representation of the expression. Upon return, *IDK
3154 indicates what kind of id-expression (if any) was present. */
3157 cp_parser_primary_expression (cp_parser *parser,
3160 bool template_arg_p,
3163 cp_token *token = NULL;
3165 /* Assume the primary expression is not an id-expression. */
3166 *idk = CP_ID_KIND_NONE;
3168 /* Peek at the next token. */
3169 token = cp_lexer_peek_token (parser->lexer);
3170 switch (token->type)
3183 token = cp_lexer_consume_token (parser->lexer);
3184 if (TREE_CODE (token->u.value) == FIXED_CST)
3186 error_at (token->location,
3187 "fixed-point types not supported in C++");
3188 return error_mark_node;
3190 /* Floating-point literals are only allowed in an integral
3191 constant expression if they are cast to an integral or
3192 enumeration type. */
3193 if (TREE_CODE (token->u.value) == REAL_CST
3194 && parser->integral_constant_expression_p
3197 /* CAST_P will be set even in invalid code like "int(2.7 +
3198 ...)". Therefore, we have to check that the next token
3199 is sure to end the cast. */
3202 cp_token *next_token;
3204 next_token = cp_lexer_peek_token (parser->lexer);
3205 if (/* The comma at the end of an
3206 enumerator-definition. */
3207 next_token->type != CPP_COMMA
3208 /* The curly brace at the end of an enum-specifier. */
3209 && next_token->type != CPP_CLOSE_BRACE
3210 /* The end of a statement. */
3211 && next_token->type != CPP_SEMICOLON
3212 /* The end of the cast-expression. */
3213 && next_token->type != CPP_CLOSE_PAREN
3214 /* The end of an array bound. */
3215 && next_token->type != CPP_CLOSE_SQUARE
3216 /* The closing ">" in a template-argument-list. */
3217 && (next_token->type != CPP_GREATER
3218 || parser->greater_than_is_operator_p)
3219 /* C++0x only: A ">>" treated like two ">" tokens,
3220 in a template-argument-list. */
3221 && (next_token->type != CPP_RSHIFT
3222 || (cxx_dialect == cxx98)
3223 || parser->greater_than_is_operator_p))
3227 /* If we are within a cast, then the constraint that the
3228 cast is to an integral or enumeration type will be
3229 checked at that point. If we are not within a cast, then
3230 this code is invalid. */
3232 cp_parser_non_integral_constant_expression
3233 (parser, "floating-point literal");
3235 return token->u.value;
3241 case CPP_UTF8STRING:
3242 /* ??? Should wide strings be allowed when parser->translate_strings_p
3243 is false (i.e. in attributes)? If not, we can kill the third
3244 argument to cp_parser_string_literal. */
3245 return cp_parser_string_literal (parser,
3246 parser->translate_strings_p,
3249 case CPP_OPEN_PAREN:
3252 bool saved_greater_than_is_operator_p;
3254 /* Consume the `('. */
3255 cp_lexer_consume_token (parser->lexer);
3256 /* Within a parenthesized expression, a `>' token is always
3257 the greater-than operator. */
3258 saved_greater_than_is_operator_p
3259 = parser->greater_than_is_operator_p;
3260 parser->greater_than_is_operator_p = true;
3261 /* If we see `( { ' then we are looking at the beginning of
3262 a GNU statement-expression. */
3263 if (cp_parser_allow_gnu_extensions_p (parser)
3264 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3266 /* Statement-expressions are not allowed by the standard. */
3267 pedwarn (token->location, OPT_pedantic,
3268 "ISO C++ forbids braced-groups within expressions");
3270 /* And they're not allowed outside of a function-body; you
3271 cannot, for example, write:
3273 int i = ({ int j = 3; j + 1; });
3275 at class or namespace scope. */
3276 if (!parser->in_function_body
3277 || parser->in_template_argument_list_p)
3279 error_at (token->location,
3280 "statement-expressions are not allowed outside "
3281 "functions nor in template-argument lists");
3282 cp_parser_skip_to_end_of_block_or_statement (parser);
3283 expr = error_mark_node;
3287 /* Start the statement-expression. */
3288 expr = begin_stmt_expr ();
3289 /* Parse the compound-statement. */
3290 cp_parser_compound_statement (parser, expr, false);
3292 expr = finish_stmt_expr (expr, false);
3297 /* Parse the parenthesized expression. */
3298 expr = cp_parser_expression (parser, cast_p, idk);
3299 /* Let the front end know that this expression was
3300 enclosed in parentheses. This matters in case, for
3301 example, the expression is of the form `A::B', since
3302 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3304 finish_parenthesized_expr (expr);
3306 /* The `>' token might be the end of a template-id or
3307 template-parameter-list now. */
3308 parser->greater_than_is_operator_p
3309 = saved_greater_than_is_operator_p;
3310 /* Consume the `)'. */
3311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3312 cp_parser_skip_to_end_of_statement (parser);
3317 case CPP_OPEN_SQUARE:
3318 if (c_dialect_objc ())
3319 /* We have an Objective-C++ message. */
3320 return cp_parser_objc_expression (parser);
3321 maybe_warn_cpp0x ("lambda expressions");
3322 return cp_parser_lambda_expression (parser);
3324 case CPP_OBJC_STRING:
3325 if (c_dialect_objc ())
3326 /* We have an Objective-C++ string literal. */
3327 return cp_parser_objc_expression (parser);
3328 cp_parser_error (parser, "expected primary-expression");
3329 return error_mark_node;
3332 switch (token->keyword)
3334 /* These two are the boolean literals. */
3336 cp_lexer_consume_token (parser->lexer);
3337 return boolean_true_node;
3339 cp_lexer_consume_token (parser->lexer);
3340 return boolean_false_node;
3342 /* The `__null' literal. */
3344 cp_lexer_consume_token (parser->lexer);
3347 /* Recognize the `this' keyword. */
3349 cp_lexer_consume_token (parser->lexer);
3350 if (parser->local_variables_forbidden_p)
3352 error_at (token->location,
3353 "%<this%> may not be used in this context");
3354 return error_mark_node;
3356 /* Pointers cannot appear in constant-expressions. */
3357 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3358 return error_mark_node;
3359 return finish_this_expr ();
3361 /* The `operator' keyword can be the beginning of an
3366 case RID_FUNCTION_NAME:
3367 case RID_PRETTY_FUNCTION_NAME:
3368 case RID_C99_FUNCTION_NAME:
3372 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3373 __func__ are the names of variables -- but they are
3374 treated specially. Therefore, they are handled here,
3375 rather than relying on the generic id-expression logic
3376 below. Grammatically, these names are id-expressions.
3378 Consume the token. */
3379 token = cp_lexer_consume_token (parser->lexer);
3381 switch (token->keyword)
3383 case RID_FUNCTION_NAME:
3384 name = "%<__FUNCTION__%>";
3386 case RID_PRETTY_FUNCTION_NAME:
3387 name = "%<__PRETTY_FUNCTION__%>";
3389 case RID_C99_FUNCTION_NAME:
3390 name = "%<__func__%>";
3396 if (cp_parser_non_integral_constant_expression (parser, name))
3397 return error_mark_node;
3399 /* Look up the name. */
3400 return finish_fname (token->u.value);
3408 /* The `__builtin_va_arg' construct is used to handle
3409 `va_arg'. Consume the `__builtin_va_arg' token. */
3410 cp_lexer_consume_token (parser->lexer);
3411 /* Look for the opening `('. */
3412 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3413 /* Now, parse the assignment-expression. */
3414 expression = cp_parser_assignment_expression (parser,
3415 /*cast_p=*/false, NULL);
3416 /* Look for the `,'. */
3417 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3418 /* Parse the type-id. */
3419 type = cp_parser_type_id (parser);
3420 /* Look for the closing `)'. */
3421 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3422 /* Using `va_arg' in a constant-expression is not
3424 if (cp_parser_non_integral_constant_expression (parser,
3426 return error_mark_node;
3427 return build_x_va_arg (expression, type);
3431 return cp_parser_builtin_offsetof (parser);
3433 case RID_HAS_NOTHROW_ASSIGN:
3434 case RID_HAS_NOTHROW_CONSTRUCTOR:
3435 case RID_HAS_NOTHROW_COPY:
3436 case RID_HAS_TRIVIAL_ASSIGN:
3437 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3438 case RID_HAS_TRIVIAL_COPY:
3439 case RID_HAS_TRIVIAL_DESTRUCTOR:
3440 case RID_HAS_VIRTUAL_DESTRUCTOR:
3441 case RID_IS_ABSTRACT:
3442 case RID_IS_BASE_OF:
3444 case RID_IS_CONVERTIBLE_TO:
3448 case RID_IS_POLYMORPHIC:
3449 case RID_IS_STD_LAYOUT:
3450 case RID_IS_TRIVIAL:
3452 return cp_parser_trait_expr (parser, token->keyword);
3454 /* Objective-C++ expressions. */
3456 case RID_AT_PROTOCOL:
3457 case RID_AT_SELECTOR:
3458 return cp_parser_objc_expression (parser);
3461 cp_parser_error (parser, "expected primary-expression");
3462 return error_mark_node;
3465 /* An id-expression can start with either an identifier, a
3466 `::' as the beginning of a qualified-id, or the "operator"
3470 case CPP_TEMPLATE_ID:
3471 case CPP_NESTED_NAME_SPECIFIER:
3475 const char *error_msg;
3478 cp_token *id_expr_token;
3481 /* Parse the id-expression. */
3483 = cp_parser_id_expression (parser,
3484 /*template_keyword_p=*/false,
3485 /*check_dependency_p=*/true,
3487 /*declarator_p=*/false,
3488 /*optional_p=*/false);
3489 if (id_expression == error_mark_node)
3490 return error_mark_node;
3491 id_expr_token = token;
3492 token = cp_lexer_peek_token (parser->lexer);
3493 done = (token->type != CPP_OPEN_SQUARE
3494 && token->type != CPP_OPEN_PAREN
3495 && token->type != CPP_DOT
3496 && token->type != CPP_DEREF
3497 && token->type != CPP_PLUS_PLUS
3498 && token->type != CPP_MINUS_MINUS);
3499 /* If we have a template-id, then no further lookup is
3500 required. If the template-id was for a template-class, we
3501 will sometimes have a TYPE_DECL at this point. */
3502 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3503 || TREE_CODE (id_expression) == TYPE_DECL)
3504 decl = id_expression;
3505 /* Look up the name. */
3508 tree ambiguous_decls;
3510 decl = cp_parser_lookup_name (parser, id_expression,
3513 /*is_namespace=*/false,
3514 /*check_dependency=*/true,
3516 id_expr_token->location);
3517 /* If the lookup was ambiguous, an error will already have
3519 if (ambiguous_decls)
3520 return error_mark_node;
3522 /* In Objective-C++, an instance variable (ivar) may be preferred
3523 to whatever cp_parser_lookup_name() found. */
3524 decl = objc_lookup_ivar (decl, id_expression);
3526 /* If name lookup gives us a SCOPE_REF, then the
3527 qualifying scope was dependent. */
3528 if (TREE_CODE (decl) == SCOPE_REF)
3530 /* At this point, we do not know if DECL is a valid
3531 integral constant expression. We assume that it is
3532 in fact such an expression, so that code like:
3534 template <int N> struct A {
3538 is accepted. At template-instantiation time, we
3539 will check that B<N>::i is actually a constant. */
3542 /* Check to see if DECL is a local variable in a context
3543 where that is forbidden. */
3544 if (parser->local_variables_forbidden_p
3545 && local_variable_p (decl))
3547 /* It might be that we only found DECL because we are
3548 trying to be generous with pre-ISO scoping rules.
3549 For example, consider:
3553 for (int i = 0; i < 10; ++i) {}
3554 extern void f(int j = i);
3557 Here, name look up will originally find the out
3558 of scope `i'. We need to issue a warning message,
3559 but then use the global `i'. */
3560 decl = check_for_out_of_scope_variable (decl);
3561 if (local_variable_p (decl))
3563 error_at (id_expr_token->location,
3564 "local variable %qD may not appear in this context",
3566 return error_mark_node;
3571 decl = (finish_id_expression
3572 (id_expression, decl, parser->scope,
3574 parser->integral_constant_expression_p,
3575 parser->allow_non_integral_constant_expression_p,
3576 &parser->non_integral_constant_expression_p,
3577 template_p, done, address_p,
3580 id_expr_token->location));
3582 cp_parser_error (parser, error_msg);
3586 /* Anything else is an error. */
3588 cp_parser_error (parser, "expected primary-expression");
3589 return error_mark_node;
3593 /* Parse an id-expression.
3600 :: [opt] nested-name-specifier template [opt] unqualified-id
3602 :: operator-function-id
3605 Return a representation of the unqualified portion of the
3606 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3607 a `::' or nested-name-specifier.
3609 Often, if the id-expression was a qualified-id, the caller will
3610 want to make a SCOPE_REF to represent the qualified-id. This
3611 function does not do this in order to avoid wastefully creating
3612 SCOPE_REFs when they are not required.
3614 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3617 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3618 uninstantiated templates.
3620 If *TEMPLATE_P is non-NULL, it is set to true iff the
3621 `template' keyword is used to explicitly indicate that the entity
3622 named is a template.
3624 If DECLARATOR_P is true, the id-expression is appearing as part of
3625 a declarator, rather than as part of an expression. */
3628 cp_parser_id_expression (cp_parser *parser,
3629 bool template_keyword_p,
3630 bool check_dependency_p,
3635 bool global_scope_p;
3636 bool nested_name_specifier_p;
3638 /* Assume the `template' keyword was not used. */
3640 *template_p = template_keyword_p;
3642 /* Look for the optional `::' operator. */
3644 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3646 /* Look for the optional nested-name-specifier. */
3647 nested_name_specifier_p
3648 = (cp_parser_nested_name_specifier_opt (parser,
3649 /*typename_keyword_p=*/false,
3654 /* If there is a nested-name-specifier, then we are looking at
3655 the first qualified-id production. */
3656 if (nested_name_specifier_p)
3659 tree saved_object_scope;
3660 tree saved_qualifying_scope;
3661 tree unqualified_id;
3664 /* See if the next token is the `template' keyword. */
3666 template_p = &is_template;
3667 *template_p = cp_parser_optional_template_keyword (parser);
3668 /* Name lookup we do during the processing of the
3669 unqualified-id might obliterate SCOPE. */
3670 saved_scope = parser->scope;
3671 saved_object_scope = parser->object_scope;
3672 saved_qualifying_scope = parser->qualifying_scope;
3673 /* Process the final unqualified-id. */
3674 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3677 /*optional_p=*/false);
3678 /* Restore the SAVED_SCOPE for our caller. */
3679 parser->scope = saved_scope;
3680 parser->object_scope = saved_object_scope;
3681 parser->qualifying_scope = saved_qualifying_scope;
3683 return unqualified_id;
3685 /* Otherwise, if we are in global scope, then we are looking at one
3686 of the other qualified-id productions. */
3687 else if (global_scope_p)
3692 /* Peek at the next token. */
3693 token = cp_lexer_peek_token (parser->lexer);
3695 /* If it's an identifier, and the next token is not a "<", then
3696 we can avoid the template-id case. This is an optimization
3697 for this common case. */
3698 if (token->type == CPP_NAME
3699 && !cp_parser_nth_token_starts_template_argument_list_p
3701 return cp_parser_identifier (parser);
3703 cp_parser_parse_tentatively (parser);
3704 /* Try a template-id. */
3705 id = cp_parser_template_id (parser,
3706 /*template_keyword_p=*/false,
3707 /*check_dependency_p=*/true,
3709 /* If that worked, we're done. */
3710 if (cp_parser_parse_definitely (parser))
3713 /* Peek at the next token. (Changes in the token buffer may
3714 have invalidated the pointer obtained above.) */
3715 token = cp_lexer_peek_token (parser->lexer);
3717 switch (token->type)
3720 return cp_parser_identifier (parser);
3723 if (token->keyword == RID_OPERATOR)
3724 return cp_parser_operator_function_id (parser);
3728 cp_parser_error (parser, "expected id-expression");
3729 return error_mark_node;
3733 return cp_parser_unqualified_id (parser, template_keyword_p,
3734 /*check_dependency_p=*/true,
3739 /* Parse an unqualified-id.
3743 operator-function-id
3744 conversion-function-id
3748 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3749 keyword, in a construct like `A::template ...'.
3751 Returns a representation of unqualified-id. For the `identifier'
3752 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3753 production a BIT_NOT_EXPR is returned; the operand of the
3754 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3755 other productions, see the documentation accompanying the
3756 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3757 names are looked up in uninstantiated templates. If DECLARATOR_P
3758 is true, the unqualified-id is appearing as part of a declarator,
3759 rather than as part of an expression. */
3762 cp_parser_unqualified_id (cp_parser* parser,
3763 bool template_keyword_p,
3764 bool check_dependency_p,
3770 /* Peek at the next token. */
3771 token = cp_lexer_peek_token (parser->lexer);
3773 switch (token->type)
3779 /* We don't know yet whether or not this will be a
3781 cp_parser_parse_tentatively (parser);
3782 /* Try a template-id. */
3783 id = cp_parser_template_id (parser, template_keyword_p,
3786 /* If it worked, we're done. */
3787 if (cp_parser_parse_definitely (parser))
3789 /* Otherwise, it's an ordinary identifier. */
3790 return cp_parser_identifier (parser);
3793 case CPP_TEMPLATE_ID:
3794 return cp_parser_template_id (parser, template_keyword_p,
3801 tree qualifying_scope;
3806 /* Consume the `~' token. */
3807 cp_lexer_consume_token (parser->lexer);
3808 /* Parse the class-name. The standard, as written, seems to
3811 template <typename T> struct S { ~S (); };
3812 template <typename T> S<T>::~S() {}
3814 is invalid, since `~' must be followed by a class-name, but
3815 `S<T>' is dependent, and so not known to be a class.
3816 That's not right; we need to look in uninstantiated
3817 templates. A further complication arises from:
3819 template <typename T> void f(T t) {
3823 Here, it is not possible to look up `T' in the scope of `T'
3824 itself. We must look in both the current scope, and the
3825 scope of the containing complete expression.
3827 Yet another issue is:
3836 The standard does not seem to say that the `S' in `~S'
3837 should refer to the type `S' and not the data member
3840 /* DR 244 says that we look up the name after the "~" in the
3841 same scope as we looked up the qualifying name. That idea
3842 isn't fully worked out; it's more complicated than that. */
3843 scope = parser->scope;
3844 object_scope = parser->object_scope;
3845 qualifying_scope = parser->qualifying_scope;
3847 /* Check for invalid scopes. */
3848 if (scope == error_mark_node)
3850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3851 cp_lexer_consume_token (parser->lexer);
3852 return error_mark_node;
3854 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3857 error_at (token->location,
3858 "scope %qT before %<~%> is not a class-name",
3860 cp_parser_simulate_error (parser);
3861 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3862 cp_lexer_consume_token (parser->lexer);
3863 return error_mark_node;
3865 gcc_assert (!scope || TYPE_P (scope));
3867 /* If the name is of the form "X::~X" it's OK. */
3868 token = cp_lexer_peek_token (parser->lexer);
3870 && token->type == CPP_NAME
3871 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3873 && constructor_name_p (token->u.value, scope))
3875 cp_lexer_consume_token (parser->lexer);
3876 return build_nt (BIT_NOT_EXPR, scope);
3879 /* If there was an explicit qualification (S::~T), first look
3880 in the scope given by the qualification (i.e., S). */
3882 type_decl = NULL_TREE;
3885 cp_parser_parse_tentatively (parser);
3886 type_decl = cp_parser_class_name (parser,
3887 /*typename_keyword_p=*/false,
3888 /*template_keyword_p=*/false,
3890 /*check_dependency=*/false,
3891 /*class_head_p=*/false,
3893 if (cp_parser_parse_definitely (parser))
3896 /* In "N::S::~S", look in "N" as well. */
3897 if (!done && scope && qualifying_scope)
3899 cp_parser_parse_tentatively (parser);
3900 parser->scope = qualifying_scope;
3901 parser->object_scope = NULL_TREE;
3902 parser->qualifying_scope = NULL_TREE;
3904 = cp_parser_class_name (parser,
3905 /*typename_keyword_p=*/false,
3906 /*template_keyword_p=*/false,
3908 /*check_dependency=*/false,
3909 /*class_head_p=*/false,
3911 if (cp_parser_parse_definitely (parser))
3914 /* In "p->S::~T", look in the scope given by "*p" as well. */
3915 else if (!done && object_scope)
3917 cp_parser_parse_tentatively (parser);
3918 parser->scope = object_scope;
3919 parser->object_scope = NULL_TREE;
3920 parser->qualifying_scope = NULL_TREE;
3922 = cp_parser_class_name (parser,
3923 /*typename_keyword_p=*/false,
3924 /*template_keyword_p=*/false,
3926 /*check_dependency=*/false,
3927 /*class_head_p=*/false,
3929 if (cp_parser_parse_definitely (parser))
3932 /* Look in the surrounding context. */
3935 parser->scope = NULL_TREE;
3936 parser->object_scope = NULL_TREE;
3937 parser->qualifying_scope = NULL_TREE;
3938 if (processing_template_decl)
3939 cp_parser_parse_tentatively (parser);
3941 = cp_parser_class_name (parser,
3942 /*typename_keyword_p=*/false,
3943 /*template_keyword_p=*/false,
3945 /*check_dependency=*/false,
3946 /*class_head_p=*/false,
3948 if (processing_template_decl
3949 && ! cp_parser_parse_definitely (parser))
3951 /* We couldn't find a type with this name, so just accept
3952 it and check for a match at instantiation time. */
3953 type_decl = cp_parser_identifier (parser);
3954 if (type_decl != error_mark_node)
3955 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3959 /* If an error occurred, assume that the name of the
3960 destructor is the same as the name of the qualifying
3961 class. That allows us to keep parsing after running
3962 into ill-formed destructor names. */
3963 if (type_decl == error_mark_node && scope)
3964 return build_nt (BIT_NOT_EXPR, scope);
3965 else if (type_decl == error_mark_node)
3966 return error_mark_node;
3968 /* Check that destructor name and scope match. */
3969 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3971 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3972 error_at (token->location,
3973 "declaration of %<~%T%> as member of %qT",
3975 cp_parser_simulate_error (parser);
3976 return error_mark_node;
3981 A typedef-name that names a class shall not be used as the
3982 identifier in the declarator for a destructor declaration. */
3984 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3985 && !DECL_SELF_REFERENCE_P (type_decl)
3986 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3987 error_at (token->location,
3988 "typedef-name %qD used as destructor declarator",
3991 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3995 if (token->keyword == RID_OPERATOR)
3999 /* This could be a template-id, so we try that first. */
4000 cp_parser_parse_tentatively (parser);
4001 /* Try a template-id. */
4002 id = cp_parser_template_id (parser, template_keyword_p,
4003 /*check_dependency_p=*/true,
4005 /* If that worked, we're done. */
4006 if (cp_parser_parse_definitely (parser))
4008 /* We still don't know whether we're looking at an
4009 operator-function-id or a conversion-function-id. */
4010 cp_parser_parse_tentatively (parser);
4011 /* Try an operator-function-id. */
4012 id = cp_parser_operator_function_id (parser);
4013 /* If that didn't work, try a conversion-function-id. */
4014 if (!cp_parser_parse_definitely (parser))
4015 id = cp_parser_conversion_function_id (parser);
4024 cp_parser_error (parser, "expected unqualified-id");
4025 return error_mark_node;
4029 /* Parse an (optional) nested-name-specifier.
4031 nested-name-specifier: [C++98]
4032 class-or-namespace-name :: nested-name-specifier [opt]
4033 class-or-namespace-name :: template nested-name-specifier [opt]
4035 nested-name-specifier: [C++0x]
4038 nested-name-specifier identifier ::
4039 nested-name-specifier template [opt] simple-template-id ::
4041 PARSER->SCOPE should be set appropriately before this function is
4042 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4043 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4046 Sets PARSER->SCOPE to the class (TYPE) or namespace
4047 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4048 it unchanged if there is no nested-name-specifier. Returns the new
4049 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4051 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4052 part of a declaration and/or decl-specifier. */
4055 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4056 bool typename_keyword_p,
4057 bool check_dependency_p,
4059 bool is_declaration)
4061 bool success = false;
4062 cp_token_position start = 0;
4065 /* Remember where the nested-name-specifier starts. */
4066 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4068 start = cp_lexer_token_position (parser->lexer, false);
4069 push_deferring_access_checks (dk_deferred);
4076 tree saved_qualifying_scope;
4077 bool template_keyword_p;
4079 /* Spot cases that cannot be the beginning of a
4080 nested-name-specifier. */
4081 token = cp_lexer_peek_token (parser->lexer);
4083 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4084 the already parsed nested-name-specifier. */
4085 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4087 /* Grab the nested-name-specifier and continue the loop. */
4088 cp_parser_pre_parsed_nested_name_specifier (parser);
4089 /* If we originally encountered this nested-name-specifier
4090 with IS_DECLARATION set to false, we will not have
4091 resolved TYPENAME_TYPEs, so we must do so here. */
4093 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4095 new_scope = resolve_typename_type (parser->scope,
4096 /*only_current_p=*/false);
4097 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4098 parser->scope = new_scope;
4104 /* Spot cases that cannot be the beginning of a
4105 nested-name-specifier. On the second and subsequent times
4106 through the loop, we look for the `template' keyword. */
4107 if (success && token->keyword == RID_TEMPLATE)
4109 /* A template-id can start a nested-name-specifier. */
4110 else if (token->type == CPP_TEMPLATE_ID)
4114 /* If the next token is not an identifier, then it is
4115 definitely not a type-name or namespace-name. */
4116 if (token->type != CPP_NAME)
4118 /* If the following token is neither a `<' (to begin a
4119 template-id), nor a `::', then we are not looking at a
4120 nested-name-specifier. */
4121 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4122 if (token->type != CPP_SCOPE
4123 && !cp_parser_nth_token_starts_template_argument_list_p
4128 /* The nested-name-specifier is optional, so we parse
4130 cp_parser_parse_tentatively (parser);
4132 /* Look for the optional `template' keyword, if this isn't the
4133 first time through the loop. */
4135 template_keyword_p = cp_parser_optional_template_keyword (parser);
4137 template_keyword_p = false;
4139 /* Save the old scope since the name lookup we are about to do
4140 might destroy it. */
4141 old_scope = parser->scope;
4142 saved_qualifying_scope = parser->qualifying_scope;
4143 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4144 look up names in "X<T>::I" in order to determine that "Y" is
4145 a template. So, if we have a typename at this point, we make
4146 an effort to look through it. */
4148 && !typename_keyword_p
4150 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4151 parser->scope = resolve_typename_type (parser->scope,
4152 /*only_current_p=*/false);
4153 /* Parse the qualifying entity. */
4155 = cp_parser_qualifying_entity (parser,
4161 /* Look for the `::' token. */
4162 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4164 /* If we found what we wanted, we keep going; otherwise, we're
4166 if (!cp_parser_parse_definitely (parser))
4168 bool error_p = false;
4170 /* Restore the OLD_SCOPE since it was valid before the
4171 failed attempt at finding the last
4172 class-or-namespace-name. */
4173 parser->scope = old_scope;
4174 parser->qualifying_scope = saved_qualifying_scope;
4175 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4177 /* If the next token is an identifier, and the one after
4178 that is a `::', then any valid interpretation would have
4179 found a class-or-namespace-name. */
4180 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4181 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4183 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4186 token = cp_lexer_consume_token (parser->lexer);
4189 if (!token->ambiguous_p)
4192 tree ambiguous_decls;
4194 decl = cp_parser_lookup_name (parser, token->u.value,
4196 /*is_template=*/false,
4197 /*is_namespace=*/false,
4198 /*check_dependency=*/true,
4201 if (TREE_CODE (decl) == TEMPLATE_DECL)
4202 error_at (token->location,
4203 "%qD used without template parameters",
4205 else if (ambiguous_decls)
4207 error_at (token->location,
4208 "reference to %qD is ambiguous",
4210 print_candidates (ambiguous_decls);
4211 decl = error_mark_node;
4215 const char* msg = "is not a class or namespace";
4216 if (cxx_dialect != cxx98)
4217 msg = "is not a class, namespace, or enumeration";
4218 cp_parser_name_lookup_error
4219 (parser, token->u.value, decl, msg,
4223 parser->scope = error_mark_node;
4225 /* Treat this as a successful nested-name-specifier
4230 If the name found is not a class-name (clause
4231 _class_) or namespace-name (_namespace.def_), the
4232 program is ill-formed. */
4235 cp_lexer_consume_token (parser->lexer);
4239 /* We've found one valid nested-name-specifier. */
4241 /* Name lookup always gives us a DECL. */
4242 if (TREE_CODE (new_scope) == TYPE_DECL)
4243 new_scope = TREE_TYPE (new_scope);
4244 /* Uses of "template" must be followed by actual templates. */
4245 if (template_keyword_p
4246 && !(CLASS_TYPE_P (new_scope)
4247 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4248 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4249 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4250 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4251 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4252 == TEMPLATE_ID_EXPR)))
4253 permerror (input_location, TYPE_P (new_scope)
4254 ? "%qT is not a template"
4255 : "%qD is not a template",
4257 /* If it is a class scope, try to complete it; we are about to
4258 be looking up names inside the class. */
4259 if (TYPE_P (new_scope)
4260 /* Since checking types for dependency can be expensive,
4261 avoid doing it if the type is already complete. */
4262 && !COMPLETE_TYPE_P (new_scope)
4263 /* Do not try to complete dependent types. */
4264 && !dependent_type_p (new_scope))
4266 new_scope = complete_type (new_scope);
4267 /* If it is a typedef to current class, use the current
4268 class instead, as the typedef won't have any names inside
4270 if (!COMPLETE_TYPE_P (new_scope)
4271 && currently_open_class (new_scope))
4272 new_scope = TYPE_MAIN_VARIANT (new_scope);
4274 /* Make sure we look in the right scope the next time through
4276 parser->scope = new_scope;
4279 /* If parsing tentatively, replace the sequence of tokens that makes
4280 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4281 token. That way, should we re-parse the token stream, we will
4282 not have to repeat the effort required to do the parse, nor will
4283 we issue duplicate error messages. */
4284 if (success && start)
4288 token = cp_lexer_token_at (parser->lexer, start);
4289 /* Reset the contents of the START token. */
4290 token->type = CPP_NESTED_NAME_SPECIFIER;
4291 /* Retrieve any deferred checks. Do not pop this access checks yet
4292 so the memory will not be reclaimed during token replacing below. */
4293 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4294 token->u.tree_check_value->value = parser->scope;
4295 token->u.tree_check_value->checks = get_deferred_access_checks ();
4296 token->u.tree_check_value->qualifying_scope =
4297 parser->qualifying_scope;
4298 token->keyword = RID_MAX;
4300 /* Purge all subsequent tokens. */
4301 cp_lexer_purge_tokens_after (parser->lexer, start);
4305 pop_to_parent_deferring_access_checks ();
4307 return success ? parser->scope : NULL_TREE;
4310 /* Parse a nested-name-specifier. See
4311 cp_parser_nested_name_specifier_opt for details. This function
4312 behaves identically, except that it will an issue an error if no
4313 nested-name-specifier is present. */
4316 cp_parser_nested_name_specifier (cp_parser *parser,
4317 bool typename_keyword_p,
4318 bool check_dependency_p,
4320 bool is_declaration)
4324 /* Look for the nested-name-specifier. */
4325 scope = cp_parser_nested_name_specifier_opt (parser,
4330 /* If it was not present, issue an error message. */
4333 cp_parser_error (parser, "expected nested-name-specifier");
4334 parser->scope = NULL_TREE;
4340 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4341 this is either a class-name or a namespace-name (which corresponds
4342 to the class-or-namespace-name production in the grammar). For
4343 C++0x, it can also be a type-name that refers to an enumeration
4346 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4347 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4348 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4349 TYPE_P is TRUE iff the next name should be taken as a class-name,
4350 even the same name is declared to be another entity in the same
4353 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4354 specified by the class-or-namespace-name. If neither is found the
4355 ERROR_MARK_NODE is returned. */
4358 cp_parser_qualifying_entity (cp_parser *parser,
4359 bool typename_keyword_p,
4360 bool template_keyword_p,
4361 bool check_dependency_p,
4363 bool is_declaration)
4366 tree saved_qualifying_scope;
4367 tree saved_object_scope;
4370 bool successful_parse_p;
4372 /* Before we try to parse the class-name, we must save away the
4373 current PARSER->SCOPE since cp_parser_class_name will destroy
4375 saved_scope = parser->scope;
4376 saved_qualifying_scope = parser->qualifying_scope;
4377 saved_object_scope = parser->object_scope;
4378 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4379 there is no need to look for a namespace-name. */
4380 only_class_p = template_keyword_p
4381 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4383 cp_parser_parse_tentatively (parser);
4384 scope = cp_parser_class_name (parser,
4387 type_p ? class_type : none_type,
4389 /*class_head_p=*/false,
4391 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4392 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4394 && cxx_dialect != cxx98
4395 && !successful_parse_p)
4397 /* Restore the saved scope. */
4398 parser->scope = saved_scope;
4399 parser->qualifying_scope = saved_qualifying_scope;
4400 parser->object_scope = saved_object_scope;
4402 /* Parse tentatively. */
4403 cp_parser_parse_tentatively (parser);
4405 /* Parse a typedef-name or enum-name. */
4406 scope = cp_parser_nonclass_name (parser);
4407 successful_parse_p = cp_parser_parse_definitely (parser);
4409 /* If that didn't work, try for a namespace-name. */
4410 if (!only_class_p && !successful_parse_p)
4412 /* Restore the saved scope. */
4413 parser->scope = saved_scope;
4414 parser->qualifying_scope = saved_qualifying_scope;
4415 parser->object_scope = saved_object_scope;
4416 /* If we are not looking at an identifier followed by the scope
4417 resolution operator, then this is not part of a
4418 nested-name-specifier. (Note that this function is only used
4419 to parse the components of a nested-name-specifier.) */
4420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4421 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4422 return error_mark_node;
4423 scope = cp_parser_namespace_name (parser);
4429 /* Parse a postfix-expression.
4433 postfix-expression [ expression ]
4434 postfix-expression ( expression-list [opt] )
4435 simple-type-specifier ( expression-list [opt] )
4436 typename :: [opt] nested-name-specifier identifier
4437 ( expression-list [opt] )
4438 typename :: [opt] nested-name-specifier template [opt] template-id
4439 ( expression-list [opt] )
4440 postfix-expression . template [opt] id-expression
4441 postfix-expression -> template [opt] id-expression
4442 postfix-expression . pseudo-destructor-name
4443 postfix-expression -> pseudo-destructor-name
4444 postfix-expression ++
4445 postfix-expression --
4446 dynamic_cast < type-id > ( expression )
4447 static_cast < type-id > ( expression )
4448 reinterpret_cast < type-id > ( expression )
4449 const_cast < type-id > ( expression )
4450 typeid ( expression )
4456 ( type-id ) { initializer-list , [opt] }
4458 This extension is a GNU version of the C99 compound-literal
4459 construct. (The C99 grammar uses `type-name' instead of `type-id',
4460 but they are essentially the same concept.)
4462 If ADDRESS_P is true, the postfix expression is the operand of the
4463 `&' operator. CAST_P is true if this expression is the target of a
4466 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4467 class member access expressions [expr.ref].
4469 Returns a representation of the expression. */
4472 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4473 bool member_access_only_p,
4474 cp_id_kind * pidk_return)
4478 cp_id_kind idk = CP_ID_KIND_NONE;
4479 tree postfix_expression = NULL_TREE;
4480 bool is_member_access = false;
4482 /* Peek at the next token. */
4483 token = cp_lexer_peek_token (parser->lexer);
4484 /* Some of the productions are determined by keywords. */
4485 keyword = token->keyword;
4495 const char *saved_message;
4497 /* All of these can be handled in the same way from the point
4498 of view of parsing. Begin by consuming the token
4499 identifying the cast. */
4500 cp_lexer_consume_token (parser->lexer);
4502 /* New types cannot be defined in the cast. */
4503 saved_message = parser->type_definition_forbidden_message;
4504 parser->type_definition_forbidden_message
4505 = "types may not be defined in casts";
4507 /* Look for the opening `<'. */
4508 cp_parser_require (parser, CPP_LESS, "%<<%>");
4509 /* Parse the type to which we are casting. */
4510 type = cp_parser_type_id (parser);
4511 /* Look for the closing `>'. */
4512 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4513 /* Restore the old message. */
4514 parser->type_definition_forbidden_message = saved_message;
4516 /* And the expression which is being cast. */
4517 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4518 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4519 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4521 /* Only type conversions to integral or enumeration types
4522 can be used in constant-expressions. */
4523 if (!cast_valid_in_integral_constant_expression_p (type)
4524 && (cp_parser_non_integral_constant_expression
4526 "a cast to a type other than an integral or "
4527 "enumeration type")))
4528 return error_mark_node;
4534 = build_dynamic_cast (type, expression, tf_warning_or_error);
4538 = build_static_cast (type, expression, tf_warning_or_error);
4542 = build_reinterpret_cast (type, expression,
4543 tf_warning_or_error);
4547 = build_const_cast (type, expression, tf_warning_or_error);
4558 const char *saved_message;
4559 bool saved_in_type_id_in_expr_p;
4561 /* Consume the `typeid' token. */
4562 cp_lexer_consume_token (parser->lexer);
4563 /* Look for the `(' token. */
4564 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4565 /* Types cannot be defined in a `typeid' expression. */
4566 saved_message = parser->type_definition_forbidden_message;
4567 parser->type_definition_forbidden_message
4568 = "types may not be defined in a %<typeid%> expression";
4569 /* We can't be sure yet whether we're looking at a type-id or an
4571 cp_parser_parse_tentatively (parser);
4572 /* Try a type-id first. */
4573 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4574 parser->in_type_id_in_expr_p = true;
4575 type = cp_parser_type_id (parser);
4576 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4577 /* Look for the `)' token. Otherwise, we can't be sure that
4578 we're not looking at an expression: consider `typeid (int
4579 (3))', for example. */
4580 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4581 /* If all went well, simply lookup the type-id. */
4582 if (cp_parser_parse_definitely (parser))
4583 postfix_expression = get_typeid (type);
4584 /* Otherwise, fall back to the expression variant. */
4589 /* Look for an expression. */
4590 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4591 /* Compute its typeid. */
4592 postfix_expression = build_typeid (expression);
4593 /* Look for the `)' token. */
4594 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4596 /* Restore the saved message. */
4597 parser->type_definition_forbidden_message = saved_message;
4598 /* `typeid' may not appear in an integral constant expression. */
4599 if (cp_parser_non_integral_constant_expression(parser,
4600 "%<typeid%> operator"))
4601 return error_mark_node;
4608 /* The syntax permitted here is the same permitted for an
4609 elaborated-type-specifier. */
4610 type = cp_parser_elaborated_type_specifier (parser,
4611 /*is_friend=*/false,
4612 /*is_declaration=*/false);
4613 postfix_expression = cp_parser_functional_cast (parser, type);
4621 /* If the next thing is a simple-type-specifier, we may be
4622 looking at a functional cast. We could also be looking at
4623 an id-expression. So, we try the functional cast, and if
4624 that doesn't work we fall back to the primary-expression. */
4625 cp_parser_parse_tentatively (parser);
4626 /* Look for the simple-type-specifier. */
4627 type = cp_parser_simple_type_specifier (parser,
4628 /*decl_specs=*/NULL,
4629 CP_PARSER_FLAGS_NONE);
4630 /* Parse the cast itself. */
4631 if (!cp_parser_error_occurred (parser))
4633 = cp_parser_functional_cast (parser, type);
4634 /* If that worked, we're done. */
4635 if (cp_parser_parse_definitely (parser))
4638 /* If the functional-cast didn't work out, try a
4639 compound-literal. */
4640 if (cp_parser_allow_gnu_extensions_p (parser)
4641 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4643 VEC(constructor_elt,gc) *initializer_list = NULL;
4644 bool saved_in_type_id_in_expr_p;
4646 cp_parser_parse_tentatively (parser);
4647 /* Consume the `('. */
4648 cp_lexer_consume_token (parser->lexer);
4649 /* Parse the type. */
4650 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4651 parser->in_type_id_in_expr_p = true;
4652 type = cp_parser_type_id (parser);
4653 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4654 /* Look for the `)'. */
4655 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4656 /* Look for the `{'. */
4657 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4658 /* If things aren't going well, there's no need to
4660 if (!cp_parser_error_occurred (parser))
4662 bool non_constant_p;
4663 /* Parse the initializer-list. */
4665 = cp_parser_initializer_list (parser, &non_constant_p);
4666 /* Allow a trailing `,'. */
4667 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4668 cp_lexer_consume_token (parser->lexer);
4669 /* Look for the final `}'. */
4670 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4672 /* If that worked, we're definitely looking at a
4673 compound-literal expression. */
4674 if (cp_parser_parse_definitely (parser))
4676 /* Warn the user that a compound literal is not
4677 allowed in standard C++. */
4678 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4679 /* For simplicity, we disallow compound literals in
4680 constant-expressions. We could
4681 allow compound literals of integer type, whose
4682 initializer was a constant, in constant
4683 expressions. Permitting that usage, as a further
4684 extension, would not change the meaning of any
4685 currently accepted programs. (Of course, as
4686 compound literals are not part of ISO C++, the
4687 standard has nothing to say.) */
4688 if (cp_parser_non_integral_constant_expression
4689 (parser, "non-constant compound literals"))
4691 postfix_expression = error_mark_node;
4694 /* Form the representation of the compound-literal. */
4696 = (finish_compound_literal
4697 (type, build_constructor (init_list_type_node,
4698 initializer_list)));
4703 /* It must be a primary-expression. */
4705 = cp_parser_primary_expression (parser, address_p, cast_p,
4706 /*template_arg_p=*/false,
4712 /* Keep looping until the postfix-expression is complete. */
4715 if (idk == CP_ID_KIND_UNQUALIFIED
4716 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4717 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4718 /* It is not a Koenig lookup function call. */
4720 = unqualified_name_lookup_error (postfix_expression);
4722 /* Peek at the next token. */
4723 token = cp_lexer_peek_token (parser->lexer);
4725 switch (token->type)
4727 case CPP_OPEN_SQUARE:
4729 = cp_parser_postfix_open_square_expression (parser,
4732 idk = CP_ID_KIND_NONE;
4733 is_member_access = false;
4736 case CPP_OPEN_PAREN:
4737 /* postfix-expression ( expression-list [opt] ) */
4740 bool is_builtin_constant_p;
4741 bool saved_integral_constant_expression_p = false;
4742 bool saved_non_integral_constant_expression_p = false;
4745 is_member_access = false;
4747 is_builtin_constant_p
4748 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4749 if (is_builtin_constant_p)
4751 /* The whole point of __builtin_constant_p is to allow
4752 non-constant expressions to appear as arguments. */
4753 saved_integral_constant_expression_p
4754 = parser->integral_constant_expression_p;
4755 saved_non_integral_constant_expression_p
4756 = parser->non_integral_constant_expression_p;
4757 parser->integral_constant_expression_p = false;
4759 args = (cp_parser_parenthesized_expression_list
4760 (parser, /*is_attribute_list=*/false,
4761 /*cast_p=*/false, /*allow_expansion_p=*/true,
4762 /*non_constant_p=*/NULL));
4763 if (is_builtin_constant_p)
4765 parser->integral_constant_expression_p
4766 = saved_integral_constant_expression_p;
4767 parser->non_integral_constant_expression_p
4768 = saved_non_integral_constant_expression_p;
4773 postfix_expression = error_mark_node;
4777 /* Function calls are not permitted in
4778 constant-expressions. */
4779 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4780 && cp_parser_non_integral_constant_expression (parser,
4783 postfix_expression = error_mark_node;
4784 release_tree_vector (args);
4789 if (idk == CP_ID_KIND_UNQUALIFIED
4790 || idk == CP_ID_KIND_TEMPLATE_ID)
4792 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4794 if (!VEC_empty (tree, args))
4797 if (!any_type_dependent_arguments_p (args))
4799 = perform_koenig_lookup (postfix_expression, args);
4803 = unqualified_fn_lookup_error (postfix_expression);
4805 /* We do not perform argument-dependent lookup if
4806 normal lookup finds a non-function, in accordance
4807 with the expected resolution of DR 218. */
4808 else if (!VEC_empty (tree, args)
4809 && is_overloaded_fn (postfix_expression))
4811 tree fn = get_first_fn (postfix_expression);
4813 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4814 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4816 /* Only do argument dependent lookup if regular
4817 lookup does not find a set of member functions.
4818 [basic.lookup.koenig]/2a */
4819 if (!DECL_FUNCTION_MEMBER_P (fn))
4822 if (!any_type_dependent_arguments_p (args))
4824 = perform_koenig_lookup (postfix_expression, args);
4829 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4831 tree instance = TREE_OPERAND (postfix_expression, 0);
4832 tree fn = TREE_OPERAND (postfix_expression, 1);
4834 if (processing_template_decl
4835 && (type_dependent_expression_p (instance)
4836 || (!BASELINK_P (fn)
4837 && TREE_CODE (fn) != FIELD_DECL)
4838 || type_dependent_expression_p (fn)
4839 || any_type_dependent_arguments_p (args)))
4842 = build_nt_call_vec (postfix_expression, args);
4843 release_tree_vector (args);
4847 if (BASELINK_P (fn))
4850 = (build_new_method_call
4851 (instance, fn, &args, NULL_TREE,
4852 (idk == CP_ID_KIND_QUALIFIED
4853 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4855 tf_warning_or_error));
4859 = finish_call_expr (postfix_expression, &args,
4860 /*disallow_virtual=*/false,
4862 tf_warning_or_error);
4864 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4865 || TREE_CODE (postfix_expression) == MEMBER_REF
4866 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4867 postfix_expression = (build_offset_ref_call_from_tree
4868 (postfix_expression, &args));
4869 else if (idk == CP_ID_KIND_QUALIFIED)
4870 /* A call to a static class member, or a namespace-scope
4873 = finish_call_expr (postfix_expression, &args,
4874 /*disallow_virtual=*/true,
4876 tf_warning_or_error);
4878 /* All other function calls. */
4880 = finish_call_expr (postfix_expression, &args,
4881 /*disallow_virtual=*/false,
4883 tf_warning_or_error);
4885 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4886 idk = CP_ID_KIND_NONE;
4888 release_tree_vector (args);
4894 /* postfix-expression . template [opt] id-expression
4895 postfix-expression . pseudo-destructor-name
4896 postfix-expression -> template [opt] id-expression
4897 postfix-expression -> pseudo-destructor-name */
4899 /* Consume the `.' or `->' operator. */
4900 cp_lexer_consume_token (parser->lexer);
4903 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4908 is_member_access = true;
4912 /* postfix-expression ++ */
4913 /* Consume the `++' token. */
4914 cp_lexer_consume_token (parser->lexer);
4915 /* Generate a representation for the complete expression. */
4917 = finish_increment_expr (postfix_expression,
4918 POSTINCREMENT_EXPR);
4919 /* Increments may not appear in constant-expressions. */
4920 if (cp_parser_non_integral_constant_expression (parser,
4922 postfix_expression = error_mark_node;
4923 idk = CP_ID_KIND_NONE;
4924 is_member_access = false;
4927 case CPP_MINUS_MINUS:
4928 /* postfix-expression -- */
4929 /* Consume the `--' token. */
4930 cp_lexer_consume_token (parser->lexer);
4931 /* Generate a representation for the complete expression. */
4933 = finish_increment_expr (postfix_expression,
4934 POSTDECREMENT_EXPR);
4935 /* Decrements may not appear in constant-expressions. */
4936 if (cp_parser_non_integral_constant_expression (parser,
4938 postfix_expression = error_mark_node;
4939 idk = CP_ID_KIND_NONE;
4940 is_member_access = false;
4944 if (pidk_return != NULL)
4945 * pidk_return = idk;
4946 if (member_access_only_p)
4947 return is_member_access? postfix_expression : error_mark_node;
4949 return postfix_expression;
4953 /* We should never get here. */
4955 return error_mark_node;
4958 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4959 by cp_parser_builtin_offsetof. We're looking for
4961 postfix-expression [ expression ]
4963 FOR_OFFSETOF is set if we're being called in that context, which
4964 changes how we deal with integer constant expressions. */
4967 cp_parser_postfix_open_square_expression (cp_parser *parser,
4968 tree postfix_expression,
4973 /* Consume the `[' token. */
4974 cp_lexer_consume_token (parser->lexer);
4976 /* Parse the index expression. */
4977 /* ??? For offsetof, there is a question of what to allow here. If
4978 offsetof is not being used in an integral constant expression context,
4979 then we *could* get the right answer by computing the value at runtime.
4980 If we are in an integral constant expression context, then we might
4981 could accept any constant expression; hard to say without analysis.
4982 Rather than open the barn door too wide right away, allow only integer
4983 constant expressions here. */
4985 index = cp_parser_constant_expression (parser, false, NULL);
4987 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4989 /* Look for the closing `]'. */
4990 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4992 /* Build the ARRAY_REF. */
4993 postfix_expression = grok_array_decl (postfix_expression, index);
4995 /* When not doing offsetof, array references are not permitted in
4996 constant-expressions. */
4998 && (cp_parser_non_integral_constant_expression
4999 (parser, "an array reference")))
5000 postfix_expression = error_mark_node;
5002 return postfix_expression;
5005 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5006 by cp_parser_builtin_offsetof. We're looking for
5008 postfix-expression . template [opt] id-expression
5009 postfix-expression . pseudo-destructor-name
5010 postfix-expression -> template [opt] id-expression
5011 postfix-expression -> pseudo-destructor-name
5013 FOR_OFFSETOF is set if we're being called in that context. That sorta
5014 limits what of the above we'll actually accept, but nevermind.
5015 TOKEN_TYPE is the "." or "->" token, which will already have been
5016 removed from the stream. */
5019 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5020 enum cpp_ttype token_type,
5021 tree postfix_expression,
5022 bool for_offsetof, cp_id_kind *idk,
5023 location_t location)
5027 bool pseudo_destructor_p;
5028 tree scope = NULL_TREE;
5030 /* If this is a `->' operator, dereference the pointer. */
5031 if (token_type == CPP_DEREF)
5032 postfix_expression = build_x_arrow (postfix_expression);
5033 /* Check to see whether or not the expression is type-dependent. */
5034 dependent_p = type_dependent_expression_p (postfix_expression);
5035 /* The identifier following the `->' or `.' is not qualified. */
5036 parser->scope = NULL_TREE;
5037 parser->qualifying_scope = NULL_TREE;
5038 parser->object_scope = NULL_TREE;
5039 *idk = CP_ID_KIND_NONE;
5041 /* Enter the scope corresponding to the type of the object
5042 given by the POSTFIX_EXPRESSION. */
5043 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5045 scope = TREE_TYPE (postfix_expression);
5046 /* According to the standard, no expression should ever have
5047 reference type. Unfortunately, we do not currently match
5048 the standard in this respect in that our internal representation
5049 of an expression may have reference type even when the standard
5050 says it does not. Therefore, we have to manually obtain the
5051 underlying type here. */
5052 scope = non_reference (scope);
5053 /* The type of the POSTFIX_EXPRESSION must be complete. */
5054 if (scope == unknown_type_node)
5056 error_at (location, "%qE does not have class type",
5057 postfix_expression);
5061 scope = complete_type_or_else (scope, NULL_TREE);
5062 /* Let the name lookup machinery know that we are processing a
5063 class member access expression. */
5064 parser->context->object_type = scope;
5065 /* If something went wrong, we want to be able to discern that case,
5066 as opposed to the case where there was no SCOPE due to the type
5067 of expression being dependent. */
5069 scope = error_mark_node;
5070 /* If the SCOPE was erroneous, make the various semantic analysis
5071 functions exit quickly -- and without issuing additional error
5073 if (scope == error_mark_node)
5074 postfix_expression = error_mark_node;
5077 /* Assume this expression is not a pseudo-destructor access. */
5078 pseudo_destructor_p = false;
5080 /* If the SCOPE is a scalar type, then, if this is a valid program,
5081 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5082 is type dependent, it can be pseudo-destructor-name or something else.
5083 Try to parse it as pseudo-destructor-name first. */
5084 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5089 cp_parser_parse_tentatively (parser);
5090 /* Parse the pseudo-destructor-name. */
5092 cp_parser_pseudo_destructor_name (parser, &s, &type);
5094 && (cp_parser_error_occurred (parser)
5095 || TREE_CODE (type) != TYPE_DECL
5096 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5097 cp_parser_abort_tentative_parse (parser);
5098 else if (cp_parser_parse_definitely (parser))
5100 pseudo_destructor_p = true;
5102 = finish_pseudo_destructor_expr (postfix_expression,
5103 s, TREE_TYPE (type));
5107 if (!pseudo_destructor_p)
5109 /* If the SCOPE is not a scalar type, we are looking at an
5110 ordinary class member access expression, rather than a
5111 pseudo-destructor-name. */
5113 cp_token *token = cp_lexer_peek_token (parser->lexer);
5114 /* Parse the id-expression. */
5115 name = (cp_parser_id_expression
5117 cp_parser_optional_template_keyword (parser),
5118 /*check_dependency_p=*/true,
5120 /*declarator_p=*/false,
5121 /*optional_p=*/false));
5122 /* In general, build a SCOPE_REF if the member name is qualified.
5123 However, if the name was not dependent and has already been
5124 resolved; there is no need to build the SCOPE_REF. For example;
5126 struct X { void f(); };
5127 template <typename T> void f(T* t) { t->X::f(); }
5129 Even though "t" is dependent, "X::f" is not and has been resolved
5130 to a BASELINK; there is no need to include scope information. */
5132 /* But we do need to remember that there was an explicit scope for
5133 virtual function calls. */
5135 *idk = CP_ID_KIND_QUALIFIED;
5137 /* If the name is a template-id that names a type, we will get a
5138 TYPE_DECL here. That is invalid code. */
5139 if (TREE_CODE (name) == TYPE_DECL)
5141 error_at (token->location, "invalid use of %qD", name);
5142 postfix_expression = error_mark_node;
5146 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5148 name = build_qualified_name (/*type=*/NULL_TREE,
5152 parser->scope = NULL_TREE;
5153 parser->qualifying_scope = NULL_TREE;
5154 parser->object_scope = NULL_TREE;
5156 if (scope && name && BASELINK_P (name))
5157 adjust_result_of_qualified_name_lookup
5158 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5160 = finish_class_member_access_expr (postfix_expression, name,
5162 tf_warning_or_error);
5166 /* We no longer need to look up names in the scope of the object on
5167 the left-hand side of the `.' or `->' operator. */
5168 parser->context->object_type = NULL_TREE;
5170 /* Outside of offsetof, these operators may not appear in
5171 constant-expressions. */
5173 && (cp_parser_non_integral_constant_expression
5174 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5175 postfix_expression = error_mark_node;
5177 return postfix_expression;
5180 /* Parse a parenthesized expression-list.
5183 assignment-expression
5184 expression-list, assignment-expression
5189 identifier, expression-list
5191 CAST_P is true if this expression is the target of a cast.
5193 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5196 Returns a vector of trees. Each element is a representation of an
5197 assignment-expression. NULL is returned if the ( and or ) are
5198 missing. An empty, but allocated, vector is returned on no
5199 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5200 if this is really an attribute list being parsed. If
5201 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5202 not all of the expressions in the list were constant. */
5204 static VEC(tree,gc) *
5205 cp_parser_parenthesized_expression_list (cp_parser* parser,
5206 bool is_attribute_list,
5208 bool allow_expansion_p,
5209 bool *non_constant_p)
5211 VEC(tree,gc) *expression_list;
5212 bool fold_expr_p = is_attribute_list;
5213 tree identifier = NULL_TREE;
5214 bool saved_greater_than_is_operator_p;
5216 /* Assume all the expressions will be constant. */
5218 *non_constant_p = false;
5220 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5223 expression_list = make_tree_vector ();
5225 /* Within a parenthesized expression, a `>' token is always
5226 the greater-than operator. */
5227 saved_greater_than_is_operator_p
5228 = parser->greater_than_is_operator_p;
5229 parser->greater_than_is_operator_p = true;
5231 /* Consume expressions until there are no more. */
5232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5237 /* At the beginning of attribute lists, check to see if the
5238 next token is an identifier. */
5239 if (is_attribute_list
5240 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5244 /* Consume the identifier. */
5245 token = cp_lexer_consume_token (parser->lexer);
5246 /* Save the identifier. */
5247 identifier = token->u.value;
5251 bool expr_non_constant_p;
5253 /* Parse the next assignment-expression. */
5254 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5256 /* A braced-init-list. */
5257 maybe_warn_cpp0x ("extended initializer lists");
5258 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5259 if (non_constant_p && expr_non_constant_p)
5260 *non_constant_p = true;
5262 else if (non_constant_p)
5264 expr = (cp_parser_constant_expression
5265 (parser, /*allow_non_constant_p=*/true,
5266 &expr_non_constant_p));
5267 if (expr_non_constant_p)
5268 *non_constant_p = true;
5271 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5274 expr = fold_non_dependent_expr (expr);
5276 /* If we have an ellipsis, then this is an expression
5278 if (allow_expansion_p
5279 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5281 /* Consume the `...'. */
5282 cp_lexer_consume_token (parser->lexer);
5284 /* Build the argument pack. */
5285 expr = make_pack_expansion (expr);
5288 /* Add it to the list. We add error_mark_node
5289 expressions to the list, so that we can still tell if
5290 the correct form for a parenthesized expression-list
5291 is found. That gives better errors. */
5292 VEC_safe_push (tree, gc, expression_list, expr);
5294 if (expr == error_mark_node)
5298 /* After the first item, attribute lists look the same as
5299 expression lists. */
5300 is_attribute_list = false;
5303 /* If the next token isn't a `,', then we are done. */
5304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5307 /* Otherwise, consume the `,' and keep going. */
5308 cp_lexer_consume_token (parser->lexer);
5311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5316 /* We try and resync to an unnested comma, as that will give the
5317 user better diagnostics. */
5318 ending = cp_parser_skip_to_closing_parenthesis (parser,
5319 /*recovering=*/true,
5321 /*consume_paren=*/true);
5326 parser->greater_than_is_operator_p
5327 = saved_greater_than_is_operator_p;
5332 parser->greater_than_is_operator_p
5333 = saved_greater_than_is_operator_p;
5336 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5338 return expression_list;
5341 /* Parse a pseudo-destructor-name.
5343 pseudo-destructor-name:
5344 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5345 :: [opt] nested-name-specifier template template-id :: ~ type-name
5346 :: [opt] nested-name-specifier [opt] ~ type-name
5348 If either of the first two productions is used, sets *SCOPE to the
5349 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5350 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5351 or ERROR_MARK_NODE if the parse fails. */
5354 cp_parser_pseudo_destructor_name (cp_parser* parser,
5358 bool nested_name_specifier_p;
5360 /* Assume that things will not work out. */
5361 *type = error_mark_node;
5363 /* Look for the optional `::' operator. */
5364 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5365 /* Look for the optional nested-name-specifier. */
5366 nested_name_specifier_p
5367 = (cp_parser_nested_name_specifier_opt (parser,
5368 /*typename_keyword_p=*/false,
5369 /*check_dependency_p=*/true,
5371 /*is_declaration=*/false)
5373 /* Now, if we saw a nested-name-specifier, we might be doing the
5374 second production. */
5375 if (nested_name_specifier_p
5376 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5378 /* Consume the `template' keyword. */
5379 cp_lexer_consume_token (parser->lexer);
5380 /* Parse the template-id. */
5381 cp_parser_template_id (parser,
5382 /*template_keyword_p=*/true,
5383 /*check_dependency_p=*/false,
5384 /*is_declaration=*/true);
5385 /* Look for the `::' token. */
5386 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5388 /* If the next token is not a `~', then there might be some
5389 additional qualification. */
5390 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5392 /* At this point, we're looking for "type-name :: ~". The type-name
5393 must not be a class-name, since this is a pseudo-destructor. So,
5394 it must be either an enum-name, or a typedef-name -- both of which
5395 are just identifiers. So, we peek ahead to check that the "::"
5396 and "~" tokens are present; if they are not, then we can avoid
5397 calling type_name. */
5398 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5399 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5400 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5402 cp_parser_error (parser, "non-scalar type");
5406 /* Look for the type-name. */
5407 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5408 if (*scope == error_mark_node)
5411 /* Look for the `::' token. */
5412 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5417 /* Look for the `~'. */
5418 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5419 /* Look for the type-name again. We are not responsible for
5420 checking that it matches the first type-name. */
5421 *type = cp_parser_nonclass_name (parser);
5424 /* Parse a unary-expression.
5430 unary-operator cast-expression
5431 sizeof unary-expression
5439 __extension__ cast-expression
5440 __alignof__ unary-expression
5441 __alignof__ ( type-id )
5442 __real__ cast-expression
5443 __imag__ cast-expression
5446 ADDRESS_P is true iff the unary-expression is appearing as the
5447 operand of the `&' operator. CAST_P is true if this expression is
5448 the target of a cast.
5450 Returns a representation of the expression. */
5453 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5457 enum tree_code unary_operator;
5459 /* Peek at the next token. */
5460 token = cp_lexer_peek_token (parser->lexer);
5461 /* Some keywords give away the kind of expression. */
5462 if (token->type == CPP_KEYWORD)
5464 enum rid keyword = token->keyword;
5474 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5475 /* Consume the token. */
5476 cp_lexer_consume_token (parser->lexer);
5477 /* Parse the operand. */
5478 operand = cp_parser_sizeof_operand (parser, keyword);
5480 if (TYPE_P (operand))
5481 return cxx_sizeof_or_alignof_type (operand, op, true);
5483 return cxx_sizeof_or_alignof_expr (operand, op, true);
5487 return cp_parser_new_expression (parser);
5490 return cp_parser_delete_expression (parser);
5494 /* The saved value of the PEDANTIC flag. */
5498 /* Save away the PEDANTIC flag. */
5499 cp_parser_extension_opt (parser, &saved_pedantic);
5500 /* Parse the cast-expression. */
5501 expr = cp_parser_simple_cast_expression (parser);
5502 /* Restore the PEDANTIC flag. */
5503 pedantic = saved_pedantic;
5513 /* Consume the `__real__' or `__imag__' token. */
5514 cp_lexer_consume_token (parser->lexer);
5515 /* Parse the cast-expression. */
5516 expression = cp_parser_simple_cast_expression (parser);
5517 /* Create the complete representation. */
5518 return build_x_unary_op ((keyword == RID_REALPART
5519 ? REALPART_EXPR : IMAGPART_EXPR),
5521 tf_warning_or_error);
5530 /* Look for the `:: new' and `:: delete', which also signal the
5531 beginning of a new-expression, or delete-expression,
5532 respectively. If the next token is `::', then it might be one of
5534 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5538 /* See if the token after the `::' is one of the keywords in
5539 which we're interested. */
5540 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5541 /* If it's `new', we have a new-expression. */
5542 if (keyword == RID_NEW)
5543 return cp_parser_new_expression (parser);
5544 /* Similarly, for `delete'. */
5545 else if (keyword == RID_DELETE)
5546 return cp_parser_delete_expression (parser);
5549 /* Look for a unary operator. */
5550 unary_operator = cp_parser_unary_operator (token);
5551 /* The `++' and `--' operators can be handled similarly, even though
5552 they are not technically unary-operators in the grammar. */
5553 if (unary_operator == ERROR_MARK)
5555 if (token->type == CPP_PLUS_PLUS)
5556 unary_operator = PREINCREMENT_EXPR;
5557 else if (token->type == CPP_MINUS_MINUS)
5558 unary_operator = PREDECREMENT_EXPR;
5559 /* Handle the GNU address-of-label extension. */
5560 else if (cp_parser_allow_gnu_extensions_p (parser)
5561 && token->type == CPP_AND_AND)
5565 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5567 /* Consume the '&&' token. */
5568 cp_lexer_consume_token (parser->lexer);
5569 /* Look for the identifier. */
5570 identifier = cp_parser_identifier (parser);
5571 /* Create an expression representing the address. */
5572 expression = finish_label_address_expr (identifier, loc);
5573 if (cp_parser_non_integral_constant_expression (parser,
5574 "the address of a label"))
5575 expression = error_mark_node;
5579 if (unary_operator != ERROR_MARK)
5581 tree cast_expression;
5582 tree expression = error_mark_node;
5583 const char *non_constant_p = NULL;
5585 /* Consume the operator token. */
5586 token = cp_lexer_consume_token (parser->lexer);
5587 /* Parse the cast-expression. */
5589 = cp_parser_cast_expression (parser,
5590 unary_operator == ADDR_EXPR,
5591 /*cast_p=*/false, pidk);
5592 /* Now, build an appropriate representation. */
5593 switch (unary_operator)
5596 non_constant_p = "%<*%>";
5597 expression = build_x_indirect_ref (cast_expression, "unary *",
5598 tf_warning_or_error);
5602 non_constant_p = "%<&%>";
5605 expression = build_x_unary_op (unary_operator, cast_expression,
5606 tf_warning_or_error);
5609 case PREINCREMENT_EXPR:
5610 case PREDECREMENT_EXPR:
5611 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5612 ? "%<++%>" : "%<--%>");
5614 case UNARY_PLUS_EXPR:
5616 case TRUTH_NOT_EXPR:
5617 expression = finish_unary_op_expr (unary_operator, cast_expression);
5625 && cp_parser_non_integral_constant_expression (parser,
5627 expression = error_mark_node;
5632 return cp_parser_postfix_expression (parser, address_p, cast_p,
5633 /*member_access_only_p=*/false,
5637 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5638 unary-operator, the corresponding tree code is returned. */
5640 static enum tree_code
5641 cp_parser_unary_operator (cp_token* token)
5643 switch (token->type)
5646 return INDIRECT_REF;
5652 return UNARY_PLUS_EXPR;
5658 return TRUTH_NOT_EXPR;
5661 return BIT_NOT_EXPR;
5668 /* Parse a new-expression.
5671 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5672 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5674 Returns a representation of the expression. */
5677 cp_parser_new_expression (cp_parser* parser)
5679 bool global_scope_p;
5680 VEC(tree,gc) *placement;
5682 VEC(tree,gc) *initializer;
5686 /* Look for the optional `::' operator. */
5688 = (cp_parser_global_scope_opt (parser,
5689 /*current_scope_valid_p=*/false)
5691 /* Look for the `new' operator. */
5692 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5693 /* There's no easy way to tell a new-placement from the
5694 `( type-id )' construct. */
5695 cp_parser_parse_tentatively (parser);
5696 /* Look for a new-placement. */
5697 placement = cp_parser_new_placement (parser);
5698 /* If that didn't work out, there's no new-placement. */
5699 if (!cp_parser_parse_definitely (parser))
5701 if (placement != NULL)
5702 release_tree_vector (placement);
5706 /* If the next token is a `(', then we have a parenthesized
5708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5711 /* Consume the `('. */
5712 cp_lexer_consume_token (parser->lexer);
5713 /* Parse the type-id. */
5714 type = cp_parser_type_id (parser);
5715 /* Look for the closing `)'. */
5716 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5717 token = cp_lexer_peek_token (parser->lexer);
5718 /* There should not be a direct-new-declarator in this production,
5719 but GCC used to allowed this, so we check and emit a sensible error
5720 message for this case. */
5721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5723 error_at (token->location,
5724 "array bound forbidden after parenthesized type-id");
5725 inform (token->location,
5726 "try removing the parentheses around the type-id");
5727 cp_parser_direct_new_declarator (parser);
5731 /* Otherwise, there must be a new-type-id. */
5733 type = cp_parser_new_type_id (parser, &nelts);
5735 /* If the next token is a `(' or '{', then we have a new-initializer. */
5736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5737 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5738 initializer = cp_parser_new_initializer (parser);
5742 /* A new-expression may not appear in an integral constant
5744 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5745 ret = error_mark_node;
5748 /* Create a representation of the new-expression. */
5749 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5750 tf_warning_or_error);
5753 if (placement != NULL)
5754 release_tree_vector (placement);
5755 if (initializer != NULL)
5756 release_tree_vector (initializer);
5761 /* Parse a new-placement.
5766 Returns the same representation as for an expression-list. */
5768 static VEC(tree,gc) *
5769 cp_parser_new_placement (cp_parser* parser)
5771 VEC(tree,gc) *expression_list;
5773 /* Parse the expression-list. */
5774 expression_list = (cp_parser_parenthesized_expression_list
5775 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5776 /*non_constant_p=*/NULL));
5778 return expression_list;
5781 /* Parse a new-type-id.
5784 type-specifier-seq new-declarator [opt]
5786 Returns the TYPE allocated. If the new-type-id indicates an array
5787 type, *NELTS is set to the number of elements in the last array
5788 bound; the TYPE will not include the last array bound. */
5791 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5793 cp_decl_specifier_seq type_specifier_seq;
5794 cp_declarator *new_declarator;
5795 cp_declarator *declarator;
5796 cp_declarator *outer_declarator;
5797 const char *saved_message;
5800 /* The type-specifier sequence must not contain type definitions.
5801 (It cannot contain declarations of new types either, but if they
5802 are not definitions we will catch that because they are not
5804 saved_message = parser->type_definition_forbidden_message;
5805 parser->type_definition_forbidden_message
5806 = "types may not be defined in a new-type-id";
5807 /* Parse the type-specifier-seq. */
5808 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5809 /*is_trailing_return=*/false,
5810 &type_specifier_seq);
5811 /* Restore the old message. */
5812 parser->type_definition_forbidden_message = saved_message;
5813 /* Parse the new-declarator. */
5814 new_declarator = cp_parser_new_declarator_opt (parser);
5816 /* Determine the number of elements in the last array dimension, if
5819 /* Skip down to the last array dimension. */
5820 declarator = new_declarator;
5821 outer_declarator = NULL;
5822 while (declarator && (declarator->kind == cdk_pointer
5823 || declarator->kind == cdk_ptrmem))
5825 outer_declarator = declarator;
5826 declarator = declarator->declarator;
5829 && declarator->kind == cdk_array
5830 && declarator->declarator
5831 && declarator->declarator->kind == cdk_array)
5833 outer_declarator = declarator;
5834 declarator = declarator->declarator;
5837 if (declarator && declarator->kind == cdk_array)
5839 *nelts = declarator->u.array.bounds;
5840 if (*nelts == error_mark_node)
5841 *nelts = integer_one_node;
5843 if (outer_declarator)
5844 outer_declarator->declarator = declarator->declarator;
5846 new_declarator = NULL;
5849 type = groktypename (&type_specifier_seq, new_declarator, false);
5853 /* Parse an (optional) new-declarator.
5856 ptr-operator new-declarator [opt]
5857 direct-new-declarator
5859 Returns the declarator. */
5861 static cp_declarator *
5862 cp_parser_new_declarator_opt (cp_parser* parser)
5864 enum tree_code code;
5866 cp_cv_quals cv_quals;
5868 /* We don't know if there's a ptr-operator next, or not. */
5869 cp_parser_parse_tentatively (parser);
5870 /* Look for a ptr-operator. */
5871 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5872 /* If that worked, look for more new-declarators. */
5873 if (cp_parser_parse_definitely (parser))
5875 cp_declarator *declarator;
5877 /* Parse another optional declarator. */
5878 declarator = cp_parser_new_declarator_opt (parser);
5880 return cp_parser_make_indirect_declarator
5881 (code, type, cv_quals, declarator);
5884 /* If the next token is a `[', there is a direct-new-declarator. */
5885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5886 return cp_parser_direct_new_declarator (parser);
5891 /* Parse a direct-new-declarator.
5893 direct-new-declarator:
5895 direct-new-declarator [constant-expression]
5899 static cp_declarator *
5900 cp_parser_direct_new_declarator (cp_parser* parser)
5902 cp_declarator *declarator = NULL;
5908 /* Look for the opening `['. */
5909 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5910 /* The first expression is not required to be constant. */
5913 cp_token *token = cp_lexer_peek_token (parser->lexer);
5914 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5915 /* The standard requires that the expression have integral
5916 type. DR 74 adds enumeration types. We believe that the
5917 real intent is that these expressions be handled like the
5918 expression in a `switch' condition, which also allows
5919 classes with a single conversion to integral or
5920 enumeration type. */
5921 if (!processing_template_decl)
5924 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5929 error_at (token->location,
5930 "expression in new-declarator must have integral "
5931 "or enumeration type");
5932 expression = error_mark_node;
5936 /* But all the other expressions must be. */
5939 = cp_parser_constant_expression (parser,
5940 /*allow_non_constant=*/false,
5942 /* Look for the closing `]'. */
5943 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5945 /* Add this bound to the declarator. */
5946 declarator = make_array_declarator (declarator, expression);
5948 /* If the next token is not a `[', then there are no more
5950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5957 /* Parse a new-initializer.
5960 ( expression-list [opt] )
5963 Returns a representation of the expression-list. */
5965 static VEC(tree,gc) *
5966 cp_parser_new_initializer (cp_parser* parser)
5968 VEC(tree,gc) *expression_list;
5970 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5973 bool expr_non_constant_p;
5974 maybe_warn_cpp0x ("extended initializer lists");
5975 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5976 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5977 expression_list = make_tree_vector_single (t);
5980 expression_list = (cp_parser_parenthesized_expression_list
5981 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5982 /*non_constant_p=*/NULL));
5984 return expression_list;
5987 /* Parse a delete-expression.
5990 :: [opt] delete cast-expression
5991 :: [opt] delete [ ] cast-expression
5993 Returns a representation of the expression. */
5996 cp_parser_delete_expression (cp_parser* parser)
5998 bool global_scope_p;
6002 /* Look for the optional `::' operator. */
6004 = (cp_parser_global_scope_opt (parser,
6005 /*current_scope_valid_p=*/false)
6007 /* Look for the `delete' keyword. */
6008 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6009 /* See if the array syntax is in use. */
6010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6012 /* Consume the `[' token. */
6013 cp_lexer_consume_token (parser->lexer);
6014 /* Look for the `]' token. */
6015 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6016 /* Remember that this is the `[]' construct. */
6022 /* Parse the cast-expression. */
6023 expression = cp_parser_simple_cast_expression (parser);
6025 /* A delete-expression may not appear in an integral constant
6027 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6028 return error_mark_node;
6030 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6033 /* Returns true if TOKEN may start a cast-expression and false
6037 cp_parser_token_starts_cast_expression (cp_token *token)
6039 switch (token->type)
6045 case CPP_CLOSE_SQUARE:
6046 case CPP_CLOSE_PAREN:
6047 case CPP_CLOSE_BRACE:
6051 case CPP_DEREF_STAR:
6059 case CPP_GREATER_EQ:
6079 /* '[' may start a primary-expression in obj-c++. */
6080 case CPP_OPEN_SQUARE:
6081 return c_dialect_objc ();
6088 /* Parse a cast-expression.
6092 ( type-id ) cast-expression
6094 ADDRESS_P is true iff the unary-expression is appearing as the
6095 operand of the `&' operator. CAST_P is true if this expression is
6096 the target of a cast.
6098 Returns a representation of the expression. */
6101 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6104 /* If it's a `(', then we might be looking at a cast. */
6105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6107 tree type = NULL_TREE;
6108 tree expr = NULL_TREE;
6109 bool compound_literal_p;
6110 const char *saved_message;
6112 /* There's no way to know yet whether or not this is a cast.
6113 For example, `(int (3))' is a unary-expression, while `(int)
6114 3' is a cast. So, we resort to parsing tentatively. */
6115 cp_parser_parse_tentatively (parser);
6116 /* Types may not be defined in a cast. */
6117 saved_message = parser->type_definition_forbidden_message;
6118 parser->type_definition_forbidden_message
6119 = "types may not be defined in casts";
6120 /* Consume the `('. */
6121 cp_lexer_consume_token (parser->lexer);
6122 /* A very tricky bit is that `(struct S) { 3 }' is a
6123 compound-literal (which we permit in C++ as an extension).
6124 But, that construct is not a cast-expression -- it is a
6125 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6126 is legal; if the compound-literal were a cast-expression,
6127 you'd need an extra set of parentheses.) But, if we parse
6128 the type-id, and it happens to be a class-specifier, then we
6129 will commit to the parse at that point, because we cannot
6130 undo the action that is done when creating a new class. So,
6131 then we cannot back up and do a postfix-expression.
6133 Therefore, we scan ahead to the closing `)', and check to see
6134 if the token after the `)' is a `{'. If so, we are not
6135 looking at a cast-expression.
6137 Save tokens so that we can put them back. */
6138 cp_lexer_save_tokens (parser->lexer);
6139 /* Skip tokens until the next token is a closing parenthesis.
6140 If we find the closing `)', and the next token is a `{', then
6141 we are looking at a compound-literal. */
6143 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6144 /*consume_paren=*/true)
6145 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6146 /* Roll back the tokens we skipped. */
6147 cp_lexer_rollback_tokens (parser->lexer);
6148 /* If we were looking at a compound-literal, simulate an error
6149 so that the call to cp_parser_parse_definitely below will
6151 if (compound_literal_p)
6152 cp_parser_simulate_error (parser);
6155 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6156 parser->in_type_id_in_expr_p = true;
6157 /* Look for the type-id. */
6158 type = cp_parser_type_id (parser);
6159 /* Look for the closing `)'. */
6160 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6161 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6164 /* Restore the saved message. */
6165 parser->type_definition_forbidden_message = saved_message;
6167 /* At this point this can only be either a cast or a
6168 parenthesized ctor such as `(T ())' that looks like a cast to
6169 function returning T. */
6170 if (!cp_parser_error_occurred (parser)
6171 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6174 cp_parser_parse_definitely (parser);
6175 expr = cp_parser_cast_expression (parser,
6176 /*address_p=*/false,
6177 /*cast_p=*/true, pidk);
6179 /* Warn about old-style casts, if so requested. */
6180 if (warn_old_style_cast
6181 && !in_system_header
6182 && !VOID_TYPE_P (type)
6183 && current_lang_name != lang_name_c)
6184 warning (OPT_Wold_style_cast, "use of old-style cast");
6186 /* Only type conversions to integral or enumeration types
6187 can be used in constant-expressions. */
6188 if (!cast_valid_in_integral_constant_expression_p (type)
6189 && (cp_parser_non_integral_constant_expression
6191 "a cast to a type other than an integral or "
6192 "enumeration type")))
6193 return error_mark_node;
6195 /* Perform the cast. */
6196 expr = build_c_cast (input_location, type, expr);
6200 cp_parser_abort_tentative_parse (parser);
6203 /* If we get here, then it's not a cast, so it must be a
6204 unary-expression. */
6205 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6208 /* Parse a binary expression of the general form:
6212 pm-expression .* cast-expression
6213 pm-expression ->* cast-expression
6215 multiplicative-expression:
6217 multiplicative-expression * pm-expression
6218 multiplicative-expression / pm-expression
6219 multiplicative-expression % pm-expression
6221 additive-expression:
6222 multiplicative-expression
6223 additive-expression + multiplicative-expression
6224 additive-expression - multiplicative-expression
6228 shift-expression << additive-expression
6229 shift-expression >> additive-expression
6231 relational-expression:
6233 relational-expression < shift-expression
6234 relational-expression > shift-expression
6235 relational-expression <= shift-expression
6236 relational-expression >= shift-expression
6240 relational-expression:
6241 relational-expression <? shift-expression
6242 relational-expression >? shift-expression
6244 equality-expression:
6245 relational-expression
6246 equality-expression == relational-expression
6247 equality-expression != relational-expression
6251 and-expression & equality-expression
6253 exclusive-or-expression:
6255 exclusive-or-expression ^ and-expression
6257 inclusive-or-expression:
6258 exclusive-or-expression
6259 inclusive-or-expression | exclusive-or-expression
6261 logical-and-expression:
6262 inclusive-or-expression
6263 logical-and-expression && inclusive-or-expression
6265 logical-or-expression:
6266 logical-and-expression
6267 logical-or-expression || logical-and-expression
6269 All these are implemented with a single function like:
6272 simple-cast-expression
6273 binary-expression <token> binary-expression
6275 CAST_P is true if this expression is the target of a cast.
6277 The binops_by_token map is used to get the tree codes for each <token> type.
6278 binary-expressions are associated according to a precedence table. */
6280 #define TOKEN_PRECEDENCE(token) \
6281 (((token->type == CPP_GREATER \
6282 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6283 && !parser->greater_than_is_operator_p) \
6284 ? PREC_NOT_OPERATOR \
6285 : binops_by_token[token->type].prec)
6288 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6289 bool no_toplevel_fold_p,
6290 enum cp_parser_prec prec,
6293 cp_parser_expression_stack stack;
6294 cp_parser_expression_stack_entry *sp = &stack[0];
6297 enum tree_code tree_type, lhs_type, rhs_type;
6298 enum cp_parser_prec new_prec, lookahead_prec;
6301 /* Parse the first expression. */
6302 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6303 lhs_type = ERROR_MARK;
6307 /* Get an operator token. */
6308 token = cp_lexer_peek_token (parser->lexer);
6310 if (warn_cxx0x_compat
6311 && token->type == CPP_RSHIFT
6312 && !parser->greater_than_is_operator_p)
6314 if (warning_at (token->location, OPT_Wc__0x_compat,
6315 "%<>>%> operator will be treated as"
6316 " two right angle brackets in C++0x"))
6317 inform (token->location,
6318 "suggest parentheses around %<>>%> expression");
6321 new_prec = TOKEN_PRECEDENCE (token);
6323 /* Popping an entry off the stack means we completed a subexpression:
6324 - either we found a token which is not an operator (`>' where it is not
6325 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6326 will happen repeatedly;
6327 - or, we found an operator which has lower priority. This is the case
6328 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6330 if (new_prec <= prec)
6339 tree_type = binops_by_token[token->type].tree_type;
6341 /* We used the operator token. */
6342 cp_lexer_consume_token (parser->lexer);
6344 /* For "false && x" or "true || x", x will never be executed;
6345 disable warnings while evaluating it. */
6346 if (tree_type == TRUTH_ANDIF_EXPR)
6347 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6348 else if (tree_type == TRUTH_ORIF_EXPR)
6349 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6351 /* Extract another operand. It may be the RHS of this expression
6352 or the LHS of a new, higher priority expression. */
6353 rhs = cp_parser_simple_cast_expression (parser);
6354 rhs_type = ERROR_MARK;
6356 /* Get another operator token. Look up its precedence to avoid
6357 building a useless (immediately popped) stack entry for common
6358 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6359 token = cp_lexer_peek_token (parser->lexer);
6360 lookahead_prec = TOKEN_PRECEDENCE (token);
6361 if (lookahead_prec > new_prec)
6363 /* ... and prepare to parse the RHS of the new, higher priority
6364 expression. Since precedence levels on the stack are
6365 monotonically increasing, we do not have to care about
6368 sp->tree_type = tree_type;
6370 sp->lhs_type = lhs_type;
6373 lhs_type = rhs_type;
6375 new_prec = lookahead_prec;
6379 lookahead_prec = new_prec;
6380 /* If the stack is not empty, we have parsed into LHS the right side
6381 (`4' in the example above) of an expression we had suspended.
6382 We can use the information on the stack to recover the LHS (`3')
6383 from the stack together with the tree code (`MULT_EXPR'), and
6384 the precedence of the higher level subexpression
6385 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6386 which will be used to actually build the additive expression. */
6389 tree_type = sp->tree_type;
6391 rhs_type = lhs_type;
6393 lhs_type = sp->lhs_type;
6396 /* Undo the disabling of warnings done above. */
6397 if (tree_type == TRUTH_ANDIF_EXPR)
6398 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6399 else if (tree_type == TRUTH_ORIF_EXPR)
6400 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6402 overloaded_p = false;
6403 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6404 ERROR_MARK for everything that is not a binary expression.
6405 This makes warn_about_parentheses miss some warnings that
6406 involve unary operators. For unary expressions we should
6407 pass the correct tree_code unless the unary expression was
6408 surrounded by parentheses.
6410 if (no_toplevel_fold_p
6411 && lookahead_prec <= prec
6413 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6414 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6416 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6417 &overloaded_p, tf_warning_or_error);
6418 lhs_type = tree_type;
6420 /* If the binary operator required the use of an overloaded operator,
6421 then this expression cannot be an integral constant-expression.
6422 An overloaded operator can be used even if both operands are
6423 otherwise permissible in an integral constant-expression if at
6424 least one of the operands is of enumeration type. */
6427 && (cp_parser_non_integral_constant_expression
6428 (parser, "calls to overloaded operators")))
6429 return error_mark_node;
6436 /* Parse the `? expression : assignment-expression' part of a
6437 conditional-expression. The LOGICAL_OR_EXPR is the
6438 logical-or-expression that started the conditional-expression.
6439 Returns a representation of the entire conditional-expression.
6441 This routine is used by cp_parser_assignment_expression.
6443 ? expression : assignment-expression
6447 ? : assignment-expression */
6450 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6453 tree assignment_expr;
6455 /* Consume the `?' token. */
6456 cp_lexer_consume_token (parser->lexer);
6457 if (cp_parser_allow_gnu_extensions_p (parser)
6458 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6460 /* Implicit true clause. */
6462 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6466 /* Parse the expression. */
6467 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6468 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6469 c_inhibit_evaluation_warnings +=
6470 ((logical_or_expr == truthvalue_true_node)
6471 - (logical_or_expr == truthvalue_false_node));
6474 /* The next token should be a `:'. */
6475 cp_parser_require (parser, CPP_COLON, "%<:%>");
6476 /* Parse the assignment-expression. */
6477 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6478 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6480 /* Build the conditional-expression. */
6481 return build_x_conditional_expr (logical_or_expr,
6484 tf_warning_or_error);
6487 /* Parse an assignment-expression.
6489 assignment-expression:
6490 conditional-expression
6491 logical-or-expression assignment-operator assignment_expression
6494 CAST_P is true if this expression is the target of a cast.
6496 Returns a representation for the expression. */
6499 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6504 /* If the next token is the `throw' keyword, then we're looking at
6505 a throw-expression. */
6506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6507 expr = cp_parser_throw_expression (parser);
6508 /* Otherwise, it must be that we are looking at a
6509 logical-or-expression. */
6512 /* Parse the binary expressions (logical-or-expression). */
6513 expr = cp_parser_binary_expression (parser, cast_p, false,
6514 PREC_NOT_OPERATOR, pidk);
6515 /* If the next token is a `?' then we're actually looking at a
6516 conditional-expression. */
6517 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6518 return cp_parser_question_colon_clause (parser, expr);
6521 enum tree_code assignment_operator;
6523 /* If it's an assignment-operator, we're using the second
6526 = cp_parser_assignment_operator_opt (parser);
6527 if (assignment_operator != ERROR_MARK)
6529 bool non_constant_p;
6531 /* Parse the right-hand side of the assignment. */
6532 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6534 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6535 maybe_warn_cpp0x ("extended initializer lists");
6537 /* An assignment may not appear in a
6538 constant-expression. */
6539 if (cp_parser_non_integral_constant_expression (parser,
6541 return error_mark_node;
6542 /* Build the assignment expression. */
6543 expr = build_x_modify_expr (expr,
6544 assignment_operator,
6546 tf_warning_or_error);
6554 /* Parse an (optional) assignment-operator.
6556 assignment-operator: one of
6557 = *= /= %= += -= >>= <<= &= ^= |=
6561 assignment-operator: one of
6564 If the next token is an assignment operator, the corresponding tree
6565 code is returned, and the token is consumed. For example, for
6566 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6567 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6568 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6569 operator, ERROR_MARK is returned. */
6571 static enum tree_code
6572 cp_parser_assignment_operator_opt (cp_parser* parser)
6577 /* Peek at the next token. */
6578 token = cp_lexer_peek_token (parser->lexer);
6580 switch (token->type)
6591 op = TRUNC_DIV_EXPR;
6595 op = TRUNC_MOD_EXPR;
6627 /* Nothing else is an assignment operator. */
6631 /* If it was an assignment operator, consume it. */
6632 if (op != ERROR_MARK)
6633 cp_lexer_consume_token (parser->lexer);
6638 /* Parse an expression.
6641 assignment-expression
6642 expression , assignment-expression
6644 CAST_P is true if this expression is the target of a cast.
6646 Returns a representation of the expression. */
6649 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6651 tree expression = NULL_TREE;
6655 tree assignment_expression;
6657 /* Parse the next assignment-expression. */
6658 assignment_expression
6659 = cp_parser_assignment_expression (parser, cast_p, pidk);
6660 /* If this is the first assignment-expression, we can just
6663 expression = assignment_expression;
6665 expression = build_x_compound_expr (expression,
6666 assignment_expression,
6667 tf_warning_or_error);
6668 /* If the next token is not a comma, then we are done with the
6670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6672 /* Consume the `,'. */
6673 cp_lexer_consume_token (parser->lexer);
6674 /* A comma operator cannot appear in a constant-expression. */
6675 if (cp_parser_non_integral_constant_expression (parser,
6676 "a comma operator"))
6677 expression = error_mark_node;
6683 /* Parse a constant-expression.
6685 constant-expression:
6686 conditional-expression
6688 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6689 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6690 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6691 is false, NON_CONSTANT_P should be NULL. */
6694 cp_parser_constant_expression (cp_parser* parser,
6695 bool allow_non_constant_p,
6696 bool *non_constant_p)
6698 bool saved_integral_constant_expression_p;
6699 bool saved_allow_non_integral_constant_expression_p;
6700 bool saved_non_integral_constant_expression_p;
6703 /* It might seem that we could simply parse the
6704 conditional-expression, and then check to see if it were
6705 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6706 one that the compiler can figure out is constant, possibly after
6707 doing some simplifications or optimizations. The standard has a
6708 precise definition of constant-expression, and we must honor
6709 that, even though it is somewhat more restrictive.
6715 is not a legal declaration, because `(2, 3)' is not a
6716 constant-expression. The `,' operator is forbidden in a
6717 constant-expression. However, GCC's constant-folding machinery
6718 will fold this operation to an INTEGER_CST for `3'. */
6720 /* Save the old settings. */
6721 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6722 saved_allow_non_integral_constant_expression_p
6723 = parser->allow_non_integral_constant_expression_p;
6724 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6725 /* We are now parsing a constant-expression. */
6726 parser->integral_constant_expression_p = true;
6727 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6728 parser->non_integral_constant_expression_p = false;
6729 /* Although the grammar says "conditional-expression", we parse an
6730 "assignment-expression", which also permits "throw-expression"
6731 and the use of assignment operators. In the case that
6732 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6733 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6734 actually essential that we look for an assignment-expression.
6735 For example, cp_parser_initializer_clauses uses this function to
6736 determine whether a particular assignment-expression is in fact
6738 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6739 /* Restore the old settings. */
6740 parser->integral_constant_expression_p
6741 = saved_integral_constant_expression_p;
6742 parser->allow_non_integral_constant_expression_p
6743 = saved_allow_non_integral_constant_expression_p;
6744 if (allow_non_constant_p)
6745 *non_constant_p = parser->non_integral_constant_expression_p;
6746 else if (parser->non_integral_constant_expression_p)
6747 expression = error_mark_node;
6748 parser->non_integral_constant_expression_p
6749 = saved_non_integral_constant_expression_p;
6754 /* Parse __builtin_offsetof.
6756 offsetof-expression:
6757 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6759 offsetof-member-designator:
6761 | offsetof-member-designator "." id-expression
6762 | offsetof-member-designator "[" expression "]"
6763 | offsetof-member-designator "->" id-expression */
6766 cp_parser_builtin_offsetof (cp_parser *parser)
6768 int save_ice_p, save_non_ice_p;
6773 /* We're about to accept non-integral-constant things, but will
6774 definitely yield an integral constant expression. Save and
6775 restore these values around our local parsing. */
6776 save_ice_p = parser->integral_constant_expression_p;
6777 save_non_ice_p = parser->non_integral_constant_expression_p;
6779 /* Consume the "__builtin_offsetof" token. */
6780 cp_lexer_consume_token (parser->lexer);
6781 /* Consume the opening `('. */
6782 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6783 /* Parse the type-id. */
6784 type = cp_parser_type_id (parser);
6785 /* Look for the `,'. */
6786 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6787 token = cp_lexer_peek_token (parser->lexer);
6789 /* Build the (type *)null that begins the traditional offsetof macro. */
6790 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6791 tf_warning_or_error);
6793 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6794 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6795 true, &dummy, token->location);
6798 token = cp_lexer_peek_token (parser->lexer);
6799 switch (token->type)
6801 case CPP_OPEN_SQUARE:
6802 /* offsetof-member-designator "[" expression "]" */
6803 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6807 /* offsetof-member-designator "->" identifier */
6808 expr = grok_array_decl (expr, integer_zero_node);
6812 /* offsetof-member-designator "." identifier */
6813 cp_lexer_consume_token (parser->lexer);
6814 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6819 case CPP_CLOSE_PAREN:
6820 /* Consume the ")" token. */
6821 cp_lexer_consume_token (parser->lexer);
6825 /* Error. We know the following require will fail, but
6826 that gives the proper error message. */
6827 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6828 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6829 expr = error_mark_node;
6835 /* If we're processing a template, we can't finish the semantics yet.
6836 Otherwise we can fold the entire expression now. */
6837 if (processing_template_decl)
6838 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6840 expr = finish_offsetof (expr);
6843 parser->integral_constant_expression_p = save_ice_p;
6844 parser->non_integral_constant_expression_p = save_non_ice_p;
6849 /* Parse a trait expression. */
6852 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6855 tree type1, type2 = NULL_TREE;
6856 bool binary = false;
6857 cp_decl_specifier_seq decl_specs;
6861 case RID_HAS_NOTHROW_ASSIGN:
6862 kind = CPTK_HAS_NOTHROW_ASSIGN;
6864 case RID_HAS_NOTHROW_CONSTRUCTOR:
6865 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6867 case RID_HAS_NOTHROW_COPY:
6868 kind = CPTK_HAS_NOTHROW_COPY;
6870 case RID_HAS_TRIVIAL_ASSIGN:
6871 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6873 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6874 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6876 case RID_HAS_TRIVIAL_COPY:
6877 kind = CPTK_HAS_TRIVIAL_COPY;
6879 case RID_HAS_TRIVIAL_DESTRUCTOR:
6880 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6882 case RID_HAS_VIRTUAL_DESTRUCTOR:
6883 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6885 case RID_IS_ABSTRACT:
6886 kind = CPTK_IS_ABSTRACT;
6888 case RID_IS_BASE_OF:
6889 kind = CPTK_IS_BASE_OF;
6893 kind = CPTK_IS_CLASS;
6895 case RID_IS_CONVERTIBLE_TO:
6896 kind = CPTK_IS_CONVERTIBLE_TO;
6900 kind = CPTK_IS_EMPTY;
6903 kind = CPTK_IS_ENUM;
6908 case RID_IS_POLYMORPHIC:
6909 kind = CPTK_IS_POLYMORPHIC;
6911 case RID_IS_STD_LAYOUT:
6912 kind = CPTK_IS_STD_LAYOUT;
6914 case RID_IS_TRIVIAL:
6915 kind = CPTK_IS_TRIVIAL;
6918 kind = CPTK_IS_UNION;
6924 /* Consume the token. */
6925 cp_lexer_consume_token (parser->lexer);
6927 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6929 type1 = cp_parser_type_id (parser);
6931 if (type1 == error_mark_node)
6932 return error_mark_node;
6934 /* Build a trivial decl-specifier-seq. */
6935 clear_decl_specs (&decl_specs);
6936 decl_specs.type = type1;
6938 /* Call grokdeclarator to figure out what type this is. */
6939 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6940 /*initialized=*/0, /*attrlist=*/NULL);
6944 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6946 type2 = cp_parser_type_id (parser);
6948 if (type2 == error_mark_node)
6949 return error_mark_node;
6951 /* Build a trivial decl-specifier-seq. */
6952 clear_decl_specs (&decl_specs);
6953 decl_specs.type = type2;
6955 /* Call grokdeclarator to figure out what type this is. */
6956 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6957 /*initialized=*/0, /*attrlist=*/NULL);
6960 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6962 /* Complete the trait expression, which may mean either processing
6963 the trait expr now or saving it for template instantiation. */
6964 return finish_trait_expr (kind, type1, type2);
6967 /* Lambdas that appear in variable initializer or default argument scope
6968 get that in their mangling, so we need to record it. We might as well
6969 use the count for function and namespace scopes as well. */
6970 static GTY(()) tree lambda_scope;
6971 static GTY(()) int lambda_count;
6972 typedef struct GTY(()) tree_int
6977 DEF_VEC_O(tree_int);
6978 DEF_VEC_ALLOC_O(tree_int,gc);
6979 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6982 start_lambda_scope (tree decl)
6986 /* Once we're inside a function, we ignore other scopes and just push
6987 the function again so that popping works properly. */
6988 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6989 decl = current_function_decl;
6990 ti.t = lambda_scope;
6991 ti.i = lambda_count;
6992 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6993 if (lambda_scope != decl)
6995 /* Don't reset the count if we're still in the same function. */
6996 lambda_scope = decl;
7002 record_lambda_scope (tree lambda)
7004 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7005 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7009 finish_lambda_scope (void)
7011 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7012 if (lambda_scope != p->t)
7014 lambda_scope = p->t;
7015 lambda_count = p->i;
7017 VEC_pop (tree_int, lambda_scope_stack);
7020 /* Parse a lambda expression.
7023 lambda-introducer lambda-declarator [opt] compound-statement
7025 Returns a representation of the expression. */
7028 cp_parser_lambda_expression (cp_parser* parser)
7030 tree lambda_expr = build_lambda_expr ();
7033 LAMBDA_EXPR_LOCATION (lambda_expr)
7034 = cp_lexer_peek_token (parser->lexer)->location;
7036 /* We may be in the middle of deferred access check. Disable
7038 push_deferring_access_checks (dk_no_deferred);
7040 type = begin_lambda_type (lambda_expr);
7042 record_lambda_scope (lambda_expr);
7044 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7045 determine_visibility (TYPE_NAME (type));
7048 /* Inside the class, surrounding template-parameter-lists do not apply. */
7049 unsigned int saved_num_template_parameter_lists
7050 = parser->num_template_parameter_lists;
7052 parser->num_template_parameter_lists = 0;
7054 cp_parser_lambda_introducer (parser, lambda_expr);
7056 /* By virtue of defining a local class, a lambda expression has access to
7057 the private variables of enclosing classes. */
7059 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7061 cp_parser_lambda_body (parser, lambda_expr);
7063 /* The capture list was built up in reverse order; fix that now. */
7065 tree newlist = NULL_TREE;
7068 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7071 tree field = TREE_PURPOSE (elt);
7074 next = TREE_CHAIN (elt);
7075 TREE_CHAIN (elt) = newlist;
7078 /* Also add __ to the beginning of the field name so that code
7079 outside the lambda body can't see the captured name. We could
7080 just remove the name entirely, but this is more useful for
7082 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7083 /* The 'this' capture already starts with __. */
7086 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7087 buf[1] = buf[0] = '_';
7088 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7089 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7090 DECL_NAME (field) = get_identifier (buf);
7092 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7095 maybe_add_lambda_conv_op (type);
7097 type = finish_struct (type, /*attributes=*/NULL_TREE);
7099 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7102 pop_deferring_access_checks ();
7104 return build_lambda_object (lambda_expr);
7107 /* Parse the beginning of a lambda expression.
7110 [ lambda-capture [opt] ]
7112 LAMBDA_EXPR is the current representation of the lambda expression. */
7115 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7117 /* Need commas after the first capture. */
7120 /* Eat the leading `['. */
7121 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7123 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7124 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7125 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7126 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7127 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7128 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7130 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7132 cp_lexer_consume_token (parser->lexer);
7136 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7138 cp_token* capture_token;
7140 tree capture_init_expr;
7141 cp_id_kind idk = CP_ID_KIND_NONE;
7142 bool explicit_init_p = false;
7144 enum capture_kind_type
7149 enum capture_kind_type capture_kind = BY_COPY;
7151 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7153 error ("expected end of capture-list");
7160 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7162 /* Possibly capture `this'. */
7163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7165 cp_lexer_consume_token (parser->lexer);
7166 add_capture (lambda_expr,
7167 /*id=*/get_identifier ("__this"),
7168 /*initializer=*/finish_this_expr(),
7169 /*by_reference_p=*/false,
7174 /* Remember whether we want to capture as a reference or not. */
7175 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7177 capture_kind = BY_REFERENCE;
7178 cp_lexer_consume_token (parser->lexer);
7181 /* Get the identifier. */
7182 capture_token = cp_lexer_peek_token (parser->lexer);
7183 capture_id = cp_parser_identifier (parser);
7185 if (capture_id == error_mark_node)
7186 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7187 delimiters, but I modified this to stop on unnested ']' as well. It
7188 was already changed to stop on unnested '}', so the
7189 "closing_parenthesis" name is no more misleading with my change. */
7191 cp_parser_skip_to_closing_parenthesis (parser,
7192 /*recovering=*/true,
7194 /*consume_paren=*/true);
7198 /* Find the initializer for this capture. */
7199 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7201 /* An explicit expression exists. */
7202 cp_lexer_consume_token (parser->lexer);
7203 pedwarn (input_location, OPT_pedantic,
7204 "ISO C++ does not allow initializers "
7205 "in lambda expression capture lists");
7206 capture_init_expr = cp_parser_assignment_expression (parser,
7209 explicit_init_p = true;
7213 const char* error_msg;
7215 /* Turn the identifier into an id-expression. */
7217 = cp_parser_lookup_name
7221 /*is_template=*/false,
7222 /*is_namespace=*/false,
7223 /*check_dependency=*/true,
7224 /*ambiguous_decls=*/NULL,
7225 capture_token->location);
7228 = finish_id_expression
7233 /*integral_constant_expression_p=*/false,
7234 /*allow_non_integral_constant_expression_p=*/false,
7235 /*non_integral_constant_expression_p=*/NULL,
7236 /*template_p=*/false,
7238 /*address_p=*/false,
7239 /*template_arg_p=*/false,
7241 capture_token->location);
7244 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7246 = unqualified_name_lookup_error (capture_init_expr);
7248 add_capture (lambda_expr,
7251 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7255 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7258 /* Parse the (optional) middle of a lambda expression.
7261 ( parameter-declaration-clause [opt] )
7262 attribute-specifier [opt]
7264 exception-specification [opt]
7265 lambda-return-type-clause [opt]
7267 LAMBDA_EXPR is the current representation of the lambda expression. */
7270 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7272 /* 5.1.1.4 of the standard says:
7273 If a lambda-expression does not include a lambda-declarator, it is as if
7274 the lambda-declarator were ().
7275 This means an empty parameter list, no attributes, and no exception
7277 tree param_list = void_list_node;
7278 tree attributes = NULL_TREE;
7279 tree exception_spec = NULL_TREE;
7282 /* The lambda-declarator is optional, but must begin with an opening
7283 parenthesis if present. */
7284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7286 cp_lexer_consume_token (parser->lexer);
7288 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7290 /* Parse parameters. */
7291 param_list = cp_parser_parameter_declaration_clause (parser);
7293 /* Default arguments shall not be specified in the
7294 parameter-declaration-clause of a lambda-declarator. */
7295 for (t = param_list; t; t = TREE_CHAIN (t))
7296 if (TREE_PURPOSE (t))
7297 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7298 "default argument specified for lambda parameter");
7300 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7302 attributes = cp_parser_attributes_opt (parser);
7304 /* Parse optional `mutable' keyword. */
7305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7307 cp_lexer_consume_token (parser->lexer);
7308 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7311 /* Parse optional exception specification. */
7312 exception_spec = cp_parser_exception_specification_opt (parser);
7314 /* Parse optional trailing return type. */
7315 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7317 cp_lexer_consume_token (parser->lexer);
7318 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7321 /* The function parameters must be in scope all the way until after the
7322 trailing-return-type in case of decltype. */
7323 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7324 pop_binding (DECL_NAME (t), t);
7329 /* Create the function call operator.
7331 Messing with declarators like this is no uglier than building up the
7332 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7335 cp_decl_specifier_seq return_type_specs;
7336 cp_declarator* declarator;
7341 clear_decl_specs (&return_type_specs);
7342 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7343 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7345 /* Maybe we will deduce the return type later, but we can use void
7346 as a placeholder return type anyways. */
7347 return_type_specs.type = void_type_node;
7349 p = obstack_alloc (&declarator_obstack, 0);
7351 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7354 quals = TYPE_UNQUALIFIED;
7355 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7356 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7358 /* A lambda with no captures has a static op() and a conversion op
7359 to function type. */
7360 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7361 error ("lambda expression with no captures declared mutable");
7362 return_type_specs.storage_class = sc_static;
7364 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7365 quals = TYPE_QUAL_CONST;
7366 declarator = make_call_declarator (declarator, param_list, quals,
7368 /*late_return_type=*/NULL_TREE);
7370 fco = grokmethod (&return_type_specs,
7373 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7374 DECL_ARTIFICIAL (fco) = 1;
7376 finish_member_declaration (fco);
7378 obstack_free (&declarator_obstack, p);
7382 /* Parse the body of a lambda expression, which is simply
7386 but which requires special handling.
7387 LAMBDA_EXPR is the current representation of the lambda expression. */
7390 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7392 bool nested = (current_function_decl != NULL_TREE);
7394 push_function_context ();
7396 /* Finish the function call operator
7398 + late_parsing_for_member
7399 + function_definition_after_declarator
7400 + ctor_initializer_opt_and_function_body */
7402 tree fco = lambda_function (lambda_expr);
7406 /* Let the front end know that we are going to be defining this
7408 start_preparsed_function (fco,
7410 SF_PRE_PARSED | SF_INCLASS_INLINE);
7412 start_lambda_scope (fco);
7413 body = begin_function_body ();
7415 /* 5.1.1.4 of the standard says:
7416 If a lambda-expression does not include a trailing-return-type, it
7417 is as if the trailing-return-type denotes the following type:
7418 * if the compound-statement is of the form
7419 { return attribute-specifier [opt] expression ; }
7420 the type of the returned expression after lvalue-to-rvalue
7421 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7422 (_conv.array_ 4.2), and function-to-pointer conversion
7424 * otherwise, void. */
7426 /* In a lambda that has neither a lambda-return-type-clause
7427 nor a deducible form, errors should be reported for return statements
7428 in the body. Since we used void as the placeholder return type, parsing
7429 the body as usual will give such desired behavior. */
7430 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7431 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7432 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7433 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7436 tree expr = NULL_TREE;
7437 cp_id_kind idk = CP_ID_KIND_NONE;
7439 /* Parse tentatively in case there's more after the initial return
7441 cp_parser_parse_tentatively (parser);
7443 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7444 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7446 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7448 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7449 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7451 if (cp_parser_parse_definitely (parser))
7453 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7455 compound_stmt = begin_compound_stmt (0);
7456 /* Will get error here if type not deduced yet. */
7457 finish_return_stmt (expr);
7458 finish_compound_stmt (compound_stmt);
7466 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7467 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7468 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7469 cp_parser_compound_stmt does not pass it. */
7470 cp_parser_function_body (parser);
7471 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7474 finish_function_body (body);
7475 finish_lambda_scope ();
7477 /* Finish the function and generate code for it if necessary. */
7478 expand_or_defer_fn (finish_function (/*inline*/2));
7482 pop_function_context();
7485 /* Statements [gram.stmt.stmt] */
7487 /* Parse a statement.
7491 expression-statement
7496 declaration-statement
7499 IN_COMPOUND is true when the statement is nested inside a
7500 cp_parser_compound_statement; this matters for certain pragmas.
7502 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7503 is a (possibly labeled) if statement which is not enclosed in braces
7504 and has an else clause. This is used to implement -Wparentheses. */
7507 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7508 bool in_compound, bool *if_p)
7512 location_t statement_location;
7517 /* There is no statement yet. */
7518 statement = NULL_TREE;
7519 /* Peek at the next token. */
7520 token = cp_lexer_peek_token (parser->lexer);
7521 /* Remember the location of the first token in the statement. */
7522 statement_location = token->location;
7523 /* If this is a keyword, then that will often determine what kind of
7524 statement we have. */
7525 if (token->type == CPP_KEYWORD)
7527 enum rid keyword = token->keyword;
7533 /* Looks like a labeled-statement with a case label.
7534 Parse the label, and then use tail recursion to parse
7536 cp_parser_label_for_labeled_statement (parser);
7541 statement = cp_parser_selection_statement (parser, if_p);
7547 statement = cp_parser_iteration_statement (parser);
7554 statement = cp_parser_jump_statement (parser);
7557 /* Objective-C++ exception-handling constructs. */
7560 case RID_AT_FINALLY:
7561 case RID_AT_SYNCHRONIZED:
7563 statement = cp_parser_objc_statement (parser);
7567 statement = cp_parser_try_block (parser);
7571 /* This must be a namespace alias definition. */
7572 cp_parser_declaration_statement (parser);
7576 /* It might be a keyword like `int' that can start a
7577 declaration-statement. */
7581 else if (token->type == CPP_NAME)
7583 /* If the next token is a `:', then we are looking at a
7584 labeled-statement. */
7585 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7586 if (token->type == CPP_COLON)
7588 /* Looks like a labeled-statement with an ordinary label.
7589 Parse the label, and then use tail recursion to parse
7591 cp_parser_label_for_labeled_statement (parser);
7595 /* Anything that starts with a `{' must be a compound-statement. */
7596 else if (token->type == CPP_OPEN_BRACE)
7597 statement = cp_parser_compound_statement (parser, NULL, false);
7598 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7599 a statement all its own. */
7600 else if (token->type == CPP_PRAGMA)
7602 /* Only certain OpenMP pragmas are attached to statements, and thus
7603 are considered statements themselves. All others are not. In
7604 the context of a compound, accept the pragma as a "statement" and
7605 return so that we can check for a close brace. Otherwise we
7606 require a real statement and must go back and read one. */
7608 cp_parser_pragma (parser, pragma_compound);
7609 else if (!cp_parser_pragma (parser, pragma_stmt))
7613 else if (token->type == CPP_EOF)
7615 cp_parser_error (parser, "expected statement");
7619 /* Everything else must be a declaration-statement or an
7620 expression-statement. Try for the declaration-statement
7621 first, unless we are looking at a `;', in which case we know that
7622 we have an expression-statement. */
7625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7627 cp_parser_parse_tentatively (parser);
7628 /* Try to parse the declaration-statement. */
7629 cp_parser_declaration_statement (parser);
7630 /* If that worked, we're done. */
7631 if (cp_parser_parse_definitely (parser))
7634 /* Look for an expression-statement instead. */
7635 statement = cp_parser_expression_statement (parser, in_statement_expr);
7638 /* Set the line number for the statement. */
7639 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7640 SET_EXPR_LOCATION (statement, statement_location);
7643 /* Parse the label for a labeled-statement, i.e.
7646 case constant-expression :
7650 case constant-expression ... constant-expression : statement
7652 When a label is parsed without errors, the label is added to the
7653 parse tree by the finish_* functions, so this function doesn't
7654 have to return the label. */
7657 cp_parser_label_for_labeled_statement (cp_parser* parser)
7660 tree label = NULL_TREE;
7662 /* The next token should be an identifier. */
7663 token = cp_lexer_peek_token (parser->lexer);
7664 if (token->type != CPP_NAME
7665 && token->type != CPP_KEYWORD)
7667 cp_parser_error (parser, "expected labeled-statement");
7671 switch (token->keyword)
7678 /* Consume the `case' token. */
7679 cp_lexer_consume_token (parser->lexer);
7680 /* Parse the constant-expression. */
7681 expr = cp_parser_constant_expression (parser,
7682 /*allow_non_constant_p=*/false,
7685 ellipsis = cp_lexer_peek_token (parser->lexer);
7686 if (ellipsis->type == CPP_ELLIPSIS)
7688 /* Consume the `...' token. */
7689 cp_lexer_consume_token (parser->lexer);
7691 cp_parser_constant_expression (parser,
7692 /*allow_non_constant_p=*/false,
7694 /* We don't need to emit warnings here, as the common code
7695 will do this for us. */
7698 expr_hi = NULL_TREE;
7700 if (parser->in_switch_statement_p)
7701 finish_case_label (token->location, expr, expr_hi);
7703 error_at (token->location,
7704 "case label %qE not within a switch statement",
7710 /* Consume the `default' token. */
7711 cp_lexer_consume_token (parser->lexer);
7713 if (parser->in_switch_statement_p)
7714 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7716 error_at (token->location, "case label not within a switch statement");
7720 /* Anything else must be an ordinary label. */
7721 label = finish_label_stmt (cp_parser_identifier (parser));
7725 /* Require the `:' token. */
7726 cp_parser_require (parser, CPP_COLON, "%<:%>");
7728 /* An ordinary label may optionally be followed by attributes.
7729 However, this is only permitted if the attributes are then
7730 followed by a semicolon. This is because, for backward
7731 compatibility, when parsing
7732 lab: __attribute__ ((unused)) int i;
7733 we want the attribute to attach to "i", not "lab". */
7734 if (label != NULL_TREE
7735 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7739 cp_parser_parse_tentatively (parser);
7740 attrs = cp_parser_attributes_opt (parser);
7741 if (attrs == NULL_TREE
7742 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7743 cp_parser_abort_tentative_parse (parser);
7744 else if (!cp_parser_parse_definitely (parser))
7747 cplus_decl_attributes (&label, attrs, 0);
7751 /* Parse an expression-statement.
7753 expression-statement:
7756 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7757 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7758 indicates whether this expression-statement is part of an
7759 expression statement. */
7762 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7764 tree statement = NULL_TREE;
7765 cp_token *token = cp_lexer_peek_token (parser->lexer);
7767 /* If the next token is a ';', then there is no expression
7769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7770 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7772 /* Give a helpful message for "A<T>::type t;" */
7773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7774 && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7775 && TREE_CODE (statement) == SCOPE_REF)
7776 error_at (token->location, "need %<typename%> before %qE to name "
7777 "a type in dependent scope %qT",
7778 statement, TREE_OPERAND (statement, 0));
7780 /* Consume the final `;'. */
7781 cp_parser_consume_semicolon_at_end_of_statement (parser);
7783 if (in_statement_expr
7784 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7785 /* This is the final expression statement of a statement
7787 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7789 statement = finish_expr_stmt (statement);
7796 /* Parse a compound-statement.
7799 { statement-seq [opt] }
7804 { label-declaration-seq [opt] statement-seq [opt] }
7806 label-declaration-seq:
7808 label-declaration-seq label-declaration
7810 Returns a tree representing the statement. */
7813 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7818 /* Consume the `{'. */
7819 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7820 return error_mark_node;
7821 /* Begin the compound-statement. */
7822 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7823 /* If the next keyword is `__label__' we have a label declaration. */
7824 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7825 cp_parser_label_declaration (parser);
7826 /* Parse an (optional) statement-seq. */
7827 cp_parser_statement_seq_opt (parser, in_statement_expr);
7828 /* Finish the compound-statement. */
7829 finish_compound_stmt (compound_stmt);
7830 /* Consume the `}'. */
7831 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7833 return compound_stmt;
7836 /* Parse an (optional) statement-seq.
7840 statement-seq [opt] statement */
7843 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7845 /* Scan statements until there aren't any more. */
7848 cp_token *token = cp_lexer_peek_token (parser->lexer);
7850 /* If we're looking at a `}', then we've run out of statements. */
7851 if (token->type == CPP_CLOSE_BRACE
7852 || token->type == CPP_EOF
7853 || token->type == CPP_PRAGMA_EOL)
7856 /* If we are in a compound statement and find 'else' then
7857 something went wrong. */
7858 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7860 if (parser->in_statement & IN_IF_STMT)
7864 token = cp_lexer_consume_token (parser->lexer);
7865 error_at (token->location, "%<else%> without a previous %<if%>");
7869 /* Parse the statement. */
7870 cp_parser_statement (parser, in_statement_expr, true, NULL);
7874 /* Parse a selection-statement.
7876 selection-statement:
7877 if ( condition ) statement
7878 if ( condition ) statement else statement
7879 switch ( condition ) statement
7881 Returns the new IF_STMT or SWITCH_STMT.
7883 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7884 is a (possibly labeled) if statement which is not enclosed in
7885 braces and has an else clause. This is used to implement
7889 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7897 /* Peek at the next token. */
7898 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7900 /* See what kind of keyword it is. */
7901 keyword = token->keyword;
7910 /* Look for the `('. */
7911 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7913 cp_parser_skip_to_end_of_statement (parser);
7914 return error_mark_node;
7917 /* Begin the selection-statement. */
7918 if (keyword == RID_IF)
7919 statement = begin_if_stmt ();
7921 statement = begin_switch_stmt ();
7923 /* Parse the condition. */
7924 condition = cp_parser_condition (parser);
7925 /* Look for the `)'. */
7926 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7927 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7928 /*consume_paren=*/true);
7930 if (keyword == RID_IF)
7933 unsigned char in_statement;
7935 /* Add the condition. */
7936 finish_if_stmt_cond (condition, statement);
7938 /* Parse the then-clause. */
7939 in_statement = parser->in_statement;
7940 parser->in_statement |= IN_IF_STMT;
7941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7943 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7944 add_stmt (build_empty_stmt (loc));
7945 cp_lexer_consume_token (parser->lexer);
7946 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7947 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7948 "empty body in an %<if%> statement");
7952 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7953 parser->in_statement = in_statement;
7955 finish_then_clause (statement);
7957 /* If the next token is `else', parse the else-clause. */
7958 if (cp_lexer_next_token_is_keyword (parser->lexer,
7961 /* Consume the `else' keyword. */
7962 cp_lexer_consume_token (parser->lexer);
7963 begin_else_clause (statement);
7964 /* Parse the else-clause. */
7965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7968 loc = cp_lexer_peek_token (parser->lexer)->location;
7970 OPT_Wempty_body, "suggest braces around "
7971 "empty body in an %<else%> statement");
7972 add_stmt (build_empty_stmt (loc));
7973 cp_lexer_consume_token (parser->lexer);
7976 cp_parser_implicitly_scoped_statement (parser, NULL);
7978 finish_else_clause (statement);
7980 /* If we are currently parsing a then-clause, then
7981 IF_P will not be NULL. We set it to true to
7982 indicate that this if statement has an else clause.
7983 This may trigger the Wparentheses warning below
7984 when we get back up to the parent if statement. */
7990 /* This if statement does not have an else clause. If
7991 NESTED_IF is true, then the then-clause is an if
7992 statement which does have an else clause. We warn
7993 about the potential ambiguity. */
7995 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7996 "suggest explicit braces to avoid ambiguous"
8000 /* Now we're all done with the if-statement. */
8001 finish_if_stmt (statement);
8005 bool in_switch_statement_p;
8006 unsigned char in_statement;
8008 /* Add the condition. */
8009 finish_switch_cond (condition, statement);
8011 /* Parse the body of the switch-statement. */
8012 in_switch_statement_p = parser->in_switch_statement_p;
8013 in_statement = parser->in_statement;
8014 parser->in_switch_statement_p = true;
8015 parser->in_statement |= IN_SWITCH_STMT;
8016 cp_parser_implicitly_scoped_statement (parser, NULL);
8017 parser->in_switch_statement_p = in_switch_statement_p;
8018 parser->in_statement = in_statement;
8020 /* Now we're all done with the switch-statement. */
8021 finish_switch_stmt (statement);
8029 cp_parser_error (parser, "expected selection-statement");
8030 return error_mark_node;
8034 /* Parse a condition.
8038 type-specifier-seq declarator = initializer-clause
8039 type-specifier-seq declarator braced-init-list
8044 type-specifier-seq declarator asm-specification [opt]
8045 attributes [opt] = assignment-expression
8047 Returns the expression that should be tested. */
8050 cp_parser_condition (cp_parser* parser)
8052 cp_decl_specifier_seq type_specifiers;
8053 const char *saved_message;
8055 /* Try the declaration first. */
8056 cp_parser_parse_tentatively (parser);
8057 /* New types are not allowed in the type-specifier-seq for a
8059 saved_message = parser->type_definition_forbidden_message;
8060 parser->type_definition_forbidden_message
8061 = "types may not be defined in conditions";
8062 /* Parse the type-specifier-seq. */
8063 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8064 /*is_trailing_return=*/false,
8066 /* Restore the saved message. */
8067 parser->type_definition_forbidden_message = saved_message;
8068 /* If all is well, we might be looking at a declaration. */
8069 if (!cp_parser_error_occurred (parser))
8072 tree asm_specification;
8074 cp_declarator *declarator;
8075 tree initializer = NULL_TREE;
8077 /* Parse the declarator. */
8078 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8079 /*ctor_dtor_or_conv_p=*/NULL,
8080 /*parenthesized_p=*/NULL,
8081 /*member_p=*/false);
8082 /* Parse the attributes. */
8083 attributes = cp_parser_attributes_opt (parser);
8084 /* Parse the asm-specification. */
8085 asm_specification = cp_parser_asm_specification_opt (parser);
8086 /* If the next token is not an `=' or '{', then we might still be
8087 looking at an expression. For example:
8091 looks like a decl-specifier-seq and a declarator -- but then
8092 there is no `=', so this is an expression. */
8093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8094 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8095 cp_parser_simulate_error (parser);
8097 /* If we did see an `=' or '{', then we are looking at a declaration
8099 if (cp_parser_parse_definitely (parser))
8102 bool non_constant_p;
8103 bool flags = LOOKUP_ONLYCONVERTING;
8105 /* Create the declaration. */
8106 decl = start_decl (declarator, &type_specifiers,
8107 /*initialized_p=*/true,
8108 attributes, /*prefix_attributes=*/NULL_TREE,
8111 /* Parse the initializer. */
8112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8114 initializer = cp_parser_braced_list (parser, &non_constant_p);
8115 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8120 /* Consume the `='. */
8121 cp_parser_require (parser, CPP_EQ, "%<=%>");
8122 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8124 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8125 maybe_warn_cpp0x ("extended initializer lists");
8127 if (!non_constant_p)
8128 initializer = fold_non_dependent_expr (initializer);
8130 /* Process the initializer. */
8131 cp_finish_decl (decl,
8132 initializer, !non_constant_p,
8137 pop_scope (pushed_scope);
8139 return convert_from_reference (decl);
8142 /* If we didn't even get past the declarator successfully, we are
8143 definitely not looking at a declaration. */
8145 cp_parser_abort_tentative_parse (parser);
8147 /* Otherwise, we are looking at an expression. */
8148 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8151 /* Parse an iteration-statement.
8153 iteration-statement:
8154 while ( condition ) statement
8155 do statement while ( expression ) ;
8156 for ( for-init-statement condition [opt] ; expression [opt] )
8159 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8162 cp_parser_iteration_statement (cp_parser* parser)
8167 unsigned char in_statement;
8169 /* Peek at the next token. */
8170 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8172 return error_mark_node;
8174 /* Remember whether or not we are already within an iteration
8176 in_statement = parser->in_statement;
8178 /* See what kind of keyword it is. */
8179 keyword = token->keyword;
8186 /* Begin the while-statement. */
8187 statement = begin_while_stmt ();
8188 /* Look for the `('. */
8189 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8190 /* Parse the condition. */
8191 condition = cp_parser_condition (parser);
8192 finish_while_stmt_cond (condition, statement);
8193 /* Look for the `)'. */
8194 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8195 /* Parse the dependent statement. */
8196 parser->in_statement = IN_ITERATION_STMT;
8197 cp_parser_already_scoped_statement (parser);
8198 parser->in_statement = in_statement;
8199 /* We're done with the while-statement. */
8200 finish_while_stmt (statement);
8208 /* Begin the do-statement. */
8209 statement = begin_do_stmt ();
8210 /* Parse the body of the do-statement. */
8211 parser->in_statement = IN_ITERATION_STMT;
8212 cp_parser_implicitly_scoped_statement (parser, NULL);
8213 parser->in_statement = in_statement;
8214 finish_do_body (statement);
8215 /* Look for the `while' keyword. */
8216 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8217 /* Look for the `('. */
8218 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8219 /* Parse the expression. */
8220 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8221 /* We're done with the do-statement. */
8222 finish_do_stmt (expression, statement);
8223 /* Look for the `)'. */
8224 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8225 /* Look for the `;'. */
8226 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8232 tree condition = NULL_TREE;
8233 tree expression = NULL_TREE;
8235 /* Begin the for-statement. */
8236 statement = begin_for_stmt ();
8237 /* Look for the `('. */
8238 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8239 /* Parse the initialization. */
8240 cp_parser_for_init_statement (parser);
8241 finish_for_init_stmt (statement);
8243 /* If there's a condition, process it. */
8244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8245 condition = cp_parser_condition (parser);
8246 finish_for_cond (condition, statement);
8247 /* Look for the `;'. */
8248 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8250 /* If there's an expression, process it. */
8251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8252 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8253 finish_for_expr (expression, statement);
8254 /* Look for the `)'. */
8255 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8257 /* Parse the body of the for-statement. */
8258 parser->in_statement = IN_ITERATION_STMT;
8259 cp_parser_already_scoped_statement (parser);
8260 parser->in_statement = in_statement;
8262 /* We're done with the for-statement. */
8263 finish_for_stmt (statement);
8268 cp_parser_error (parser, "expected iteration-statement");
8269 statement = error_mark_node;
8276 /* Parse a for-init-statement.
8279 expression-statement
8280 simple-declaration */
8283 cp_parser_for_init_statement (cp_parser* parser)
8285 /* If the next token is a `;', then we have an empty
8286 expression-statement. Grammatically, this is also a
8287 simple-declaration, but an invalid one, because it does not
8288 declare anything. Therefore, if we did not handle this case
8289 specially, we would issue an error message about an invalid
8291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8293 /* We're going to speculatively look for a declaration, falling back
8294 to an expression, if necessary. */
8295 cp_parser_parse_tentatively (parser);
8296 /* Parse the declaration. */
8297 cp_parser_simple_declaration (parser,
8298 /*function_definition_allowed_p=*/false);
8299 /* If the tentative parse failed, then we shall need to look for an
8300 expression-statement. */
8301 if (cp_parser_parse_definitely (parser))
8305 cp_parser_expression_statement (parser, false);
8308 /* Parse a jump-statement.
8313 return expression [opt] ;
8314 return braced-init-list ;
8322 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8325 cp_parser_jump_statement (cp_parser* parser)
8327 tree statement = error_mark_node;
8330 unsigned char in_statement;
8332 /* Peek at the next token. */
8333 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8335 return error_mark_node;
8337 /* See what kind of keyword it is. */
8338 keyword = token->keyword;
8342 in_statement = parser->in_statement & ~IN_IF_STMT;
8343 switch (in_statement)
8346 error_at (token->location, "break statement not within loop or switch");
8349 gcc_assert ((in_statement & IN_SWITCH_STMT)
8350 || in_statement == IN_ITERATION_STMT);
8351 statement = finish_break_stmt ();
8354 error_at (token->location, "invalid exit from OpenMP structured block");
8357 error_at (token->location, "break statement used with OpenMP for loop");
8360 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8364 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8367 error_at (token->location, "continue statement not within a loop");
8369 case IN_ITERATION_STMT:
8371 statement = finish_continue_stmt ();
8374 error_at (token->location, "invalid exit from OpenMP structured block");
8379 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8385 bool expr_non_constant_p;
8387 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8389 maybe_warn_cpp0x ("extended initializer lists");
8390 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8392 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8393 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8395 /* If the next token is a `;', then there is no
8398 /* Build the return-statement. */
8399 statement = finish_return_stmt (expr);
8400 /* Look for the final `;'. */
8401 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8406 /* Create the goto-statement. */
8407 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8409 /* Issue a warning about this use of a GNU extension. */
8410 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8411 /* Consume the '*' token. */
8412 cp_lexer_consume_token (parser->lexer);
8413 /* Parse the dependent expression. */
8414 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8417 finish_goto_stmt (cp_parser_identifier (parser));
8418 /* Look for the final `;'. */
8419 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8423 cp_parser_error (parser, "expected jump-statement");
8430 /* Parse a declaration-statement.
8432 declaration-statement:
8433 block-declaration */
8436 cp_parser_declaration_statement (cp_parser* parser)
8440 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8441 p = obstack_alloc (&declarator_obstack, 0);
8443 /* Parse the block-declaration. */
8444 cp_parser_block_declaration (parser, /*statement_p=*/true);
8446 /* Free any declarators allocated. */
8447 obstack_free (&declarator_obstack, p);
8449 /* Finish off the statement. */
8453 /* Some dependent statements (like `if (cond) statement'), are
8454 implicitly in their own scope. In other words, if the statement is
8455 a single statement (as opposed to a compound-statement), it is
8456 none-the-less treated as if it were enclosed in braces. Any
8457 declarations appearing in the dependent statement are out of scope
8458 after control passes that point. This function parses a statement,
8459 but ensures that is in its own scope, even if it is not a
8462 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8463 is a (possibly labeled) if statement which is not enclosed in
8464 braces and has an else clause. This is used to implement
8467 Returns the new statement. */
8470 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8477 /* Mark if () ; with a special NOP_EXPR. */
8478 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8480 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8481 cp_lexer_consume_token (parser->lexer);
8482 statement = add_stmt (build_empty_stmt (loc));
8484 /* if a compound is opened, we simply parse the statement directly. */
8485 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8486 statement = cp_parser_compound_statement (parser, NULL, false);
8487 /* If the token is not a `{', then we must take special action. */
8490 /* Create a compound-statement. */
8491 statement = begin_compound_stmt (0);
8492 /* Parse the dependent-statement. */
8493 cp_parser_statement (parser, NULL_TREE, false, if_p);
8494 /* Finish the dummy compound-statement. */
8495 finish_compound_stmt (statement);
8498 /* Return the statement. */
8502 /* For some dependent statements (like `while (cond) statement'), we
8503 have already created a scope. Therefore, even if the dependent
8504 statement is a compound-statement, we do not want to create another
8508 cp_parser_already_scoped_statement (cp_parser* parser)
8510 /* If the token is a `{', then we must take special action. */
8511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8512 cp_parser_statement (parser, NULL_TREE, false, NULL);
8515 /* Avoid calling cp_parser_compound_statement, so that we
8516 don't create a new scope. Do everything else by hand. */
8517 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8518 /* If the next keyword is `__label__' we have a label declaration. */
8519 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8520 cp_parser_label_declaration (parser);
8521 /* Parse an (optional) statement-seq. */
8522 cp_parser_statement_seq_opt (parser, NULL_TREE);
8523 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8527 /* Declarations [gram.dcl.dcl] */
8529 /* Parse an optional declaration-sequence.
8533 declaration-seq declaration */
8536 cp_parser_declaration_seq_opt (cp_parser* parser)
8542 token = cp_lexer_peek_token (parser->lexer);
8544 if (token->type == CPP_CLOSE_BRACE
8545 || token->type == CPP_EOF
8546 || token->type == CPP_PRAGMA_EOL)
8549 if (token->type == CPP_SEMICOLON)
8551 /* A declaration consisting of a single semicolon is
8552 invalid. Allow it unless we're being pedantic. */
8553 cp_lexer_consume_token (parser->lexer);
8554 if (!in_system_header)
8555 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8559 /* If we're entering or exiting a region that's implicitly
8560 extern "C", modify the lang context appropriately. */
8561 if (!parser->implicit_extern_c && token->implicit_extern_c)
8563 push_lang_context (lang_name_c);
8564 parser->implicit_extern_c = true;
8566 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8568 pop_lang_context ();
8569 parser->implicit_extern_c = false;
8572 if (token->type == CPP_PRAGMA)
8574 /* A top-level declaration can consist solely of a #pragma.
8575 A nested declaration cannot, so this is done here and not
8576 in cp_parser_declaration. (A #pragma at block scope is
8577 handled in cp_parser_statement.) */
8578 cp_parser_pragma (parser, pragma_external);
8582 /* Parse the declaration itself. */
8583 cp_parser_declaration (parser);
8587 /* Parse a declaration.
8592 template-declaration
8593 explicit-instantiation
8594 explicit-specialization
8595 linkage-specification
8596 namespace-definition
8601 __extension__ declaration */
8604 cp_parser_declaration (cp_parser* parser)
8611 /* Check for the `__extension__' keyword. */
8612 if (cp_parser_extension_opt (parser, &saved_pedantic))
8614 /* Parse the qualified declaration. */
8615 cp_parser_declaration (parser);
8616 /* Restore the PEDANTIC flag. */
8617 pedantic = saved_pedantic;
8622 /* Try to figure out what kind of declaration is present. */
8623 token1 = *cp_lexer_peek_token (parser->lexer);
8625 if (token1.type != CPP_EOF)
8626 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8629 token2.type = CPP_EOF;
8630 token2.keyword = RID_MAX;
8633 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8634 p = obstack_alloc (&declarator_obstack, 0);
8636 /* If the next token is `extern' and the following token is a string
8637 literal, then we have a linkage specification. */
8638 if (token1.keyword == RID_EXTERN
8639 && cp_parser_is_string_literal (&token2))
8640 cp_parser_linkage_specification (parser);
8641 /* If the next token is `template', then we have either a template
8642 declaration, an explicit instantiation, or an explicit
8644 else if (token1.keyword == RID_TEMPLATE)
8646 /* `template <>' indicates a template specialization. */
8647 if (token2.type == CPP_LESS
8648 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8649 cp_parser_explicit_specialization (parser);
8650 /* `template <' indicates a template declaration. */
8651 else if (token2.type == CPP_LESS)
8652 cp_parser_template_declaration (parser, /*member_p=*/false);
8653 /* Anything else must be an explicit instantiation. */
8655 cp_parser_explicit_instantiation (parser);
8657 /* If the next token is `export', then we have a template
8659 else if (token1.keyword == RID_EXPORT)
8660 cp_parser_template_declaration (parser, /*member_p=*/false);
8661 /* If the next token is `extern', 'static' or 'inline' and the one
8662 after that is `template', we have a GNU extended explicit
8663 instantiation directive. */
8664 else if (cp_parser_allow_gnu_extensions_p (parser)
8665 && (token1.keyword == RID_EXTERN
8666 || token1.keyword == RID_STATIC
8667 || token1.keyword == RID_INLINE)
8668 && token2.keyword == RID_TEMPLATE)
8669 cp_parser_explicit_instantiation (parser);
8670 /* If the next token is `namespace', check for a named or unnamed
8671 namespace definition. */
8672 else if (token1.keyword == RID_NAMESPACE
8673 && (/* A named namespace definition. */
8674 (token2.type == CPP_NAME
8675 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8677 /* An unnamed namespace definition. */
8678 || token2.type == CPP_OPEN_BRACE
8679 || token2.keyword == RID_ATTRIBUTE))
8680 cp_parser_namespace_definition (parser);
8681 /* An inline (associated) namespace definition. */
8682 else if (token1.keyword == RID_INLINE
8683 && token2.keyword == RID_NAMESPACE)
8684 cp_parser_namespace_definition (parser);
8685 /* Objective-C++ declaration/definition. */
8686 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8687 cp_parser_objc_declaration (parser);
8688 /* We must have either a block declaration or a function
8691 /* Try to parse a block-declaration, or a function-definition. */
8692 cp_parser_block_declaration (parser, /*statement_p=*/false);
8694 /* Free any declarators allocated. */
8695 obstack_free (&declarator_obstack, p);
8698 /* Parse a block-declaration.
8703 namespace-alias-definition
8710 __extension__ block-declaration
8715 static_assert-declaration
8717 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8718 part of a declaration-statement. */
8721 cp_parser_block_declaration (cp_parser *parser,
8727 /* Check for the `__extension__' keyword. */
8728 if (cp_parser_extension_opt (parser, &saved_pedantic))
8730 /* Parse the qualified declaration. */
8731 cp_parser_block_declaration (parser, statement_p);
8732 /* Restore the PEDANTIC flag. */
8733 pedantic = saved_pedantic;
8738 /* Peek at the next token to figure out which kind of declaration is
8740 token1 = cp_lexer_peek_token (parser->lexer);
8742 /* If the next keyword is `asm', we have an asm-definition. */
8743 if (token1->keyword == RID_ASM)
8746 cp_parser_commit_to_tentative_parse (parser);
8747 cp_parser_asm_definition (parser);
8749 /* If the next keyword is `namespace', we have a
8750 namespace-alias-definition. */
8751 else if (token1->keyword == RID_NAMESPACE)
8752 cp_parser_namespace_alias_definition (parser);
8753 /* If the next keyword is `using', we have either a
8754 using-declaration or a using-directive. */
8755 else if (token1->keyword == RID_USING)
8760 cp_parser_commit_to_tentative_parse (parser);
8761 /* If the token after `using' is `namespace', then we have a
8763 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8764 if (token2->keyword == RID_NAMESPACE)
8765 cp_parser_using_directive (parser);
8766 /* Otherwise, it's a using-declaration. */
8768 cp_parser_using_declaration (parser,
8769 /*access_declaration_p=*/false);
8771 /* If the next keyword is `__label__' we have a misplaced label
8773 else if (token1->keyword == RID_LABEL)
8775 cp_lexer_consume_token (parser->lexer);
8776 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8777 cp_parser_skip_to_end_of_statement (parser);
8778 /* If the next token is now a `;', consume it. */
8779 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8780 cp_lexer_consume_token (parser->lexer);
8782 /* If the next token is `static_assert' we have a static assertion. */
8783 else if (token1->keyword == RID_STATIC_ASSERT)
8784 cp_parser_static_assert (parser, /*member_p=*/false);
8785 /* Anything else must be a simple-declaration. */
8787 cp_parser_simple_declaration (parser, !statement_p);
8790 /* Parse a simple-declaration.
8793 decl-specifier-seq [opt] init-declarator-list [opt] ;
8795 init-declarator-list:
8797 init-declarator-list , init-declarator
8799 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8800 function-definition as a simple-declaration. */
8803 cp_parser_simple_declaration (cp_parser* parser,
8804 bool function_definition_allowed_p)
8806 cp_decl_specifier_seq decl_specifiers;
8807 int declares_class_or_enum;
8808 bool saw_declarator;
8810 /* Defer access checks until we know what is being declared; the
8811 checks for names appearing in the decl-specifier-seq should be
8812 done as if we were in the scope of the thing being declared. */
8813 push_deferring_access_checks (dk_deferred);
8815 /* Parse the decl-specifier-seq. We have to keep track of whether
8816 or not the decl-specifier-seq declares a named class or
8817 enumeration type, since that is the only case in which the
8818 init-declarator-list is allowed to be empty.
8822 In a simple-declaration, the optional init-declarator-list can be
8823 omitted only when declaring a class or enumeration, that is when
8824 the decl-specifier-seq contains either a class-specifier, an
8825 elaborated-type-specifier, or an enum-specifier. */
8826 cp_parser_decl_specifier_seq (parser,
8827 CP_PARSER_FLAGS_OPTIONAL,
8829 &declares_class_or_enum);
8830 /* We no longer need to defer access checks. */
8831 stop_deferring_access_checks ();
8833 /* In a block scope, a valid declaration must always have a
8834 decl-specifier-seq. By not trying to parse declarators, we can
8835 resolve the declaration/expression ambiguity more quickly. */
8836 if (!function_definition_allowed_p
8837 && !decl_specifiers.any_specifiers_p)
8839 cp_parser_error (parser, "expected declaration");
8843 /* If the next two tokens are both identifiers, the code is
8844 erroneous. The usual cause of this situation is code like:
8848 where "T" should name a type -- but does not. */
8849 if (!decl_specifiers.type
8850 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8852 /* If parsing tentatively, we should commit; we really are
8853 looking at a declaration. */
8854 cp_parser_commit_to_tentative_parse (parser);
8859 /* If we have seen at least one decl-specifier, and the next token
8860 is not a parenthesis, then we must be looking at a declaration.
8861 (After "int (" we might be looking at a functional cast.) */
8862 if (decl_specifiers.any_specifiers_p
8863 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8864 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8865 && !cp_parser_error_occurred (parser))
8866 cp_parser_commit_to_tentative_parse (parser);
8868 /* Keep going until we hit the `;' at the end of the simple
8870 saw_declarator = false;
8871 while (cp_lexer_next_token_is_not (parser->lexer,
8875 bool function_definition_p;
8880 /* If we are processing next declarator, coma is expected */
8881 token = cp_lexer_peek_token (parser->lexer);
8882 gcc_assert (token->type == CPP_COMMA);
8883 cp_lexer_consume_token (parser->lexer);
8886 saw_declarator = true;
8888 /* Parse the init-declarator. */
8889 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8891 function_definition_allowed_p,
8893 declares_class_or_enum,
8894 &function_definition_p);
8895 /* If an error occurred while parsing tentatively, exit quickly.
8896 (That usually happens when in the body of a function; each
8897 statement is treated as a declaration-statement until proven
8899 if (cp_parser_error_occurred (parser))
8901 /* Handle function definitions specially. */
8902 if (function_definition_p)
8904 /* If the next token is a `,', then we are probably
8905 processing something like:
8909 which is erroneous. */
8910 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8912 cp_token *token = cp_lexer_peek_token (parser->lexer);
8913 error_at (token->location,
8915 " declarations and function-definitions is forbidden");
8917 /* Otherwise, we're done with the list of declarators. */
8920 pop_deferring_access_checks ();
8924 /* The next token should be either a `,' or a `;'. */
8925 token = cp_lexer_peek_token (parser->lexer);
8926 /* If it's a `,', there are more declarators to come. */
8927 if (token->type == CPP_COMMA)
8928 /* will be consumed next time around */;
8929 /* If it's a `;', we are done. */
8930 else if (token->type == CPP_SEMICOLON)
8932 /* Anything else is an error. */
8935 /* If we have already issued an error message we don't need
8936 to issue another one. */
8937 if (decl != error_mark_node
8938 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8939 cp_parser_error (parser, "expected %<,%> or %<;%>");
8940 /* Skip tokens until we reach the end of the statement. */
8941 cp_parser_skip_to_end_of_statement (parser);
8942 /* If the next token is now a `;', consume it. */
8943 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8944 cp_lexer_consume_token (parser->lexer);
8947 /* After the first time around, a function-definition is not
8948 allowed -- even if it was OK at first. For example:
8953 function_definition_allowed_p = false;
8956 /* Issue an error message if no declarators are present, and the
8957 decl-specifier-seq does not itself declare a class or
8959 if (!saw_declarator)
8961 if (cp_parser_declares_only_class_p (parser))
8962 shadow_tag (&decl_specifiers);
8963 /* Perform any deferred access checks. */
8964 perform_deferred_access_checks ();
8967 /* Consume the `;'. */
8968 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8971 pop_deferring_access_checks ();
8974 /* Parse a decl-specifier-seq.
8977 decl-specifier-seq [opt] decl-specifier
8980 storage-class-specifier
8991 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8993 The parser flags FLAGS is used to control type-specifier parsing.
8995 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8998 1: one of the decl-specifiers is an elaborated-type-specifier
8999 (i.e., a type declaration)
9000 2: one of the decl-specifiers is an enum-specifier or a
9001 class-specifier (i.e., a type definition)
9006 cp_parser_decl_specifier_seq (cp_parser* parser,
9007 cp_parser_flags flags,
9008 cp_decl_specifier_seq *decl_specs,
9009 int* declares_class_or_enum)
9011 bool constructor_possible_p = !parser->in_declarator_p;
9012 cp_token *start_token = NULL;
9014 /* Clear DECL_SPECS. */
9015 clear_decl_specs (decl_specs);
9017 /* Assume no class or enumeration type is declared. */
9018 *declares_class_or_enum = 0;
9020 /* Keep reading specifiers until there are no more to read. */
9024 bool found_decl_spec;
9027 /* Peek at the next token. */
9028 token = cp_lexer_peek_token (parser->lexer);
9030 /* Save the first token of the decl spec list for error
9033 start_token = token;
9034 /* Handle attributes. */
9035 if (token->keyword == RID_ATTRIBUTE)
9037 /* Parse the attributes. */
9038 decl_specs->attributes
9039 = chainon (decl_specs->attributes,
9040 cp_parser_attributes_opt (parser));
9043 /* Assume we will find a decl-specifier keyword. */
9044 found_decl_spec = true;
9045 /* If the next token is an appropriate keyword, we can simply
9046 add it to the list. */
9047 switch (token->keyword)
9053 if (!at_class_scope_p ())
9055 error_at (token->location, "%<friend%> used outside of class");
9056 cp_lexer_purge_token (parser->lexer);
9060 ++decl_specs->specs[(int) ds_friend];
9061 /* Consume the token. */
9062 cp_lexer_consume_token (parser->lexer);
9067 ++decl_specs->specs[(int) ds_constexpr];
9068 cp_lexer_consume_token (parser->lexer);
9071 /* function-specifier:
9078 cp_parser_function_specifier_opt (parser, decl_specs);
9084 ++decl_specs->specs[(int) ds_typedef];
9085 /* Consume the token. */
9086 cp_lexer_consume_token (parser->lexer);
9087 /* A constructor declarator cannot appear in a typedef. */
9088 constructor_possible_p = false;
9089 /* The "typedef" keyword can only occur in a declaration; we
9090 may as well commit at this point. */
9091 cp_parser_commit_to_tentative_parse (parser);
9093 if (decl_specs->storage_class != sc_none)
9094 decl_specs->conflicting_specifiers_p = true;
9097 /* storage-class-specifier:
9107 if (cxx_dialect == cxx98)
9109 /* Consume the token. */
9110 cp_lexer_consume_token (parser->lexer);
9112 /* Complain about `auto' as a storage specifier, if
9113 we're complaining about C++0x compatibility. */
9114 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9115 " will change meaning in C++0x; please remove it");
9117 /* Set the storage class anyway. */
9118 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9122 /* C++0x auto type-specifier. */
9123 found_decl_spec = false;
9130 /* Consume the token. */
9131 cp_lexer_consume_token (parser->lexer);
9132 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9136 /* Consume the token. */
9137 cp_lexer_consume_token (parser->lexer);
9138 ++decl_specs->specs[(int) ds_thread];
9142 /* We did not yet find a decl-specifier yet. */
9143 found_decl_spec = false;
9147 /* Constructors are a special case. The `S' in `S()' is not a
9148 decl-specifier; it is the beginning of the declarator. */
9151 && constructor_possible_p
9152 && (cp_parser_constructor_declarator_p
9153 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9155 /* If we don't have a DECL_SPEC yet, then we must be looking at
9156 a type-specifier. */
9157 if (!found_decl_spec && !constructor_p)
9159 int decl_spec_declares_class_or_enum;
9160 bool is_cv_qualifier;
9164 = cp_parser_type_specifier (parser, flags,
9166 /*is_declaration=*/true,
9167 &decl_spec_declares_class_or_enum,
9169 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9171 /* If this type-specifier referenced a user-defined type
9172 (a typedef, class-name, etc.), then we can't allow any
9173 more such type-specifiers henceforth.
9177 The longest sequence of decl-specifiers that could
9178 possibly be a type name is taken as the
9179 decl-specifier-seq of a declaration. The sequence shall
9180 be self-consistent as described below.
9184 As a general rule, at most one type-specifier is allowed
9185 in the complete decl-specifier-seq of a declaration. The
9186 only exceptions are the following:
9188 -- const or volatile can be combined with any other
9191 -- signed or unsigned can be combined with char, long,
9199 void g (const int Pc);
9201 Here, Pc is *not* part of the decl-specifier seq; it's
9202 the declarator. Therefore, once we see a type-specifier
9203 (other than a cv-qualifier), we forbid any additional
9204 user-defined types. We *do* still allow things like `int
9205 int' to be considered a decl-specifier-seq, and issue the
9206 error message later. */
9207 if (type_spec && !is_cv_qualifier)
9208 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9209 /* A constructor declarator cannot follow a type-specifier. */
9212 constructor_possible_p = false;
9213 found_decl_spec = true;
9217 /* If we still do not have a DECL_SPEC, then there are no more
9219 if (!found_decl_spec)
9222 decl_specs->any_specifiers_p = true;
9223 /* After we see one decl-specifier, further decl-specifiers are
9225 flags |= CP_PARSER_FLAGS_OPTIONAL;
9228 cp_parser_check_decl_spec (decl_specs, start_token->location);
9230 /* Don't allow a friend specifier with a class definition. */
9231 if (decl_specs->specs[(int) ds_friend] != 0
9232 && (*declares_class_or_enum & 2))
9233 error_at (start_token->location,
9234 "class definition may not be declared a friend");
9237 /* Parse an (optional) storage-class-specifier.
9239 storage-class-specifier:
9248 storage-class-specifier:
9251 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9254 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9256 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9259 if (cxx_dialect != cxx98)
9261 /* Fall through for C++98. */
9268 /* Consume the token. */
9269 return cp_lexer_consume_token (parser->lexer)->u.value;
9276 /* Parse an (optional) function-specifier.
9283 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9284 Updates DECL_SPECS, if it is non-NULL. */
9287 cp_parser_function_specifier_opt (cp_parser* parser,
9288 cp_decl_specifier_seq *decl_specs)
9290 cp_token *token = cp_lexer_peek_token (parser->lexer);
9291 switch (token->keyword)
9295 ++decl_specs->specs[(int) ds_inline];
9299 /* 14.5.2.3 [temp.mem]
9301 A member function template shall not be virtual. */
9302 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9303 error_at (token->location, "templates may not be %<virtual%>");
9304 else if (decl_specs)
9305 ++decl_specs->specs[(int) ds_virtual];
9310 ++decl_specs->specs[(int) ds_explicit];
9317 /* Consume the token. */
9318 return cp_lexer_consume_token (parser->lexer)->u.value;
9321 /* Parse a linkage-specification.
9323 linkage-specification:
9324 extern string-literal { declaration-seq [opt] }
9325 extern string-literal declaration */
9328 cp_parser_linkage_specification (cp_parser* parser)
9332 /* Look for the `extern' keyword. */
9333 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9335 /* Look for the string-literal. */
9336 linkage = cp_parser_string_literal (parser, false, false);
9338 /* Transform the literal into an identifier. If the literal is a
9339 wide-character string, or contains embedded NULs, then we can't
9340 handle it as the user wants. */
9341 if (strlen (TREE_STRING_POINTER (linkage))
9342 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9344 cp_parser_error (parser, "invalid linkage-specification");
9345 /* Assume C++ linkage. */
9346 linkage = lang_name_cplusplus;
9349 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9351 /* We're now using the new linkage. */
9352 push_lang_context (linkage);
9354 /* If the next token is a `{', then we're using the first
9356 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9358 /* Consume the `{' token. */
9359 cp_lexer_consume_token (parser->lexer);
9360 /* Parse the declarations. */
9361 cp_parser_declaration_seq_opt (parser);
9362 /* Look for the closing `}'. */
9363 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9365 /* Otherwise, there's just one declaration. */
9368 bool saved_in_unbraced_linkage_specification_p;
9370 saved_in_unbraced_linkage_specification_p
9371 = parser->in_unbraced_linkage_specification_p;
9372 parser->in_unbraced_linkage_specification_p = true;
9373 cp_parser_declaration (parser);
9374 parser->in_unbraced_linkage_specification_p
9375 = saved_in_unbraced_linkage_specification_p;
9378 /* We're done with the linkage-specification. */
9379 pop_lang_context ();
9382 /* Parse a static_assert-declaration.
9384 static_assert-declaration:
9385 static_assert ( constant-expression , string-literal ) ;
9387 If MEMBER_P, this static_assert is a class member. */
9390 cp_parser_static_assert(cp_parser *parser, bool member_p)
9395 location_t saved_loc;
9397 /* Peek at the `static_assert' token so we can keep track of exactly
9398 where the static assertion started. */
9399 token = cp_lexer_peek_token (parser->lexer);
9400 saved_loc = token->location;
9402 /* Look for the `static_assert' keyword. */
9403 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9404 "%<static_assert%>"))
9407 /* We know we are in a static assertion; commit to any tentative
9409 if (cp_parser_parsing_tentatively (parser))
9410 cp_parser_commit_to_tentative_parse (parser);
9412 /* Parse the `(' starting the static assertion condition. */
9413 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9415 /* Parse the constant-expression. */
9417 cp_parser_constant_expression (parser,
9418 /*allow_non_constant_p=*/false,
9419 /*non_constant_p=*/NULL);
9421 /* Parse the separating `,'. */
9422 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9424 /* Parse the string-literal message. */
9425 message = cp_parser_string_literal (parser,
9426 /*translate=*/false,
9429 /* A `)' completes the static assertion. */
9430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9431 cp_parser_skip_to_closing_parenthesis (parser,
9432 /*recovering=*/true,
9434 /*consume_paren=*/true);
9436 /* A semicolon terminates the declaration. */
9437 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9439 /* Complete the static assertion, which may mean either processing
9440 the static assert now or saving it for template instantiation. */
9441 finish_static_assert (condition, message, saved_loc, member_p);
9444 /* Parse a `decltype' type. Returns the type.
9446 simple-type-specifier:
9447 decltype ( expression ) */
9450 cp_parser_decltype (cp_parser *parser)
9453 bool id_expression_or_member_access_p = false;
9454 const char *saved_message;
9455 bool saved_integral_constant_expression_p;
9456 bool saved_non_integral_constant_expression_p;
9457 cp_token *id_expr_start_token;
9459 /* Look for the `decltype' token. */
9460 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9461 return error_mark_node;
9463 /* Types cannot be defined in a `decltype' expression. Save away the
9465 saved_message = parser->type_definition_forbidden_message;
9467 /* And create the new one. */
9468 parser->type_definition_forbidden_message
9469 = "types may not be defined in %<decltype%> expressions";
9471 /* The restrictions on constant-expressions do not apply inside
9472 decltype expressions. */
9473 saved_integral_constant_expression_p
9474 = parser->integral_constant_expression_p;
9475 saved_non_integral_constant_expression_p
9476 = parser->non_integral_constant_expression_p;
9477 parser->integral_constant_expression_p = false;
9479 /* Do not actually evaluate the expression. */
9480 ++cp_unevaluated_operand;
9482 /* Do not warn about problems with the expression. */
9483 ++c_inhibit_evaluation_warnings;
9485 /* Parse the opening `('. */
9486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9487 return error_mark_node;
9489 /* First, try parsing an id-expression. */
9490 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9491 cp_parser_parse_tentatively (parser);
9492 expr = cp_parser_id_expression (parser,
9493 /*template_keyword_p=*/false,
9494 /*check_dependency_p=*/true,
9495 /*template_p=*/NULL,
9496 /*declarator_p=*/false,
9497 /*optional_p=*/false);
9499 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9501 bool non_integral_constant_expression_p = false;
9502 tree id_expression = expr;
9504 const char *error_msg;
9506 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9507 /* Lookup the name we got back from the id-expression. */
9508 expr = cp_parser_lookup_name (parser, expr,
9510 /*is_template=*/false,
9511 /*is_namespace=*/false,
9512 /*check_dependency=*/true,
9513 /*ambiguous_decls=*/NULL,
9514 id_expr_start_token->location);
9517 && expr != error_mark_node
9518 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9519 && TREE_CODE (expr) != TYPE_DECL
9520 && (TREE_CODE (expr) != BIT_NOT_EXPR
9521 || !TYPE_P (TREE_OPERAND (expr, 0)))
9522 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9524 /* Complete lookup of the id-expression. */
9525 expr = (finish_id_expression
9526 (id_expression, expr, parser->scope, &idk,
9527 /*integral_constant_expression_p=*/false,
9528 /*allow_non_integral_constant_expression_p=*/true,
9529 &non_integral_constant_expression_p,
9530 /*template_p=*/false,
9532 /*address_p=*/false,
9533 /*template_arg_p=*/false,
9535 id_expr_start_token->location));
9537 if (expr == error_mark_node)
9538 /* We found an id-expression, but it was something that we
9539 should not have found. This is an error, not something
9540 we can recover from, so note that we found an
9541 id-expression and we'll recover as gracefully as
9543 id_expression_or_member_access_p = true;
9547 && expr != error_mark_node
9548 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9549 /* We have an id-expression. */
9550 id_expression_or_member_access_p = true;
9553 if (!id_expression_or_member_access_p)
9555 /* Abort the id-expression parse. */
9556 cp_parser_abort_tentative_parse (parser);
9558 /* Parsing tentatively, again. */
9559 cp_parser_parse_tentatively (parser);
9561 /* Parse a class member access. */
9562 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9564 /*member_access_only_p=*/true, NULL);
9567 && expr != error_mark_node
9568 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9569 /* We have an id-expression. */
9570 id_expression_or_member_access_p = true;
9573 if (id_expression_or_member_access_p)
9574 /* We have parsed the complete id-expression or member access. */
9575 cp_parser_parse_definitely (parser);
9578 bool saved_greater_than_is_operator_p;
9580 /* Abort our attempt to parse an id-expression or member access
9582 cp_parser_abort_tentative_parse (parser);
9584 /* Within a parenthesized expression, a `>' token is always
9585 the greater-than operator. */
9586 saved_greater_than_is_operator_p
9587 = parser->greater_than_is_operator_p;
9588 parser->greater_than_is_operator_p = true;
9590 /* Parse a full expression. */
9591 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9593 /* The `>' token might be the end of a template-id or
9594 template-parameter-list now. */
9595 parser->greater_than_is_operator_p
9596 = saved_greater_than_is_operator_p;
9599 /* Go back to evaluating expressions. */
9600 --cp_unevaluated_operand;
9601 --c_inhibit_evaluation_warnings;
9603 /* Restore the old message and the integral constant expression
9605 parser->type_definition_forbidden_message = saved_message;
9606 parser->integral_constant_expression_p
9607 = saved_integral_constant_expression_p;
9608 parser->non_integral_constant_expression_p
9609 = saved_non_integral_constant_expression_p;
9611 if (expr == error_mark_node)
9613 /* Skip everything up to the closing `)'. */
9614 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9615 /*consume_paren=*/true);
9616 return error_mark_node;
9619 /* Parse to the closing `)'. */
9620 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9622 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9623 /*consume_paren=*/true);
9624 return error_mark_node;
9627 return finish_decltype_type (expr, id_expression_or_member_access_p);
9630 /* Special member functions [gram.special] */
9632 /* Parse a conversion-function-id.
9634 conversion-function-id:
9635 operator conversion-type-id
9637 Returns an IDENTIFIER_NODE representing the operator. */
9640 cp_parser_conversion_function_id (cp_parser* parser)
9644 tree saved_qualifying_scope;
9645 tree saved_object_scope;
9646 tree pushed_scope = NULL_TREE;
9648 /* Look for the `operator' token. */
9649 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9650 return error_mark_node;
9651 /* When we parse the conversion-type-id, the current scope will be
9652 reset. However, we need that information in able to look up the
9653 conversion function later, so we save it here. */
9654 saved_scope = parser->scope;
9655 saved_qualifying_scope = parser->qualifying_scope;
9656 saved_object_scope = parser->object_scope;
9657 /* We must enter the scope of the class so that the names of
9658 entities declared within the class are available in the
9659 conversion-type-id. For example, consider:
9666 S::operator I() { ... }
9668 In order to see that `I' is a type-name in the definition, we
9669 must be in the scope of `S'. */
9671 pushed_scope = push_scope (saved_scope);
9672 /* Parse the conversion-type-id. */
9673 type = cp_parser_conversion_type_id (parser);
9674 /* Leave the scope of the class, if any. */
9676 pop_scope (pushed_scope);
9677 /* Restore the saved scope. */
9678 parser->scope = saved_scope;
9679 parser->qualifying_scope = saved_qualifying_scope;
9680 parser->object_scope = saved_object_scope;
9681 /* If the TYPE is invalid, indicate failure. */
9682 if (type == error_mark_node)
9683 return error_mark_node;
9684 return mangle_conv_op_name_for_type (type);
9687 /* Parse a conversion-type-id:
9690 type-specifier-seq conversion-declarator [opt]
9692 Returns the TYPE specified. */
9695 cp_parser_conversion_type_id (cp_parser* parser)
9698 cp_decl_specifier_seq type_specifiers;
9699 cp_declarator *declarator;
9700 tree type_specified;
9702 /* Parse the attributes. */
9703 attributes = cp_parser_attributes_opt (parser);
9704 /* Parse the type-specifiers. */
9705 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9706 /*is_trailing_return=*/false,
9708 /* If that didn't work, stop. */
9709 if (type_specifiers.type == error_mark_node)
9710 return error_mark_node;
9711 /* Parse the conversion-declarator. */
9712 declarator = cp_parser_conversion_declarator_opt (parser);
9714 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9715 /*initialized=*/0, &attributes);
9717 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9719 /* Don't give this error when parsing tentatively. This happens to
9720 work because we always parse this definitively once. */
9721 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9722 && type_uses_auto (type_specified))
9724 error ("invalid use of %<auto%> in conversion operator");
9725 return error_mark_node;
9728 return type_specified;
9731 /* Parse an (optional) conversion-declarator.
9733 conversion-declarator:
9734 ptr-operator conversion-declarator [opt]
9738 static cp_declarator *
9739 cp_parser_conversion_declarator_opt (cp_parser* parser)
9741 enum tree_code code;
9743 cp_cv_quals cv_quals;
9745 /* We don't know if there's a ptr-operator next, or not. */
9746 cp_parser_parse_tentatively (parser);
9747 /* Try the ptr-operator. */
9748 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9749 /* If it worked, look for more conversion-declarators. */
9750 if (cp_parser_parse_definitely (parser))
9752 cp_declarator *declarator;
9754 /* Parse another optional declarator. */
9755 declarator = cp_parser_conversion_declarator_opt (parser);
9757 return cp_parser_make_indirect_declarator
9758 (code, class_type, cv_quals, declarator);
9764 /* Parse an (optional) ctor-initializer.
9767 : mem-initializer-list
9769 Returns TRUE iff the ctor-initializer was actually present. */
9772 cp_parser_ctor_initializer_opt (cp_parser* parser)
9774 /* If the next token is not a `:', then there is no
9775 ctor-initializer. */
9776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9778 /* Do default initialization of any bases and members. */
9779 if (DECL_CONSTRUCTOR_P (current_function_decl))
9780 finish_mem_initializers (NULL_TREE);
9785 /* Consume the `:' token. */
9786 cp_lexer_consume_token (parser->lexer);
9787 /* And the mem-initializer-list. */
9788 cp_parser_mem_initializer_list (parser);
9793 /* Parse a mem-initializer-list.
9795 mem-initializer-list:
9796 mem-initializer ... [opt]
9797 mem-initializer ... [opt] , mem-initializer-list */
9800 cp_parser_mem_initializer_list (cp_parser* parser)
9802 tree mem_initializer_list = NULL_TREE;
9803 cp_token *token = cp_lexer_peek_token (parser->lexer);
9805 /* Let the semantic analysis code know that we are starting the
9806 mem-initializer-list. */
9807 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9808 error_at (token->location,
9809 "only constructors take base initializers");
9811 /* Loop through the list. */
9814 tree mem_initializer;
9816 token = cp_lexer_peek_token (parser->lexer);
9817 /* Parse the mem-initializer. */
9818 mem_initializer = cp_parser_mem_initializer (parser);
9819 /* If the next token is a `...', we're expanding member initializers. */
9820 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9822 /* Consume the `...'. */
9823 cp_lexer_consume_token (parser->lexer);
9825 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9826 can be expanded but members cannot. */
9827 if (mem_initializer != error_mark_node
9828 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9830 error_at (token->location,
9831 "cannot expand initializer for member %<%D%>",
9832 TREE_PURPOSE (mem_initializer));
9833 mem_initializer = error_mark_node;
9836 /* Construct the pack expansion type. */
9837 if (mem_initializer != error_mark_node)
9838 mem_initializer = make_pack_expansion (mem_initializer);
9840 /* Add it to the list, unless it was erroneous. */
9841 if (mem_initializer != error_mark_node)
9843 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9844 mem_initializer_list = mem_initializer;
9846 /* If the next token is not a `,', we're done. */
9847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9849 /* Consume the `,' token. */
9850 cp_lexer_consume_token (parser->lexer);
9853 /* Perform semantic analysis. */
9854 if (DECL_CONSTRUCTOR_P (current_function_decl))
9855 finish_mem_initializers (mem_initializer_list);
9858 /* Parse a mem-initializer.
9861 mem-initializer-id ( expression-list [opt] )
9862 mem-initializer-id braced-init-list
9867 ( expression-list [opt] )
9869 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9870 class) or FIELD_DECL (for a non-static data member) to initialize;
9871 the TREE_VALUE is the expression-list. An empty initialization
9872 list is represented by void_list_node. */
9875 cp_parser_mem_initializer (cp_parser* parser)
9877 tree mem_initializer_id;
9878 tree expression_list;
9880 cp_token *token = cp_lexer_peek_token (parser->lexer);
9882 /* Find out what is being initialized. */
9883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9885 permerror (token->location,
9886 "anachronistic old-style base class initializer");
9887 mem_initializer_id = NULL_TREE;
9891 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9892 if (mem_initializer_id == error_mark_node)
9893 return mem_initializer_id;
9895 member = expand_member_init (mem_initializer_id);
9896 if (member && !DECL_P (member))
9897 in_base_initializer = 1;
9899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9901 bool expr_non_constant_p;
9902 maybe_warn_cpp0x ("extended initializer lists");
9903 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9904 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9905 expression_list = build_tree_list (NULL_TREE, expression_list);
9910 vec = cp_parser_parenthesized_expression_list (parser, false,
9912 /*allow_expansion_p=*/true,
9913 /*non_constant_p=*/NULL);
9915 return error_mark_node;
9916 expression_list = build_tree_list_vec (vec);
9917 release_tree_vector (vec);
9920 if (expression_list == error_mark_node)
9921 return error_mark_node;
9922 if (!expression_list)
9923 expression_list = void_type_node;
9925 in_base_initializer = 0;
9927 return member ? build_tree_list (member, expression_list) : error_mark_node;
9930 /* Parse a mem-initializer-id.
9933 :: [opt] nested-name-specifier [opt] class-name
9936 Returns a TYPE indicating the class to be initializer for the first
9937 production. Returns an IDENTIFIER_NODE indicating the data member
9938 to be initialized for the second production. */
9941 cp_parser_mem_initializer_id (cp_parser* parser)
9943 bool global_scope_p;
9944 bool nested_name_specifier_p;
9945 bool template_p = false;
9948 cp_token *token = cp_lexer_peek_token (parser->lexer);
9950 /* `typename' is not allowed in this context ([temp.res]). */
9951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9953 error_at (token->location,
9954 "keyword %<typename%> not allowed in this context (a qualified "
9955 "member initializer is implicitly a type)");
9956 cp_lexer_consume_token (parser->lexer);
9958 /* Look for the optional `::' operator. */
9960 = (cp_parser_global_scope_opt (parser,
9961 /*current_scope_valid_p=*/false)
9963 /* Look for the optional nested-name-specifier. The simplest way to
9968 The keyword `typename' is not permitted in a base-specifier or
9969 mem-initializer; in these contexts a qualified name that
9970 depends on a template-parameter is implicitly assumed to be a
9973 is to assume that we have seen the `typename' keyword at this
9975 nested_name_specifier_p
9976 = (cp_parser_nested_name_specifier_opt (parser,
9977 /*typename_keyword_p=*/true,
9978 /*check_dependency_p=*/true,
9980 /*is_declaration=*/true)
9982 if (nested_name_specifier_p)
9983 template_p = cp_parser_optional_template_keyword (parser);
9984 /* If there is a `::' operator or a nested-name-specifier, then we
9985 are definitely looking for a class-name. */
9986 if (global_scope_p || nested_name_specifier_p)
9987 return cp_parser_class_name (parser,
9988 /*typename_keyword_p=*/true,
9989 /*template_keyword_p=*/template_p,
9991 /*check_dependency_p=*/true,
9992 /*class_head_p=*/false,
9993 /*is_declaration=*/true);
9994 /* Otherwise, we could also be looking for an ordinary identifier. */
9995 cp_parser_parse_tentatively (parser);
9996 /* Try a class-name. */
9997 id = cp_parser_class_name (parser,
9998 /*typename_keyword_p=*/true,
9999 /*template_keyword_p=*/false,
10001 /*check_dependency_p=*/true,
10002 /*class_head_p=*/false,
10003 /*is_declaration=*/true);
10004 /* If we found one, we're done. */
10005 if (cp_parser_parse_definitely (parser))
10007 /* Otherwise, look for an ordinary identifier. */
10008 return cp_parser_identifier (parser);
10011 /* Overloading [gram.over] */
10013 /* Parse an operator-function-id.
10015 operator-function-id:
10018 Returns an IDENTIFIER_NODE for the operator which is a
10019 human-readable spelling of the identifier, e.g., `operator +'. */
10022 cp_parser_operator_function_id (cp_parser* parser)
10024 /* Look for the `operator' keyword. */
10025 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10026 return error_mark_node;
10027 /* And then the name of the operator itself. */
10028 return cp_parser_operator (parser);
10031 /* Parse an operator.
10034 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10035 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10036 || ++ -- , ->* -> () []
10043 Returns an IDENTIFIER_NODE for the operator which is a
10044 human-readable spelling of the identifier, e.g., `operator +'. */
10047 cp_parser_operator (cp_parser* parser)
10049 tree id = NULL_TREE;
10052 /* Peek at the next token. */
10053 token = cp_lexer_peek_token (parser->lexer);
10054 /* Figure out which operator we have. */
10055 switch (token->type)
10061 /* The keyword should be either `new' or `delete'. */
10062 if (token->keyword == RID_NEW)
10064 else if (token->keyword == RID_DELETE)
10069 /* Consume the `new' or `delete' token. */
10070 cp_lexer_consume_token (parser->lexer);
10072 /* Peek at the next token. */
10073 token = cp_lexer_peek_token (parser->lexer);
10074 /* If it's a `[' token then this is the array variant of the
10076 if (token->type == CPP_OPEN_SQUARE)
10078 /* Consume the `[' token. */
10079 cp_lexer_consume_token (parser->lexer);
10080 /* Look for the `]' token. */
10081 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10082 id = ansi_opname (op == NEW_EXPR
10083 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10085 /* Otherwise, we have the non-array variant. */
10087 id = ansi_opname (op);
10093 id = ansi_opname (PLUS_EXPR);
10097 id = ansi_opname (MINUS_EXPR);
10101 id = ansi_opname (MULT_EXPR);
10105 id = ansi_opname (TRUNC_DIV_EXPR);
10109 id = ansi_opname (TRUNC_MOD_EXPR);
10113 id = ansi_opname (BIT_XOR_EXPR);
10117 id = ansi_opname (BIT_AND_EXPR);
10121 id = ansi_opname (BIT_IOR_EXPR);
10125 id = ansi_opname (BIT_NOT_EXPR);
10129 id = ansi_opname (TRUTH_NOT_EXPR);
10133 id = ansi_assopname (NOP_EXPR);
10137 id = ansi_opname (LT_EXPR);
10141 id = ansi_opname (GT_EXPR);
10145 id = ansi_assopname (PLUS_EXPR);
10149 id = ansi_assopname (MINUS_EXPR);
10153 id = ansi_assopname (MULT_EXPR);
10157 id = ansi_assopname (TRUNC_DIV_EXPR);
10161 id = ansi_assopname (TRUNC_MOD_EXPR);
10165 id = ansi_assopname (BIT_XOR_EXPR);
10169 id = ansi_assopname (BIT_AND_EXPR);
10173 id = ansi_assopname (BIT_IOR_EXPR);
10177 id = ansi_opname (LSHIFT_EXPR);
10181 id = ansi_opname (RSHIFT_EXPR);
10184 case CPP_LSHIFT_EQ:
10185 id = ansi_assopname (LSHIFT_EXPR);
10188 case CPP_RSHIFT_EQ:
10189 id = ansi_assopname (RSHIFT_EXPR);
10193 id = ansi_opname (EQ_EXPR);
10197 id = ansi_opname (NE_EXPR);
10201 id = ansi_opname (LE_EXPR);
10204 case CPP_GREATER_EQ:
10205 id = ansi_opname (GE_EXPR);
10209 id = ansi_opname (TRUTH_ANDIF_EXPR);
10213 id = ansi_opname (TRUTH_ORIF_EXPR);
10216 case CPP_PLUS_PLUS:
10217 id = ansi_opname (POSTINCREMENT_EXPR);
10220 case CPP_MINUS_MINUS:
10221 id = ansi_opname (PREDECREMENT_EXPR);
10225 id = ansi_opname (COMPOUND_EXPR);
10228 case CPP_DEREF_STAR:
10229 id = ansi_opname (MEMBER_REF);
10233 id = ansi_opname (COMPONENT_REF);
10236 case CPP_OPEN_PAREN:
10237 /* Consume the `('. */
10238 cp_lexer_consume_token (parser->lexer);
10239 /* Look for the matching `)'. */
10240 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10241 return ansi_opname (CALL_EXPR);
10243 case CPP_OPEN_SQUARE:
10244 /* Consume the `['. */
10245 cp_lexer_consume_token (parser->lexer);
10246 /* Look for the matching `]'. */
10247 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10248 return ansi_opname (ARRAY_REF);
10251 /* Anything else is an error. */
10255 /* If we have selected an identifier, we need to consume the
10258 cp_lexer_consume_token (parser->lexer);
10259 /* Otherwise, no valid operator name was present. */
10262 cp_parser_error (parser, "expected operator");
10263 id = error_mark_node;
10269 /* Parse a template-declaration.
10271 template-declaration:
10272 export [opt] template < template-parameter-list > declaration
10274 If MEMBER_P is TRUE, this template-declaration occurs within a
10277 The grammar rule given by the standard isn't correct. What
10278 is really meant is:
10280 template-declaration:
10281 export [opt] template-parameter-list-seq
10282 decl-specifier-seq [opt] init-declarator [opt] ;
10283 export [opt] template-parameter-list-seq
10284 function-definition
10286 template-parameter-list-seq:
10287 template-parameter-list-seq [opt]
10288 template < template-parameter-list > */
10291 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10293 /* Check for `export'. */
10294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10296 /* Consume the `export' token. */
10297 cp_lexer_consume_token (parser->lexer);
10298 /* Warn that we do not support `export'. */
10299 warning (0, "keyword %<export%> not implemented, and will be ignored");
10302 cp_parser_template_declaration_after_export (parser, member_p);
10305 /* Parse a template-parameter-list.
10307 template-parameter-list:
10309 template-parameter-list , template-parameter
10311 Returns a TREE_LIST. Each node represents a template parameter.
10312 The nodes are connected via their TREE_CHAINs. */
10315 cp_parser_template_parameter_list (cp_parser* parser)
10317 tree parameter_list = NULL_TREE;
10319 begin_template_parm_list ();
10324 bool is_parameter_pack;
10325 location_t parm_loc;
10327 /* Parse the template-parameter. */
10328 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10329 parameter = cp_parser_template_parameter (parser,
10331 &is_parameter_pack);
10332 /* Add it to the list. */
10333 if (parameter != error_mark_node)
10334 parameter_list = process_template_parm (parameter_list,
10338 is_parameter_pack);
10341 tree err_parm = build_tree_list (parameter, parameter);
10342 TREE_VALUE (err_parm) = error_mark_node;
10343 parameter_list = chainon (parameter_list, err_parm);
10346 /* If the next token is not a `,', we're done. */
10347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10349 /* Otherwise, consume the `,' token. */
10350 cp_lexer_consume_token (parser->lexer);
10353 return end_template_parm_list (parameter_list);
10356 /* Parse a template-parameter.
10358 template-parameter:
10360 parameter-declaration
10362 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10363 the parameter. The TREE_PURPOSE is the default value, if any.
10364 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10365 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10366 set to true iff this parameter is a parameter pack. */
10369 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10370 bool *is_parameter_pack)
10373 cp_parameter_declarator *parameter_declarator;
10374 cp_declarator *id_declarator;
10377 /* Assume it is a type parameter or a template parameter. */
10378 *is_non_type = false;
10379 /* Assume it not a parameter pack. */
10380 *is_parameter_pack = false;
10381 /* Peek at the next token. */
10382 token = cp_lexer_peek_token (parser->lexer);
10383 /* If it is `class' or `template', we have a type-parameter. */
10384 if (token->keyword == RID_TEMPLATE)
10385 return cp_parser_type_parameter (parser, is_parameter_pack);
10386 /* If it is `class' or `typename' we do not know yet whether it is a
10387 type parameter or a non-type parameter. Consider:
10389 template <typename T, typename T::X X> ...
10393 template <class C, class D*> ...
10395 Here, the first parameter is a type parameter, and the second is
10396 a non-type parameter. We can tell by looking at the token after
10397 the identifier -- if it is a `,', `=', or `>' then we have a type
10399 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10401 /* Peek at the token after `class' or `typename'. */
10402 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10403 /* If it's an ellipsis, we have a template type parameter
10405 if (token->type == CPP_ELLIPSIS)
10406 return cp_parser_type_parameter (parser, is_parameter_pack);
10407 /* If it's an identifier, skip it. */
10408 if (token->type == CPP_NAME)
10409 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10410 /* Now, see if the token looks like the end of a template
10412 if (token->type == CPP_COMMA
10413 || token->type == CPP_EQ
10414 || token->type == CPP_GREATER)
10415 return cp_parser_type_parameter (parser, is_parameter_pack);
10418 /* Otherwise, it is a non-type parameter.
10422 When parsing a default template-argument for a non-type
10423 template-parameter, the first non-nested `>' is taken as the end
10424 of the template parameter-list rather than a greater-than
10426 *is_non_type = true;
10427 parameter_declarator
10428 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10429 /*parenthesized_p=*/NULL);
10431 /* If the parameter declaration is marked as a parameter pack, set
10432 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10433 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10435 if (parameter_declarator
10436 && parameter_declarator->declarator
10437 && parameter_declarator->declarator->parameter_pack_p)
10439 *is_parameter_pack = true;
10440 parameter_declarator->declarator->parameter_pack_p = false;
10443 /* If the next token is an ellipsis, and we don't already have it
10444 marked as a parameter pack, then we have a parameter pack (that
10445 has no declarator). */
10446 if (!*is_parameter_pack
10447 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10448 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10450 /* Consume the `...'. */
10451 cp_lexer_consume_token (parser->lexer);
10452 maybe_warn_variadic_templates ();
10454 *is_parameter_pack = true;
10456 /* We might end up with a pack expansion as the type of the non-type
10457 template parameter, in which case this is a non-type template
10459 else if (parameter_declarator
10460 && parameter_declarator->decl_specifiers.type
10461 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10463 *is_parameter_pack = true;
10464 parameter_declarator->decl_specifiers.type =
10465 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10468 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10470 /* Parameter packs cannot have default arguments. However, a
10471 user may try to do so, so we'll parse them and give an
10472 appropriate diagnostic here. */
10474 /* Consume the `='. */
10475 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10476 cp_lexer_consume_token (parser->lexer);
10478 /* Find the name of the parameter pack. */
10479 id_declarator = parameter_declarator->declarator;
10480 while (id_declarator && id_declarator->kind != cdk_id)
10481 id_declarator = id_declarator->declarator;
10483 if (id_declarator && id_declarator->kind == cdk_id)
10484 error_at (start_token->location,
10485 "template parameter pack %qD cannot have a default argument",
10486 id_declarator->u.id.unqualified_name);
10488 error_at (start_token->location,
10489 "template parameter pack cannot have a default argument");
10491 /* Parse the default argument, but throw away the result. */
10492 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10495 parm = grokdeclarator (parameter_declarator->declarator,
10496 ¶meter_declarator->decl_specifiers,
10497 PARM, /*initialized=*/0,
10498 /*attrlist=*/NULL);
10499 if (parm == error_mark_node)
10500 return error_mark_node;
10502 return build_tree_list (parameter_declarator->default_argument, parm);
10505 /* Parse a type-parameter.
10508 class identifier [opt]
10509 class identifier [opt] = type-id
10510 typename identifier [opt]
10511 typename identifier [opt] = type-id
10512 template < template-parameter-list > class identifier [opt]
10513 template < template-parameter-list > class identifier [opt]
10516 GNU Extension (variadic templates):
10519 class ... identifier [opt]
10520 typename ... identifier [opt]
10522 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10523 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10524 the declaration of the parameter.
10526 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10529 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10534 /* Look for a keyword to tell us what kind of parameter this is. */
10535 token = cp_parser_require (parser, CPP_KEYWORD,
10536 "%<class%>, %<typename%>, or %<template%>");
10538 return error_mark_node;
10540 switch (token->keyword)
10546 tree default_argument;
10548 /* If the next token is an ellipsis, we have a template
10550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10552 /* Consume the `...' token. */
10553 cp_lexer_consume_token (parser->lexer);
10554 maybe_warn_variadic_templates ();
10556 *is_parameter_pack = true;
10559 /* If the next token is an identifier, then it names the
10561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10562 identifier = cp_parser_identifier (parser);
10564 identifier = NULL_TREE;
10566 /* Create the parameter. */
10567 parameter = finish_template_type_parm (class_type_node, identifier);
10569 /* If the next token is an `=', we have a default argument. */
10570 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10572 /* Consume the `=' token. */
10573 cp_lexer_consume_token (parser->lexer);
10574 /* Parse the default-argument. */
10575 push_deferring_access_checks (dk_no_deferred);
10576 default_argument = cp_parser_type_id (parser);
10578 /* Template parameter packs cannot have default
10580 if (*is_parameter_pack)
10583 error_at (token->location,
10584 "template parameter pack %qD cannot have a "
10585 "default argument", identifier);
10587 error_at (token->location,
10588 "template parameter packs cannot have "
10589 "default arguments");
10590 default_argument = NULL_TREE;
10592 pop_deferring_access_checks ();
10595 default_argument = NULL_TREE;
10597 /* Create the combined representation of the parameter and the
10598 default argument. */
10599 parameter = build_tree_list (default_argument, parameter);
10605 tree parameter_list;
10607 tree default_argument;
10609 /* Look for the `<'. */
10610 cp_parser_require (parser, CPP_LESS, "%<<%>");
10611 /* Parse the template-parameter-list. */
10612 parameter_list = cp_parser_template_parameter_list (parser);
10613 /* Look for the `>'. */
10614 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10615 /* Look for the `class' keyword. */
10616 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10617 /* If the next token is an ellipsis, we have a template
10619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10621 /* Consume the `...' token. */
10622 cp_lexer_consume_token (parser->lexer);
10623 maybe_warn_variadic_templates ();
10625 *is_parameter_pack = true;
10627 /* If the next token is an `=', then there is a
10628 default-argument. If the next token is a `>', we are at
10629 the end of the parameter-list. If the next token is a `,',
10630 then we are at the end of this parameter. */
10631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10632 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10633 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10635 identifier = cp_parser_identifier (parser);
10636 /* Treat invalid names as if the parameter were nameless. */
10637 if (identifier == error_mark_node)
10638 identifier = NULL_TREE;
10641 identifier = NULL_TREE;
10643 /* Create the template parameter. */
10644 parameter = finish_template_template_parm (class_type_node,
10647 /* If the next token is an `=', then there is a
10648 default-argument. */
10649 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10653 /* Consume the `='. */
10654 cp_lexer_consume_token (parser->lexer);
10655 /* Parse the id-expression. */
10656 push_deferring_access_checks (dk_no_deferred);
10657 /* save token before parsing the id-expression, for error
10659 token = cp_lexer_peek_token (parser->lexer);
10661 = cp_parser_id_expression (parser,
10662 /*template_keyword_p=*/false,
10663 /*check_dependency_p=*/true,
10664 /*template_p=*/&is_template,
10665 /*declarator_p=*/false,
10666 /*optional_p=*/false);
10667 if (TREE_CODE (default_argument) == TYPE_DECL)
10668 /* If the id-expression was a template-id that refers to
10669 a template-class, we already have the declaration here,
10670 so no further lookup is needed. */
10673 /* Look up the name. */
10675 = cp_parser_lookup_name (parser, default_argument,
10677 /*is_template=*/is_template,
10678 /*is_namespace=*/false,
10679 /*check_dependency=*/true,
10680 /*ambiguous_decls=*/NULL,
10682 /* See if the default argument is valid. */
10684 = check_template_template_default_arg (default_argument);
10686 /* Template parameter packs cannot have default
10688 if (*is_parameter_pack)
10691 error_at (token->location,
10692 "template parameter pack %qD cannot "
10693 "have a default argument",
10696 error_at (token->location, "template parameter packs cannot "
10697 "have default arguments");
10698 default_argument = NULL_TREE;
10700 pop_deferring_access_checks ();
10703 default_argument = NULL_TREE;
10705 /* Create the combined representation of the parameter and the
10706 default argument. */
10707 parameter = build_tree_list (default_argument, parameter);
10712 gcc_unreachable ();
10719 /* Parse a template-id.
10722 template-name < template-argument-list [opt] >
10724 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10725 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10726 returned. Otherwise, if the template-name names a function, or set
10727 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10728 names a class, returns a TYPE_DECL for the specialization.
10730 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10731 uninstantiated templates. */
10734 cp_parser_template_id (cp_parser *parser,
10735 bool template_keyword_p,
10736 bool check_dependency_p,
10737 bool is_declaration)
10743 cp_token_position start_of_id = 0;
10744 deferred_access_check *chk;
10745 VEC (deferred_access_check,gc) *access_check;
10746 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10747 bool is_identifier;
10749 /* If the next token corresponds to a template-id, there is no need
10751 next_token = cp_lexer_peek_token (parser->lexer);
10752 if (next_token->type == CPP_TEMPLATE_ID)
10754 struct tree_check *check_value;
10756 /* Get the stored value. */
10757 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10758 /* Perform any access checks that were deferred. */
10759 access_check = check_value->checks;
10763 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10766 perform_or_defer_access_check (chk->binfo,
10771 /* Return the stored value. */
10772 return check_value->value;
10775 /* Avoid performing name lookup if there is no possibility of
10776 finding a template-id. */
10777 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10778 || (next_token->type == CPP_NAME
10779 && !cp_parser_nth_token_starts_template_argument_list_p
10782 cp_parser_error (parser, "expected template-id");
10783 return error_mark_node;
10786 /* Remember where the template-id starts. */
10787 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10788 start_of_id = cp_lexer_token_position (parser->lexer, false);
10790 push_deferring_access_checks (dk_deferred);
10792 /* Parse the template-name. */
10793 is_identifier = false;
10794 token = cp_lexer_peek_token (parser->lexer);
10795 templ = cp_parser_template_name (parser, template_keyword_p,
10796 check_dependency_p,
10799 if (templ == error_mark_node || is_identifier)
10801 pop_deferring_access_checks ();
10805 /* If we find the sequence `[:' after a template-name, it's probably
10806 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10807 parse correctly the argument list. */
10808 next_token = cp_lexer_peek_token (parser->lexer);
10809 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10810 if (next_token->type == CPP_OPEN_SQUARE
10811 && next_token->flags & DIGRAPH
10812 && next_token_2->type == CPP_COLON
10813 && !(next_token_2->flags & PREV_WHITE))
10815 cp_parser_parse_tentatively (parser);
10816 /* Change `:' into `::'. */
10817 next_token_2->type = CPP_SCOPE;
10818 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10820 cp_lexer_consume_token (parser->lexer);
10822 /* Parse the arguments. */
10823 arguments = cp_parser_enclosed_template_argument_list (parser);
10824 if (!cp_parser_parse_definitely (parser))
10826 /* If we couldn't parse an argument list, then we revert our changes
10827 and return simply an error. Maybe this is not a template-id
10829 next_token_2->type = CPP_COLON;
10830 cp_parser_error (parser, "expected %<<%>");
10831 pop_deferring_access_checks ();
10832 return error_mark_node;
10834 /* Otherwise, emit an error about the invalid digraph, but continue
10835 parsing because we got our argument list. */
10836 if (permerror (next_token->location,
10837 "%<<::%> cannot begin a template-argument list"))
10839 static bool hint = false;
10840 inform (next_token->location,
10841 "%<<:%> is an alternate spelling for %<[%>."
10842 " Insert whitespace between %<<%> and %<::%>");
10843 if (!hint && !flag_permissive)
10845 inform (next_token->location, "(if you use %<-fpermissive%>"
10846 " G++ will accept your code)");
10853 /* Look for the `<' that starts the template-argument-list. */
10854 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10856 pop_deferring_access_checks ();
10857 return error_mark_node;
10859 /* Parse the arguments. */
10860 arguments = cp_parser_enclosed_template_argument_list (parser);
10863 /* Build a representation of the specialization. */
10864 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10865 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10866 else if (DECL_CLASS_TEMPLATE_P (templ)
10867 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10869 bool entering_scope;
10870 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10871 template (rather than some instantiation thereof) only if
10872 is not nested within some other construct. For example, in
10873 "template <typename T> void f(T) { A<T>::", A<T> is just an
10874 instantiation of A. */
10875 entering_scope = (template_parm_scope_p ()
10876 && cp_lexer_next_token_is (parser->lexer,
10879 = finish_template_type (templ, arguments, entering_scope);
10883 /* If it's not a class-template or a template-template, it should be
10884 a function-template. */
10885 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10886 || TREE_CODE (templ) == OVERLOAD
10887 || BASELINK_P (templ)));
10889 template_id = lookup_template_function (templ, arguments);
10892 /* If parsing tentatively, replace the sequence of tokens that makes
10893 up the template-id with a CPP_TEMPLATE_ID token. That way,
10894 should we re-parse the token stream, we will not have to repeat
10895 the effort required to do the parse, nor will we issue duplicate
10896 error messages about problems during instantiation of the
10900 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10902 /* Reset the contents of the START_OF_ID token. */
10903 token->type = CPP_TEMPLATE_ID;
10904 /* Retrieve any deferred checks. Do not pop this access checks yet
10905 so the memory will not be reclaimed during token replacing below. */
10906 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10907 token->u.tree_check_value->value = template_id;
10908 token->u.tree_check_value->checks = get_deferred_access_checks ();
10909 token->keyword = RID_MAX;
10911 /* Purge all subsequent tokens. */
10912 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10914 /* ??? Can we actually assume that, if template_id ==
10915 error_mark_node, we will have issued a diagnostic to the
10916 user, as opposed to simply marking the tentative parse as
10918 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10919 error_at (token->location, "parse error in template argument list");
10922 pop_deferring_access_checks ();
10923 return template_id;
10926 /* Parse a template-name.
10931 The standard should actually say:
10935 operator-function-id
10937 A defect report has been filed about this issue.
10939 A conversion-function-id cannot be a template name because they cannot
10940 be part of a template-id. In fact, looking at this code:
10942 a.operator K<int>()
10944 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10945 It is impossible to call a templated conversion-function-id with an
10946 explicit argument list, since the only allowed template parameter is
10947 the type to which it is converting.
10949 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10950 `template' keyword, in a construction like:
10954 In that case `f' is taken to be a template-name, even though there
10955 is no way of knowing for sure.
10957 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10958 name refers to a set of overloaded functions, at least one of which
10959 is a template, or an IDENTIFIER_NODE with the name of the template,
10960 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10961 names are looked up inside uninstantiated templates. */
10964 cp_parser_template_name (cp_parser* parser,
10965 bool template_keyword_p,
10966 bool check_dependency_p,
10967 bool is_declaration,
10968 bool *is_identifier)
10973 cp_token *token = cp_lexer_peek_token (parser->lexer);
10975 /* If the next token is `operator', then we have either an
10976 operator-function-id or a conversion-function-id. */
10977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10979 /* We don't know whether we're looking at an
10980 operator-function-id or a conversion-function-id. */
10981 cp_parser_parse_tentatively (parser);
10982 /* Try an operator-function-id. */
10983 identifier = cp_parser_operator_function_id (parser);
10984 /* If that didn't work, try a conversion-function-id. */
10985 if (!cp_parser_parse_definitely (parser))
10987 cp_parser_error (parser, "expected template-name");
10988 return error_mark_node;
10991 /* Look for the identifier. */
10993 identifier = cp_parser_identifier (parser);
10995 /* If we didn't find an identifier, we don't have a template-id. */
10996 if (identifier == error_mark_node)
10997 return error_mark_node;
10999 /* If the name immediately followed the `template' keyword, then it
11000 is a template-name. However, if the next token is not `<', then
11001 we do not treat it as a template-name, since it is not being used
11002 as part of a template-id. This enables us to handle constructs
11005 template <typename T> struct S { S(); };
11006 template <typename T> S<T>::S();
11008 correctly. We would treat `S' as a template -- if it were `S<T>'
11009 -- but we do not if there is no `<'. */
11011 if (processing_template_decl
11012 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11014 /* In a declaration, in a dependent context, we pretend that the
11015 "template" keyword was present in order to improve error
11016 recovery. For example, given:
11018 template <typename T> void f(T::X<int>);
11020 we want to treat "X<int>" as a template-id. */
11022 && !template_keyword_p
11023 && parser->scope && TYPE_P (parser->scope)
11024 && check_dependency_p
11025 && dependent_scope_p (parser->scope)
11026 /* Do not do this for dtors (or ctors), since they never
11027 need the template keyword before their name. */
11028 && !constructor_name_p (identifier, parser->scope))
11030 cp_token_position start = 0;
11032 /* Explain what went wrong. */
11033 error_at (token->location, "non-template %qD used as template",
11035 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11036 parser->scope, identifier);
11037 /* If parsing tentatively, find the location of the "<" token. */
11038 if (cp_parser_simulate_error (parser))
11039 start = cp_lexer_token_position (parser->lexer, true);
11040 /* Parse the template arguments so that we can issue error
11041 messages about them. */
11042 cp_lexer_consume_token (parser->lexer);
11043 cp_parser_enclosed_template_argument_list (parser);
11044 /* Skip tokens until we find a good place from which to
11045 continue parsing. */
11046 cp_parser_skip_to_closing_parenthesis (parser,
11047 /*recovering=*/true,
11049 /*consume_paren=*/false);
11050 /* If parsing tentatively, permanently remove the
11051 template argument list. That will prevent duplicate
11052 error messages from being issued about the missing
11053 "template" keyword. */
11055 cp_lexer_purge_tokens_after (parser->lexer, start);
11057 *is_identifier = true;
11061 /* If the "template" keyword is present, then there is generally
11062 no point in doing name-lookup, so we just return IDENTIFIER.
11063 But, if the qualifying scope is non-dependent then we can
11064 (and must) do name-lookup normally. */
11065 if (template_keyword_p
11067 || (TYPE_P (parser->scope)
11068 && dependent_type_p (parser->scope))))
11072 /* Look up the name. */
11073 decl = cp_parser_lookup_name (parser, identifier,
11075 /*is_template=*/false,
11076 /*is_namespace=*/false,
11077 check_dependency_p,
11078 /*ambiguous_decls=*/NULL,
11080 decl = maybe_get_template_decl_from_type_decl (decl);
11082 /* If DECL is a template, then the name was a template-name. */
11083 if (TREE_CODE (decl) == TEMPLATE_DECL)
11087 tree fn = NULL_TREE;
11089 /* The standard does not explicitly indicate whether a name that
11090 names a set of overloaded declarations, some of which are
11091 templates, is a template-name. However, such a name should
11092 be a template-name; otherwise, there is no way to form a
11093 template-id for the overloaded templates. */
11094 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11095 if (TREE_CODE (fns) == OVERLOAD)
11096 for (fn = fns; fn; fn = OVL_NEXT (fn))
11097 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11102 /* The name does not name a template. */
11103 cp_parser_error (parser, "expected template-name");
11104 return error_mark_node;
11108 /* If DECL is dependent, and refers to a function, then just return
11109 its name; we will look it up again during template instantiation. */
11110 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11112 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11113 if (TYPE_P (scope) && dependent_type_p (scope))
11120 /* Parse a template-argument-list.
11122 template-argument-list:
11123 template-argument ... [opt]
11124 template-argument-list , template-argument ... [opt]
11126 Returns a TREE_VEC containing the arguments. */
11129 cp_parser_template_argument_list (cp_parser* parser)
11131 tree fixed_args[10];
11132 unsigned n_args = 0;
11133 unsigned alloced = 10;
11134 tree *arg_ary = fixed_args;
11136 bool saved_in_template_argument_list_p;
11138 bool saved_non_ice_p;
11140 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11141 parser->in_template_argument_list_p = true;
11142 /* Even if the template-id appears in an integral
11143 constant-expression, the contents of the argument list do
11145 saved_ice_p = parser->integral_constant_expression_p;
11146 parser->integral_constant_expression_p = false;
11147 saved_non_ice_p = parser->non_integral_constant_expression_p;
11148 parser->non_integral_constant_expression_p = false;
11149 /* Parse the arguments. */
11155 /* Consume the comma. */
11156 cp_lexer_consume_token (parser->lexer);
11158 /* Parse the template-argument. */
11159 argument = cp_parser_template_argument (parser);
11161 /* If the next token is an ellipsis, we're expanding a template
11163 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11165 if (argument == error_mark_node)
11167 cp_token *token = cp_lexer_peek_token (parser->lexer);
11168 error_at (token->location,
11169 "expected parameter pack before %<...%>");
11171 /* Consume the `...' token. */
11172 cp_lexer_consume_token (parser->lexer);
11174 /* Make the argument into a TYPE_PACK_EXPANSION or
11175 EXPR_PACK_EXPANSION. */
11176 argument = make_pack_expansion (argument);
11179 if (n_args == alloced)
11183 if (arg_ary == fixed_args)
11185 arg_ary = XNEWVEC (tree, alloced);
11186 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11189 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11191 arg_ary[n_args++] = argument;
11193 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11195 vec = make_tree_vec (n_args);
11198 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11200 if (arg_ary != fixed_args)
11202 parser->non_integral_constant_expression_p = saved_non_ice_p;
11203 parser->integral_constant_expression_p = saved_ice_p;
11204 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11208 /* Parse a template-argument.
11211 assignment-expression
11215 The representation is that of an assignment-expression, type-id, or
11216 id-expression -- except that the qualified id-expression is
11217 evaluated, so that the value returned is either a DECL or an
11220 Although the standard says "assignment-expression", it forbids
11221 throw-expressions or assignments in the template argument.
11222 Therefore, we use "conditional-expression" instead. */
11225 cp_parser_template_argument (cp_parser* parser)
11230 bool maybe_type_id = false;
11231 cp_token *token = NULL, *argument_start_token = NULL;
11234 /* There's really no way to know what we're looking at, so we just
11235 try each alternative in order.
11239 In a template-argument, an ambiguity between a type-id and an
11240 expression is resolved to a type-id, regardless of the form of
11241 the corresponding template-parameter.
11243 Therefore, we try a type-id first. */
11244 cp_parser_parse_tentatively (parser);
11245 argument = cp_parser_template_type_arg (parser);
11246 /* If there was no error parsing the type-id but the next token is a
11247 '>>', our behavior depends on which dialect of C++ we're
11248 parsing. In C++98, we probably found a typo for '> >'. But there
11249 are type-id which are also valid expressions. For instance:
11251 struct X { int operator >> (int); };
11252 template <int V> struct Foo {};
11255 Here 'X()' is a valid type-id of a function type, but the user just
11256 wanted to write the expression "X() >> 5". Thus, we remember that we
11257 found a valid type-id, but we still try to parse the argument as an
11258 expression to see what happens.
11260 In C++0x, the '>>' will be considered two separate '>'
11262 if (!cp_parser_error_occurred (parser)
11263 && cxx_dialect == cxx98
11264 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11266 maybe_type_id = true;
11267 cp_parser_abort_tentative_parse (parser);
11271 /* If the next token isn't a `,' or a `>', then this argument wasn't
11272 really finished. This means that the argument is not a valid
11274 if (!cp_parser_next_token_ends_template_argument_p (parser))
11275 cp_parser_error (parser, "expected template-argument");
11276 /* If that worked, we're done. */
11277 if (cp_parser_parse_definitely (parser))
11280 /* We're still not sure what the argument will be. */
11281 cp_parser_parse_tentatively (parser);
11282 /* Try a template. */
11283 argument_start_token = cp_lexer_peek_token (parser->lexer);
11284 argument = cp_parser_id_expression (parser,
11285 /*template_keyword_p=*/false,
11286 /*check_dependency_p=*/true,
11288 /*declarator_p=*/false,
11289 /*optional_p=*/false);
11290 /* If the next token isn't a `,' or a `>', then this argument wasn't
11291 really finished. */
11292 if (!cp_parser_next_token_ends_template_argument_p (parser))
11293 cp_parser_error (parser, "expected template-argument");
11294 if (!cp_parser_error_occurred (parser))
11296 /* Figure out what is being referred to. If the id-expression
11297 was for a class template specialization, then we will have a
11298 TYPE_DECL at this point. There is no need to do name lookup
11299 at this point in that case. */
11300 if (TREE_CODE (argument) != TYPE_DECL)
11301 argument = cp_parser_lookup_name (parser, argument,
11303 /*is_template=*/template_p,
11304 /*is_namespace=*/false,
11305 /*check_dependency=*/true,
11306 /*ambiguous_decls=*/NULL,
11307 argument_start_token->location);
11308 if (TREE_CODE (argument) != TEMPLATE_DECL
11309 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11310 cp_parser_error (parser, "expected template-name");
11312 if (cp_parser_parse_definitely (parser))
11314 /* It must be a non-type argument. There permitted cases are given
11315 in [temp.arg.nontype]:
11317 -- an integral constant-expression of integral or enumeration
11320 -- the name of a non-type template-parameter; or
11322 -- the name of an object or function with external linkage...
11324 -- the address of an object or function with external linkage...
11326 -- a pointer to member... */
11327 /* Look for a non-type template parameter. */
11328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11330 cp_parser_parse_tentatively (parser);
11331 argument = cp_parser_primary_expression (parser,
11332 /*address_p=*/false,
11334 /*template_arg_p=*/true,
11336 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11337 || !cp_parser_next_token_ends_template_argument_p (parser))
11338 cp_parser_simulate_error (parser);
11339 if (cp_parser_parse_definitely (parser))
11343 /* If the next token is "&", the argument must be the address of an
11344 object or function with external linkage. */
11345 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11347 cp_lexer_consume_token (parser->lexer);
11348 /* See if we might have an id-expression. */
11349 token = cp_lexer_peek_token (parser->lexer);
11350 if (token->type == CPP_NAME
11351 || token->keyword == RID_OPERATOR
11352 || token->type == CPP_SCOPE
11353 || token->type == CPP_TEMPLATE_ID
11354 || token->type == CPP_NESTED_NAME_SPECIFIER)
11356 cp_parser_parse_tentatively (parser);
11357 argument = cp_parser_primary_expression (parser,
11360 /*template_arg_p=*/true,
11362 if (cp_parser_error_occurred (parser)
11363 || !cp_parser_next_token_ends_template_argument_p (parser))
11364 cp_parser_abort_tentative_parse (parser);
11367 if (TREE_CODE (argument) == INDIRECT_REF)
11369 gcc_assert (REFERENCE_REF_P (argument));
11370 argument = TREE_OPERAND (argument, 0);
11373 if (TREE_CODE (argument) == VAR_DECL)
11375 /* A variable without external linkage might still be a
11376 valid constant-expression, so no error is issued here
11377 if the external-linkage check fails. */
11378 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11379 cp_parser_simulate_error (parser);
11381 else if (is_overloaded_fn (argument))
11382 /* All overloaded functions are allowed; if the external
11383 linkage test does not pass, an error will be issued
11387 && (TREE_CODE (argument) == OFFSET_REF
11388 || TREE_CODE (argument) == SCOPE_REF))
11389 /* A pointer-to-member. */
11391 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11394 cp_parser_simulate_error (parser);
11396 if (cp_parser_parse_definitely (parser))
11399 argument = build_x_unary_op (ADDR_EXPR, argument,
11400 tf_warning_or_error);
11405 /* If the argument started with "&", there are no other valid
11406 alternatives at this point. */
11409 cp_parser_error (parser, "invalid non-type template argument");
11410 return error_mark_node;
11413 /* If the argument wasn't successfully parsed as a type-id followed
11414 by '>>', the argument can only be a constant expression now.
11415 Otherwise, we try parsing the constant-expression tentatively,
11416 because the argument could really be a type-id. */
11418 cp_parser_parse_tentatively (parser);
11419 argument = cp_parser_constant_expression (parser,
11420 /*allow_non_constant_p=*/false,
11421 /*non_constant_p=*/NULL);
11422 argument = fold_non_dependent_expr (argument);
11423 if (!maybe_type_id)
11425 if (!cp_parser_next_token_ends_template_argument_p (parser))
11426 cp_parser_error (parser, "expected template-argument");
11427 if (cp_parser_parse_definitely (parser))
11429 /* We did our best to parse the argument as a non type-id, but that
11430 was the only alternative that matched (albeit with a '>' after
11431 it). We can assume it's just a typo from the user, and a
11432 diagnostic will then be issued. */
11433 return cp_parser_template_type_arg (parser);
11436 /* Parse an explicit-instantiation.
11438 explicit-instantiation:
11439 template declaration
11441 Although the standard says `declaration', what it really means is:
11443 explicit-instantiation:
11444 template decl-specifier-seq [opt] declarator [opt] ;
11446 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11447 supposed to be allowed. A defect report has been filed about this
11452 explicit-instantiation:
11453 storage-class-specifier template
11454 decl-specifier-seq [opt] declarator [opt] ;
11455 function-specifier template
11456 decl-specifier-seq [opt] declarator [opt] ; */
11459 cp_parser_explicit_instantiation (cp_parser* parser)
11461 int declares_class_or_enum;
11462 cp_decl_specifier_seq decl_specifiers;
11463 tree extension_specifier = NULL_TREE;
11466 /* Look for an (optional) storage-class-specifier or
11467 function-specifier. */
11468 if (cp_parser_allow_gnu_extensions_p (parser))
11470 extension_specifier
11471 = cp_parser_storage_class_specifier_opt (parser);
11472 if (!extension_specifier)
11473 extension_specifier
11474 = cp_parser_function_specifier_opt (parser,
11475 /*decl_specs=*/NULL);
11478 /* Look for the `template' keyword. */
11479 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11480 /* Let the front end know that we are processing an explicit
11482 begin_explicit_instantiation ();
11483 /* [temp.explicit] says that we are supposed to ignore access
11484 control while processing explicit instantiation directives. */
11485 push_deferring_access_checks (dk_no_check);
11486 /* Parse a decl-specifier-seq. */
11487 token = cp_lexer_peek_token (parser->lexer);
11488 cp_parser_decl_specifier_seq (parser,
11489 CP_PARSER_FLAGS_OPTIONAL,
11491 &declares_class_or_enum);
11492 /* If there was exactly one decl-specifier, and it declared a class,
11493 and there's no declarator, then we have an explicit type
11495 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11499 type = check_tag_decl (&decl_specifiers);
11500 /* Turn access control back on for names used during
11501 template instantiation. */
11502 pop_deferring_access_checks ();
11504 do_type_instantiation (type, extension_specifier,
11505 /*complain=*/tf_error);
11509 cp_declarator *declarator;
11512 /* Parse the declarator. */
11514 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11515 /*ctor_dtor_or_conv_p=*/NULL,
11516 /*parenthesized_p=*/NULL,
11517 /*member_p=*/false);
11518 if (declares_class_or_enum & 2)
11519 cp_parser_check_for_definition_in_return_type (declarator,
11520 decl_specifiers.type,
11521 decl_specifiers.type_location);
11522 if (declarator != cp_error_declarator)
11524 decl = grokdeclarator (declarator, &decl_specifiers,
11525 NORMAL, 0, &decl_specifiers.attributes);
11526 /* Turn access control back on for names used during
11527 template instantiation. */
11528 pop_deferring_access_checks ();
11529 /* Do the explicit instantiation. */
11530 do_decl_instantiation (decl, extension_specifier);
11534 pop_deferring_access_checks ();
11535 /* Skip the body of the explicit instantiation. */
11536 cp_parser_skip_to_end_of_statement (parser);
11539 /* We're done with the instantiation. */
11540 end_explicit_instantiation ();
11542 cp_parser_consume_semicolon_at_end_of_statement (parser);
11545 /* Parse an explicit-specialization.
11547 explicit-specialization:
11548 template < > declaration
11550 Although the standard says `declaration', what it really means is:
11552 explicit-specialization:
11553 template <> decl-specifier [opt] init-declarator [opt] ;
11554 template <> function-definition
11555 template <> explicit-specialization
11556 template <> template-declaration */
11559 cp_parser_explicit_specialization (cp_parser* parser)
11561 bool need_lang_pop;
11562 cp_token *token = cp_lexer_peek_token (parser->lexer);
11564 /* Look for the `template' keyword. */
11565 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11566 /* Look for the `<'. */
11567 cp_parser_require (parser, CPP_LESS, "%<<%>");
11568 /* Look for the `>'. */
11569 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11570 /* We have processed another parameter list. */
11571 ++parser->num_template_parameter_lists;
11574 A template ... explicit specialization ... shall not have C
11576 if (current_lang_name == lang_name_c)
11578 error_at (token->location, "template specialization with C linkage");
11579 /* Give it C++ linkage to avoid confusing other parts of the
11581 push_lang_context (lang_name_cplusplus);
11582 need_lang_pop = true;
11585 need_lang_pop = false;
11586 /* Let the front end know that we are beginning a specialization. */
11587 if (!begin_specialization ())
11589 end_specialization ();
11593 /* If the next keyword is `template', we need to figure out whether
11594 or not we're looking a template-declaration. */
11595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11597 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11598 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11599 cp_parser_template_declaration_after_export (parser,
11600 /*member_p=*/false);
11602 cp_parser_explicit_specialization (parser);
11605 /* Parse the dependent declaration. */
11606 cp_parser_single_declaration (parser,
11608 /*member_p=*/false,
11609 /*explicit_specialization_p=*/true,
11610 /*friend_p=*/NULL);
11611 /* We're done with the specialization. */
11612 end_specialization ();
11613 /* For the erroneous case of a template with C linkage, we pushed an
11614 implicit C++ linkage scope; exit that scope now. */
11616 pop_lang_context ();
11617 /* We're done with this parameter list. */
11618 --parser->num_template_parameter_lists;
11621 /* Parse a type-specifier.
11624 simple-type-specifier
11627 elaborated-type-specifier
11635 Returns a representation of the type-specifier. For a
11636 class-specifier, enum-specifier, or elaborated-type-specifier, a
11637 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11639 The parser flags FLAGS is used to control type-specifier parsing.
11641 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11642 in a decl-specifier-seq.
11644 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11645 class-specifier, enum-specifier, or elaborated-type-specifier, then
11646 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11647 if a type is declared; 2 if it is defined. Otherwise, it is set to
11650 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11651 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11652 is set to FALSE. */
11655 cp_parser_type_specifier (cp_parser* parser,
11656 cp_parser_flags flags,
11657 cp_decl_specifier_seq *decl_specs,
11658 bool is_declaration,
11659 int* declares_class_or_enum,
11660 bool* is_cv_qualifier)
11662 tree type_spec = NULL_TREE;
11665 cp_decl_spec ds = ds_last;
11667 /* Assume this type-specifier does not declare a new type. */
11668 if (declares_class_or_enum)
11669 *declares_class_or_enum = 0;
11670 /* And that it does not specify a cv-qualifier. */
11671 if (is_cv_qualifier)
11672 *is_cv_qualifier = false;
11673 /* Peek at the next token. */
11674 token = cp_lexer_peek_token (parser->lexer);
11676 /* If we're looking at a keyword, we can use that to guide the
11677 production we choose. */
11678 keyword = token->keyword;
11682 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11683 goto elaborated_type_specifier;
11685 /* Look for the enum-specifier. */
11686 type_spec = cp_parser_enum_specifier (parser);
11687 /* If that worked, we're done. */
11690 if (declares_class_or_enum)
11691 *declares_class_or_enum = 2;
11693 cp_parser_set_decl_spec_type (decl_specs,
11696 /*user_defined_p=*/true);
11700 goto elaborated_type_specifier;
11702 /* Any of these indicate either a class-specifier, or an
11703 elaborated-type-specifier. */
11707 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11708 goto elaborated_type_specifier;
11710 /* Parse tentatively so that we can back up if we don't find a
11711 class-specifier. */
11712 cp_parser_parse_tentatively (parser);
11713 /* Look for the class-specifier. */
11714 type_spec = cp_parser_class_specifier (parser);
11715 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11716 /* If that worked, we're done. */
11717 if (cp_parser_parse_definitely (parser))
11719 if (declares_class_or_enum)
11720 *declares_class_or_enum = 2;
11722 cp_parser_set_decl_spec_type (decl_specs,
11725 /*user_defined_p=*/true);
11729 /* Fall through. */
11730 elaborated_type_specifier:
11731 /* We're declaring (not defining) a class or enum. */
11732 if (declares_class_or_enum)
11733 *declares_class_or_enum = 1;
11735 /* Fall through. */
11737 /* Look for an elaborated-type-specifier. */
11739 = (cp_parser_elaborated_type_specifier
11741 decl_specs && decl_specs->specs[(int) ds_friend],
11744 cp_parser_set_decl_spec_type (decl_specs,
11747 /*user_defined_p=*/true);
11752 if (is_cv_qualifier)
11753 *is_cv_qualifier = true;
11758 if (is_cv_qualifier)
11759 *is_cv_qualifier = true;
11764 if (is_cv_qualifier)
11765 *is_cv_qualifier = true;
11769 /* The `__complex__' keyword is a GNU extension. */
11777 /* Handle simple keywords. */
11782 ++decl_specs->specs[(int)ds];
11783 decl_specs->any_specifiers_p = true;
11785 return cp_lexer_consume_token (parser->lexer)->u.value;
11788 /* If we do not already have a type-specifier, assume we are looking
11789 at a simple-type-specifier. */
11790 type_spec = cp_parser_simple_type_specifier (parser,
11794 /* If we didn't find a type-specifier, and a type-specifier was not
11795 optional in this context, issue an error message. */
11796 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11798 cp_parser_error (parser, "expected type specifier");
11799 return error_mark_node;
11805 /* Parse a simple-type-specifier.
11807 simple-type-specifier:
11808 :: [opt] nested-name-specifier [opt] type-name
11809 :: [opt] nested-name-specifier template template-id
11824 simple-type-specifier:
11826 decltype ( expression )
11832 simple-type-specifier:
11833 __typeof__ unary-expression
11834 __typeof__ ( type-id )
11836 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11837 appropriately updated. */
11840 cp_parser_simple_type_specifier (cp_parser* parser,
11841 cp_decl_specifier_seq *decl_specs,
11842 cp_parser_flags flags)
11844 tree type = NULL_TREE;
11847 /* Peek at the next token. */
11848 token = cp_lexer_peek_token (parser->lexer);
11850 /* If we're looking at a keyword, things are easy. */
11851 switch (token->keyword)
11855 decl_specs->explicit_char_p = true;
11856 type = char_type_node;
11859 type = char16_type_node;
11862 type = char32_type_node;
11865 type = wchar_type_node;
11868 type = boolean_type_node;
11872 ++decl_specs->specs[(int) ds_short];
11873 type = short_integer_type_node;
11877 decl_specs->explicit_int_p = true;
11878 type = integer_type_node;
11882 ++decl_specs->specs[(int) ds_long];
11883 type = long_integer_type_node;
11887 ++decl_specs->specs[(int) ds_signed];
11888 type = integer_type_node;
11892 ++decl_specs->specs[(int) ds_unsigned];
11893 type = unsigned_type_node;
11896 type = float_type_node;
11899 type = double_type_node;
11902 type = void_type_node;
11906 maybe_warn_cpp0x ("C++0x auto");
11907 type = make_auto ();
11911 /* Parse the `decltype' type. */
11912 type = cp_parser_decltype (parser);
11915 cp_parser_set_decl_spec_type (decl_specs, type,
11917 /*user_defined_p=*/true);
11922 /* Consume the `typeof' token. */
11923 cp_lexer_consume_token (parser->lexer);
11924 /* Parse the operand to `typeof'. */
11925 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11926 /* If it is not already a TYPE, take its type. */
11927 if (!TYPE_P (type))
11928 type = finish_typeof (type);
11931 cp_parser_set_decl_spec_type (decl_specs, type,
11933 /*user_defined_p=*/true);
11941 /* If the type-specifier was for a built-in type, we're done. */
11946 /* Record the type. */
11948 && (token->keyword != RID_SIGNED
11949 && token->keyword != RID_UNSIGNED
11950 && token->keyword != RID_SHORT
11951 && token->keyword != RID_LONG))
11952 cp_parser_set_decl_spec_type (decl_specs,
11955 /*user_defined=*/false);
11957 decl_specs->any_specifiers_p = true;
11959 /* Consume the token. */
11960 id = cp_lexer_consume_token (parser->lexer)->u.value;
11962 /* There is no valid C++ program where a non-template type is
11963 followed by a "<". That usually indicates that the user thought
11964 that the type was a template. */
11965 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11967 return TYPE_NAME (type);
11970 /* The type-specifier must be a user-defined type. */
11971 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11976 /* Don't gobble tokens or issue error messages if this is an
11977 optional type-specifier. */
11978 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11979 cp_parser_parse_tentatively (parser);
11981 /* Look for the optional `::' operator. */
11983 = (cp_parser_global_scope_opt (parser,
11984 /*current_scope_valid_p=*/false)
11986 /* Look for the nested-name specifier. */
11988 = (cp_parser_nested_name_specifier_opt (parser,
11989 /*typename_keyword_p=*/false,
11990 /*check_dependency_p=*/true,
11992 /*is_declaration=*/false)
11994 token = cp_lexer_peek_token (parser->lexer);
11995 /* If we have seen a nested-name-specifier, and the next token
11996 is `template', then we are using the template-id production. */
11998 && cp_parser_optional_template_keyword (parser))
12000 /* Look for the template-id. */
12001 type = cp_parser_template_id (parser,
12002 /*template_keyword_p=*/true,
12003 /*check_dependency_p=*/true,
12004 /*is_declaration=*/false);
12005 /* If the template-id did not name a type, we are out of
12007 if (TREE_CODE (type) != TYPE_DECL)
12009 cp_parser_error (parser, "expected template-id for type");
12013 /* Otherwise, look for a type-name. */
12015 type = cp_parser_type_name (parser);
12016 /* Keep track of all name-lookups performed in class scopes. */
12020 && TREE_CODE (type) == TYPE_DECL
12021 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12022 maybe_note_name_used_in_class (DECL_NAME (type), type);
12023 /* If it didn't work out, we don't have a TYPE. */
12024 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12025 && !cp_parser_parse_definitely (parser))
12027 if (type && decl_specs)
12028 cp_parser_set_decl_spec_type (decl_specs, type,
12030 /*user_defined=*/true);
12033 /* If we didn't get a type-name, issue an error message. */
12034 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12036 cp_parser_error (parser, "expected type-name");
12037 return error_mark_node;
12040 /* There is no valid C++ program where a non-template type is
12041 followed by a "<". That usually indicates that the user thought
12042 that the type was a template. */
12043 if (type && type != error_mark_node)
12045 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12046 If it is, then the '<'...'>' enclose protocol names rather than
12047 template arguments, and so everything is fine. */
12048 if (c_dialect_objc ()
12049 && (objc_is_id (type) || objc_is_class_name (type)))
12051 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12052 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12054 /* Clobber the "unqualified" type previously entered into
12055 DECL_SPECS with the new, improved protocol-qualified version. */
12057 decl_specs->type = qual_type;
12062 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12069 /* Parse a type-name.
12082 Returns a TYPE_DECL for the type. */
12085 cp_parser_type_name (cp_parser* parser)
12089 /* We can't know yet whether it is a class-name or not. */
12090 cp_parser_parse_tentatively (parser);
12091 /* Try a class-name. */
12092 type_decl = cp_parser_class_name (parser,
12093 /*typename_keyword_p=*/false,
12094 /*template_keyword_p=*/false,
12096 /*check_dependency_p=*/true,
12097 /*class_head_p=*/false,
12098 /*is_declaration=*/false);
12099 /* If it's not a class-name, keep looking. */
12100 if (!cp_parser_parse_definitely (parser))
12102 /* It must be a typedef-name or an enum-name. */
12103 return cp_parser_nonclass_name (parser);
12109 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12117 Returns a TYPE_DECL for the type. */
12120 cp_parser_nonclass_name (cp_parser* parser)
12125 cp_token *token = cp_lexer_peek_token (parser->lexer);
12126 identifier = cp_parser_identifier (parser);
12127 if (identifier == error_mark_node)
12128 return error_mark_node;
12130 /* Look up the type-name. */
12131 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12133 if (TREE_CODE (type_decl) != TYPE_DECL
12134 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12136 /* See if this is an Objective-C type. */
12137 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12138 tree type = objc_get_protocol_qualified_type (identifier, protos);
12140 type_decl = TYPE_NAME (type);
12143 /* Issue an error if we did not find a type-name. */
12144 if (TREE_CODE (type_decl) != TYPE_DECL)
12146 if (!cp_parser_simulate_error (parser))
12147 cp_parser_name_lookup_error (parser, identifier, type_decl,
12148 "is not a type", token->location);
12149 return error_mark_node;
12151 /* Remember that the name was used in the definition of the
12152 current class so that we can check later to see if the
12153 meaning would have been different after the class was
12154 entirely defined. */
12155 else if (type_decl != error_mark_node
12157 maybe_note_name_used_in_class (identifier, type_decl);
12162 /* Parse an elaborated-type-specifier. Note that the grammar given
12163 here incorporates the resolution to DR68.
12165 elaborated-type-specifier:
12166 class-key :: [opt] nested-name-specifier [opt] identifier
12167 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12168 enum-key :: [opt] nested-name-specifier [opt] identifier
12169 typename :: [opt] nested-name-specifier identifier
12170 typename :: [opt] nested-name-specifier template [opt]
12175 elaborated-type-specifier:
12176 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12177 class-key attributes :: [opt] nested-name-specifier [opt]
12178 template [opt] template-id
12179 enum attributes :: [opt] nested-name-specifier [opt] identifier
12181 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12182 declared `friend'. If IS_DECLARATION is TRUE, then this
12183 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12184 something is being declared.
12186 Returns the TYPE specified. */
12189 cp_parser_elaborated_type_specifier (cp_parser* parser,
12191 bool is_declaration)
12193 enum tag_types tag_type;
12195 tree type = NULL_TREE;
12196 tree attributes = NULL_TREE;
12198 cp_token *token = NULL;
12200 /* See if we're looking at the `enum' keyword. */
12201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12203 /* Consume the `enum' token. */
12204 cp_lexer_consume_token (parser->lexer);
12205 /* Remember that it's an enumeration type. */
12206 tag_type = enum_type;
12207 /* Parse the optional `struct' or `class' key (for C++0x scoped
12209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12210 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12212 if (cxx_dialect == cxx98)
12213 maybe_warn_cpp0x ("scoped enums");
12215 /* Consume the `struct' or `class'. */
12216 cp_lexer_consume_token (parser->lexer);
12218 /* Parse the attributes. */
12219 attributes = cp_parser_attributes_opt (parser);
12221 /* Or, it might be `typename'. */
12222 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12225 /* Consume the `typename' token. */
12226 cp_lexer_consume_token (parser->lexer);
12227 /* Remember that it's a `typename' type. */
12228 tag_type = typename_type;
12230 /* Otherwise it must be a class-key. */
12233 tag_type = cp_parser_class_key (parser);
12234 if (tag_type == none_type)
12235 return error_mark_node;
12236 /* Parse the attributes. */
12237 attributes = cp_parser_attributes_opt (parser);
12240 /* Look for the `::' operator. */
12241 globalscope = cp_parser_global_scope_opt (parser,
12242 /*current_scope_valid_p=*/false);
12243 /* Look for the nested-name-specifier. */
12244 if (tag_type == typename_type && !globalscope)
12246 if (!cp_parser_nested_name_specifier (parser,
12247 /*typename_keyword_p=*/true,
12248 /*check_dependency_p=*/true,
12251 return error_mark_node;
12254 /* Even though `typename' is not present, the proposed resolution
12255 to Core Issue 180 says that in `class A<T>::B', `B' should be
12256 considered a type-name, even if `A<T>' is dependent. */
12257 cp_parser_nested_name_specifier_opt (parser,
12258 /*typename_keyword_p=*/true,
12259 /*check_dependency_p=*/true,
12262 /* For everything but enumeration types, consider a template-id.
12263 For an enumeration type, consider only a plain identifier. */
12264 if (tag_type != enum_type)
12266 bool template_p = false;
12269 /* Allow the `template' keyword. */
12270 template_p = cp_parser_optional_template_keyword (parser);
12271 /* If we didn't see `template', we don't know if there's a
12272 template-id or not. */
12274 cp_parser_parse_tentatively (parser);
12275 /* Parse the template-id. */
12276 token = cp_lexer_peek_token (parser->lexer);
12277 decl = cp_parser_template_id (parser, template_p,
12278 /*check_dependency_p=*/true,
12280 /* If we didn't find a template-id, look for an ordinary
12282 if (!template_p && !cp_parser_parse_definitely (parser))
12284 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12285 in effect, then we must assume that, upon instantiation, the
12286 template will correspond to a class. */
12287 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12288 && tag_type == typename_type)
12289 type = make_typename_type (parser->scope, decl,
12291 /*complain=*/tf_error);
12292 /* If the `typename' keyword is in effect and DECL is not a type
12293 decl. Then type is non existant. */
12294 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12297 type = TREE_TYPE (decl);
12302 token = cp_lexer_peek_token (parser->lexer);
12303 identifier = cp_parser_identifier (parser);
12305 if (identifier == error_mark_node)
12307 parser->scope = NULL_TREE;
12308 return error_mark_node;
12311 /* For a `typename', we needn't call xref_tag. */
12312 if (tag_type == typename_type
12313 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12314 return cp_parser_make_typename_type (parser, parser->scope,
12317 /* Look up a qualified name in the usual way. */
12321 tree ambiguous_decls;
12323 decl = cp_parser_lookup_name (parser, identifier,
12325 /*is_template=*/false,
12326 /*is_namespace=*/false,
12327 /*check_dependency=*/true,
12331 /* If the lookup was ambiguous, an error will already have been
12333 if (ambiguous_decls)
12334 return error_mark_node;
12336 /* If we are parsing friend declaration, DECL may be a
12337 TEMPLATE_DECL tree node here. However, we need to check
12338 whether this TEMPLATE_DECL results in valid code. Consider
12339 the following example:
12342 template <class T> class C {};
12345 template <class T> friend class N::C; // #1, valid code
12347 template <class T> class Y {
12348 friend class N::C; // #2, invalid code
12351 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12352 name lookup of `N::C'. We see that friend declaration must
12353 be template for the code to be valid. Note that
12354 processing_template_decl does not work here since it is
12355 always 1 for the above two cases. */
12357 decl = (cp_parser_maybe_treat_template_as_class
12358 (decl, /*tag_name_p=*/is_friend
12359 && parser->num_template_parameter_lists));
12361 if (TREE_CODE (decl) != TYPE_DECL)
12363 cp_parser_diagnose_invalid_type_name (parser,
12367 return error_mark_node;
12370 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12372 bool allow_template = (parser->num_template_parameter_lists
12373 || DECL_SELF_REFERENCE_P (decl));
12374 type = check_elaborated_type_specifier (tag_type, decl,
12377 if (type == error_mark_node)
12378 return error_mark_node;
12381 /* Forward declarations of nested types, such as
12386 are invalid unless all components preceding the final '::'
12387 are complete. If all enclosing types are complete, these
12388 declarations become merely pointless.
12390 Invalid forward declarations of nested types are errors
12391 caught elsewhere in parsing. Those that are pointless arrive
12394 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12395 && !is_friend && !processing_explicit_instantiation)
12396 warning (0, "declaration %qD does not declare anything", decl);
12398 type = TREE_TYPE (decl);
12402 /* An elaborated-type-specifier sometimes introduces a new type and
12403 sometimes names an existing type. Normally, the rule is that it
12404 introduces a new type only if there is not an existing type of
12405 the same name already in scope. For example, given:
12408 void f() { struct S s; }
12410 the `struct S' in the body of `f' is the same `struct S' as in
12411 the global scope; the existing definition is used. However, if
12412 there were no global declaration, this would introduce a new
12413 local class named `S'.
12415 An exception to this rule applies to the following code:
12417 namespace N { struct S; }
12419 Here, the elaborated-type-specifier names a new type
12420 unconditionally; even if there is already an `S' in the
12421 containing scope this declaration names a new type.
12422 This exception only applies if the elaborated-type-specifier
12423 forms the complete declaration:
12427 A declaration consisting solely of `class-key identifier ;' is
12428 either a redeclaration of the name in the current scope or a
12429 forward declaration of the identifier as a class name. It
12430 introduces the name into the current scope.
12432 We are in this situation precisely when the next token is a `;'.
12434 An exception to the exception is that a `friend' declaration does
12435 *not* name a new type; i.e., given:
12437 struct S { friend struct T; };
12439 `T' is not a new type in the scope of `S'.
12441 Also, `new struct S' or `sizeof (struct S)' never results in the
12442 definition of a new type; a new type can only be declared in a
12443 declaration context. */
12449 /* Friends have special name lookup rules. */
12450 ts = ts_within_enclosing_non_class;
12451 else if (is_declaration
12452 && cp_lexer_next_token_is (parser->lexer,
12454 /* This is a `class-key identifier ;' */
12460 (parser->num_template_parameter_lists
12461 && (cp_parser_next_token_starts_class_definition_p (parser)
12462 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12463 /* An unqualified name was used to reference this type, so
12464 there were no qualifying templates. */
12465 if (!cp_parser_check_template_parameters (parser,
12466 /*num_templates=*/0,
12468 /*declarator=*/NULL))
12469 return error_mark_node;
12470 type = xref_tag (tag_type, identifier, ts, template_p);
12474 if (type == error_mark_node)
12475 return error_mark_node;
12477 /* Allow attributes on forward declarations of classes. */
12480 if (TREE_CODE (type) == TYPENAME_TYPE)
12481 warning (OPT_Wattributes,
12482 "attributes ignored on uninstantiated type");
12483 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12484 && ! processing_explicit_instantiation)
12485 warning (OPT_Wattributes,
12486 "attributes ignored on template instantiation");
12487 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12488 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12490 warning (OPT_Wattributes,
12491 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12494 if (tag_type != enum_type)
12495 cp_parser_check_class_key (tag_type, type);
12497 /* A "<" cannot follow an elaborated type specifier. If that
12498 happens, the user was probably trying to form a template-id. */
12499 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12504 /* Parse an enum-specifier.
12507 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12512 enum struct [C++0x]
12515 : type-specifier-seq
12518 enum-key attributes[opt] identifier [opt] enum-base [opt]
12519 { enumerator-list [opt] }attributes[opt]
12521 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12522 if the token stream isn't an enum-specifier after all. */
12525 cp_parser_enum_specifier (cp_parser* parser)
12530 bool scoped_enum_p = false;
12531 bool has_underlying_type = false;
12532 tree underlying_type = NULL_TREE;
12534 /* Parse tentatively so that we can back up if we don't find a
12536 cp_parser_parse_tentatively (parser);
12538 /* Caller guarantees that the current token is 'enum', an identifier
12539 possibly follows, and the token after that is an opening brace.
12540 If we don't have an identifier, fabricate an anonymous name for
12541 the enumeration being defined. */
12542 cp_lexer_consume_token (parser->lexer);
12544 /* Parse the "class" or "struct", which indicates a scoped
12545 enumeration type in C++0x. */
12546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12547 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12549 if (cxx_dialect == cxx98)
12550 maybe_warn_cpp0x ("scoped enums");
12552 /* Consume the `struct' or `class' token. */
12553 cp_lexer_consume_token (parser->lexer);
12555 scoped_enum_p = true;
12558 attributes = cp_parser_attributes_opt (parser);
12560 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12561 identifier = cp_parser_identifier (parser);
12563 identifier = make_anon_name ();
12565 /* Check for the `:' that denotes a specified underlying type in C++0x.
12566 Note that a ':' could also indicate a bitfield width, however. */
12567 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12569 cp_decl_specifier_seq type_specifiers;
12571 /* Consume the `:'. */
12572 cp_lexer_consume_token (parser->lexer);
12574 /* Parse the type-specifier-seq. */
12575 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12576 /*is_trailing_return=*/false,
12579 /* At this point this is surely not elaborated type specifier. */
12580 if (!cp_parser_parse_definitely (parser))
12583 if (cxx_dialect == cxx98)
12584 maybe_warn_cpp0x ("scoped enums");
12586 has_underlying_type = true;
12588 /* If that didn't work, stop. */
12589 if (type_specifiers.type != error_mark_node)
12591 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12592 /*initialized=*/0, NULL);
12593 if (underlying_type == error_mark_node)
12594 underlying_type = NULL_TREE;
12598 /* Look for the `{' but don't consume it yet. */
12599 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12601 cp_parser_error (parser, "expected %<{%>");
12602 if (has_underlying_type)
12606 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12609 /* Issue an error message if type-definitions are forbidden here. */
12610 if (!cp_parser_check_type_definition (parser))
12611 type = error_mark_node;
12613 /* Create the new type. We do this before consuming the opening
12614 brace so the enum will be recorded as being on the line of its
12615 tag (or the 'enum' keyword, if there is no tag). */
12616 type = start_enum (identifier, underlying_type, scoped_enum_p);
12618 /* Consume the opening brace. */
12619 cp_lexer_consume_token (parser->lexer);
12621 if (type == error_mark_node)
12623 cp_parser_skip_to_end_of_block_or_statement (parser);
12624 return error_mark_node;
12627 /* If the next token is not '}', then there are some enumerators. */
12628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12629 cp_parser_enumerator_list (parser, type);
12631 /* Consume the final '}'. */
12632 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12634 /* Look for trailing attributes to apply to this enumeration, and
12635 apply them if appropriate. */
12636 if (cp_parser_allow_gnu_extensions_p (parser))
12638 tree trailing_attr = cp_parser_attributes_opt (parser);
12639 trailing_attr = chainon (trailing_attr, attributes);
12640 cplus_decl_attributes (&type,
12642 (int) ATTR_FLAG_TYPE_IN_PLACE);
12645 /* Finish up the enumeration. */
12646 finish_enum (type);
12651 /* Parse an enumerator-list. The enumerators all have the indicated
12655 enumerator-definition
12656 enumerator-list , enumerator-definition */
12659 cp_parser_enumerator_list (cp_parser* parser, tree type)
12663 /* Parse an enumerator-definition. */
12664 cp_parser_enumerator_definition (parser, type);
12666 /* If the next token is not a ',', we've reached the end of
12668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12670 /* Otherwise, consume the `,' and keep going. */
12671 cp_lexer_consume_token (parser->lexer);
12672 /* If the next token is a `}', there is a trailing comma. */
12673 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12675 if (!in_system_header)
12676 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12682 /* Parse an enumerator-definition. The enumerator has the indicated
12685 enumerator-definition:
12687 enumerator = constant-expression
12693 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12698 /* Look for the identifier. */
12699 identifier = cp_parser_identifier (parser);
12700 if (identifier == error_mark_node)
12703 /* If the next token is an '=', then there is an explicit value. */
12704 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12706 /* Consume the `=' token. */
12707 cp_lexer_consume_token (parser->lexer);
12708 /* Parse the value. */
12709 value = cp_parser_constant_expression (parser,
12710 /*allow_non_constant_p=*/false,
12716 /* If we are processing a template, make sure the initializer of the
12717 enumerator doesn't contain any bare template parameter pack. */
12718 if (check_for_bare_parameter_packs (value))
12719 value = error_mark_node;
12721 /* Create the enumerator. */
12722 build_enumerator (identifier, value, type);
12725 /* Parse a namespace-name.
12728 original-namespace-name
12731 Returns the NAMESPACE_DECL for the namespace. */
12734 cp_parser_namespace_name (cp_parser* parser)
12737 tree namespace_decl;
12739 cp_token *token = cp_lexer_peek_token (parser->lexer);
12741 /* Get the name of the namespace. */
12742 identifier = cp_parser_identifier (parser);
12743 if (identifier == error_mark_node)
12744 return error_mark_node;
12746 /* Look up the identifier in the currently active scope. Look only
12747 for namespaces, due to:
12749 [basic.lookup.udir]
12751 When looking up a namespace-name in a using-directive or alias
12752 definition, only namespace names are considered.
12756 [basic.lookup.qual]
12758 During the lookup of a name preceding the :: scope resolution
12759 operator, object, function, and enumerator names are ignored.
12761 (Note that cp_parser_qualifying_entity only calls this
12762 function if the token after the name is the scope resolution
12764 namespace_decl = cp_parser_lookup_name (parser, identifier,
12766 /*is_template=*/false,
12767 /*is_namespace=*/true,
12768 /*check_dependency=*/true,
12769 /*ambiguous_decls=*/NULL,
12771 /* If it's not a namespace, issue an error. */
12772 if (namespace_decl == error_mark_node
12773 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12775 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12776 error_at (token->location, "%qD is not a namespace-name", identifier);
12777 cp_parser_error (parser, "expected namespace-name");
12778 namespace_decl = error_mark_node;
12781 return namespace_decl;
12784 /* Parse a namespace-definition.
12786 namespace-definition:
12787 named-namespace-definition
12788 unnamed-namespace-definition
12790 named-namespace-definition:
12791 original-namespace-definition
12792 extension-namespace-definition
12794 original-namespace-definition:
12795 namespace identifier { namespace-body }
12797 extension-namespace-definition:
12798 namespace original-namespace-name { namespace-body }
12800 unnamed-namespace-definition:
12801 namespace { namespace-body } */
12804 cp_parser_namespace_definition (cp_parser* parser)
12806 tree identifier, attribs;
12807 bool has_visibility;
12810 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12813 cp_lexer_consume_token (parser->lexer);
12818 /* Look for the `namespace' keyword. */
12819 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12821 /* Get the name of the namespace. We do not attempt to distinguish
12822 between an original-namespace-definition and an
12823 extension-namespace-definition at this point. The semantic
12824 analysis routines are responsible for that. */
12825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12826 identifier = cp_parser_identifier (parser);
12828 identifier = NULL_TREE;
12830 /* Parse any specified attributes. */
12831 attribs = cp_parser_attributes_opt (parser);
12833 /* Look for the `{' to start the namespace. */
12834 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12835 /* Start the namespace. */
12836 push_namespace (identifier);
12838 /* "inline namespace" is equivalent to a stub namespace definition
12839 followed by a strong using directive. */
12842 tree name_space = current_namespace;
12843 /* Set up namespace association. */
12844 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12845 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12846 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12847 /* Import the contents of the inline namespace. */
12849 do_using_directive (name_space);
12850 push_namespace (identifier);
12853 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12855 /* Parse the body of the namespace. */
12856 cp_parser_namespace_body (parser);
12858 #ifdef HANDLE_PRAGMA_VISIBILITY
12859 if (has_visibility)
12860 pop_visibility (1);
12863 /* Finish the namespace. */
12865 /* Look for the final `}'. */
12866 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12869 /* Parse a namespace-body.
12872 declaration-seq [opt] */
12875 cp_parser_namespace_body (cp_parser* parser)
12877 cp_parser_declaration_seq_opt (parser);
12880 /* Parse a namespace-alias-definition.
12882 namespace-alias-definition:
12883 namespace identifier = qualified-namespace-specifier ; */
12886 cp_parser_namespace_alias_definition (cp_parser* parser)
12889 tree namespace_specifier;
12891 cp_token *token = cp_lexer_peek_token (parser->lexer);
12893 /* Look for the `namespace' keyword. */
12894 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12895 /* Look for the identifier. */
12896 identifier = cp_parser_identifier (parser);
12897 if (identifier == error_mark_node)
12899 /* Look for the `=' token. */
12900 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12901 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12903 error_at (token->location, "%<namespace%> definition is not allowed here");
12904 /* Skip the definition. */
12905 cp_lexer_consume_token (parser->lexer);
12906 if (cp_parser_skip_to_closing_brace (parser))
12907 cp_lexer_consume_token (parser->lexer);
12910 cp_parser_require (parser, CPP_EQ, "%<=%>");
12911 /* Look for the qualified-namespace-specifier. */
12912 namespace_specifier
12913 = cp_parser_qualified_namespace_specifier (parser);
12914 /* Look for the `;' token. */
12915 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12917 /* Register the alias in the symbol table. */
12918 do_namespace_alias (identifier, namespace_specifier);
12921 /* Parse a qualified-namespace-specifier.
12923 qualified-namespace-specifier:
12924 :: [opt] nested-name-specifier [opt] namespace-name
12926 Returns a NAMESPACE_DECL corresponding to the specified
12930 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12932 /* Look for the optional `::'. */
12933 cp_parser_global_scope_opt (parser,
12934 /*current_scope_valid_p=*/false);
12936 /* Look for the optional nested-name-specifier. */
12937 cp_parser_nested_name_specifier_opt (parser,
12938 /*typename_keyword_p=*/false,
12939 /*check_dependency_p=*/true,
12941 /*is_declaration=*/true);
12943 return cp_parser_namespace_name (parser);
12946 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12947 access declaration.
12950 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12951 using :: unqualified-id ;
12953 access-declaration:
12959 cp_parser_using_declaration (cp_parser* parser,
12960 bool access_declaration_p)
12963 bool typename_p = false;
12964 bool global_scope_p;
12969 if (access_declaration_p)
12970 cp_parser_parse_tentatively (parser);
12973 /* Look for the `using' keyword. */
12974 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12976 /* Peek at the next token. */
12977 token = cp_lexer_peek_token (parser->lexer);
12978 /* See if it's `typename'. */
12979 if (token->keyword == RID_TYPENAME)
12981 /* Remember that we've seen it. */
12983 /* Consume the `typename' token. */
12984 cp_lexer_consume_token (parser->lexer);
12988 /* Look for the optional global scope qualification. */
12990 = (cp_parser_global_scope_opt (parser,
12991 /*current_scope_valid_p=*/false)
12994 /* If we saw `typename', or didn't see `::', then there must be a
12995 nested-name-specifier present. */
12996 if (typename_p || !global_scope_p)
12997 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12998 /*check_dependency_p=*/true,
13000 /*is_declaration=*/true);
13001 /* Otherwise, we could be in either of the two productions. In that
13002 case, treat the nested-name-specifier as optional. */
13004 qscope = cp_parser_nested_name_specifier_opt (parser,
13005 /*typename_keyword_p=*/false,
13006 /*check_dependency_p=*/true,
13008 /*is_declaration=*/true);
13010 qscope = global_namespace;
13012 if (access_declaration_p && cp_parser_error_occurred (parser))
13013 /* Something has already gone wrong; there's no need to parse
13014 further. Since an error has occurred, the return value of
13015 cp_parser_parse_definitely will be false, as required. */
13016 return cp_parser_parse_definitely (parser);
13018 token = cp_lexer_peek_token (parser->lexer);
13019 /* Parse the unqualified-id. */
13020 identifier = cp_parser_unqualified_id (parser,
13021 /*template_keyword_p=*/false,
13022 /*check_dependency_p=*/true,
13023 /*declarator_p=*/true,
13024 /*optional_p=*/false);
13026 if (access_declaration_p)
13028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13029 cp_parser_simulate_error (parser);
13030 if (!cp_parser_parse_definitely (parser))
13034 /* The function we call to handle a using-declaration is different
13035 depending on what scope we are in. */
13036 if (qscope == error_mark_node || identifier == error_mark_node)
13038 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13039 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13040 /* [namespace.udecl]
13042 A using declaration shall not name a template-id. */
13043 error_at (token->location,
13044 "a template-id may not appear in a using-declaration");
13047 if (at_class_scope_p ())
13049 /* Create the USING_DECL. */
13050 decl = do_class_using_decl (parser->scope, identifier);
13052 if (check_for_bare_parameter_packs (decl))
13055 /* Add it to the list of members in this class. */
13056 finish_member_declaration (decl);
13060 decl = cp_parser_lookup_name_simple (parser,
13063 if (decl == error_mark_node)
13064 cp_parser_name_lookup_error (parser, identifier,
13067 else if (check_for_bare_parameter_packs (decl))
13069 else if (!at_namespace_scope_p ())
13070 do_local_using_decl (decl, qscope, identifier);
13072 do_toplevel_using_decl (decl, qscope, identifier);
13076 /* Look for the final `;'. */
13077 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13082 /* Parse a using-directive.
13085 using namespace :: [opt] nested-name-specifier [opt]
13086 namespace-name ; */
13089 cp_parser_using_directive (cp_parser* parser)
13091 tree namespace_decl;
13094 /* Look for the `using' keyword. */
13095 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13096 /* And the `namespace' keyword. */
13097 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13098 /* Look for the optional `::' operator. */
13099 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13100 /* And the optional nested-name-specifier. */
13101 cp_parser_nested_name_specifier_opt (parser,
13102 /*typename_keyword_p=*/false,
13103 /*check_dependency_p=*/true,
13105 /*is_declaration=*/true);
13106 /* Get the namespace being used. */
13107 namespace_decl = cp_parser_namespace_name (parser);
13108 /* And any specified attributes. */
13109 attribs = cp_parser_attributes_opt (parser);
13110 /* Update the symbol table. */
13111 parse_using_directive (namespace_decl, attribs);
13112 /* Look for the final `;'. */
13113 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13116 /* Parse an asm-definition.
13119 asm ( string-literal ) ;
13124 asm volatile [opt] ( string-literal ) ;
13125 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13126 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13127 : asm-operand-list [opt] ) ;
13128 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13129 : asm-operand-list [opt]
13130 : asm-clobber-list [opt] ) ;
13131 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13132 : asm-clobber-list [opt]
13133 : asm-goto-list ) ; */
13136 cp_parser_asm_definition (cp_parser* parser)
13139 tree outputs = NULL_TREE;
13140 tree inputs = NULL_TREE;
13141 tree clobbers = NULL_TREE;
13142 tree labels = NULL_TREE;
13144 bool volatile_p = false;
13145 bool extended_p = false;
13146 bool invalid_inputs_p = false;
13147 bool invalid_outputs_p = false;
13148 bool goto_p = false;
13149 const char *missing = NULL;
13151 /* Look for the `asm' keyword. */
13152 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13153 /* See if the next token is `volatile'. */
13154 if (cp_parser_allow_gnu_extensions_p (parser)
13155 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13157 /* Remember that we saw the `volatile' keyword. */
13159 /* Consume the token. */
13160 cp_lexer_consume_token (parser->lexer);
13162 if (cp_parser_allow_gnu_extensions_p (parser)
13163 && parser->in_function_body
13164 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13166 /* Remember that we saw the `goto' keyword. */
13168 /* Consume the token. */
13169 cp_lexer_consume_token (parser->lexer);
13171 /* Look for the opening `('. */
13172 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13174 /* Look for the string. */
13175 string = cp_parser_string_literal (parser, false, false);
13176 if (string == error_mark_node)
13178 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13179 /*consume_paren=*/true);
13183 /* If we're allowing GNU extensions, check for the extended assembly
13184 syntax. Unfortunately, the `:' tokens need not be separated by
13185 a space in C, and so, for compatibility, we tolerate that here
13186 too. Doing that means that we have to treat the `::' operator as
13188 if (cp_parser_allow_gnu_extensions_p (parser)
13189 && parser->in_function_body
13190 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13191 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13193 bool inputs_p = false;
13194 bool clobbers_p = false;
13195 bool labels_p = false;
13197 /* The extended syntax was used. */
13200 /* Look for outputs. */
13201 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13203 /* Consume the `:'. */
13204 cp_lexer_consume_token (parser->lexer);
13205 /* Parse the output-operands. */
13206 if (cp_lexer_next_token_is_not (parser->lexer,
13208 && cp_lexer_next_token_is_not (parser->lexer,
13210 && cp_lexer_next_token_is_not (parser->lexer,
13213 outputs = cp_parser_asm_operand_list (parser);
13215 if (outputs == error_mark_node)
13216 invalid_outputs_p = true;
13218 /* If the next token is `::', there are no outputs, and the
13219 next token is the beginning of the inputs. */
13220 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13221 /* The inputs are coming next. */
13224 /* Look for inputs. */
13226 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13228 /* Consume the `:' or `::'. */
13229 cp_lexer_consume_token (parser->lexer);
13230 /* Parse the output-operands. */
13231 if (cp_lexer_next_token_is_not (parser->lexer,
13233 && cp_lexer_next_token_is_not (parser->lexer,
13235 && cp_lexer_next_token_is_not (parser->lexer,
13237 inputs = cp_parser_asm_operand_list (parser);
13239 if (inputs == error_mark_node)
13240 invalid_inputs_p = true;
13242 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13243 /* The clobbers are coming next. */
13246 /* Look for clobbers. */
13248 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13251 /* Consume the `:' or `::'. */
13252 cp_lexer_consume_token (parser->lexer);
13253 /* Parse the clobbers. */
13254 if (cp_lexer_next_token_is_not (parser->lexer,
13256 && cp_lexer_next_token_is_not (parser->lexer,
13258 clobbers = cp_parser_asm_clobber_list (parser);
13261 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13262 /* The labels are coming next. */
13265 /* Look for labels. */
13267 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13270 /* Consume the `:' or `::'. */
13271 cp_lexer_consume_token (parser->lexer);
13272 /* Parse the labels. */
13273 labels = cp_parser_asm_label_list (parser);
13276 if (goto_p && !labels_p)
13277 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13280 missing = "%<:%> or %<::%>";
13282 /* Look for the closing `)'. */
13283 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13284 missing ? missing : "%<)%>"))
13285 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13286 /*consume_paren=*/true);
13287 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13289 if (!invalid_inputs_p && !invalid_outputs_p)
13291 /* Create the ASM_EXPR. */
13292 if (parser->in_function_body)
13294 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13295 inputs, clobbers, labels);
13296 /* If the extended syntax was not used, mark the ASM_EXPR. */
13299 tree temp = asm_stmt;
13300 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13301 temp = TREE_OPERAND (temp, 0);
13303 ASM_INPUT_P (temp) = 1;
13307 cgraph_add_asm_node (string);
13311 /* Declarators [gram.dcl.decl] */
13313 /* Parse an init-declarator.
13316 declarator initializer [opt]
13321 declarator asm-specification [opt] attributes [opt] initializer [opt]
13323 function-definition:
13324 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13326 decl-specifier-seq [opt] declarator function-try-block
13330 function-definition:
13331 __extension__ function-definition
13333 The DECL_SPECIFIERS apply to this declarator. Returns a
13334 representation of the entity declared. If MEMBER_P is TRUE, then
13335 this declarator appears in a class scope. The new DECL created by
13336 this declarator is returned.
13338 The CHECKS are access checks that should be performed once we know
13339 what entity is being declared (and, therefore, what classes have
13342 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13343 for a function-definition here as well. If the declarator is a
13344 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13345 be TRUE upon return. By that point, the function-definition will
13346 have been completely parsed.
13348 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13352 cp_parser_init_declarator (cp_parser* parser,
13353 cp_decl_specifier_seq *decl_specifiers,
13354 VEC (deferred_access_check,gc)* checks,
13355 bool function_definition_allowed_p,
13357 int declares_class_or_enum,
13358 bool* function_definition_p)
13360 cp_token *token = NULL, *asm_spec_start_token = NULL,
13361 *attributes_start_token = NULL;
13362 cp_declarator *declarator;
13363 tree prefix_attributes;
13365 tree asm_specification;
13367 tree decl = NULL_TREE;
13369 int is_initialized;
13370 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13371 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13373 enum cpp_ttype initialization_kind;
13374 bool is_direct_init = false;
13375 bool is_non_constant_init;
13376 int ctor_dtor_or_conv_p;
13378 tree pushed_scope = NULL;
13380 /* Gather the attributes that were provided with the
13381 decl-specifiers. */
13382 prefix_attributes = decl_specifiers->attributes;
13384 /* Assume that this is not the declarator for a function
13386 if (function_definition_p)
13387 *function_definition_p = false;
13389 /* Defer access checks while parsing the declarator; we cannot know
13390 what names are accessible until we know what is being
13392 resume_deferring_access_checks ();
13394 /* Parse the declarator. */
13395 token = cp_lexer_peek_token (parser->lexer);
13397 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13398 &ctor_dtor_or_conv_p,
13399 /*parenthesized_p=*/NULL,
13400 /*member_p=*/false);
13401 /* Gather up the deferred checks. */
13402 stop_deferring_access_checks ();
13404 /* If the DECLARATOR was erroneous, there's no need to go
13406 if (declarator == cp_error_declarator)
13407 return error_mark_node;
13409 /* Check that the number of template-parameter-lists is OK. */
13410 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13412 return error_mark_node;
13414 if (declares_class_or_enum & 2)
13415 cp_parser_check_for_definition_in_return_type (declarator,
13416 decl_specifiers->type,
13417 decl_specifiers->type_location);
13419 /* Figure out what scope the entity declared by the DECLARATOR is
13420 located in. `grokdeclarator' sometimes changes the scope, so
13421 we compute it now. */
13422 scope = get_scope_of_declarator (declarator);
13424 /* If we're allowing GNU extensions, look for an asm-specification
13426 if (cp_parser_allow_gnu_extensions_p (parser))
13428 /* Look for an asm-specification. */
13429 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13430 asm_specification = cp_parser_asm_specification_opt (parser);
13431 /* And attributes. */
13432 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13433 attributes = cp_parser_attributes_opt (parser);
13437 asm_specification = NULL_TREE;
13438 attributes = NULL_TREE;
13441 /* Peek at the next token. */
13442 token = cp_lexer_peek_token (parser->lexer);
13443 /* Check to see if the token indicates the start of a
13444 function-definition. */
13445 if (function_declarator_p (declarator)
13446 && cp_parser_token_starts_function_definition_p (token))
13448 if (!function_definition_allowed_p)
13450 /* If a function-definition should not appear here, issue an
13452 cp_parser_error (parser,
13453 "a function-definition is not allowed here");
13454 return error_mark_node;
13458 location_t func_brace_location
13459 = cp_lexer_peek_token (parser->lexer)->location;
13461 /* Neither attributes nor an asm-specification are allowed
13462 on a function-definition. */
13463 if (asm_specification)
13464 error_at (asm_spec_start_token->location,
13465 "an asm-specification is not allowed "
13466 "on a function-definition");
13468 error_at (attributes_start_token->location,
13469 "attributes are not allowed on a function-definition");
13470 /* This is a function-definition. */
13471 *function_definition_p = true;
13473 /* Parse the function definition. */
13475 decl = cp_parser_save_member_function_body (parser,
13478 prefix_attributes);
13481 = (cp_parser_function_definition_from_specifiers_and_declarator
13482 (parser, decl_specifiers, prefix_attributes, declarator));
13484 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13486 /* This is where the prologue starts... */
13487 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13488 = func_brace_location;
13497 Only in function declarations for constructors, destructors, and
13498 type conversions can the decl-specifier-seq be omitted.
13500 We explicitly postpone this check past the point where we handle
13501 function-definitions because we tolerate function-definitions
13502 that are missing their return types in some modes. */
13503 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13505 cp_parser_error (parser,
13506 "expected constructor, destructor, or type conversion");
13507 return error_mark_node;
13510 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13511 if (token->type == CPP_EQ
13512 || token->type == CPP_OPEN_PAREN
13513 || token->type == CPP_OPEN_BRACE)
13515 is_initialized = SD_INITIALIZED;
13516 initialization_kind = token->type;
13518 if (token->type == CPP_EQ
13519 && function_declarator_p (declarator))
13521 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13522 if (t2->keyword == RID_DEFAULT)
13523 is_initialized = SD_DEFAULTED;
13524 else if (t2->keyword == RID_DELETE)
13525 is_initialized = SD_DELETED;
13530 /* If the init-declarator isn't initialized and isn't followed by a
13531 `,' or `;', it's not a valid init-declarator. */
13532 if (token->type != CPP_COMMA
13533 && token->type != CPP_SEMICOLON)
13535 cp_parser_error (parser, "expected initializer");
13536 return error_mark_node;
13538 is_initialized = SD_UNINITIALIZED;
13539 initialization_kind = CPP_EOF;
13542 /* Because start_decl has side-effects, we should only call it if we
13543 know we're going ahead. By this point, we know that we cannot
13544 possibly be looking at any other construct. */
13545 cp_parser_commit_to_tentative_parse (parser);
13547 /* If the decl specifiers were bad, issue an error now that we're
13548 sure this was intended to be a declarator. Then continue
13549 declaring the variable(s), as int, to try to cut down on further
13551 if (decl_specifiers->any_specifiers_p
13552 && decl_specifiers->type == error_mark_node)
13554 cp_parser_error (parser, "invalid type in declaration");
13555 decl_specifiers->type = integer_type_node;
13558 /* Check to see whether or not this declaration is a friend. */
13559 friend_p = cp_parser_friend_p (decl_specifiers);
13561 /* Enter the newly declared entry in the symbol table. If we're
13562 processing a declaration in a class-specifier, we wait until
13563 after processing the initializer. */
13566 if (parser->in_unbraced_linkage_specification_p)
13567 decl_specifiers->storage_class = sc_extern;
13568 decl = start_decl (declarator, decl_specifiers,
13569 is_initialized, attributes, prefix_attributes,
13573 /* Enter the SCOPE. That way unqualified names appearing in the
13574 initializer will be looked up in SCOPE. */
13575 pushed_scope = push_scope (scope);
13577 /* Perform deferred access control checks, now that we know in which
13578 SCOPE the declared entity resides. */
13579 if (!member_p && decl)
13581 tree saved_current_function_decl = NULL_TREE;
13583 /* If the entity being declared is a function, pretend that we
13584 are in its scope. If it is a `friend', it may have access to
13585 things that would not otherwise be accessible. */
13586 if (TREE_CODE (decl) == FUNCTION_DECL)
13588 saved_current_function_decl = current_function_decl;
13589 current_function_decl = decl;
13592 /* Perform access checks for template parameters. */
13593 cp_parser_perform_template_parameter_access_checks (checks);
13595 /* Perform the access control checks for the declarator and the
13596 decl-specifiers. */
13597 perform_deferred_access_checks ();
13599 /* Restore the saved value. */
13600 if (TREE_CODE (decl) == FUNCTION_DECL)
13601 current_function_decl = saved_current_function_decl;
13604 /* Parse the initializer. */
13605 initializer = NULL_TREE;
13606 is_direct_init = false;
13607 is_non_constant_init = true;
13608 if (is_initialized)
13610 if (function_declarator_p (declarator))
13612 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13613 if (initialization_kind == CPP_EQ)
13614 initializer = cp_parser_pure_specifier (parser);
13617 /* If the declaration was erroneous, we don't really
13618 know what the user intended, so just silently
13619 consume the initializer. */
13620 if (decl != error_mark_node)
13621 error_at (initializer_start_token->location,
13622 "initializer provided for function");
13623 cp_parser_skip_to_closing_parenthesis (parser,
13624 /*recovering=*/true,
13625 /*or_comma=*/false,
13626 /*consume_paren=*/true);
13631 /* We want to record the extra mangling scope for in-class
13632 initializers of class members and initializers of static data
13633 member templates. The former is a C++0x feature which isn't
13634 implemented yet, and I expect it will involve deferring
13635 parsing of the initializer until end of class as with default
13636 arguments. So right here we only handle the latter. */
13637 if (!member_p && processing_template_decl)
13638 start_lambda_scope (decl);
13639 initializer = cp_parser_initializer (parser,
13641 &is_non_constant_init);
13642 if (!member_p && processing_template_decl)
13643 finish_lambda_scope ();
13647 /* The old parser allows attributes to appear after a parenthesized
13648 initializer. Mark Mitchell proposed removing this functionality
13649 on the GCC mailing lists on 2002-08-13. This parser accepts the
13650 attributes -- but ignores them. */
13651 if (cp_parser_allow_gnu_extensions_p (parser)
13652 && initialization_kind == CPP_OPEN_PAREN)
13653 if (cp_parser_attributes_opt (parser))
13654 warning (OPT_Wattributes,
13655 "attributes after parenthesized initializer ignored");
13657 /* For an in-class declaration, use `grokfield' to create the
13663 pop_scope (pushed_scope);
13664 pushed_scope = false;
13666 decl = grokfield (declarator, decl_specifiers,
13667 initializer, !is_non_constant_init,
13668 /*asmspec=*/NULL_TREE,
13669 prefix_attributes);
13670 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13671 cp_parser_save_default_args (parser, decl);
13674 /* Finish processing the declaration. But, skip friend
13676 if (!friend_p && decl && decl != error_mark_node)
13678 cp_finish_decl (decl,
13679 initializer, !is_non_constant_init,
13681 /* If the initializer is in parentheses, then this is
13682 a direct-initialization, which means that an
13683 `explicit' constructor is OK. Otherwise, an
13684 `explicit' constructor cannot be used. */
13685 ((is_direct_init || !is_initialized)
13686 ? 0 : LOOKUP_ONLYCONVERTING));
13688 else if ((cxx_dialect != cxx98) && friend_p
13689 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13690 /* Core issue #226 (C++0x only): A default template-argument
13691 shall not be specified in a friend class template
13693 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13694 /*is_partial=*/0, /*is_friend_decl=*/1);
13696 if (!friend_p && pushed_scope)
13697 pop_scope (pushed_scope);
13702 /* Parse a declarator.
13706 ptr-operator declarator
13708 abstract-declarator:
13709 ptr-operator abstract-declarator [opt]
13710 direct-abstract-declarator
13715 attributes [opt] direct-declarator
13716 attributes [opt] ptr-operator declarator
13718 abstract-declarator:
13719 attributes [opt] ptr-operator abstract-declarator [opt]
13720 attributes [opt] direct-abstract-declarator
13722 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13723 detect constructor, destructor or conversion operators. It is set
13724 to -1 if the declarator is a name, and +1 if it is a
13725 function. Otherwise it is set to zero. Usually you just want to
13726 test for >0, but internally the negative value is used.
13728 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13729 a decl-specifier-seq unless it declares a constructor, destructor,
13730 or conversion. It might seem that we could check this condition in
13731 semantic analysis, rather than parsing, but that makes it difficult
13732 to handle something like `f()'. We want to notice that there are
13733 no decl-specifiers, and therefore realize that this is an
13734 expression, not a declaration.)
13736 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13737 the declarator is a direct-declarator of the form "(...)".
13739 MEMBER_P is true iff this declarator is a member-declarator. */
13741 static cp_declarator *
13742 cp_parser_declarator (cp_parser* parser,
13743 cp_parser_declarator_kind dcl_kind,
13744 int* ctor_dtor_or_conv_p,
13745 bool* parenthesized_p,
13749 cp_declarator *declarator;
13750 enum tree_code code;
13751 cp_cv_quals cv_quals;
13753 tree attributes = NULL_TREE;
13755 /* Assume this is not a constructor, destructor, or type-conversion
13757 if (ctor_dtor_or_conv_p)
13758 *ctor_dtor_or_conv_p = 0;
13760 if (cp_parser_allow_gnu_extensions_p (parser))
13761 attributes = cp_parser_attributes_opt (parser);
13763 /* Peek at the next token. */
13764 token = cp_lexer_peek_token (parser->lexer);
13766 /* Check for the ptr-operator production. */
13767 cp_parser_parse_tentatively (parser);
13768 /* Parse the ptr-operator. */
13769 code = cp_parser_ptr_operator (parser,
13772 /* If that worked, then we have a ptr-operator. */
13773 if (cp_parser_parse_definitely (parser))
13775 /* If a ptr-operator was found, then this declarator was not
13777 if (parenthesized_p)
13778 *parenthesized_p = true;
13779 /* The dependent declarator is optional if we are parsing an
13780 abstract-declarator. */
13781 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13782 cp_parser_parse_tentatively (parser);
13784 /* Parse the dependent declarator. */
13785 declarator = cp_parser_declarator (parser, dcl_kind,
13786 /*ctor_dtor_or_conv_p=*/NULL,
13787 /*parenthesized_p=*/NULL,
13788 /*member_p=*/false);
13790 /* If we are parsing an abstract-declarator, we must handle the
13791 case where the dependent declarator is absent. */
13792 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13793 && !cp_parser_parse_definitely (parser))
13796 declarator = cp_parser_make_indirect_declarator
13797 (code, class_type, cv_quals, declarator);
13799 /* Everything else is a direct-declarator. */
13802 if (parenthesized_p)
13803 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13805 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13806 ctor_dtor_or_conv_p,
13810 if (attributes && declarator && declarator != cp_error_declarator)
13811 declarator->attributes = attributes;
13816 /* Parse a direct-declarator or direct-abstract-declarator.
13820 direct-declarator ( parameter-declaration-clause )
13821 cv-qualifier-seq [opt]
13822 exception-specification [opt]
13823 direct-declarator [ constant-expression [opt] ]
13826 direct-abstract-declarator:
13827 direct-abstract-declarator [opt]
13828 ( parameter-declaration-clause )
13829 cv-qualifier-seq [opt]
13830 exception-specification [opt]
13831 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13832 ( abstract-declarator )
13834 Returns a representation of the declarator. DCL_KIND is
13835 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13836 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13837 we are parsing a direct-declarator. It is
13838 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13839 of ambiguity we prefer an abstract declarator, as per
13840 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13841 cp_parser_declarator. */
13843 static cp_declarator *
13844 cp_parser_direct_declarator (cp_parser* parser,
13845 cp_parser_declarator_kind dcl_kind,
13846 int* ctor_dtor_or_conv_p,
13850 cp_declarator *declarator = NULL;
13851 tree scope = NULL_TREE;
13852 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13853 bool saved_in_declarator_p = parser->in_declarator_p;
13855 tree pushed_scope = NULL_TREE;
13859 /* Peek at the next token. */
13860 token = cp_lexer_peek_token (parser->lexer);
13861 if (token->type == CPP_OPEN_PAREN)
13863 /* This is either a parameter-declaration-clause, or a
13864 parenthesized declarator. When we know we are parsing a
13865 named declarator, it must be a parenthesized declarator
13866 if FIRST is true. For instance, `(int)' is a
13867 parameter-declaration-clause, with an omitted
13868 direct-abstract-declarator. But `((*))', is a
13869 parenthesized abstract declarator. Finally, when T is a
13870 template parameter `(T)' is a
13871 parameter-declaration-clause, and not a parenthesized
13874 We first try and parse a parameter-declaration-clause,
13875 and then try a nested declarator (if FIRST is true).
13877 It is not an error for it not to be a
13878 parameter-declaration-clause, even when FIRST is
13884 The first is the declaration of a function while the
13885 second is the definition of a variable, including its
13888 Having seen only the parenthesis, we cannot know which of
13889 these two alternatives should be selected. Even more
13890 complex are examples like:
13895 The former is a function-declaration; the latter is a
13896 variable initialization.
13898 Thus again, we try a parameter-declaration-clause, and if
13899 that fails, we back out and return. */
13901 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13904 unsigned saved_num_template_parameter_lists;
13905 bool is_declarator = false;
13908 /* In a member-declarator, the only valid interpretation
13909 of a parenthesis is the start of a
13910 parameter-declaration-clause. (It is invalid to
13911 initialize a static data member with a parenthesized
13912 initializer; only the "=" form of initialization is
13915 cp_parser_parse_tentatively (parser);
13917 /* Consume the `('. */
13918 cp_lexer_consume_token (parser->lexer);
13921 /* If this is going to be an abstract declarator, we're
13922 in a declarator and we can't have default args. */
13923 parser->default_arg_ok_p = false;
13924 parser->in_declarator_p = true;
13927 /* Inside the function parameter list, surrounding
13928 template-parameter-lists do not apply. */
13929 saved_num_template_parameter_lists
13930 = parser->num_template_parameter_lists;
13931 parser->num_template_parameter_lists = 0;
13933 begin_scope (sk_function_parms, NULL_TREE);
13935 /* Parse the parameter-declaration-clause. */
13936 params = cp_parser_parameter_declaration_clause (parser);
13938 parser->num_template_parameter_lists
13939 = saved_num_template_parameter_lists;
13941 /* If all went well, parse the cv-qualifier-seq and the
13942 exception-specification. */
13943 if (member_p || cp_parser_parse_definitely (parser))
13945 cp_cv_quals cv_quals;
13946 tree exception_specification;
13949 is_declarator = true;
13951 if (ctor_dtor_or_conv_p)
13952 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13954 /* Consume the `)'. */
13955 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13957 /* Parse the cv-qualifier-seq. */
13958 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13959 /* And the exception-specification. */
13960 exception_specification
13961 = cp_parser_exception_specification_opt (parser);
13964 = cp_parser_late_return_type_opt (parser);
13966 /* Create the function-declarator. */
13967 declarator = make_call_declarator (declarator,
13970 exception_specification,
13972 /* Any subsequent parameter lists are to do with
13973 return type, so are not those of the declared
13975 parser->default_arg_ok_p = false;
13978 /* Remove the function parms from scope. */
13979 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13980 pop_binding (DECL_NAME (t), t);
13984 /* Repeat the main loop. */
13988 /* If this is the first, we can try a parenthesized
13992 bool saved_in_type_id_in_expr_p;
13994 parser->default_arg_ok_p = saved_default_arg_ok_p;
13995 parser->in_declarator_p = saved_in_declarator_p;
13997 /* Consume the `('. */
13998 cp_lexer_consume_token (parser->lexer);
13999 /* Parse the nested declarator. */
14000 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14001 parser->in_type_id_in_expr_p = true;
14003 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14004 /*parenthesized_p=*/NULL,
14006 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14008 /* Expect a `)'. */
14009 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14010 declarator = cp_error_declarator;
14011 if (declarator == cp_error_declarator)
14014 goto handle_declarator;
14016 /* Otherwise, we must be done. */
14020 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14021 && token->type == CPP_OPEN_SQUARE)
14023 /* Parse an array-declarator. */
14026 if (ctor_dtor_or_conv_p)
14027 *ctor_dtor_or_conv_p = 0;
14030 parser->default_arg_ok_p = false;
14031 parser->in_declarator_p = true;
14032 /* Consume the `['. */
14033 cp_lexer_consume_token (parser->lexer);
14034 /* Peek at the next token. */
14035 token = cp_lexer_peek_token (parser->lexer);
14036 /* If the next token is `]', then there is no
14037 constant-expression. */
14038 if (token->type != CPP_CLOSE_SQUARE)
14040 bool non_constant_p;
14043 = cp_parser_constant_expression (parser,
14044 /*allow_non_constant=*/true,
14046 if (!non_constant_p)
14047 bounds = fold_non_dependent_expr (bounds);
14048 /* Normally, the array bound must be an integral constant
14049 expression. However, as an extension, we allow VLAs
14050 in function scopes. */
14051 else if (!parser->in_function_body)
14053 error_at (token->location,
14054 "array bound is not an integer constant");
14055 bounds = error_mark_node;
14057 else if (processing_template_decl && !error_operand_p (bounds))
14059 /* Remember this wasn't a constant-expression. */
14060 bounds = build_nop (TREE_TYPE (bounds), bounds);
14061 TREE_SIDE_EFFECTS (bounds) = 1;
14065 bounds = NULL_TREE;
14066 /* Look for the closing `]'. */
14067 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14069 declarator = cp_error_declarator;
14073 declarator = make_array_declarator (declarator, bounds);
14075 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14078 tree qualifying_scope;
14079 tree unqualified_name;
14080 special_function_kind sfk;
14082 bool pack_expansion_p = false;
14083 cp_token *declarator_id_start_token;
14085 /* Parse a declarator-id */
14086 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14089 cp_parser_parse_tentatively (parser);
14091 /* If we see an ellipsis, we should be looking at a
14093 if (token->type == CPP_ELLIPSIS)
14095 /* Consume the `...' */
14096 cp_lexer_consume_token (parser->lexer);
14098 pack_expansion_p = true;
14102 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14104 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14105 qualifying_scope = parser->scope;
14110 if (!unqualified_name && pack_expansion_p)
14112 /* Check whether an error occurred. */
14113 okay = !cp_parser_error_occurred (parser);
14115 /* We already consumed the ellipsis to mark a
14116 parameter pack, but we have no way to report it,
14117 so abort the tentative parse. We will be exiting
14118 immediately anyway. */
14119 cp_parser_abort_tentative_parse (parser);
14122 okay = cp_parser_parse_definitely (parser);
14125 unqualified_name = error_mark_node;
14126 else if (unqualified_name
14127 && (qualifying_scope
14128 || (TREE_CODE (unqualified_name)
14129 != IDENTIFIER_NODE)))
14131 cp_parser_error (parser, "expected unqualified-id");
14132 unqualified_name = error_mark_node;
14136 if (!unqualified_name)
14138 if (unqualified_name == error_mark_node)
14140 declarator = cp_error_declarator;
14141 pack_expansion_p = false;
14142 declarator->parameter_pack_p = false;
14146 if (qualifying_scope && at_namespace_scope_p ()
14147 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14149 /* In the declaration of a member of a template class
14150 outside of the class itself, the SCOPE will sometimes
14151 be a TYPENAME_TYPE. For example, given:
14153 template <typename T>
14154 int S<T>::R::i = 3;
14156 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14157 this context, we must resolve S<T>::R to an ordinary
14158 type, rather than a typename type.
14160 The reason we normally avoid resolving TYPENAME_TYPEs
14161 is that a specialization of `S' might render
14162 `S<T>::R' not a type. However, if `S' is
14163 specialized, then this `i' will not be used, so there
14164 is no harm in resolving the types here. */
14167 /* Resolve the TYPENAME_TYPE. */
14168 type = resolve_typename_type (qualifying_scope,
14169 /*only_current_p=*/false);
14170 /* If that failed, the declarator is invalid. */
14171 if (TREE_CODE (type) == TYPENAME_TYPE)
14172 error_at (declarator_id_start_token->location,
14173 "%<%T::%E%> is not a type",
14174 TYPE_CONTEXT (qualifying_scope),
14175 TYPE_IDENTIFIER (qualifying_scope));
14176 qualifying_scope = type;
14181 if (unqualified_name)
14185 if (qualifying_scope
14186 && CLASS_TYPE_P (qualifying_scope))
14187 class_type = qualifying_scope;
14189 class_type = current_class_type;
14191 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14193 tree name_type = TREE_TYPE (unqualified_name);
14194 if (class_type && same_type_p (name_type, class_type))
14196 if (qualifying_scope
14197 && CLASSTYPE_USE_TEMPLATE (name_type))
14199 error_at (declarator_id_start_token->location,
14200 "invalid use of constructor as a template");
14201 inform (declarator_id_start_token->location,
14202 "use %<%T::%D%> instead of %<%T::%D%> to "
14203 "name the constructor in a qualified name",
14205 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14206 class_type, name_type);
14207 declarator = cp_error_declarator;
14211 unqualified_name = constructor_name (class_type);
14215 /* We do not attempt to print the declarator
14216 here because we do not have enough
14217 information about its original syntactic
14219 cp_parser_error (parser, "invalid declarator");
14220 declarator = cp_error_declarator;
14227 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14228 sfk = sfk_destructor;
14229 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14230 sfk = sfk_conversion;
14231 else if (/* There's no way to declare a constructor
14232 for an anonymous type, even if the type
14233 got a name for linkage purposes. */
14234 !TYPE_WAS_ANONYMOUS (class_type)
14235 && constructor_name_p (unqualified_name,
14238 unqualified_name = constructor_name (class_type);
14239 sfk = sfk_constructor;
14242 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14243 *ctor_dtor_or_conv_p = -1;
14246 declarator = make_id_declarator (qualifying_scope,
14249 declarator->id_loc = token->location;
14250 declarator->parameter_pack_p = pack_expansion_p;
14252 if (pack_expansion_p)
14253 maybe_warn_variadic_templates ();
14256 handle_declarator:;
14257 scope = get_scope_of_declarator (declarator);
14259 /* Any names that appear after the declarator-id for a
14260 member are looked up in the containing scope. */
14261 pushed_scope = push_scope (scope);
14262 parser->in_declarator_p = true;
14263 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14264 || (declarator && declarator->kind == cdk_id))
14265 /* Default args are only allowed on function
14267 parser->default_arg_ok_p = saved_default_arg_ok_p;
14269 parser->default_arg_ok_p = false;
14278 /* For an abstract declarator, we might wind up with nothing at this
14279 point. That's an error; the declarator is not optional. */
14281 cp_parser_error (parser, "expected declarator");
14283 /* If we entered a scope, we must exit it now. */
14285 pop_scope (pushed_scope);
14287 parser->default_arg_ok_p = saved_default_arg_ok_p;
14288 parser->in_declarator_p = saved_in_declarator_p;
14293 /* Parse a ptr-operator.
14296 * cv-qualifier-seq [opt]
14298 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14303 & cv-qualifier-seq [opt]
14305 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14306 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14307 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14308 filled in with the TYPE containing the member. *CV_QUALS is
14309 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14310 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14311 Note that the tree codes returned by this function have nothing
14312 to do with the types of trees that will be eventually be created
14313 to represent the pointer or reference type being parsed. They are
14314 just constants with suggestive names. */
14315 static enum tree_code
14316 cp_parser_ptr_operator (cp_parser* parser,
14318 cp_cv_quals *cv_quals)
14320 enum tree_code code = ERROR_MARK;
14323 /* Assume that it's not a pointer-to-member. */
14325 /* And that there are no cv-qualifiers. */
14326 *cv_quals = TYPE_UNQUALIFIED;
14328 /* Peek at the next token. */
14329 token = cp_lexer_peek_token (parser->lexer);
14331 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14332 if (token->type == CPP_MULT)
14333 code = INDIRECT_REF;
14334 else if (token->type == CPP_AND)
14336 else if ((cxx_dialect != cxx98) &&
14337 token->type == CPP_AND_AND) /* C++0x only */
14338 code = NON_LVALUE_EXPR;
14340 if (code != ERROR_MARK)
14342 /* Consume the `*', `&' or `&&'. */
14343 cp_lexer_consume_token (parser->lexer);
14345 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14346 `&', if we are allowing GNU extensions. (The only qualifier
14347 that can legally appear after `&' is `restrict', but that is
14348 enforced during semantic analysis. */
14349 if (code == INDIRECT_REF
14350 || cp_parser_allow_gnu_extensions_p (parser))
14351 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14355 /* Try the pointer-to-member case. */
14356 cp_parser_parse_tentatively (parser);
14357 /* Look for the optional `::' operator. */
14358 cp_parser_global_scope_opt (parser,
14359 /*current_scope_valid_p=*/false);
14360 /* Look for the nested-name specifier. */
14361 token = cp_lexer_peek_token (parser->lexer);
14362 cp_parser_nested_name_specifier (parser,
14363 /*typename_keyword_p=*/false,
14364 /*check_dependency_p=*/true,
14366 /*is_declaration=*/false);
14367 /* If we found it, and the next token is a `*', then we are
14368 indeed looking at a pointer-to-member operator. */
14369 if (!cp_parser_error_occurred (parser)
14370 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14372 /* Indicate that the `*' operator was used. */
14373 code = INDIRECT_REF;
14375 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14376 error_at (token->location, "%qD is a namespace", parser->scope);
14379 /* The type of which the member is a member is given by the
14381 *type = parser->scope;
14382 /* The next name will not be qualified. */
14383 parser->scope = NULL_TREE;
14384 parser->qualifying_scope = NULL_TREE;
14385 parser->object_scope = NULL_TREE;
14386 /* Look for the optional cv-qualifier-seq. */
14387 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14390 /* If that didn't work we don't have a ptr-operator. */
14391 if (!cp_parser_parse_definitely (parser))
14392 cp_parser_error (parser, "expected ptr-operator");
14398 /* Parse an (optional) cv-qualifier-seq.
14401 cv-qualifier cv-qualifier-seq [opt]
14412 Returns a bitmask representing the cv-qualifiers. */
14415 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14417 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14422 cp_cv_quals cv_qualifier;
14424 /* Peek at the next token. */
14425 token = cp_lexer_peek_token (parser->lexer);
14426 /* See if it's a cv-qualifier. */
14427 switch (token->keyword)
14430 cv_qualifier = TYPE_QUAL_CONST;
14434 cv_qualifier = TYPE_QUAL_VOLATILE;
14438 cv_qualifier = TYPE_QUAL_RESTRICT;
14442 cv_qualifier = TYPE_UNQUALIFIED;
14449 if (cv_quals & cv_qualifier)
14451 error_at (token->location, "duplicate cv-qualifier");
14452 cp_lexer_purge_token (parser->lexer);
14456 cp_lexer_consume_token (parser->lexer);
14457 cv_quals |= cv_qualifier;
14464 /* Parse a late-specified return type, if any. This is not a separate
14465 non-terminal, but part of a function declarator, which looks like
14467 -> trailing-type-specifier-seq abstract-declarator(opt)
14469 Returns the type indicated by the type-id. */
14472 cp_parser_late_return_type_opt (cp_parser* parser)
14476 /* Peek at the next token. */
14477 token = cp_lexer_peek_token (parser->lexer);
14478 /* A late-specified return type is indicated by an initial '->'. */
14479 if (token->type != CPP_DEREF)
14482 /* Consume the ->. */
14483 cp_lexer_consume_token (parser->lexer);
14485 return cp_parser_trailing_type_id (parser);
14488 /* Parse a declarator-id.
14492 :: [opt] nested-name-specifier [opt] type-name
14494 In the `id-expression' case, the value returned is as for
14495 cp_parser_id_expression if the id-expression was an unqualified-id.
14496 If the id-expression was a qualified-id, then a SCOPE_REF is
14497 returned. The first operand is the scope (either a NAMESPACE_DECL
14498 or TREE_TYPE), but the second is still just a representation of an
14502 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14505 /* The expression must be an id-expression. Assume that qualified
14506 names are the names of types so that:
14509 int S<T>::R::i = 3;
14511 will work; we must treat `S<T>::R' as the name of a type.
14512 Similarly, assume that qualified names are templates, where
14516 int S<T>::R<T>::i = 3;
14519 id = cp_parser_id_expression (parser,
14520 /*template_keyword_p=*/false,
14521 /*check_dependency_p=*/false,
14522 /*template_p=*/NULL,
14523 /*declarator_p=*/true,
14525 if (id && BASELINK_P (id))
14526 id = BASELINK_FUNCTIONS (id);
14530 /* Parse a type-id.
14533 type-specifier-seq abstract-declarator [opt]
14535 Returns the TYPE specified. */
14538 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14539 bool is_trailing_return)
14541 cp_decl_specifier_seq type_specifier_seq;
14542 cp_declarator *abstract_declarator;
14544 /* Parse the type-specifier-seq. */
14545 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14546 is_trailing_return,
14547 &type_specifier_seq);
14548 if (type_specifier_seq.type == error_mark_node)
14549 return error_mark_node;
14551 /* There might or might not be an abstract declarator. */
14552 cp_parser_parse_tentatively (parser);
14553 /* Look for the declarator. */
14554 abstract_declarator
14555 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14556 /*parenthesized_p=*/NULL,
14557 /*member_p=*/false);
14558 /* Check to see if there really was a declarator. */
14559 if (!cp_parser_parse_definitely (parser))
14560 abstract_declarator = NULL;
14562 if (type_specifier_seq.type
14563 && type_uses_auto (type_specifier_seq.type))
14565 /* A type-id with type 'auto' is only ok if the abstract declarator
14566 is a function declarator with a late-specified return type. */
14567 if (abstract_declarator
14568 && abstract_declarator->kind == cdk_function
14569 && abstract_declarator->u.function.late_return_type)
14573 error ("invalid use of %<auto%>");
14574 return error_mark_node;
14578 return groktypename (&type_specifier_seq, abstract_declarator,
14582 static tree cp_parser_type_id (cp_parser *parser)
14584 return cp_parser_type_id_1 (parser, false, false);
14587 static tree cp_parser_template_type_arg (cp_parser *parser)
14589 return cp_parser_type_id_1 (parser, true, false);
14592 static tree cp_parser_trailing_type_id (cp_parser *parser)
14594 return cp_parser_type_id_1 (parser, false, true);
14597 /* Parse a type-specifier-seq.
14599 type-specifier-seq:
14600 type-specifier type-specifier-seq [opt]
14604 type-specifier-seq:
14605 attributes type-specifier-seq [opt]
14607 If IS_DECLARATION is true, we are at the start of a "condition" or
14608 exception-declaration, so we might be followed by a declarator-id.
14610 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14611 i.e. we've just seen "->".
14613 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14616 cp_parser_type_specifier_seq (cp_parser* parser,
14617 bool is_declaration,
14618 bool is_trailing_return,
14619 cp_decl_specifier_seq *type_specifier_seq)
14621 bool seen_type_specifier = false;
14622 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14623 cp_token *start_token = NULL;
14625 /* Clear the TYPE_SPECIFIER_SEQ. */
14626 clear_decl_specs (type_specifier_seq);
14628 /* In the context of a trailing return type, enum E { } is an
14629 elaborated-type-specifier followed by a function-body, not an
14631 if (is_trailing_return)
14632 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14634 /* Parse the type-specifiers and attributes. */
14637 tree type_specifier;
14638 bool is_cv_qualifier;
14640 /* Check for attributes first. */
14641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14643 type_specifier_seq->attributes =
14644 chainon (type_specifier_seq->attributes,
14645 cp_parser_attributes_opt (parser));
14649 /* record the token of the beginning of the type specifier seq,
14650 for error reporting purposes*/
14652 start_token = cp_lexer_peek_token (parser->lexer);
14654 /* Look for the type-specifier. */
14655 type_specifier = cp_parser_type_specifier (parser,
14657 type_specifier_seq,
14658 /*is_declaration=*/false,
14661 if (!type_specifier)
14663 /* If the first type-specifier could not be found, this is not a
14664 type-specifier-seq at all. */
14665 if (!seen_type_specifier)
14667 cp_parser_error (parser, "expected type-specifier");
14668 type_specifier_seq->type = error_mark_node;
14671 /* If subsequent type-specifiers could not be found, the
14672 type-specifier-seq is complete. */
14676 seen_type_specifier = true;
14677 /* The standard says that a condition can be:
14679 type-specifier-seq declarator = assignment-expression
14686 we should treat the "S" as a declarator, not as a
14687 type-specifier. The standard doesn't say that explicitly for
14688 type-specifier-seq, but it does say that for
14689 decl-specifier-seq in an ordinary declaration. Perhaps it
14690 would be clearer just to allow a decl-specifier-seq here, and
14691 then add a semantic restriction that if any decl-specifiers
14692 that are not type-specifiers appear, the program is invalid. */
14693 if (is_declaration && !is_cv_qualifier)
14694 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14697 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14700 /* Parse a parameter-declaration-clause.
14702 parameter-declaration-clause:
14703 parameter-declaration-list [opt] ... [opt]
14704 parameter-declaration-list , ...
14706 Returns a representation for the parameter declarations. A return
14707 value of NULL indicates a parameter-declaration-clause consisting
14708 only of an ellipsis. */
14711 cp_parser_parameter_declaration_clause (cp_parser* parser)
14718 /* Peek at the next token. */
14719 token = cp_lexer_peek_token (parser->lexer);
14720 /* Check for trivial parameter-declaration-clauses. */
14721 if (token->type == CPP_ELLIPSIS)
14723 /* Consume the `...' token. */
14724 cp_lexer_consume_token (parser->lexer);
14727 else if (token->type == CPP_CLOSE_PAREN)
14728 /* There are no parameters. */
14730 #ifndef NO_IMPLICIT_EXTERN_C
14731 if (in_system_header && current_class_type == NULL
14732 && current_lang_name == lang_name_c)
14736 return void_list_node;
14738 /* Check for `(void)', too, which is a special case. */
14739 else if (token->keyword == RID_VOID
14740 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14741 == CPP_CLOSE_PAREN))
14743 /* Consume the `void' token. */
14744 cp_lexer_consume_token (parser->lexer);
14745 /* There are no parameters. */
14746 return void_list_node;
14749 /* Parse the parameter-declaration-list. */
14750 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14751 /* If a parse error occurred while parsing the
14752 parameter-declaration-list, then the entire
14753 parameter-declaration-clause is erroneous. */
14757 /* Peek at the next token. */
14758 token = cp_lexer_peek_token (parser->lexer);
14759 /* If it's a `,', the clause should terminate with an ellipsis. */
14760 if (token->type == CPP_COMMA)
14762 /* Consume the `,'. */
14763 cp_lexer_consume_token (parser->lexer);
14764 /* Expect an ellipsis. */
14766 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14768 /* It might also be `...' if the optional trailing `,' was
14770 else if (token->type == CPP_ELLIPSIS)
14772 /* Consume the `...' token. */
14773 cp_lexer_consume_token (parser->lexer);
14774 /* And remember that we saw it. */
14778 ellipsis_p = false;
14780 /* Finish the parameter list. */
14782 parameters = chainon (parameters, void_list_node);
14787 /* Parse a parameter-declaration-list.
14789 parameter-declaration-list:
14790 parameter-declaration
14791 parameter-declaration-list , parameter-declaration
14793 Returns a representation of the parameter-declaration-list, as for
14794 cp_parser_parameter_declaration_clause. However, the
14795 `void_list_node' is never appended to the list. Upon return,
14796 *IS_ERROR will be true iff an error occurred. */
14799 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14801 tree parameters = NULL_TREE;
14802 tree *tail = ¶meters;
14803 bool saved_in_unbraced_linkage_specification_p;
14806 /* Assume all will go well. */
14808 /* The special considerations that apply to a function within an
14809 unbraced linkage specifications do not apply to the parameters
14810 to the function. */
14811 saved_in_unbraced_linkage_specification_p
14812 = parser->in_unbraced_linkage_specification_p;
14813 parser->in_unbraced_linkage_specification_p = false;
14815 /* Look for more parameters. */
14818 cp_parameter_declarator *parameter;
14819 tree decl = error_mark_node;
14820 bool parenthesized_p;
14821 /* Parse the parameter. */
14823 = cp_parser_parameter_declaration (parser,
14824 /*template_parm_p=*/false,
14827 /* We don't know yet if the enclosing context is deprecated, so wait
14828 and warn in grokparms if appropriate. */
14829 deprecated_state = DEPRECATED_SUPPRESS;
14832 decl = grokdeclarator (parameter->declarator,
14833 ¶meter->decl_specifiers,
14835 parameter->default_argument != NULL_TREE,
14836 ¶meter->decl_specifiers.attributes);
14838 deprecated_state = DEPRECATED_NORMAL;
14840 /* If a parse error occurred parsing the parameter declaration,
14841 then the entire parameter-declaration-list is erroneous. */
14842 if (decl == error_mark_node)
14845 parameters = error_mark_node;
14849 if (parameter->decl_specifiers.attributes)
14850 cplus_decl_attributes (&decl,
14851 parameter->decl_specifiers.attributes,
14853 if (DECL_NAME (decl))
14854 decl = pushdecl (decl);
14856 if (decl != error_mark_node)
14858 retrofit_lang_decl (decl);
14859 DECL_PARM_INDEX (decl) = ++index;
14862 /* Add the new parameter to the list. */
14863 *tail = build_tree_list (parameter->default_argument, decl);
14864 tail = &TREE_CHAIN (*tail);
14866 /* Peek at the next token. */
14867 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14868 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14869 /* These are for Objective-C++ */
14870 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14871 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14872 /* The parameter-declaration-list is complete. */
14874 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14878 /* Peek at the next token. */
14879 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14880 /* If it's an ellipsis, then the list is complete. */
14881 if (token->type == CPP_ELLIPSIS)
14883 /* Otherwise, there must be more parameters. Consume the
14885 cp_lexer_consume_token (parser->lexer);
14886 /* When parsing something like:
14888 int i(float f, double d)
14890 we can tell after seeing the declaration for "f" that we
14891 are not looking at an initialization of a variable "i",
14892 but rather at the declaration of a function "i".
14894 Due to the fact that the parsing of template arguments
14895 (as specified to a template-id) requires backtracking we
14896 cannot use this technique when inside a template argument
14898 if (!parser->in_template_argument_list_p
14899 && !parser->in_type_id_in_expr_p
14900 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14901 /* However, a parameter-declaration of the form
14902 "foat(f)" (which is a valid declaration of a
14903 parameter "f") can also be interpreted as an
14904 expression (the conversion of "f" to "float"). */
14905 && !parenthesized_p)
14906 cp_parser_commit_to_tentative_parse (parser);
14910 cp_parser_error (parser, "expected %<,%> or %<...%>");
14911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14912 cp_parser_skip_to_closing_parenthesis (parser,
14913 /*recovering=*/true,
14914 /*or_comma=*/false,
14915 /*consume_paren=*/false);
14920 parser->in_unbraced_linkage_specification_p
14921 = saved_in_unbraced_linkage_specification_p;
14926 /* Parse a parameter declaration.
14928 parameter-declaration:
14929 decl-specifier-seq ... [opt] declarator
14930 decl-specifier-seq declarator = assignment-expression
14931 decl-specifier-seq ... [opt] abstract-declarator [opt]
14932 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14934 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14935 declares a template parameter. (In that case, a non-nested `>'
14936 token encountered during the parsing of the assignment-expression
14937 is not interpreted as a greater-than operator.)
14939 Returns a representation of the parameter, or NULL if an error
14940 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14941 true iff the declarator is of the form "(p)". */
14943 static cp_parameter_declarator *
14944 cp_parser_parameter_declaration (cp_parser *parser,
14945 bool template_parm_p,
14946 bool *parenthesized_p)
14948 int declares_class_or_enum;
14949 bool greater_than_is_operator_p;
14950 cp_decl_specifier_seq decl_specifiers;
14951 cp_declarator *declarator;
14952 tree default_argument;
14953 cp_token *token = NULL, *declarator_token_start = NULL;
14954 const char *saved_message;
14956 /* In a template parameter, `>' is not an operator.
14960 When parsing a default template-argument for a non-type
14961 template-parameter, the first non-nested `>' is taken as the end
14962 of the template parameter-list rather than a greater-than
14964 greater_than_is_operator_p = !template_parm_p;
14966 /* Type definitions may not appear in parameter types. */
14967 saved_message = parser->type_definition_forbidden_message;
14968 parser->type_definition_forbidden_message
14969 = "types may not be defined in parameter types";
14971 /* Parse the declaration-specifiers. */
14972 cp_parser_decl_specifier_seq (parser,
14973 CP_PARSER_FLAGS_NONE,
14975 &declares_class_or_enum);
14976 /* If an error occurred, there's no reason to attempt to parse the
14977 rest of the declaration. */
14978 if (cp_parser_error_occurred (parser))
14980 parser->type_definition_forbidden_message = saved_message;
14984 /* Peek at the next token. */
14985 token = cp_lexer_peek_token (parser->lexer);
14987 /* If the next token is a `)', `,', `=', `>', or `...', then there
14988 is no declarator. However, when variadic templates are enabled,
14989 there may be a declarator following `...'. */
14990 if (token->type == CPP_CLOSE_PAREN
14991 || token->type == CPP_COMMA
14992 || token->type == CPP_EQ
14993 || token->type == CPP_GREATER)
14996 if (parenthesized_p)
14997 *parenthesized_p = false;
14999 /* Otherwise, there should be a declarator. */
15002 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15003 parser->default_arg_ok_p = false;
15005 /* After seeing a decl-specifier-seq, if the next token is not a
15006 "(", there is no possibility that the code is a valid
15007 expression. Therefore, if parsing tentatively, we commit at
15009 if (!parser->in_template_argument_list_p
15010 /* In an expression context, having seen:
15014 we cannot be sure whether we are looking at a
15015 function-type (taking a "char" as a parameter) or a cast
15016 of some object of type "char" to "int". */
15017 && !parser->in_type_id_in_expr_p
15018 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15019 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15020 cp_parser_commit_to_tentative_parse (parser);
15021 /* Parse the declarator. */
15022 declarator_token_start = token;
15023 declarator = cp_parser_declarator (parser,
15024 CP_PARSER_DECLARATOR_EITHER,
15025 /*ctor_dtor_or_conv_p=*/NULL,
15027 /*member_p=*/false);
15028 parser->default_arg_ok_p = saved_default_arg_ok_p;
15029 /* After the declarator, allow more attributes. */
15030 decl_specifiers.attributes
15031 = chainon (decl_specifiers.attributes,
15032 cp_parser_attributes_opt (parser));
15035 /* If the next token is an ellipsis, and we have not seen a
15036 declarator name, and the type of the declarator contains parameter
15037 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15038 a parameter pack expansion expression. Otherwise, leave the
15039 ellipsis for a C-style variadic function. */
15040 token = cp_lexer_peek_token (parser->lexer);
15041 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15043 tree type = decl_specifiers.type;
15045 if (type && DECL_P (type))
15046 type = TREE_TYPE (type);
15049 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15050 && declarator_can_be_parameter_pack (declarator)
15051 && (!declarator || !declarator->parameter_pack_p)
15052 && uses_parameter_packs (type))
15054 /* Consume the `...'. */
15055 cp_lexer_consume_token (parser->lexer);
15056 maybe_warn_variadic_templates ();
15058 /* Build a pack expansion type */
15060 declarator->parameter_pack_p = true;
15062 decl_specifiers.type = make_pack_expansion (type);
15066 /* The restriction on defining new types applies only to the type
15067 of the parameter, not to the default argument. */
15068 parser->type_definition_forbidden_message = saved_message;
15070 /* If the next token is `=', then process a default argument. */
15071 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15073 /* Consume the `='. */
15074 cp_lexer_consume_token (parser->lexer);
15076 /* If we are defining a class, then the tokens that make up the
15077 default argument must be saved and processed later. */
15078 if (!template_parm_p && at_class_scope_p ()
15079 && TYPE_BEING_DEFINED (current_class_type)
15080 && !LAMBDA_TYPE_P (current_class_type))
15082 unsigned depth = 0;
15083 int maybe_template_id = 0;
15084 cp_token *first_token;
15087 /* Add tokens until we have processed the entire default
15088 argument. We add the range [first_token, token). */
15089 first_token = cp_lexer_peek_token (parser->lexer);
15094 /* Peek at the next token. */
15095 token = cp_lexer_peek_token (parser->lexer);
15096 /* What we do depends on what token we have. */
15097 switch (token->type)
15099 /* In valid code, a default argument must be
15100 immediately followed by a `,' `)', or `...'. */
15102 if (depth == 0 && maybe_template_id)
15104 /* If we've seen a '<', we might be in a
15105 template-argument-list. Until Core issue 325 is
15106 resolved, we don't know how this situation ought
15107 to be handled, so try to DTRT. We check whether
15108 what comes after the comma is a valid parameter
15109 declaration list. If it is, then the comma ends
15110 the default argument; otherwise the default
15111 argument continues. */
15112 bool error = false;
15114 /* Set ITALP so cp_parser_parameter_declaration_list
15115 doesn't decide to commit to this parse. */
15116 bool saved_italp = parser->in_template_argument_list_p;
15117 parser->in_template_argument_list_p = true;
15119 cp_parser_parse_tentatively (parser);
15120 cp_lexer_consume_token (parser->lexer);
15121 cp_parser_parameter_declaration_list (parser, &error);
15122 if (!cp_parser_error_occurred (parser) && !error)
15124 cp_parser_abort_tentative_parse (parser);
15126 parser->in_template_argument_list_p = saved_italp;
15129 case CPP_CLOSE_PAREN:
15131 /* If we run into a non-nested `;', `}', or `]',
15132 then the code is invalid -- but the default
15133 argument is certainly over. */
15134 case CPP_SEMICOLON:
15135 case CPP_CLOSE_BRACE:
15136 case CPP_CLOSE_SQUARE:
15139 /* Update DEPTH, if necessary. */
15140 else if (token->type == CPP_CLOSE_PAREN
15141 || token->type == CPP_CLOSE_BRACE
15142 || token->type == CPP_CLOSE_SQUARE)
15146 case CPP_OPEN_PAREN:
15147 case CPP_OPEN_SQUARE:
15148 case CPP_OPEN_BRACE:
15154 /* This might be the comparison operator, or it might
15155 start a template argument list. */
15156 ++maybe_template_id;
15160 if (cxx_dialect == cxx98)
15162 /* Fall through for C++0x, which treats the `>>'
15163 operator like two `>' tokens in certain
15169 /* This might be an operator, or it might close a
15170 template argument list. But if a previous '<'
15171 started a template argument list, this will have
15172 closed it, so we can't be in one anymore. */
15173 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15174 if (maybe_template_id < 0)
15175 maybe_template_id = 0;
15179 /* If we run out of tokens, issue an error message. */
15181 case CPP_PRAGMA_EOL:
15182 error_at (token->location, "file ends in default argument");
15188 /* In these cases, we should look for template-ids.
15189 For example, if the default argument is
15190 `X<int, double>()', we need to do name lookup to
15191 figure out whether or not `X' is a template; if
15192 so, the `,' does not end the default argument.
15194 That is not yet done. */
15201 /* If we've reached the end, stop. */
15205 /* Add the token to the token block. */
15206 token = cp_lexer_consume_token (parser->lexer);
15209 /* Create a DEFAULT_ARG to represent the unparsed default
15211 default_argument = make_node (DEFAULT_ARG);
15212 DEFARG_TOKENS (default_argument)
15213 = cp_token_cache_new (first_token, token);
15214 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15216 /* Outside of a class definition, we can just parse the
15217 assignment-expression. */
15220 token = cp_lexer_peek_token (parser->lexer);
15222 = cp_parser_default_argument (parser, template_parm_p);
15225 if (!parser->default_arg_ok_p)
15227 if (flag_permissive)
15228 warning (0, "deprecated use of default argument for parameter of non-function");
15231 error_at (token->location,
15232 "default arguments are only "
15233 "permitted for function parameters");
15234 default_argument = NULL_TREE;
15237 else if ((declarator && declarator->parameter_pack_p)
15238 || (decl_specifiers.type
15239 && PACK_EXPANSION_P (decl_specifiers.type)))
15241 /* Find the name of the parameter pack. */
15242 cp_declarator *id_declarator = declarator;
15243 while (id_declarator && id_declarator->kind != cdk_id)
15244 id_declarator = id_declarator->declarator;
15246 if (id_declarator && id_declarator->kind == cdk_id)
15247 error_at (declarator_token_start->location,
15249 ? "template parameter pack %qD"
15250 " cannot have a default argument"
15251 : "parameter pack %qD cannot have a default argument",
15252 id_declarator->u.id.unqualified_name);
15254 error_at (declarator_token_start->location,
15256 ? "template parameter pack cannot have a default argument"
15257 : "parameter pack cannot have a default argument");
15259 default_argument = NULL_TREE;
15263 default_argument = NULL_TREE;
15265 return make_parameter_declarator (&decl_specifiers,
15270 /* Parse a default argument and return it.
15272 TEMPLATE_PARM_P is true if this is a default argument for a
15273 non-type template parameter. */
15275 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15277 tree default_argument = NULL_TREE;
15278 bool saved_greater_than_is_operator_p;
15279 bool saved_local_variables_forbidden_p;
15281 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15283 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15284 parser->greater_than_is_operator_p = !template_parm_p;
15285 /* Local variable names (and the `this' keyword) may not
15286 appear in a default argument. */
15287 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15288 parser->local_variables_forbidden_p = true;
15289 /* Parse the assignment-expression. */
15290 if (template_parm_p)
15291 push_deferring_access_checks (dk_no_deferred);
15293 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15294 if (template_parm_p)
15295 pop_deferring_access_checks ();
15296 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15297 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15299 return default_argument;
15302 /* Parse a function-body.
15305 compound_statement */
15308 cp_parser_function_body (cp_parser *parser)
15310 cp_parser_compound_statement (parser, NULL, false);
15313 /* Parse a ctor-initializer-opt followed by a function-body. Return
15314 true if a ctor-initializer was present. */
15317 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15320 bool ctor_initializer_p;
15322 /* Begin the function body. */
15323 body = begin_function_body ();
15324 /* Parse the optional ctor-initializer. */
15325 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15326 /* Parse the function-body. */
15327 cp_parser_function_body (parser);
15328 /* Finish the function body. */
15329 finish_function_body (body);
15331 return ctor_initializer_p;
15334 /* Parse an initializer.
15337 = initializer-clause
15338 ( expression-list )
15340 Returns an expression representing the initializer. If no
15341 initializer is present, NULL_TREE is returned.
15343 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15344 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15345 set to TRUE if there is no initializer present. If there is an
15346 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15347 is set to true; otherwise it is set to false. */
15350 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15351 bool* non_constant_p)
15356 /* Peek at the next token. */
15357 token = cp_lexer_peek_token (parser->lexer);
15359 /* Let our caller know whether or not this initializer was
15361 *is_direct_init = (token->type != CPP_EQ);
15362 /* Assume that the initializer is constant. */
15363 *non_constant_p = false;
15365 if (token->type == CPP_EQ)
15367 /* Consume the `='. */
15368 cp_lexer_consume_token (parser->lexer);
15369 /* Parse the initializer-clause. */
15370 init = cp_parser_initializer_clause (parser, non_constant_p);
15372 else if (token->type == CPP_OPEN_PAREN)
15375 vec = cp_parser_parenthesized_expression_list (parser, false,
15377 /*allow_expansion_p=*/true,
15380 return error_mark_node;
15381 init = build_tree_list_vec (vec);
15382 release_tree_vector (vec);
15384 else if (token->type == CPP_OPEN_BRACE)
15386 maybe_warn_cpp0x ("extended initializer lists");
15387 init = cp_parser_braced_list (parser, non_constant_p);
15388 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15392 /* Anything else is an error. */
15393 cp_parser_error (parser, "expected initializer");
15394 init = error_mark_node;
15400 /* Parse an initializer-clause.
15402 initializer-clause:
15403 assignment-expression
15406 Returns an expression representing the initializer.
15408 If the `assignment-expression' production is used the value
15409 returned is simply a representation for the expression.
15411 Otherwise, calls cp_parser_braced_list. */
15414 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15418 /* Assume the expression is constant. */
15419 *non_constant_p = false;
15421 /* If it is not a `{', then we are looking at an
15422 assignment-expression. */
15423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15426 = cp_parser_constant_expression (parser,
15427 /*allow_non_constant_p=*/true,
15429 if (!*non_constant_p)
15430 initializer = fold_non_dependent_expr (initializer);
15433 initializer = cp_parser_braced_list (parser, non_constant_p);
15435 return initializer;
15438 /* Parse a brace-enclosed initializer list.
15441 { initializer-list , [opt] }
15444 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15445 the elements of the initializer-list (or NULL, if the last
15446 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15447 NULL_TREE. There is no way to detect whether or not the optional
15448 trailing `,' was provided. NON_CONSTANT_P is as for
15449 cp_parser_initializer. */
15452 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15456 /* Consume the `{' token. */
15457 cp_lexer_consume_token (parser->lexer);
15458 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15459 initializer = make_node (CONSTRUCTOR);
15460 /* If it's not a `}', then there is a non-trivial initializer. */
15461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15463 /* Parse the initializer list. */
15464 CONSTRUCTOR_ELTS (initializer)
15465 = cp_parser_initializer_list (parser, non_constant_p);
15466 /* A trailing `,' token is allowed. */
15467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15468 cp_lexer_consume_token (parser->lexer);
15470 /* Now, there should be a trailing `}'. */
15471 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15472 TREE_TYPE (initializer) = init_list_type_node;
15473 return initializer;
15476 /* Parse an initializer-list.
15479 initializer-clause ... [opt]
15480 initializer-list , initializer-clause ... [opt]
15485 identifier : initializer-clause
15486 initializer-list, identifier : initializer-clause
15488 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15489 for the initializer. If the INDEX of the elt is non-NULL, it is the
15490 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15491 as for cp_parser_initializer. */
15493 static VEC(constructor_elt,gc) *
15494 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15496 VEC(constructor_elt,gc) *v = NULL;
15498 /* Assume all of the expressions are constant. */
15499 *non_constant_p = false;
15501 /* Parse the rest of the list. */
15507 bool clause_non_constant_p;
15509 /* If the next token is an identifier and the following one is a
15510 colon, we are looking at the GNU designated-initializer
15512 if (cp_parser_allow_gnu_extensions_p (parser)
15513 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15514 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15516 /* Warn the user that they are using an extension. */
15517 pedwarn (input_location, OPT_pedantic,
15518 "ISO C++ does not allow designated initializers");
15519 /* Consume the identifier. */
15520 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15521 /* Consume the `:'. */
15522 cp_lexer_consume_token (parser->lexer);
15525 identifier = NULL_TREE;
15527 /* Parse the initializer. */
15528 initializer = cp_parser_initializer_clause (parser,
15529 &clause_non_constant_p);
15530 /* If any clause is non-constant, so is the entire initializer. */
15531 if (clause_non_constant_p)
15532 *non_constant_p = true;
15534 /* If we have an ellipsis, this is an initializer pack
15536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15538 /* Consume the `...'. */
15539 cp_lexer_consume_token (parser->lexer);
15541 /* Turn the initializer into an initializer expansion. */
15542 initializer = make_pack_expansion (initializer);
15545 /* Add it to the vector. */
15546 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15548 /* If the next token is not a comma, we have reached the end of
15550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15553 /* Peek at the next token. */
15554 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15555 /* If the next token is a `}', then we're still done. An
15556 initializer-clause can have a trailing `,' after the
15557 initializer-list and before the closing `}'. */
15558 if (token->type == CPP_CLOSE_BRACE)
15561 /* Consume the `,' token. */
15562 cp_lexer_consume_token (parser->lexer);
15568 /* Classes [gram.class] */
15570 /* Parse a class-name.
15576 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15577 to indicate that names looked up in dependent types should be
15578 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15579 keyword has been used to indicate that the name that appears next
15580 is a template. TAG_TYPE indicates the explicit tag given before
15581 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15582 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15583 is the class being defined in a class-head.
15585 Returns the TYPE_DECL representing the class. */
15588 cp_parser_class_name (cp_parser *parser,
15589 bool typename_keyword_p,
15590 bool template_keyword_p,
15591 enum tag_types tag_type,
15592 bool check_dependency_p,
15594 bool is_declaration)
15600 tree identifier = NULL_TREE;
15602 /* All class-names start with an identifier. */
15603 token = cp_lexer_peek_token (parser->lexer);
15604 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15606 cp_parser_error (parser, "expected class-name");
15607 return error_mark_node;
15610 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15611 to a template-id, so we save it here. */
15612 scope = parser->scope;
15613 if (scope == error_mark_node)
15614 return error_mark_node;
15616 /* Any name names a type if we're following the `typename' keyword
15617 in a qualified name where the enclosing scope is type-dependent. */
15618 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15619 && dependent_type_p (scope));
15620 /* Handle the common case (an identifier, but not a template-id)
15622 if (token->type == CPP_NAME
15623 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15625 cp_token *identifier_token;
15628 /* Look for the identifier. */
15629 identifier_token = cp_lexer_peek_token (parser->lexer);
15630 ambiguous_p = identifier_token->ambiguous_p;
15631 identifier = cp_parser_identifier (parser);
15632 /* If the next token isn't an identifier, we are certainly not
15633 looking at a class-name. */
15634 if (identifier == error_mark_node)
15635 decl = error_mark_node;
15636 /* If we know this is a type-name, there's no need to look it
15638 else if (typename_p)
15642 tree ambiguous_decls;
15643 /* If we already know that this lookup is ambiguous, then
15644 we've already issued an error message; there's no reason
15648 cp_parser_simulate_error (parser);
15649 return error_mark_node;
15651 /* If the next token is a `::', then the name must be a type
15654 [basic.lookup.qual]
15656 During the lookup for a name preceding the :: scope
15657 resolution operator, object, function, and enumerator
15658 names are ignored. */
15659 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15660 tag_type = typename_type;
15661 /* Look up the name. */
15662 decl = cp_parser_lookup_name (parser, identifier,
15664 /*is_template=*/false,
15665 /*is_namespace=*/false,
15666 check_dependency_p,
15668 identifier_token->location);
15669 if (ambiguous_decls)
15671 error_at (identifier_token->location,
15672 "reference to %qD is ambiguous", identifier);
15673 print_candidates (ambiguous_decls);
15674 if (cp_parser_parsing_tentatively (parser))
15676 identifier_token->ambiguous_p = true;
15677 cp_parser_simulate_error (parser);
15679 return error_mark_node;
15685 /* Try a template-id. */
15686 decl = cp_parser_template_id (parser, template_keyword_p,
15687 check_dependency_p,
15689 if (decl == error_mark_node)
15690 return error_mark_node;
15693 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15695 /* If this is a typename, create a TYPENAME_TYPE. */
15696 if (typename_p && decl != error_mark_node)
15698 decl = make_typename_type (scope, decl, typename_type,
15699 /*complain=*/tf_error);
15700 if (decl != error_mark_node)
15701 decl = TYPE_NAME (decl);
15704 /* Check to see that it is really the name of a class. */
15705 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15706 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15707 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15708 /* Situations like this:
15710 template <typename T> struct A {
15711 typename T::template X<int>::I i;
15714 are problematic. Is `T::template X<int>' a class-name? The
15715 standard does not seem to be definitive, but there is no other
15716 valid interpretation of the following `::'. Therefore, those
15717 names are considered class-names. */
15719 decl = make_typename_type (scope, decl, tag_type, tf_error);
15720 if (decl != error_mark_node)
15721 decl = TYPE_NAME (decl);
15723 else if (TREE_CODE (decl) != TYPE_DECL
15724 || TREE_TYPE (decl) == error_mark_node
15725 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15726 decl = error_mark_node;
15728 if (decl == error_mark_node)
15729 cp_parser_error (parser, "expected class-name");
15730 else if (identifier && !parser->scope)
15731 maybe_note_name_used_in_class (identifier, decl);
15736 /* Parse a class-specifier.
15739 class-head { member-specification [opt] }
15741 Returns the TREE_TYPE representing the class. */
15744 cp_parser_class_specifier (cp_parser* parser)
15747 tree attributes = NULL_TREE;
15748 bool nested_name_specifier_p;
15749 unsigned saved_num_template_parameter_lists;
15750 bool saved_in_function_body;
15751 bool saved_in_unbraced_linkage_specification_p;
15752 tree old_scope = NULL_TREE;
15753 tree scope = NULL_TREE;
15756 push_deferring_access_checks (dk_no_deferred);
15758 /* Parse the class-head. */
15759 type = cp_parser_class_head (parser,
15760 &nested_name_specifier_p,
15763 /* If the class-head was a semantic disaster, skip the entire body
15767 cp_parser_skip_to_end_of_block_or_statement (parser);
15768 pop_deferring_access_checks ();
15769 return error_mark_node;
15772 /* Look for the `{'. */
15773 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15775 pop_deferring_access_checks ();
15776 return error_mark_node;
15779 /* Process the base classes. If they're invalid, skip the
15780 entire class body. */
15781 if (!xref_basetypes (type, bases))
15783 /* Consuming the closing brace yields better error messages
15785 if (cp_parser_skip_to_closing_brace (parser))
15786 cp_lexer_consume_token (parser->lexer);
15787 pop_deferring_access_checks ();
15788 return error_mark_node;
15791 /* Issue an error message if type-definitions are forbidden here. */
15792 cp_parser_check_type_definition (parser);
15793 /* Remember that we are defining one more class. */
15794 ++parser->num_classes_being_defined;
15795 /* Inside the class, surrounding template-parameter-lists do not
15797 saved_num_template_parameter_lists
15798 = parser->num_template_parameter_lists;
15799 parser->num_template_parameter_lists = 0;
15800 /* We are not in a function body. */
15801 saved_in_function_body = parser->in_function_body;
15802 parser->in_function_body = false;
15803 /* We are not immediately inside an extern "lang" block. */
15804 saved_in_unbraced_linkage_specification_p
15805 = parser->in_unbraced_linkage_specification_p;
15806 parser->in_unbraced_linkage_specification_p = false;
15808 /* Start the class. */
15809 if (nested_name_specifier_p)
15811 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15812 old_scope = push_inner_scope (scope);
15814 type = begin_class_definition (type, attributes);
15816 if (type == error_mark_node)
15817 /* If the type is erroneous, skip the entire body of the class. */
15818 cp_parser_skip_to_closing_brace (parser);
15820 /* Parse the member-specification. */
15821 cp_parser_member_specification_opt (parser);
15823 /* Look for the trailing `}'. */
15824 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15825 /* Look for trailing attributes to apply to this class. */
15826 if (cp_parser_allow_gnu_extensions_p (parser))
15827 attributes = cp_parser_attributes_opt (parser);
15828 if (type != error_mark_node)
15829 type = finish_struct (type, attributes);
15830 if (nested_name_specifier_p)
15831 pop_inner_scope (old_scope, scope);
15832 /* If this class is not itself within the scope of another class,
15833 then we need to parse the bodies of all of the queued function
15834 definitions. Note that the queued functions defined in a class
15835 are not always processed immediately following the
15836 class-specifier for that class. Consider:
15839 struct B { void f() { sizeof (A); } };
15842 If `f' were processed before the processing of `A' were
15843 completed, there would be no way to compute the size of `A'.
15844 Note that the nesting we are interested in here is lexical --
15845 not the semantic nesting given by TYPE_CONTEXT. In particular,
15848 struct A { struct B; };
15849 struct A::B { void f() { } };
15851 there is no need to delay the parsing of `A::B::f'. */
15852 if (--parser->num_classes_being_defined == 0)
15856 tree class_type = NULL_TREE;
15857 tree pushed_scope = NULL_TREE;
15859 /* In a first pass, parse default arguments to the functions.
15860 Then, in a second pass, parse the bodies of the functions.
15861 This two-phased approach handles cases like:
15869 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15870 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15871 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15872 TREE_PURPOSE (parser->unparsed_functions_queues)
15873 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15875 fn = TREE_VALUE (queue_entry);
15876 /* If there are default arguments that have not yet been processed,
15877 take care of them now. */
15878 if (class_type != TREE_PURPOSE (queue_entry))
15881 pop_scope (pushed_scope);
15882 class_type = TREE_PURPOSE (queue_entry);
15883 pushed_scope = push_scope (class_type);
15885 /* Make sure that any template parameters are in scope. */
15886 maybe_begin_member_template_processing (fn);
15887 /* Parse the default argument expressions. */
15888 cp_parser_late_parsing_default_args (parser, fn);
15889 /* Remove any template parameters from the symbol table. */
15890 maybe_end_member_template_processing ();
15893 pop_scope (pushed_scope);
15894 /* Now parse the body of the functions. */
15895 for (TREE_VALUE (parser->unparsed_functions_queues)
15896 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15897 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15898 TREE_VALUE (parser->unparsed_functions_queues)
15899 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15901 /* Figure out which function we need to process. */
15902 fn = TREE_VALUE (queue_entry);
15903 /* Parse the function. */
15904 cp_parser_late_parsing_for_member (parser, fn);
15908 /* Put back any saved access checks. */
15909 pop_deferring_access_checks ();
15911 /* Restore saved state. */
15912 parser->in_function_body = saved_in_function_body;
15913 parser->num_template_parameter_lists
15914 = saved_num_template_parameter_lists;
15915 parser->in_unbraced_linkage_specification_p
15916 = saved_in_unbraced_linkage_specification_p;
15921 /* Parse a class-head.
15924 class-key identifier [opt] base-clause [opt]
15925 class-key nested-name-specifier identifier base-clause [opt]
15926 class-key nested-name-specifier [opt] template-id
15930 class-key attributes identifier [opt] base-clause [opt]
15931 class-key attributes nested-name-specifier identifier base-clause [opt]
15932 class-key attributes nested-name-specifier [opt] template-id
15935 Upon return BASES is initialized to the list of base classes (or
15936 NULL, if there are none) in the same form returned by
15937 cp_parser_base_clause.
15939 Returns the TYPE of the indicated class. Sets
15940 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15941 involving a nested-name-specifier was used, and FALSE otherwise.
15943 Returns error_mark_node if this is not a class-head.
15945 Returns NULL_TREE if the class-head is syntactically valid, but
15946 semantically invalid in a way that means we should skip the entire
15947 body of the class. */
15950 cp_parser_class_head (cp_parser* parser,
15951 bool* nested_name_specifier_p,
15952 tree *attributes_p,
15955 tree nested_name_specifier;
15956 enum tag_types class_key;
15957 tree id = NULL_TREE;
15958 tree type = NULL_TREE;
15960 bool template_id_p = false;
15961 bool qualified_p = false;
15962 bool invalid_nested_name_p = false;
15963 bool invalid_explicit_specialization_p = false;
15964 tree pushed_scope = NULL_TREE;
15965 unsigned num_templates;
15966 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15967 /* Assume no nested-name-specifier will be present. */
15968 *nested_name_specifier_p = false;
15969 /* Assume no template parameter lists will be used in defining the
15973 *bases = NULL_TREE;
15975 /* Look for the class-key. */
15976 class_key = cp_parser_class_key (parser);
15977 if (class_key == none_type)
15978 return error_mark_node;
15980 /* Parse the attributes. */
15981 attributes = cp_parser_attributes_opt (parser);
15983 /* If the next token is `::', that is invalid -- but sometimes
15984 people do try to write:
15988 Handle this gracefully by accepting the extra qualifier, and then
15989 issuing an error about it later if this really is a
15990 class-head. If it turns out just to be an elaborated type
15991 specifier, remain silent. */
15992 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15993 qualified_p = true;
15995 push_deferring_access_checks (dk_no_check);
15997 /* Determine the name of the class. Begin by looking for an
15998 optional nested-name-specifier. */
15999 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16000 nested_name_specifier
16001 = cp_parser_nested_name_specifier_opt (parser,
16002 /*typename_keyword_p=*/false,
16003 /*check_dependency_p=*/false,
16005 /*is_declaration=*/false);
16006 /* If there was a nested-name-specifier, then there *must* be an
16008 if (nested_name_specifier)
16010 type_start_token = cp_lexer_peek_token (parser->lexer);
16011 /* Although the grammar says `identifier', it really means
16012 `class-name' or `template-name'. You are only allowed to
16013 define a class that has already been declared with this
16016 The proposed resolution for Core Issue 180 says that wherever
16017 you see `class T::X' you should treat `X' as a type-name.
16019 It is OK to define an inaccessible class; for example:
16021 class A { class B; };
16024 We do not know if we will see a class-name, or a
16025 template-name. We look for a class-name first, in case the
16026 class-name is a template-id; if we looked for the
16027 template-name first we would stop after the template-name. */
16028 cp_parser_parse_tentatively (parser);
16029 type = cp_parser_class_name (parser,
16030 /*typename_keyword_p=*/false,
16031 /*template_keyword_p=*/false,
16033 /*check_dependency_p=*/false,
16034 /*class_head_p=*/true,
16035 /*is_declaration=*/false);
16036 /* If that didn't work, ignore the nested-name-specifier. */
16037 if (!cp_parser_parse_definitely (parser))
16039 invalid_nested_name_p = true;
16040 type_start_token = cp_lexer_peek_token (parser->lexer);
16041 id = cp_parser_identifier (parser);
16042 if (id == error_mark_node)
16045 /* If we could not find a corresponding TYPE, treat this
16046 declaration like an unqualified declaration. */
16047 if (type == error_mark_node)
16048 nested_name_specifier = NULL_TREE;
16049 /* Otherwise, count the number of templates used in TYPE and its
16050 containing scopes. */
16055 for (scope = TREE_TYPE (type);
16056 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16057 scope = (TYPE_P (scope)
16058 ? TYPE_CONTEXT (scope)
16059 : DECL_CONTEXT (scope)))
16061 && CLASS_TYPE_P (scope)
16062 && CLASSTYPE_TEMPLATE_INFO (scope)
16063 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16064 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16068 /* Otherwise, the identifier is optional. */
16071 /* We don't know whether what comes next is a template-id,
16072 an identifier, or nothing at all. */
16073 cp_parser_parse_tentatively (parser);
16074 /* Check for a template-id. */
16075 type_start_token = cp_lexer_peek_token (parser->lexer);
16076 id = cp_parser_template_id (parser,
16077 /*template_keyword_p=*/false,
16078 /*check_dependency_p=*/true,
16079 /*is_declaration=*/true);
16080 /* If that didn't work, it could still be an identifier. */
16081 if (!cp_parser_parse_definitely (parser))
16083 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16085 type_start_token = cp_lexer_peek_token (parser->lexer);
16086 id = cp_parser_identifier (parser);
16093 template_id_p = true;
16098 pop_deferring_access_checks ();
16101 cp_parser_check_for_invalid_template_id (parser, id,
16102 type_start_token->location);
16104 /* If it's not a `:' or a `{' then we can't really be looking at a
16105 class-head, since a class-head only appears as part of a
16106 class-specifier. We have to detect this situation before calling
16107 xref_tag, since that has irreversible side-effects. */
16108 if (!cp_parser_next_token_starts_class_definition_p (parser))
16110 cp_parser_error (parser, "expected %<{%> or %<:%>");
16111 return error_mark_node;
16114 /* At this point, we're going ahead with the class-specifier, even
16115 if some other problem occurs. */
16116 cp_parser_commit_to_tentative_parse (parser);
16117 /* Issue the error about the overly-qualified name now. */
16120 cp_parser_error (parser,
16121 "global qualification of class name is invalid");
16122 return error_mark_node;
16124 else if (invalid_nested_name_p)
16126 cp_parser_error (parser,
16127 "qualified name does not name a class");
16128 return error_mark_node;
16130 else if (nested_name_specifier)
16134 /* Reject typedef-names in class heads. */
16135 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16137 error_at (type_start_token->location,
16138 "invalid class name in declaration of %qD",
16144 /* Figure out in what scope the declaration is being placed. */
16145 scope = current_scope ();
16146 /* If that scope does not contain the scope in which the
16147 class was originally declared, the program is invalid. */
16148 if (scope && !is_ancestor (scope, nested_name_specifier))
16150 if (at_namespace_scope_p ())
16151 error_at (type_start_token->location,
16152 "declaration of %qD in namespace %qD which does not "
16154 type, scope, nested_name_specifier);
16156 error_at (type_start_token->location,
16157 "declaration of %qD in %qD which does not enclose %qD",
16158 type, scope, nested_name_specifier);
16164 A declarator-id shall not be qualified except for the
16165 definition of a ... nested class outside of its class
16166 ... [or] the definition or explicit instantiation of a
16167 class member of a namespace outside of its namespace. */
16168 if (scope == nested_name_specifier)
16170 permerror (nested_name_specifier_token_start->location,
16171 "extra qualification not allowed");
16172 nested_name_specifier = NULL_TREE;
16176 /* An explicit-specialization must be preceded by "template <>". If
16177 it is not, try to recover gracefully. */
16178 if (at_namespace_scope_p ()
16179 && parser->num_template_parameter_lists == 0
16182 error_at (type_start_token->location,
16183 "an explicit specialization must be preceded by %<template <>%>");
16184 invalid_explicit_specialization_p = true;
16185 /* Take the same action that would have been taken by
16186 cp_parser_explicit_specialization. */
16187 ++parser->num_template_parameter_lists;
16188 begin_specialization ();
16190 /* There must be no "return" statements between this point and the
16191 end of this function; set "type "to the correct return value and
16192 use "goto done;" to return. */
16193 /* Make sure that the right number of template parameters were
16195 if (!cp_parser_check_template_parameters (parser, num_templates,
16196 type_start_token->location,
16197 /*declarator=*/NULL))
16199 /* If something went wrong, there is no point in even trying to
16200 process the class-definition. */
16205 /* Look up the type. */
16208 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16209 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16210 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16212 error_at (type_start_token->location,
16213 "function template %qD redeclared as a class template", id);
16214 type = error_mark_node;
16218 type = TREE_TYPE (id);
16219 type = maybe_process_partial_specialization (type);
16221 if (nested_name_specifier)
16222 pushed_scope = push_scope (nested_name_specifier);
16224 else if (nested_name_specifier)
16230 template <typename T> struct S { struct T };
16231 template <typename T> struct S<T>::T { };
16233 we will get a TYPENAME_TYPE when processing the definition of
16234 `S::T'. We need to resolve it to the actual type before we
16235 try to define it. */
16236 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16238 class_type = resolve_typename_type (TREE_TYPE (type),
16239 /*only_current_p=*/false);
16240 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16241 type = TYPE_NAME (class_type);
16244 cp_parser_error (parser, "could not resolve typename type");
16245 type = error_mark_node;
16249 if (maybe_process_partial_specialization (TREE_TYPE (type))
16250 == error_mark_node)
16256 class_type = current_class_type;
16257 /* Enter the scope indicated by the nested-name-specifier. */
16258 pushed_scope = push_scope (nested_name_specifier);
16259 /* Get the canonical version of this type. */
16260 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16261 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16262 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16264 type = push_template_decl (type);
16265 if (type == error_mark_node)
16272 type = TREE_TYPE (type);
16273 *nested_name_specifier_p = true;
16275 else /* The name is not a nested name. */
16277 /* If the class was unnamed, create a dummy name. */
16279 id = make_anon_name ();
16280 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16281 parser->num_template_parameter_lists);
16284 /* Indicate whether this class was declared as a `class' or as a
16286 if (TREE_CODE (type) == RECORD_TYPE)
16287 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16288 cp_parser_check_class_key (class_key, type);
16290 /* If this type was already complete, and we see another definition,
16291 that's an error. */
16292 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16294 error_at (type_start_token->location, "redefinition of %q#T",
16296 error_at (type_start_token->location, "previous definition of %q+#T",
16301 else if (type == error_mark_node)
16304 /* We will have entered the scope containing the class; the names of
16305 base classes should be looked up in that context. For example:
16307 struct A { struct B {}; struct C; };
16308 struct A::C : B {};
16312 /* Get the list of base-classes, if there is one. */
16313 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16314 *bases = cp_parser_base_clause (parser);
16317 /* Leave the scope given by the nested-name-specifier. We will
16318 enter the class scope itself while processing the members. */
16320 pop_scope (pushed_scope);
16322 if (invalid_explicit_specialization_p)
16324 end_specialization ();
16325 --parser->num_template_parameter_lists;
16327 *attributes_p = attributes;
16331 /* Parse a class-key.
16338 Returns the kind of class-key specified, or none_type to indicate
16341 static enum tag_types
16342 cp_parser_class_key (cp_parser* parser)
16345 enum tag_types tag_type;
16347 /* Look for the class-key. */
16348 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16352 /* Check to see if the TOKEN is a class-key. */
16353 tag_type = cp_parser_token_is_class_key (token);
16355 cp_parser_error (parser, "expected class-key");
16359 /* Parse an (optional) member-specification.
16361 member-specification:
16362 member-declaration member-specification [opt]
16363 access-specifier : member-specification [opt] */
16366 cp_parser_member_specification_opt (cp_parser* parser)
16373 /* Peek at the next token. */
16374 token = cp_lexer_peek_token (parser->lexer);
16375 /* If it's a `}', or EOF then we've seen all the members. */
16376 if (token->type == CPP_CLOSE_BRACE
16377 || token->type == CPP_EOF
16378 || token->type == CPP_PRAGMA_EOL)
16381 /* See if this token is a keyword. */
16382 keyword = token->keyword;
16386 case RID_PROTECTED:
16388 /* Consume the access-specifier. */
16389 cp_lexer_consume_token (parser->lexer);
16390 /* Remember which access-specifier is active. */
16391 current_access_specifier = token->u.value;
16392 /* Look for the `:'. */
16393 cp_parser_require (parser, CPP_COLON, "%<:%>");
16397 /* Accept #pragmas at class scope. */
16398 if (token->type == CPP_PRAGMA)
16400 cp_parser_pragma (parser, pragma_external);
16404 /* Otherwise, the next construction must be a
16405 member-declaration. */
16406 cp_parser_member_declaration (parser);
16411 /* Parse a member-declaration.
16413 member-declaration:
16414 decl-specifier-seq [opt] member-declarator-list [opt] ;
16415 function-definition ; [opt]
16416 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16418 template-declaration
16420 member-declarator-list:
16422 member-declarator-list , member-declarator
16425 declarator pure-specifier [opt]
16426 declarator constant-initializer [opt]
16427 identifier [opt] : constant-expression
16431 member-declaration:
16432 __extension__ member-declaration
16435 declarator attributes [opt] pure-specifier [opt]
16436 declarator attributes [opt] constant-initializer [opt]
16437 identifier [opt] attributes [opt] : constant-expression
16441 member-declaration:
16442 static_assert-declaration */
16445 cp_parser_member_declaration (cp_parser* parser)
16447 cp_decl_specifier_seq decl_specifiers;
16448 tree prefix_attributes;
16450 int declares_class_or_enum;
16452 cp_token *token = NULL;
16453 cp_token *decl_spec_token_start = NULL;
16454 cp_token *initializer_token_start = NULL;
16455 int saved_pedantic;
16457 /* Check for the `__extension__' keyword. */
16458 if (cp_parser_extension_opt (parser, &saved_pedantic))
16461 cp_parser_member_declaration (parser);
16462 /* Restore the old value of the PEDANTIC flag. */
16463 pedantic = saved_pedantic;
16468 /* Check for a template-declaration. */
16469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16471 /* An explicit specialization here is an error condition, and we
16472 expect the specialization handler to detect and report this. */
16473 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16474 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16475 cp_parser_explicit_specialization (parser);
16477 cp_parser_template_declaration (parser, /*member_p=*/true);
16482 /* Check for a using-declaration. */
16483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16485 /* Parse the using-declaration. */
16486 cp_parser_using_declaration (parser,
16487 /*access_declaration_p=*/false);
16491 /* Check for @defs. */
16492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16495 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16496 ivar = ivar_chains;
16500 ivar = TREE_CHAIN (member);
16501 TREE_CHAIN (member) = NULL_TREE;
16502 finish_member_declaration (member);
16507 /* If the next token is `static_assert' we have a static assertion. */
16508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16510 cp_parser_static_assert (parser, /*member_p=*/true);
16514 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16517 /* Parse the decl-specifier-seq. */
16518 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16519 cp_parser_decl_specifier_seq (parser,
16520 CP_PARSER_FLAGS_OPTIONAL,
16522 &declares_class_or_enum);
16523 prefix_attributes = decl_specifiers.attributes;
16524 decl_specifiers.attributes = NULL_TREE;
16525 /* Check for an invalid type-name. */
16526 if (!decl_specifiers.type
16527 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16529 /* If there is no declarator, then the decl-specifier-seq should
16531 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16533 /* If there was no decl-specifier-seq, and the next token is a
16534 `;', then we have something like:
16540 Each member-declaration shall declare at least one member
16541 name of the class. */
16542 if (!decl_specifiers.any_specifiers_p)
16544 cp_token *token = cp_lexer_peek_token (parser->lexer);
16545 if (!in_system_header_at (token->location))
16546 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16552 /* See if this declaration is a friend. */
16553 friend_p = cp_parser_friend_p (&decl_specifiers);
16554 /* If there were decl-specifiers, check to see if there was
16555 a class-declaration. */
16556 type = check_tag_decl (&decl_specifiers);
16557 /* Nested classes have already been added to the class, but
16558 a `friend' needs to be explicitly registered. */
16561 /* If the `friend' keyword was present, the friend must
16562 be introduced with a class-key. */
16563 if (!declares_class_or_enum)
16564 error_at (decl_spec_token_start->location,
16565 "a class-key must be used when declaring a friend");
16568 template <typename T> struct A {
16569 friend struct A<T>::B;
16572 A<T>::B will be represented by a TYPENAME_TYPE, and
16573 therefore not recognized by check_tag_decl. */
16575 && decl_specifiers.type
16576 && TYPE_P (decl_specifiers.type))
16577 type = decl_specifiers.type;
16578 if (!type || !TYPE_P (type))
16579 error_at (decl_spec_token_start->location,
16580 "friend declaration does not name a class or "
16583 make_friend_class (current_class_type, type,
16584 /*complain=*/true);
16586 /* If there is no TYPE, an error message will already have
16588 else if (!type || type == error_mark_node)
16590 /* An anonymous aggregate has to be handled specially; such
16591 a declaration really declares a data member (with a
16592 particular type), as opposed to a nested class. */
16593 else if (ANON_AGGR_TYPE_P (type))
16595 /* Remove constructors and such from TYPE, now that we
16596 know it is an anonymous aggregate. */
16597 fixup_anonymous_aggr (type);
16598 /* And make the corresponding data member. */
16599 decl = build_decl (decl_spec_token_start->location,
16600 FIELD_DECL, NULL_TREE, type);
16601 /* Add it to the class. */
16602 finish_member_declaration (decl);
16605 cp_parser_check_access_in_redeclaration
16607 decl_spec_token_start->location);
16612 /* See if these declarations will be friends. */
16613 friend_p = cp_parser_friend_p (&decl_specifiers);
16615 /* Keep going until we hit the `;' at the end of the
16617 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16619 tree attributes = NULL_TREE;
16620 tree first_attribute;
16622 /* Peek at the next token. */
16623 token = cp_lexer_peek_token (parser->lexer);
16625 /* Check for a bitfield declaration. */
16626 if (token->type == CPP_COLON
16627 || (token->type == CPP_NAME
16628 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16634 /* Get the name of the bitfield. Note that we cannot just
16635 check TOKEN here because it may have been invalidated by
16636 the call to cp_lexer_peek_nth_token above. */
16637 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16638 identifier = cp_parser_identifier (parser);
16640 identifier = NULL_TREE;
16642 /* Consume the `:' token. */
16643 cp_lexer_consume_token (parser->lexer);
16644 /* Get the width of the bitfield. */
16646 = cp_parser_constant_expression (parser,
16647 /*allow_non_constant=*/false,
16650 /* Look for attributes that apply to the bitfield. */
16651 attributes = cp_parser_attributes_opt (parser);
16652 /* Remember which attributes are prefix attributes and
16654 first_attribute = attributes;
16655 /* Combine the attributes. */
16656 attributes = chainon (prefix_attributes, attributes);
16658 /* Create the bitfield declaration. */
16659 decl = grokbitfield (identifier
16660 ? make_id_declarator (NULL_TREE,
16670 cp_declarator *declarator;
16672 tree asm_specification;
16673 int ctor_dtor_or_conv_p;
16675 /* Parse the declarator. */
16677 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16678 &ctor_dtor_or_conv_p,
16679 /*parenthesized_p=*/NULL,
16680 /*member_p=*/true);
16682 /* If something went wrong parsing the declarator, make sure
16683 that we at least consume some tokens. */
16684 if (declarator == cp_error_declarator)
16686 /* Skip to the end of the statement. */
16687 cp_parser_skip_to_end_of_statement (parser);
16688 /* If the next token is not a semicolon, that is
16689 probably because we just skipped over the body of
16690 a function. So, we consume a semicolon if
16691 present, but do not issue an error message if it
16693 if (cp_lexer_next_token_is (parser->lexer,
16695 cp_lexer_consume_token (parser->lexer);
16699 if (declares_class_or_enum & 2)
16700 cp_parser_check_for_definition_in_return_type
16701 (declarator, decl_specifiers.type,
16702 decl_specifiers.type_location);
16704 /* Look for an asm-specification. */
16705 asm_specification = cp_parser_asm_specification_opt (parser);
16706 /* Look for attributes that apply to the declaration. */
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 /* If it's an `=', then we have a constant-initializer or a
16715 pure-specifier. It is not correct to parse the
16716 initializer before registering the member declaration
16717 since the member declaration should be in scope while
16718 its initializer is processed. However, the rest of the
16719 front end does not yet provide an interface that allows
16720 us to handle this correctly. */
16721 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16725 A pure-specifier shall be used only in the declaration of
16726 a virtual function.
16728 A member-declarator can contain a constant-initializer
16729 only if it declares a static member of integral or
16732 Therefore, if the DECLARATOR is for a function, we look
16733 for a pure-specifier; otherwise, we look for a
16734 constant-initializer. When we call `grokfield', it will
16735 perform more stringent semantics checks. */
16736 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16737 if (function_declarator_p (declarator))
16738 initializer = cp_parser_pure_specifier (parser);
16740 /* Parse the initializer. */
16741 initializer = cp_parser_constant_initializer (parser);
16743 /* Otherwise, there is no initializer. */
16745 initializer = NULL_TREE;
16747 /* See if we are probably looking at a function
16748 definition. We are certainly not looking at a
16749 member-declarator. Calling `grokfield' has
16750 side-effects, so we must not do it unless we are sure
16751 that we are looking at a member-declarator. */
16752 if (cp_parser_token_starts_function_definition_p
16753 (cp_lexer_peek_token (parser->lexer)))
16755 /* The grammar does not allow a pure-specifier to be
16756 used when a member function is defined. (It is
16757 possible that this fact is an oversight in the
16758 standard, since a pure function may be defined
16759 outside of the class-specifier. */
16761 error_at (initializer_token_start->location,
16762 "pure-specifier on function-definition");
16763 decl = cp_parser_save_member_function_body (parser,
16767 /* If the member was not a friend, declare it here. */
16769 finish_member_declaration (decl);
16770 /* Peek at the next token. */
16771 token = cp_lexer_peek_token (parser->lexer);
16772 /* If the next token is a semicolon, consume it. */
16773 if (token->type == CPP_SEMICOLON)
16774 cp_lexer_consume_token (parser->lexer);
16778 if (declarator->kind == cdk_function)
16779 declarator->id_loc = token->location;
16780 /* Create the declaration. */
16781 decl = grokfield (declarator, &decl_specifiers,
16782 initializer, /*init_const_expr_p=*/true,
16787 /* Reset PREFIX_ATTRIBUTES. */
16788 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16789 attributes = TREE_CHAIN (attributes);
16791 TREE_CHAIN (attributes) = NULL_TREE;
16793 /* If there is any qualification still in effect, clear it
16794 now; we will be starting fresh with the next declarator. */
16795 parser->scope = NULL_TREE;
16796 parser->qualifying_scope = NULL_TREE;
16797 parser->object_scope = NULL_TREE;
16798 /* If it's a `,', then there are more declarators. */
16799 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16800 cp_lexer_consume_token (parser->lexer);
16801 /* If the next token isn't a `;', then we have a parse error. */
16802 else if (cp_lexer_next_token_is_not (parser->lexer,
16805 cp_parser_error (parser, "expected %<;%>");
16806 /* Skip tokens until we find a `;'. */
16807 cp_parser_skip_to_end_of_statement (parser);
16814 /* Add DECL to the list of members. */
16816 finish_member_declaration (decl);
16818 if (TREE_CODE (decl) == FUNCTION_DECL)
16819 cp_parser_save_default_args (parser, decl);
16824 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16827 /* Parse a pure-specifier.
16832 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16833 Otherwise, ERROR_MARK_NODE is returned. */
16836 cp_parser_pure_specifier (cp_parser* parser)
16840 /* Look for the `=' token. */
16841 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16842 return error_mark_node;
16843 /* Look for the `0' token. */
16844 token = cp_lexer_peek_token (parser->lexer);
16846 if (token->type == CPP_EOF
16847 || token->type == CPP_PRAGMA_EOL)
16848 return error_mark_node;
16850 cp_lexer_consume_token (parser->lexer);
16852 /* Accept = default or = delete in c++0x mode. */
16853 if (token->keyword == RID_DEFAULT
16854 || token->keyword == RID_DELETE)
16856 maybe_warn_cpp0x ("defaulted and deleted functions");
16857 return token->u.value;
16860 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16861 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16863 cp_parser_error (parser,
16864 "invalid pure specifier (only %<= 0%> is allowed)");
16865 cp_parser_skip_to_end_of_statement (parser);
16866 return error_mark_node;
16868 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16870 error_at (token->location, "templates may not be %<virtual%>");
16871 return error_mark_node;
16874 return integer_zero_node;
16877 /* Parse a constant-initializer.
16879 constant-initializer:
16880 = constant-expression
16882 Returns a representation of the constant-expression. */
16885 cp_parser_constant_initializer (cp_parser* parser)
16887 /* Look for the `=' token. */
16888 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16889 return error_mark_node;
16891 /* It is invalid to write:
16893 struct S { static const int i = { 7 }; };
16896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16898 cp_parser_error (parser,
16899 "a brace-enclosed initializer is not allowed here");
16900 /* Consume the opening brace. */
16901 cp_lexer_consume_token (parser->lexer);
16902 /* Skip the initializer. */
16903 cp_parser_skip_to_closing_brace (parser);
16904 /* Look for the trailing `}'. */
16905 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16907 return error_mark_node;
16910 return cp_parser_constant_expression (parser,
16911 /*allow_non_constant=*/false,
16915 /* Derived classes [gram.class.derived] */
16917 /* Parse a base-clause.
16920 : base-specifier-list
16922 base-specifier-list:
16923 base-specifier ... [opt]
16924 base-specifier-list , base-specifier ... [opt]
16926 Returns a TREE_LIST representing the base-classes, in the order in
16927 which they were declared. The representation of each node is as
16928 described by cp_parser_base_specifier.
16930 In the case that no bases are specified, this function will return
16931 NULL_TREE, not ERROR_MARK_NODE. */
16934 cp_parser_base_clause (cp_parser* parser)
16936 tree bases = NULL_TREE;
16938 /* Look for the `:' that begins the list. */
16939 cp_parser_require (parser, CPP_COLON, "%<:%>");
16941 /* Scan the base-specifier-list. */
16946 bool pack_expansion_p = false;
16948 /* Look for the base-specifier. */
16949 base = cp_parser_base_specifier (parser);
16950 /* Look for the (optional) ellipsis. */
16951 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16953 /* Consume the `...'. */
16954 cp_lexer_consume_token (parser->lexer);
16956 pack_expansion_p = true;
16959 /* Add BASE to the front of the list. */
16960 if (base != error_mark_node)
16962 if (pack_expansion_p)
16963 /* Make this a pack expansion type. */
16964 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16967 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16969 TREE_CHAIN (base) = bases;
16973 /* Peek at the next token. */
16974 token = cp_lexer_peek_token (parser->lexer);
16975 /* If it's not a comma, then the list is complete. */
16976 if (token->type != CPP_COMMA)
16978 /* Consume the `,'. */
16979 cp_lexer_consume_token (parser->lexer);
16982 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16983 base class had a qualified name. However, the next name that
16984 appears is certainly not qualified. */
16985 parser->scope = NULL_TREE;
16986 parser->qualifying_scope = NULL_TREE;
16987 parser->object_scope = NULL_TREE;
16989 return nreverse (bases);
16992 /* Parse a base-specifier.
16995 :: [opt] nested-name-specifier [opt] class-name
16996 virtual access-specifier [opt] :: [opt] nested-name-specifier
16998 access-specifier virtual [opt] :: [opt] nested-name-specifier
17001 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17002 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17003 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17004 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17007 cp_parser_base_specifier (cp_parser* parser)
17011 bool virtual_p = false;
17012 bool duplicate_virtual_error_issued_p = false;
17013 bool duplicate_access_error_issued_p = false;
17014 bool class_scope_p, template_p;
17015 tree access = access_default_node;
17018 /* Process the optional `virtual' and `access-specifier'. */
17021 /* Peek at the next token. */
17022 token = cp_lexer_peek_token (parser->lexer);
17023 /* Process `virtual'. */
17024 switch (token->keyword)
17027 /* If `virtual' appears more than once, issue an error. */
17028 if (virtual_p && !duplicate_virtual_error_issued_p)
17030 cp_parser_error (parser,
17031 "%<virtual%> specified more than once in base-specified");
17032 duplicate_virtual_error_issued_p = true;
17037 /* Consume the `virtual' token. */
17038 cp_lexer_consume_token (parser->lexer);
17043 case RID_PROTECTED:
17045 /* If more than one access specifier appears, issue an
17047 if (access != access_default_node
17048 && !duplicate_access_error_issued_p)
17050 cp_parser_error (parser,
17051 "more than one access specifier in base-specified");
17052 duplicate_access_error_issued_p = true;
17055 access = ridpointers[(int) token->keyword];
17057 /* Consume the access-specifier. */
17058 cp_lexer_consume_token (parser->lexer);
17067 /* It is not uncommon to see programs mechanically, erroneously, use
17068 the 'typename' keyword to denote (dependent) qualified types
17069 as base classes. */
17070 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17072 token = cp_lexer_peek_token (parser->lexer);
17073 if (!processing_template_decl)
17074 error_at (token->location,
17075 "keyword %<typename%> not allowed outside of templates");
17077 error_at (token->location,
17078 "keyword %<typename%> not allowed in this context "
17079 "(the base class is implicitly a type)");
17080 cp_lexer_consume_token (parser->lexer);
17083 /* Look for the optional `::' operator. */
17084 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17085 /* Look for the nested-name-specifier. The simplest way to
17090 The keyword `typename' is not permitted in a base-specifier or
17091 mem-initializer; in these contexts a qualified name that
17092 depends on a template-parameter is implicitly assumed to be a
17095 is to pretend that we have seen the `typename' keyword at this
17097 cp_parser_nested_name_specifier_opt (parser,
17098 /*typename_keyword_p=*/true,
17099 /*check_dependency_p=*/true,
17101 /*is_declaration=*/true);
17102 /* If the base class is given by a qualified name, assume that names
17103 we see are type names or templates, as appropriate. */
17104 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17105 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17107 /* Finally, look for the class-name. */
17108 type = cp_parser_class_name (parser,
17112 /*check_dependency_p=*/true,
17113 /*class_head_p=*/false,
17114 /*is_declaration=*/true);
17116 if (type == error_mark_node)
17117 return error_mark_node;
17119 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17122 /* Exception handling [gram.exception] */
17124 /* Parse an (optional) exception-specification.
17126 exception-specification:
17127 throw ( type-id-list [opt] )
17129 Returns a TREE_LIST representing the exception-specification. The
17130 TREE_VALUE of each node is a type. */
17133 cp_parser_exception_specification_opt (cp_parser* parser)
17138 /* Peek at the next token. */
17139 token = cp_lexer_peek_token (parser->lexer);
17140 /* If it's not `throw', then there's no exception-specification. */
17141 if (!cp_parser_is_keyword (token, RID_THROW))
17144 /* Consume the `throw'. */
17145 cp_lexer_consume_token (parser->lexer);
17147 /* Look for the `('. */
17148 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17150 /* Peek at the next token. */
17151 token = cp_lexer_peek_token (parser->lexer);
17152 /* If it's not a `)', then there is a type-id-list. */
17153 if (token->type != CPP_CLOSE_PAREN)
17155 const char *saved_message;
17157 /* Types may not be defined in an exception-specification. */
17158 saved_message = parser->type_definition_forbidden_message;
17159 parser->type_definition_forbidden_message
17160 = "types may not be defined in an exception-specification";
17161 /* Parse the type-id-list. */
17162 type_id_list = cp_parser_type_id_list (parser);
17163 /* Restore the saved message. */
17164 parser->type_definition_forbidden_message = saved_message;
17167 type_id_list = empty_except_spec;
17169 /* Look for the `)'. */
17170 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17172 return type_id_list;
17175 /* Parse an (optional) type-id-list.
17179 type-id-list , type-id ... [opt]
17181 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17182 in the order that the types were presented. */
17185 cp_parser_type_id_list (cp_parser* parser)
17187 tree types = NULL_TREE;
17194 /* Get the next type-id. */
17195 type = cp_parser_type_id (parser);
17196 /* Parse the optional ellipsis. */
17197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17199 /* Consume the `...'. */
17200 cp_lexer_consume_token (parser->lexer);
17202 /* Turn the type into a pack expansion expression. */
17203 type = make_pack_expansion (type);
17205 /* Add it to the list. */
17206 types = add_exception_specifier (types, type, /*complain=*/1);
17207 /* Peek at the next token. */
17208 token = cp_lexer_peek_token (parser->lexer);
17209 /* If it is not a `,', we are done. */
17210 if (token->type != CPP_COMMA)
17212 /* Consume the `,'. */
17213 cp_lexer_consume_token (parser->lexer);
17216 return nreverse (types);
17219 /* Parse a try-block.
17222 try compound-statement handler-seq */
17225 cp_parser_try_block (cp_parser* parser)
17229 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17230 try_block = begin_try_block ();
17231 cp_parser_compound_statement (parser, NULL, true);
17232 finish_try_block (try_block);
17233 cp_parser_handler_seq (parser);
17234 finish_handler_sequence (try_block);
17239 /* Parse a function-try-block.
17241 function-try-block:
17242 try ctor-initializer [opt] function-body handler-seq */
17245 cp_parser_function_try_block (cp_parser* parser)
17247 tree compound_stmt;
17249 bool ctor_initializer_p;
17251 /* Look for the `try' keyword. */
17252 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17254 /* Let the rest of the front end know where we are. */
17255 try_block = begin_function_try_block (&compound_stmt);
17256 /* Parse the function-body. */
17258 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17259 /* We're done with the `try' part. */
17260 finish_function_try_block (try_block);
17261 /* Parse the handlers. */
17262 cp_parser_handler_seq (parser);
17263 /* We're done with the handlers. */
17264 finish_function_handler_sequence (try_block, compound_stmt);
17266 return ctor_initializer_p;
17269 /* Parse a handler-seq.
17272 handler handler-seq [opt] */
17275 cp_parser_handler_seq (cp_parser* parser)
17281 /* Parse the handler. */
17282 cp_parser_handler (parser);
17283 /* Peek at the next token. */
17284 token = cp_lexer_peek_token (parser->lexer);
17285 /* If it's not `catch' then there are no more handlers. */
17286 if (!cp_parser_is_keyword (token, RID_CATCH))
17291 /* Parse a handler.
17294 catch ( exception-declaration ) compound-statement */
17297 cp_parser_handler (cp_parser* parser)
17302 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17303 handler = begin_handler ();
17304 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17305 declaration = cp_parser_exception_declaration (parser);
17306 finish_handler_parms (declaration, handler);
17307 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17308 cp_parser_compound_statement (parser, NULL, false);
17309 finish_handler (handler);
17312 /* Parse an exception-declaration.
17314 exception-declaration:
17315 type-specifier-seq declarator
17316 type-specifier-seq abstract-declarator
17320 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17321 ellipsis variant is used. */
17324 cp_parser_exception_declaration (cp_parser* parser)
17326 cp_decl_specifier_seq type_specifiers;
17327 cp_declarator *declarator;
17328 const char *saved_message;
17330 /* If it's an ellipsis, it's easy to handle. */
17331 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17333 /* Consume the `...' token. */
17334 cp_lexer_consume_token (parser->lexer);
17338 /* Types may not be defined in exception-declarations. */
17339 saved_message = parser->type_definition_forbidden_message;
17340 parser->type_definition_forbidden_message
17341 = "types may not be defined in exception-declarations";
17343 /* Parse the type-specifier-seq. */
17344 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17345 /*is_trailing_return=*/false,
17347 /* If it's a `)', then there is no declarator. */
17348 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17351 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17352 /*ctor_dtor_or_conv_p=*/NULL,
17353 /*parenthesized_p=*/NULL,
17354 /*member_p=*/false);
17356 /* Restore the saved message. */
17357 parser->type_definition_forbidden_message = saved_message;
17359 if (!type_specifiers.any_specifiers_p)
17360 return error_mark_node;
17362 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17365 /* Parse a throw-expression.
17368 throw assignment-expression [opt]
17370 Returns a THROW_EXPR representing the throw-expression. */
17373 cp_parser_throw_expression (cp_parser* parser)
17378 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17379 token = cp_lexer_peek_token (parser->lexer);
17380 /* Figure out whether or not there is an assignment-expression
17381 following the "throw" keyword. */
17382 if (token->type == CPP_COMMA
17383 || token->type == CPP_SEMICOLON
17384 || token->type == CPP_CLOSE_PAREN
17385 || token->type == CPP_CLOSE_SQUARE
17386 || token->type == CPP_CLOSE_BRACE
17387 || token->type == CPP_COLON)
17388 expression = NULL_TREE;
17390 expression = cp_parser_assignment_expression (parser,
17391 /*cast_p=*/false, NULL);
17393 return build_throw (expression);
17396 /* GNU Extensions */
17398 /* Parse an (optional) asm-specification.
17401 asm ( string-literal )
17403 If the asm-specification is present, returns a STRING_CST
17404 corresponding to the string-literal. Otherwise, returns
17408 cp_parser_asm_specification_opt (cp_parser* parser)
17411 tree asm_specification;
17413 /* Peek at the next token. */
17414 token = cp_lexer_peek_token (parser->lexer);
17415 /* If the next token isn't the `asm' keyword, then there's no
17416 asm-specification. */
17417 if (!cp_parser_is_keyword (token, RID_ASM))
17420 /* Consume the `asm' token. */
17421 cp_lexer_consume_token (parser->lexer);
17422 /* Look for the `('. */
17423 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17425 /* Look for the string-literal. */
17426 asm_specification = cp_parser_string_literal (parser, false, false);
17428 /* Look for the `)'. */
17429 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17431 return asm_specification;
17434 /* Parse an asm-operand-list.
17438 asm-operand-list , asm-operand
17441 string-literal ( expression )
17442 [ string-literal ] string-literal ( expression )
17444 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17445 each node is the expression. The TREE_PURPOSE is itself a
17446 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17447 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17448 is a STRING_CST for the string literal before the parenthesis. Returns
17449 ERROR_MARK_NODE if any of the operands are invalid. */
17452 cp_parser_asm_operand_list (cp_parser* parser)
17454 tree asm_operands = NULL_TREE;
17455 bool invalid_operands = false;
17459 tree string_literal;
17463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17465 /* Consume the `[' token. */
17466 cp_lexer_consume_token (parser->lexer);
17467 /* Read the operand name. */
17468 name = cp_parser_identifier (parser);
17469 if (name != error_mark_node)
17470 name = build_string (IDENTIFIER_LENGTH (name),
17471 IDENTIFIER_POINTER (name));
17472 /* Look for the closing `]'. */
17473 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17477 /* Look for the string-literal. */
17478 string_literal = cp_parser_string_literal (parser, false, false);
17480 /* Look for the `('. */
17481 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17482 /* Parse the expression. */
17483 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17484 /* Look for the `)'. */
17485 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17487 if (name == error_mark_node
17488 || string_literal == error_mark_node
17489 || expression == error_mark_node)
17490 invalid_operands = true;
17492 /* Add this operand to the list. */
17493 asm_operands = tree_cons (build_tree_list (name, string_literal),
17496 /* If the next token is not a `,', there are no more
17498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17500 /* Consume the `,'. */
17501 cp_lexer_consume_token (parser->lexer);
17504 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17507 /* Parse an asm-clobber-list.
17511 asm-clobber-list , string-literal
17513 Returns a TREE_LIST, indicating the clobbers in the order that they
17514 appeared. The TREE_VALUE of each node is a STRING_CST. */
17517 cp_parser_asm_clobber_list (cp_parser* parser)
17519 tree clobbers = NULL_TREE;
17523 tree string_literal;
17525 /* Look for the string literal. */
17526 string_literal = cp_parser_string_literal (parser, false, false);
17527 /* Add it to the list. */
17528 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17529 /* If the next token is not a `,', then the list is
17531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17533 /* Consume the `,' token. */
17534 cp_lexer_consume_token (parser->lexer);
17540 /* Parse an asm-label-list.
17544 asm-label-list , identifier
17546 Returns a TREE_LIST, indicating the labels in the order that they
17547 appeared. The TREE_VALUE of each node is a label. */
17550 cp_parser_asm_label_list (cp_parser* parser)
17552 tree labels = NULL_TREE;
17556 tree identifier, label, name;
17558 /* Look for the identifier. */
17559 identifier = cp_parser_identifier (parser);
17560 if (!error_operand_p (identifier))
17562 label = lookup_label (identifier);
17563 if (TREE_CODE (label) == LABEL_DECL)
17565 TREE_USED (label) = 1;
17566 check_goto (label);
17567 name = build_string (IDENTIFIER_LENGTH (identifier),
17568 IDENTIFIER_POINTER (identifier));
17569 labels = tree_cons (name, label, labels);
17572 /* If the next token is not a `,', then the list is
17574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17576 /* Consume the `,' token. */
17577 cp_lexer_consume_token (parser->lexer);
17580 return nreverse (labels);
17583 /* Parse an (optional) series of attributes.
17586 attributes attribute
17589 __attribute__ (( attribute-list [opt] ))
17591 The return value is as for cp_parser_attribute_list. */
17594 cp_parser_attributes_opt (cp_parser* parser)
17596 tree attributes = NULL_TREE;
17601 tree attribute_list;
17603 /* Peek at the next token. */
17604 token = cp_lexer_peek_token (parser->lexer);
17605 /* If it's not `__attribute__', then we're done. */
17606 if (token->keyword != RID_ATTRIBUTE)
17609 /* Consume the `__attribute__' keyword. */
17610 cp_lexer_consume_token (parser->lexer);
17611 /* Look for the two `(' tokens. */
17612 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17613 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17615 /* Peek at the next token. */
17616 token = cp_lexer_peek_token (parser->lexer);
17617 if (token->type != CPP_CLOSE_PAREN)
17618 /* Parse the attribute-list. */
17619 attribute_list = cp_parser_attribute_list (parser);
17621 /* If the next token is a `)', then there is no attribute
17623 attribute_list = NULL;
17625 /* Look for the two `)' tokens. */
17626 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17627 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17629 /* Add these new attributes to the list. */
17630 attributes = chainon (attributes, attribute_list);
17636 /* Parse an attribute-list.
17640 attribute-list , attribute
17644 identifier ( identifier )
17645 identifier ( identifier , expression-list )
17646 identifier ( expression-list )
17648 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17649 to an attribute. The TREE_PURPOSE of each node is the identifier
17650 indicating which attribute is in use. The TREE_VALUE represents
17651 the arguments, if any. */
17654 cp_parser_attribute_list (cp_parser* parser)
17656 tree attribute_list = NULL_TREE;
17657 bool save_translate_strings_p = parser->translate_strings_p;
17659 parser->translate_strings_p = false;
17666 /* Look for the identifier. We also allow keywords here; for
17667 example `__attribute__ ((const))' is legal. */
17668 token = cp_lexer_peek_token (parser->lexer);
17669 if (token->type == CPP_NAME
17670 || token->type == CPP_KEYWORD)
17672 tree arguments = NULL_TREE;
17674 /* Consume the token. */
17675 token = cp_lexer_consume_token (parser->lexer);
17677 /* Save away the identifier that indicates which attribute
17679 identifier = (token->type == CPP_KEYWORD)
17680 /* For keywords, use the canonical spelling, not the
17681 parsed identifier. */
17682 ? ridpointers[(int) token->keyword]
17685 attribute = build_tree_list (identifier, NULL_TREE);
17687 /* Peek at the next token. */
17688 token = cp_lexer_peek_token (parser->lexer);
17689 /* If it's an `(', then parse the attribute arguments. */
17690 if (token->type == CPP_OPEN_PAREN)
17693 vec = cp_parser_parenthesized_expression_list
17694 (parser, true, /*cast_p=*/false,
17695 /*allow_expansion_p=*/false,
17696 /*non_constant_p=*/NULL);
17698 arguments = error_mark_node;
17701 arguments = build_tree_list_vec (vec);
17702 release_tree_vector (vec);
17704 /* Save the arguments away. */
17705 TREE_VALUE (attribute) = arguments;
17708 if (arguments != error_mark_node)
17710 /* Add this attribute to the list. */
17711 TREE_CHAIN (attribute) = attribute_list;
17712 attribute_list = attribute;
17715 token = cp_lexer_peek_token (parser->lexer);
17717 /* Now, look for more attributes. If the next token isn't a
17718 `,', we're done. */
17719 if (token->type != CPP_COMMA)
17722 /* Consume the comma and keep going. */
17723 cp_lexer_consume_token (parser->lexer);
17725 parser->translate_strings_p = save_translate_strings_p;
17727 /* We built up the list in reverse order. */
17728 return nreverse (attribute_list);
17731 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17732 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17733 current value of the PEDANTIC flag, regardless of whether or not
17734 the `__extension__' keyword is present. The caller is responsible
17735 for restoring the value of the PEDANTIC flag. */
17738 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17740 /* Save the old value of the PEDANTIC flag. */
17741 *saved_pedantic = pedantic;
17743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17745 /* Consume the `__extension__' token. */
17746 cp_lexer_consume_token (parser->lexer);
17747 /* We're not being pedantic while the `__extension__' keyword is
17757 /* Parse a label declaration.
17760 __label__ label-declarator-seq ;
17762 label-declarator-seq:
17763 identifier , label-declarator-seq
17767 cp_parser_label_declaration (cp_parser* parser)
17769 /* Look for the `__label__' keyword. */
17770 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17776 /* Look for an identifier. */
17777 identifier = cp_parser_identifier (parser);
17778 /* If we failed, stop. */
17779 if (identifier == error_mark_node)
17781 /* Declare it as a label. */
17782 finish_label_decl (identifier);
17783 /* If the next token is a `;', stop. */
17784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17786 /* Look for the `,' separating the label declarations. */
17787 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17790 /* Look for the final `;'. */
17791 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17794 /* Support Functions */
17796 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17797 NAME should have one of the representations used for an
17798 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17799 is returned. If PARSER->SCOPE is a dependent type, then a
17800 SCOPE_REF is returned.
17802 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17803 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17804 was formed. Abstractly, such entities should not be passed to this
17805 function, because they do not need to be looked up, but it is
17806 simpler to check for this special case here, rather than at the
17809 In cases not explicitly covered above, this function returns a
17810 DECL, OVERLOAD, or baselink representing the result of the lookup.
17811 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17814 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17815 (e.g., "struct") that was used. In that case bindings that do not
17816 refer to types are ignored.
17818 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17821 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17824 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17827 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17828 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17829 NULL_TREE otherwise. */
17832 cp_parser_lookup_name (cp_parser *parser, tree name,
17833 enum tag_types tag_type,
17836 bool check_dependency,
17837 tree *ambiguous_decls,
17838 location_t name_location)
17842 tree object_type = parser->context->object_type;
17844 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17845 flags |= LOOKUP_COMPLAIN;
17847 /* Assume that the lookup will be unambiguous. */
17848 if (ambiguous_decls)
17849 *ambiguous_decls = NULL_TREE;
17851 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17852 no longer valid. Note that if we are parsing tentatively, and
17853 the parse fails, OBJECT_TYPE will be automatically restored. */
17854 parser->context->object_type = NULL_TREE;
17856 if (name == error_mark_node)
17857 return error_mark_node;
17859 /* A template-id has already been resolved; there is no lookup to
17861 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17863 if (BASELINK_P (name))
17865 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17866 == TEMPLATE_ID_EXPR);
17870 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17871 it should already have been checked to make sure that the name
17872 used matches the type being destroyed. */
17873 if (TREE_CODE (name) == BIT_NOT_EXPR)
17877 /* Figure out to which type this destructor applies. */
17879 type = parser->scope;
17880 else if (object_type)
17881 type = object_type;
17883 type = current_class_type;
17884 /* If that's not a class type, there is no destructor. */
17885 if (!type || !CLASS_TYPE_P (type))
17886 return error_mark_node;
17887 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17888 lazily_declare_fn (sfk_destructor, type);
17889 if (!CLASSTYPE_DESTRUCTORS (type))
17890 return error_mark_node;
17891 /* If it was a class type, return the destructor. */
17892 return CLASSTYPE_DESTRUCTORS (type);
17895 /* By this point, the NAME should be an ordinary identifier. If
17896 the id-expression was a qualified name, the qualifying scope is
17897 stored in PARSER->SCOPE at this point. */
17898 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17900 /* Perform the lookup. */
17905 if (parser->scope == error_mark_node)
17906 return error_mark_node;
17908 /* If the SCOPE is dependent, the lookup must be deferred until
17909 the template is instantiated -- unless we are explicitly
17910 looking up names in uninstantiated templates. Even then, we
17911 cannot look up the name if the scope is not a class type; it
17912 might, for example, be a template type parameter. */
17913 dependent_p = (TYPE_P (parser->scope)
17914 && dependent_scope_p (parser->scope));
17915 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17917 /* Defer lookup. */
17918 decl = error_mark_node;
17921 tree pushed_scope = NULL_TREE;
17923 /* If PARSER->SCOPE is a dependent type, then it must be a
17924 class type, and we must not be checking dependencies;
17925 otherwise, we would have processed this lookup above. So
17926 that PARSER->SCOPE is not considered a dependent base by
17927 lookup_member, we must enter the scope here. */
17929 pushed_scope = push_scope (parser->scope);
17930 /* If the PARSER->SCOPE is a template specialization, it
17931 may be instantiated during name lookup. In that case,
17932 errors may be issued. Even if we rollback the current
17933 tentative parse, those errors are valid. */
17934 decl = lookup_qualified_name (parser->scope, name,
17935 tag_type != none_type,
17936 /*complain=*/true);
17938 /* If we have a single function from a using decl, pull it out. */
17939 if (TREE_CODE (decl) == OVERLOAD
17940 && !really_overloaded_fn (decl))
17941 decl = OVL_FUNCTION (decl);
17944 pop_scope (pushed_scope);
17947 /* If the scope is a dependent type and either we deferred lookup or
17948 we did lookup but didn't find the name, rememeber the name. */
17949 if (decl == error_mark_node && TYPE_P (parser->scope)
17950 && dependent_type_p (parser->scope))
17956 /* The resolution to Core Issue 180 says that `struct
17957 A::B' should be considered a type-name, even if `A'
17959 type = make_typename_type (parser->scope, name, tag_type,
17960 /*complain=*/tf_error);
17961 decl = TYPE_NAME (type);
17963 else if (is_template
17964 && (cp_parser_next_token_ends_template_argument_p (parser)
17965 || cp_lexer_next_token_is (parser->lexer,
17967 decl = make_unbound_class_template (parser->scope,
17969 /*complain=*/tf_error);
17971 decl = build_qualified_name (/*type=*/NULL_TREE,
17972 parser->scope, name,
17975 parser->qualifying_scope = parser->scope;
17976 parser->object_scope = NULL_TREE;
17978 else if (object_type)
17980 tree object_decl = NULL_TREE;
17981 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17982 OBJECT_TYPE is not a class. */
17983 if (CLASS_TYPE_P (object_type))
17984 /* If the OBJECT_TYPE is a template specialization, it may
17985 be instantiated during name lookup. In that case, errors
17986 may be issued. Even if we rollback the current tentative
17987 parse, those errors are valid. */
17988 object_decl = lookup_member (object_type,
17991 tag_type != none_type);
17992 /* Look it up in the enclosing context, too. */
17993 decl = lookup_name_real (name, tag_type != none_type,
17995 /*block_p=*/true, is_namespace, flags);
17996 parser->object_scope = object_type;
17997 parser->qualifying_scope = NULL_TREE;
17999 decl = object_decl;
18003 decl = lookup_name_real (name, tag_type != none_type,
18005 /*block_p=*/true, is_namespace, flags);
18006 parser->qualifying_scope = NULL_TREE;
18007 parser->object_scope = NULL_TREE;
18010 /* If the lookup failed, let our caller know. */
18011 if (!decl || decl == error_mark_node)
18012 return error_mark_node;
18014 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18015 if (TREE_CODE (decl) == TREE_LIST)
18017 if (ambiguous_decls)
18018 *ambiguous_decls = decl;
18019 /* The error message we have to print is too complicated for
18020 cp_parser_error, so we incorporate its actions directly. */
18021 if (!cp_parser_simulate_error (parser))
18023 error_at (name_location, "reference to %qD is ambiguous",
18025 print_candidates (decl);
18027 return error_mark_node;
18030 gcc_assert (DECL_P (decl)
18031 || TREE_CODE (decl) == OVERLOAD
18032 || TREE_CODE (decl) == SCOPE_REF
18033 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18034 || BASELINK_P (decl));
18036 /* If we have resolved the name of a member declaration, check to
18037 see if the declaration is accessible. When the name resolves to
18038 set of overloaded functions, accessibility is checked when
18039 overload resolution is done.
18041 During an explicit instantiation, access is not checked at all,
18042 as per [temp.explicit]. */
18044 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18049 /* Like cp_parser_lookup_name, but for use in the typical case where
18050 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18051 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18054 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18056 return cp_parser_lookup_name (parser, name,
18058 /*is_template=*/false,
18059 /*is_namespace=*/false,
18060 /*check_dependency=*/true,
18061 /*ambiguous_decls=*/NULL,
18065 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18066 the current context, return the TYPE_DECL. If TAG_NAME_P is
18067 true, the DECL indicates the class being defined in a class-head,
18068 or declared in an elaborated-type-specifier.
18070 Otherwise, return DECL. */
18073 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18075 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18076 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18079 template <typename T> struct B;
18082 template <typename T> struct A::B {};
18084 Similarly, in an elaborated-type-specifier:
18086 namespace N { struct X{}; }
18089 template <typename T> friend struct N::X;
18092 However, if the DECL refers to a class type, and we are in
18093 the scope of the class, then the name lookup automatically
18094 finds the TYPE_DECL created by build_self_reference rather
18095 than a TEMPLATE_DECL. For example, in:
18097 template <class T> struct S {
18101 there is no need to handle such case. */
18103 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18104 return DECL_TEMPLATE_RESULT (decl);
18109 /* If too many, or too few, template-parameter lists apply to the
18110 declarator, issue an error message. Returns TRUE if all went well,
18111 and FALSE otherwise. */
18114 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18115 cp_declarator *declarator,
18116 location_t declarator_location)
18118 unsigned num_templates;
18120 /* We haven't seen any classes that involve template parameters yet. */
18123 switch (declarator->kind)
18126 if (declarator->u.id.qualifying_scope)
18131 scope = declarator->u.id.qualifying_scope;
18132 member = declarator->u.id.unqualified_name;
18134 while (scope && CLASS_TYPE_P (scope))
18136 /* You're supposed to have one `template <...>'
18137 for every template class, but you don't need one
18138 for a full specialization. For example:
18140 template <class T> struct S{};
18141 template <> struct S<int> { void f(); };
18142 void S<int>::f () {}
18144 is correct; there shouldn't be a `template <>' for
18145 the definition of `S<int>::f'. */
18146 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18147 /* If SCOPE does not have template information of any
18148 kind, then it is not a template, nor is it nested
18149 within a template. */
18151 if (explicit_class_specialization_p (scope))
18153 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18156 scope = TYPE_CONTEXT (scope);
18159 else if (TREE_CODE (declarator->u.id.unqualified_name)
18160 == TEMPLATE_ID_EXPR)
18161 /* If the DECLARATOR has the form `X<y>' then it uses one
18162 additional level of template parameters. */
18165 return cp_parser_check_template_parameters
18166 (parser, num_templates, declarator_location, declarator);
18172 case cdk_reference:
18174 return (cp_parser_check_declarator_template_parameters
18175 (parser, declarator->declarator, declarator_location));
18181 gcc_unreachable ();
18186 /* NUM_TEMPLATES were used in the current declaration. If that is
18187 invalid, return FALSE and issue an error messages. Otherwise,
18188 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18189 declarator and we can print more accurate diagnostics. */
18192 cp_parser_check_template_parameters (cp_parser* parser,
18193 unsigned num_templates,
18194 location_t location,
18195 cp_declarator *declarator)
18197 /* If there are the same number of template classes and parameter
18198 lists, that's OK. */
18199 if (parser->num_template_parameter_lists == num_templates)
18201 /* If there are more, but only one more, then we are referring to a
18202 member template. That's OK too. */
18203 if (parser->num_template_parameter_lists == num_templates + 1)
18205 /* If there are more template classes than parameter lists, we have
18208 template <class T> void S<T>::R<T>::f (); */
18209 if (parser->num_template_parameter_lists < num_templates)
18211 if (declarator && !current_function_decl)
18212 error_at (location, "specializing member %<%T::%E%> "
18213 "requires %<template<>%> syntax",
18214 declarator->u.id.qualifying_scope,
18215 declarator->u.id.unqualified_name);
18216 else if (declarator)
18217 error_at (location, "invalid declaration of %<%T::%E%>",
18218 declarator->u.id.qualifying_scope,
18219 declarator->u.id.unqualified_name);
18221 error_at (location, "too few template-parameter-lists");
18224 /* Otherwise, there are too many template parameter lists. We have
18227 template <class T> template <class U> void S::f(); */
18228 error_at (location, "too many template-parameter-lists");
18232 /* Parse an optional `::' token indicating that the following name is
18233 from the global namespace. If so, PARSER->SCOPE is set to the
18234 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18235 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18236 Returns the new value of PARSER->SCOPE, if the `::' token is
18237 present, and NULL_TREE otherwise. */
18240 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18244 /* Peek at the next token. */
18245 token = cp_lexer_peek_token (parser->lexer);
18246 /* If we're looking at a `::' token then we're starting from the
18247 global namespace, not our current location. */
18248 if (token->type == CPP_SCOPE)
18250 /* Consume the `::' token. */
18251 cp_lexer_consume_token (parser->lexer);
18252 /* Set the SCOPE so that we know where to start the lookup. */
18253 parser->scope = global_namespace;
18254 parser->qualifying_scope = global_namespace;
18255 parser->object_scope = NULL_TREE;
18257 return parser->scope;
18259 else if (!current_scope_valid_p)
18261 parser->scope = NULL_TREE;
18262 parser->qualifying_scope = NULL_TREE;
18263 parser->object_scope = NULL_TREE;
18269 /* Returns TRUE if the upcoming token sequence is the start of a
18270 constructor declarator. If FRIEND_P is true, the declarator is
18271 preceded by the `friend' specifier. */
18274 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18276 bool constructor_p;
18277 tree type_decl = NULL_TREE;
18278 bool nested_name_p;
18279 cp_token *next_token;
18281 /* The common case is that this is not a constructor declarator, so
18282 try to avoid doing lots of work if at all possible. It's not
18283 valid declare a constructor at function scope. */
18284 if (parser->in_function_body)
18286 /* And only certain tokens can begin a constructor declarator. */
18287 next_token = cp_lexer_peek_token (parser->lexer);
18288 if (next_token->type != CPP_NAME
18289 && next_token->type != CPP_SCOPE
18290 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18291 && next_token->type != CPP_TEMPLATE_ID)
18294 /* Parse tentatively; we are going to roll back all of the tokens
18296 cp_parser_parse_tentatively (parser);
18297 /* Assume that we are looking at a constructor declarator. */
18298 constructor_p = true;
18300 /* Look for the optional `::' operator. */
18301 cp_parser_global_scope_opt (parser,
18302 /*current_scope_valid_p=*/false);
18303 /* Look for the nested-name-specifier. */
18305 = (cp_parser_nested_name_specifier_opt (parser,
18306 /*typename_keyword_p=*/false,
18307 /*check_dependency_p=*/false,
18309 /*is_declaration=*/false)
18311 /* Outside of a class-specifier, there must be a
18312 nested-name-specifier. */
18313 if (!nested_name_p &&
18314 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18316 constructor_p = false;
18317 /* If we still think that this might be a constructor-declarator,
18318 look for a class-name. */
18323 template <typename T> struct S { S(); };
18324 template <typename T> S<T>::S ();
18326 we must recognize that the nested `S' names a class.
18329 template <typename T> S<T>::S<T> ();
18331 we must recognize that the nested `S' names a template. */
18332 type_decl = cp_parser_class_name (parser,
18333 /*typename_keyword_p=*/false,
18334 /*template_keyword_p=*/false,
18336 /*check_dependency_p=*/false,
18337 /*class_head_p=*/false,
18338 /*is_declaration=*/false);
18339 /* If there was no class-name, then this is not a constructor. */
18340 constructor_p = !cp_parser_error_occurred (parser);
18343 /* If we're still considering a constructor, we have to see a `(',
18344 to begin the parameter-declaration-clause, followed by either a
18345 `)', an `...', or a decl-specifier. We need to check for a
18346 type-specifier to avoid being fooled into thinking that:
18350 is a constructor. (It is actually a function named `f' that
18351 takes one parameter (of type `int') and returns a value of type
18354 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18357 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18358 /* A parameter declaration begins with a decl-specifier,
18359 which is either the "attribute" keyword, a storage class
18360 specifier, or (usually) a type-specifier. */
18361 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18364 tree pushed_scope = NULL_TREE;
18365 unsigned saved_num_template_parameter_lists;
18367 /* Names appearing in the type-specifier should be looked up
18368 in the scope of the class. */
18369 if (current_class_type)
18373 type = TREE_TYPE (type_decl);
18374 if (TREE_CODE (type) == TYPENAME_TYPE)
18376 type = resolve_typename_type (type,
18377 /*only_current_p=*/false);
18378 if (TREE_CODE (type) == TYPENAME_TYPE)
18380 cp_parser_abort_tentative_parse (parser);
18384 pushed_scope = push_scope (type);
18387 /* Inside the constructor parameter list, surrounding
18388 template-parameter-lists do not apply. */
18389 saved_num_template_parameter_lists
18390 = parser->num_template_parameter_lists;
18391 parser->num_template_parameter_lists = 0;
18393 /* Look for the type-specifier. */
18394 cp_parser_type_specifier (parser,
18395 CP_PARSER_FLAGS_NONE,
18396 /*decl_specs=*/NULL,
18397 /*is_declarator=*/true,
18398 /*declares_class_or_enum=*/NULL,
18399 /*is_cv_qualifier=*/NULL);
18401 parser->num_template_parameter_lists
18402 = saved_num_template_parameter_lists;
18404 /* Leave the scope of the class. */
18406 pop_scope (pushed_scope);
18408 constructor_p = !cp_parser_error_occurred (parser);
18412 constructor_p = false;
18413 /* We did not really want to consume any tokens. */
18414 cp_parser_abort_tentative_parse (parser);
18416 return constructor_p;
18419 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18420 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18421 they must be performed once we are in the scope of the function.
18423 Returns the function defined. */
18426 cp_parser_function_definition_from_specifiers_and_declarator
18427 (cp_parser* parser,
18428 cp_decl_specifier_seq *decl_specifiers,
18430 const cp_declarator *declarator)
18435 /* Begin the function-definition. */
18436 success_p = start_function (decl_specifiers, declarator, attributes);
18438 /* The things we're about to see are not directly qualified by any
18439 template headers we've seen thus far. */
18440 reset_specialization ();
18442 /* If there were names looked up in the decl-specifier-seq that we
18443 did not check, check them now. We must wait until we are in the
18444 scope of the function to perform the checks, since the function
18445 might be a friend. */
18446 perform_deferred_access_checks ();
18450 /* Skip the entire function. */
18451 cp_parser_skip_to_end_of_block_or_statement (parser);
18452 fn = error_mark_node;
18454 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18456 /* Seen already, skip it. An error message has already been output. */
18457 cp_parser_skip_to_end_of_block_or_statement (parser);
18458 fn = current_function_decl;
18459 current_function_decl = NULL_TREE;
18460 /* If this is a function from a class, pop the nested class. */
18461 if (current_class_name)
18462 pop_nested_class ();
18465 fn = cp_parser_function_definition_after_declarator (parser,
18466 /*inline_p=*/false);
18471 /* Parse the part of a function-definition that follows the
18472 declarator. INLINE_P is TRUE iff this function is an inline
18473 function defined within a class-specifier.
18475 Returns the function defined. */
18478 cp_parser_function_definition_after_declarator (cp_parser* parser,
18482 bool ctor_initializer_p = false;
18483 bool saved_in_unbraced_linkage_specification_p;
18484 bool saved_in_function_body;
18485 unsigned saved_num_template_parameter_lists;
18488 saved_in_function_body = parser->in_function_body;
18489 parser->in_function_body = true;
18490 /* If the next token is `return', then the code may be trying to
18491 make use of the "named return value" extension that G++ used to
18493 token = cp_lexer_peek_token (parser->lexer);
18494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18496 /* Consume the `return' keyword. */
18497 cp_lexer_consume_token (parser->lexer);
18498 /* Look for the identifier that indicates what value is to be
18500 cp_parser_identifier (parser);
18501 /* Issue an error message. */
18502 error_at (token->location,
18503 "named return values are no longer supported");
18504 /* Skip tokens until we reach the start of the function body. */
18507 cp_token *token = cp_lexer_peek_token (parser->lexer);
18508 if (token->type == CPP_OPEN_BRACE
18509 || token->type == CPP_EOF
18510 || token->type == CPP_PRAGMA_EOL)
18512 cp_lexer_consume_token (parser->lexer);
18515 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18516 anything declared inside `f'. */
18517 saved_in_unbraced_linkage_specification_p
18518 = parser->in_unbraced_linkage_specification_p;
18519 parser->in_unbraced_linkage_specification_p = false;
18520 /* Inside the function, surrounding template-parameter-lists do not
18522 saved_num_template_parameter_lists
18523 = parser->num_template_parameter_lists;
18524 parser->num_template_parameter_lists = 0;
18526 start_lambda_scope (current_function_decl);
18528 /* If the next token is `try', then we are looking at a
18529 function-try-block. */
18530 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18531 ctor_initializer_p = cp_parser_function_try_block (parser);
18532 /* A function-try-block includes the function-body, so we only do
18533 this next part if we're not processing a function-try-block. */
18536 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18538 finish_lambda_scope ();
18540 /* Finish the function. */
18541 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18542 (inline_p ? 2 : 0));
18543 /* Generate code for it, if necessary. */
18544 expand_or_defer_fn (fn);
18545 /* Restore the saved values. */
18546 parser->in_unbraced_linkage_specification_p
18547 = saved_in_unbraced_linkage_specification_p;
18548 parser->num_template_parameter_lists
18549 = saved_num_template_parameter_lists;
18550 parser->in_function_body = saved_in_function_body;
18555 /* Parse a template-declaration, assuming that the `export' (and
18556 `extern') keywords, if present, has already been scanned. MEMBER_P
18557 is as for cp_parser_template_declaration. */
18560 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18562 tree decl = NULL_TREE;
18563 VEC (deferred_access_check,gc) *checks;
18564 tree parameter_list;
18565 bool friend_p = false;
18566 bool need_lang_pop;
18569 /* Look for the `template' keyword. */
18570 token = cp_lexer_peek_token (parser->lexer);
18571 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18575 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18577 if (at_class_scope_p () && current_function_decl)
18579 /* 14.5.2.2 [temp.mem]
18581 A local class shall not have member templates. */
18582 error_at (token->location,
18583 "invalid declaration of member template in local class");
18584 cp_parser_skip_to_end_of_block_or_statement (parser);
18589 A template ... shall not have C linkage. */
18590 if (current_lang_name == lang_name_c)
18592 error_at (token->location, "template with C linkage");
18593 /* Give it C++ linkage to avoid confusing other parts of the
18595 push_lang_context (lang_name_cplusplus);
18596 need_lang_pop = true;
18599 need_lang_pop = false;
18601 /* We cannot perform access checks on the template parameter
18602 declarations until we know what is being declared, just as we
18603 cannot check the decl-specifier list. */
18604 push_deferring_access_checks (dk_deferred);
18606 /* If the next token is `>', then we have an invalid
18607 specialization. Rather than complain about an invalid template
18608 parameter, issue an error message here. */
18609 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18611 cp_parser_error (parser, "invalid explicit specialization");
18612 begin_specialization ();
18613 parameter_list = NULL_TREE;
18616 /* Parse the template parameters. */
18617 parameter_list = cp_parser_template_parameter_list (parser);
18619 /* Get the deferred access checks from the parameter list. These
18620 will be checked once we know what is being declared, as for a
18621 member template the checks must be performed in the scope of the
18622 class containing the member. */
18623 checks = get_deferred_access_checks ();
18625 /* Look for the `>'. */
18626 cp_parser_skip_to_end_of_template_parameter_list (parser);
18627 /* We just processed one more parameter list. */
18628 ++parser->num_template_parameter_lists;
18629 /* If the next token is `template', there are more template
18631 if (cp_lexer_next_token_is_keyword (parser->lexer,
18633 cp_parser_template_declaration_after_export (parser, member_p);
18636 /* There are no access checks when parsing a template, as we do not
18637 know if a specialization will be a friend. */
18638 push_deferring_access_checks (dk_no_check);
18639 token = cp_lexer_peek_token (parser->lexer);
18640 decl = cp_parser_single_declaration (parser,
18643 /*explicit_specialization_p=*/false,
18645 pop_deferring_access_checks ();
18647 /* If this is a member template declaration, let the front
18649 if (member_p && !friend_p && decl)
18651 if (TREE_CODE (decl) == TYPE_DECL)
18652 cp_parser_check_access_in_redeclaration (decl, token->location);
18654 decl = finish_member_template_decl (decl);
18656 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18657 make_friend_class (current_class_type, TREE_TYPE (decl),
18658 /*complain=*/true);
18660 /* We are done with the current parameter list. */
18661 --parser->num_template_parameter_lists;
18663 pop_deferring_access_checks ();
18666 finish_template_decl (parameter_list);
18668 /* Register member declarations. */
18669 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18670 finish_member_declaration (decl);
18671 /* For the erroneous case of a template with C linkage, we pushed an
18672 implicit C++ linkage scope; exit that scope now. */
18674 pop_lang_context ();
18675 /* If DECL is a function template, we must return to parse it later.
18676 (Even though there is no definition, there might be default
18677 arguments that need handling.) */
18678 if (member_p && decl
18679 && (TREE_CODE (decl) == FUNCTION_DECL
18680 || DECL_FUNCTION_TEMPLATE_P (decl)))
18681 TREE_VALUE (parser->unparsed_functions_queues)
18682 = tree_cons (NULL_TREE, decl,
18683 TREE_VALUE (parser->unparsed_functions_queues));
18686 /* Perform the deferred access checks from a template-parameter-list.
18687 CHECKS is a TREE_LIST of access checks, as returned by
18688 get_deferred_access_checks. */
18691 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18693 ++processing_template_parmlist;
18694 perform_access_checks (checks);
18695 --processing_template_parmlist;
18698 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18699 `function-definition' sequence. MEMBER_P is true, this declaration
18700 appears in a class scope.
18702 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18703 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18706 cp_parser_single_declaration (cp_parser* parser,
18707 VEC (deferred_access_check,gc)* checks,
18709 bool explicit_specialization_p,
18712 int declares_class_or_enum;
18713 tree decl = NULL_TREE;
18714 cp_decl_specifier_seq decl_specifiers;
18715 bool function_definition_p = false;
18716 cp_token *decl_spec_token_start;
18718 /* This function is only used when processing a template
18720 gcc_assert (innermost_scope_kind () == sk_template_parms
18721 || innermost_scope_kind () == sk_template_spec);
18723 /* Defer access checks until we know what is being declared. */
18724 push_deferring_access_checks (dk_deferred);
18726 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18728 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18729 cp_parser_decl_specifier_seq (parser,
18730 CP_PARSER_FLAGS_OPTIONAL,
18732 &declares_class_or_enum);
18734 *friend_p = cp_parser_friend_p (&decl_specifiers);
18736 /* There are no template typedefs. */
18737 if (decl_specifiers.specs[(int) ds_typedef])
18739 error_at (decl_spec_token_start->location,
18740 "template declaration of %<typedef%>");
18741 decl = error_mark_node;
18744 /* Gather up the access checks that occurred the
18745 decl-specifier-seq. */
18746 stop_deferring_access_checks ();
18748 /* Check for the declaration of a template class. */
18749 if (declares_class_or_enum)
18751 if (cp_parser_declares_only_class_p (parser))
18753 decl = shadow_tag (&decl_specifiers);
18758 friend template <typename T> struct A<T>::B;
18761 A<T>::B will be represented by a TYPENAME_TYPE, and
18762 therefore not recognized by shadow_tag. */
18763 if (friend_p && *friend_p
18765 && decl_specifiers.type
18766 && TYPE_P (decl_specifiers.type))
18767 decl = decl_specifiers.type;
18769 if (decl && decl != error_mark_node)
18770 decl = TYPE_NAME (decl);
18772 decl = error_mark_node;
18774 /* Perform access checks for template parameters. */
18775 cp_parser_perform_template_parameter_access_checks (checks);
18778 /* If it's not a template class, try for a template function. If
18779 the next token is a `;', then this declaration does not declare
18780 anything. But, if there were errors in the decl-specifiers, then
18781 the error might well have come from an attempted class-specifier.
18782 In that case, there's no need to warn about a missing declarator. */
18784 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18785 || decl_specifiers.type != error_mark_node))
18787 decl = cp_parser_init_declarator (parser,
18790 /*function_definition_allowed_p=*/true,
18792 declares_class_or_enum,
18793 &function_definition_p);
18795 /* 7.1.1-1 [dcl.stc]
18797 A storage-class-specifier shall not be specified in an explicit
18798 specialization... */
18800 && explicit_specialization_p
18801 && decl_specifiers.storage_class != sc_none)
18803 error_at (decl_spec_token_start->location,
18804 "explicit template specialization cannot have a storage class");
18805 decl = error_mark_node;
18809 pop_deferring_access_checks ();
18811 /* Clear any current qualification; whatever comes next is the start
18812 of something new. */
18813 parser->scope = NULL_TREE;
18814 parser->qualifying_scope = NULL_TREE;
18815 parser->object_scope = NULL_TREE;
18816 /* Look for a trailing `;' after the declaration. */
18817 if (!function_definition_p
18818 && (decl == error_mark_node
18819 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18820 cp_parser_skip_to_end_of_block_or_statement (parser);
18825 /* Parse a cast-expression that is not the operand of a unary "&". */
18828 cp_parser_simple_cast_expression (cp_parser *parser)
18830 return cp_parser_cast_expression (parser, /*address_p=*/false,
18831 /*cast_p=*/false, NULL);
18834 /* Parse a functional cast to TYPE. Returns an expression
18835 representing the cast. */
18838 cp_parser_functional_cast (cp_parser* parser, tree type)
18841 tree expression_list;
18845 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18847 maybe_warn_cpp0x ("extended initializer lists");
18848 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18849 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18850 if (TREE_CODE (type) == TYPE_DECL)
18851 type = TREE_TYPE (type);
18852 return finish_compound_literal (type, expression_list);
18856 vec = cp_parser_parenthesized_expression_list (parser, false,
18858 /*allow_expansion_p=*/true,
18859 /*non_constant_p=*/NULL);
18861 expression_list = error_mark_node;
18864 expression_list = build_tree_list_vec (vec);
18865 release_tree_vector (vec);
18868 cast = build_functional_cast (type, expression_list,
18869 tf_warning_or_error);
18870 /* [expr.const]/1: In an integral constant expression "only type
18871 conversions to integral or enumeration type can be used". */
18872 if (TREE_CODE (type) == TYPE_DECL)
18873 type = TREE_TYPE (type);
18874 if (cast != error_mark_node
18875 && !cast_valid_in_integral_constant_expression_p (type)
18876 && (cp_parser_non_integral_constant_expression
18877 (parser, "a call to a constructor")))
18878 return error_mark_node;
18882 /* Save the tokens that make up the body of a member function defined
18883 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18884 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18885 specifiers applied to the declaration. Returns the FUNCTION_DECL
18886 for the member function. */
18889 cp_parser_save_member_function_body (cp_parser* parser,
18890 cp_decl_specifier_seq *decl_specifiers,
18891 cp_declarator *declarator,
18898 /* Create the FUNCTION_DECL. */
18899 fn = grokmethod (decl_specifiers, declarator, attributes);
18900 /* If something went badly wrong, bail out now. */
18901 if (fn == error_mark_node)
18903 /* If there's a function-body, skip it. */
18904 if (cp_parser_token_starts_function_definition_p
18905 (cp_lexer_peek_token (parser->lexer)))
18906 cp_parser_skip_to_end_of_block_or_statement (parser);
18907 return error_mark_node;
18910 /* Remember it, if there default args to post process. */
18911 cp_parser_save_default_args (parser, fn);
18913 /* Save away the tokens that make up the body of the
18915 first = parser->lexer->next_token;
18916 /* We can have braced-init-list mem-initializers before the fn body. */
18917 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18919 cp_lexer_consume_token (parser->lexer);
18920 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18921 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18923 /* cache_group will stop after an un-nested { } pair, too. */
18924 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18927 /* variadic mem-inits have ... after the ')'. */
18928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18929 cp_lexer_consume_token (parser->lexer);
18932 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18933 /* Handle function try blocks. */
18934 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18935 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18936 last = parser->lexer->next_token;
18938 /* Save away the inline definition; we will process it when the
18939 class is complete. */
18940 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18941 DECL_PENDING_INLINE_P (fn) = 1;
18943 /* We need to know that this was defined in the class, so that
18944 friend templates are handled correctly. */
18945 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18947 /* Add FN to the queue of functions to be parsed later. */
18948 TREE_VALUE (parser->unparsed_functions_queues)
18949 = tree_cons (NULL_TREE, fn,
18950 TREE_VALUE (parser->unparsed_functions_queues));
18955 /* Parse a template-argument-list, as well as the trailing ">" (but
18956 not the opening ">"). See cp_parser_template_argument_list for the
18960 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18964 tree saved_qualifying_scope;
18965 tree saved_object_scope;
18966 bool saved_greater_than_is_operator_p;
18967 int saved_unevaluated_operand;
18968 int saved_inhibit_evaluation_warnings;
18972 When parsing a template-id, the first non-nested `>' is taken as
18973 the end of the template-argument-list rather than a greater-than
18975 saved_greater_than_is_operator_p
18976 = parser->greater_than_is_operator_p;
18977 parser->greater_than_is_operator_p = false;
18978 /* Parsing the argument list may modify SCOPE, so we save it
18980 saved_scope = parser->scope;
18981 saved_qualifying_scope = parser->qualifying_scope;
18982 saved_object_scope = parser->object_scope;
18983 /* We need to evaluate the template arguments, even though this
18984 template-id may be nested within a "sizeof". */
18985 saved_unevaluated_operand = cp_unevaluated_operand;
18986 cp_unevaluated_operand = 0;
18987 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18988 c_inhibit_evaluation_warnings = 0;
18989 /* Parse the template-argument-list itself. */
18990 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18991 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18992 arguments = NULL_TREE;
18994 arguments = cp_parser_template_argument_list (parser);
18995 /* Look for the `>' that ends the template-argument-list. If we find
18996 a '>>' instead, it's probably just a typo. */
18997 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18999 if (cxx_dialect != cxx98)
19001 /* In C++0x, a `>>' in a template argument list or cast
19002 expression is considered to be two separate `>'
19003 tokens. So, change the current token to a `>', but don't
19004 consume it: it will be consumed later when the outer
19005 template argument list (or cast expression) is parsed.
19006 Note that this replacement of `>' for `>>' is necessary
19007 even if we are parsing tentatively: in the tentative
19008 case, after calling
19009 cp_parser_enclosed_template_argument_list we will always
19010 throw away all of the template arguments and the first
19011 closing `>', either because the template argument list
19012 was erroneous or because we are replacing those tokens
19013 with a CPP_TEMPLATE_ID token. The second `>' (which will
19014 not have been thrown away) is needed either to close an
19015 outer template argument list or to complete a new-style
19017 cp_token *token = cp_lexer_peek_token (parser->lexer);
19018 token->type = CPP_GREATER;
19020 else if (!saved_greater_than_is_operator_p)
19022 /* If we're in a nested template argument list, the '>>' has
19023 to be a typo for '> >'. We emit the error message, but we
19024 continue parsing and we push a '>' as next token, so that
19025 the argument list will be parsed correctly. Note that the
19026 global source location is still on the token before the
19027 '>>', so we need to say explicitly where we want it. */
19028 cp_token *token = cp_lexer_peek_token (parser->lexer);
19029 error_at (token->location, "%<>>%> should be %<> >%> "
19030 "within a nested template argument list");
19032 token->type = CPP_GREATER;
19036 /* If this is not a nested template argument list, the '>>'
19037 is a typo for '>'. Emit an error message and continue.
19038 Same deal about the token location, but here we can get it
19039 right by consuming the '>>' before issuing the diagnostic. */
19040 cp_token *token = cp_lexer_consume_token (parser->lexer);
19041 error_at (token->location,
19042 "spurious %<>>%>, use %<>%> to terminate "
19043 "a template argument list");
19047 cp_parser_skip_to_end_of_template_parameter_list (parser);
19048 /* The `>' token might be a greater-than operator again now. */
19049 parser->greater_than_is_operator_p
19050 = saved_greater_than_is_operator_p;
19051 /* Restore the SAVED_SCOPE. */
19052 parser->scope = saved_scope;
19053 parser->qualifying_scope = saved_qualifying_scope;
19054 parser->object_scope = saved_object_scope;
19055 cp_unevaluated_operand = saved_unevaluated_operand;
19056 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19061 /* MEMBER_FUNCTION is a member function, or a friend. If default
19062 arguments, or the body of the function have not yet been parsed,
19066 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19068 /* If this member is a template, get the underlying
19070 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19071 member_function = DECL_TEMPLATE_RESULT (member_function);
19073 /* There should not be any class definitions in progress at this
19074 point; the bodies of members are only parsed outside of all class
19076 gcc_assert (parser->num_classes_being_defined == 0);
19077 /* While we're parsing the member functions we might encounter more
19078 classes. We want to handle them right away, but we don't want
19079 them getting mixed up with functions that are currently in the
19081 parser->unparsed_functions_queues
19082 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19084 /* Make sure that any template parameters are in scope. */
19085 maybe_begin_member_template_processing (member_function);
19087 /* If the body of the function has not yet been parsed, parse it
19089 if (DECL_PENDING_INLINE_P (member_function))
19091 tree function_scope;
19092 cp_token_cache *tokens;
19094 /* The function is no longer pending; we are processing it. */
19095 tokens = DECL_PENDING_INLINE_INFO (member_function);
19096 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19097 DECL_PENDING_INLINE_P (member_function) = 0;
19099 /* If this is a local class, enter the scope of the containing
19101 function_scope = current_function_decl;
19102 if (function_scope)
19103 push_function_context ();
19105 /* Push the body of the function onto the lexer stack. */
19106 cp_parser_push_lexer_for_tokens (parser, tokens);
19108 /* Let the front end know that we going to be defining this
19110 start_preparsed_function (member_function, NULL_TREE,
19111 SF_PRE_PARSED | SF_INCLASS_INLINE);
19113 /* Don't do access checking if it is a templated function. */
19114 if (processing_template_decl)
19115 push_deferring_access_checks (dk_no_check);
19117 /* Now, parse the body of the function. */
19118 cp_parser_function_definition_after_declarator (parser,
19119 /*inline_p=*/true);
19121 if (processing_template_decl)
19122 pop_deferring_access_checks ();
19124 /* Leave the scope of the containing function. */
19125 if (function_scope)
19126 pop_function_context ();
19127 cp_parser_pop_lexer (parser);
19130 /* Remove any template parameters from the symbol table. */
19131 maybe_end_member_template_processing ();
19133 /* Restore the queue. */
19134 parser->unparsed_functions_queues
19135 = TREE_CHAIN (parser->unparsed_functions_queues);
19138 /* If DECL contains any default args, remember it on the unparsed
19139 functions queue. */
19142 cp_parser_save_default_args (cp_parser* parser, tree decl)
19146 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19148 probe = TREE_CHAIN (probe))
19149 if (TREE_PURPOSE (probe))
19151 TREE_PURPOSE (parser->unparsed_functions_queues)
19152 = tree_cons (current_class_type, decl,
19153 TREE_PURPOSE (parser->unparsed_functions_queues));
19158 /* FN is a FUNCTION_DECL which may contains a parameter with an
19159 unparsed DEFAULT_ARG. Parse the default args now. This function
19160 assumes that the current scope is the scope in which the default
19161 argument should be processed. */
19164 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19166 bool saved_local_variables_forbidden_p;
19167 tree parm, parmdecl;
19169 /* While we're parsing the default args, we might (due to the
19170 statement expression extension) encounter more classes. We want
19171 to handle them right away, but we don't want them getting mixed
19172 up with default args that are currently in the queue. */
19173 parser->unparsed_functions_queues
19174 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19176 /* Local variable names (and the `this' keyword) may not appear
19177 in a default argument. */
19178 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19179 parser->local_variables_forbidden_p = true;
19181 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19182 parmdecl = DECL_ARGUMENTS (fn);
19183 parm && parm != void_list_node;
19184 parm = TREE_CHAIN (parm),
19185 parmdecl = TREE_CHAIN (parmdecl))
19187 cp_token_cache *tokens;
19188 tree default_arg = TREE_PURPOSE (parm);
19190 VEC(tree,gc) *insts;
19197 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19198 /* This can happen for a friend declaration for a function
19199 already declared with default arguments. */
19202 /* Push the saved tokens for the default argument onto the parser's
19204 tokens = DEFARG_TOKENS (default_arg);
19205 cp_parser_push_lexer_for_tokens (parser, tokens);
19207 start_lambda_scope (parmdecl);
19209 /* Parse the assignment-expression. */
19210 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19211 if (parsed_arg == error_mark_node)
19213 cp_parser_pop_lexer (parser);
19217 if (!processing_template_decl)
19218 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19220 TREE_PURPOSE (parm) = parsed_arg;
19222 /* Update any instantiations we've already created. */
19223 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19224 VEC_iterate (tree, insts, ix, copy); ix++)
19225 TREE_PURPOSE (copy) = parsed_arg;
19227 finish_lambda_scope ();
19229 /* If the token stream has not been completely used up, then
19230 there was extra junk after the end of the default
19232 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19233 cp_parser_error (parser, "expected %<,%>");
19235 /* Revert to the main lexer. */
19236 cp_parser_pop_lexer (parser);
19239 /* Make sure no default arg is missing. */
19240 check_default_args (fn);
19242 /* Restore the state of local_variables_forbidden_p. */
19243 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19245 /* Restore the queue. */
19246 parser->unparsed_functions_queues
19247 = TREE_CHAIN (parser->unparsed_functions_queues);
19250 /* Parse the operand of `sizeof' (or a similar operator). Returns
19251 either a TYPE or an expression, depending on the form of the
19252 input. The KEYWORD indicates which kind of expression we have
19256 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19258 tree expr = NULL_TREE;
19259 const char *saved_message;
19261 bool saved_integral_constant_expression_p;
19262 bool saved_non_integral_constant_expression_p;
19263 bool pack_expansion_p = false;
19265 /* Types cannot be defined in a `sizeof' expression. Save away the
19267 saved_message = parser->type_definition_forbidden_message;
19268 /* And create the new one. */
19269 tmp = concat ("types may not be defined in %<",
19270 IDENTIFIER_POINTER (ridpointers[keyword]),
19271 "%> expressions", NULL);
19272 parser->type_definition_forbidden_message = tmp;
19274 /* The restrictions on constant-expressions do not apply inside
19275 sizeof expressions. */
19276 saved_integral_constant_expression_p
19277 = parser->integral_constant_expression_p;
19278 saved_non_integral_constant_expression_p
19279 = parser->non_integral_constant_expression_p;
19280 parser->integral_constant_expression_p = false;
19282 /* If it's a `...', then we are computing the length of a parameter
19284 if (keyword == RID_SIZEOF
19285 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19287 /* Consume the `...'. */
19288 cp_lexer_consume_token (parser->lexer);
19289 maybe_warn_variadic_templates ();
19291 /* Note that this is an expansion. */
19292 pack_expansion_p = true;
19295 /* Do not actually evaluate the expression. */
19296 ++cp_unevaluated_operand;
19297 ++c_inhibit_evaluation_warnings;
19298 /* If it's a `(', then we might be looking at the type-id
19300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19303 bool saved_in_type_id_in_expr_p;
19305 /* We can't be sure yet whether we're looking at a type-id or an
19307 cp_parser_parse_tentatively (parser);
19308 /* Consume the `('. */
19309 cp_lexer_consume_token (parser->lexer);
19310 /* Parse the type-id. */
19311 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19312 parser->in_type_id_in_expr_p = true;
19313 type = cp_parser_type_id (parser);
19314 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19315 /* Now, look for the trailing `)'. */
19316 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19317 /* If all went well, then we're done. */
19318 if (cp_parser_parse_definitely (parser))
19320 cp_decl_specifier_seq decl_specs;
19322 /* Build a trivial decl-specifier-seq. */
19323 clear_decl_specs (&decl_specs);
19324 decl_specs.type = type;
19326 /* Call grokdeclarator to figure out what type this is. */
19327 expr = grokdeclarator (NULL,
19331 /*attrlist=*/NULL);
19335 /* If the type-id production did not work out, then we must be
19336 looking at the unary-expression production. */
19338 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19339 /*cast_p=*/false, NULL);
19341 if (pack_expansion_p)
19342 /* Build a pack expansion. */
19343 expr = make_pack_expansion (expr);
19345 /* Go back to evaluating expressions. */
19346 --cp_unevaluated_operand;
19347 --c_inhibit_evaluation_warnings;
19349 /* Free the message we created. */
19351 /* And restore the old one. */
19352 parser->type_definition_forbidden_message = saved_message;
19353 parser->integral_constant_expression_p
19354 = saved_integral_constant_expression_p;
19355 parser->non_integral_constant_expression_p
19356 = saved_non_integral_constant_expression_p;
19361 /* If the current declaration has no declarator, return true. */
19364 cp_parser_declares_only_class_p (cp_parser *parser)
19366 /* If the next token is a `;' or a `,' then there is no
19368 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19369 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19372 /* Update the DECL_SPECS to reflect the storage class indicated by
19376 cp_parser_set_storage_class (cp_parser *parser,
19377 cp_decl_specifier_seq *decl_specs,
19379 location_t location)
19381 cp_storage_class storage_class;
19383 if (parser->in_unbraced_linkage_specification_p)
19385 error_at (location, "invalid use of %qD in linkage specification",
19386 ridpointers[keyword]);
19389 else if (decl_specs->storage_class != sc_none)
19391 decl_specs->conflicting_specifiers_p = true;
19395 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19396 && decl_specs->specs[(int) ds_thread])
19398 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19399 decl_specs->specs[(int) ds_thread] = 0;
19405 storage_class = sc_auto;
19408 storage_class = sc_register;
19411 storage_class = sc_static;
19414 storage_class = sc_extern;
19417 storage_class = sc_mutable;
19420 gcc_unreachable ();
19422 decl_specs->storage_class = storage_class;
19424 /* A storage class specifier cannot be applied alongside a typedef
19425 specifier. If there is a typedef specifier present then set
19426 conflicting_specifiers_p which will trigger an error later
19427 on in grokdeclarator. */
19428 if (decl_specs->specs[(int)ds_typedef])
19429 decl_specs->conflicting_specifiers_p = true;
19432 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19433 is true, the type is a user-defined type; otherwise it is a
19434 built-in type specified by a keyword. */
19437 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19439 location_t location,
19440 bool user_defined_p)
19442 decl_specs->any_specifiers_p = true;
19444 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19445 (with, for example, in "typedef int wchar_t;") we remember that
19446 this is what happened. In system headers, we ignore these
19447 declarations so that G++ can work with system headers that are not
19449 if (decl_specs->specs[(int) ds_typedef]
19451 && (type_spec == boolean_type_node
19452 || type_spec == char16_type_node
19453 || type_spec == char32_type_node
19454 || type_spec == wchar_type_node)
19455 && (decl_specs->type
19456 || decl_specs->specs[(int) ds_long]
19457 || decl_specs->specs[(int) ds_short]
19458 || decl_specs->specs[(int) ds_unsigned]
19459 || decl_specs->specs[(int) ds_signed]))
19461 decl_specs->redefined_builtin_type = type_spec;
19462 if (!decl_specs->type)
19464 decl_specs->type = type_spec;
19465 decl_specs->user_defined_type_p = false;
19466 decl_specs->type_location = location;
19469 else if (decl_specs->type)
19470 decl_specs->multiple_types_p = true;
19473 decl_specs->type = type_spec;
19474 decl_specs->user_defined_type_p = user_defined_p;
19475 decl_specs->redefined_builtin_type = NULL_TREE;
19476 decl_specs->type_location = location;
19480 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19481 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19484 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19486 return decl_specifiers->specs[(int) ds_friend] != 0;
19489 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19490 issue an error message indicating that TOKEN_DESC was expected.
19492 Returns the token consumed, if the token had the appropriate type.
19493 Otherwise, returns NULL. */
19496 cp_parser_require (cp_parser* parser,
19497 enum cpp_ttype type,
19498 const char* token_desc)
19500 if (cp_lexer_next_token_is (parser->lexer, type))
19501 return cp_lexer_consume_token (parser->lexer);
19504 /* Output the MESSAGE -- unless we're parsing tentatively. */
19505 if (!cp_parser_simulate_error (parser))
19507 char *message = concat ("expected ", token_desc, NULL);
19508 cp_parser_error (parser, message);
19515 /* An error message is produced if the next token is not '>'.
19516 All further tokens are skipped until the desired token is
19517 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19520 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19522 /* Current level of '< ... >'. */
19523 unsigned level = 0;
19524 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19525 unsigned nesting_depth = 0;
19527 /* Are we ready, yet? If not, issue error message. */
19528 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19531 /* Skip tokens until the desired token is found. */
19534 /* Peek at the next token. */
19535 switch (cp_lexer_peek_token (parser->lexer)->type)
19538 if (!nesting_depth)
19543 if (cxx_dialect == cxx98)
19544 /* C++0x views the `>>' operator as two `>' tokens, but
19547 else if (!nesting_depth && level-- == 0)
19549 /* We've hit a `>>' where the first `>' closes the
19550 template argument list, and the second `>' is
19551 spurious. Just consume the `>>' and stop; we've
19552 already produced at least one error. */
19553 cp_lexer_consume_token (parser->lexer);
19556 /* Fall through for C++0x, so we handle the second `>' in
19560 if (!nesting_depth && level-- == 0)
19562 /* We've reached the token we want, consume it and stop. */
19563 cp_lexer_consume_token (parser->lexer);
19568 case CPP_OPEN_PAREN:
19569 case CPP_OPEN_SQUARE:
19573 case CPP_CLOSE_PAREN:
19574 case CPP_CLOSE_SQUARE:
19575 if (nesting_depth-- == 0)
19580 case CPP_PRAGMA_EOL:
19581 case CPP_SEMICOLON:
19582 case CPP_OPEN_BRACE:
19583 case CPP_CLOSE_BRACE:
19584 /* The '>' was probably forgotten, don't look further. */
19591 /* Consume this token. */
19592 cp_lexer_consume_token (parser->lexer);
19596 /* If the next token is the indicated keyword, consume it. Otherwise,
19597 issue an error message indicating that TOKEN_DESC was expected.
19599 Returns the token consumed, if the token had the appropriate type.
19600 Otherwise, returns NULL. */
19603 cp_parser_require_keyword (cp_parser* parser,
19605 const char* token_desc)
19607 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19609 if (token && token->keyword != keyword)
19611 dyn_string_t error_msg;
19613 /* Format the error message. */
19614 error_msg = dyn_string_new (0);
19615 dyn_string_append_cstr (error_msg, "expected ");
19616 dyn_string_append_cstr (error_msg, token_desc);
19617 cp_parser_error (parser, error_msg->s);
19618 dyn_string_delete (error_msg);
19625 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19626 function-definition. */
19629 cp_parser_token_starts_function_definition_p (cp_token* token)
19631 return (/* An ordinary function-body begins with an `{'. */
19632 token->type == CPP_OPEN_BRACE
19633 /* A ctor-initializer begins with a `:'. */
19634 || token->type == CPP_COLON
19635 /* A function-try-block begins with `try'. */
19636 || token->keyword == RID_TRY
19637 /* The named return value extension begins with `return'. */
19638 || token->keyword == RID_RETURN);
19641 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19645 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19649 token = cp_lexer_peek_token (parser->lexer);
19650 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19653 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19654 C++0x) ending a template-argument. */
19657 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19661 token = cp_lexer_peek_token (parser->lexer);
19662 return (token->type == CPP_COMMA
19663 || token->type == CPP_GREATER
19664 || token->type == CPP_ELLIPSIS
19665 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19668 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19669 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19672 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19677 token = cp_lexer_peek_nth_token (parser->lexer, n);
19678 if (token->type == CPP_LESS)
19680 /* Check for the sequence `<::' in the original code. It would be lexed as
19681 `[:', where `[' is a digraph, and there is no whitespace before
19683 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19686 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19687 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19693 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19694 or none_type otherwise. */
19696 static enum tag_types
19697 cp_parser_token_is_class_key (cp_token* token)
19699 switch (token->keyword)
19704 return record_type;
19713 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19716 cp_parser_check_class_key (enum tag_types class_key, tree type)
19718 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19719 permerror (input_location, "%qs tag used in naming %q#T",
19720 class_key == union_type ? "union"
19721 : class_key == record_type ? "struct" : "class",
19725 /* Issue an error message if DECL is redeclared with different
19726 access than its original declaration [class.access.spec/3].
19727 This applies to nested classes and nested class templates.
19731 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19733 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19736 if ((TREE_PRIVATE (decl)
19737 != (current_access_specifier == access_private_node))
19738 || (TREE_PROTECTED (decl)
19739 != (current_access_specifier == access_protected_node)))
19740 error_at (location, "%qD redeclared with different access", decl);
19743 /* Look for the `template' keyword, as a syntactic disambiguator.
19744 Return TRUE iff it is present, in which case it will be
19748 cp_parser_optional_template_keyword (cp_parser *parser)
19750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19752 /* The `template' keyword can only be used within templates;
19753 outside templates the parser can always figure out what is a
19754 template and what is not. */
19755 if (!processing_template_decl)
19757 cp_token *token = cp_lexer_peek_token (parser->lexer);
19758 error_at (token->location,
19759 "%<template%> (as a disambiguator) is only allowed "
19760 "within templates");
19761 /* If this part of the token stream is rescanned, the same
19762 error message would be generated. So, we purge the token
19763 from the stream. */
19764 cp_lexer_purge_token (parser->lexer);
19769 /* Consume the `template' keyword. */
19770 cp_lexer_consume_token (parser->lexer);
19778 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19779 set PARSER->SCOPE, and perform other related actions. */
19782 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19785 struct tree_check *check_value;
19786 deferred_access_check *chk;
19787 VEC (deferred_access_check,gc) *checks;
19789 /* Get the stored value. */
19790 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19791 /* Perform any access checks that were deferred. */
19792 checks = check_value->checks;
19796 VEC_iterate (deferred_access_check, checks, i, chk) ;
19799 perform_or_defer_access_check (chk->binfo,
19804 /* Set the scope from the stored value. */
19805 parser->scope = check_value->value;
19806 parser->qualifying_scope = check_value->qualifying_scope;
19807 parser->object_scope = NULL_TREE;
19810 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19811 encounter the end of a block before what we were looking for. */
19814 cp_parser_cache_group (cp_parser *parser,
19815 enum cpp_ttype end,
19820 cp_token *token = cp_lexer_peek_token (parser->lexer);
19822 /* Abort a parenthesized expression if we encounter a semicolon. */
19823 if ((end == CPP_CLOSE_PAREN || depth == 0)
19824 && token->type == CPP_SEMICOLON)
19826 /* If we've reached the end of the file, stop. */
19827 if (token->type == CPP_EOF
19828 || (end != CPP_PRAGMA_EOL
19829 && token->type == CPP_PRAGMA_EOL))
19831 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19832 /* We've hit the end of an enclosing block, so there's been some
19833 kind of syntax error. */
19836 /* Consume the token. */
19837 cp_lexer_consume_token (parser->lexer);
19838 /* See if it starts a new group. */
19839 if (token->type == CPP_OPEN_BRACE)
19841 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19842 /* In theory this should probably check end == '}', but
19843 cp_parser_save_member_function_body needs it to exit
19844 after either '}' or ')' when called with ')'. */
19848 else if (token->type == CPP_OPEN_PAREN)
19850 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19851 if (depth == 0 && end == CPP_CLOSE_PAREN)
19854 else if (token->type == CPP_PRAGMA)
19855 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19856 else if (token->type == end)
19861 /* Begin parsing tentatively. We always save tokens while parsing
19862 tentatively so that if the tentative parsing fails we can restore the
19866 cp_parser_parse_tentatively (cp_parser* parser)
19868 /* Enter a new parsing context. */
19869 parser->context = cp_parser_context_new (parser->context);
19870 /* Begin saving tokens. */
19871 cp_lexer_save_tokens (parser->lexer);
19872 /* In order to avoid repetitive access control error messages,
19873 access checks are queued up until we are no longer parsing
19875 push_deferring_access_checks (dk_deferred);
19878 /* Commit to the currently active tentative parse. */
19881 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19883 cp_parser_context *context;
19886 /* Mark all of the levels as committed. */
19887 lexer = parser->lexer;
19888 for (context = parser->context; context->next; context = context->next)
19890 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19892 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19893 while (!cp_lexer_saving_tokens (lexer))
19894 lexer = lexer->next;
19895 cp_lexer_commit_tokens (lexer);
19899 /* Abort the currently active tentative parse. All consumed tokens
19900 will be rolled back, and no diagnostics will be issued. */
19903 cp_parser_abort_tentative_parse (cp_parser* parser)
19905 cp_parser_simulate_error (parser);
19906 /* Now, pretend that we want to see if the construct was
19907 successfully parsed. */
19908 cp_parser_parse_definitely (parser);
19911 /* Stop parsing tentatively. If a parse error has occurred, restore the
19912 token stream. Otherwise, commit to the tokens we have consumed.
19913 Returns true if no error occurred; false otherwise. */
19916 cp_parser_parse_definitely (cp_parser* parser)
19918 bool error_occurred;
19919 cp_parser_context *context;
19921 /* Remember whether or not an error occurred, since we are about to
19922 destroy that information. */
19923 error_occurred = cp_parser_error_occurred (parser);
19924 /* Remove the topmost context from the stack. */
19925 context = parser->context;
19926 parser->context = context->next;
19927 /* If no parse errors occurred, commit to the tentative parse. */
19928 if (!error_occurred)
19930 /* Commit to the tokens read tentatively, unless that was
19932 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19933 cp_lexer_commit_tokens (parser->lexer);
19935 pop_to_parent_deferring_access_checks ();
19937 /* Otherwise, if errors occurred, roll back our state so that things
19938 are just as they were before we began the tentative parse. */
19941 cp_lexer_rollback_tokens (parser->lexer);
19942 pop_deferring_access_checks ();
19944 /* Add the context to the front of the free list. */
19945 context->next = cp_parser_context_free_list;
19946 cp_parser_context_free_list = context;
19948 return !error_occurred;
19951 /* Returns true if we are parsing tentatively and are not committed to
19952 this tentative parse. */
19955 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19957 return (cp_parser_parsing_tentatively (parser)
19958 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19961 /* Returns nonzero iff an error has occurred during the most recent
19962 tentative parse. */
19965 cp_parser_error_occurred (cp_parser* parser)
19967 return (cp_parser_parsing_tentatively (parser)
19968 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19971 /* Returns nonzero if GNU extensions are allowed. */
19974 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19976 return parser->allow_gnu_extensions_p;
19979 /* Objective-C++ Productions */
19982 /* Parse an Objective-C expression, which feeds into a primary-expression
19986 objc-message-expression
19987 objc-string-literal
19988 objc-encode-expression
19989 objc-protocol-expression
19990 objc-selector-expression
19992 Returns a tree representation of the expression. */
19995 cp_parser_objc_expression (cp_parser* parser)
19997 /* Try to figure out what kind of declaration is present. */
19998 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20002 case CPP_OPEN_SQUARE:
20003 return cp_parser_objc_message_expression (parser);
20005 case CPP_OBJC_STRING:
20006 kwd = cp_lexer_consume_token (parser->lexer);
20007 return objc_build_string_object (kwd->u.value);
20010 switch (kwd->keyword)
20012 case RID_AT_ENCODE:
20013 return cp_parser_objc_encode_expression (parser);
20015 case RID_AT_PROTOCOL:
20016 return cp_parser_objc_protocol_expression (parser);
20018 case RID_AT_SELECTOR:
20019 return cp_parser_objc_selector_expression (parser);
20025 error_at (kwd->location,
20026 "misplaced %<@%D%> Objective-C++ construct",
20028 cp_parser_skip_to_end_of_block_or_statement (parser);
20031 return error_mark_node;
20034 /* Parse an Objective-C message expression.
20036 objc-message-expression:
20037 [ objc-message-receiver objc-message-args ]
20039 Returns a representation of an Objective-C message. */
20042 cp_parser_objc_message_expression (cp_parser* parser)
20044 tree receiver, messageargs;
20046 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20047 receiver = cp_parser_objc_message_receiver (parser);
20048 messageargs = cp_parser_objc_message_args (parser);
20049 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20051 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20054 /* Parse an objc-message-receiver.
20056 objc-message-receiver:
20058 simple-type-specifier
20060 Returns a representation of the type or expression. */
20063 cp_parser_objc_message_receiver (cp_parser* parser)
20067 /* An Objective-C message receiver may be either (1) a type
20068 or (2) an expression. */
20069 cp_parser_parse_tentatively (parser);
20070 rcv = cp_parser_expression (parser, false, NULL);
20072 if (cp_parser_parse_definitely (parser))
20075 rcv = cp_parser_simple_type_specifier (parser,
20076 /*decl_specs=*/NULL,
20077 CP_PARSER_FLAGS_NONE);
20079 return objc_get_class_reference (rcv);
20082 /* Parse the arguments and selectors comprising an Objective-C message.
20087 objc-selector-args , objc-comma-args
20089 objc-selector-args:
20090 objc-selector [opt] : assignment-expression
20091 objc-selector-args objc-selector [opt] : assignment-expression
20094 assignment-expression
20095 objc-comma-args , assignment-expression
20097 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20098 selector arguments and TREE_VALUE containing a list of comma
20102 cp_parser_objc_message_args (cp_parser* parser)
20104 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20105 bool maybe_unary_selector_p = true;
20106 cp_token *token = cp_lexer_peek_token (parser->lexer);
20108 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20110 tree selector = NULL_TREE, arg;
20112 if (token->type != CPP_COLON)
20113 selector = cp_parser_objc_selector (parser);
20115 /* Detect if we have a unary selector. */
20116 if (maybe_unary_selector_p
20117 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20118 return build_tree_list (selector, NULL_TREE);
20120 maybe_unary_selector_p = false;
20121 cp_parser_require (parser, CPP_COLON, "%<:%>");
20122 arg = cp_parser_assignment_expression (parser, false, NULL);
20125 = chainon (sel_args,
20126 build_tree_list (selector, arg));
20128 token = cp_lexer_peek_token (parser->lexer);
20131 /* Handle non-selector arguments, if any. */
20132 while (token->type == CPP_COMMA)
20136 cp_lexer_consume_token (parser->lexer);
20137 arg = cp_parser_assignment_expression (parser, false, NULL);
20140 = chainon (addl_args,
20141 build_tree_list (NULL_TREE, arg));
20143 token = cp_lexer_peek_token (parser->lexer);
20146 return build_tree_list (sel_args, addl_args);
20149 /* Parse an Objective-C encode expression.
20151 objc-encode-expression:
20152 @encode objc-typename
20154 Returns an encoded representation of the type argument. */
20157 cp_parser_objc_encode_expression (cp_parser* parser)
20162 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20163 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20164 token = cp_lexer_peek_token (parser->lexer);
20165 type = complete_type (cp_parser_type_id (parser));
20166 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20170 error_at (token->location,
20171 "%<@encode%> must specify a type as an argument");
20172 return error_mark_node;
20175 return objc_build_encode_expr (type);
20178 /* Parse an Objective-C @defs expression. */
20181 cp_parser_objc_defs_expression (cp_parser *parser)
20185 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20186 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20187 name = cp_parser_identifier (parser);
20188 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20190 return objc_get_class_ivars (name);
20193 /* Parse an Objective-C protocol expression.
20195 objc-protocol-expression:
20196 @protocol ( identifier )
20198 Returns a representation of the protocol expression. */
20201 cp_parser_objc_protocol_expression (cp_parser* parser)
20205 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20206 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20207 proto = cp_parser_identifier (parser);
20208 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20210 return objc_build_protocol_expr (proto);
20213 /* Parse an Objective-C selector expression.
20215 objc-selector-expression:
20216 @selector ( objc-method-signature )
20218 objc-method-signature:
20224 objc-selector-seq objc-selector :
20226 Returns a representation of the method selector. */
20229 cp_parser_objc_selector_expression (cp_parser* parser)
20231 tree sel_seq = NULL_TREE;
20232 bool maybe_unary_selector_p = true;
20234 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20236 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20237 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20238 token = cp_lexer_peek_token (parser->lexer);
20240 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20241 || token->type == CPP_SCOPE)
20243 tree selector = NULL_TREE;
20245 if (token->type != CPP_COLON
20246 || token->type == CPP_SCOPE)
20247 selector = cp_parser_objc_selector (parser);
20249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20250 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20252 /* Detect if we have a unary selector. */
20253 if (maybe_unary_selector_p)
20255 sel_seq = selector;
20256 goto finish_selector;
20260 cp_parser_error (parser, "expected %<:%>");
20263 maybe_unary_selector_p = false;
20264 token = cp_lexer_consume_token (parser->lexer);
20266 if (token->type == CPP_SCOPE)
20269 = chainon (sel_seq,
20270 build_tree_list (selector, NULL_TREE));
20272 = chainon (sel_seq,
20273 build_tree_list (NULL_TREE, NULL_TREE));
20277 = chainon (sel_seq,
20278 build_tree_list (selector, NULL_TREE));
20280 token = cp_lexer_peek_token (parser->lexer);
20284 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20286 return objc_build_selector_expr (loc, sel_seq);
20289 /* Parse a list of identifiers.
20291 objc-identifier-list:
20293 objc-identifier-list , identifier
20295 Returns a TREE_LIST of identifier nodes. */
20298 cp_parser_objc_identifier_list (cp_parser* parser)
20300 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20301 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20303 while (sep->type == CPP_COMMA)
20305 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20306 list = chainon (list,
20307 build_tree_list (NULL_TREE,
20308 cp_parser_identifier (parser)));
20309 sep = cp_lexer_peek_token (parser->lexer);
20315 /* Parse an Objective-C alias declaration.
20317 objc-alias-declaration:
20318 @compatibility_alias identifier identifier ;
20320 This function registers the alias mapping with the Objective-C front end.
20321 It returns nothing. */
20324 cp_parser_objc_alias_declaration (cp_parser* parser)
20328 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20329 alias = cp_parser_identifier (parser);
20330 orig = cp_parser_identifier (parser);
20331 objc_declare_alias (alias, orig);
20332 cp_parser_consume_semicolon_at_end_of_statement (parser);
20335 /* Parse an Objective-C class forward-declaration.
20337 objc-class-declaration:
20338 @class objc-identifier-list ;
20340 The function registers the forward declarations with the Objective-C
20341 front end. It returns nothing. */
20344 cp_parser_objc_class_declaration (cp_parser* parser)
20346 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20347 objc_declare_class (cp_parser_objc_identifier_list (parser));
20348 cp_parser_consume_semicolon_at_end_of_statement (parser);
20351 /* Parse a list of Objective-C protocol references.
20353 objc-protocol-refs-opt:
20354 objc-protocol-refs [opt]
20356 objc-protocol-refs:
20357 < objc-identifier-list >
20359 Returns a TREE_LIST of identifiers, if any. */
20362 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20364 tree protorefs = NULL_TREE;
20366 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20368 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20369 protorefs = cp_parser_objc_identifier_list (parser);
20370 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20376 /* Parse a Objective-C visibility specification. */
20379 cp_parser_objc_visibility_spec (cp_parser* parser)
20381 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20383 switch (vis->keyword)
20385 case RID_AT_PRIVATE:
20386 objc_set_visibility (2);
20388 case RID_AT_PROTECTED:
20389 objc_set_visibility (0);
20391 case RID_AT_PUBLIC:
20392 objc_set_visibility (1);
20398 /* Eat '@private'/'@protected'/'@public'. */
20399 cp_lexer_consume_token (parser->lexer);
20402 /* Parse an Objective-C method type. */
20405 cp_parser_objc_method_type (cp_parser* parser)
20407 objc_set_method_type
20408 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20413 /* Parse an Objective-C protocol qualifier. */
20416 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20418 tree quals = NULL_TREE, node;
20419 cp_token *token = cp_lexer_peek_token (parser->lexer);
20421 node = token->u.value;
20423 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20424 && (node == ridpointers [(int) RID_IN]
20425 || node == ridpointers [(int) RID_OUT]
20426 || node == ridpointers [(int) RID_INOUT]
20427 || node == ridpointers [(int) RID_BYCOPY]
20428 || node == ridpointers [(int) RID_BYREF]
20429 || node == ridpointers [(int) RID_ONEWAY]))
20431 quals = tree_cons (NULL_TREE, node, quals);
20432 cp_lexer_consume_token (parser->lexer);
20433 token = cp_lexer_peek_token (parser->lexer);
20434 node = token->u.value;
20440 /* Parse an Objective-C typename. */
20443 cp_parser_objc_typename (cp_parser* parser)
20445 tree type_name = NULL_TREE;
20447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20449 tree proto_quals, cp_type = NULL_TREE;
20451 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20452 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20454 /* An ObjC type name may consist of just protocol qualifiers, in which
20455 case the type shall default to 'id'. */
20456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20457 cp_type = cp_parser_type_id (parser);
20459 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20460 type_name = build_tree_list (proto_quals, cp_type);
20466 /* Check to see if TYPE refers to an Objective-C selector name. */
20469 cp_parser_objc_selector_p (enum cpp_ttype type)
20471 return (type == CPP_NAME || type == CPP_KEYWORD
20472 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20473 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20474 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20475 || type == CPP_XOR || type == CPP_XOR_EQ);
20478 /* Parse an Objective-C selector. */
20481 cp_parser_objc_selector (cp_parser* parser)
20483 cp_token *token = cp_lexer_consume_token (parser->lexer);
20485 if (!cp_parser_objc_selector_p (token->type))
20487 error_at (token->location, "invalid Objective-C++ selector name");
20488 return error_mark_node;
20491 /* C++ operator names are allowed to appear in ObjC selectors. */
20492 switch (token->type)
20494 case CPP_AND_AND: return get_identifier ("and");
20495 case CPP_AND_EQ: return get_identifier ("and_eq");
20496 case CPP_AND: return get_identifier ("bitand");
20497 case CPP_OR: return get_identifier ("bitor");
20498 case CPP_COMPL: return get_identifier ("compl");
20499 case CPP_NOT: return get_identifier ("not");
20500 case CPP_NOT_EQ: return get_identifier ("not_eq");
20501 case CPP_OR_OR: return get_identifier ("or");
20502 case CPP_OR_EQ: return get_identifier ("or_eq");
20503 case CPP_XOR: return get_identifier ("xor");
20504 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20505 default: return token->u.value;
20509 /* Parse an Objective-C params list. */
20512 cp_parser_objc_method_keyword_params (cp_parser* parser)
20514 tree params = NULL_TREE;
20515 bool maybe_unary_selector_p = true;
20516 cp_token *token = cp_lexer_peek_token (parser->lexer);
20518 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20520 tree selector = NULL_TREE, type_name, identifier;
20522 if (token->type != CPP_COLON)
20523 selector = cp_parser_objc_selector (parser);
20525 /* Detect if we have a unary selector. */
20526 if (maybe_unary_selector_p
20527 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20530 maybe_unary_selector_p = false;
20531 cp_parser_require (parser, CPP_COLON, "%<:%>");
20532 type_name = cp_parser_objc_typename (parser);
20533 identifier = cp_parser_identifier (parser);
20537 objc_build_keyword_decl (selector,
20541 token = cp_lexer_peek_token (parser->lexer);
20547 /* Parse the non-keyword Objective-C params. */
20550 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20552 tree params = make_node (TREE_LIST);
20553 cp_token *token = cp_lexer_peek_token (parser->lexer);
20554 *ellipsisp = false; /* Initially, assume no ellipsis. */
20556 while (token->type == CPP_COMMA)
20558 cp_parameter_declarator *parmdecl;
20561 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20562 token = cp_lexer_peek_token (parser->lexer);
20564 if (token->type == CPP_ELLIPSIS)
20566 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20571 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20572 parm = grokdeclarator (parmdecl->declarator,
20573 &parmdecl->decl_specifiers,
20574 PARM, /*initialized=*/0,
20575 /*attrlist=*/NULL);
20577 chainon (params, build_tree_list (NULL_TREE, parm));
20578 token = cp_lexer_peek_token (parser->lexer);
20584 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20587 cp_parser_objc_interstitial_code (cp_parser* parser)
20589 cp_token *token = cp_lexer_peek_token (parser->lexer);
20591 /* If the next token is `extern' and the following token is a string
20592 literal, then we have a linkage specification. */
20593 if (token->keyword == RID_EXTERN
20594 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20595 cp_parser_linkage_specification (parser);
20596 /* Handle #pragma, if any. */
20597 else if (token->type == CPP_PRAGMA)
20598 cp_parser_pragma (parser, pragma_external);
20599 /* Allow stray semicolons. */
20600 else if (token->type == CPP_SEMICOLON)
20601 cp_lexer_consume_token (parser->lexer);
20602 /* Finally, try to parse a block-declaration, or a function-definition. */
20604 cp_parser_block_declaration (parser, /*statement_p=*/false);
20607 /* Parse a method signature. */
20610 cp_parser_objc_method_signature (cp_parser* parser)
20612 tree rettype, kwdparms, optparms;
20613 bool ellipsis = false;
20615 cp_parser_objc_method_type (parser);
20616 rettype = cp_parser_objc_typename (parser);
20617 kwdparms = cp_parser_objc_method_keyword_params (parser);
20618 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20620 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20623 /* Pars an Objective-C method prototype list. */
20626 cp_parser_objc_method_prototype_list (cp_parser* parser)
20628 cp_token *token = cp_lexer_peek_token (parser->lexer);
20630 while (token->keyword != RID_AT_END)
20632 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20634 objc_add_method_declaration
20635 (cp_parser_objc_method_signature (parser));
20636 cp_parser_consume_semicolon_at_end_of_statement (parser);
20639 /* Allow for interspersed non-ObjC++ code. */
20640 cp_parser_objc_interstitial_code (parser);
20642 token = cp_lexer_peek_token (parser->lexer);
20645 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20646 objc_finish_interface ();
20649 /* Parse an Objective-C method definition list. */
20652 cp_parser_objc_method_definition_list (cp_parser* parser)
20654 cp_token *token = cp_lexer_peek_token (parser->lexer);
20656 while (token->keyword != RID_AT_END)
20660 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20662 push_deferring_access_checks (dk_deferred);
20663 objc_start_method_definition
20664 (cp_parser_objc_method_signature (parser));
20666 /* For historical reasons, we accept an optional semicolon. */
20667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20668 cp_lexer_consume_token (parser->lexer);
20670 perform_deferred_access_checks ();
20671 stop_deferring_access_checks ();
20672 meth = cp_parser_function_definition_after_declarator (parser,
20674 pop_deferring_access_checks ();
20675 objc_finish_method_definition (meth);
20678 /* Allow for interspersed non-ObjC++ code. */
20679 cp_parser_objc_interstitial_code (parser);
20681 token = cp_lexer_peek_token (parser->lexer);
20684 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20685 objc_finish_implementation ();
20688 /* Parse Objective-C ivars. */
20691 cp_parser_objc_class_ivars (cp_parser* parser)
20693 cp_token *token = cp_lexer_peek_token (parser->lexer);
20695 if (token->type != CPP_OPEN_BRACE)
20696 return; /* No ivars specified. */
20698 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20699 token = cp_lexer_peek_token (parser->lexer);
20701 while (token->type != CPP_CLOSE_BRACE)
20703 cp_decl_specifier_seq declspecs;
20704 int decl_class_or_enum_p;
20705 tree prefix_attributes;
20707 cp_parser_objc_visibility_spec (parser);
20709 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20712 cp_parser_decl_specifier_seq (parser,
20713 CP_PARSER_FLAGS_OPTIONAL,
20715 &decl_class_or_enum_p);
20716 prefix_attributes = declspecs.attributes;
20717 declspecs.attributes = NULL_TREE;
20719 /* Keep going until we hit the `;' at the end of the
20721 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20723 tree width = NULL_TREE, attributes, first_attribute, decl;
20724 cp_declarator *declarator = NULL;
20725 int ctor_dtor_or_conv_p;
20727 /* Check for a (possibly unnamed) bitfield declaration. */
20728 token = cp_lexer_peek_token (parser->lexer);
20729 if (token->type == CPP_COLON)
20732 if (token->type == CPP_NAME
20733 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20736 /* Get the name of the bitfield. */
20737 declarator = make_id_declarator (NULL_TREE,
20738 cp_parser_identifier (parser),
20742 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20743 /* Get the width of the bitfield. */
20745 = cp_parser_constant_expression (parser,
20746 /*allow_non_constant=*/false,
20751 /* Parse the declarator. */
20753 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20754 &ctor_dtor_or_conv_p,
20755 /*parenthesized_p=*/NULL,
20756 /*member_p=*/false);
20759 /* Look for attributes that apply to the ivar. */
20760 attributes = cp_parser_attributes_opt (parser);
20761 /* Remember which attributes are prefix attributes and
20763 first_attribute = attributes;
20764 /* Combine the attributes. */
20765 attributes = chainon (prefix_attributes, attributes);
20768 /* Create the bitfield declaration. */
20769 decl = grokbitfield (declarator, &declspecs,
20773 decl = grokfield (declarator, &declspecs,
20774 NULL_TREE, /*init_const_expr_p=*/false,
20775 NULL_TREE, attributes);
20777 /* Add the instance variable. */
20778 objc_add_instance_variable (decl);
20780 /* Reset PREFIX_ATTRIBUTES. */
20781 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20782 attributes = TREE_CHAIN (attributes);
20784 TREE_CHAIN (attributes) = NULL_TREE;
20786 token = cp_lexer_peek_token (parser->lexer);
20788 if (token->type == CPP_COMMA)
20790 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20796 cp_parser_consume_semicolon_at_end_of_statement (parser);
20797 token = cp_lexer_peek_token (parser->lexer);
20800 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20801 /* For historical reasons, we accept an optional semicolon. */
20802 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20803 cp_lexer_consume_token (parser->lexer);
20806 /* Parse an Objective-C protocol declaration. */
20809 cp_parser_objc_protocol_declaration (cp_parser* parser)
20811 tree proto, protorefs;
20814 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20817 tok = cp_lexer_peek_token (parser->lexer);
20818 error_at (tok->location, "identifier expected after %<@protocol%>");
20822 /* See if we have a forward declaration or a definition. */
20823 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20825 /* Try a forward declaration first. */
20826 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20828 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20830 cp_parser_consume_semicolon_at_end_of_statement (parser);
20833 /* Ok, we got a full-fledged definition (or at least should). */
20836 proto = cp_parser_identifier (parser);
20837 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20838 objc_start_protocol (proto, protorefs);
20839 cp_parser_objc_method_prototype_list (parser);
20843 /* Parse an Objective-C superclass or category. */
20846 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20849 cp_token *next = cp_lexer_peek_token (parser->lexer);
20851 *super = *categ = NULL_TREE;
20852 if (next->type == CPP_COLON)
20854 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20855 *super = cp_parser_identifier (parser);
20857 else if (next->type == CPP_OPEN_PAREN)
20859 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20860 *categ = cp_parser_identifier (parser);
20861 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20865 /* Parse an Objective-C class interface. */
20868 cp_parser_objc_class_interface (cp_parser* parser)
20870 tree name, super, categ, protos;
20872 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20873 name = cp_parser_identifier (parser);
20874 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20875 protos = cp_parser_objc_protocol_refs_opt (parser);
20877 /* We have either a class or a category on our hands. */
20879 objc_start_category_interface (name, categ, protos);
20882 objc_start_class_interface (name, super, protos);
20883 /* Handle instance variable declarations, if any. */
20884 cp_parser_objc_class_ivars (parser);
20885 objc_continue_interface ();
20888 cp_parser_objc_method_prototype_list (parser);
20891 /* Parse an Objective-C class implementation. */
20894 cp_parser_objc_class_implementation (cp_parser* parser)
20896 tree name, super, categ;
20898 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20899 name = cp_parser_identifier (parser);
20900 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20902 /* We have either a class or a category on our hands. */
20904 objc_start_category_implementation (name, categ);
20907 objc_start_class_implementation (name, super);
20908 /* Handle instance variable declarations, if any. */
20909 cp_parser_objc_class_ivars (parser);
20910 objc_continue_implementation ();
20913 cp_parser_objc_method_definition_list (parser);
20916 /* Consume the @end token and finish off the implementation. */
20919 cp_parser_objc_end_implementation (cp_parser* parser)
20921 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20922 objc_finish_implementation ();
20925 /* Parse an Objective-C declaration. */
20928 cp_parser_objc_declaration (cp_parser* parser)
20930 /* Try to figure out what kind of declaration is present. */
20931 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20933 switch (kwd->keyword)
20936 cp_parser_objc_alias_declaration (parser);
20939 cp_parser_objc_class_declaration (parser);
20941 case RID_AT_PROTOCOL:
20942 cp_parser_objc_protocol_declaration (parser);
20944 case RID_AT_INTERFACE:
20945 cp_parser_objc_class_interface (parser);
20947 case RID_AT_IMPLEMENTATION:
20948 cp_parser_objc_class_implementation (parser);
20951 cp_parser_objc_end_implementation (parser);
20954 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20956 cp_parser_skip_to_end_of_block_or_statement (parser);
20960 /* Parse an Objective-C try-catch-finally statement.
20962 objc-try-catch-finally-stmt:
20963 @try compound-statement objc-catch-clause-seq [opt]
20964 objc-finally-clause [opt]
20966 objc-catch-clause-seq:
20967 objc-catch-clause objc-catch-clause-seq [opt]
20970 @catch ( exception-declaration ) compound-statement
20972 objc-finally-clause
20973 @finally compound-statement
20975 Returns NULL_TREE. */
20978 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20979 location_t location;
20982 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20983 location = cp_lexer_peek_token (parser->lexer)->location;
20984 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20985 node, lest it get absorbed into the surrounding block. */
20986 stmt = push_stmt_list ();
20987 cp_parser_compound_statement (parser, NULL, false);
20988 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20990 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20992 cp_parameter_declarator *parmdecl;
20995 cp_lexer_consume_token (parser->lexer);
20996 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20997 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20998 parm = grokdeclarator (parmdecl->declarator,
20999 &parmdecl->decl_specifiers,
21000 PARM, /*initialized=*/0,
21001 /*attrlist=*/NULL);
21002 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21003 objc_begin_catch_clause (parm);
21004 cp_parser_compound_statement (parser, NULL, false);
21005 objc_finish_catch_clause ();
21008 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21010 cp_lexer_consume_token (parser->lexer);
21011 location = cp_lexer_peek_token (parser->lexer)->location;
21012 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21013 node, lest it get absorbed into the surrounding block. */
21014 stmt = push_stmt_list ();
21015 cp_parser_compound_statement (parser, NULL, false);
21016 objc_build_finally_clause (location, pop_stmt_list (stmt));
21019 return objc_finish_try_stmt ();
21022 /* Parse an Objective-C synchronized statement.
21024 objc-synchronized-stmt:
21025 @synchronized ( expression ) compound-statement
21027 Returns NULL_TREE. */
21030 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21031 location_t location;
21034 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21036 location = cp_lexer_peek_token (parser->lexer)->location;
21037 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21038 lock = cp_parser_expression (parser, false, NULL);
21039 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21041 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21042 node, lest it get absorbed into the surrounding block. */
21043 stmt = push_stmt_list ();
21044 cp_parser_compound_statement (parser, NULL, false);
21046 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21049 /* Parse an Objective-C throw statement.
21052 @throw assignment-expression [opt] ;
21054 Returns a constructed '@throw' statement. */
21057 cp_parser_objc_throw_statement (cp_parser *parser) {
21058 tree expr = NULL_TREE;
21059 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21061 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21064 expr = cp_parser_assignment_expression (parser, false, NULL);
21066 cp_parser_consume_semicolon_at_end_of_statement (parser);
21068 return objc_build_throw_stmt (loc, expr);
21071 /* Parse an Objective-C statement. */
21074 cp_parser_objc_statement (cp_parser * parser) {
21075 /* Try to figure out what kind of declaration is present. */
21076 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21078 switch (kwd->keyword)
21081 return cp_parser_objc_try_catch_finally_statement (parser);
21082 case RID_AT_SYNCHRONIZED:
21083 return cp_parser_objc_synchronized_statement (parser);
21085 return cp_parser_objc_throw_statement (parser);
21087 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21089 cp_parser_skip_to_end_of_block_or_statement (parser);
21092 return error_mark_node;
21095 /* OpenMP 2.5 parsing routines. */
21097 /* Returns name of the next clause.
21098 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21099 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21100 returned and the token is consumed. */
21102 static pragma_omp_clause
21103 cp_parser_omp_clause_name (cp_parser *parser)
21105 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21108 result = PRAGMA_OMP_CLAUSE_IF;
21109 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21110 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21111 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21112 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21113 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21116 const char *p = IDENTIFIER_POINTER (id);
21121 if (!strcmp ("collapse", p))
21122 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21123 else if (!strcmp ("copyin", p))
21124 result = PRAGMA_OMP_CLAUSE_COPYIN;
21125 else if (!strcmp ("copyprivate", p))
21126 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21129 if (!strcmp ("firstprivate", p))
21130 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21133 if (!strcmp ("lastprivate", p))
21134 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21137 if (!strcmp ("nowait", p))
21138 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21139 else if (!strcmp ("num_threads", p))
21140 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21143 if (!strcmp ("ordered", p))
21144 result = PRAGMA_OMP_CLAUSE_ORDERED;
21147 if (!strcmp ("reduction", p))
21148 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21151 if (!strcmp ("schedule", p))
21152 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21153 else if (!strcmp ("shared", p))
21154 result = PRAGMA_OMP_CLAUSE_SHARED;
21157 if (!strcmp ("untied", p))
21158 result = PRAGMA_OMP_CLAUSE_UNTIED;
21163 if (result != PRAGMA_OMP_CLAUSE_NONE)
21164 cp_lexer_consume_token (parser->lexer);
21169 /* Validate that a clause of the given type does not already exist. */
21172 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21173 const char *name, location_t location)
21177 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21178 if (OMP_CLAUSE_CODE (c) == code)
21180 error_at (location, "too many %qs clauses", name);
21188 variable-list , identifier
21190 In addition, we match a closing parenthesis. An opening parenthesis
21191 will have been consumed by the caller.
21193 If KIND is nonzero, create the appropriate node and install the decl
21194 in OMP_CLAUSE_DECL and add the node to the head of the list.
21196 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21197 return the list created. */
21200 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21208 token = cp_lexer_peek_token (parser->lexer);
21209 name = cp_parser_id_expression (parser, /*template_p=*/false,
21210 /*check_dependency_p=*/true,
21211 /*template_p=*/NULL,
21212 /*declarator_p=*/false,
21213 /*optional_p=*/false);
21214 if (name == error_mark_node)
21217 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21218 if (decl == error_mark_node)
21219 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21220 else if (kind != 0)
21222 tree u = build_omp_clause (token->location, kind);
21223 OMP_CLAUSE_DECL (u) = decl;
21224 OMP_CLAUSE_CHAIN (u) = list;
21228 list = tree_cons (decl, NULL_TREE, list);
21231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21233 cp_lexer_consume_token (parser->lexer);
21236 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21240 /* Try to resync to an unnested comma. Copied from
21241 cp_parser_parenthesized_expression_list. */
21243 ending = cp_parser_skip_to_closing_parenthesis (parser,
21244 /*recovering=*/true,
21246 /*consume_paren=*/true);
21254 /* Similarly, but expect leading and trailing parenthesis. This is a very
21255 common case for omp clauses. */
21258 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21260 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21261 return cp_parser_omp_var_list_no_open (parser, kind, list);
21266 collapse ( constant-expression ) */
21269 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21275 loc = cp_lexer_peek_token (parser->lexer)->location;
21276 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21279 num = cp_parser_constant_expression (parser, false, NULL);
21281 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21282 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21283 /*or_comma=*/false,
21284 /*consume_paren=*/true);
21286 if (num == error_mark_node)
21288 num = fold_non_dependent_expr (num);
21289 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21290 || !host_integerp (num, 0)
21291 || (n = tree_low_cst (num, 0)) <= 0
21294 error_at (loc, "collapse argument needs positive constant integer expression");
21298 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21299 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21300 OMP_CLAUSE_CHAIN (c) = list;
21301 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21307 default ( shared | none ) */
21310 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21312 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21315 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21317 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21319 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21320 const char *p = IDENTIFIER_POINTER (id);
21325 if (strcmp ("none", p) != 0)
21327 kind = OMP_CLAUSE_DEFAULT_NONE;
21331 if (strcmp ("shared", p) != 0)
21333 kind = OMP_CLAUSE_DEFAULT_SHARED;
21340 cp_lexer_consume_token (parser->lexer);
21345 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21348 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21349 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21350 /*or_comma=*/false,
21351 /*consume_paren=*/true);
21353 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21356 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21357 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21358 OMP_CLAUSE_CHAIN (c) = list;
21359 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21365 if ( expression ) */
21368 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21372 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21375 t = cp_parser_condition (parser);
21377 if (t == error_mark_node
21378 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21379 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21380 /*or_comma=*/false,
21381 /*consume_paren=*/true);
21383 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21385 c = build_omp_clause (location, OMP_CLAUSE_IF);
21386 OMP_CLAUSE_IF_EXPR (c) = t;
21387 OMP_CLAUSE_CHAIN (c) = list;
21396 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21397 tree list, location_t location)
21401 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21403 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21404 OMP_CLAUSE_CHAIN (c) = list;
21409 num_threads ( expression ) */
21412 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21413 location_t location)
21417 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21420 t = cp_parser_expression (parser, false, NULL);
21422 if (t == error_mark_node
21423 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21425 /*or_comma=*/false,
21426 /*consume_paren=*/true);
21428 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21429 "num_threads", location);
21431 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21432 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21433 OMP_CLAUSE_CHAIN (c) = list;
21442 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21443 tree list, location_t location)
21447 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21448 "ordered", location);
21450 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21451 OMP_CLAUSE_CHAIN (c) = list;
21456 reduction ( reduction-operator : variable-list )
21458 reduction-operator:
21459 One of: + * - & ^ | && || */
21462 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21464 enum tree_code code;
21467 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21470 switch (cp_lexer_peek_token (parser->lexer)->type)
21482 code = BIT_AND_EXPR;
21485 code = BIT_XOR_EXPR;
21488 code = BIT_IOR_EXPR;
21491 code = TRUTH_ANDIF_EXPR;
21494 code = TRUTH_ORIF_EXPR;
21497 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21498 "%<|%>, %<&&%>, or %<||%>");
21500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21501 /*or_comma=*/false,
21502 /*consume_paren=*/true);
21505 cp_lexer_consume_token (parser->lexer);
21507 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21510 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21511 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21512 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21518 schedule ( schedule-kind )
21519 schedule ( schedule-kind , expression )
21522 static | dynamic | guided | runtime | auto */
21525 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21529 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21532 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21534 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21536 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21537 const char *p = IDENTIFIER_POINTER (id);
21542 if (strcmp ("dynamic", p) != 0)
21544 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21548 if (strcmp ("guided", p) != 0)
21550 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21554 if (strcmp ("runtime", p) != 0)
21556 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21563 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21564 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21565 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21566 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21569 cp_lexer_consume_token (parser->lexer);
21571 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21574 cp_lexer_consume_token (parser->lexer);
21576 token = cp_lexer_peek_token (parser->lexer);
21577 t = cp_parser_assignment_expression (parser, false, NULL);
21579 if (t == error_mark_node)
21581 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21582 error_at (token->location, "schedule %<runtime%> does not take "
21583 "a %<chunk_size%> parameter");
21584 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21585 error_at (token->location, "schedule %<auto%> does not take "
21586 "a %<chunk_size%> parameter");
21588 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21590 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21593 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21596 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21597 OMP_CLAUSE_CHAIN (c) = list;
21601 cp_parser_error (parser, "invalid schedule kind");
21603 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21604 /*or_comma=*/false,
21605 /*consume_paren=*/true);
21613 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21614 tree list, location_t location)
21618 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21620 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21621 OMP_CLAUSE_CHAIN (c) = list;
21625 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21626 is a bitmask in MASK. Return the list of clauses found; the result
21627 of clause default goes in *pdefault. */
21630 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21631 const char *where, cp_token *pragma_tok)
21633 tree clauses = NULL;
21635 cp_token *token = NULL;
21637 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21639 pragma_omp_clause c_kind;
21640 const char *c_name;
21641 tree prev = clauses;
21643 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21644 cp_lexer_consume_token (parser->lexer);
21646 token = cp_lexer_peek_token (parser->lexer);
21647 c_kind = cp_parser_omp_clause_name (parser);
21652 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21653 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21655 c_name = "collapse";
21657 case PRAGMA_OMP_CLAUSE_COPYIN:
21658 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21661 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21662 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21664 c_name = "copyprivate";
21666 case PRAGMA_OMP_CLAUSE_DEFAULT:
21667 clauses = cp_parser_omp_clause_default (parser, clauses,
21669 c_name = "default";
21671 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21674 c_name = "firstprivate";
21676 case PRAGMA_OMP_CLAUSE_IF:
21677 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21680 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21681 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21683 c_name = "lastprivate";
21685 case PRAGMA_OMP_CLAUSE_NOWAIT:
21686 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21689 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21690 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21692 c_name = "num_threads";
21694 case PRAGMA_OMP_CLAUSE_ORDERED:
21695 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21697 c_name = "ordered";
21699 case PRAGMA_OMP_CLAUSE_PRIVATE:
21700 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21702 c_name = "private";
21704 case PRAGMA_OMP_CLAUSE_REDUCTION:
21705 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21706 c_name = "reduction";
21708 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21709 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21711 c_name = "schedule";
21713 case PRAGMA_OMP_CLAUSE_SHARED:
21714 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21718 case PRAGMA_OMP_CLAUSE_UNTIED:
21719 clauses = cp_parser_omp_clause_untied (parser, clauses,
21724 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21728 if (((mask >> c_kind) & 1) == 0)
21730 /* Remove the invalid clause(s) from the list to avoid
21731 confusing the rest of the compiler. */
21733 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21737 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21738 return finish_omp_clauses (clauses);
21745 In practice, we're also interested in adding the statement to an
21746 outer node. So it is convenient if we work around the fact that
21747 cp_parser_statement calls add_stmt. */
21750 cp_parser_begin_omp_structured_block (cp_parser *parser)
21752 unsigned save = parser->in_statement;
21754 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21755 This preserves the "not within loop or switch" style error messages
21756 for nonsense cases like
21762 if (parser->in_statement)
21763 parser->in_statement = IN_OMP_BLOCK;
21769 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21771 parser->in_statement = save;
21775 cp_parser_omp_structured_block (cp_parser *parser)
21777 tree stmt = begin_omp_structured_block ();
21778 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21780 cp_parser_statement (parser, NULL_TREE, false, NULL);
21782 cp_parser_end_omp_structured_block (parser, save);
21783 return finish_omp_structured_block (stmt);
21787 # pragma omp atomic new-line
21791 x binop= expr | x++ | ++x | x-- | --x
21793 +, *, -, /, &, ^, |, <<, >>
21795 where x is an lvalue expression with scalar type. */
21798 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21801 enum tree_code code;
21803 cp_parser_require_pragma_eol (parser, pragma_tok);
21805 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21806 /*cast_p=*/false, NULL);
21807 switch (TREE_CODE (lhs))
21812 case PREINCREMENT_EXPR:
21813 case POSTINCREMENT_EXPR:
21814 lhs = TREE_OPERAND (lhs, 0);
21816 rhs = integer_one_node;
21819 case PREDECREMENT_EXPR:
21820 case POSTDECREMENT_EXPR:
21821 lhs = TREE_OPERAND (lhs, 0);
21823 rhs = integer_one_node;
21827 switch (cp_lexer_peek_token (parser->lexer)->type)
21833 code = TRUNC_DIV_EXPR;
21841 case CPP_LSHIFT_EQ:
21842 code = LSHIFT_EXPR;
21844 case CPP_RSHIFT_EQ:
21845 code = RSHIFT_EXPR;
21848 code = BIT_AND_EXPR;
21851 code = BIT_IOR_EXPR;
21854 code = BIT_XOR_EXPR;
21857 cp_parser_error (parser,
21858 "invalid operator for %<#pragma omp atomic%>");
21861 cp_lexer_consume_token (parser->lexer);
21863 rhs = cp_parser_expression (parser, false, NULL);
21864 if (rhs == error_mark_node)
21868 finish_omp_atomic (code, lhs, rhs);
21869 cp_parser_consume_semicolon_at_end_of_statement (parser);
21873 cp_parser_skip_to_end_of_block_or_statement (parser);
21878 # pragma omp barrier new-line */
21881 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21883 cp_parser_require_pragma_eol (parser, pragma_tok);
21884 finish_omp_barrier ();
21888 # pragma omp critical [(name)] new-line
21889 structured-block */
21892 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21894 tree stmt, name = NULL;
21896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21898 cp_lexer_consume_token (parser->lexer);
21900 name = cp_parser_identifier (parser);
21902 if (name == error_mark_node
21903 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21904 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21905 /*or_comma=*/false,
21906 /*consume_paren=*/true);
21907 if (name == error_mark_node)
21910 cp_parser_require_pragma_eol (parser, pragma_tok);
21912 stmt = cp_parser_omp_structured_block (parser);
21913 return c_finish_omp_critical (input_location, stmt, name);
21917 # pragma omp flush flush-vars[opt] new-line
21920 ( variable-list ) */
21923 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21925 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21926 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21927 cp_parser_require_pragma_eol (parser, pragma_tok);
21929 finish_omp_flush ();
21932 /* Helper function, to parse omp for increment expression. */
21935 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21937 tree cond = cp_parser_binary_expression (parser, false, true,
21938 PREC_NOT_OPERATOR, NULL);
21941 if (cond == error_mark_node
21942 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21944 cp_parser_skip_to_end_of_statement (parser);
21945 return error_mark_node;
21948 switch (TREE_CODE (cond))
21956 return error_mark_node;
21959 /* If decl is an iterator, preserve LHS and RHS of the relational
21960 expr until finish_omp_for. */
21962 && (type_dependent_expression_p (decl)
21963 || CLASS_TYPE_P (TREE_TYPE (decl))))
21966 return build_x_binary_op (TREE_CODE (cond),
21967 TREE_OPERAND (cond, 0), ERROR_MARK,
21968 TREE_OPERAND (cond, 1), ERROR_MARK,
21969 &overloaded_p, tf_warning_or_error);
21972 /* Helper function, to parse omp for increment expression. */
21975 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21977 cp_token *token = cp_lexer_peek_token (parser->lexer);
21983 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21985 op = (token->type == CPP_PLUS_PLUS
21986 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21987 cp_lexer_consume_token (parser->lexer);
21988 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21990 return error_mark_node;
21991 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21994 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21996 return error_mark_node;
21998 token = cp_lexer_peek_token (parser->lexer);
21999 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22001 op = (token->type == CPP_PLUS_PLUS
22002 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22003 cp_lexer_consume_token (parser->lexer);
22004 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22007 op = cp_parser_assignment_operator_opt (parser);
22008 if (op == ERROR_MARK)
22009 return error_mark_node;
22011 if (op != NOP_EXPR)
22013 rhs = cp_parser_assignment_expression (parser, false, NULL);
22014 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22015 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22018 lhs = cp_parser_binary_expression (parser, false, false,
22019 PREC_ADDITIVE_EXPRESSION, NULL);
22020 token = cp_lexer_peek_token (parser->lexer);
22021 decl_first = lhs == decl;
22024 if (token->type != CPP_PLUS
22025 && token->type != CPP_MINUS)
22026 return error_mark_node;
22030 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22031 cp_lexer_consume_token (parser->lexer);
22032 rhs = cp_parser_binary_expression (parser, false, false,
22033 PREC_ADDITIVE_EXPRESSION, NULL);
22034 token = cp_lexer_peek_token (parser->lexer);
22035 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22037 if (lhs == NULL_TREE)
22039 if (op == PLUS_EXPR)
22042 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22045 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22046 NULL, tf_warning_or_error);
22049 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22053 if (rhs != decl || op == MINUS_EXPR)
22054 return error_mark_node;
22055 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22058 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22060 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22063 /* Parse the restricted form of the for statement allowed by OpenMP. */
22066 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22068 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22069 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22070 tree this_pre_body, cl;
22071 location_t loc_first;
22072 bool collapse_err = false;
22073 int i, collapse = 1, nbraces = 0;
22075 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22076 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22077 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22079 gcc_assert (collapse >= 1);
22081 declv = make_tree_vec (collapse);
22082 initv = make_tree_vec (collapse);
22083 condv = make_tree_vec (collapse);
22084 incrv = make_tree_vec (collapse);
22086 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22088 for (i = 0; i < collapse; i++)
22090 int bracecount = 0;
22091 bool add_private_clause = false;
22094 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22096 cp_parser_error (parser, "for statement expected");
22099 loc = cp_lexer_consume_token (parser->lexer)->location;
22101 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22104 init = decl = real_decl = NULL;
22105 this_pre_body = push_stmt_list ();
22106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22108 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22112 integer-type var = lb
22113 random-access-iterator-type var = lb
22114 pointer-type var = lb
22116 cp_decl_specifier_seq type_specifiers;
22118 /* First, try to parse as an initialized declaration. See
22119 cp_parser_condition, from whence the bulk of this is copied. */
22121 cp_parser_parse_tentatively (parser);
22122 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22123 /*is_trailing_return=*/false,
22125 if (cp_parser_parse_definitely (parser))
22127 /* If parsing a type specifier seq succeeded, then this
22128 MUST be a initialized declaration. */
22129 tree asm_specification, attributes;
22130 cp_declarator *declarator;
22132 declarator = cp_parser_declarator (parser,
22133 CP_PARSER_DECLARATOR_NAMED,
22134 /*ctor_dtor_or_conv_p=*/NULL,
22135 /*parenthesized_p=*/NULL,
22136 /*member_p=*/false);
22137 attributes = cp_parser_attributes_opt (parser);
22138 asm_specification = cp_parser_asm_specification_opt (parser);
22140 if (declarator == cp_error_declarator)
22141 cp_parser_skip_to_end_of_statement (parser);
22145 tree pushed_scope, auto_node;
22147 decl = start_decl (declarator, &type_specifiers,
22148 SD_INITIALIZED, attributes,
22149 /*prefix_attributes=*/NULL_TREE,
22152 auto_node = type_uses_auto (TREE_TYPE (decl));
22153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22155 if (cp_lexer_next_token_is (parser->lexer,
22157 error ("parenthesized initialization is not allowed in "
22158 "OpenMP %<for%> loop");
22160 /* Trigger an error. */
22161 cp_parser_require (parser, CPP_EQ, "%<=%>");
22163 init = error_mark_node;
22164 cp_parser_skip_to_end_of_statement (parser);
22166 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22167 || type_dependent_expression_p (decl)
22170 bool is_direct_init, is_non_constant_init;
22172 init = cp_parser_initializer (parser,
22174 &is_non_constant_init);
22176 if (auto_node && describable_type (init))
22179 = do_auto_deduction (TREE_TYPE (decl), init,
22182 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22183 && !type_dependent_expression_p (decl))
22187 cp_finish_decl (decl, init, !is_non_constant_init,
22189 LOOKUP_ONLYCONVERTING);
22190 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22193 = tree_cons (NULL, this_pre_body, for_block);
22197 init = pop_stmt_list (this_pre_body);
22198 this_pre_body = NULL_TREE;
22203 cp_lexer_consume_token (parser->lexer);
22204 init = cp_parser_assignment_expression (parser, false, NULL);
22207 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22208 init = error_mark_node;
22210 cp_finish_decl (decl, NULL_TREE,
22211 /*init_const_expr_p=*/false,
22213 LOOKUP_ONLYCONVERTING);
22217 pop_scope (pushed_scope);
22223 /* If parsing a type specifier sequence failed, then
22224 this MUST be a simple expression. */
22225 cp_parser_parse_tentatively (parser);
22226 decl = cp_parser_primary_expression (parser, false, false,
22228 if (!cp_parser_error_occurred (parser)
22231 && CLASS_TYPE_P (TREE_TYPE (decl)))
22235 cp_parser_parse_definitely (parser);
22236 cp_parser_require (parser, CPP_EQ, "%<=%>");
22237 rhs = cp_parser_assignment_expression (parser, false, NULL);
22238 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22240 tf_warning_or_error));
22241 add_private_clause = true;
22246 cp_parser_abort_tentative_parse (parser);
22247 init = cp_parser_expression (parser, false, NULL);
22250 if (TREE_CODE (init) == MODIFY_EXPR
22251 || TREE_CODE (init) == MODOP_EXPR)
22252 real_decl = TREE_OPERAND (init, 0);
22257 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22260 this_pre_body = pop_stmt_list (this_pre_body);
22264 pre_body = push_stmt_list ();
22266 add_stmt (this_pre_body);
22267 pre_body = pop_stmt_list (pre_body);
22270 pre_body = this_pre_body;
22275 if (par_clauses != NULL && real_decl != NULL_TREE)
22278 for (c = par_clauses; *c ; )
22279 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22280 && OMP_CLAUSE_DECL (*c) == real_decl)
22282 error_at (loc, "iteration variable %qD"
22283 " should not be firstprivate", real_decl);
22284 *c = OMP_CLAUSE_CHAIN (*c);
22286 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22287 && OMP_CLAUSE_DECL (*c) == real_decl)
22289 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22290 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22291 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22292 OMP_CLAUSE_DECL (l) = real_decl;
22293 OMP_CLAUSE_CHAIN (l) = clauses;
22294 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22296 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22297 CP_OMP_CLAUSE_INFO (*c) = NULL;
22298 add_private_clause = false;
22302 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22303 && OMP_CLAUSE_DECL (*c) == real_decl)
22304 add_private_clause = false;
22305 c = &OMP_CLAUSE_CHAIN (*c);
22309 if (add_private_clause)
22312 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22314 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22315 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22316 && OMP_CLAUSE_DECL (c) == decl)
22318 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22319 && OMP_CLAUSE_DECL (c) == decl)
22320 error_at (loc, "iteration variable %qD "
22321 "should not be firstprivate",
22323 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22324 && OMP_CLAUSE_DECL (c) == decl)
22325 error_at (loc, "iteration variable %qD should not be reduction",
22330 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22331 OMP_CLAUSE_DECL (c) = decl;
22332 c = finish_omp_clauses (c);
22335 OMP_CLAUSE_CHAIN (c) = clauses;
22342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22343 cond = cp_parser_omp_for_cond (parser, decl);
22344 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22349 /* If decl is an iterator, preserve the operator on decl
22350 until finish_omp_for. */
22352 && (type_dependent_expression_p (decl)
22353 || CLASS_TYPE_P (TREE_TYPE (decl))))
22354 incr = cp_parser_omp_for_incr (parser, decl);
22356 incr = cp_parser_expression (parser, false, NULL);
22359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22360 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22361 /*or_comma=*/false,
22362 /*consume_paren=*/true);
22364 TREE_VEC_ELT (declv, i) = decl;
22365 TREE_VEC_ELT (initv, i) = init;
22366 TREE_VEC_ELT (condv, i) = cond;
22367 TREE_VEC_ELT (incrv, i) = incr;
22369 if (i == collapse - 1)
22372 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22373 in between the collapsed for loops to be still considered perfectly
22374 nested. Hopefully the final version clarifies this.
22375 For now handle (multiple) {'s and empty statements. */
22376 cp_parser_parse_tentatively (parser);
22379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22381 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22383 cp_lexer_consume_token (parser->lexer);
22386 else if (bracecount
22387 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22388 cp_lexer_consume_token (parser->lexer);
22391 loc = cp_lexer_peek_token (parser->lexer)->location;
22392 error_at (loc, "not enough collapsed for loops");
22393 collapse_err = true;
22394 cp_parser_abort_tentative_parse (parser);
22403 cp_parser_parse_definitely (parser);
22404 nbraces += bracecount;
22408 /* Note that we saved the original contents of this flag when we entered
22409 the structured block, and so we don't need to re-save it here. */
22410 parser->in_statement = IN_OMP_FOR;
22412 /* Note that the grammar doesn't call for a structured block here,
22413 though the loop as a whole is a structured block. */
22414 body = push_stmt_list ();
22415 cp_parser_statement (parser, NULL_TREE, false, NULL);
22416 body = pop_stmt_list (body);
22418 if (declv == NULL_TREE)
22421 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22422 pre_body, clauses);
22426 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22428 cp_lexer_consume_token (parser->lexer);
22431 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22432 cp_lexer_consume_token (parser->lexer);
22437 error_at (cp_lexer_peek_token (parser->lexer)->location,
22438 "collapsed loops not perfectly nested");
22440 collapse_err = true;
22441 cp_parser_statement_seq_opt (parser, NULL);
22442 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22449 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22450 for_block = TREE_CHAIN (for_block);
22457 #pragma omp for for-clause[optseq] new-line
22460 #define OMP_FOR_CLAUSE_MASK \
22461 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22462 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22463 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22464 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22465 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22466 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22467 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22468 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22471 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22473 tree clauses, sb, ret;
22476 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22477 "#pragma omp for", pragma_tok);
22479 sb = begin_omp_structured_block ();
22480 save = cp_parser_begin_omp_structured_block (parser);
22482 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22484 cp_parser_end_omp_structured_block (parser, save);
22485 add_stmt (finish_omp_structured_block (sb));
22491 # pragma omp master new-line
22492 structured-block */
22495 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22497 cp_parser_require_pragma_eol (parser, pragma_tok);
22498 return c_finish_omp_master (input_location,
22499 cp_parser_omp_structured_block (parser));
22503 # pragma omp ordered new-line
22504 structured-block */
22507 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22510 cp_parser_require_pragma_eol (parser, pragma_tok);
22511 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22517 { section-sequence }
22520 section-directive[opt] structured-block
22521 section-sequence section-directive structured-block */
22524 cp_parser_omp_sections_scope (cp_parser *parser)
22526 tree stmt, substmt;
22527 bool error_suppress = false;
22530 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22533 stmt = push_stmt_list ();
22535 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22539 substmt = begin_omp_structured_block ();
22540 save = cp_parser_begin_omp_structured_block (parser);
22544 cp_parser_statement (parser, NULL_TREE, false, NULL);
22546 tok = cp_lexer_peek_token (parser->lexer);
22547 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22549 if (tok->type == CPP_CLOSE_BRACE)
22551 if (tok->type == CPP_EOF)
22555 cp_parser_end_omp_structured_block (parser, save);
22556 substmt = finish_omp_structured_block (substmt);
22557 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22558 add_stmt (substmt);
22563 tok = cp_lexer_peek_token (parser->lexer);
22564 if (tok->type == CPP_CLOSE_BRACE)
22566 if (tok->type == CPP_EOF)
22569 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22571 cp_lexer_consume_token (parser->lexer);
22572 cp_parser_require_pragma_eol (parser, tok);
22573 error_suppress = false;
22575 else if (!error_suppress)
22577 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22578 error_suppress = true;
22581 substmt = cp_parser_omp_structured_block (parser);
22582 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22583 add_stmt (substmt);
22585 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22587 substmt = pop_stmt_list (stmt);
22589 stmt = make_node (OMP_SECTIONS);
22590 TREE_TYPE (stmt) = void_type_node;
22591 OMP_SECTIONS_BODY (stmt) = substmt;
22598 # pragma omp sections sections-clause[optseq] newline
22601 #define OMP_SECTIONS_CLAUSE_MASK \
22602 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22603 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22604 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22605 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22606 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22609 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22613 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22614 "#pragma omp sections", pragma_tok);
22616 ret = cp_parser_omp_sections_scope (parser);
22618 OMP_SECTIONS_CLAUSES (ret) = clauses;
22624 # pragma parallel parallel-clause new-line
22625 # pragma parallel for parallel-for-clause new-line
22626 # pragma parallel sections parallel-sections-clause new-line */
22628 #define OMP_PARALLEL_CLAUSE_MASK \
22629 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22630 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22631 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22632 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22633 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22634 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22635 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22636 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22639 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22641 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22642 const char *p_name = "#pragma omp parallel";
22643 tree stmt, clauses, par_clause, ws_clause, block;
22644 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22646 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22650 cp_lexer_consume_token (parser->lexer);
22651 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22652 p_name = "#pragma omp parallel for";
22653 mask |= OMP_FOR_CLAUSE_MASK;
22654 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22656 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22658 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22659 const char *p = IDENTIFIER_POINTER (id);
22660 if (strcmp (p, "sections") == 0)
22662 cp_lexer_consume_token (parser->lexer);
22663 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22664 p_name = "#pragma omp parallel sections";
22665 mask |= OMP_SECTIONS_CLAUSE_MASK;
22666 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22670 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22671 block = begin_omp_parallel ();
22672 save = cp_parser_begin_omp_structured_block (parser);
22676 case PRAGMA_OMP_PARALLEL:
22677 cp_parser_statement (parser, NULL_TREE, false, NULL);
22678 par_clause = clauses;
22681 case PRAGMA_OMP_PARALLEL_FOR:
22682 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22683 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22686 case PRAGMA_OMP_PARALLEL_SECTIONS:
22687 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22688 stmt = cp_parser_omp_sections_scope (parser);
22690 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22694 gcc_unreachable ();
22697 cp_parser_end_omp_structured_block (parser, save);
22698 stmt = finish_omp_parallel (par_clause, block);
22699 if (p_kind != PRAGMA_OMP_PARALLEL)
22700 OMP_PARALLEL_COMBINED (stmt) = 1;
22705 # pragma omp single single-clause[optseq] new-line
22706 structured-block */
22708 #define OMP_SINGLE_CLAUSE_MASK \
22709 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22710 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22711 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22712 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22715 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22717 tree stmt = make_node (OMP_SINGLE);
22718 TREE_TYPE (stmt) = void_type_node;
22720 OMP_SINGLE_CLAUSES (stmt)
22721 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22722 "#pragma omp single", pragma_tok);
22723 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22725 return add_stmt (stmt);
22729 # pragma omp task task-clause[optseq] new-line
22730 structured-block */
22732 #define OMP_TASK_CLAUSE_MASK \
22733 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22734 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22735 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22736 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22737 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22738 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22741 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22743 tree clauses, block;
22746 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22747 "#pragma omp task", pragma_tok);
22748 block = begin_omp_task ();
22749 save = cp_parser_begin_omp_structured_block (parser);
22750 cp_parser_statement (parser, NULL_TREE, false, NULL);
22751 cp_parser_end_omp_structured_block (parser, save);
22752 return finish_omp_task (clauses, block);
22756 # pragma omp taskwait new-line */
22759 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22761 cp_parser_require_pragma_eol (parser, pragma_tok);
22762 finish_omp_taskwait ();
22766 # pragma omp threadprivate (variable-list) */
22769 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22773 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22774 cp_parser_require_pragma_eol (parser, pragma_tok);
22776 finish_omp_threadprivate (vars);
22779 /* Main entry point to OpenMP statement pragmas. */
22782 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22786 switch (pragma_tok->pragma_kind)
22788 case PRAGMA_OMP_ATOMIC:
22789 cp_parser_omp_atomic (parser, pragma_tok);
22791 case PRAGMA_OMP_CRITICAL:
22792 stmt = cp_parser_omp_critical (parser, pragma_tok);
22794 case PRAGMA_OMP_FOR:
22795 stmt = cp_parser_omp_for (parser, pragma_tok);
22797 case PRAGMA_OMP_MASTER:
22798 stmt = cp_parser_omp_master (parser, pragma_tok);
22800 case PRAGMA_OMP_ORDERED:
22801 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22803 case PRAGMA_OMP_PARALLEL:
22804 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22806 case PRAGMA_OMP_SECTIONS:
22807 stmt = cp_parser_omp_sections (parser, pragma_tok);
22809 case PRAGMA_OMP_SINGLE:
22810 stmt = cp_parser_omp_single (parser, pragma_tok);
22812 case PRAGMA_OMP_TASK:
22813 stmt = cp_parser_omp_task (parser, pragma_tok);
22816 gcc_unreachable ();
22820 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22825 static GTY (()) cp_parser *the_parser;
22828 /* Special handling for the first token or line in the file. The first
22829 thing in the file might be #pragma GCC pch_preprocess, which loads a
22830 PCH file, which is a GC collection point. So we need to handle this
22831 first pragma without benefit of an existing lexer structure.
22833 Always returns one token to the caller in *FIRST_TOKEN. This is
22834 either the true first token of the file, or the first token after
22835 the initial pragma. */
22838 cp_parser_initial_pragma (cp_token *first_token)
22842 cp_lexer_get_preprocessor_token (NULL, first_token);
22843 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22846 cp_lexer_get_preprocessor_token (NULL, first_token);
22847 if (first_token->type == CPP_STRING)
22849 name = first_token->u.value;
22851 cp_lexer_get_preprocessor_token (NULL, first_token);
22852 if (first_token->type != CPP_PRAGMA_EOL)
22853 error_at (first_token->location,
22854 "junk at end of %<#pragma GCC pch_preprocess%>");
22857 error_at (first_token->location, "expected string literal");
22859 /* Skip to the end of the pragma. */
22860 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22861 cp_lexer_get_preprocessor_token (NULL, first_token);
22863 /* Now actually load the PCH file. */
22865 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22867 /* Read one more token to return to our caller. We have to do this
22868 after reading the PCH file in, since its pointers have to be
22870 cp_lexer_get_preprocessor_token (NULL, first_token);
22873 /* Normal parsing of a pragma token. Here we can (and must) use the
22877 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22879 cp_token *pragma_tok;
22882 pragma_tok = cp_lexer_consume_token (parser->lexer);
22883 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22884 parser->lexer->in_pragma = true;
22886 id = pragma_tok->pragma_kind;
22889 case PRAGMA_GCC_PCH_PREPROCESS:
22890 error_at (pragma_tok->location,
22891 "%<#pragma GCC pch_preprocess%> must be first");
22894 case PRAGMA_OMP_BARRIER:
22897 case pragma_compound:
22898 cp_parser_omp_barrier (parser, pragma_tok);
22901 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22902 "used in compound statements");
22909 case PRAGMA_OMP_FLUSH:
22912 case pragma_compound:
22913 cp_parser_omp_flush (parser, pragma_tok);
22916 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22917 "used in compound statements");
22924 case PRAGMA_OMP_TASKWAIT:
22927 case pragma_compound:
22928 cp_parser_omp_taskwait (parser, pragma_tok);
22931 error_at (pragma_tok->location,
22932 "%<#pragma omp taskwait%> may only be "
22933 "used in compound statements");
22940 case PRAGMA_OMP_THREADPRIVATE:
22941 cp_parser_omp_threadprivate (parser, pragma_tok);
22944 case PRAGMA_OMP_ATOMIC:
22945 case PRAGMA_OMP_CRITICAL:
22946 case PRAGMA_OMP_FOR:
22947 case PRAGMA_OMP_MASTER:
22948 case PRAGMA_OMP_ORDERED:
22949 case PRAGMA_OMP_PARALLEL:
22950 case PRAGMA_OMP_SECTIONS:
22951 case PRAGMA_OMP_SINGLE:
22952 case PRAGMA_OMP_TASK:
22953 if (context == pragma_external)
22955 cp_parser_omp_construct (parser, pragma_tok);
22958 case PRAGMA_OMP_SECTION:
22959 error_at (pragma_tok->location,
22960 "%<#pragma omp section%> may only be used in "
22961 "%<#pragma omp sections%> construct");
22965 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22966 c_invoke_pragma_handler (id);
22970 cp_parser_error (parser, "expected declaration specifiers");
22974 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22978 /* The interface the pragma parsers have to the lexer. */
22981 pragma_lex (tree *value)
22984 enum cpp_ttype ret;
22986 tok = cp_lexer_peek_token (the_parser->lexer);
22989 *value = tok->u.value;
22991 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22993 else if (ret == CPP_STRING)
22994 *value = cp_parser_string_literal (the_parser, false, false);
22997 cp_lexer_consume_token (the_parser->lexer);
22998 if (ret == CPP_KEYWORD)
23006 /* External interface. */
23008 /* Parse one entire translation unit. */
23011 c_parse_file (void)
23013 bool error_occurred;
23014 static bool already_called = false;
23016 if (already_called)
23018 sorry ("inter-module optimizations not implemented for C++");
23021 already_called = true;
23023 the_parser = cp_parser_new ();
23024 push_deferring_access_checks (flag_access_control
23025 ? dk_no_deferred : dk_no_check);
23026 error_occurred = cp_parser_translation_unit (the_parser);
23030 #include "gt-cp-parser.h"