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 error_at (location, "%qE in class %qT does not name a type",
2409 cp_parser_commit_to_tentative_parse (parser);
2412 /* Check for a common situation where a type-name should be present,
2413 but is not, and issue a sensible error message. Returns true if an
2414 invalid type-name was detected.
2416 The situation handled by this function are variable declarations of the
2417 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2418 Usually, `ID' should name a type, but if we got here it means that it
2419 does not. We try to emit the best possible error message depending on
2420 how exactly the id-expression looks like. */
2423 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2426 cp_token *token = cp_lexer_peek_token (parser->lexer);
2428 cp_parser_parse_tentatively (parser);
2429 id = cp_parser_id_expression (parser,
2430 /*template_keyword_p=*/false,
2431 /*check_dependency_p=*/true,
2432 /*template_p=*/NULL,
2433 /*declarator_p=*/true,
2434 /*optional_p=*/false);
2435 /* After the id-expression, there should be a plain identifier,
2436 otherwise this is not a simple variable declaration. Also, if
2437 the scope is dependent, we cannot do much. */
2438 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2439 || (parser->scope && TYPE_P (parser->scope)
2440 && dependent_type_p (parser->scope))
2441 || TREE_CODE (id) == TYPE_DECL)
2443 cp_parser_abort_tentative_parse (parser);
2446 if (!cp_parser_parse_definitely (parser))
2449 /* Emit a diagnostic for the invalid type. */
2450 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2451 id, token->location);
2452 /* Skip to the end of the declaration; there's no point in
2453 trying to process it. */
2454 cp_parser_skip_to_end_of_block_or_statement (parser);
2458 /* Consume tokens up to, and including, the next non-nested closing `)'.
2459 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2460 are doing error recovery. Returns -1 if OR_COMMA is true and we
2461 found an unnested comma. */
2464 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2469 unsigned paren_depth = 0;
2470 unsigned brace_depth = 0;
2471 unsigned square_depth = 0;
2473 if (recovering && !or_comma
2474 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2479 cp_token * token = cp_lexer_peek_token (parser->lexer);
2481 switch (token->type)
2484 case CPP_PRAGMA_EOL:
2485 /* If we've run out of tokens, then there is no closing `)'. */
2488 /* This is good for lambda expression capture-lists. */
2489 case CPP_OPEN_SQUARE:
2492 case CPP_CLOSE_SQUARE:
2493 if (!square_depth--)
2498 /* This matches the processing in skip_to_end_of_statement. */
2503 case CPP_OPEN_BRACE:
2506 case CPP_CLOSE_BRACE:
2512 if (recovering && or_comma && !brace_depth && !paren_depth
2517 case CPP_OPEN_PAREN:
2522 case CPP_CLOSE_PAREN:
2523 if (!brace_depth && !paren_depth--)
2526 cp_lexer_consume_token (parser->lexer);
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser->lexer);
2540 /* Consume tokens until we reach the end of the current statement.
2541 Normally, that will be just before consuming a `;'. However, if a
2542 non-nested `}' comes first, then we stop before consuming that. */
2545 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2547 unsigned nesting_depth = 0;
2551 cp_token *token = cp_lexer_peek_token (parser->lexer);
2553 switch (token->type)
2556 case CPP_PRAGMA_EOL:
2557 /* If we've run out of tokens, stop. */
2561 /* If the next token is a `;', we have reached the end of the
2567 case CPP_CLOSE_BRACE:
2568 /* If this is a non-nested '}', stop before consuming it.
2569 That way, when confronted with something like:
2573 we stop before consuming the closing '}', even though we
2574 have not yet reached a `;'. */
2575 if (nesting_depth == 0)
2578 /* If it is the closing '}' for a block that we have
2579 scanned, stop -- but only after consuming the token.
2585 we will stop after the body of the erroneously declared
2586 function, but before consuming the following `typedef'
2588 if (--nesting_depth == 0)
2590 cp_lexer_consume_token (parser->lexer);
2594 case CPP_OPEN_BRACE:
2602 /* Consume the token. */
2603 cp_lexer_consume_token (parser->lexer);
2607 /* This function is called at the end of a statement or declaration.
2608 If the next token is a semicolon, it is consumed; otherwise, error
2609 recovery is attempted. */
2612 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2614 /* Look for the trailing `;'. */
2615 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2617 /* If there is additional (erroneous) input, skip to the end of
2619 cp_parser_skip_to_end_of_statement (parser);
2620 /* If the next token is now a `;', consume it. */
2621 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2622 cp_lexer_consume_token (parser->lexer);
2626 /* Skip tokens until we have consumed an entire block, or until we
2627 have consumed a non-nested `;'. */
2630 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2632 int nesting_depth = 0;
2634 while (nesting_depth >= 0)
2636 cp_token *token = cp_lexer_peek_token (parser->lexer);
2638 switch (token->type)
2641 case CPP_PRAGMA_EOL:
2642 /* If we've run out of tokens, stop. */
2646 /* Stop if this is an unnested ';'. */
2651 case CPP_CLOSE_BRACE:
2652 /* Stop if this is an unnested '}', or closes the outermost
2655 if (nesting_depth < 0)
2661 case CPP_OPEN_BRACE:
2670 /* Consume the token. */
2671 cp_lexer_consume_token (parser->lexer);
2675 /* Skip tokens until a non-nested closing curly brace is the next
2676 token, or there are no more tokens. Return true in the first case,
2680 cp_parser_skip_to_closing_brace (cp_parser *parser)
2682 unsigned nesting_depth = 0;
2686 cp_token *token = cp_lexer_peek_token (parser->lexer);
2688 switch (token->type)
2691 case CPP_PRAGMA_EOL:
2692 /* If we've run out of tokens, stop. */
2695 case CPP_CLOSE_BRACE:
2696 /* If the next token is a non-nested `}', then we have reached
2697 the end of the current block. */
2698 if (nesting_depth-- == 0)
2702 case CPP_OPEN_BRACE:
2703 /* If it the next token is a `{', then we are entering a new
2704 block. Consume the entire block. */
2712 /* Consume the token. */
2713 cp_lexer_consume_token (parser->lexer);
2717 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2718 parameter is the PRAGMA token, allowing us to purge the entire pragma
2722 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2726 parser->lexer->in_pragma = false;
2729 token = cp_lexer_consume_token (parser->lexer);
2730 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2732 /* Ensure that the pragma is not parsed again. */
2733 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2736 /* Require pragma end of line, resyncing with it as necessary. The
2737 arguments are as for cp_parser_skip_to_pragma_eol. */
2740 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2742 parser->lexer->in_pragma = false;
2743 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2747 /* This is a simple wrapper around make_typename_type. When the id is
2748 an unresolved identifier node, we can provide a superior diagnostic
2749 using cp_parser_diagnose_invalid_type_name. */
2752 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2753 tree id, location_t id_location)
2756 if (TREE_CODE (id) == IDENTIFIER_NODE)
2758 result = make_typename_type (scope, id, typename_type,
2759 /*complain=*/tf_none);
2760 if (result == error_mark_node)
2761 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2764 return make_typename_type (scope, id, typename_type, tf_error);
2767 /* This is a wrapper around the
2768 make_{pointer,ptrmem,reference}_declarator functions that decides
2769 which one to call based on the CODE and CLASS_TYPE arguments. The
2770 CODE argument should be one of the values returned by
2771 cp_parser_ptr_operator. */
2772 static cp_declarator *
2773 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2774 cp_cv_quals cv_qualifiers,
2775 cp_declarator *target)
2777 if (code == ERROR_MARK)
2778 return cp_error_declarator;
2780 if (code == INDIRECT_REF)
2781 if (class_type == NULL_TREE)
2782 return make_pointer_declarator (cv_qualifiers, target);
2784 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2785 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2786 return make_reference_declarator (cv_qualifiers, target, false);
2787 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2788 return make_reference_declarator (cv_qualifiers, target, true);
2792 /* Create a new C++ parser. */
2795 cp_parser_new (void)
2801 /* cp_lexer_new_main is called before calling ggc_alloc because
2802 cp_lexer_new_main might load a PCH file. */
2803 lexer = cp_lexer_new_main ();
2805 /* Initialize the binops_by_token so that we can get the tree
2806 directly from the token. */
2807 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2808 binops_by_token[binops[i].token_type] = binops[i];
2810 parser = GGC_CNEW (cp_parser);
2811 parser->lexer = lexer;
2812 parser->context = cp_parser_context_new (NULL);
2814 /* For now, we always accept GNU extensions. */
2815 parser->allow_gnu_extensions_p = 1;
2817 /* The `>' token is a greater-than operator, not the end of a
2819 parser->greater_than_is_operator_p = true;
2821 parser->default_arg_ok_p = true;
2823 /* We are not parsing a constant-expression. */
2824 parser->integral_constant_expression_p = false;
2825 parser->allow_non_integral_constant_expression_p = false;
2826 parser->non_integral_constant_expression_p = false;
2828 /* Local variable names are not forbidden. */
2829 parser->local_variables_forbidden_p = false;
2831 /* We are not processing an `extern "C"' declaration. */
2832 parser->in_unbraced_linkage_specification_p = false;
2834 /* We are not processing a declarator. */
2835 parser->in_declarator_p = false;
2837 /* We are not processing a template-argument-list. */
2838 parser->in_template_argument_list_p = false;
2840 /* We are not in an iteration statement. */
2841 parser->in_statement = 0;
2843 /* We are not in a switch statement. */
2844 parser->in_switch_statement_p = false;
2846 /* We are not parsing a type-id inside an expression. */
2847 parser->in_type_id_in_expr_p = false;
2849 /* Declarations aren't implicitly extern "C". */
2850 parser->implicit_extern_c = false;
2852 /* String literals should be translated to the execution character set. */
2853 parser->translate_strings_p = true;
2855 /* We are not parsing a function body. */
2856 parser->in_function_body = false;
2858 /* The unparsed function queue is empty. */
2859 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2861 /* There are no classes being defined. */
2862 parser->num_classes_being_defined = 0;
2864 /* No template parameters apply. */
2865 parser->num_template_parameter_lists = 0;
2870 /* Create a cp_lexer structure which will emit the tokens in CACHE
2871 and push it onto the parser's lexer stack. This is used for delayed
2872 parsing of in-class method bodies and default arguments, and should
2873 not be confused with tentative parsing. */
2875 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2877 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2878 lexer->next = parser->lexer;
2879 parser->lexer = lexer;
2881 /* Move the current source position to that of the first token in the
2883 cp_lexer_set_source_position_from_token (lexer->next_token);
2886 /* Pop the top lexer off the parser stack. This is never used for the
2887 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2889 cp_parser_pop_lexer (cp_parser *parser)
2891 cp_lexer *lexer = parser->lexer;
2892 parser->lexer = lexer->next;
2893 cp_lexer_destroy (lexer);
2895 /* Put the current source position back where it was before this
2896 lexer was pushed. */
2897 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2900 /* Lexical conventions [gram.lex] */
2902 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2906 cp_parser_identifier (cp_parser* parser)
2910 /* Look for the identifier. */
2911 token = cp_parser_require (parser, CPP_NAME, "identifier");
2912 /* Return the value. */
2913 return token ? token->u.value : error_mark_node;
2916 /* Parse a sequence of adjacent string constants. Returns a
2917 TREE_STRING representing the combined, nul-terminated string
2918 constant. If TRANSLATE is true, translate the string to the
2919 execution character set. If WIDE_OK is true, a wide string is
2922 C++98 [lex.string] says that if a narrow string literal token is
2923 adjacent to a wide string literal token, the behavior is undefined.
2924 However, C99 6.4.5p4 says that this results in a wide string literal.
2925 We follow C99 here, for consistency with the C front end.
2927 This code is largely lifted from lex_string() in c-lex.c.
2929 FUTURE: ObjC++ will need to handle @-strings here. */
2931 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2935 struct obstack str_ob;
2936 cpp_string str, istr, *strs;
2938 enum cpp_ttype type;
2940 tok = cp_lexer_peek_token (parser->lexer);
2941 if (!cp_parser_is_string_literal (tok))
2943 cp_parser_error (parser, "expected string-literal");
2944 return error_mark_node;
2949 /* Try to avoid the overhead of creating and destroying an obstack
2950 for the common case of just one string. */
2951 if (!cp_parser_is_string_literal
2952 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2954 cp_lexer_consume_token (parser->lexer);
2956 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2957 str.len = TREE_STRING_LENGTH (tok->u.value);
2964 gcc_obstack_init (&str_ob);
2969 cp_lexer_consume_token (parser->lexer);
2971 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2972 str.len = TREE_STRING_LENGTH (tok->u.value);
2974 if (type != tok->type)
2976 if (type == CPP_STRING)
2978 else if (tok->type != CPP_STRING)
2979 error_at (tok->location,
2980 "unsupported non-standard concatenation "
2981 "of string literals");
2984 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2986 tok = cp_lexer_peek_token (parser->lexer);
2988 while (cp_parser_is_string_literal (tok));
2990 strs = (cpp_string *) obstack_finish (&str_ob);
2993 if (type != CPP_STRING && !wide_ok)
2995 cp_parser_error (parser, "a wide string is invalid in this context");
2999 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3000 (parse_in, strs, count, &istr, type))
3002 value = build_string (istr.len, (const char *)istr.text);
3003 free (CONST_CAST (unsigned char *, istr.text));
3009 case CPP_UTF8STRING:
3010 TREE_TYPE (value) = char_array_type_node;
3013 TREE_TYPE (value) = char16_array_type_node;
3016 TREE_TYPE (value) = char32_array_type_node;
3019 TREE_TYPE (value) = wchar_array_type_node;
3023 value = fix_string_type (value);
3026 /* cpp_interpret_string has issued an error. */
3027 value = error_mark_node;
3030 obstack_free (&str_ob, 0);
3036 /* Basic concepts [gram.basic] */
3038 /* Parse a translation-unit.
3041 declaration-seq [opt]
3043 Returns TRUE if all went well. */
3046 cp_parser_translation_unit (cp_parser* parser)
3048 /* The address of the first non-permanent object on the declarator
3050 static void *declarator_obstack_base;
3054 /* Create the declarator obstack, if necessary. */
3055 if (!cp_error_declarator)
3057 gcc_obstack_init (&declarator_obstack);
3058 /* Create the error declarator. */
3059 cp_error_declarator = make_declarator (cdk_error);
3060 /* Create the empty parameter list. */
3061 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3062 /* Remember where the base of the declarator obstack lies. */
3063 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3066 cp_parser_declaration_seq_opt (parser);
3068 /* If there are no tokens left then all went well. */
3069 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3071 /* Get rid of the token array; we don't need it any more. */
3072 cp_lexer_destroy (parser->lexer);
3073 parser->lexer = NULL;
3075 /* This file might have been a context that's implicitly extern
3076 "C". If so, pop the lang context. (Only relevant for PCH.) */
3077 if (parser->implicit_extern_c)
3079 pop_lang_context ();
3080 parser->implicit_extern_c = false;
3084 finish_translation_unit ();
3090 cp_parser_error (parser, "expected declaration");
3094 /* Make sure the declarator obstack was fully cleaned up. */
3095 gcc_assert (obstack_next_free (&declarator_obstack)
3096 == declarator_obstack_base);
3098 /* All went well. */
3102 /* Expressions [gram.expr] */
3104 /* Parse a primary-expression.
3115 ( compound-statement )
3116 __builtin_va_arg ( assignment-expression , type-id )
3117 __builtin_offsetof ( type-id , offsetof-expression )
3120 __has_nothrow_assign ( type-id )
3121 __has_nothrow_constructor ( type-id )
3122 __has_nothrow_copy ( type-id )
3123 __has_trivial_assign ( type-id )
3124 __has_trivial_constructor ( type-id )
3125 __has_trivial_copy ( type-id )
3126 __has_trivial_destructor ( type-id )
3127 __has_virtual_destructor ( type-id )
3128 __is_abstract ( type-id )
3129 __is_base_of ( type-id , type-id )
3130 __is_class ( type-id )
3131 __is_convertible_to ( type-id , type-id )
3132 __is_empty ( type-id )
3133 __is_enum ( type-id )
3134 __is_pod ( type-id )
3135 __is_polymorphic ( type-id )
3136 __is_union ( type-id )
3138 Objective-C++ Extension:
3146 ADDRESS_P is true iff this expression was immediately preceded by
3147 "&" and therefore might denote a pointer-to-member. CAST_P is true
3148 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3149 true iff this expression is a template argument.
3151 Returns a representation of the expression. Upon return, *IDK
3152 indicates what kind of id-expression (if any) was present. */
3155 cp_parser_primary_expression (cp_parser *parser,
3158 bool template_arg_p,
3161 cp_token *token = NULL;
3163 /* Assume the primary expression is not an id-expression. */
3164 *idk = CP_ID_KIND_NONE;
3166 /* Peek at the next token. */
3167 token = cp_lexer_peek_token (parser->lexer);
3168 switch (token->type)
3181 token = cp_lexer_consume_token (parser->lexer);
3182 if (TREE_CODE (token->u.value) == FIXED_CST)
3184 error_at (token->location,
3185 "fixed-point types not supported in C++");
3186 return error_mark_node;
3188 /* Floating-point literals are only allowed in an integral
3189 constant expression if they are cast to an integral or
3190 enumeration type. */
3191 if (TREE_CODE (token->u.value) == REAL_CST
3192 && parser->integral_constant_expression_p
3195 /* CAST_P will be set even in invalid code like "int(2.7 +
3196 ...)". Therefore, we have to check that the next token
3197 is sure to end the cast. */
3200 cp_token *next_token;
3202 next_token = cp_lexer_peek_token (parser->lexer);
3203 if (/* The comma at the end of an
3204 enumerator-definition. */
3205 next_token->type != CPP_COMMA
3206 /* The curly brace at the end of an enum-specifier. */
3207 && next_token->type != CPP_CLOSE_BRACE
3208 /* The end of a statement. */
3209 && next_token->type != CPP_SEMICOLON
3210 /* The end of the cast-expression. */
3211 && next_token->type != CPP_CLOSE_PAREN
3212 /* The end of an array bound. */
3213 && next_token->type != CPP_CLOSE_SQUARE
3214 /* The closing ">" in a template-argument-list. */
3215 && (next_token->type != CPP_GREATER
3216 || parser->greater_than_is_operator_p)
3217 /* C++0x only: A ">>" treated like two ">" tokens,
3218 in a template-argument-list. */
3219 && (next_token->type != CPP_RSHIFT
3220 || (cxx_dialect == cxx98)
3221 || parser->greater_than_is_operator_p))
3225 /* If we are within a cast, then the constraint that the
3226 cast is to an integral or enumeration type will be
3227 checked at that point. If we are not within a cast, then
3228 this code is invalid. */
3230 cp_parser_non_integral_constant_expression
3231 (parser, "floating-point literal");
3233 return token->u.value;
3239 case CPP_UTF8STRING:
3240 /* ??? Should wide strings be allowed when parser->translate_strings_p
3241 is false (i.e. in attributes)? If not, we can kill the third
3242 argument to cp_parser_string_literal. */
3243 return cp_parser_string_literal (parser,
3244 parser->translate_strings_p,
3247 case CPP_OPEN_PAREN:
3250 bool saved_greater_than_is_operator_p;
3252 /* Consume the `('. */
3253 cp_lexer_consume_token (parser->lexer);
3254 /* Within a parenthesized expression, a `>' token is always
3255 the greater-than operator. */
3256 saved_greater_than_is_operator_p
3257 = parser->greater_than_is_operator_p;
3258 parser->greater_than_is_operator_p = true;
3259 /* If we see `( { ' then we are looking at the beginning of
3260 a GNU statement-expression. */
3261 if (cp_parser_allow_gnu_extensions_p (parser)
3262 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3264 /* Statement-expressions are not allowed by the standard. */
3265 pedwarn (token->location, OPT_pedantic,
3266 "ISO C++ forbids braced-groups within expressions");
3268 /* And they're not allowed outside of a function-body; you
3269 cannot, for example, write:
3271 int i = ({ int j = 3; j + 1; });
3273 at class or namespace scope. */
3274 if (!parser->in_function_body
3275 || parser->in_template_argument_list_p)
3277 error_at (token->location,
3278 "statement-expressions are not allowed outside "
3279 "functions nor in template-argument lists");
3280 cp_parser_skip_to_end_of_block_or_statement (parser);
3281 expr = error_mark_node;
3285 /* Start the statement-expression. */
3286 expr = begin_stmt_expr ();
3287 /* Parse the compound-statement. */
3288 cp_parser_compound_statement (parser, expr, false);
3290 expr = finish_stmt_expr (expr, false);
3295 /* Parse the parenthesized expression. */
3296 expr = cp_parser_expression (parser, cast_p, idk);
3297 /* Let the front end know that this expression was
3298 enclosed in parentheses. This matters in case, for
3299 example, the expression is of the form `A::B', since
3300 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3302 finish_parenthesized_expr (expr);
3304 /* The `>' token might be the end of a template-id or
3305 template-parameter-list now. */
3306 parser->greater_than_is_operator_p
3307 = saved_greater_than_is_operator_p;
3308 /* Consume the `)'. */
3309 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3310 cp_parser_skip_to_end_of_statement (parser);
3315 case CPP_OPEN_SQUARE:
3316 if (c_dialect_objc ())
3317 /* We have an Objective-C++ message. */
3318 return cp_parser_objc_expression (parser);
3319 maybe_warn_cpp0x ("lambda expressions");
3320 return cp_parser_lambda_expression (parser);
3322 case CPP_OBJC_STRING:
3323 if (c_dialect_objc ())
3324 /* We have an Objective-C++ string literal. */
3325 return cp_parser_objc_expression (parser);
3326 cp_parser_error (parser, "expected primary-expression");
3327 return error_mark_node;
3330 switch (token->keyword)
3332 /* These two are the boolean literals. */
3334 cp_lexer_consume_token (parser->lexer);
3335 return boolean_true_node;
3337 cp_lexer_consume_token (parser->lexer);
3338 return boolean_false_node;
3340 /* The `__null' literal. */
3342 cp_lexer_consume_token (parser->lexer);
3345 /* Recognize the `this' keyword. */
3347 cp_lexer_consume_token (parser->lexer);
3348 if (parser->local_variables_forbidden_p)
3350 error_at (token->location,
3351 "%<this%> may not be used in this context");
3352 return error_mark_node;
3354 /* Pointers cannot appear in constant-expressions. */
3355 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3356 return error_mark_node;
3357 return finish_this_expr ();
3359 /* The `operator' keyword can be the beginning of an
3364 case RID_FUNCTION_NAME:
3365 case RID_PRETTY_FUNCTION_NAME:
3366 case RID_C99_FUNCTION_NAME:
3370 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3371 __func__ are the names of variables -- but they are
3372 treated specially. Therefore, they are handled here,
3373 rather than relying on the generic id-expression logic
3374 below. Grammatically, these names are id-expressions.
3376 Consume the token. */
3377 token = cp_lexer_consume_token (parser->lexer);
3379 switch (token->keyword)
3381 case RID_FUNCTION_NAME:
3382 name = "%<__FUNCTION__%>";
3384 case RID_PRETTY_FUNCTION_NAME:
3385 name = "%<__PRETTY_FUNCTION__%>";
3387 case RID_C99_FUNCTION_NAME:
3388 name = "%<__func__%>";
3394 if (cp_parser_non_integral_constant_expression (parser, name))
3395 return error_mark_node;
3397 /* Look up the name. */
3398 return finish_fname (token->u.value);
3406 /* The `__builtin_va_arg' construct is used to handle
3407 `va_arg'. Consume the `__builtin_va_arg' token. */
3408 cp_lexer_consume_token (parser->lexer);
3409 /* Look for the opening `('. */
3410 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3411 /* Now, parse the assignment-expression. */
3412 expression = cp_parser_assignment_expression (parser,
3413 /*cast_p=*/false, NULL);
3414 /* Look for the `,'. */
3415 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3416 /* Parse the type-id. */
3417 type = cp_parser_type_id (parser);
3418 /* Look for the closing `)'. */
3419 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3420 /* Using `va_arg' in a constant-expression is not
3422 if (cp_parser_non_integral_constant_expression (parser,
3424 return error_mark_node;
3425 return build_x_va_arg (expression, type);
3429 return cp_parser_builtin_offsetof (parser);
3431 case RID_HAS_NOTHROW_ASSIGN:
3432 case RID_HAS_NOTHROW_CONSTRUCTOR:
3433 case RID_HAS_NOTHROW_COPY:
3434 case RID_HAS_TRIVIAL_ASSIGN:
3435 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3436 case RID_HAS_TRIVIAL_COPY:
3437 case RID_HAS_TRIVIAL_DESTRUCTOR:
3438 case RID_HAS_VIRTUAL_DESTRUCTOR:
3439 case RID_IS_ABSTRACT:
3440 case RID_IS_BASE_OF:
3442 case RID_IS_CONVERTIBLE_TO:
3446 case RID_IS_POLYMORPHIC:
3447 case RID_IS_STD_LAYOUT:
3448 case RID_IS_TRIVIAL:
3450 return cp_parser_trait_expr (parser, token->keyword);
3452 /* Objective-C++ expressions. */
3454 case RID_AT_PROTOCOL:
3455 case RID_AT_SELECTOR:
3456 return cp_parser_objc_expression (parser);
3459 cp_parser_error (parser, "expected primary-expression");
3460 return error_mark_node;
3463 /* An id-expression can start with either an identifier, a
3464 `::' as the beginning of a qualified-id, or the "operator"
3468 case CPP_TEMPLATE_ID:
3469 case CPP_NESTED_NAME_SPECIFIER:
3473 const char *error_msg;
3476 cp_token *id_expr_token;
3479 /* Parse the id-expression. */
3481 = cp_parser_id_expression (parser,
3482 /*template_keyword_p=*/false,
3483 /*check_dependency_p=*/true,
3485 /*declarator_p=*/false,
3486 /*optional_p=*/false);
3487 if (id_expression == error_mark_node)
3488 return error_mark_node;
3489 id_expr_token = token;
3490 token = cp_lexer_peek_token (parser->lexer);
3491 done = (token->type != CPP_OPEN_SQUARE
3492 && token->type != CPP_OPEN_PAREN
3493 && token->type != CPP_DOT
3494 && token->type != CPP_DEREF
3495 && token->type != CPP_PLUS_PLUS
3496 && token->type != CPP_MINUS_MINUS);
3497 /* If we have a template-id, then no further lookup is
3498 required. If the template-id was for a template-class, we
3499 will sometimes have a TYPE_DECL at this point. */
3500 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3501 || TREE_CODE (id_expression) == TYPE_DECL)
3502 decl = id_expression;
3503 /* Look up the name. */
3506 tree ambiguous_decls;
3508 decl = cp_parser_lookup_name (parser, id_expression,
3511 /*is_namespace=*/false,
3512 /*check_dependency=*/true,
3514 id_expr_token->location);
3515 /* If the lookup was ambiguous, an error will already have
3517 if (ambiguous_decls)
3518 return error_mark_node;
3520 /* In Objective-C++, an instance variable (ivar) may be preferred
3521 to whatever cp_parser_lookup_name() found. */
3522 decl = objc_lookup_ivar (decl, id_expression);
3524 /* If name lookup gives us a SCOPE_REF, then the
3525 qualifying scope was dependent. */
3526 if (TREE_CODE (decl) == SCOPE_REF)
3528 /* At this point, we do not know if DECL is a valid
3529 integral constant expression. We assume that it is
3530 in fact such an expression, so that code like:
3532 template <int N> struct A {
3536 is accepted. At template-instantiation time, we
3537 will check that B<N>::i is actually a constant. */
3540 /* Check to see if DECL is a local variable in a context
3541 where that is forbidden. */
3542 if (parser->local_variables_forbidden_p
3543 && local_variable_p (decl))
3545 /* It might be that we only found DECL because we are
3546 trying to be generous with pre-ISO scoping rules.
3547 For example, consider:
3551 for (int i = 0; i < 10; ++i) {}
3552 extern void f(int j = i);
3555 Here, name look up will originally find the out
3556 of scope `i'. We need to issue a warning message,
3557 but then use the global `i'. */
3558 decl = check_for_out_of_scope_variable (decl);
3559 if (local_variable_p (decl))
3561 error_at (id_expr_token->location,
3562 "local variable %qD may not appear in this context",
3564 return error_mark_node;
3569 decl = (finish_id_expression
3570 (id_expression, decl, parser->scope,
3572 parser->integral_constant_expression_p,
3573 parser->allow_non_integral_constant_expression_p,
3574 &parser->non_integral_constant_expression_p,
3575 template_p, done, address_p,
3578 id_expr_token->location));
3580 cp_parser_error (parser, error_msg);
3584 /* Anything else is an error. */
3586 cp_parser_error (parser, "expected primary-expression");
3587 return error_mark_node;
3591 /* Parse an id-expression.
3598 :: [opt] nested-name-specifier template [opt] unqualified-id
3600 :: operator-function-id
3603 Return a representation of the unqualified portion of the
3604 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3605 a `::' or nested-name-specifier.
3607 Often, if the id-expression was a qualified-id, the caller will
3608 want to make a SCOPE_REF to represent the qualified-id. This
3609 function does not do this in order to avoid wastefully creating
3610 SCOPE_REFs when they are not required.
3612 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3615 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3616 uninstantiated templates.
3618 If *TEMPLATE_P is non-NULL, it is set to true iff the
3619 `template' keyword is used to explicitly indicate that the entity
3620 named is a template.
3622 If DECLARATOR_P is true, the id-expression is appearing as part of
3623 a declarator, rather than as part of an expression. */
3626 cp_parser_id_expression (cp_parser *parser,
3627 bool template_keyword_p,
3628 bool check_dependency_p,
3633 bool global_scope_p;
3634 bool nested_name_specifier_p;
3636 /* Assume the `template' keyword was not used. */
3638 *template_p = template_keyword_p;
3640 /* Look for the optional `::' operator. */
3642 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3644 /* Look for the optional nested-name-specifier. */
3645 nested_name_specifier_p
3646 = (cp_parser_nested_name_specifier_opt (parser,
3647 /*typename_keyword_p=*/false,
3652 /* If there is a nested-name-specifier, then we are looking at
3653 the first qualified-id production. */
3654 if (nested_name_specifier_p)
3657 tree saved_object_scope;
3658 tree saved_qualifying_scope;
3659 tree unqualified_id;
3662 /* See if the next token is the `template' keyword. */
3664 template_p = &is_template;
3665 *template_p = cp_parser_optional_template_keyword (parser);
3666 /* Name lookup we do during the processing of the
3667 unqualified-id might obliterate SCOPE. */
3668 saved_scope = parser->scope;
3669 saved_object_scope = parser->object_scope;
3670 saved_qualifying_scope = parser->qualifying_scope;
3671 /* Process the final unqualified-id. */
3672 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3675 /*optional_p=*/false);
3676 /* Restore the SAVED_SCOPE for our caller. */
3677 parser->scope = saved_scope;
3678 parser->object_scope = saved_object_scope;
3679 parser->qualifying_scope = saved_qualifying_scope;
3681 return unqualified_id;
3683 /* Otherwise, if we are in global scope, then we are looking at one
3684 of the other qualified-id productions. */
3685 else if (global_scope_p)
3690 /* Peek at the next token. */
3691 token = cp_lexer_peek_token (parser->lexer);
3693 /* If it's an identifier, and the next token is not a "<", then
3694 we can avoid the template-id case. This is an optimization
3695 for this common case. */
3696 if (token->type == CPP_NAME
3697 && !cp_parser_nth_token_starts_template_argument_list_p
3699 return cp_parser_identifier (parser);
3701 cp_parser_parse_tentatively (parser);
3702 /* Try a template-id. */
3703 id = cp_parser_template_id (parser,
3704 /*template_keyword_p=*/false,
3705 /*check_dependency_p=*/true,
3707 /* If that worked, we're done. */
3708 if (cp_parser_parse_definitely (parser))
3711 /* Peek at the next token. (Changes in the token buffer may
3712 have invalidated the pointer obtained above.) */
3713 token = cp_lexer_peek_token (parser->lexer);
3715 switch (token->type)
3718 return cp_parser_identifier (parser);
3721 if (token->keyword == RID_OPERATOR)
3722 return cp_parser_operator_function_id (parser);
3726 cp_parser_error (parser, "expected id-expression");
3727 return error_mark_node;
3731 return cp_parser_unqualified_id (parser, template_keyword_p,
3732 /*check_dependency_p=*/true,
3737 /* Parse an unqualified-id.
3741 operator-function-id
3742 conversion-function-id
3746 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3747 keyword, in a construct like `A::template ...'.
3749 Returns a representation of unqualified-id. For the `identifier'
3750 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3751 production a BIT_NOT_EXPR is returned; the operand of the
3752 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3753 other productions, see the documentation accompanying the
3754 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3755 names are looked up in uninstantiated templates. If DECLARATOR_P
3756 is true, the unqualified-id is appearing as part of a declarator,
3757 rather than as part of an expression. */
3760 cp_parser_unqualified_id (cp_parser* parser,
3761 bool template_keyword_p,
3762 bool check_dependency_p,
3768 /* Peek at the next token. */
3769 token = cp_lexer_peek_token (parser->lexer);
3771 switch (token->type)
3777 /* We don't know yet whether or not this will be a
3779 cp_parser_parse_tentatively (parser);
3780 /* Try a template-id. */
3781 id = cp_parser_template_id (parser, template_keyword_p,
3784 /* If it worked, we're done. */
3785 if (cp_parser_parse_definitely (parser))
3787 /* Otherwise, it's an ordinary identifier. */
3788 return cp_parser_identifier (parser);
3791 case CPP_TEMPLATE_ID:
3792 return cp_parser_template_id (parser, template_keyword_p,
3799 tree qualifying_scope;
3804 /* Consume the `~' token. */
3805 cp_lexer_consume_token (parser->lexer);
3806 /* Parse the class-name. The standard, as written, seems to
3809 template <typename T> struct S { ~S (); };
3810 template <typename T> S<T>::~S() {}
3812 is invalid, since `~' must be followed by a class-name, but
3813 `S<T>' is dependent, and so not known to be a class.
3814 That's not right; we need to look in uninstantiated
3815 templates. A further complication arises from:
3817 template <typename T> void f(T t) {
3821 Here, it is not possible to look up `T' in the scope of `T'
3822 itself. We must look in both the current scope, and the
3823 scope of the containing complete expression.
3825 Yet another issue is:
3834 The standard does not seem to say that the `S' in `~S'
3835 should refer to the type `S' and not the data member
3838 /* DR 244 says that we look up the name after the "~" in the
3839 same scope as we looked up the qualifying name. That idea
3840 isn't fully worked out; it's more complicated than that. */
3841 scope = parser->scope;
3842 object_scope = parser->object_scope;
3843 qualifying_scope = parser->qualifying_scope;
3845 /* Check for invalid scopes. */
3846 if (scope == error_mark_node)
3848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3849 cp_lexer_consume_token (parser->lexer);
3850 return error_mark_node;
3852 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3854 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3855 error_at (token->location,
3856 "scope %qT before %<~%> is not a class-name",
3858 cp_parser_simulate_error (parser);
3859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3860 cp_lexer_consume_token (parser->lexer);
3861 return error_mark_node;
3863 gcc_assert (!scope || TYPE_P (scope));
3865 /* If the name is of the form "X::~X" it's OK. */
3866 token = cp_lexer_peek_token (parser->lexer);
3868 && token->type == CPP_NAME
3869 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3871 && constructor_name_p (token->u.value, scope))
3873 cp_lexer_consume_token (parser->lexer);
3874 return build_nt (BIT_NOT_EXPR, scope);
3877 /* If there was an explicit qualification (S::~T), first look
3878 in the scope given by the qualification (i.e., S). */
3880 type_decl = NULL_TREE;
3883 cp_parser_parse_tentatively (parser);
3884 type_decl = cp_parser_class_name (parser,
3885 /*typename_keyword_p=*/false,
3886 /*template_keyword_p=*/false,
3888 /*check_dependency=*/false,
3889 /*class_head_p=*/false,
3891 if (cp_parser_parse_definitely (parser))
3894 /* In "N::S::~S", look in "N" as well. */
3895 if (!done && scope && qualifying_scope)
3897 cp_parser_parse_tentatively (parser);
3898 parser->scope = qualifying_scope;
3899 parser->object_scope = NULL_TREE;
3900 parser->qualifying_scope = NULL_TREE;
3902 = cp_parser_class_name (parser,
3903 /*typename_keyword_p=*/false,
3904 /*template_keyword_p=*/false,
3906 /*check_dependency=*/false,
3907 /*class_head_p=*/false,
3909 if (cp_parser_parse_definitely (parser))
3912 /* In "p->S::~T", look in the scope given by "*p" as well. */
3913 else if (!done && object_scope)
3915 cp_parser_parse_tentatively (parser);
3916 parser->scope = object_scope;
3917 parser->object_scope = NULL_TREE;
3918 parser->qualifying_scope = NULL_TREE;
3920 = cp_parser_class_name (parser,
3921 /*typename_keyword_p=*/false,
3922 /*template_keyword_p=*/false,
3924 /*check_dependency=*/false,
3925 /*class_head_p=*/false,
3927 if (cp_parser_parse_definitely (parser))
3930 /* Look in the surrounding context. */
3933 parser->scope = NULL_TREE;
3934 parser->object_scope = NULL_TREE;
3935 parser->qualifying_scope = NULL_TREE;
3936 if (processing_template_decl)
3937 cp_parser_parse_tentatively (parser);
3939 = cp_parser_class_name (parser,
3940 /*typename_keyword_p=*/false,
3941 /*template_keyword_p=*/false,
3943 /*check_dependency=*/false,
3944 /*class_head_p=*/false,
3946 if (processing_template_decl
3947 && ! cp_parser_parse_definitely (parser))
3949 /* We couldn't find a type with this name, so just accept
3950 it and check for a match at instantiation time. */
3951 type_decl = cp_parser_identifier (parser);
3952 if (type_decl != error_mark_node)
3953 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3957 /* If an error occurred, assume that the name of the
3958 destructor is the same as the name of the qualifying
3959 class. That allows us to keep parsing after running
3960 into ill-formed destructor names. */
3961 if (type_decl == error_mark_node && scope)
3962 return build_nt (BIT_NOT_EXPR, scope);
3963 else if (type_decl == error_mark_node)
3964 return error_mark_node;
3966 /* Check that destructor name and scope match. */
3967 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3969 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3970 error_at (token->location,
3971 "declaration of %<~%T%> as member of %qT",
3973 cp_parser_simulate_error (parser);
3974 return error_mark_node;
3979 A typedef-name that names a class shall not be used as the
3980 identifier in the declarator for a destructor declaration. */
3982 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3983 && !DECL_SELF_REFERENCE_P (type_decl)
3984 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3985 error_at (token->location,
3986 "typedef-name %qD used as destructor declarator",
3989 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3993 if (token->keyword == RID_OPERATOR)
3997 /* This could be a template-id, so we try that first. */
3998 cp_parser_parse_tentatively (parser);
3999 /* Try a template-id. */
4000 id = cp_parser_template_id (parser, template_keyword_p,
4001 /*check_dependency_p=*/true,
4003 /* If that worked, we're done. */
4004 if (cp_parser_parse_definitely (parser))
4006 /* We still don't know whether we're looking at an
4007 operator-function-id or a conversion-function-id. */
4008 cp_parser_parse_tentatively (parser);
4009 /* Try an operator-function-id. */
4010 id = cp_parser_operator_function_id (parser);
4011 /* If that didn't work, try a conversion-function-id. */
4012 if (!cp_parser_parse_definitely (parser))
4013 id = cp_parser_conversion_function_id (parser);
4022 cp_parser_error (parser, "expected unqualified-id");
4023 return error_mark_node;
4027 /* Parse an (optional) nested-name-specifier.
4029 nested-name-specifier: [C++98]
4030 class-or-namespace-name :: nested-name-specifier [opt]
4031 class-or-namespace-name :: template nested-name-specifier [opt]
4033 nested-name-specifier: [C++0x]
4036 nested-name-specifier identifier ::
4037 nested-name-specifier template [opt] simple-template-id ::
4039 PARSER->SCOPE should be set appropriately before this function is
4040 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4041 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4044 Sets PARSER->SCOPE to the class (TYPE) or namespace
4045 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4046 it unchanged if there is no nested-name-specifier. Returns the new
4047 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4049 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4050 part of a declaration and/or decl-specifier. */
4053 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4054 bool typename_keyword_p,
4055 bool check_dependency_p,
4057 bool is_declaration)
4059 bool success = false;
4060 cp_token_position start = 0;
4063 /* Remember where the nested-name-specifier starts. */
4064 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4066 start = cp_lexer_token_position (parser->lexer, false);
4067 push_deferring_access_checks (dk_deferred);
4074 tree saved_qualifying_scope;
4075 bool template_keyword_p;
4077 /* Spot cases that cannot be the beginning of a
4078 nested-name-specifier. */
4079 token = cp_lexer_peek_token (parser->lexer);
4081 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4082 the already parsed nested-name-specifier. */
4083 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4085 /* Grab the nested-name-specifier and continue the loop. */
4086 cp_parser_pre_parsed_nested_name_specifier (parser);
4087 /* If we originally encountered this nested-name-specifier
4088 with IS_DECLARATION set to false, we will not have
4089 resolved TYPENAME_TYPEs, so we must do so here. */
4091 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4093 new_scope = resolve_typename_type (parser->scope,
4094 /*only_current_p=*/false);
4095 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4096 parser->scope = new_scope;
4102 /* Spot cases that cannot be the beginning of a
4103 nested-name-specifier. On the second and subsequent times
4104 through the loop, we look for the `template' keyword. */
4105 if (success && token->keyword == RID_TEMPLATE)
4107 /* A template-id can start a nested-name-specifier. */
4108 else if (token->type == CPP_TEMPLATE_ID)
4112 /* If the next token is not an identifier, then it is
4113 definitely not a type-name or namespace-name. */
4114 if (token->type != CPP_NAME)
4116 /* If the following token is neither a `<' (to begin a
4117 template-id), nor a `::', then we are not looking at a
4118 nested-name-specifier. */
4119 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4120 if (token->type != CPP_SCOPE
4121 && !cp_parser_nth_token_starts_template_argument_list_p
4126 /* The nested-name-specifier is optional, so we parse
4128 cp_parser_parse_tentatively (parser);
4130 /* Look for the optional `template' keyword, if this isn't the
4131 first time through the loop. */
4133 template_keyword_p = cp_parser_optional_template_keyword (parser);
4135 template_keyword_p = false;
4137 /* Save the old scope since the name lookup we are about to do
4138 might destroy it. */
4139 old_scope = parser->scope;
4140 saved_qualifying_scope = parser->qualifying_scope;
4141 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4142 look up names in "X<T>::I" in order to determine that "Y" is
4143 a template. So, if we have a typename at this point, we make
4144 an effort to look through it. */
4146 && !typename_keyword_p
4148 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4149 parser->scope = resolve_typename_type (parser->scope,
4150 /*only_current_p=*/false);
4151 /* Parse the qualifying entity. */
4153 = cp_parser_qualifying_entity (parser,
4159 /* Look for the `::' token. */
4160 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4162 /* If we found what we wanted, we keep going; otherwise, we're
4164 if (!cp_parser_parse_definitely (parser))
4166 bool error_p = false;
4168 /* Restore the OLD_SCOPE since it was valid before the
4169 failed attempt at finding the last
4170 class-or-namespace-name. */
4171 parser->scope = old_scope;
4172 parser->qualifying_scope = saved_qualifying_scope;
4173 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4175 /* If the next token is an identifier, and the one after
4176 that is a `::', then any valid interpretation would have
4177 found a class-or-namespace-name. */
4178 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4179 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4181 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4184 token = cp_lexer_consume_token (parser->lexer);
4187 if (!token->ambiguous_p)
4190 tree ambiguous_decls;
4192 decl = cp_parser_lookup_name (parser, token->u.value,
4194 /*is_template=*/false,
4195 /*is_namespace=*/false,
4196 /*check_dependency=*/true,
4199 if (TREE_CODE (decl) == TEMPLATE_DECL)
4200 error_at (token->location,
4201 "%qD used without template parameters",
4203 else if (ambiguous_decls)
4205 error_at (token->location,
4206 "reference to %qD is ambiguous",
4208 print_candidates (ambiguous_decls);
4209 decl = error_mark_node;
4213 const char* msg = "is not a class or namespace";
4214 if (cxx_dialect != cxx98)
4215 msg = "is not a class, namespace, or enumeration";
4216 cp_parser_name_lookup_error
4217 (parser, token->u.value, decl, msg,
4221 parser->scope = error_mark_node;
4223 /* Treat this as a successful nested-name-specifier
4228 If the name found is not a class-name (clause
4229 _class_) or namespace-name (_namespace.def_), the
4230 program is ill-formed. */
4233 cp_lexer_consume_token (parser->lexer);
4237 /* We've found one valid nested-name-specifier. */
4239 /* Name lookup always gives us a DECL. */
4240 if (TREE_CODE (new_scope) == TYPE_DECL)
4241 new_scope = TREE_TYPE (new_scope);
4242 /* Uses of "template" must be followed by actual templates. */
4243 if (template_keyword_p
4244 && !(CLASS_TYPE_P (new_scope)
4245 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4246 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4247 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4248 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4249 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4250 == TEMPLATE_ID_EXPR)))
4251 permerror (input_location, TYPE_P (new_scope)
4252 ? "%qT is not a template"
4253 : "%qD is not a template",
4255 /* If it is a class scope, try to complete it; we are about to
4256 be looking up names inside the class. */
4257 if (TYPE_P (new_scope)
4258 /* Since checking types for dependency can be expensive,
4259 avoid doing it if the type is already complete. */
4260 && !COMPLETE_TYPE_P (new_scope)
4261 /* Do not try to complete dependent types. */
4262 && !dependent_type_p (new_scope))
4264 new_scope = complete_type (new_scope);
4265 /* If it is a typedef to current class, use the current
4266 class instead, as the typedef won't have any names inside
4268 if (!COMPLETE_TYPE_P (new_scope)
4269 && currently_open_class (new_scope))
4270 new_scope = TYPE_MAIN_VARIANT (new_scope);
4272 /* Make sure we look in the right scope the next time through
4274 parser->scope = new_scope;
4277 /* If parsing tentatively, replace the sequence of tokens that makes
4278 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4279 token. That way, should we re-parse the token stream, we will
4280 not have to repeat the effort required to do the parse, nor will
4281 we issue duplicate error messages. */
4282 if (success && start)
4286 token = cp_lexer_token_at (parser->lexer, start);
4287 /* Reset the contents of the START token. */
4288 token->type = CPP_NESTED_NAME_SPECIFIER;
4289 /* Retrieve any deferred checks. Do not pop this access checks yet
4290 so the memory will not be reclaimed during token replacing below. */
4291 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4292 token->u.tree_check_value->value = parser->scope;
4293 token->u.tree_check_value->checks = get_deferred_access_checks ();
4294 token->u.tree_check_value->qualifying_scope =
4295 parser->qualifying_scope;
4296 token->keyword = RID_MAX;
4298 /* Purge all subsequent tokens. */
4299 cp_lexer_purge_tokens_after (parser->lexer, start);
4303 pop_to_parent_deferring_access_checks ();
4305 return success ? parser->scope : NULL_TREE;
4308 /* Parse a nested-name-specifier. See
4309 cp_parser_nested_name_specifier_opt for details. This function
4310 behaves identically, except that it will an issue an error if no
4311 nested-name-specifier is present. */
4314 cp_parser_nested_name_specifier (cp_parser *parser,
4315 bool typename_keyword_p,
4316 bool check_dependency_p,
4318 bool is_declaration)
4322 /* Look for the nested-name-specifier. */
4323 scope = cp_parser_nested_name_specifier_opt (parser,
4328 /* If it was not present, issue an error message. */
4331 cp_parser_error (parser, "expected nested-name-specifier");
4332 parser->scope = NULL_TREE;
4338 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4339 this is either a class-name or a namespace-name (which corresponds
4340 to the class-or-namespace-name production in the grammar). For
4341 C++0x, it can also be a type-name that refers to an enumeration
4344 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4345 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4346 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4347 TYPE_P is TRUE iff the next name should be taken as a class-name,
4348 even the same name is declared to be another entity in the same
4351 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4352 specified by the class-or-namespace-name. If neither is found the
4353 ERROR_MARK_NODE is returned. */
4356 cp_parser_qualifying_entity (cp_parser *parser,
4357 bool typename_keyword_p,
4358 bool template_keyword_p,
4359 bool check_dependency_p,
4361 bool is_declaration)
4364 tree saved_qualifying_scope;
4365 tree saved_object_scope;
4368 bool successful_parse_p;
4370 /* Before we try to parse the class-name, we must save away the
4371 current PARSER->SCOPE since cp_parser_class_name will destroy
4373 saved_scope = parser->scope;
4374 saved_qualifying_scope = parser->qualifying_scope;
4375 saved_object_scope = parser->object_scope;
4376 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4377 there is no need to look for a namespace-name. */
4378 only_class_p = template_keyword_p
4379 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4381 cp_parser_parse_tentatively (parser);
4382 scope = cp_parser_class_name (parser,
4385 type_p ? class_type : none_type,
4387 /*class_head_p=*/false,
4389 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4390 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4392 && cxx_dialect != cxx98
4393 && !successful_parse_p)
4395 /* Restore the saved scope. */
4396 parser->scope = saved_scope;
4397 parser->qualifying_scope = saved_qualifying_scope;
4398 parser->object_scope = saved_object_scope;
4400 /* Parse tentatively. */
4401 cp_parser_parse_tentatively (parser);
4403 /* Parse a typedef-name or enum-name. */
4404 scope = cp_parser_nonclass_name (parser);
4405 successful_parse_p = cp_parser_parse_definitely (parser);
4407 /* If that didn't work, try for a namespace-name. */
4408 if (!only_class_p && !successful_parse_p)
4410 /* Restore the saved scope. */
4411 parser->scope = saved_scope;
4412 parser->qualifying_scope = saved_qualifying_scope;
4413 parser->object_scope = saved_object_scope;
4414 /* If we are not looking at an identifier followed by the scope
4415 resolution operator, then this is not part of a
4416 nested-name-specifier. (Note that this function is only used
4417 to parse the components of a nested-name-specifier.) */
4418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4419 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4420 return error_mark_node;
4421 scope = cp_parser_namespace_name (parser);
4427 /* Parse a postfix-expression.
4431 postfix-expression [ expression ]
4432 postfix-expression ( expression-list [opt] )
4433 simple-type-specifier ( expression-list [opt] )
4434 typename :: [opt] nested-name-specifier identifier
4435 ( expression-list [opt] )
4436 typename :: [opt] nested-name-specifier template [opt] template-id
4437 ( expression-list [opt] )
4438 postfix-expression . template [opt] id-expression
4439 postfix-expression -> template [opt] id-expression
4440 postfix-expression . pseudo-destructor-name
4441 postfix-expression -> pseudo-destructor-name
4442 postfix-expression ++
4443 postfix-expression --
4444 dynamic_cast < type-id > ( expression )
4445 static_cast < type-id > ( expression )
4446 reinterpret_cast < type-id > ( expression )
4447 const_cast < type-id > ( expression )
4448 typeid ( expression )
4454 ( type-id ) { initializer-list , [opt] }
4456 This extension is a GNU version of the C99 compound-literal
4457 construct. (The C99 grammar uses `type-name' instead of `type-id',
4458 but they are essentially the same concept.)
4460 If ADDRESS_P is true, the postfix expression is the operand of the
4461 `&' operator. CAST_P is true if this expression is the target of a
4464 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4465 class member access expressions [expr.ref].
4467 Returns a representation of the expression. */
4470 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4471 bool member_access_only_p,
4472 cp_id_kind * pidk_return)
4476 cp_id_kind idk = CP_ID_KIND_NONE;
4477 tree postfix_expression = NULL_TREE;
4478 bool is_member_access = false;
4480 /* Peek at the next token. */
4481 token = cp_lexer_peek_token (parser->lexer);
4482 /* Some of the productions are determined by keywords. */
4483 keyword = token->keyword;
4493 const char *saved_message;
4495 /* All of these can be handled in the same way from the point
4496 of view of parsing. Begin by consuming the token
4497 identifying the cast. */
4498 cp_lexer_consume_token (parser->lexer);
4500 /* New types cannot be defined in the cast. */
4501 saved_message = parser->type_definition_forbidden_message;
4502 parser->type_definition_forbidden_message
4503 = "types may not be defined in casts";
4505 /* Look for the opening `<'. */
4506 cp_parser_require (parser, CPP_LESS, "%<<%>");
4507 /* Parse the type to which we are casting. */
4508 type = cp_parser_type_id (parser);
4509 /* Look for the closing `>'. */
4510 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4511 /* Restore the old message. */
4512 parser->type_definition_forbidden_message = saved_message;
4514 /* And the expression which is being cast. */
4515 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4516 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4517 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4519 /* Only type conversions to integral or enumeration types
4520 can be used in constant-expressions. */
4521 if (!cast_valid_in_integral_constant_expression_p (type)
4522 && (cp_parser_non_integral_constant_expression
4524 "a cast to a type other than an integral or "
4525 "enumeration type")))
4526 return error_mark_node;
4532 = build_dynamic_cast (type, expression, tf_warning_or_error);
4536 = build_static_cast (type, expression, tf_warning_or_error);
4540 = build_reinterpret_cast (type, expression,
4541 tf_warning_or_error);
4545 = build_const_cast (type, expression, tf_warning_or_error);
4556 const char *saved_message;
4557 bool saved_in_type_id_in_expr_p;
4559 /* Consume the `typeid' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561 /* Look for the `(' token. */
4562 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4563 /* Types cannot be defined in a `typeid' expression. */
4564 saved_message = parser->type_definition_forbidden_message;
4565 parser->type_definition_forbidden_message
4566 = "types may not be defined in a %<typeid%> expression";
4567 /* We can't be sure yet whether we're looking at a type-id or an
4569 cp_parser_parse_tentatively (parser);
4570 /* Try a type-id first. */
4571 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4572 parser->in_type_id_in_expr_p = true;
4573 type = cp_parser_type_id (parser);
4574 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4575 /* Look for the `)' token. Otherwise, we can't be sure that
4576 we're not looking at an expression: consider `typeid (int
4577 (3))', for example. */
4578 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4579 /* If all went well, simply lookup the type-id. */
4580 if (cp_parser_parse_definitely (parser))
4581 postfix_expression = get_typeid (type);
4582 /* Otherwise, fall back to the expression variant. */
4587 /* Look for an expression. */
4588 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4589 /* Compute its typeid. */
4590 postfix_expression = build_typeid (expression);
4591 /* Look for the `)' token. */
4592 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4594 /* Restore the saved message. */
4595 parser->type_definition_forbidden_message = saved_message;
4596 /* `typeid' may not appear in an integral constant expression. */
4597 if (cp_parser_non_integral_constant_expression(parser,
4598 "%<typeid%> operator"))
4599 return error_mark_node;
4606 /* The syntax permitted here is the same permitted for an
4607 elaborated-type-specifier. */
4608 type = cp_parser_elaborated_type_specifier (parser,
4609 /*is_friend=*/false,
4610 /*is_declaration=*/false);
4611 postfix_expression = cp_parser_functional_cast (parser, type);
4619 /* If the next thing is a simple-type-specifier, we may be
4620 looking at a functional cast. We could also be looking at
4621 an id-expression. So, we try the functional cast, and if
4622 that doesn't work we fall back to the primary-expression. */
4623 cp_parser_parse_tentatively (parser);
4624 /* Look for the simple-type-specifier. */
4625 type = cp_parser_simple_type_specifier (parser,
4626 /*decl_specs=*/NULL,
4627 CP_PARSER_FLAGS_NONE);
4628 /* Parse the cast itself. */
4629 if (!cp_parser_error_occurred (parser))
4631 = cp_parser_functional_cast (parser, type);
4632 /* If that worked, we're done. */
4633 if (cp_parser_parse_definitely (parser))
4636 /* If the functional-cast didn't work out, try a
4637 compound-literal. */
4638 if (cp_parser_allow_gnu_extensions_p (parser)
4639 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4641 VEC(constructor_elt,gc) *initializer_list = NULL;
4642 bool saved_in_type_id_in_expr_p;
4644 cp_parser_parse_tentatively (parser);
4645 /* Consume the `('. */
4646 cp_lexer_consume_token (parser->lexer);
4647 /* Parse the type. */
4648 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4649 parser->in_type_id_in_expr_p = true;
4650 type = cp_parser_type_id (parser);
4651 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4652 /* Look for the `)'. */
4653 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4654 /* Look for the `{'. */
4655 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4656 /* If things aren't going well, there's no need to
4658 if (!cp_parser_error_occurred (parser))
4660 bool non_constant_p;
4661 /* Parse the initializer-list. */
4663 = cp_parser_initializer_list (parser, &non_constant_p);
4664 /* Allow a trailing `,'. */
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4666 cp_lexer_consume_token (parser->lexer);
4667 /* Look for the final `}'. */
4668 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4670 /* If that worked, we're definitely looking at a
4671 compound-literal expression. */
4672 if (cp_parser_parse_definitely (parser))
4674 /* Warn the user that a compound literal is not
4675 allowed in standard C++. */
4676 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4677 /* For simplicity, we disallow compound literals in
4678 constant-expressions. We could
4679 allow compound literals of integer type, whose
4680 initializer was a constant, in constant
4681 expressions. Permitting that usage, as a further
4682 extension, would not change the meaning of any
4683 currently accepted programs. (Of course, as
4684 compound literals are not part of ISO C++, the
4685 standard has nothing to say.) */
4686 if (cp_parser_non_integral_constant_expression
4687 (parser, "non-constant compound literals"))
4689 postfix_expression = error_mark_node;
4692 /* Form the representation of the compound-literal. */
4694 = (finish_compound_literal
4695 (type, build_constructor (init_list_type_node,
4696 initializer_list)));
4701 /* It must be a primary-expression. */
4703 = cp_parser_primary_expression (parser, address_p, cast_p,
4704 /*template_arg_p=*/false,
4710 /* Keep looping until the postfix-expression is complete. */
4713 if (idk == CP_ID_KIND_UNQUALIFIED
4714 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4715 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4716 /* It is not a Koenig lookup function call. */
4718 = unqualified_name_lookup_error (postfix_expression);
4720 /* Peek at the next token. */
4721 token = cp_lexer_peek_token (parser->lexer);
4723 switch (token->type)
4725 case CPP_OPEN_SQUARE:
4727 = cp_parser_postfix_open_square_expression (parser,
4730 idk = CP_ID_KIND_NONE;
4731 is_member_access = false;
4734 case CPP_OPEN_PAREN:
4735 /* postfix-expression ( expression-list [opt] ) */
4738 bool is_builtin_constant_p;
4739 bool saved_integral_constant_expression_p = false;
4740 bool saved_non_integral_constant_expression_p = false;
4743 is_member_access = false;
4745 is_builtin_constant_p
4746 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4747 if (is_builtin_constant_p)
4749 /* The whole point of __builtin_constant_p is to allow
4750 non-constant expressions to appear as arguments. */
4751 saved_integral_constant_expression_p
4752 = parser->integral_constant_expression_p;
4753 saved_non_integral_constant_expression_p
4754 = parser->non_integral_constant_expression_p;
4755 parser->integral_constant_expression_p = false;
4757 args = (cp_parser_parenthesized_expression_list
4758 (parser, /*is_attribute_list=*/false,
4759 /*cast_p=*/false, /*allow_expansion_p=*/true,
4760 /*non_constant_p=*/NULL));
4761 if (is_builtin_constant_p)
4763 parser->integral_constant_expression_p
4764 = saved_integral_constant_expression_p;
4765 parser->non_integral_constant_expression_p
4766 = saved_non_integral_constant_expression_p;
4771 postfix_expression = error_mark_node;
4775 /* Function calls are not permitted in
4776 constant-expressions. */
4777 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4778 && cp_parser_non_integral_constant_expression (parser,
4781 postfix_expression = error_mark_node;
4782 release_tree_vector (args);
4787 if (idk == CP_ID_KIND_UNQUALIFIED
4788 || idk == CP_ID_KIND_TEMPLATE_ID)
4790 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4792 if (!VEC_empty (tree, args))
4795 if (!any_type_dependent_arguments_p (args))
4797 = perform_koenig_lookup (postfix_expression, args);
4801 = unqualified_fn_lookup_error (postfix_expression);
4803 /* We do not perform argument-dependent lookup if
4804 normal lookup finds a non-function, in accordance
4805 with the expected resolution of DR 218. */
4806 else if (!VEC_empty (tree, args)
4807 && is_overloaded_fn (postfix_expression))
4809 tree fn = get_first_fn (postfix_expression);
4811 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4812 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4814 /* Only do argument dependent lookup if regular
4815 lookup does not find a set of member functions.
4816 [basic.lookup.koenig]/2a */
4817 if (!DECL_FUNCTION_MEMBER_P (fn))
4820 if (!any_type_dependent_arguments_p (args))
4822 = perform_koenig_lookup (postfix_expression, args);
4827 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4829 tree instance = TREE_OPERAND (postfix_expression, 0);
4830 tree fn = TREE_OPERAND (postfix_expression, 1);
4832 if (processing_template_decl
4833 && (type_dependent_expression_p (instance)
4834 || (!BASELINK_P (fn)
4835 && TREE_CODE (fn) != FIELD_DECL)
4836 || type_dependent_expression_p (fn)
4837 || any_type_dependent_arguments_p (args)))
4840 = build_nt_call_vec (postfix_expression, args);
4841 release_tree_vector (args);
4845 if (BASELINK_P (fn))
4848 = (build_new_method_call
4849 (instance, fn, &args, NULL_TREE,
4850 (idk == CP_ID_KIND_QUALIFIED
4851 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4853 tf_warning_or_error));
4857 = finish_call_expr (postfix_expression, &args,
4858 /*disallow_virtual=*/false,
4860 tf_warning_or_error);
4862 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4863 || TREE_CODE (postfix_expression) == MEMBER_REF
4864 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4865 postfix_expression = (build_offset_ref_call_from_tree
4866 (postfix_expression, &args));
4867 else if (idk == CP_ID_KIND_QUALIFIED)
4868 /* A call to a static class member, or a namespace-scope
4871 = finish_call_expr (postfix_expression, &args,
4872 /*disallow_virtual=*/true,
4874 tf_warning_or_error);
4876 /* All other function calls. */
4878 = finish_call_expr (postfix_expression, &args,
4879 /*disallow_virtual=*/false,
4881 tf_warning_or_error);
4883 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4884 idk = CP_ID_KIND_NONE;
4886 release_tree_vector (args);
4892 /* postfix-expression . template [opt] id-expression
4893 postfix-expression . pseudo-destructor-name
4894 postfix-expression -> template [opt] id-expression
4895 postfix-expression -> pseudo-destructor-name */
4897 /* Consume the `.' or `->' operator. */
4898 cp_lexer_consume_token (parser->lexer);
4901 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4906 is_member_access = true;
4910 /* postfix-expression ++ */
4911 /* Consume the `++' token. */
4912 cp_lexer_consume_token (parser->lexer);
4913 /* Generate a representation for the complete expression. */
4915 = finish_increment_expr (postfix_expression,
4916 POSTINCREMENT_EXPR);
4917 /* Increments may not appear in constant-expressions. */
4918 if (cp_parser_non_integral_constant_expression (parser,
4920 postfix_expression = error_mark_node;
4921 idk = CP_ID_KIND_NONE;
4922 is_member_access = false;
4925 case CPP_MINUS_MINUS:
4926 /* postfix-expression -- */
4927 /* Consume the `--' token. */
4928 cp_lexer_consume_token (parser->lexer);
4929 /* Generate a representation for the complete expression. */
4931 = finish_increment_expr (postfix_expression,
4932 POSTDECREMENT_EXPR);
4933 /* Decrements may not appear in constant-expressions. */
4934 if (cp_parser_non_integral_constant_expression (parser,
4936 postfix_expression = error_mark_node;
4937 idk = CP_ID_KIND_NONE;
4938 is_member_access = false;
4942 if (pidk_return != NULL)
4943 * pidk_return = idk;
4944 if (member_access_only_p)
4945 return is_member_access? postfix_expression : error_mark_node;
4947 return postfix_expression;
4951 /* We should never get here. */
4953 return error_mark_node;
4956 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4957 by cp_parser_builtin_offsetof. We're looking for
4959 postfix-expression [ expression ]
4961 FOR_OFFSETOF is set if we're being called in that context, which
4962 changes how we deal with integer constant expressions. */
4965 cp_parser_postfix_open_square_expression (cp_parser *parser,
4966 tree postfix_expression,
4971 /* Consume the `[' token. */
4972 cp_lexer_consume_token (parser->lexer);
4974 /* Parse the index expression. */
4975 /* ??? For offsetof, there is a question of what to allow here. If
4976 offsetof is not being used in an integral constant expression context,
4977 then we *could* get the right answer by computing the value at runtime.
4978 If we are in an integral constant expression context, then we might
4979 could accept any constant expression; hard to say without analysis.
4980 Rather than open the barn door too wide right away, allow only integer
4981 constant expressions here. */
4983 index = cp_parser_constant_expression (parser, false, NULL);
4985 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4987 /* Look for the closing `]'. */
4988 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4990 /* Build the ARRAY_REF. */
4991 postfix_expression = grok_array_decl (postfix_expression, index);
4993 /* When not doing offsetof, array references are not permitted in
4994 constant-expressions. */
4996 && (cp_parser_non_integral_constant_expression
4997 (parser, "an array reference")))
4998 postfix_expression = error_mark_node;
5000 return postfix_expression;
5003 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5004 by cp_parser_builtin_offsetof. We're looking for
5006 postfix-expression . template [opt] id-expression
5007 postfix-expression . pseudo-destructor-name
5008 postfix-expression -> template [opt] id-expression
5009 postfix-expression -> pseudo-destructor-name
5011 FOR_OFFSETOF is set if we're being called in that context. That sorta
5012 limits what of the above we'll actually accept, but nevermind.
5013 TOKEN_TYPE is the "." or "->" token, which will already have been
5014 removed from the stream. */
5017 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5018 enum cpp_ttype token_type,
5019 tree postfix_expression,
5020 bool for_offsetof, cp_id_kind *idk,
5021 location_t location)
5025 bool pseudo_destructor_p;
5026 tree scope = NULL_TREE;
5028 /* If this is a `->' operator, dereference the pointer. */
5029 if (token_type == CPP_DEREF)
5030 postfix_expression = build_x_arrow (postfix_expression);
5031 /* Check to see whether or not the expression is type-dependent. */
5032 dependent_p = type_dependent_expression_p (postfix_expression);
5033 /* The identifier following the `->' or `.' is not qualified. */
5034 parser->scope = NULL_TREE;
5035 parser->qualifying_scope = NULL_TREE;
5036 parser->object_scope = NULL_TREE;
5037 *idk = CP_ID_KIND_NONE;
5039 /* Enter the scope corresponding to the type of the object
5040 given by the POSTFIX_EXPRESSION. */
5041 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5043 scope = TREE_TYPE (postfix_expression);
5044 /* According to the standard, no expression should ever have
5045 reference type. Unfortunately, we do not currently match
5046 the standard in this respect in that our internal representation
5047 of an expression may have reference type even when the standard
5048 says it does not. Therefore, we have to manually obtain the
5049 underlying type here. */
5050 scope = non_reference (scope);
5051 /* The type of the POSTFIX_EXPRESSION must be complete. */
5052 if (scope == unknown_type_node)
5054 error_at (location, "%qE does not have class type",
5055 postfix_expression);
5059 scope = complete_type_or_else (scope, NULL_TREE);
5060 /* Let the name lookup machinery know that we are processing a
5061 class member access expression. */
5062 parser->context->object_type = scope;
5063 /* If something went wrong, we want to be able to discern that case,
5064 as opposed to the case where there was no SCOPE due to the type
5065 of expression being dependent. */
5067 scope = error_mark_node;
5068 /* If the SCOPE was erroneous, make the various semantic analysis
5069 functions exit quickly -- and without issuing additional error
5071 if (scope == error_mark_node)
5072 postfix_expression = error_mark_node;
5075 /* Assume this expression is not a pseudo-destructor access. */
5076 pseudo_destructor_p = false;
5078 /* If the SCOPE is a scalar type, then, if this is a valid program,
5079 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5080 is type dependent, it can be pseudo-destructor-name or something else.
5081 Try to parse it as pseudo-destructor-name first. */
5082 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5087 cp_parser_parse_tentatively (parser);
5088 /* Parse the pseudo-destructor-name. */
5090 cp_parser_pseudo_destructor_name (parser, &s, &type);
5092 && (cp_parser_error_occurred (parser)
5093 || TREE_CODE (type) != TYPE_DECL
5094 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5095 cp_parser_abort_tentative_parse (parser);
5096 else if (cp_parser_parse_definitely (parser))
5098 pseudo_destructor_p = true;
5100 = finish_pseudo_destructor_expr (postfix_expression,
5101 s, TREE_TYPE (type));
5105 if (!pseudo_destructor_p)
5107 /* If the SCOPE is not a scalar type, we are looking at an
5108 ordinary class member access expression, rather than a
5109 pseudo-destructor-name. */
5111 cp_token *token = cp_lexer_peek_token (parser->lexer);
5112 /* Parse the id-expression. */
5113 name = (cp_parser_id_expression
5115 cp_parser_optional_template_keyword (parser),
5116 /*check_dependency_p=*/true,
5118 /*declarator_p=*/false,
5119 /*optional_p=*/false));
5120 /* In general, build a SCOPE_REF if the member name is qualified.
5121 However, if the name was not dependent and has already been
5122 resolved; there is no need to build the SCOPE_REF. For example;
5124 struct X { void f(); };
5125 template <typename T> void f(T* t) { t->X::f(); }
5127 Even though "t" is dependent, "X::f" is not and has been resolved
5128 to a BASELINK; there is no need to include scope information. */
5130 /* But we do need to remember that there was an explicit scope for
5131 virtual function calls. */
5133 *idk = CP_ID_KIND_QUALIFIED;
5135 /* If the name is a template-id that names a type, we will get a
5136 TYPE_DECL here. That is invalid code. */
5137 if (TREE_CODE (name) == TYPE_DECL)
5139 error_at (token->location, "invalid use of %qD", name);
5140 postfix_expression = error_mark_node;
5144 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5146 name = build_qualified_name (/*type=*/NULL_TREE,
5150 parser->scope = NULL_TREE;
5151 parser->qualifying_scope = NULL_TREE;
5152 parser->object_scope = NULL_TREE;
5154 if (scope && name && BASELINK_P (name))
5155 adjust_result_of_qualified_name_lookup
5156 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5158 = finish_class_member_access_expr (postfix_expression, name,
5160 tf_warning_or_error);
5164 /* We no longer need to look up names in the scope of the object on
5165 the left-hand side of the `.' or `->' operator. */
5166 parser->context->object_type = NULL_TREE;
5168 /* Outside of offsetof, these operators may not appear in
5169 constant-expressions. */
5171 && (cp_parser_non_integral_constant_expression
5172 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5173 postfix_expression = error_mark_node;
5175 return postfix_expression;
5178 /* Parse a parenthesized expression-list.
5181 assignment-expression
5182 expression-list, assignment-expression
5187 identifier, expression-list
5189 CAST_P is true if this expression is the target of a cast.
5191 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5194 Returns a vector of trees. Each element is a representation of an
5195 assignment-expression. NULL is returned if the ( and or ) are
5196 missing. An empty, but allocated, vector is returned on no
5197 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5198 if this is really an attribute list being parsed. If
5199 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5200 not all of the expressions in the list were constant. */
5202 static VEC(tree,gc) *
5203 cp_parser_parenthesized_expression_list (cp_parser* parser,
5204 bool is_attribute_list,
5206 bool allow_expansion_p,
5207 bool *non_constant_p)
5209 VEC(tree,gc) *expression_list;
5210 bool fold_expr_p = is_attribute_list;
5211 tree identifier = NULL_TREE;
5212 bool saved_greater_than_is_operator_p;
5214 /* Assume all the expressions will be constant. */
5216 *non_constant_p = false;
5218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5221 expression_list = make_tree_vector ();
5223 /* Within a parenthesized expression, a `>' token is always
5224 the greater-than operator. */
5225 saved_greater_than_is_operator_p
5226 = parser->greater_than_is_operator_p;
5227 parser->greater_than_is_operator_p = true;
5229 /* Consume expressions until there are no more. */
5230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5235 /* At the beginning of attribute lists, check to see if the
5236 next token is an identifier. */
5237 if (is_attribute_list
5238 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5242 /* Consume the identifier. */
5243 token = cp_lexer_consume_token (parser->lexer);
5244 /* Save the identifier. */
5245 identifier = token->u.value;
5249 bool expr_non_constant_p;
5251 /* Parse the next assignment-expression. */
5252 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5254 /* A braced-init-list. */
5255 maybe_warn_cpp0x ("extended initializer lists");
5256 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5257 if (non_constant_p && expr_non_constant_p)
5258 *non_constant_p = true;
5260 else if (non_constant_p)
5262 expr = (cp_parser_constant_expression
5263 (parser, /*allow_non_constant_p=*/true,
5264 &expr_non_constant_p));
5265 if (expr_non_constant_p)
5266 *non_constant_p = true;
5269 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5272 expr = fold_non_dependent_expr (expr);
5274 /* If we have an ellipsis, then this is an expression
5276 if (allow_expansion_p
5277 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5279 /* Consume the `...'. */
5280 cp_lexer_consume_token (parser->lexer);
5282 /* Build the argument pack. */
5283 expr = make_pack_expansion (expr);
5286 /* Add it to the list. We add error_mark_node
5287 expressions to the list, so that we can still tell if
5288 the correct form for a parenthesized expression-list
5289 is found. That gives better errors. */
5290 VEC_safe_push (tree, gc, expression_list, expr);
5292 if (expr == error_mark_node)
5296 /* After the first item, attribute lists look the same as
5297 expression lists. */
5298 is_attribute_list = false;
5301 /* If the next token isn't a `,', then we are done. */
5302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5305 /* Otherwise, consume the `,' and keep going. */
5306 cp_lexer_consume_token (parser->lexer);
5309 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5314 /* We try and resync to an unnested comma, as that will give the
5315 user better diagnostics. */
5316 ending = cp_parser_skip_to_closing_parenthesis (parser,
5317 /*recovering=*/true,
5319 /*consume_paren=*/true);
5324 parser->greater_than_is_operator_p
5325 = saved_greater_than_is_operator_p;
5330 parser->greater_than_is_operator_p
5331 = saved_greater_than_is_operator_p;
5334 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5336 return expression_list;
5339 /* Parse a pseudo-destructor-name.
5341 pseudo-destructor-name:
5342 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5343 :: [opt] nested-name-specifier template template-id :: ~ type-name
5344 :: [opt] nested-name-specifier [opt] ~ type-name
5346 If either of the first two productions is used, sets *SCOPE to the
5347 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5348 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5349 or ERROR_MARK_NODE if the parse fails. */
5352 cp_parser_pseudo_destructor_name (cp_parser* parser,
5356 bool nested_name_specifier_p;
5358 /* Assume that things will not work out. */
5359 *type = error_mark_node;
5361 /* Look for the optional `::' operator. */
5362 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5363 /* Look for the optional nested-name-specifier. */
5364 nested_name_specifier_p
5365 = (cp_parser_nested_name_specifier_opt (parser,
5366 /*typename_keyword_p=*/false,
5367 /*check_dependency_p=*/true,
5369 /*is_declaration=*/false)
5371 /* Now, if we saw a nested-name-specifier, we might be doing the
5372 second production. */
5373 if (nested_name_specifier_p
5374 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5376 /* Consume the `template' keyword. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* Parse the template-id. */
5379 cp_parser_template_id (parser,
5380 /*template_keyword_p=*/true,
5381 /*check_dependency_p=*/false,
5382 /*is_declaration=*/true);
5383 /* Look for the `::' token. */
5384 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5386 /* If the next token is not a `~', then there might be some
5387 additional qualification. */
5388 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5390 /* At this point, we're looking for "type-name :: ~". The type-name
5391 must not be a class-name, since this is a pseudo-destructor. So,
5392 it must be either an enum-name, or a typedef-name -- both of which
5393 are just identifiers. So, we peek ahead to check that the "::"
5394 and "~" tokens are present; if they are not, then we can avoid
5395 calling type_name. */
5396 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5397 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5398 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5400 cp_parser_error (parser, "non-scalar type");
5404 /* Look for the type-name. */
5405 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5406 if (*scope == error_mark_node)
5409 /* Look for the `::' token. */
5410 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5415 /* Look for the `~'. */
5416 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5417 /* Look for the type-name again. We are not responsible for
5418 checking that it matches the first type-name. */
5419 *type = cp_parser_nonclass_name (parser);
5422 /* Parse a unary-expression.
5428 unary-operator cast-expression
5429 sizeof unary-expression
5437 __extension__ cast-expression
5438 __alignof__ unary-expression
5439 __alignof__ ( type-id )
5440 __real__ cast-expression
5441 __imag__ cast-expression
5444 ADDRESS_P is true iff the unary-expression is appearing as the
5445 operand of the `&' operator. CAST_P is true if this expression is
5446 the target of a cast.
5448 Returns a representation of the expression. */
5451 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5455 enum tree_code unary_operator;
5457 /* Peek at the next token. */
5458 token = cp_lexer_peek_token (parser->lexer);
5459 /* Some keywords give away the kind of expression. */
5460 if (token->type == CPP_KEYWORD)
5462 enum rid keyword = token->keyword;
5472 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5473 /* Consume the token. */
5474 cp_lexer_consume_token (parser->lexer);
5475 /* Parse the operand. */
5476 operand = cp_parser_sizeof_operand (parser, keyword);
5478 if (TYPE_P (operand))
5479 return cxx_sizeof_or_alignof_type (operand, op, true);
5481 return cxx_sizeof_or_alignof_expr (operand, op, true);
5485 return cp_parser_new_expression (parser);
5488 return cp_parser_delete_expression (parser);
5492 /* The saved value of the PEDANTIC flag. */
5496 /* Save away the PEDANTIC flag. */
5497 cp_parser_extension_opt (parser, &saved_pedantic);
5498 /* Parse the cast-expression. */
5499 expr = cp_parser_simple_cast_expression (parser);
5500 /* Restore the PEDANTIC flag. */
5501 pedantic = saved_pedantic;
5511 /* Consume the `__real__' or `__imag__' token. */
5512 cp_lexer_consume_token (parser->lexer);
5513 /* Parse the cast-expression. */
5514 expression = cp_parser_simple_cast_expression (parser);
5515 /* Create the complete representation. */
5516 return build_x_unary_op ((keyword == RID_REALPART
5517 ? REALPART_EXPR : IMAGPART_EXPR),
5519 tf_warning_or_error);
5528 /* Look for the `:: new' and `:: delete', which also signal the
5529 beginning of a new-expression, or delete-expression,
5530 respectively. If the next token is `::', then it might be one of
5532 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5536 /* See if the token after the `::' is one of the keywords in
5537 which we're interested. */
5538 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5539 /* If it's `new', we have a new-expression. */
5540 if (keyword == RID_NEW)
5541 return cp_parser_new_expression (parser);
5542 /* Similarly, for `delete'. */
5543 else if (keyword == RID_DELETE)
5544 return cp_parser_delete_expression (parser);
5547 /* Look for a unary operator. */
5548 unary_operator = cp_parser_unary_operator (token);
5549 /* The `++' and `--' operators can be handled similarly, even though
5550 they are not technically unary-operators in the grammar. */
5551 if (unary_operator == ERROR_MARK)
5553 if (token->type == CPP_PLUS_PLUS)
5554 unary_operator = PREINCREMENT_EXPR;
5555 else if (token->type == CPP_MINUS_MINUS)
5556 unary_operator = PREDECREMENT_EXPR;
5557 /* Handle the GNU address-of-label extension. */
5558 else if (cp_parser_allow_gnu_extensions_p (parser)
5559 && token->type == CPP_AND_AND)
5563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5565 /* Consume the '&&' token. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Look for the identifier. */
5568 identifier = cp_parser_identifier (parser);
5569 /* Create an expression representing the address. */
5570 expression = finish_label_address_expr (identifier, loc);
5571 if (cp_parser_non_integral_constant_expression (parser,
5572 "the address of a label"))
5573 expression = error_mark_node;
5577 if (unary_operator != ERROR_MARK)
5579 tree cast_expression;
5580 tree expression = error_mark_node;
5581 const char *non_constant_p = NULL;
5583 /* Consume the operator token. */
5584 token = cp_lexer_consume_token (parser->lexer);
5585 /* Parse the cast-expression. */
5587 = cp_parser_cast_expression (parser,
5588 unary_operator == ADDR_EXPR,
5589 /*cast_p=*/false, pidk);
5590 /* Now, build an appropriate representation. */
5591 switch (unary_operator)
5594 non_constant_p = "%<*%>";
5595 expression = build_x_indirect_ref (cast_expression, "unary *",
5596 tf_warning_or_error);
5600 non_constant_p = "%<&%>";
5603 expression = build_x_unary_op (unary_operator, cast_expression,
5604 tf_warning_or_error);
5607 case PREINCREMENT_EXPR:
5608 case PREDECREMENT_EXPR:
5609 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5610 ? "%<++%>" : "%<--%>");
5612 case UNARY_PLUS_EXPR:
5614 case TRUTH_NOT_EXPR:
5615 expression = finish_unary_op_expr (unary_operator, cast_expression);
5623 && cp_parser_non_integral_constant_expression (parser,
5625 expression = error_mark_node;
5630 return cp_parser_postfix_expression (parser, address_p, cast_p,
5631 /*member_access_only_p=*/false,
5635 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5636 unary-operator, the corresponding tree code is returned. */
5638 static enum tree_code
5639 cp_parser_unary_operator (cp_token* token)
5641 switch (token->type)
5644 return INDIRECT_REF;
5650 return UNARY_PLUS_EXPR;
5656 return TRUTH_NOT_EXPR;
5659 return BIT_NOT_EXPR;
5666 /* Parse a new-expression.
5669 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5670 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5672 Returns a representation of the expression. */
5675 cp_parser_new_expression (cp_parser* parser)
5677 bool global_scope_p;
5678 VEC(tree,gc) *placement;
5680 VEC(tree,gc) *initializer;
5684 /* Look for the optional `::' operator. */
5686 = (cp_parser_global_scope_opt (parser,
5687 /*current_scope_valid_p=*/false)
5689 /* Look for the `new' operator. */
5690 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5691 /* There's no easy way to tell a new-placement from the
5692 `( type-id )' construct. */
5693 cp_parser_parse_tentatively (parser);
5694 /* Look for a new-placement. */
5695 placement = cp_parser_new_placement (parser);
5696 /* If that didn't work out, there's no new-placement. */
5697 if (!cp_parser_parse_definitely (parser))
5699 if (placement != NULL)
5700 release_tree_vector (placement);
5704 /* If the next token is a `(', then we have a parenthesized
5706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5709 /* Consume the `('. */
5710 cp_lexer_consume_token (parser->lexer);
5711 /* Parse the type-id. */
5712 type = cp_parser_type_id (parser);
5713 /* Look for the closing `)'. */
5714 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5715 token = cp_lexer_peek_token (parser->lexer);
5716 /* There should not be a direct-new-declarator in this production,
5717 but GCC used to allowed this, so we check and emit a sensible error
5718 message for this case. */
5719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5721 error_at (token->location,
5722 "array bound forbidden after parenthesized type-id");
5723 inform (token->location,
5724 "try removing the parentheses around the type-id");
5725 cp_parser_direct_new_declarator (parser);
5729 /* Otherwise, there must be a new-type-id. */
5731 type = cp_parser_new_type_id (parser, &nelts);
5733 /* If the next token is a `(' or '{', then we have a new-initializer. */
5734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5735 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5736 initializer = cp_parser_new_initializer (parser);
5740 /* A new-expression may not appear in an integral constant
5742 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5743 ret = error_mark_node;
5746 /* Create a representation of the new-expression. */
5747 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5748 tf_warning_or_error);
5751 if (placement != NULL)
5752 release_tree_vector (placement);
5753 if (initializer != NULL)
5754 release_tree_vector (initializer);
5759 /* Parse a new-placement.
5764 Returns the same representation as for an expression-list. */
5766 static VEC(tree,gc) *
5767 cp_parser_new_placement (cp_parser* parser)
5769 VEC(tree,gc) *expression_list;
5771 /* Parse the expression-list. */
5772 expression_list = (cp_parser_parenthesized_expression_list
5773 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5774 /*non_constant_p=*/NULL));
5776 return expression_list;
5779 /* Parse a new-type-id.
5782 type-specifier-seq new-declarator [opt]
5784 Returns the TYPE allocated. If the new-type-id indicates an array
5785 type, *NELTS is set to the number of elements in the last array
5786 bound; the TYPE will not include the last array bound. */
5789 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5791 cp_decl_specifier_seq type_specifier_seq;
5792 cp_declarator *new_declarator;
5793 cp_declarator *declarator;
5794 cp_declarator *outer_declarator;
5795 const char *saved_message;
5798 /* The type-specifier sequence must not contain type definitions.
5799 (It cannot contain declarations of new types either, but if they
5800 are not definitions we will catch that because they are not
5802 saved_message = parser->type_definition_forbidden_message;
5803 parser->type_definition_forbidden_message
5804 = "types may not be defined in a new-type-id";
5805 /* Parse the type-specifier-seq. */
5806 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5807 /*is_trailing_return=*/false,
5808 &type_specifier_seq);
5809 /* Restore the old message. */
5810 parser->type_definition_forbidden_message = saved_message;
5811 /* Parse the new-declarator. */
5812 new_declarator = cp_parser_new_declarator_opt (parser);
5814 /* Determine the number of elements in the last array dimension, if
5817 /* Skip down to the last array dimension. */
5818 declarator = new_declarator;
5819 outer_declarator = NULL;
5820 while (declarator && (declarator->kind == cdk_pointer
5821 || declarator->kind == cdk_ptrmem))
5823 outer_declarator = declarator;
5824 declarator = declarator->declarator;
5827 && declarator->kind == cdk_array
5828 && declarator->declarator
5829 && declarator->declarator->kind == cdk_array)
5831 outer_declarator = declarator;
5832 declarator = declarator->declarator;
5835 if (declarator && declarator->kind == cdk_array)
5837 *nelts = declarator->u.array.bounds;
5838 if (*nelts == error_mark_node)
5839 *nelts = integer_one_node;
5841 if (outer_declarator)
5842 outer_declarator->declarator = declarator->declarator;
5844 new_declarator = NULL;
5847 type = groktypename (&type_specifier_seq, new_declarator, false);
5851 /* Parse an (optional) new-declarator.
5854 ptr-operator new-declarator [opt]
5855 direct-new-declarator
5857 Returns the declarator. */
5859 static cp_declarator *
5860 cp_parser_new_declarator_opt (cp_parser* parser)
5862 enum tree_code code;
5864 cp_cv_quals cv_quals;
5866 /* We don't know if there's a ptr-operator next, or not. */
5867 cp_parser_parse_tentatively (parser);
5868 /* Look for a ptr-operator. */
5869 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5870 /* If that worked, look for more new-declarators. */
5871 if (cp_parser_parse_definitely (parser))
5873 cp_declarator *declarator;
5875 /* Parse another optional declarator. */
5876 declarator = cp_parser_new_declarator_opt (parser);
5878 return cp_parser_make_indirect_declarator
5879 (code, type, cv_quals, declarator);
5882 /* If the next token is a `[', there is a direct-new-declarator. */
5883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5884 return cp_parser_direct_new_declarator (parser);
5889 /* Parse a direct-new-declarator.
5891 direct-new-declarator:
5893 direct-new-declarator [constant-expression]
5897 static cp_declarator *
5898 cp_parser_direct_new_declarator (cp_parser* parser)
5900 cp_declarator *declarator = NULL;
5906 /* Look for the opening `['. */
5907 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5908 /* The first expression is not required to be constant. */
5911 cp_token *token = cp_lexer_peek_token (parser->lexer);
5912 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5913 /* The standard requires that the expression have integral
5914 type. DR 74 adds enumeration types. We believe that the
5915 real intent is that these expressions be handled like the
5916 expression in a `switch' condition, which also allows
5917 classes with a single conversion to integral or
5918 enumeration type. */
5919 if (!processing_template_decl)
5922 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5927 error_at (token->location,
5928 "expression in new-declarator must have integral "
5929 "or enumeration type");
5930 expression = error_mark_node;
5934 /* But all the other expressions must be. */
5937 = cp_parser_constant_expression (parser,
5938 /*allow_non_constant=*/false,
5940 /* Look for the closing `]'. */
5941 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5943 /* Add this bound to the declarator. */
5944 declarator = make_array_declarator (declarator, expression);
5946 /* If the next token is not a `[', then there are no more
5948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5955 /* Parse a new-initializer.
5958 ( expression-list [opt] )
5961 Returns a representation of the expression-list. */
5963 static VEC(tree,gc) *
5964 cp_parser_new_initializer (cp_parser* parser)
5966 VEC(tree,gc) *expression_list;
5968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5971 bool expr_non_constant_p;
5972 maybe_warn_cpp0x ("extended initializer lists");
5973 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5974 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5975 expression_list = make_tree_vector_single (t);
5978 expression_list = (cp_parser_parenthesized_expression_list
5979 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5980 /*non_constant_p=*/NULL));
5982 return expression_list;
5985 /* Parse a delete-expression.
5988 :: [opt] delete cast-expression
5989 :: [opt] delete [ ] cast-expression
5991 Returns a representation of the expression. */
5994 cp_parser_delete_expression (cp_parser* parser)
5996 bool global_scope_p;
6000 /* Look for the optional `::' operator. */
6002 = (cp_parser_global_scope_opt (parser,
6003 /*current_scope_valid_p=*/false)
6005 /* Look for the `delete' keyword. */
6006 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6007 /* See if the array syntax is in use. */
6008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6010 /* Consume the `[' token. */
6011 cp_lexer_consume_token (parser->lexer);
6012 /* Look for the `]' token. */
6013 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6014 /* Remember that this is the `[]' construct. */
6020 /* Parse the cast-expression. */
6021 expression = cp_parser_simple_cast_expression (parser);
6023 /* A delete-expression may not appear in an integral constant
6025 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6026 return error_mark_node;
6028 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6031 /* Returns true if TOKEN may start a cast-expression and false
6035 cp_parser_token_starts_cast_expression (cp_token *token)
6037 switch (token->type)
6043 case CPP_CLOSE_SQUARE:
6044 case CPP_CLOSE_PAREN:
6045 case CPP_CLOSE_BRACE:
6049 case CPP_DEREF_STAR:
6057 case CPP_GREATER_EQ:
6077 /* '[' may start a primary-expression in obj-c++. */
6078 case CPP_OPEN_SQUARE:
6079 return c_dialect_objc ();
6086 /* Parse a cast-expression.
6090 ( type-id ) cast-expression
6092 ADDRESS_P is true iff the unary-expression is appearing as the
6093 operand of the `&' operator. CAST_P is true if this expression is
6094 the target of a cast.
6096 Returns a representation of the expression. */
6099 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6102 /* If it's a `(', then we might be looking at a cast. */
6103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6105 tree type = NULL_TREE;
6106 tree expr = NULL_TREE;
6107 bool compound_literal_p;
6108 const char *saved_message;
6110 /* There's no way to know yet whether or not this is a cast.
6111 For example, `(int (3))' is a unary-expression, while `(int)
6112 3' is a cast. So, we resort to parsing tentatively. */
6113 cp_parser_parse_tentatively (parser);
6114 /* Types may not be defined in a cast. */
6115 saved_message = parser->type_definition_forbidden_message;
6116 parser->type_definition_forbidden_message
6117 = "types may not be defined in casts";
6118 /* Consume the `('. */
6119 cp_lexer_consume_token (parser->lexer);
6120 /* A very tricky bit is that `(struct S) { 3 }' is a
6121 compound-literal (which we permit in C++ as an extension).
6122 But, that construct is not a cast-expression -- it is a
6123 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6124 is legal; if the compound-literal were a cast-expression,
6125 you'd need an extra set of parentheses.) But, if we parse
6126 the type-id, and it happens to be a class-specifier, then we
6127 will commit to the parse at that point, because we cannot
6128 undo the action that is done when creating a new class. So,
6129 then we cannot back up and do a postfix-expression.
6131 Therefore, we scan ahead to the closing `)', and check to see
6132 if the token after the `)' is a `{'. If so, we are not
6133 looking at a cast-expression.
6135 Save tokens so that we can put them back. */
6136 cp_lexer_save_tokens (parser->lexer);
6137 /* Skip tokens until the next token is a closing parenthesis.
6138 If we find the closing `)', and the next token is a `{', then
6139 we are looking at a compound-literal. */
6141 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6142 /*consume_paren=*/true)
6143 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6144 /* Roll back the tokens we skipped. */
6145 cp_lexer_rollback_tokens (parser->lexer);
6146 /* If we were looking at a compound-literal, simulate an error
6147 so that the call to cp_parser_parse_definitely below will
6149 if (compound_literal_p)
6150 cp_parser_simulate_error (parser);
6153 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6154 parser->in_type_id_in_expr_p = true;
6155 /* Look for the type-id. */
6156 type = cp_parser_type_id (parser);
6157 /* Look for the closing `)'. */
6158 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6159 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6162 /* Restore the saved message. */
6163 parser->type_definition_forbidden_message = saved_message;
6165 /* At this point this can only be either a cast or a
6166 parenthesized ctor such as `(T ())' that looks like a cast to
6167 function returning T. */
6168 if (!cp_parser_error_occurred (parser)
6169 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6172 cp_parser_parse_definitely (parser);
6173 expr = cp_parser_cast_expression (parser,
6174 /*address_p=*/false,
6175 /*cast_p=*/true, pidk);
6177 /* Warn about old-style casts, if so requested. */
6178 if (warn_old_style_cast
6179 && !in_system_header
6180 && !VOID_TYPE_P (type)
6181 && current_lang_name != lang_name_c)
6182 warning (OPT_Wold_style_cast, "use of old-style cast");
6184 /* Only type conversions to integral or enumeration types
6185 can be used in constant-expressions. */
6186 if (!cast_valid_in_integral_constant_expression_p (type)
6187 && (cp_parser_non_integral_constant_expression
6189 "a cast to a type other than an integral or "
6190 "enumeration type")))
6191 return error_mark_node;
6193 /* Perform the cast. */
6194 expr = build_c_cast (input_location, type, expr);
6198 cp_parser_abort_tentative_parse (parser);
6201 /* If we get here, then it's not a cast, so it must be a
6202 unary-expression. */
6203 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6206 /* Parse a binary expression of the general form:
6210 pm-expression .* cast-expression
6211 pm-expression ->* cast-expression
6213 multiplicative-expression:
6215 multiplicative-expression * pm-expression
6216 multiplicative-expression / pm-expression
6217 multiplicative-expression % pm-expression
6219 additive-expression:
6220 multiplicative-expression
6221 additive-expression + multiplicative-expression
6222 additive-expression - multiplicative-expression
6226 shift-expression << additive-expression
6227 shift-expression >> additive-expression
6229 relational-expression:
6231 relational-expression < shift-expression
6232 relational-expression > shift-expression
6233 relational-expression <= shift-expression
6234 relational-expression >= shift-expression
6238 relational-expression:
6239 relational-expression <? shift-expression
6240 relational-expression >? shift-expression
6242 equality-expression:
6243 relational-expression
6244 equality-expression == relational-expression
6245 equality-expression != relational-expression
6249 and-expression & equality-expression
6251 exclusive-or-expression:
6253 exclusive-or-expression ^ and-expression
6255 inclusive-or-expression:
6256 exclusive-or-expression
6257 inclusive-or-expression | exclusive-or-expression
6259 logical-and-expression:
6260 inclusive-or-expression
6261 logical-and-expression && inclusive-or-expression
6263 logical-or-expression:
6264 logical-and-expression
6265 logical-or-expression || logical-and-expression
6267 All these are implemented with a single function like:
6270 simple-cast-expression
6271 binary-expression <token> binary-expression
6273 CAST_P is true if this expression is the target of a cast.
6275 The binops_by_token map is used to get the tree codes for each <token> type.
6276 binary-expressions are associated according to a precedence table. */
6278 #define TOKEN_PRECEDENCE(token) \
6279 (((token->type == CPP_GREATER \
6280 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6281 && !parser->greater_than_is_operator_p) \
6282 ? PREC_NOT_OPERATOR \
6283 : binops_by_token[token->type].prec)
6286 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6287 bool no_toplevel_fold_p,
6288 enum cp_parser_prec prec,
6291 cp_parser_expression_stack stack;
6292 cp_parser_expression_stack_entry *sp = &stack[0];
6295 enum tree_code tree_type, lhs_type, rhs_type;
6296 enum cp_parser_prec new_prec, lookahead_prec;
6299 /* Parse the first expression. */
6300 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6301 lhs_type = ERROR_MARK;
6305 /* Get an operator token. */
6306 token = cp_lexer_peek_token (parser->lexer);
6308 if (warn_cxx0x_compat
6309 && token->type == CPP_RSHIFT
6310 && !parser->greater_than_is_operator_p)
6312 if (warning_at (token->location, OPT_Wc__0x_compat,
6313 "%<>>%> operator will be treated as"
6314 " two right angle brackets in C++0x"))
6315 inform (token->location,
6316 "suggest parentheses around %<>>%> expression");
6319 new_prec = TOKEN_PRECEDENCE (token);
6321 /* Popping an entry off the stack means we completed a subexpression:
6322 - either we found a token which is not an operator (`>' where it is not
6323 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6324 will happen repeatedly;
6325 - or, we found an operator which has lower priority. This is the case
6326 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6328 if (new_prec <= prec)
6337 tree_type = binops_by_token[token->type].tree_type;
6339 /* We used the operator token. */
6340 cp_lexer_consume_token (parser->lexer);
6342 /* For "false && x" or "true || x", x will never be executed;
6343 disable warnings while evaluating it. */
6344 if (tree_type == TRUTH_ANDIF_EXPR)
6345 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6346 else if (tree_type == TRUTH_ORIF_EXPR)
6347 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6349 /* Extract another operand. It may be the RHS of this expression
6350 or the LHS of a new, higher priority expression. */
6351 rhs = cp_parser_simple_cast_expression (parser);
6352 rhs_type = ERROR_MARK;
6354 /* Get another operator token. Look up its precedence to avoid
6355 building a useless (immediately popped) stack entry for common
6356 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6357 token = cp_lexer_peek_token (parser->lexer);
6358 lookahead_prec = TOKEN_PRECEDENCE (token);
6359 if (lookahead_prec > new_prec)
6361 /* ... and prepare to parse the RHS of the new, higher priority
6362 expression. Since precedence levels on the stack are
6363 monotonically increasing, we do not have to care about
6366 sp->tree_type = tree_type;
6368 sp->lhs_type = lhs_type;
6371 lhs_type = rhs_type;
6373 new_prec = lookahead_prec;
6377 lookahead_prec = new_prec;
6378 /* If the stack is not empty, we have parsed into LHS the right side
6379 (`4' in the example above) of an expression we had suspended.
6380 We can use the information on the stack to recover the LHS (`3')
6381 from the stack together with the tree code (`MULT_EXPR'), and
6382 the precedence of the higher level subexpression
6383 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6384 which will be used to actually build the additive expression. */
6387 tree_type = sp->tree_type;
6389 rhs_type = lhs_type;
6391 lhs_type = sp->lhs_type;
6394 /* Undo the disabling of warnings done above. */
6395 if (tree_type == TRUTH_ANDIF_EXPR)
6396 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6397 else if (tree_type == TRUTH_ORIF_EXPR)
6398 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6400 overloaded_p = false;
6401 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6402 ERROR_MARK for everything that is not a binary expression.
6403 This makes warn_about_parentheses miss some warnings that
6404 involve unary operators. For unary expressions we should
6405 pass the correct tree_code unless the unary expression was
6406 surrounded by parentheses.
6408 if (no_toplevel_fold_p
6409 && lookahead_prec <= prec
6411 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6412 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6414 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6415 &overloaded_p, tf_warning_or_error);
6416 lhs_type = tree_type;
6418 /* If the binary operator required the use of an overloaded operator,
6419 then this expression cannot be an integral constant-expression.
6420 An overloaded operator can be used even if both operands are
6421 otherwise permissible in an integral constant-expression if at
6422 least one of the operands is of enumeration type. */
6425 && (cp_parser_non_integral_constant_expression
6426 (parser, "calls to overloaded operators")))
6427 return error_mark_node;
6434 /* Parse the `? expression : assignment-expression' part of a
6435 conditional-expression. The LOGICAL_OR_EXPR is the
6436 logical-or-expression that started the conditional-expression.
6437 Returns a representation of the entire conditional-expression.
6439 This routine is used by cp_parser_assignment_expression.
6441 ? expression : assignment-expression
6445 ? : assignment-expression */
6448 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6451 tree assignment_expr;
6453 /* Consume the `?' token. */
6454 cp_lexer_consume_token (parser->lexer);
6455 if (cp_parser_allow_gnu_extensions_p (parser)
6456 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6458 /* Implicit true clause. */
6460 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6464 /* Parse the expression. */
6465 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6466 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6467 c_inhibit_evaluation_warnings +=
6468 ((logical_or_expr == truthvalue_true_node)
6469 - (logical_or_expr == truthvalue_false_node));
6472 /* The next token should be a `:'. */
6473 cp_parser_require (parser, CPP_COLON, "%<:%>");
6474 /* Parse the assignment-expression. */
6475 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6476 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6478 /* Build the conditional-expression. */
6479 return build_x_conditional_expr (logical_or_expr,
6482 tf_warning_or_error);
6485 /* Parse an assignment-expression.
6487 assignment-expression:
6488 conditional-expression
6489 logical-or-expression assignment-operator assignment_expression
6492 CAST_P is true if this expression is the target of a cast.
6494 Returns a representation for the expression. */
6497 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6502 /* If the next token is the `throw' keyword, then we're looking at
6503 a throw-expression. */
6504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6505 expr = cp_parser_throw_expression (parser);
6506 /* Otherwise, it must be that we are looking at a
6507 logical-or-expression. */
6510 /* Parse the binary expressions (logical-or-expression). */
6511 expr = cp_parser_binary_expression (parser, cast_p, false,
6512 PREC_NOT_OPERATOR, pidk);
6513 /* If the next token is a `?' then we're actually looking at a
6514 conditional-expression. */
6515 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6516 return cp_parser_question_colon_clause (parser, expr);
6519 enum tree_code assignment_operator;
6521 /* If it's an assignment-operator, we're using the second
6524 = cp_parser_assignment_operator_opt (parser);
6525 if (assignment_operator != ERROR_MARK)
6527 bool non_constant_p;
6529 /* Parse the right-hand side of the assignment. */
6530 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6532 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6533 maybe_warn_cpp0x ("extended initializer lists");
6535 /* An assignment may not appear in a
6536 constant-expression. */
6537 if (cp_parser_non_integral_constant_expression (parser,
6539 return error_mark_node;
6540 /* Build the assignment expression. */
6541 expr = build_x_modify_expr (expr,
6542 assignment_operator,
6544 tf_warning_or_error);
6552 /* Parse an (optional) assignment-operator.
6554 assignment-operator: one of
6555 = *= /= %= += -= >>= <<= &= ^= |=
6559 assignment-operator: one of
6562 If the next token is an assignment operator, the corresponding tree
6563 code is returned, and the token is consumed. For example, for
6564 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6565 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6566 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6567 operator, ERROR_MARK is returned. */
6569 static enum tree_code
6570 cp_parser_assignment_operator_opt (cp_parser* parser)
6575 /* Peek at the next token. */
6576 token = cp_lexer_peek_token (parser->lexer);
6578 switch (token->type)
6589 op = TRUNC_DIV_EXPR;
6593 op = TRUNC_MOD_EXPR;
6625 /* Nothing else is an assignment operator. */
6629 /* If it was an assignment operator, consume it. */
6630 if (op != ERROR_MARK)
6631 cp_lexer_consume_token (parser->lexer);
6636 /* Parse an expression.
6639 assignment-expression
6640 expression , assignment-expression
6642 CAST_P is true if this expression is the target of a cast.
6644 Returns a representation of the expression. */
6647 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6649 tree expression = NULL_TREE;
6653 tree assignment_expression;
6655 /* Parse the next assignment-expression. */
6656 assignment_expression
6657 = cp_parser_assignment_expression (parser, cast_p, pidk);
6658 /* If this is the first assignment-expression, we can just
6661 expression = assignment_expression;
6663 expression = build_x_compound_expr (expression,
6664 assignment_expression,
6665 tf_warning_or_error);
6666 /* If the next token is not a comma, then we are done with the
6668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6670 /* Consume the `,'. */
6671 cp_lexer_consume_token (parser->lexer);
6672 /* A comma operator cannot appear in a constant-expression. */
6673 if (cp_parser_non_integral_constant_expression (parser,
6674 "a comma operator"))
6675 expression = error_mark_node;
6681 /* Parse a constant-expression.
6683 constant-expression:
6684 conditional-expression
6686 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6687 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6688 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6689 is false, NON_CONSTANT_P should be NULL. */
6692 cp_parser_constant_expression (cp_parser* parser,
6693 bool allow_non_constant_p,
6694 bool *non_constant_p)
6696 bool saved_integral_constant_expression_p;
6697 bool saved_allow_non_integral_constant_expression_p;
6698 bool saved_non_integral_constant_expression_p;
6701 /* It might seem that we could simply parse the
6702 conditional-expression, and then check to see if it were
6703 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6704 one that the compiler can figure out is constant, possibly after
6705 doing some simplifications or optimizations. The standard has a
6706 precise definition of constant-expression, and we must honor
6707 that, even though it is somewhat more restrictive.
6713 is not a legal declaration, because `(2, 3)' is not a
6714 constant-expression. The `,' operator is forbidden in a
6715 constant-expression. However, GCC's constant-folding machinery
6716 will fold this operation to an INTEGER_CST for `3'. */
6718 /* Save the old settings. */
6719 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6720 saved_allow_non_integral_constant_expression_p
6721 = parser->allow_non_integral_constant_expression_p;
6722 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6723 /* We are now parsing a constant-expression. */
6724 parser->integral_constant_expression_p = true;
6725 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6726 parser->non_integral_constant_expression_p = false;
6727 /* Although the grammar says "conditional-expression", we parse an
6728 "assignment-expression", which also permits "throw-expression"
6729 and the use of assignment operators. In the case that
6730 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6731 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6732 actually essential that we look for an assignment-expression.
6733 For example, cp_parser_initializer_clauses uses this function to
6734 determine whether a particular assignment-expression is in fact
6736 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6737 /* Restore the old settings. */
6738 parser->integral_constant_expression_p
6739 = saved_integral_constant_expression_p;
6740 parser->allow_non_integral_constant_expression_p
6741 = saved_allow_non_integral_constant_expression_p;
6742 if (allow_non_constant_p)
6743 *non_constant_p = parser->non_integral_constant_expression_p;
6744 else if (parser->non_integral_constant_expression_p)
6745 expression = error_mark_node;
6746 parser->non_integral_constant_expression_p
6747 = saved_non_integral_constant_expression_p;
6752 /* Parse __builtin_offsetof.
6754 offsetof-expression:
6755 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6757 offsetof-member-designator:
6759 | offsetof-member-designator "." id-expression
6760 | offsetof-member-designator "[" expression "]"
6761 | offsetof-member-designator "->" id-expression */
6764 cp_parser_builtin_offsetof (cp_parser *parser)
6766 int save_ice_p, save_non_ice_p;
6771 /* We're about to accept non-integral-constant things, but will
6772 definitely yield an integral constant expression. Save and
6773 restore these values around our local parsing. */
6774 save_ice_p = parser->integral_constant_expression_p;
6775 save_non_ice_p = parser->non_integral_constant_expression_p;
6777 /* Consume the "__builtin_offsetof" token. */
6778 cp_lexer_consume_token (parser->lexer);
6779 /* Consume the opening `('. */
6780 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6781 /* Parse the type-id. */
6782 type = cp_parser_type_id (parser);
6783 /* Look for the `,'. */
6784 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6785 token = cp_lexer_peek_token (parser->lexer);
6787 /* Build the (type *)null that begins the traditional offsetof macro. */
6788 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6789 tf_warning_or_error);
6791 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6792 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6793 true, &dummy, token->location);
6796 token = cp_lexer_peek_token (parser->lexer);
6797 switch (token->type)
6799 case CPP_OPEN_SQUARE:
6800 /* offsetof-member-designator "[" expression "]" */
6801 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6805 /* offsetof-member-designator "->" identifier */
6806 expr = grok_array_decl (expr, integer_zero_node);
6810 /* offsetof-member-designator "." identifier */
6811 cp_lexer_consume_token (parser->lexer);
6812 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6817 case CPP_CLOSE_PAREN:
6818 /* Consume the ")" token. */
6819 cp_lexer_consume_token (parser->lexer);
6823 /* Error. We know the following require will fail, but
6824 that gives the proper error message. */
6825 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6826 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6827 expr = error_mark_node;
6833 /* If we're processing a template, we can't finish the semantics yet.
6834 Otherwise we can fold the entire expression now. */
6835 if (processing_template_decl)
6836 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6838 expr = finish_offsetof (expr);
6841 parser->integral_constant_expression_p = save_ice_p;
6842 parser->non_integral_constant_expression_p = save_non_ice_p;
6847 /* Parse a trait expression. */
6850 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6853 tree type1, type2 = NULL_TREE;
6854 bool binary = false;
6855 cp_decl_specifier_seq decl_specs;
6859 case RID_HAS_NOTHROW_ASSIGN:
6860 kind = CPTK_HAS_NOTHROW_ASSIGN;
6862 case RID_HAS_NOTHROW_CONSTRUCTOR:
6863 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6865 case RID_HAS_NOTHROW_COPY:
6866 kind = CPTK_HAS_NOTHROW_COPY;
6868 case RID_HAS_TRIVIAL_ASSIGN:
6869 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6871 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6872 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6874 case RID_HAS_TRIVIAL_COPY:
6875 kind = CPTK_HAS_TRIVIAL_COPY;
6877 case RID_HAS_TRIVIAL_DESTRUCTOR:
6878 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6880 case RID_HAS_VIRTUAL_DESTRUCTOR:
6881 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6883 case RID_IS_ABSTRACT:
6884 kind = CPTK_IS_ABSTRACT;
6886 case RID_IS_BASE_OF:
6887 kind = CPTK_IS_BASE_OF;
6891 kind = CPTK_IS_CLASS;
6893 case RID_IS_CONVERTIBLE_TO:
6894 kind = CPTK_IS_CONVERTIBLE_TO;
6898 kind = CPTK_IS_EMPTY;
6901 kind = CPTK_IS_ENUM;
6906 case RID_IS_POLYMORPHIC:
6907 kind = CPTK_IS_POLYMORPHIC;
6909 case RID_IS_STD_LAYOUT:
6910 kind = CPTK_IS_STD_LAYOUT;
6912 case RID_IS_TRIVIAL:
6913 kind = CPTK_IS_TRIVIAL;
6916 kind = CPTK_IS_UNION;
6922 /* Consume the token. */
6923 cp_lexer_consume_token (parser->lexer);
6925 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6927 type1 = cp_parser_type_id (parser);
6929 if (type1 == error_mark_node)
6930 return error_mark_node;
6932 /* Build a trivial decl-specifier-seq. */
6933 clear_decl_specs (&decl_specs);
6934 decl_specs.type = type1;
6936 /* Call grokdeclarator to figure out what type this is. */
6937 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6938 /*initialized=*/0, /*attrlist=*/NULL);
6942 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6944 type2 = cp_parser_type_id (parser);
6946 if (type2 == error_mark_node)
6947 return error_mark_node;
6949 /* Build a trivial decl-specifier-seq. */
6950 clear_decl_specs (&decl_specs);
6951 decl_specs.type = type2;
6953 /* Call grokdeclarator to figure out what type this is. */
6954 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6955 /*initialized=*/0, /*attrlist=*/NULL);
6958 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6960 /* Complete the trait expression, which may mean either processing
6961 the trait expr now or saving it for template instantiation. */
6962 return finish_trait_expr (kind, type1, type2);
6965 /* Lambdas that appear in variable initializer or default argument scope
6966 get that in their mangling, so we need to record it. We might as well
6967 use the count for function and namespace scopes as well. */
6968 static GTY(()) tree lambda_scope;
6969 static GTY(()) int lambda_count;
6970 typedef struct GTY(()) tree_int
6975 DEF_VEC_O(tree_int);
6976 DEF_VEC_ALLOC_O(tree_int,gc);
6977 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6980 start_lambda_scope (tree decl)
6984 /* Once we're inside a function, we ignore other scopes and just push
6985 the function again so that popping works properly. */
6986 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6987 decl = current_function_decl;
6988 ti.t = lambda_scope;
6989 ti.i = lambda_count;
6990 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6991 if (lambda_scope != decl)
6993 /* Don't reset the count if we're still in the same function. */
6994 lambda_scope = decl;
7000 record_lambda_scope (tree lambda)
7002 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7003 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7007 finish_lambda_scope (void)
7009 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7010 if (lambda_scope != p->t)
7012 lambda_scope = p->t;
7013 lambda_count = p->i;
7015 VEC_pop (tree_int, lambda_scope_stack);
7018 /* Parse a lambda expression.
7021 lambda-introducer lambda-declarator [opt] compound-statement
7023 Returns a representation of the expression. */
7026 cp_parser_lambda_expression (cp_parser* parser)
7028 tree lambda_expr = build_lambda_expr ();
7031 LAMBDA_EXPR_LOCATION (lambda_expr)
7032 = cp_lexer_peek_token (parser->lexer)->location;
7034 /* We may be in the middle of deferred access check. Disable
7036 push_deferring_access_checks (dk_no_deferred);
7038 type = begin_lambda_type (lambda_expr);
7040 record_lambda_scope (lambda_expr);
7042 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7043 determine_visibility (TYPE_NAME (type));
7046 /* Inside the class, surrounding template-parameter-lists do not apply. */
7047 unsigned int saved_num_template_parameter_lists
7048 = parser->num_template_parameter_lists;
7050 parser->num_template_parameter_lists = 0;
7052 cp_parser_lambda_introducer (parser, lambda_expr);
7054 /* By virtue of defining a local class, a lambda expression has access to
7055 the private variables of enclosing classes. */
7057 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7059 cp_parser_lambda_body (parser, lambda_expr);
7061 /* The capture list was built up in reverse order; fix that now. */
7063 tree newlist = NULL_TREE;
7066 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7069 tree field = TREE_PURPOSE (elt);
7072 next = TREE_CHAIN (elt);
7073 TREE_CHAIN (elt) = newlist;
7076 /* Also add __ to the beginning of the field name so that code
7077 outside the lambda body can't see the captured name. We could
7078 just remove the name entirely, but this is more useful for
7080 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7081 /* The 'this' capture already starts with __. */
7084 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7085 buf[1] = buf[0] = '_';
7086 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7087 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7088 DECL_NAME (field) = get_identifier (buf);
7090 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7093 type = finish_struct (type, /*attributes=*/NULL_TREE);
7095 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7098 pop_deferring_access_checks ();
7100 return build_lambda_object (lambda_expr);
7103 /* Parse the beginning of a lambda expression.
7106 [ lambda-capture [opt] ]
7108 LAMBDA_EXPR is the current representation of the lambda expression. */
7111 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7113 /* Need commas after the first capture. */
7116 /* Eat the leading `['. */
7117 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7119 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7120 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7121 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7122 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7123 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7124 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7126 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7128 cp_lexer_consume_token (parser->lexer);
7132 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7134 cp_token* capture_token;
7136 tree capture_init_expr;
7137 cp_id_kind idk = CP_ID_KIND_NONE;
7138 bool explicit_init_p = false;
7140 enum capture_kind_type
7145 enum capture_kind_type capture_kind = BY_COPY;
7147 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7149 error ("expected end of capture-list");
7156 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7158 /* Possibly capture `this'. */
7159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7161 cp_lexer_consume_token (parser->lexer);
7162 add_capture (lambda_expr,
7163 /*id=*/get_identifier ("__this"),
7164 /*initializer=*/finish_this_expr(),
7165 /*by_reference_p=*/false,
7170 /* Remember whether we want to capture as a reference or not. */
7171 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7173 capture_kind = BY_REFERENCE;
7174 cp_lexer_consume_token (parser->lexer);
7177 /* Get the identifier. */
7178 capture_token = cp_lexer_peek_token (parser->lexer);
7179 capture_id = cp_parser_identifier (parser);
7181 if (capture_id == error_mark_node)
7182 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7183 delimiters, but I modified this to stop on unnested ']' as well. It
7184 was already changed to stop on unnested '}', so the
7185 "closing_parenthesis" name is no more misleading with my change. */
7187 cp_parser_skip_to_closing_parenthesis (parser,
7188 /*recovering=*/true,
7190 /*consume_paren=*/true);
7194 /* Find the initializer for this capture. */
7195 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7197 /* An explicit expression exists. */
7198 cp_lexer_consume_token (parser->lexer);
7199 pedwarn (input_location, OPT_pedantic,
7200 "ISO C++ does not allow initializers "
7201 "in lambda expression capture lists");
7202 capture_init_expr = cp_parser_assignment_expression (parser,
7205 explicit_init_p = true;
7209 const char* error_msg;
7211 /* Turn the identifier into an id-expression. */
7213 = cp_parser_lookup_name
7217 /*is_template=*/false,
7218 /*is_namespace=*/false,
7219 /*check_dependency=*/true,
7220 /*ambiguous_decls=*/NULL,
7221 capture_token->location);
7224 = finish_id_expression
7229 /*integral_constant_expression_p=*/false,
7230 /*allow_non_integral_constant_expression_p=*/false,
7231 /*non_integral_constant_expression_p=*/NULL,
7232 /*template_p=*/false,
7234 /*address_p=*/false,
7235 /*template_arg_p=*/false,
7237 capture_token->location);
7240 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7242 = unqualified_name_lookup_error (capture_init_expr);
7244 add_capture (lambda_expr,
7247 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7251 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7254 /* Parse the (optional) middle of a lambda expression.
7257 ( parameter-declaration-clause [opt] )
7258 attribute-specifier [opt]
7260 exception-specification [opt]
7261 lambda-return-type-clause [opt]
7263 LAMBDA_EXPR is the current representation of the lambda expression. */
7266 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7268 /* 5.1.1.4 of the standard says:
7269 If a lambda-expression does not include a lambda-declarator, it is as if
7270 the lambda-declarator were ().
7271 This means an empty parameter list, no attributes, and no exception
7273 tree param_list = void_list_node;
7274 tree attributes = NULL_TREE;
7275 tree exception_spec = NULL_TREE;
7278 /* The lambda-declarator is optional, but must begin with an opening
7279 parenthesis if present. */
7280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7282 cp_lexer_consume_token (parser->lexer);
7284 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7286 /* Parse parameters. */
7287 param_list = cp_parser_parameter_declaration_clause (parser);
7289 /* Default arguments shall not be specified in the
7290 parameter-declaration-clause of a lambda-declarator. */
7291 for (t = param_list; t; t = TREE_CHAIN (t))
7292 if (TREE_PURPOSE (t))
7293 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7294 "default argument specified for lambda parameter");
7296 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7298 attributes = cp_parser_attributes_opt (parser);
7300 /* Parse optional `mutable' keyword. */
7301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7303 cp_lexer_consume_token (parser->lexer);
7304 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7307 /* Parse optional exception specification. */
7308 exception_spec = cp_parser_exception_specification_opt (parser);
7310 /* Parse optional trailing return type. */
7311 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7313 cp_lexer_consume_token (parser->lexer);
7314 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7317 /* The function parameters must be in scope all the way until after the
7318 trailing-return-type in case of decltype. */
7319 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7320 pop_binding (DECL_NAME (t), t);
7325 /* Create the function call operator.
7327 Messing with declarators like this is no uglier than building up the
7328 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7331 cp_decl_specifier_seq return_type_specs;
7332 cp_declarator* declarator;
7337 clear_decl_specs (&return_type_specs);
7338 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7339 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7341 /* Maybe we will deduce the return type later, but we can use void
7342 as a placeholder return type anyways. */
7343 return_type_specs.type = void_type_node;
7345 p = obstack_alloc (&declarator_obstack, 0);
7347 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7350 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7351 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7352 declarator = make_call_declarator (declarator, param_list, quals,
7354 /*late_return_type=*/NULL_TREE);
7356 fco = grokmethod (&return_type_specs,
7359 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7360 DECL_ARTIFICIAL (fco) = 1;
7362 finish_member_declaration (fco);
7364 obstack_free (&declarator_obstack, p);
7368 /* Parse the body of a lambda expression, which is simply
7372 but which requires special handling.
7373 LAMBDA_EXPR is the current representation of the lambda expression. */
7376 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7378 bool nested = (current_function_decl != NULL_TREE);
7380 push_function_context ();
7382 /* Finish the function call operator
7384 + late_parsing_for_member
7385 + function_definition_after_declarator
7386 + ctor_initializer_opt_and_function_body */
7388 tree fco = lambda_function (lambda_expr);
7392 /* Let the front end know that we are going to be defining this
7394 start_preparsed_function (fco,
7396 SF_PRE_PARSED | SF_INCLASS_INLINE);
7398 start_lambda_scope (fco);
7399 body = begin_function_body ();
7401 /* 5.1.1.4 of the standard says:
7402 If a lambda-expression does not include a trailing-return-type, it
7403 is as if the trailing-return-type denotes the following type:
7404 * if the compound-statement is of the form
7405 { return attribute-specifier [opt] expression ; }
7406 the type of the returned expression after lvalue-to-rvalue
7407 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7408 (_conv.array_ 4.2), and function-to-pointer conversion
7410 * otherwise, void. */
7412 /* In a lambda that has neither a lambda-return-type-clause
7413 nor a deducible form, errors should be reported for return statements
7414 in the body. Since we used void as the placeholder return type, parsing
7415 the body as usual will give such desired behavior. */
7416 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7417 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7418 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7419 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7422 tree expr = NULL_TREE;
7423 cp_id_kind idk = CP_ID_KIND_NONE;
7425 /* Parse tentatively in case there's more after the initial return
7427 cp_parser_parse_tentatively (parser);
7429 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7430 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7432 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7434 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7435 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7437 if (cp_parser_parse_definitely (parser))
7439 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7441 compound_stmt = begin_compound_stmt (0);
7442 /* Will get error here if type not deduced yet. */
7443 finish_return_stmt (expr);
7444 finish_compound_stmt (compound_stmt);
7452 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7453 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7454 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7455 cp_parser_compound_stmt does not pass it. */
7456 cp_parser_function_body (parser);
7457 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7460 finish_function_body (body);
7461 finish_lambda_scope ();
7463 /* Finish the function and generate code for it if necessary. */
7464 expand_or_defer_fn (finish_function (/*inline*/2));
7468 pop_function_context();
7471 /* Statements [gram.stmt.stmt] */
7473 /* Parse a statement.
7477 expression-statement
7482 declaration-statement
7485 IN_COMPOUND is true when the statement is nested inside a
7486 cp_parser_compound_statement; this matters for certain pragmas.
7488 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7489 is a (possibly labeled) if statement which is not enclosed in braces
7490 and has an else clause. This is used to implement -Wparentheses. */
7493 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7494 bool in_compound, bool *if_p)
7498 location_t statement_location;
7503 /* There is no statement yet. */
7504 statement = NULL_TREE;
7505 /* Peek at the next token. */
7506 token = cp_lexer_peek_token (parser->lexer);
7507 /* Remember the location of the first token in the statement. */
7508 statement_location = token->location;
7509 /* If this is a keyword, then that will often determine what kind of
7510 statement we have. */
7511 if (token->type == CPP_KEYWORD)
7513 enum rid keyword = token->keyword;
7519 /* Looks like a labeled-statement with a case label.
7520 Parse the label, and then use tail recursion to parse
7522 cp_parser_label_for_labeled_statement (parser);
7527 statement = cp_parser_selection_statement (parser, if_p);
7533 statement = cp_parser_iteration_statement (parser);
7540 statement = cp_parser_jump_statement (parser);
7543 /* Objective-C++ exception-handling constructs. */
7546 case RID_AT_FINALLY:
7547 case RID_AT_SYNCHRONIZED:
7549 statement = cp_parser_objc_statement (parser);
7553 statement = cp_parser_try_block (parser);
7557 /* This must be a namespace alias definition. */
7558 cp_parser_declaration_statement (parser);
7562 /* It might be a keyword like `int' that can start a
7563 declaration-statement. */
7567 else if (token->type == CPP_NAME)
7569 /* If the next token is a `:', then we are looking at a
7570 labeled-statement. */
7571 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7572 if (token->type == CPP_COLON)
7574 /* Looks like a labeled-statement with an ordinary label.
7575 Parse the label, and then use tail recursion to parse
7577 cp_parser_label_for_labeled_statement (parser);
7581 /* Anything that starts with a `{' must be a compound-statement. */
7582 else if (token->type == CPP_OPEN_BRACE)
7583 statement = cp_parser_compound_statement (parser, NULL, false);
7584 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7585 a statement all its own. */
7586 else if (token->type == CPP_PRAGMA)
7588 /* Only certain OpenMP pragmas are attached to statements, and thus
7589 are considered statements themselves. All others are not. In
7590 the context of a compound, accept the pragma as a "statement" and
7591 return so that we can check for a close brace. Otherwise we
7592 require a real statement and must go back and read one. */
7594 cp_parser_pragma (parser, pragma_compound);
7595 else if (!cp_parser_pragma (parser, pragma_stmt))
7599 else if (token->type == CPP_EOF)
7601 cp_parser_error (parser, "expected statement");
7605 /* Everything else must be a declaration-statement or an
7606 expression-statement. Try for the declaration-statement
7607 first, unless we are looking at a `;', in which case we know that
7608 we have an expression-statement. */
7611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7613 cp_parser_parse_tentatively (parser);
7614 /* Try to parse the declaration-statement. */
7615 cp_parser_declaration_statement (parser);
7616 /* If that worked, we're done. */
7617 if (cp_parser_parse_definitely (parser))
7620 /* Look for an expression-statement instead. */
7621 statement = cp_parser_expression_statement (parser, in_statement_expr);
7624 /* Set the line number for the statement. */
7625 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7626 SET_EXPR_LOCATION (statement, statement_location);
7629 /* Parse the label for a labeled-statement, i.e.
7632 case constant-expression :
7636 case constant-expression ... constant-expression : statement
7638 When a label is parsed without errors, the label is added to the
7639 parse tree by the finish_* functions, so this function doesn't
7640 have to return the label. */
7643 cp_parser_label_for_labeled_statement (cp_parser* parser)
7646 tree label = NULL_TREE;
7648 /* The next token should be an identifier. */
7649 token = cp_lexer_peek_token (parser->lexer);
7650 if (token->type != CPP_NAME
7651 && token->type != CPP_KEYWORD)
7653 cp_parser_error (parser, "expected labeled-statement");
7657 switch (token->keyword)
7664 /* Consume the `case' token. */
7665 cp_lexer_consume_token (parser->lexer);
7666 /* Parse the constant-expression. */
7667 expr = cp_parser_constant_expression (parser,
7668 /*allow_non_constant_p=*/false,
7671 ellipsis = cp_lexer_peek_token (parser->lexer);
7672 if (ellipsis->type == CPP_ELLIPSIS)
7674 /* Consume the `...' token. */
7675 cp_lexer_consume_token (parser->lexer);
7677 cp_parser_constant_expression (parser,
7678 /*allow_non_constant_p=*/false,
7680 /* We don't need to emit warnings here, as the common code
7681 will do this for us. */
7684 expr_hi = NULL_TREE;
7686 if (parser->in_switch_statement_p)
7687 finish_case_label (token->location, expr, expr_hi);
7689 error_at (token->location,
7690 "case label %qE not within a switch statement",
7696 /* Consume the `default' token. */
7697 cp_lexer_consume_token (parser->lexer);
7699 if (parser->in_switch_statement_p)
7700 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7702 error_at (token->location, "case label not within a switch statement");
7706 /* Anything else must be an ordinary label. */
7707 label = finish_label_stmt (cp_parser_identifier (parser));
7711 /* Require the `:' token. */
7712 cp_parser_require (parser, CPP_COLON, "%<:%>");
7714 /* An ordinary label may optionally be followed by attributes.
7715 However, this is only permitted if the attributes are then
7716 followed by a semicolon. This is because, for backward
7717 compatibility, when parsing
7718 lab: __attribute__ ((unused)) int i;
7719 we want the attribute to attach to "i", not "lab". */
7720 if (label != NULL_TREE
7721 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7725 cp_parser_parse_tentatively (parser);
7726 attrs = cp_parser_attributes_opt (parser);
7727 if (attrs == NULL_TREE
7728 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7729 cp_parser_abort_tentative_parse (parser);
7730 else if (!cp_parser_parse_definitely (parser))
7733 cplus_decl_attributes (&label, attrs, 0);
7737 /* Parse an expression-statement.
7739 expression-statement:
7742 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7743 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7744 indicates whether this expression-statement is part of an
7745 expression statement. */
7748 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7750 tree statement = NULL_TREE;
7752 /* If the next token is a ';', then there is no expression
7754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7755 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7757 /* Consume the final `;'. */
7758 cp_parser_consume_semicolon_at_end_of_statement (parser);
7760 if (in_statement_expr
7761 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7762 /* This is the final expression statement of a statement
7764 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7766 statement = finish_expr_stmt (statement);
7773 /* Parse a compound-statement.
7776 { statement-seq [opt] }
7781 { label-declaration-seq [opt] statement-seq [opt] }
7783 label-declaration-seq:
7785 label-declaration-seq label-declaration
7787 Returns a tree representing the statement. */
7790 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7795 /* Consume the `{'. */
7796 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7797 return error_mark_node;
7798 /* Begin the compound-statement. */
7799 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7800 /* If the next keyword is `__label__' we have a label declaration. */
7801 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7802 cp_parser_label_declaration (parser);
7803 /* Parse an (optional) statement-seq. */
7804 cp_parser_statement_seq_opt (parser, in_statement_expr);
7805 /* Finish the compound-statement. */
7806 finish_compound_stmt (compound_stmt);
7807 /* Consume the `}'. */
7808 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7810 return compound_stmt;
7813 /* Parse an (optional) statement-seq.
7817 statement-seq [opt] statement */
7820 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7822 /* Scan statements until there aren't any more. */
7825 cp_token *token = cp_lexer_peek_token (parser->lexer);
7827 /* If we're looking at a `}', then we've run out of statements. */
7828 if (token->type == CPP_CLOSE_BRACE
7829 || token->type == CPP_EOF
7830 || token->type == CPP_PRAGMA_EOL)
7833 /* If we are in a compound statement and find 'else' then
7834 something went wrong. */
7835 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7837 if (parser->in_statement & IN_IF_STMT)
7841 token = cp_lexer_consume_token (parser->lexer);
7842 error_at (token->location, "%<else%> without a previous %<if%>");
7846 /* Parse the statement. */
7847 cp_parser_statement (parser, in_statement_expr, true, NULL);
7851 /* Parse a selection-statement.
7853 selection-statement:
7854 if ( condition ) statement
7855 if ( condition ) statement else statement
7856 switch ( condition ) statement
7858 Returns the new IF_STMT or SWITCH_STMT.
7860 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7861 is a (possibly labeled) if statement which is not enclosed in
7862 braces and has an else clause. This is used to implement
7866 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7874 /* Peek at the next token. */
7875 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7877 /* See what kind of keyword it is. */
7878 keyword = token->keyword;
7887 /* Look for the `('. */
7888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7890 cp_parser_skip_to_end_of_statement (parser);
7891 return error_mark_node;
7894 /* Begin the selection-statement. */
7895 if (keyword == RID_IF)
7896 statement = begin_if_stmt ();
7898 statement = begin_switch_stmt ();
7900 /* Parse the condition. */
7901 condition = cp_parser_condition (parser);
7902 /* Look for the `)'. */
7903 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7904 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7905 /*consume_paren=*/true);
7907 if (keyword == RID_IF)
7910 unsigned char in_statement;
7912 /* Add the condition. */
7913 finish_if_stmt_cond (condition, statement);
7915 /* Parse the then-clause. */
7916 in_statement = parser->in_statement;
7917 parser->in_statement |= IN_IF_STMT;
7918 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7921 add_stmt (build_empty_stmt (loc));
7922 cp_lexer_consume_token (parser->lexer);
7923 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7924 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7925 "empty body in an %<if%> statement");
7929 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7930 parser->in_statement = in_statement;
7932 finish_then_clause (statement);
7934 /* If the next token is `else', parse the else-clause. */
7935 if (cp_lexer_next_token_is_keyword (parser->lexer,
7938 /* Consume the `else' keyword. */
7939 cp_lexer_consume_token (parser->lexer);
7940 begin_else_clause (statement);
7941 /* Parse the else-clause. */
7942 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7945 loc = cp_lexer_peek_token (parser->lexer)->location;
7947 OPT_Wempty_body, "suggest braces around "
7948 "empty body in an %<else%> statement");
7949 add_stmt (build_empty_stmt (loc));
7950 cp_lexer_consume_token (parser->lexer);
7953 cp_parser_implicitly_scoped_statement (parser, NULL);
7955 finish_else_clause (statement);
7957 /* If we are currently parsing a then-clause, then
7958 IF_P will not be NULL. We set it to true to
7959 indicate that this if statement has an else clause.
7960 This may trigger the Wparentheses warning below
7961 when we get back up to the parent if statement. */
7967 /* This if statement does not have an else clause. If
7968 NESTED_IF is true, then the then-clause is an if
7969 statement which does have an else clause. We warn
7970 about the potential ambiguity. */
7972 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7973 "suggest explicit braces to avoid ambiguous"
7977 /* Now we're all done with the if-statement. */
7978 finish_if_stmt (statement);
7982 bool in_switch_statement_p;
7983 unsigned char in_statement;
7985 /* Add the condition. */
7986 finish_switch_cond (condition, statement);
7988 /* Parse the body of the switch-statement. */
7989 in_switch_statement_p = parser->in_switch_statement_p;
7990 in_statement = parser->in_statement;
7991 parser->in_switch_statement_p = true;
7992 parser->in_statement |= IN_SWITCH_STMT;
7993 cp_parser_implicitly_scoped_statement (parser, NULL);
7994 parser->in_switch_statement_p = in_switch_statement_p;
7995 parser->in_statement = in_statement;
7997 /* Now we're all done with the switch-statement. */
7998 finish_switch_stmt (statement);
8006 cp_parser_error (parser, "expected selection-statement");
8007 return error_mark_node;
8011 /* Parse a condition.
8015 type-specifier-seq declarator = initializer-clause
8016 type-specifier-seq declarator braced-init-list
8021 type-specifier-seq declarator asm-specification [opt]
8022 attributes [opt] = assignment-expression
8024 Returns the expression that should be tested. */
8027 cp_parser_condition (cp_parser* parser)
8029 cp_decl_specifier_seq type_specifiers;
8030 const char *saved_message;
8032 /* Try the declaration first. */
8033 cp_parser_parse_tentatively (parser);
8034 /* New types are not allowed in the type-specifier-seq for a
8036 saved_message = parser->type_definition_forbidden_message;
8037 parser->type_definition_forbidden_message
8038 = "types may not be defined in conditions";
8039 /* Parse the type-specifier-seq. */
8040 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8041 /*is_trailing_return=*/false,
8043 /* Restore the saved message. */
8044 parser->type_definition_forbidden_message = saved_message;
8045 /* If all is well, we might be looking at a declaration. */
8046 if (!cp_parser_error_occurred (parser))
8049 tree asm_specification;
8051 cp_declarator *declarator;
8052 tree initializer = NULL_TREE;
8054 /* Parse the declarator. */
8055 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8056 /*ctor_dtor_or_conv_p=*/NULL,
8057 /*parenthesized_p=*/NULL,
8058 /*member_p=*/false);
8059 /* Parse the attributes. */
8060 attributes = cp_parser_attributes_opt (parser);
8061 /* Parse the asm-specification. */
8062 asm_specification = cp_parser_asm_specification_opt (parser);
8063 /* If the next token is not an `=' or '{', then we might still be
8064 looking at an expression. For example:
8068 looks like a decl-specifier-seq and a declarator -- but then
8069 there is no `=', so this is an expression. */
8070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8071 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8072 cp_parser_simulate_error (parser);
8074 /* If we did see an `=' or '{', then we are looking at a declaration
8076 if (cp_parser_parse_definitely (parser))
8079 bool non_constant_p;
8080 bool flags = LOOKUP_ONLYCONVERTING;
8082 /* Create the declaration. */
8083 decl = start_decl (declarator, &type_specifiers,
8084 /*initialized_p=*/true,
8085 attributes, /*prefix_attributes=*/NULL_TREE,
8088 /* Parse the initializer. */
8089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8091 initializer = cp_parser_braced_list (parser, &non_constant_p);
8092 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8097 /* Consume the `='. */
8098 cp_parser_require (parser, CPP_EQ, "%<=%>");
8099 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8101 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8102 maybe_warn_cpp0x ("extended initializer lists");
8104 if (!non_constant_p)
8105 initializer = fold_non_dependent_expr (initializer);
8107 /* Process the initializer. */
8108 cp_finish_decl (decl,
8109 initializer, !non_constant_p,
8114 pop_scope (pushed_scope);
8116 return convert_from_reference (decl);
8119 /* If we didn't even get past the declarator successfully, we are
8120 definitely not looking at a declaration. */
8122 cp_parser_abort_tentative_parse (parser);
8124 /* Otherwise, we are looking at an expression. */
8125 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8128 /* Parse an iteration-statement.
8130 iteration-statement:
8131 while ( condition ) statement
8132 do statement while ( expression ) ;
8133 for ( for-init-statement condition [opt] ; expression [opt] )
8136 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8139 cp_parser_iteration_statement (cp_parser* parser)
8144 unsigned char in_statement;
8146 /* Peek at the next token. */
8147 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8149 return error_mark_node;
8151 /* Remember whether or not we are already within an iteration
8153 in_statement = parser->in_statement;
8155 /* See what kind of keyword it is. */
8156 keyword = token->keyword;
8163 /* Begin the while-statement. */
8164 statement = begin_while_stmt ();
8165 /* Look for the `('. */
8166 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8167 /* Parse the condition. */
8168 condition = cp_parser_condition (parser);
8169 finish_while_stmt_cond (condition, statement);
8170 /* Look for the `)'. */
8171 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8172 /* Parse the dependent statement. */
8173 parser->in_statement = IN_ITERATION_STMT;
8174 cp_parser_already_scoped_statement (parser);
8175 parser->in_statement = in_statement;
8176 /* We're done with the while-statement. */
8177 finish_while_stmt (statement);
8185 /* Begin the do-statement. */
8186 statement = begin_do_stmt ();
8187 /* Parse the body of the do-statement. */
8188 parser->in_statement = IN_ITERATION_STMT;
8189 cp_parser_implicitly_scoped_statement (parser, NULL);
8190 parser->in_statement = in_statement;
8191 finish_do_body (statement);
8192 /* Look for the `while' keyword. */
8193 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8194 /* Look for the `('. */
8195 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8196 /* Parse the expression. */
8197 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8198 /* We're done with the do-statement. */
8199 finish_do_stmt (expression, statement);
8200 /* Look for the `)'. */
8201 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8202 /* Look for the `;'. */
8203 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8209 tree condition = NULL_TREE;
8210 tree expression = NULL_TREE;
8212 /* Begin the for-statement. */
8213 statement = begin_for_stmt ();
8214 /* Look for the `('. */
8215 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8216 /* Parse the initialization. */
8217 cp_parser_for_init_statement (parser);
8218 finish_for_init_stmt (statement);
8220 /* If there's a condition, process it. */
8221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8222 condition = cp_parser_condition (parser);
8223 finish_for_cond (condition, statement);
8224 /* Look for the `;'. */
8225 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8227 /* If there's an expression, process it. */
8228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8229 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8230 finish_for_expr (expression, statement);
8231 /* Look for the `)'. */
8232 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8234 /* Parse the body of the for-statement. */
8235 parser->in_statement = IN_ITERATION_STMT;
8236 cp_parser_already_scoped_statement (parser);
8237 parser->in_statement = in_statement;
8239 /* We're done with the for-statement. */
8240 finish_for_stmt (statement);
8245 cp_parser_error (parser, "expected iteration-statement");
8246 statement = error_mark_node;
8253 /* Parse a for-init-statement.
8256 expression-statement
8257 simple-declaration */
8260 cp_parser_for_init_statement (cp_parser* parser)
8262 /* If the next token is a `;', then we have an empty
8263 expression-statement. Grammatically, this is also a
8264 simple-declaration, but an invalid one, because it does not
8265 declare anything. Therefore, if we did not handle this case
8266 specially, we would issue an error message about an invalid
8268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8270 /* We're going to speculatively look for a declaration, falling back
8271 to an expression, if necessary. */
8272 cp_parser_parse_tentatively (parser);
8273 /* Parse the declaration. */
8274 cp_parser_simple_declaration (parser,
8275 /*function_definition_allowed_p=*/false);
8276 /* If the tentative parse failed, then we shall need to look for an
8277 expression-statement. */
8278 if (cp_parser_parse_definitely (parser))
8282 cp_parser_expression_statement (parser, false);
8285 /* Parse a jump-statement.
8290 return expression [opt] ;
8291 return braced-init-list ;
8299 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8302 cp_parser_jump_statement (cp_parser* parser)
8304 tree statement = error_mark_node;
8307 unsigned char in_statement;
8309 /* Peek at the next token. */
8310 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8312 return error_mark_node;
8314 /* See what kind of keyword it is. */
8315 keyword = token->keyword;
8319 in_statement = parser->in_statement & ~IN_IF_STMT;
8320 switch (in_statement)
8323 error_at (token->location, "break statement not within loop or switch");
8326 gcc_assert ((in_statement & IN_SWITCH_STMT)
8327 || in_statement == IN_ITERATION_STMT);
8328 statement = finish_break_stmt ();
8331 error_at (token->location, "invalid exit from OpenMP structured block");
8334 error_at (token->location, "break statement used with OpenMP for loop");
8337 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8341 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8344 error_at (token->location, "continue statement not within a loop");
8346 case IN_ITERATION_STMT:
8348 statement = finish_continue_stmt ();
8351 error_at (token->location, "invalid exit from OpenMP structured block");
8356 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8362 bool expr_non_constant_p;
8364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8366 maybe_warn_cpp0x ("extended initializer lists");
8367 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8369 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8370 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8372 /* If the next token is a `;', then there is no
8375 /* Build the return-statement. */
8376 statement = finish_return_stmt (expr);
8377 /* Look for the final `;'. */
8378 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8383 /* Create the goto-statement. */
8384 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8386 /* Issue a warning about this use of a GNU extension. */
8387 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8388 /* Consume the '*' token. */
8389 cp_lexer_consume_token (parser->lexer);
8390 /* Parse the dependent expression. */
8391 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8394 finish_goto_stmt (cp_parser_identifier (parser));
8395 /* Look for the final `;'. */
8396 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8400 cp_parser_error (parser, "expected jump-statement");
8407 /* Parse a declaration-statement.
8409 declaration-statement:
8410 block-declaration */
8413 cp_parser_declaration_statement (cp_parser* parser)
8417 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8418 p = obstack_alloc (&declarator_obstack, 0);
8420 /* Parse the block-declaration. */
8421 cp_parser_block_declaration (parser, /*statement_p=*/true);
8423 /* Free any declarators allocated. */
8424 obstack_free (&declarator_obstack, p);
8426 /* Finish off the statement. */
8430 /* Some dependent statements (like `if (cond) statement'), are
8431 implicitly in their own scope. In other words, if the statement is
8432 a single statement (as opposed to a compound-statement), it is
8433 none-the-less treated as if it were enclosed in braces. Any
8434 declarations appearing in the dependent statement are out of scope
8435 after control passes that point. This function parses a statement,
8436 but ensures that is in its own scope, even if it is not a
8439 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8440 is a (possibly labeled) if statement which is not enclosed in
8441 braces and has an else clause. This is used to implement
8444 Returns the new statement. */
8447 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8454 /* Mark if () ; with a special NOP_EXPR. */
8455 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8458 cp_lexer_consume_token (parser->lexer);
8459 statement = add_stmt (build_empty_stmt (loc));
8461 /* if a compound is opened, we simply parse the statement directly. */
8462 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8463 statement = cp_parser_compound_statement (parser, NULL, false);
8464 /* If the token is not a `{', then we must take special action. */
8467 /* Create a compound-statement. */
8468 statement = begin_compound_stmt (0);
8469 /* Parse the dependent-statement. */
8470 cp_parser_statement (parser, NULL_TREE, false, if_p);
8471 /* Finish the dummy compound-statement. */
8472 finish_compound_stmt (statement);
8475 /* Return the statement. */
8479 /* For some dependent statements (like `while (cond) statement'), we
8480 have already created a scope. Therefore, even if the dependent
8481 statement is a compound-statement, we do not want to create another
8485 cp_parser_already_scoped_statement (cp_parser* parser)
8487 /* If the token is a `{', then we must take special action. */
8488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8489 cp_parser_statement (parser, NULL_TREE, false, NULL);
8492 /* Avoid calling cp_parser_compound_statement, so that we
8493 don't create a new scope. Do everything else by hand. */
8494 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8495 /* If the next keyword is `__label__' we have a label declaration. */
8496 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8497 cp_parser_label_declaration (parser);
8498 /* Parse an (optional) statement-seq. */
8499 cp_parser_statement_seq_opt (parser, NULL_TREE);
8500 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8504 /* Declarations [gram.dcl.dcl] */
8506 /* Parse an optional declaration-sequence.
8510 declaration-seq declaration */
8513 cp_parser_declaration_seq_opt (cp_parser* parser)
8519 token = cp_lexer_peek_token (parser->lexer);
8521 if (token->type == CPP_CLOSE_BRACE
8522 || token->type == CPP_EOF
8523 || token->type == CPP_PRAGMA_EOL)
8526 if (token->type == CPP_SEMICOLON)
8528 /* A declaration consisting of a single semicolon is
8529 invalid. Allow it unless we're being pedantic. */
8530 cp_lexer_consume_token (parser->lexer);
8531 if (!in_system_header)
8532 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8536 /* If we're entering or exiting a region that's implicitly
8537 extern "C", modify the lang context appropriately. */
8538 if (!parser->implicit_extern_c && token->implicit_extern_c)
8540 push_lang_context (lang_name_c);
8541 parser->implicit_extern_c = true;
8543 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8545 pop_lang_context ();
8546 parser->implicit_extern_c = false;
8549 if (token->type == CPP_PRAGMA)
8551 /* A top-level declaration can consist solely of a #pragma.
8552 A nested declaration cannot, so this is done here and not
8553 in cp_parser_declaration. (A #pragma at block scope is
8554 handled in cp_parser_statement.) */
8555 cp_parser_pragma (parser, pragma_external);
8559 /* Parse the declaration itself. */
8560 cp_parser_declaration (parser);
8564 /* Parse a declaration.
8569 template-declaration
8570 explicit-instantiation
8571 explicit-specialization
8572 linkage-specification
8573 namespace-definition
8578 __extension__ declaration */
8581 cp_parser_declaration (cp_parser* parser)
8588 /* Check for the `__extension__' keyword. */
8589 if (cp_parser_extension_opt (parser, &saved_pedantic))
8591 /* Parse the qualified declaration. */
8592 cp_parser_declaration (parser);
8593 /* Restore the PEDANTIC flag. */
8594 pedantic = saved_pedantic;
8599 /* Try to figure out what kind of declaration is present. */
8600 token1 = *cp_lexer_peek_token (parser->lexer);
8602 if (token1.type != CPP_EOF)
8603 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8606 token2.type = CPP_EOF;
8607 token2.keyword = RID_MAX;
8610 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8611 p = obstack_alloc (&declarator_obstack, 0);
8613 /* If the next token is `extern' and the following token is a string
8614 literal, then we have a linkage specification. */
8615 if (token1.keyword == RID_EXTERN
8616 && cp_parser_is_string_literal (&token2))
8617 cp_parser_linkage_specification (parser);
8618 /* If the next token is `template', then we have either a template
8619 declaration, an explicit instantiation, or an explicit
8621 else if (token1.keyword == RID_TEMPLATE)
8623 /* `template <>' indicates a template specialization. */
8624 if (token2.type == CPP_LESS
8625 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8626 cp_parser_explicit_specialization (parser);
8627 /* `template <' indicates a template declaration. */
8628 else if (token2.type == CPP_LESS)
8629 cp_parser_template_declaration (parser, /*member_p=*/false);
8630 /* Anything else must be an explicit instantiation. */
8632 cp_parser_explicit_instantiation (parser);
8634 /* If the next token is `export', then we have a template
8636 else if (token1.keyword == RID_EXPORT)
8637 cp_parser_template_declaration (parser, /*member_p=*/false);
8638 /* If the next token is `extern', 'static' or 'inline' and the one
8639 after that is `template', we have a GNU extended explicit
8640 instantiation directive. */
8641 else if (cp_parser_allow_gnu_extensions_p (parser)
8642 && (token1.keyword == RID_EXTERN
8643 || token1.keyword == RID_STATIC
8644 || token1.keyword == RID_INLINE)
8645 && token2.keyword == RID_TEMPLATE)
8646 cp_parser_explicit_instantiation (parser);
8647 /* If the next token is `namespace', check for a named or unnamed
8648 namespace definition. */
8649 else if (token1.keyword == RID_NAMESPACE
8650 && (/* A named namespace definition. */
8651 (token2.type == CPP_NAME
8652 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8654 /* An unnamed namespace definition. */
8655 || token2.type == CPP_OPEN_BRACE
8656 || token2.keyword == RID_ATTRIBUTE))
8657 cp_parser_namespace_definition (parser);
8658 /* An inline (associated) namespace definition. */
8659 else if (token1.keyword == RID_INLINE
8660 && token2.keyword == RID_NAMESPACE)
8661 cp_parser_namespace_definition (parser);
8662 /* Objective-C++ declaration/definition. */
8663 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8664 cp_parser_objc_declaration (parser);
8665 /* We must have either a block declaration or a function
8668 /* Try to parse a block-declaration, or a function-definition. */
8669 cp_parser_block_declaration (parser, /*statement_p=*/false);
8671 /* Free any declarators allocated. */
8672 obstack_free (&declarator_obstack, p);
8675 /* Parse a block-declaration.
8680 namespace-alias-definition
8687 __extension__ block-declaration
8692 static_assert-declaration
8694 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8695 part of a declaration-statement. */
8698 cp_parser_block_declaration (cp_parser *parser,
8704 /* Check for the `__extension__' keyword. */
8705 if (cp_parser_extension_opt (parser, &saved_pedantic))
8707 /* Parse the qualified declaration. */
8708 cp_parser_block_declaration (parser, statement_p);
8709 /* Restore the PEDANTIC flag. */
8710 pedantic = saved_pedantic;
8715 /* Peek at the next token to figure out which kind of declaration is
8717 token1 = cp_lexer_peek_token (parser->lexer);
8719 /* If the next keyword is `asm', we have an asm-definition. */
8720 if (token1->keyword == RID_ASM)
8723 cp_parser_commit_to_tentative_parse (parser);
8724 cp_parser_asm_definition (parser);
8726 /* If the next keyword is `namespace', we have a
8727 namespace-alias-definition. */
8728 else if (token1->keyword == RID_NAMESPACE)
8729 cp_parser_namespace_alias_definition (parser);
8730 /* If the next keyword is `using', we have either a
8731 using-declaration or a using-directive. */
8732 else if (token1->keyword == RID_USING)
8737 cp_parser_commit_to_tentative_parse (parser);
8738 /* If the token after `using' is `namespace', then we have a
8740 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8741 if (token2->keyword == RID_NAMESPACE)
8742 cp_parser_using_directive (parser);
8743 /* Otherwise, it's a using-declaration. */
8745 cp_parser_using_declaration (parser,
8746 /*access_declaration_p=*/false);
8748 /* If the next keyword is `__label__' we have a misplaced label
8750 else if (token1->keyword == RID_LABEL)
8752 cp_lexer_consume_token (parser->lexer);
8753 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8754 cp_parser_skip_to_end_of_statement (parser);
8755 /* If the next token is now a `;', consume it. */
8756 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8757 cp_lexer_consume_token (parser->lexer);
8759 /* If the next token is `static_assert' we have a static assertion. */
8760 else if (token1->keyword == RID_STATIC_ASSERT)
8761 cp_parser_static_assert (parser, /*member_p=*/false);
8762 /* Anything else must be a simple-declaration. */
8764 cp_parser_simple_declaration (parser, !statement_p);
8767 /* Parse a simple-declaration.
8770 decl-specifier-seq [opt] init-declarator-list [opt] ;
8772 init-declarator-list:
8774 init-declarator-list , init-declarator
8776 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8777 function-definition as a simple-declaration. */
8780 cp_parser_simple_declaration (cp_parser* parser,
8781 bool function_definition_allowed_p)
8783 cp_decl_specifier_seq decl_specifiers;
8784 int declares_class_or_enum;
8785 bool saw_declarator;
8787 /* Defer access checks until we know what is being declared; the
8788 checks for names appearing in the decl-specifier-seq should be
8789 done as if we were in the scope of the thing being declared. */
8790 push_deferring_access_checks (dk_deferred);
8792 /* Parse the decl-specifier-seq. We have to keep track of whether
8793 or not the decl-specifier-seq declares a named class or
8794 enumeration type, since that is the only case in which the
8795 init-declarator-list is allowed to be empty.
8799 In a simple-declaration, the optional init-declarator-list can be
8800 omitted only when declaring a class or enumeration, that is when
8801 the decl-specifier-seq contains either a class-specifier, an
8802 elaborated-type-specifier, or an enum-specifier. */
8803 cp_parser_decl_specifier_seq (parser,
8804 CP_PARSER_FLAGS_OPTIONAL,
8806 &declares_class_or_enum);
8807 /* We no longer need to defer access checks. */
8808 stop_deferring_access_checks ();
8810 /* In a block scope, a valid declaration must always have a
8811 decl-specifier-seq. By not trying to parse declarators, we can
8812 resolve the declaration/expression ambiguity more quickly. */
8813 if (!function_definition_allowed_p
8814 && !decl_specifiers.any_specifiers_p)
8816 cp_parser_error (parser, "expected declaration");
8820 /* If the next two tokens are both identifiers, the code is
8821 erroneous. The usual cause of this situation is code like:
8825 where "T" should name a type -- but does not. */
8826 if (!decl_specifiers.type
8827 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8829 /* If parsing tentatively, we should commit; we really are
8830 looking at a declaration. */
8831 cp_parser_commit_to_tentative_parse (parser);
8836 /* If we have seen at least one decl-specifier, and the next token
8837 is not a parenthesis, then we must be looking at a declaration.
8838 (After "int (" we might be looking at a functional cast.) */
8839 if (decl_specifiers.any_specifiers_p
8840 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8841 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8842 && !cp_parser_error_occurred (parser))
8843 cp_parser_commit_to_tentative_parse (parser);
8845 /* Keep going until we hit the `;' at the end of the simple
8847 saw_declarator = false;
8848 while (cp_lexer_next_token_is_not (parser->lexer,
8852 bool function_definition_p;
8857 /* If we are processing next declarator, coma is expected */
8858 token = cp_lexer_peek_token (parser->lexer);
8859 gcc_assert (token->type == CPP_COMMA);
8860 cp_lexer_consume_token (parser->lexer);
8863 saw_declarator = true;
8865 /* Parse the init-declarator. */
8866 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8868 function_definition_allowed_p,
8870 declares_class_or_enum,
8871 &function_definition_p);
8872 /* If an error occurred while parsing tentatively, exit quickly.
8873 (That usually happens when in the body of a function; each
8874 statement is treated as a declaration-statement until proven
8876 if (cp_parser_error_occurred (parser))
8878 /* Handle function definitions specially. */
8879 if (function_definition_p)
8881 /* If the next token is a `,', then we are probably
8882 processing something like:
8886 which is erroneous. */
8887 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8889 cp_token *token = cp_lexer_peek_token (parser->lexer);
8890 error_at (token->location,
8892 " declarations and function-definitions is forbidden");
8894 /* Otherwise, we're done with the list of declarators. */
8897 pop_deferring_access_checks ();
8901 /* The next token should be either a `,' or a `;'. */
8902 token = cp_lexer_peek_token (parser->lexer);
8903 /* If it's a `,', there are more declarators to come. */
8904 if (token->type == CPP_COMMA)
8905 /* will be consumed next time around */;
8906 /* If it's a `;', we are done. */
8907 else if (token->type == CPP_SEMICOLON)
8909 /* Anything else is an error. */
8912 /* If we have already issued an error message we don't need
8913 to issue another one. */
8914 if (decl != error_mark_node
8915 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8916 cp_parser_error (parser, "expected %<,%> or %<;%>");
8917 /* Skip tokens until we reach the end of the statement. */
8918 cp_parser_skip_to_end_of_statement (parser);
8919 /* If the next token is now a `;', consume it. */
8920 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8921 cp_lexer_consume_token (parser->lexer);
8924 /* After the first time around, a function-definition is not
8925 allowed -- even if it was OK at first. For example:
8930 function_definition_allowed_p = false;
8933 /* Issue an error message if no declarators are present, and the
8934 decl-specifier-seq does not itself declare a class or
8936 if (!saw_declarator)
8938 if (cp_parser_declares_only_class_p (parser))
8939 shadow_tag (&decl_specifiers);
8940 /* Perform any deferred access checks. */
8941 perform_deferred_access_checks ();
8944 /* Consume the `;'. */
8945 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8948 pop_deferring_access_checks ();
8951 /* Parse a decl-specifier-seq.
8954 decl-specifier-seq [opt] decl-specifier
8957 storage-class-specifier
8968 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8970 The parser flags FLAGS is used to control type-specifier parsing.
8972 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8975 1: one of the decl-specifiers is an elaborated-type-specifier
8976 (i.e., a type declaration)
8977 2: one of the decl-specifiers is an enum-specifier or a
8978 class-specifier (i.e., a type definition)
8983 cp_parser_decl_specifier_seq (cp_parser* parser,
8984 cp_parser_flags flags,
8985 cp_decl_specifier_seq *decl_specs,
8986 int* declares_class_or_enum)
8988 bool constructor_possible_p = !parser->in_declarator_p;
8989 cp_token *start_token = NULL;
8991 /* Clear DECL_SPECS. */
8992 clear_decl_specs (decl_specs);
8994 /* Assume no class or enumeration type is declared. */
8995 *declares_class_or_enum = 0;
8997 /* Keep reading specifiers until there are no more to read. */
9001 bool found_decl_spec;
9004 /* Peek at the next token. */
9005 token = cp_lexer_peek_token (parser->lexer);
9007 /* Save the first token of the decl spec list for error
9010 start_token = token;
9011 /* Handle attributes. */
9012 if (token->keyword == RID_ATTRIBUTE)
9014 /* Parse the attributes. */
9015 decl_specs->attributes
9016 = chainon (decl_specs->attributes,
9017 cp_parser_attributes_opt (parser));
9020 /* Assume we will find a decl-specifier keyword. */
9021 found_decl_spec = true;
9022 /* If the next token is an appropriate keyword, we can simply
9023 add it to the list. */
9024 switch (token->keyword)
9030 if (!at_class_scope_p ())
9032 error_at (token->location, "%<friend%> used outside of class");
9033 cp_lexer_purge_token (parser->lexer);
9037 ++decl_specs->specs[(int) ds_friend];
9038 /* Consume the token. */
9039 cp_lexer_consume_token (parser->lexer);
9044 ++decl_specs->specs[(int) ds_constexpr];
9045 cp_lexer_consume_token (parser->lexer);
9048 /* function-specifier:
9055 cp_parser_function_specifier_opt (parser, decl_specs);
9061 ++decl_specs->specs[(int) ds_typedef];
9062 /* Consume the token. */
9063 cp_lexer_consume_token (parser->lexer);
9064 /* A constructor declarator cannot appear in a typedef. */
9065 constructor_possible_p = false;
9066 /* The "typedef" keyword can only occur in a declaration; we
9067 may as well commit at this point. */
9068 cp_parser_commit_to_tentative_parse (parser);
9070 if (decl_specs->storage_class != sc_none)
9071 decl_specs->conflicting_specifiers_p = true;
9074 /* storage-class-specifier:
9084 if (cxx_dialect == cxx98)
9086 /* Consume the token. */
9087 cp_lexer_consume_token (parser->lexer);
9089 /* Complain about `auto' as a storage specifier, if
9090 we're complaining about C++0x compatibility. */
9091 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9092 " will change meaning in C++0x; please remove it");
9094 /* Set the storage class anyway. */
9095 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9099 /* C++0x auto type-specifier. */
9100 found_decl_spec = false;
9107 /* Consume the token. */
9108 cp_lexer_consume_token (parser->lexer);
9109 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9113 /* Consume the token. */
9114 cp_lexer_consume_token (parser->lexer);
9115 ++decl_specs->specs[(int) ds_thread];
9119 /* We did not yet find a decl-specifier yet. */
9120 found_decl_spec = false;
9124 /* Constructors are a special case. The `S' in `S()' is not a
9125 decl-specifier; it is the beginning of the declarator. */
9128 && constructor_possible_p
9129 && (cp_parser_constructor_declarator_p
9130 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9132 /* If we don't have a DECL_SPEC yet, then we must be looking at
9133 a type-specifier. */
9134 if (!found_decl_spec && !constructor_p)
9136 int decl_spec_declares_class_or_enum;
9137 bool is_cv_qualifier;
9141 = cp_parser_type_specifier (parser, flags,
9143 /*is_declaration=*/true,
9144 &decl_spec_declares_class_or_enum,
9146 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9148 /* If this type-specifier referenced a user-defined type
9149 (a typedef, class-name, etc.), then we can't allow any
9150 more such type-specifiers henceforth.
9154 The longest sequence of decl-specifiers that could
9155 possibly be a type name is taken as the
9156 decl-specifier-seq of a declaration. The sequence shall
9157 be self-consistent as described below.
9161 As a general rule, at most one type-specifier is allowed
9162 in the complete decl-specifier-seq of a declaration. The
9163 only exceptions are the following:
9165 -- const or volatile can be combined with any other
9168 -- signed or unsigned can be combined with char, long,
9176 void g (const int Pc);
9178 Here, Pc is *not* part of the decl-specifier seq; it's
9179 the declarator. Therefore, once we see a type-specifier
9180 (other than a cv-qualifier), we forbid any additional
9181 user-defined types. We *do* still allow things like `int
9182 int' to be considered a decl-specifier-seq, and issue the
9183 error message later. */
9184 if (type_spec && !is_cv_qualifier)
9185 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9186 /* A constructor declarator cannot follow a type-specifier. */
9189 constructor_possible_p = false;
9190 found_decl_spec = true;
9194 /* If we still do not have a DECL_SPEC, then there are no more
9196 if (!found_decl_spec)
9199 decl_specs->any_specifiers_p = true;
9200 /* After we see one decl-specifier, further decl-specifiers are
9202 flags |= CP_PARSER_FLAGS_OPTIONAL;
9205 cp_parser_check_decl_spec (decl_specs, start_token->location);
9207 /* Don't allow a friend specifier with a class definition. */
9208 if (decl_specs->specs[(int) ds_friend] != 0
9209 && (*declares_class_or_enum & 2))
9210 error_at (start_token->location,
9211 "class definition may not be declared a friend");
9214 /* Parse an (optional) storage-class-specifier.
9216 storage-class-specifier:
9225 storage-class-specifier:
9228 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9231 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9233 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9236 if (cxx_dialect != cxx98)
9238 /* Fall through for C++98. */
9245 /* Consume the token. */
9246 return cp_lexer_consume_token (parser->lexer)->u.value;
9253 /* Parse an (optional) function-specifier.
9260 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9261 Updates DECL_SPECS, if it is non-NULL. */
9264 cp_parser_function_specifier_opt (cp_parser* parser,
9265 cp_decl_specifier_seq *decl_specs)
9267 cp_token *token = cp_lexer_peek_token (parser->lexer);
9268 switch (token->keyword)
9272 ++decl_specs->specs[(int) ds_inline];
9276 /* 14.5.2.3 [temp.mem]
9278 A member function template shall not be virtual. */
9279 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9280 error_at (token->location, "templates may not be %<virtual%>");
9281 else if (decl_specs)
9282 ++decl_specs->specs[(int) ds_virtual];
9287 ++decl_specs->specs[(int) ds_explicit];
9294 /* Consume the token. */
9295 return cp_lexer_consume_token (parser->lexer)->u.value;
9298 /* Parse a linkage-specification.
9300 linkage-specification:
9301 extern string-literal { declaration-seq [opt] }
9302 extern string-literal declaration */
9305 cp_parser_linkage_specification (cp_parser* parser)
9309 /* Look for the `extern' keyword. */
9310 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9312 /* Look for the string-literal. */
9313 linkage = cp_parser_string_literal (parser, false, false);
9315 /* Transform the literal into an identifier. If the literal is a
9316 wide-character string, or contains embedded NULs, then we can't
9317 handle it as the user wants. */
9318 if (strlen (TREE_STRING_POINTER (linkage))
9319 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9321 cp_parser_error (parser, "invalid linkage-specification");
9322 /* Assume C++ linkage. */
9323 linkage = lang_name_cplusplus;
9326 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9328 /* We're now using the new linkage. */
9329 push_lang_context (linkage);
9331 /* If the next token is a `{', then we're using the first
9333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9335 /* Consume the `{' token. */
9336 cp_lexer_consume_token (parser->lexer);
9337 /* Parse the declarations. */
9338 cp_parser_declaration_seq_opt (parser);
9339 /* Look for the closing `}'. */
9340 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9342 /* Otherwise, there's just one declaration. */
9345 bool saved_in_unbraced_linkage_specification_p;
9347 saved_in_unbraced_linkage_specification_p
9348 = parser->in_unbraced_linkage_specification_p;
9349 parser->in_unbraced_linkage_specification_p = true;
9350 cp_parser_declaration (parser);
9351 parser->in_unbraced_linkage_specification_p
9352 = saved_in_unbraced_linkage_specification_p;
9355 /* We're done with the linkage-specification. */
9356 pop_lang_context ();
9359 /* Parse a static_assert-declaration.
9361 static_assert-declaration:
9362 static_assert ( constant-expression , string-literal ) ;
9364 If MEMBER_P, this static_assert is a class member. */
9367 cp_parser_static_assert(cp_parser *parser, bool member_p)
9372 location_t saved_loc;
9374 /* Peek at the `static_assert' token so we can keep track of exactly
9375 where the static assertion started. */
9376 token = cp_lexer_peek_token (parser->lexer);
9377 saved_loc = token->location;
9379 /* Look for the `static_assert' keyword. */
9380 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9381 "%<static_assert%>"))
9384 /* We know we are in a static assertion; commit to any tentative
9386 if (cp_parser_parsing_tentatively (parser))
9387 cp_parser_commit_to_tentative_parse (parser);
9389 /* Parse the `(' starting the static assertion condition. */
9390 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9392 /* Parse the constant-expression. */
9394 cp_parser_constant_expression (parser,
9395 /*allow_non_constant_p=*/false,
9396 /*non_constant_p=*/NULL);
9398 /* Parse the separating `,'. */
9399 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9401 /* Parse the string-literal message. */
9402 message = cp_parser_string_literal (parser,
9403 /*translate=*/false,
9406 /* A `)' completes the static assertion. */
9407 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9408 cp_parser_skip_to_closing_parenthesis (parser,
9409 /*recovering=*/true,
9411 /*consume_paren=*/true);
9413 /* A semicolon terminates the declaration. */
9414 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9416 /* Complete the static assertion, which may mean either processing
9417 the static assert now or saving it for template instantiation. */
9418 finish_static_assert (condition, message, saved_loc, member_p);
9421 /* Parse a `decltype' type. Returns the type.
9423 simple-type-specifier:
9424 decltype ( expression ) */
9427 cp_parser_decltype (cp_parser *parser)
9430 bool id_expression_or_member_access_p = false;
9431 const char *saved_message;
9432 bool saved_integral_constant_expression_p;
9433 bool saved_non_integral_constant_expression_p;
9434 cp_token *id_expr_start_token;
9436 /* Look for the `decltype' token. */
9437 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9438 return error_mark_node;
9440 /* Types cannot be defined in a `decltype' expression. Save away the
9442 saved_message = parser->type_definition_forbidden_message;
9444 /* And create the new one. */
9445 parser->type_definition_forbidden_message
9446 = "types may not be defined in %<decltype%> expressions";
9448 /* The restrictions on constant-expressions do not apply inside
9449 decltype expressions. */
9450 saved_integral_constant_expression_p
9451 = parser->integral_constant_expression_p;
9452 saved_non_integral_constant_expression_p
9453 = parser->non_integral_constant_expression_p;
9454 parser->integral_constant_expression_p = false;
9456 /* Do not actually evaluate the expression. */
9457 ++cp_unevaluated_operand;
9459 /* Do not warn about problems with the expression. */
9460 ++c_inhibit_evaluation_warnings;
9462 /* Parse the opening `('. */
9463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9464 return error_mark_node;
9466 /* First, try parsing an id-expression. */
9467 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9468 cp_parser_parse_tentatively (parser);
9469 expr = cp_parser_id_expression (parser,
9470 /*template_keyword_p=*/false,
9471 /*check_dependency_p=*/true,
9472 /*template_p=*/NULL,
9473 /*declarator_p=*/false,
9474 /*optional_p=*/false);
9476 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9478 bool non_integral_constant_expression_p = false;
9479 tree id_expression = expr;
9481 const char *error_msg;
9483 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9484 /* Lookup the name we got back from the id-expression. */
9485 expr = cp_parser_lookup_name (parser, expr,
9487 /*is_template=*/false,
9488 /*is_namespace=*/false,
9489 /*check_dependency=*/true,
9490 /*ambiguous_decls=*/NULL,
9491 id_expr_start_token->location);
9494 && expr != error_mark_node
9495 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9496 && TREE_CODE (expr) != TYPE_DECL
9497 && (TREE_CODE (expr) != BIT_NOT_EXPR
9498 || !TYPE_P (TREE_OPERAND (expr, 0)))
9499 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9501 /* Complete lookup of the id-expression. */
9502 expr = (finish_id_expression
9503 (id_expression, expr, parser->scope, &idk,
9504 /*integral_constant_expression_p=*/false,
9505 /*allow_non_integral_constant_expression_p=*/true,
9506 &non_integral_constant_expression_p,
9507 /*template_p=*/false,
9509 /*address_p=*/false,
9510 /*template_arg_p=*/false,
9512 id_expr_start_token->location));
9514 if (expr == error_mark_node)
9515 /* We found an id-expression, but it was something that we
9516 should not have found. This is an error, not something
9517 we can recover from, so note that we found an
9518 id-expression and we'll recover as gracefully as
9520 id_expression_or_member_access_p = true;
9524 && expr != error_mark_node
9525 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9526 /* We have an id-expression. */
9527 id_expression_or_member_access_p = true;
9530 if (!id_expression_or_member_access_p)
9532 /* Abort the id-expression parse. */
9533 cp_parser_abort_tentative_parse (parser);
9535 /* Parsing tentatively, again. */
9536 cp_parser_parse_tentatively (parser);
9538 /* Parse a class member access. */
9539 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9541 /*member_access_only_p=*/true, NULL);
9544 && expr != error_mark_node
9545 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9546 /* We have an id-expression. */
9547 id_expression_or_member_access_p = true;
9550 if (id_expression_or_member_access_p)
9551 /* We have parsed the complete id-expression or member access. */
9552 cp_parser_parse_definitely (parser);
9555 bool saved_greater_than_is_operator_p;
9557 /* Abort our attempt to parse an id-expression or member access
9559 cp_parser_abort_tentative_parse (parser);
9561 /* Within a parenthesized expression, a `>' token is always
9562 the greater-than operator. */
9563 saved_greater_than_is_operator_p
9564 = parser->greater_than_is_operator_p;
9565 parser->greater_than_is_operator_p = true;
9567 /* Parse a full expression. */
9568 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9570 /* The `>' token might be the end of a template-id or
9571 template-parameter-list now. */
9572 parser->greater_than_is_operator_p
9573 = saved_greater_than_is_operator_p;
9576 /* Go back to evaluating expressions. */
9577 --cp_unevaluated_operand;
9578 --c_inhibit_evaluation_warnings;
9580 /* Restore the old message and the integral constant expression
9582 parser->type_definition_forbidden_message = saved_message;
9583 parser->integral_constant_expression_p
9584 = saved_integral_constant_expression_p;
9585 parser->non_integral_constant_expression_p
9586 = saved_non_integral_constant_expression_p;
9588 if (expr == error_mark_node)
9590 /* Skip everything up to the closing `)'. */
9591 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9592 /*consume_paren=*/true);
9593 return error_mark_node;
9596 /* Parse to the closing `)'. */
9597 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9599 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9600 /*consume_paren=*/true);
9601 return error_mark_node;
9604 return finish_decltype_type (expr, id_expression_or_member_access_p);
9607 /* Special member functions [gram.special] */
9609 /* Parse a conversion-function-id.
9611 conversion-function-id:
9612 operator conversion-type-id
9614 Returns an IDENTIFIER_NODE representing the operator. */
9617 cp_parser_conversion_function_id (cp_parser* parser)
9621 tree saved_qualifying_scope;
9622 tree saved_object_scope;
9623 tree pushed_scope = NULL_TREE;
9625 /* Look for the `operator' token. */
9626 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9627 return error_mark_node;
9628 /* When we parse the conversion-type-id, the current scope will be
9629 reset. However, we need that information in able to look up the
9630 conversion function later, so we save it here. */
9631 saved_scope = parser->scope;
9632 saved_qualifying_scope = parser->qualifying_scope;
9633 saved_object_scope = parser->object_scope;
9634 /* We must enter the scope of the class so that the names of
9635 entities declared within the class are available in the
9636 conversion-type-id. For example, consider:
9643 S::operator I() { ... }
9645 In order to see that `I' is a type-name in the definition, we
9646 must be in the scope of `S'. */
9648 pushed_scope = push_scope (saved_scope);
9649 /* Parse the conversion-type-id. */
9650 type = cp_parser_conversion_type_id (parser);
9651 /* Leave the scope of the class, if any. */
9653 pop_scope (pushed_scope);
9654 /* Restore the saved scope. */
9655 parser->scope = saved_scope;
9656 parser->qualifying_scope = saved_qualifying_scope;
9657 parser->object_scope = saved_object_scope;
9658 /* If the TYPE is invalid, indicate failure. */
9659 if (type == error_mark_node)
9660 return error_mark_node;
9661 return mangle_conv_op_name_for_type (type);
9664 /* Parse a conversion-type-id:
9667 type-specifier-seq conversion-declarator [opt]
9669 Returns the TYPE specified. */
9672 cp_parser_conversion_type_id (cp_parser* parser)
9675 cp_decl_specifier_seq type_specifiers;
9676 cp_declarator *declarator;
9677 tree type_specified;
9679 /* Parse the attributes. */
9680 attributes = cp_parser_attributes_opt (parser);
9681 /* Parse the type-specifiers. */
9682 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9683 /*is_trailing_return=*/false,
9685 /* If that didn't work, stop. */
9686 if (type_specifiers.type == error_mark_node)
9687 return error_mark_node;
9688 /* Parse the conversion-declarator. */
9689 declarator = cp_parser_conversion_declarator_opt (parser);
9691 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9692 /*initialized=*/0, &attributes);
9694 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9696 /* Don't give this error when parsing tentatively. This happens to
9697 work because we always parse this definitively once. */
9698 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9699 && type_uses_auto (type_specified))
9701 error ("invalid use of %<auto%> in conversion operator");
9702 return error_mark_node;
9705 return type_specified;
9708 /* Parse an (optional) conversion-declarator.
9710 conversion-declarator:
9711 ptr-operator conversion-declarator [opt]
9715 static cp_declarator *
9716 cp_parser_conversion_declarator_opt (cp_parser* parser)
9718 enum tree_code code;
9720 cp_cv_quals cv_quals;
9722 /* We don't know if there's a ptr-operator next, or not. */
9723 cp_parser_parse_tentatively (parser);
9724 /* Try the ptr-operator. */
9725 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9726 /* If it worked, look for more conversion-declarators. */
9727 if (cp_parser_parse_definitely (parser))
9729 cp_declarator *declarator;
9731 /* Parse another optional declarator. */
9732 declarator = cp_parser_conversion_declarator_opt (parser);
9734 return cp_parser_make_indirect_declarator
9735 (code, class_type, cv_quals, declarator);
9741 /* Parse an (optional) ctor-initializer.
9744 : mem-initializer-list
9746 Returns TRUE iff the ctor-initializer was actually present. */
9749 cp_parser_ctor_initializer_opt (cp_parser* parser)
9751 /* If the next token is not a `:', then there is no
9752 ctor-initializer. */
9753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9755 /* Do default initialization of any bases and members. */
9756 if (DECL_CONSTRUCTOR_P (current_function_decl))
9757 finish_mem_initializers (NULL_TREE);
9762 /* Consume the `:' token. */
9763 cp_lexer_consume_token (parser->lexer);
9764 /* And the mem-initializer-list. */
9765 cp_parser_mem_initializer_list (parser);
9770 /* Parse a mem-initializer-list.
9772 mem-initializer-list:
9773 mem-initializer ... [opt]
9774 mem-initializer ... [opt] , mem-initializer-list */
9777 cp_parser_mem_initializer_list (cp_parser* parser)
9779 tree mem_initializer_list = NULL_TREE;
9780 cp_token *token = cp_lexer_peek_token (parser->lexer);
9782 /* Let the semantic analysis code know that we are starting the
9783 mem-initializer-list. */
9784 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9785 error_at (token->location,
9786 "only constructors take base initializers");
9788 /* Loop through the list. */
9791 tree mem_initializer;
9793 token = cp_lexer_peek_token (parser->lexer);
9794 /* Parse the mem-initializer. */
9795 mem_initializer = cp_parser_mem_initializer (parser);
9796 /* If the next token is a `...', we're expanding member initializers. */
9797 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9799 /* Consume the `...'. */
9800 cp_lexer_consume_token (parser->lexer);
9802 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9803 can be expanded but members cannot. */
9804 if (mem_initializer != error_mark_node
9805 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9807 error_at (token->location,
9808 "cannot expand initializer for member %<%D%>",
9809 TREE_PURPOSE (mem_initializer));
9810 mem_initializer = error_mark_node;
9813 /* Construct the pack expansion type. */
9814 if (mem_initializer != error_mark_node)
9815 mem_initializer = make_pack_expansion (mem_initializer);
9817 /* Add it to the list, unless it was erroneous. */
9818 if (mem_initializer != error_mark_node)
9820 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9821 mem_initializer_list = mem_initializer;
9823 /* If the next token is not a `,', we're done. */
9824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9826 /* Consume the `,' token. */
9827 cp_lexer_consume_token (parser->lexer);
9830 /* Perform semantic analysis. */
9831 if (DECL_CONSTRUCTOR_P (current_function_decl))
9832 finish_mem_initializers (mem_initializer_list);
9835 /* Parse a mem-initializer.
9838 mem-initializer-id ( expression-list [opt] )
9839 mem-initializer-id braced-init-list
9844 ( expression-list [opt] )
9846 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9847 class) or FIELD_DECL (for a non-static data member) to initialize;
9848 the TREE_VALUE is the expression-list. An empty initialization
9849 list is represented by void_list_node. */
9852 cp_parser_mem_initializer (cp_parser* parser)
9854 tree mem_initializer_id;
9855 tree expression_list;
9857 cp_token *token = cp_lexer_peek_token (parser->lexer);
9859 /* Find out what is being initialized. */
9860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9862 permerror (token->location,
9863 "anachronistic old-style base class initializer");
9864 mem_initializer_id = NULL_TREE;
9868 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9869 if (mem_initializer_id == error_mark_node)
9870 return mem_initializer_id;
9872 member = expand_member_init (mem_initializer_id);
9873 if (member && !DECL_P (member))
9874 in_base_initializer = 1;
9876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9878 bool expr_non_constant_p;
9879 maybe_warn_cpp0x ("extended initializer lists");
9880 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9881 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9882 expression_list = build_tree_list (NULL_TREE, expression_list);
9887 vec = cp_parser_parenthesized_expression_list (parser, false,
9889 /*allow_expansion_p=*/true,
9890 /*non_constant_p=*/NULL);
9892 return error_mark_node;
9893 expression_list = build_tree_list_vec (vec);
9894 release_tree_vector (vec);
9897 if (expression_list == error_mark_node)
9898 return error_mark_node;
9899 if (!expression_list)
9900 expression_list = void_type_node;
9902 in_base_initializer = 0;
9904 return member ? build_tree_list (member, expression_list) : error_mark_node;
9907 /* Parse a mem-initializer-id.
9910 :: [opt] nested-name-specifier [opt] class-name
9913 Returns a TYPE indicating the class to be initializer for the first
9914 production. Returns an IDENTIFIER_NODE indicating the data member
9915 to be initialized for the second production. */
9918 cp_parser_mem_initializer_id (cp_parser* parser)
9920 bool global_scope_p;
9921 bool nested_name_specifier_p;
9922 bool template_p = false;
9925 cp_token *token = cp_lexer_peek_token (parser->lexer);
9927 /* `typename' is not allowed in this context ([temp.res]). */
9928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9930 error_at (token->location,
9931 "keyword %<typename%> not allowed in this context (a qualified "
9932 "member initializer is implicitly a type)");
9933 cp_lexer_consume_token (parser->lexer);
9935 /* Look for the optional `::' operator. */
9937 = (cp_parser_global_scope_opt (parser,
9938 /*current_scope_valid_p=*/false)
9940 /* Look for the optional nested-name-specifier. The simplest way to
9945 The keyword `typename' is not permitted in a base-specifier or
9946 mem-initializer; in these contexts a qualified name that
9947 depends on a template-parameter is implicitly assumed to be a
9950 is to assume that we have seen the `typename' keyword at this
9952 nested_name_specifier_p
9953 = (cp_parser_nested_name_specifier_opt (parser,
9954 /*typename_keyword_p=*/true,
9955 /*check_dependency_p=*/true,
9957 /*is_declaration=*/true)
9959 if (nested_name_specifier_p)
9960 template_p = cp_parser_optional_template_keyword (parser);
9961 /* If there is a `::' operator or a nested-name-specifier, then we
9962 are definitely looking for a class-name. */
9963 if (global_scope_p || nested_name_specifier_p)
9964 return cp_parser_class_name (parser,
9965 /*typename_keyword_p=*/true,
9966 /*template_keyword_p=*/template_p,
9968 /*check_dependency_p=*/true,
9969 /*class_head_p=*/false,
9970 /*is_declaration=*/true);
9971 /* Otherwise, we could also be looking for an ordinary identifier. */
9972 cp_parser_parse_tentatively (parser);
9973 /* Try a class-name. */
9974 id = cp_parser_class_name (parser,
9975 /*typename_keyword_p=*/true,
9976 /*template_keyword_p=*/false,
9978 /*check_dependency_p=*/true,
9979 /*class_head_p=*/false,
9980 /*is_declaration=*/true);
9981 /* If we found one, we're done. */
9982 if (cp_parser_parse_definitely (parser))
9984 /* Otherwise, look for an ordinary identifier. */
9985 return cp_parser_identifier (parser);
9988 /* Overloading [gram.over] */
9990 /* Parse an operator-function-id.
9992 operator-function-id:
9995 Returns an IDENTIFIER_NODE for the operator which is a
9996 human-readable spelling of the identifier, e.g., `operator +'. */
9999 cp_parser_operator_function_id (cp_parser* parser)
10001 /* Look for the `operator' keyword. */
10002 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10003 return error_mark_node;
10004 /* And then the name of the operator itself. */
10005 return cp_parser_operator (parser);
10008 /* Parse an operator.
10011 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10012 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10013 || ++ -- , ->* -> () []
10020 Returns an IDENTIFIER_NODE for the operator which is a
10021 human-readable spelling of the identifier, e.g., `operator +'. */
10024 cp_parser_operator (cp_parser* parser)
10026 tree id = NULL_TREE;
10029 /* Peek at the next token. */
10030 token = cp_lexer_peek_token (parser->lexer);
10031 /* Figure out which operator we have. */
10032 switch (token->type)
10038 /* The keyword should be either `new' or `delete'. */
10039 if (token->keyword == RID_NEW)
10041 else if (token->keyword == RID_DELETE)
10046 /* Consume the `new' or `delete' token. */
10047 cp_lexer_consume_token (parser->lexer);
10049 /* Peek at the next token. */
10050 token = cp_lexer_peek_token (parser->lexer);
10051 /* If it's a `[' token then this is the array variant of the
10053 if (token->type == CPP_OPEN_SQUARE)
10055 /* Consume the `[' token. */
10056 cp_lexer_consume_token (parser->lexer);
10057 /* Look for the `]' token. */
10058 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10059 id = ansi_opname (op == NEW_EXPR
10060 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10062 /* Otherwise, we have the non-array variant. */
10064 id = ansi_opname (op);
10070 id = ansi_opname (PLUS_EXPR);
10074 id = ansi_opname (MINUS_EXPR);
10078 id = ansi_opname (MULT_EXPR);
10082 id = ansi_opname (TRUNC_DIV_EXPR);
10086 id = ansi_opname (TRUNC_MOD_EXPR);
10090 id = ansi_opname (BIT_XOR_EXPR);
10094 id = ansi_opname (BIT_AND_EXPR);
10098 id = ansi_opname (BIT_IOR_EXPR);
10102 id = ansi_opname (BIT_NOT_EXPR);
10106 id = ansi_opname (TRUTH_NOT_EXPR);
10110 id = ansi_assopname (NOP_EXPR);
10114 id = ansi_opname (LT_EXPR);
10118 id = ansi_opname (GT_EXPR);
10122 id = ansi_assopname (PLUS_EXPR);
10126 id = ansi_assopname (MINUS_EXPR);
10130 id = ansi_assopname (MULT_EXPR);
10134 id = ansi_assopname (TRUNC_DIV_EXPR);
10138 id = ansi_assopname (TRUNC_MOD_EXPR);
10142 id = ansi_assopname (BIT_XOR_EXPR);
10146 id = ansi_assopname (BIT_AND_EXPR);
10150 id = ansi_assopname (BIT_IOR_EXPR);
10154 id = ansi_opname (LSHIFT_EXPR);
10158 id = ansi_opname (RSHIFT_EXPR);
10161 case CPP_LSHIFT_EQ:
10162 id = ansi_assopname (LSHIFT_EXPR);
10165 case CPP_RSHIFT_EQ:
10166 id = ansi_assopname (RSHIFT_EXPR);
10170 id = ansi_opname (EQ_EXPR);
10174 id = ansi_opname (NE_EXPR);
10178 id = ansi_opname (LE_EXPR);
10181 case CPP_GREATER_EQ:
10182 id = ansi_opname (GE_EXPR);
10186 id = ansi_opname (TRUTH_ANDIF_EXPR);
10190 id = ansi_opname (TRUTH_ORIF_EXPR);
10193 case CPP_PLUS_PLUS:
10194 id = ansi_opname (POSTINCREMENT_EXPR);
10197 case CPP_MINUS_MINUS:
10198 id = ansi_opname (PREDECREMENT_EXPR);
10202 id = ansi_opname (COMPOUND_EXPR);
10205 case CPP_DEREF_STAR:
10206 id = ansi_opname (MEMBER_REF);
10210 id = ansi_opname (COMPONENT_REF);
10213 case CPP_OPEN_PAREN:
10214 /* Consume the `('. */
10215 cp_lexer_consume_token (parser->lexer);
10216 /* Look for the matching `)'. */
10217 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10218 return ansi_opname (CALL_EXPR);
10220 case CPP_OPEN_SQUARE:
10221 /* Consume the `['. */
10222 cp_lexer_consume_token (parser->lexer);
10223 /* Look for the matching `]'. */
10224 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10225 return ansi_opname (ARRAY_REF);
10228 /* Anything else is an error. */
10232 /* If we have selected an identifier, we need to consume the
10235 cp_lexer_consume_token (parser->lexer);
10236 /* Otherwise, no valid operator name was present. */
10239 cp_parser_error (parser, "expected operator");
10240 id = error_mark_node;
10246 /* Parse a template-declaration.
10248 template-declaration:
10249 export [opt] template < template-parameter-list > declaration
10251 If MEMBER_P is TRUE, this template-declaration occurs within a
10254 The grammar rule given by the standard isn't correct. What
10255 is really meant is:
10257 template-declaration:
10258 export [opt] template-parameter-list-seq
10259 decl-specifier-seq [opt] init-declarator [opt] ;
10260 export [opt] template-parameter-list-seq
10261 function-definition
10263 template-parameter-list-seq:
10264 template-parameter-list-seq [opt]
10265 template < template-parameter-list > */
10268 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10270 /* Check for `export'. */
10271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10273 /* Consume the `export' token. */
10274 cp_lexer_consume_token (parser->lexer);
10275 /* Warn that we do not support `export'. */
10276 warning (0, "keyword %<export%> not implemented, and will be ignored");
10279 cp_parser_template_declaration_after_export (parser, member_p);
10282 /* Parse a template-parameter-list.
10284 template-parameter-list:
10286 template-parameter-list , template-parameter
10288 Returns a TREE_LIST. Each node represents a template parameter.
10289 The nodes are connected via their TREE_CHAINs. */
10292 cp_parser_template_parameter_list (cp_parser* parser)
10294 tree parameter_list = NULL_TREE;
10296 begin_template_parm_list ();
10301 bool is_parameter_pack;
10302 location_t parm_loc;
10304 /* Parse the template-parameter. */
10305 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10306 parameter = cp_parser_template_parameter (parser,
10308 &is_parameter_pack);
10309 /* Add it to the list. */
10310 if (parameter != error_mark_node)
10311 parameter_list = process_template_parm (parameter_list,
10315 is_parameter_pack);
10318 tree err_parm = build_tree_list (parameter, parameter);
10319 TREE_VALUE (err_parm) = error_mark_node;
10320 parameter_list = chainon (parameter_list, err_parm);
10323 /* If the next token is not a `,', we're done. */
10324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10326 /* Otherwise, consume the `,' token. */
10327 cp_lexer_consume_token (parser->lexer);
10330 return end_template_parm_list (parameter_list);
10333 /* Parse a template-parameter.
10335 template-parameter:
10337 parameter-declaration
10339 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10340 the parameter. The TREE_PURPOSE is the default value, if any.
10341 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10342 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10343 set to true iff this parameter is a parameter pack. */
10346 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10347 bool *is_parameter_pack)
10350 cp_parameter_declarator *parameter_declarator;
10351 cp_declarator *id_declarator;
10354 /* Assume it is a type parameter or a template parameter. */
10355 *is_non_type = false;
10356 /* Assume it not a parameter pack. */
10357 *is_parameter_pack = false;
10358 /* Peek at the next token. */
10359 token = cp_lexer_peek_token (parser->lexer);
10360 /* If it is `class' or `template', we have a type-parameter. */
10361 if (token->keyword == RID_TEMPLATE)
10362 return cp_parser_type_parameter (parser, is_parameter_pack);
10363 /* If it is `class' or `typename' we do not know yet whether it is a
10364 type parameter or a non-type parameter. Consider:
10366 template <typename T, typename T::X X> ...
10370 template <class C, class D*> ...
10372 Here, the first parameter is a type parameter, and the second is
10373 a non-type parameter. We can tell by looking at the token after
10374 the identifier -- if it is a `,', `=', or `>' then we have a type
10376 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10378 /* Peek at the token after `class' or `typename'. */
10379 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10380 /* If it's an ellipsis, we have a template type parameter
10382 if (token->type == CPP_ELLIPSIS)
10383 return cp_parser_type_parameter (parser, is_parameter_pack);
10384 /* If it's an identifier, skip it. */
10385 if (token->type == CPP_NAME)
10386 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10387 /* Now, see if the token looks like the end of a template
10389 if (token->type == CPP_COMMA
10390 || token->type == CPP_EQ
10391 || token->type == CPP_GREATER)
10392 return cp_parser_type_parameter (parser, is_parameter_pack);
10395 /* Otherwise, it is a non-type parameter.
10399 When parsing a default template-argument for a non-type
10400 template-parameter, the first non-nested `>' is taken as the end
10401 of the template parameter-list rather than a greater-than
10403 *is_non_type = true;
10404 parameter_declarator
10405 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10406 /*parenthesized_p=*/NULL);
10408 /* If the parameter declaration is marked as a parameter pack, set
10409 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10410 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10412 if (parameter_declarator
10413 && parameter_declarator->declarator
10414 && parameter_declarator->declarator->parameter_pack_p)
10416 *is_parameter_pack = true;
10417 parameter_declarator->declarator->parameter_pack_p = false;
10420 /* If the next token is an ellipsis, and we don't already have it
10421 marked as a parameter pack, then we have a parameter pack (that
10422 has no declarator). */
10423 if (!*is_parameter_pack
10424 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10425 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10427 /* Consume the `...'. */
10428 cp_lexer_consume_token (parser->lexer);
10429 maybe_warn_variadic_templates ();
10431 *is_parameter_pack = true;
10433 /* We might end up with a pack expansion as the type of the non-type
10434 template parameter, in which case this is a non-type template
10436 else if (parameter_declarator
10437 && parameter_declarator->decl_specifiers.type
10438 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10440 *is_parameter_pack = true;
10441 parameter_declarator->decl_specifiers.type =
10442 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10445 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10447 /* Parameter packs cannot have default arguments. However, a
10448 user may try to do so, so we'll parse them and give an
10449 appropriate diagnostic here. */
10451 /* Consume the `='. */
10452 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10453 cp_lexer_consume_token (parser->lexer);
10455 /* Find the name of the parameter pack. */
10456 id_declarator = parameter_declarator->declarator;
10457 while (id_declarator && id_declarator->kind != cdk_id)
10458 id_declarator = id_declarator->declarator;
10460 if (id_declarator && id_declarator->kind == cdk_id)
10461 error_at (start_token->location,
10462 "template parameter pack %qD cannot have a default argument",
10463 id_declarator->u.id.unqualified_name);
10465 error_at (start_token->location,
10466 "template parameter pack cannot have a default argument");
10468 /* Parse the default argument, but throw away the result. */
10469 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10472 parm = grokdeclarator (parameter_declarator->declarator,
10473 ¶meter_declarator->decl_specifiers,
10474 PARM, /*initialized=*/0,
10475 /*attrlist=*/NULL);
10476 if (parm == error_mark_node)
10477 return error_mark_node;
10479 return build_tree_list (parameter_declarator->default_argument, parm);
10482 /* Parse a type-parameter.
10485 class identifier [opt]
10486 class identifier [opt] = type-id
10487 typename identifier [opt]
10488 typename identifier [opt] = type-id
10489 template < template-parameter-list > class identifier [opt]
10490 template < template-parameter-list > class identifier [opt]
10493 GNU Extension (variadic templates):
10496 class ... identifier [opt]
10497 typename ... identifier [opt]
10499 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10500 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10501 the declaration of the parameter.
10503 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10506 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10511 /* Look for a keyword to tell us what kind of parameter this is. */
10512 token = cp_parser_require (parser, CPP_KEYWORD,
10513 "%<class%>, %<typename%>, or %<template%>");
10515 return error_mark_node;
10517 switch (token->keyword)
10523 tree default_argument;
10525 /* If the next token is an ellipsis, we have a template
10527 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10529 /* Consume the `...' token. */
10530 cp_lexer_consume_token (parser->lexer);
10531 maybe_warn_variadic_templates ();
10533 *is_parameter_pack = true;
10536 /* If the next token is an identifier, then it names the
10538 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10539 identifier = cp_parser_identifier (parser);
10541 identifier = NULL_TREE;
10543 /* Create the parameter. */
10544 parameter = finish_template_type_parm (class_type_node, identifier);
10546 /* If the next token is an `=', we have a default argument. */
10547 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10549 /* Consume the `=' token. */
10550 cp_lexer_consume_token (parser->lexer);
10551 /* Parse the default-argument. */
10552 push_deferring_access_checks (dk_no_deferred);
10553 default_argument = cp_parser_type_id (parser);
10555 /* Template parameter packs cannot have default
10557 if (*is_parameter_pack)
10560 error_at (token->location,
10561 "template parameter pack %qD cannot have a "
10562 "default argument", identifier);
10564 error_at (token->location,
10565 "template parameter packs cannot have "
10566 "default arguments");
10567 default_argument = NULL_TREE;
10569 pop_deferring_access_checks ();
10572 default_argument = NULL_TREE;
10574 /* Create the combined representation of the parameter and the
10575 default argument. */
10576 parameter = build_tree_list (default_argument, parameter);
10582 tree parameter_list;
10584 tree default_argument;
10586 /* Look for the `<'. */
10587 cp_parser_require (parser, CPP_LESS, "%<<%>");
10588 /* Parse the template-parameter-list. */
10589 parameter_list = cp_parser_template_parameter_list (parser);
10590 /* Look for the `>'. */
10591 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10592 /* Look for the `class' keyword. */
10593 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10594 /* If the next token is an ellipsis, we have a template
10596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10598 /* Consume the `...' token. */
10599 cp_lexer_consume_token (parser->lexer);
10600 maybe_warn_variadic_templates ();
10602 *is_parameter_pack = true;
10604 /* If the next token is an `=', then there is a
10605 default-argument. If the next token is a `>', we are at
10606 the end of the parameter-list. If the next token is a `,',
10607 then we are at the end of this parameter. */
10608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10609 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10610 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10612 identifier = cp_parser_identifier (parser);
10613 /* Treat invalid names as if the parameter were nameless. */
10614 if (identifier == error_mark_node)
10615 identifier = NULL_TREE;
10618 identifier = NULL_TREE;
10620 /* Create the template parameter. */
10621 parameter = finish_template_template_parm (class_type_node,
10624 /* If the next token is an `=', then there is a
10625 default-argument. */
10626 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10630 /* Consume the `='. */
10631 cp_lexer_consume_token (parser->lexer);
10632 /* Parse the id-expression. */
10633 push_deferring_access_checks (dk_no_deferred);
10634 /* save token before parsing the id-expression, for error
10636 token = cp_lexer_peek_token (parser->lexer);
10638 = cp_parser_id_expression (parser,
10639 /*template_keyword_p=*/false,
10640 /*check_dependency_p=*/true,
10641 /*template_p=*/&is_template,
10642 /*declarator_p=*/false,
10643 /*optional_p=*/false);
10644 if (TREE_CODE (default_argument) == TYPE_DECL)
10645 /* If the id-expression was a template-id that refers to
10646 a template-class, we already have the declaration here,
10647 so no further lookup is needed. */
10650 /* Look up the name. */
10652 = cp_parser_lookup_name (parser, default_argument,
10654 /*is_template=*/is_template,
10655 /*is_namespace=*/false,
10656 /*check_dependency=*/true,
10657 /*ambiguous_decls=*/NULL,
10659 /* See if the default argument is valid. */
10661 = check_template_template_default_arg (default_argument);
10663 /* Template parameter packs cannot have default
10665 if (*is_parameter_pack)
10668 error_at (token->location,
10669 "template parameter pack %qD cannot "
10670 "have a default argument",
10673 error_at (token->location, "template parameter packs cannot "
10674 "have default arguments");
10675 default_argument = NULL_TREE;
10677 pop_deferring_access_checks ();
10680 default_argument = NULL_TREE;
10682 /* Create the combined representation of the parameter and the
10683 default argument. */
10684 parameter = build_tree_list (default_argument, parameter);
10689 gcc_unreachable ();
10696 /* Parse a template-id.
10699 template-name < template-argument-list [opt] >
10701 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10702 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10703 returned. Otherwise, if the template-name names a function, or set
10704 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10705 names a class, returns a TYPE_DECL for the specialization.
10707 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10708 uninstantiated templates. */
10711 cp_parser_template_id (cp_parser *parser,
10712 bool template_keyword_p,
10713 bool check_dependency_p,
10714 bool is_declaration)
10720 cp_token_position start_of_id = 0;
10721 deferred_access_check *chk;
10722 VEC (deferred_access_check,gc) *access_check;
10723 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10724 bool is_identifier;
10726 /* If the next token corresponds to a template-id, there is no need
10728 next_token = cp_lexer_peek_token (parser->lexer);
10729 if (next_token->type == CPP_TEMPLATE_ID)
10731 struct tree_check *check_value;
10733 /* Get the stored value. */
10734 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10735 /* Perform any access checks that were deferred. */
10736 access_check = check_value->checks;
10740 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10743 perform_or_defer_access_check (chk->binfo,
10748 /* Return the stored value. */
10749 return check_value->value;
10752 /* Avoid performing name lookup if there is no possibility of
10753 finding a template-id. */
10754 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10755 || (next_token->type == CPP_NAME
10756 && !cp_parser_nth_token_starts_template_argument_list_p
10759 cp_parser_error (parser, "expected template-id");
10760 return error_mark_node;
10763 /* Remember where the template-id starts. */
10764 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10765 start_of_id = cp_lexer_token_position (parser->lexer, false);
10767 push_deferring_access_checks (dk_deferred);
10769 /* Parse the template-name. */
10770 is_identifier = false;
10771 token = cp_lexer_peek_token (parser->lexer);
10772 templ = cp_parser_template_name (parser, template_keyword_p,
10773 check_dependency_p,
10776 if (templ == error_mark_node || is_identifier)
10778 pop_deferring_access_checks ();
10782 /* If we find the sequence `[:' after a template-name, it's probably
10783 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10784 parse correctly the argument list. */
10785 next_token = cp_lexer_peek_token (parser->lexer);
10786 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10787 if (next_token->type == CPP_OPEN_SQUARE
10788 && next_token->flags & DIGRAPH
10789 && next_token_2->type == CPP_COLON
10790 && !(next_token_2->flags & PREV_WHITE))
10792 cp_parser_parse_tentatively (parser);
10793 /* Change `:' into `::'. */
10794 next_token_2->type = CPP_SCOPE;
10795 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10797 cp_lexer_consume_token (parser->lexer);
10799 /* Parse the arguments. */
10800 arguments = cp_parser_enclosed_template_argument_list (parser);
10801 if (!cp_parser_parse_definitely (parser))
10803 /* If we couldn't parse an argument list, then we revert our changes
10804 and return simply an error. Maybe this is not a template-id
10806 next_token_2->type = CPP_COLON;
10807 cp_parser_error (parser, "expected %<<%>");
10808 pop_deferring_access_checks ();
10809 return error_mark_node;
10811 /* Otherwise, emit an error about the invalid digraph, but continue
10812 parsing because we got our argument list. */
10813 if (permerror (next_token->location,
10814 "%<<::%> cannot begin a template-argument list"))
10816 static bool hint = false;
10817 inform (next_token->location,
10818 "%<<:%> is an alternate spelling for %<[%>."
10819 " Insert whitespace between %<<%> and %<::%>");
10820 if (!hint && !flag_permissive)
10822 inform (next_token->location, "(if you use %<-fpermissive%>"
10823 " G++ will accept your code)");
10830 /* Look for the `<' that starts the template-argument-list. */
10831 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10833 pop_deferring_access_checks ();
10834 return error_mark_node;
10836 /* Parse the arguments. */
10837 arguments = cp_parser_enclosed_template_argument_list (parser);
10840 /* Build a representation of the specialization. */
10841 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10842 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10843 else if (DECL_CLASS_TEMPLATE_P (templ)
10844 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10846 bool entering_scope;
10847 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10848 template (rather than some instantiation thereof) only if
10849 is not nested within some other construct. For example, in
10850 "template <typename T> void f(T) { A<T>::", A<T> is just an
10851 instantiation of A. */
10852 entering_scope = (template_parm_scope_p ()
10853 && cp_lexer_next_token_is (parser->lexer,
10856 = finish_template_type (templ, arguments, entering_scope);
10860 /* If it's not a class-template or a template-template, it should be
10861 a function-template. */
10862 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10863 || TREE_CODE (templ) == OVERLOAD
10864 || BASELINK_P (templ)));
10866 template_id = lookup_template_function (templ, arguments);
10869 /* If parsing tentatively, replace the sequence of tokens that makes
10870 up the template-id with a CPP_TEMPLATE_ID token. That way,
10871 should we re-parse the token stream, we will not have to repeat
10872 the effort required to do the parse, nor will we issue duplicate
10873 error messages about problems during instantiation of the
10877 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10879 /* Reset the contents of the START_OF_ID token. */
10880 token->type = CPP_TEMPLATE_ID;
10881 /* Retrieve any deferred checks. Do not pop this access checks yet
10882 so the memory will not be reclaimed during token replacing below. */
10883 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10884 token->u.tree_check_value->value = template_id;
10885 token->u.tree_check_value->checks = get_deferred_access_checks ();
10886 token->keyword = RID_MAX;
10888 /* Purge all subsequent tokens. */
10889 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10891 /* ??? Can we actually assume that, if template_id ==
10892 error_mark_node, we will have issued a diagnostic to the
10893 user, as opposed to simply marking the tentative parse as
10895 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10896 error_at (token->location, "parse error in template argument list");
10899 pop_deferring_access_checks ();
10900 return template_id;
10903 /* Parse a template-name.
10908 The standard should actually say:
10912 operator-function-id
10914 A defect report has been filed about this issue.
10916 A conversion-function-id cannot be a template name because they cannot
10917 be part of a template-id. In fact, looking at this code:
10919 a.operator K<int>()
10921 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10922 It is impossible to call a templated conversion-function-id with an
10923 explicit argument list, since the only allowed template parameter is
10924 the type to which it is converting.
10926 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10927 `template' keyword, in a construction like:
10931 In that case `f' is taken to be a template-name, even though there
10932 is no way of knowing for sure.
10934 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10935 name refers to a set of overloaded functions, at least one of which
10936 is a template, or an IDENTIFIER_NODE with the name of the template,
10937 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10938 names are looked up inside uninstantiated templates. */
10941 cp_parser_template_name (cp_parser* parser,
10942 bool template_keyword_p,
10943 bool check_dependency_p,
10944 bool is_declaration,
10945 bool *is_identifier)
10950 cp_token *token = cp_lexer_peek_token (parser->lexer);
10952 /* If the next token is `operator', then we have either an
10953 operator-function-id or a conversion-function-id. */
10954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10956 /* We don't know whether we're looking at an
10957 operator-function-id or a conversion-function-id. */
10958 cp_parser_parse_tentatively (parser);
10959 /* Try an operator-function-id. */
10960 identifier = cp_parser_operator_function_id (parser);
10961 /* If that didn't work, try a conversion-function-id. */
10962 if (!cp_parser_parse_definitely (parser))
10964 cp_parser_error (parser, "expected template-name");
10965 return error_mark_node;
10968 /* Look for the identifier. */
10970 identifier = cp_parser_identifier (parser);
10972 /* If we didn't find an identifier, we don't have a template-id. */
10973 if (identifier == error_mark_node)
10974 return error_mark_node;
10976 /* If the name immediately followed the `template' keyword, then it
10977 is a template-name. However, if the next token is not `<', then
10978 we do not treat it as a template-name, since it is not being used
10979 as part of a template-id. This enables us to handle constructs
10982 template <typename T> struct S { S(); };
10983 template <typename T> S<T>::S();
10985 correctly. We would treat `S' as a template -- if it were `S<T>'
10986 -- but we do not if there is no `<'. */
10988 if (processing_template_decl
10989 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10991 /* In a declaration, in a dependent context, we pretend that the
10992 "template" keyword was present in order to improve error
10993 recovery. For example, given:
10995 template <typename T> void f(T::X<int>);
10997 we want to treat "X<int>" as a template-id. */
10999 && !template_keyword_p
11000 && parser->scope && TYPE_P (parser->scope)
11001 && check_dependency_p
11002 && dependent_scope_p (parser->scope)
11003 /* Do not do this for dtors (or ctors), since they never
11004 need the template keyword before their name. */
11005 && !constructor_name_p (identifier, parser->scope))
11007 cp_token_position start = 0;
11009 /* Explain what went wrong. */
11010 error_at (token->location, "non-template %qD used as template",
11012 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11013 parser->scope, identifier);
11014 /* If parsing tentatively, find the location of the "<" token. */
11015 if (cp_parser_simulate_error (parser))
11016 start = cp_lexer_token_position (parser->lexer, true);
11017 /* Parse the template arguments so that we can issue error
11018 messages about them. */
11019 cp_lexer_consume_token (parser->lexer);
11020 cp_parser_enclosed_template_argument_list (parser);
11021 /* Skip tokens until we find a good place from which to
11022 continue parsing. */
11023 cp_parser_skip_to_closing_parenthesis (parser,
11024 /*recovering=*/true,
11026 /*consume_paren=*/false);
11027 /* If parsing tentatively, permanently remove the
11028 template argument list. That will prevent duplicate
11029 error messages from being issued about the missing
11030 "template" keyword. */
11032 cp_lexer_purge_tokens_after (parser->lexer, start);
11034 *is_identifier = true;
11038 /* If the "template" keyword is present, then there is generally
11039 no point in doing name-lookup, so we just return IDENTIFIER.
11040 But, if the qualifying scope is non-dependent then we can
11041 (and must) do name-lookup normally. */
11042 if (template_keyword_p
11044 || (TYPE_P (parser->scope)
11045 && dependent_type_p (parser->scope))))
11049 /* Look up the name. */
11050 decl = cp_parser_lookup_name (parser, identifier,
11052 /*is_template=*/false,
11053 /*is_namespace=*/false,
11054 check_dependency_p,
11055 /*ambiguous_decls=*/NULL,
11057 decl = maybe_get_template_decl_from_type_decl (decl);
11059 /* If DECL is a template, then the name was a template-name. */
11060 if (TREE_CODE (decl) == TEMPLATE_DECL)
11064 tree fn = NULL_TREE;
11066 /* The standard does not explicitly indicate whether a name that
11067 names a set of overloaded declarations, some of which are
11068 templates, is a template-name. However, such a name should
11069 be a template-name; otherwise, there is no way to form a
11070 template-id for the overloaded templates. */
11071 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11072 if (TREE_CODE (fns) == OVERLOAD)
11073 for (fn = fns; fn; fn = OVL_NEXT (fn))
11074 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11079 /* The name does not name a template. */
11080 cp_parser_error (parser, "expected template-name");
11081 return error_mark_node;
11085 /* If DECL is dependent, and refers to a function, then just return
11086 its name; we will look it up again during template instantiation. */
11087 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11089 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11090 if (TYPE_P (scope) && dependent_type_p (scope))
11097 /* Parse a template-argument-list.
11099 template-argument-list:
11100 template-argument ... [opt]
11101 template-argument-list , template-argument ... [opt]
11103 Returns a TREE_VEC containing the arguments. */
11106 cp_parser_template_argument_list (cp_parser* parser)
11108 tree fixed_args[10];
11109 unsigned n_args = 0;
11110 unsigned alloced = 10;
11111 tree *arg_ary = fixed_args;
11113 bool saved_in_template_argument_list_p;
11115 bool saved_non_ice_p;
11117 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11118 parser->in_template_argument_list_p = true;
11119 /* Even if the template-id appears in an integral
11120 constant-expression, the contents of the argument list do
11122 saved_ice_p = parser->integral_constant_expression_p;
11123 parser->integral_constant_expression_p = false;
11124 saved_non_ice_p = parser->non_integral_constant_expression_p;
11125 parser->non_integral_constant_expression_p = false;
11126 /* Parse the arguments. */
11132 /* Consume the comma. */
11133 cp_lexer_consume_token (parser->lexer);
11135 /* Parse the template-argument. */
11136 argument = cp_parser_template_argument (parser);
11138 /* If the next token is an ellipsis, we're expanding a template
11140 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11142 if (argument == error_mark_node)
11144 cp_token *token = cp_lexer_peek_token (parser->lexer);
11145 error_at (token->location,
11146 "expected parameter pack before %<...%>");
11148 /* Consume the `...' token. */
11149 cp_lexer_consume_token (parser->lexer);
11151 /* Make the argument into a TYPE_PACK_EXPANSION or
11152 EXPR_PACK_EXPANSION. */
11153 argument = make_pack_expansion (argument);
11156 if (n_args == alloced)
11160 if (arg_ary == fixed_args)
11162 arg_ary = XNEWVEC (tree, alloced);
11163 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11166 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11168 arg_ary[n_args++] = argument;
11170 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11172 vec = make_tree_vec (n_args);
11175 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11177 if (arg_ary != fixed_args)
11179 parser->non_integral_constant_expression_p = saved_non_ice_p;
11180 parser->integral_constant_expression_p = saved_ice_p;
11181 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11185 /* Parse a template-argument.
11188 assignment-expression
11192 The representation is that of an assignment-expression, type-id, or
11193 id-expression -- except that the qualified id-expression is
11194 evaluated, so that the value returned is either a DECL or an
11197 Although the standard says "assignment-expression", it forbids
11198 throw-expressions or assignments in the template argument.
11199 Therefore, we use "conditional-expression" instead. */
11202 cp_parser_template_argument (cp_parser* parser)
11207 bool maybe_type_id = false;
11208 cp_token *token = NULL, *argument_start_token = NULL;
11211 /* There's really no way to know what we're looking at, so we just
11212 try each alternative in order.
11216 In a template-argument, an ambiguity between a type-id and an
11217 expression is resolved to a type-id, regardless of the form of
11218 the corresponding template-parameter.
11220 Therefore, we try a type-id first. */
11221 cp_parser_parse_tentatively (parser);
11222 argument = cp_parser_template_type_arg (parser);
11223 /* If there was no error parsing the type-id but the next token is a
11224 '>>', our behavior depends on which dialect of C++ we're
11225 parsing. In C++98, we probably found a typo for '> >'. But there
11226 are type-id which are also valid expressions. For instance:
11228 struct X { int operator >> (int); };
11229 template <int V> struct Foo {};
11232 Here 'X()' is a valid type-id of a function type, but the user just
11233 wanted to write the expression "X() >> 5". Thus, we remember that we
11234 found a valid type-id, but we still try to parse the argument as an
11235 expression to see what happens.
11237 In C++0x, the '>>' will be considered two separate '>'
11239 if (!cp_parser_error_occurred (parser)
11240 && cxx_dialect == cxx98
11241 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11243 maybe_type_id = true;
11244 cp_parser_abort_tentative_parse (parser);
11248 /* If the next token isn't a `,' or a `>', then this argument wasn't
11249 really finished. This means that the argument is not a valid
11251 if (!cp_parser_next_token_ends_template_argument_p (parser))
11252 cp_parser_error (parser, "expected template-argument");
11253 /* If that worked, we're done. */
11254 if (cp_parser_parse_definitely (parser))
11257 /* We're still not sure what the argument will be. */
11258 cp_parser_parse_tentatively (parser);
11259 /* Try a template. */
11260 argument_start_token = cp_lexer_peek_token (parser->lexer);
11261 argument = cp_parser_id_expression (parser,
11262 /*template_keyword_p=*/false,
11263 /*check_dependency_p=*/true,
11265 /*declarator_p=*/false,
11266 /*optional_p=*/false);
11267 /* If the next token isn't a `,' or a `>', then this argument wasn't
11268 really finished. */
11269 if (!cp_parser_next_token_ends_template_argument_p (parser))
11270 cp_parser_error (parser, "expected template-argument");
11271 if (!cp_parser_error_occurred (parser))
11273 /* Figure out what is being referred to. If the id-expression
11274 was for a class template specialization, then we will have a
11275 TYPE_DECL at this point. There is no need to do name lookup
11276 at this point in that case. */
11277 if (TREE_CODE (argument) != TYPE_DECL)
11278 argument = cp_parser_lookup_name (parser, argument,
11280 /*is_template=*/template_p,
11281 /*is_namespace=*/false,
11282 /*check_dependency=*/true,
11283 /*ambiguous_decls=*/NULL,
11284 argument_start_token->location);
11285 if (TREE_CODE (argument) != TEMPLATE_DECL
11286 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11287 cp_parser_error (parser, "expected template-name");
11289 if (cp_parser_parse_definitely (parser))
11291 /* It must be a non-type argument. There permitted cases are given
11292 in [temp.arg.nontype]:
11294 -- an integral constant-expression of integral or enumeration
11297 -- the name of a non-type template-parameter; or
11299 -- the name of an object or function with external linkage...
11301 -- the address of an object or function with external linkage...
11303 -- a pointer to member... */
11304 /* Look for a non-type template parameter. */
11305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11307 cp_parser_parse_tentatively (parser);
11308 argument = cp_parser_primary_expression (parser,
11309 /*address_p=*/false,
11311 /*template_arg_p=*/true,
11313 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11314 || !cp_parser_next_token_ends_template_argument_p (parser))
11315 cp_parser_simulate_error (parser);
11316 if (cp_parser_parse_definitely (parser))
11320 /* If the next token is "&", the argument must be the address of an
11321 object or function with external linkage. */
11322 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11324 cp_lexer_consume_token (parser->lexer);
11325 /* See if we might have an id-expression. */
11326 token = cp_lexer_peek_token (parser->lexer);
11327 if (token->type == CPP_NAME
11328 || token->keyword == RID_OPERATOR
11329 || token->type == CPP_SCOPE
11330 || token->type == CPP_TEMPLATE_ID
11331 || token->type == CPP_NESTED_NAME_SPECIFIER)
11333 cp_parser_parse_tentatively (parser);
11334 argument = cp_parser_primary_expression (parser,
11337 /*template_arg_p=*/true,
11339 if (cp_parser_error_occurred (parser)
11340 || !cp_parser_next_token_ends_template_argument_p (parser))
11341 cp_parser_abort_tentative_parse (parser);
11344 if (TREE_CODE (argument) == INDIRECT_REF)
11346 gcc_assert (REFERENCE_REF_P (argument));
11347 argument = TREE_OPERAND (argument, 0);
11350 if (TREE_CODE (argument) == VAR_DECL)
11352 /* A variable without external linkage might still be a
11353 valid constant-expression, so no error is issued here
11354 if the external-linkage check fails. */
11355 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11356 cp_parser_simulate_error (parser);
11358 else if (is_overloaded_fn (argument))
11359 /* All overloaded functions are allowed; if the external
11360 linkage test does not pass, an error will be issued
11364 && (TREE_CODE (argument) == OFFSET_REF
11365 || TREE_CODE (argument) == SCOPE_REF))
11366 /* A pointer-to-member. */
11368 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11371 cp_parser_simulate_error (parser);
11373 if (cp_parser_parse_definitely (parser))
11376 argument = build_x_unary_op (ADDR_EXPR, argument,
11377 tf_warning_or_error);
11382 /* If the argument started with "&", there are no other valid
11383 alternatives at this point. */
11386 cp_parser_error (parser, "invalid non-type template argument");
11387 return error_mark_node;
11390 /* If the argument wasn't successfully parsed as a type-id followed
11391 by '>>', the argument can only be a constant expression now.
11392 Otherwise, we try parsing the constant-expression tentatively,
11393 because the argument could really be a type-id. */
11395 cp_parser_parse_tentatively (parser);
11396 argument = cp_parser_constant_expression (parser,
11397 /*allow_non_constant_p=*/false,
11398 /*non_constant_p=*/NULL);
11399 argument = fold_non_dependent_expr (argument);
11400 if (!maybe_type_id)
11402 if (!cp_parser_next_token_ends_template_argument_p (parser))
11403 cp_parser_error (parser, "expected template-argument");
11404 if (cp_parser_parse_definitely (parser))
11406 /* We did our best to parse the argument as a non type-id, but that
11407 was the only alternative that matched (albeit with a '>' after
11408 it). We can assume it's just a typo from the user, and a
11409 diagnostic will then be issued. */
11410 return cp_parser_template_type_arg (parser);
11413 /* Parse an explicit-instantiation.
11415 explicit-instantiation:
11416 template declaration
11418 Although the standard says `declaration', what it really means is:
11420 explicit-instantiation:
11421 template decl-specifier-seq [opt] declarator [opt] ;
11423 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11424 supposed to be allowed. A defect report has been filed about this
11429 explicit-instantiation:
11430 storage-class-specifier template
11431 decl-specifier-seq [opt] declarator [opt] ;
11432 function-specifier template
11433 decl-specifier-seq [opt] declarator [opt] ; */
11436 cp_parser_explicit_instantiation (cp_parser* parser)
11438 int declares_class_or_enum;
11439 cp_decl_specifier_seq decl_specifiers;
11440 tree extension_specifier = NULL_TREE;
11443 /* Look for an (optional) storage-class-specifier or
11444 function-specifier. */
11445 if (cp_parser_allow_gnu_extensions_p (parser))
11447 extension_specifier
11448 = cp_parser_storage_class_specifier_opt (parser);
11449 if (!extension_specifier)
11450 extension_specifier
11451 = cp_parser_function_specifier_opt (parser,
11452 /*decl_specs=*/NULL);
11455 /* Look for the `template' keyword. */
11456 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11457 /* Let the front end know that we are processing an explicit
11459 begin_explicit_instantiation ();
11460 /* [temp.explicit] says that we are supposed to ignore access
11461 control while processing explicit instantiation directives. */
11462 push_deferring_access_checks (dk_no_check);
11463 /* Parse a decl-specifier-seq. */
11464 token = cp_lexer_peek_token (parser->lexer);
11465 cp_parser_decl_specifier_seq (parser,
11466 CP_PARSER_FLAGS_OPTIONAL,
11468 &declares_class_or_enum);
11469 /* If there was exactly one decl-specifier, and it declared a class,
11470 and there's no declarator, then we have an explicit type
11472 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11476 type = check_tag_decl (&decl_specifiers);
11477 /* Turn access control back on for names used during
11478 template instantiation. */
11479 pop_deferring_access_checks ();
11481 do_type_instantiation (type, extension_specifier,
11482 /*complain=*/tf_error);
11486 cp_declarator *declarator;
11489 /* Parse the declarator. */
11491 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11492 /*ctor_dtor_or_conv_p=*/NULL,
11493 /*parenthesized_p=*/NULL,
11494 /*member_p=*/false);
11495 if (declares_class_or_enum & 2)
11496 cp_parser_check_for_definition_in_return_type (declarator,
11497 decl_specifiers.type,
11498 decl_specifiers.type_location);
11499 if (declarator != cp_error_declarator)
11501 decl = grokdeclarator (declarator, &decl_specifiers,
11502 NORMAL, 0, &decl_specifiers.attributes);
11503 /* Turn access control back on for names used during
11504 template instantiation. */
11505 pop_deferring_access_checks ();
11506 /* Do the explicit instantiation. */
11507 do_decl_instantiation (decl, extension_specifier);
11511 pop_deferring_access_checks ();
11512 /* Skip the body of the explicit instantiation. */
11513 cp_parser_skip_to_end_of_statement (parser);
11516 /* We're done with the instantiation. */
11517 end_explicit_instantiation ();
11519 cp_parser_consume_semicolon_at_end_of_statement (parser);
11522 /* Parse an explicit-specialization.
11524 explicit-specialization:
11525 template < > declaration
11527 Although the standard says `declaration', what it really means is:
11529 explicit-specialization:
11530 template <> decl-specifier [opt] init-declarator [opt] ;
11531 template <> function-definition
11532 template <> explicit-specialization
11533 template <> template-declaration */
11536 cp_parser_explicit_specialization (cp_parser* parser)
11538 bool need_lang_pop;
11539 cp_token *token = cp_lexer_peek_token (parser->lexer);
11541 /* Look for the `template' keyword. */
11542 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11543 /* Look for the `<'. */
11544 cp_parser_require (parser, CPP_LESS, "%<<%>");
11545 /* Look for the `>'. */
11546 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11547 /* We have processed another parameter list. */
11548 ++parser->num_template_parameter_lists;
11551 A template ... explicit specialization ... shall not have C
11553 if (current_lang_name == lang_name_c)
11555 error_at (token->location, "template specialization with C linkage");
11556 /* Give it C++ linkage to avoid confusing other parts of the
11558 push_lang_context (lang_name_cplusplus);
11559 need_lang_pop = true;
11562 need_lang_pop = false;
11563 /* Let the front end know that we are beginning a specialization. */
11564 if (!begin_specialization ())
11566 end_specialization ();
11570 /* If the next keyword is `template', we need to figure out whether
11571 or not we're looking a template-declaration. */
11572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11574 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11575 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11576 cp_parser_template_declaration_after_export (parser,
11577 /*member_p=*/false);
11579 cp_parser_explicit_specialization (parser);
11582 /* Parse the dependent declaration. */
11583 cp_parser_single_declaration (parser,
11585 /*member_p=*/false,
11586 /*explicit_specialization_p=*/true,
11587 /*friend_p=*/NULL);
11588 /* We're done with the specialization. */
11589 end_specialization ();
11590 /* For the erroneous case of a template with C linkage, we pushed an
11591 implicit C++ linkage scope; exit that scope now. */
11593 pop_lang_context ();
11594 /* We're done with this parameter list. */
11595 --parser->num_template_parameter_lists;
11598 /* Parse a type-specifier.
11601 simple-type-specifier
11604 elaborated-type-specifier
11612 Returns a representation of the type-specifier. For a
11613 class-specifier, enum-specifier, or elaborated-type-specifier, a
11614 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11616 The parser flags FLAGS is used to control type-specifier parsing.
11618 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11619 in a decl-specifier-seq.
11621 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11622 class-specifier, enum-specifier, or elaborated-type-specifier, then
11623 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11624 if a type is declared; 2 if it is defined. Otherwise, it is set to
11627 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11628 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11629 is set to FALSE. */
11632 cp_parser_type_specifier (cp_parser* parser,
11633 cp_parser_flags flags,
11634 cp_decl_specifier_seq *decl_specs,
11635 bool is_declaration,
11636 int* declares_class_or_enum,
11637 bool* is_cv_qualifier)
11639 tree type_spec = NULL_TREE;
11642 cp_decl_spec ds = ds_last;
11644 /* Assume this type-specifier does not declare a new type. */
11645 if (declares_class_or_enum)
11646 *declares_class_or_enum = 0;
11647 /* And that it does not specify a cv-qualifier. */
11648 if (is_cv_qualifier)
11649 *is_cv_qualifier = false;
11650 /* Peek at the next token. */
11651 token = cp_lexer_peek_token (parser->lexer);
11653 /* If we're looking at a keyword, we can use that to guide the
11654 production we choose. */
11655 keyword = token->keyword;
11659 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11660 goto elaborated_type_specifier;
11662 /* Look for the enum-specifier. */
11663 type_spec = cp_parser_enum_specifier (parser);
11664 /* If that worked, we're done. */
11667 if (declares_class_or_enum)
11668 *declares_class_or_enum = 2;
11670 cp_parser_set_decl_spec_type (decl_specs,
11673 /*user_defined_p=*/true);
11677 goto elaborated_type_specifier;
11679 /* Any of these indicate either a class-specifier, or an
11680 elaborated-type-specifier. */
11684 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11685 goto elaborated_type_specifier;
11687 /* Parse tentatively so that we can back up if we don't find a
11688 class-specifier. */
11689 cp_parser_parse_tentatively (parser);
11690 /* Look for the class-specifier. */
11691 type_spec = cp_parser_class_specifier (parser);
11692 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11693 /* If that worked, we're done. */
11694 if (cp_parser_parse_definitely (parser))
11696 if (declares_class_or_enum)
11697 *declares_class_or_enum = 2;
11699 cp_parser_set_decl_spec_type (decl_specs,
11702 /*user_defined_p=*/true);
11706 /* Fall through. */
11707 elaborated_type_specifier:
11708 /* We're declaring (not defining) a class or enum. */
11709 if (declares_class_or_enum)
11710 *declares_class_or_enum = 1;
11712 /* Fall through. */
11714 /* Look for an elaborated-type-specifier. */
11716 = (cp_parser_elaborated_type_specifier
11718 decl_specs && decl_specs->specs[(int) ds_friend],
11721 cp_parser_set_decl_spec_type (decl_specs,
11724 /*user_defined_p=*/true);
11729 if (is_cv_qualifier)
11730 *is_cv_qualifier = true;
11735 if (is_cv_qualifier)
11736 *is_cv_qualifier = true;
11741 if (is_cv_qualifier)
11742 *is_cv_qualifier = true;
11746 /* The `__complex__' keyword is a GNU extension. */
11754 /* Handle simple keywords. */
11759 ++decl_specs->specs[(int)ds];
11760 decl_specs->any_specifiers_p = true;
11762 return cp_lexer_consume_token (parser->lexer)->u.value;
11765 /* If we do not already have a type-specifier, assume we are looking
11766 at a simple-type-specifier. */
11767 type_spec = cp_parser_simple_type_specifier (parser,
11771 /* If we didn't find a type-specifier, and a type-specifier was not
11772 optional in this context, issue an error message. */
11773 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11775 cp_parser_error (parser, "expected type specifier");
11776 return error_mark_node;
11782 /* Parse a simple-type-specifier.
11784 simple-type-specifier:
11785 :: [opt] nested-name-specifier [opt] type-name
11786 :: [opt] nested-name-specifier template template-id
11801 simple-type-specifier:
11803 decltype ( expression )
11809 simple-type-specifier:
11810 __typeof__ unary-expression
11811 __typeof__ ( type-id )
11813 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11814 appropriately updated. */
11817 cp_parser_simple_type_specifier (cp_parser* parser,
11818 cp_decl_specifier_seq *decl_specs,
11819 cp_parser_flags flags)
11821 tree type = NULL_TREE;
11824 /* Peek at the next token. */
11825 token = cp_lexer_peek_token (parser->lexer);
11827 /* If we're looking at a keyword, things are easy. */
11828 switch (token->keyword)
11832 decl_specs->explicit_char_p = true;
11833 type = char_type_node;
11836 type = char16_type_node;
11839 type = char32_type_node;
11842 type = wchar_type_node;
11845 type = boolean_type_node;
11849 ++decl_specs->specs[(int) ds_short];
11850 type = short_integer_type_node;
11854 decl_specs->explicit_int_p = true;
11855 type = integer_type_node;
11859 ++decl_specs->specs[(int) ds_long];
11860 type = long_integer_type_node;
11864 ++decl_specs->specs[(int) ds_signed];
11865 type = integer_type_node;
11869 ++decl_specs->specs[(int) ds_unsigned];
11870 type = unsigned_type_node;
11873 type = float_type_node;
11876 type = double_type_node;
11879 type = void_type_node;
11883 maybe_warn_cpp0x ("C++0x auto");
11884 type = make_auto ();
11888 /* Parse the `decltype' type. */
11889 type = cp_parser_decltype (parser);
11892 cp_parser_set_decl_spec_type (decl_specs, type,
11894 /*user_defined_p=*/true);
11899 /* Consume the `typeof' token. */
11900 cp_lexer_consume_token (parser->lexer);
11901 /* Parse the operand to `typeof'. */
11902 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11903 /* If it is not already a TYPE, take its type. */
11904 if (!TYPE_P (type))
11905 type = finish_typeof (type);
11908 cp_parser_set_decl_spec_type (decl_specs, type,
11910 /*user_defined_p=*/true);
11918 /* If the type-specifier was for a built-in type, we're done. */
11923 /* Record the type. */
11925 && (token->keyword != RID_SIGNED
11926 && token->keyword != RID_UNSIGNED
11927 && token->keyword != RID_SHORT
11928 && token->keyword != RID_LONG))
11929 cp_parser_set_decl_spec_type (decl_specs,
11932 /*user_defined=*/false);
11934 decl_specs->any_specifiers_p = true;
11936 /* Consume the token. */
11937 id = cp_lexer_consume_token (parser->lexer)->u.value;
11939 /* There is no valid C++ program where a non-template type is
11940 followed by a "<". That usually indicates that the user thought
11941 that the type was a template. */
11942 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11944 return TYPE_NAME (type);
11947 /* The type-specifier must be a user-defined type. */
11948 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11953 /* Don't gobble tokens or issue error messages if this is an
11954 optional type-specifier. */
11955 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11956 cp_parser_parse_tentatively (parser);
11958 /* Look for the optional `::' operator. */
11960 = (cp_parser_global_scope_opt (parser,
11961 /*current_scope_valid_p=*/false)
11963 /* Look for the nested-name specifier. */
11965 = (cp_parser_nested_name_specifier_opt (parser,
11966 /*typename_keyword_p=*/false,
11967 /*check_dependency_p=*/true,
11969 /*is_declaration=*/false)
11971 token = cp_lexer_peek_token (parser->lexer);
11972 /* If we have seen a nested-name-specifier, and the next token
11973 is `template', then we are using the template-id production. */
11975 && cp_parser_optional_template_keyword (parser))
11977 /* Look for the template-id. */
11978 type = cp_parser_template_id (parser,
11979 /*template_keyword_p=*/true,
11980 /*check_dependency_p=*/true,
11981 /*is_declaration=*/false);
11982 /* If the template-id did not name a type, we are out of
11984 if (TREE_CODE (type) != TYPE_DECL)
11986 cp_parser_error (parser, "expected template-id for type");
11990 /* Otherwise, look for a type-name. */
11992 type = cp_parser_type_name (parser);
11993 /* Keep track of all name-lookups performed in class scopes. */
11997 && TREE_CODE (type) == TYPE_DECL
11998 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11999 maybe_note_name_used_in_class (DECL_NAME (type), type);
12000 /* If it didn't work out, we don't have a TYPE. */
12001 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12002 && !cp_parser_parse_definitely (parser))
12004 if (type && decl_specs)
12005 cp_parser_set_decl_spec_type (decl_specs, type,
12007 /*user_defined=*/true);
12010 /* If we didn't get a type-name, issue an error message. */
12011 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12013 cp_parser_error (parser, "expected type-name");
12014 return error_mark_node;
12017 /* There is no valid C++ program where a non-template type is
12018 followed by a "<". That usually indicates that the user thought
12019 that the type was a template. */
12020 if (type && type != error_mark_node)
12022 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12023 If it is, then the '<'...'>' enclose protocol names rather than
12024 template arguments, and so everything is fine. */
12025 if (c_dialect_objc ()
12026 && (objc_is_id (type) || objc_is_class_name (type)))
12028 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12029 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12031 /* Clobber the "unqualified" type previously entered into
12032 DECL_SPECS with the new, improved protocol-qualified version. */
12034 decl_specs->type = qual_type;
12039 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12046 /* Parse a type-name.
12059 Returns a TYPE_DECL for the type. */
12062 cp_parser_type_name (cp_parser* parser)
12066 /* We can't know yet whether it is a class-name or not. */
12067 cp_parser_parse_tentatively (parser);
12068 /* Try a class-name. */
12069 type_decl = cp_parser_class_name (parser,
12070 /*typename_keyword_p=*/false,
12071 /*template_keyword_p=*/false,
12073 /*check_dependency_p=*/true,
12074 /*class_head_p=*/false,
12075 /*is_declaration=*/false);
12076 /* If it's not a class-name, keep looking. */
12077 if (!cp_parser_parse_definitely (parser))
12079 /* It must be a typedef-name or an enum-name. */
12080 return cp_parser_nonclass_name (parser);
12086 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12094 Returns a TYPE_DECL for the type. */
12097 cp_parser_nonclass_name (cp_parser* parser)
12102 cp_token *token = cp_lexer_peek_token (parser->lexer);
12103 identifier = cp_parser_identifier (parser);
12104 if (identifier == error_mark_node)
12105 return error_mark_node;
12107 /* Look up the type-name. */
12108 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12110 if (TREE_CODE (type_decl) != TYPE_DECL
12111 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12113 /* See if this is an Objective-C type. */
12114 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12115 tree type = objc_get_protocol_qualified_type (identifier, protos);
12117 type_decl = TYPE_NAME (type);
12120 /* Issue an error if we did not find a type-name. */
12121 if (TREE_CODE (type_decl) != TYPE_DECL)
12123 if (!cp_parser_simulate_error (parser))
12124 cp_parser_name_lookup_error (parser, identifier, type_decl,
12125 "is not a type", token->location);
12126 return error_mark_node;
12128 /* Remember that the name was used in the definition of the
12129 current class so that we can check later to see if the
12130 meaning would have been different after the class was
12131 entirely defined. */
12132 else if (type_decl != error_mark_node
12134 maybe_note_name_used_in_class (identifier, type_decl);
12139 /* Parse an elaborated-type-specifier. Note that the grammar given
12140 here incorporates the resolution to DR68.
12142 elaborated-type-specifier:
12143 class-key :: [opt] nested-name-specifier [opt] identifier
12144 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12145 enum-key :: [opt] nested-name-specifier [opt] identifier
12146 typename :: [opt] nested-name-specifier identifier
12147 typename :: [opt] nested-name-specifier template [opt]
12152 elaborated-type-specifier:
12153 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12154 class-key attributes :: [opt] nested-name-specifier [opt]
12155 template [opt] template-id
12156 enum attributes :: [opt] nested-name-specifier [opt] identifier
12158 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12159 declared `friend'. If IS_DECLARATION is TRUE, then this
12160 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12161 something is being declared.
12163 Returns the TYPE specified. */
12166 cp_parser_elaborated_type_specifier (cp_parser* parser,
12168 bool is_declaration)
12170 enum tag_types tag_type;
12172 tree type = NULL_TREE;
12173 tree attributes = NULL_TREE;
12175 cp_token *token = NULL;
12177 /* See if we're looking at the `enum' keyword. */
12178 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12180 /* Consume the `enum' token. */
12181 cp_lexer_consume_token (parser->lexer);
12182 /* Remember that it's an enumeration type. */
12183 tag_type = enum_type;
12184 /* Parse the optional `struct' or `class' key (for C++0x scoped
12186 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12187 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12189 if (cxx_dialect == cxx98)
12190 maybe_warn_cpp0x ("scoped enums");
12192 /* Consume the `struct' or `class'. */
12193 cp_lexer_consume_token (parser->lexer);
12195 /* Parse the attributes. */
12196 attributes = cp_parser_attributes_opt (parser);
12198 /* Or, it might be `typename'. */
12199 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12202 /* Consume the `typename' token. */
12203 cp_lexer_consume_token (parser->lexer);
12204 /* Remember that it's a `typename' type. */
12205 tag_type = typename_type;
12207 /* Otherwise it must be a class-key. */
12210 tag_type = cp_parser_class_key (parser);
12211 if (tag_type == none_type)
12212 return error_mark_node;
12213 /* Parse the attributes. */
12214 attributes = cp_parser_attributes_opt (parser);
12217 /* Look for the `::' operator. */
12218 globalscope = cp_parser_global_scope_opt (parser,
12219 /*current_scope_valid_p=*/false);
12220 /* Look for the nested-name-specifier. */
12221 if (tag_type == typename_type && !globalscope)
12223 if (!cp_parser_nested_name_specifier (parser,
12224 /*typename_keyword_p=*/true,
12225 /*check_dependency_p=*/true,
12228 return error_mark_node;
12231 /* Even though `typename' is not present, the proposed resolution
12232 to Core Issue 180 says that in `class A<T>::B', `B' should be
12233 considered a type-name, even if `A<T>' is dependent. */
12234 cp_parser_nested_name_specifier_opt (parser,
12235 /*typename_keyword_p=*/true,
12236 /*check_dependency_p=*/true,
12239 /* For everything but enumeration types, consider a template-id.
12240 For an enumeration type, consider only a plain identifier. */
12241 if (tag_type != enum_type)
12243 bool template_p = false;
12246 /* Allow the `template' keyword. */
12247 template_p = cp_parser_optional_template_keyword (parser);
12248 /* If we didn't see `template', we don't know if there's a
12249 template-id or not. */
12251 cp_parser_parse_tentatively (parser);
12252 /* Parse the template-id. */
12253 token = cp_lexer_peek_token (parser->lexer);
12254 decl = cp_parser_template_id (parser, template_p,
12255 /*check_dependency_p=*/true,
12257 /* If we didn't find a template-id, look for an ordinary
12259 if (!template_p && !cp_parser_parse_definitely (parser))
12261 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12262 in effect, then we must assume that, upon instantiation, the
12263 template will correspond to a class. */
12264 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12265 && tag_type == typename_type)
12266 type = make_typename_type (parser->scope, decl,
12268 /*complain=*/tf_error);
12269 /* If the `typename' keyword is in effect and DECL is not a type
12270 decl. Then type is non existant. */
12271 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12274 type = TREE_TYPE (decl);
12279 token = cp_lexer_peek_token (parser->lexer);
12280 identifier = cp_parser_identifier (parser);
12282 if (identifier == error_mark_node)
12284 parser->scope = NULL_TREE;
12285 return error_mark_node;
12288 /* For a `typename', we needn't call xref_tag. */
12289 if (tag_type == typename_type
12290 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12291 return cp_parser_make_typename_type (parser, parser->scope,
12294 /* Look up a qualified name in the usual way. */
12298 tree ambiguous_decls;
12300 decl = cp_parser_lookup_name (parser, identifier,
12302 /*is_template=*/false,
12303 /*is_namespace=*/false,
12304 /*check_dependency=*/true,
12308 /* If the lookup was ambiguous, an error will already have been
12310 if (ambiguous_decls)
12311 return error_mark_node;
12313 /* If we are parsing friend declaration, DECL may be a
12314 TEMPLATE_DECL tree node here. However, we need to check
12315 whether this TEMPLATE_DECL results in valid code. Consider
12316 the following example:
12319 template <class T> class C {};
12322 template <class T> friend class N::C; // #1, valid code
12324 template <class T> class Y {
12325 friend class N::C; // #2, invalid code
12328 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12329 name lookup of `N::C'. We see that friend declaration must
12330 be template for the code to be valid. Note that
12331 processing_template_decl does not work here since it is
12332 always 1 for the above two cases. */
12334 decl = (cp_parser_maybe_treat_template_as_class
12335 (decl, /*tag_name_p=*/is_friend
12336 && parser->num_template_parameter_lists));
12338 if (TREE_CODE (decl) != TYPE_DECL)
12340 cp_parser_diagnose_invalid_type_name (parser,
12344 return error_mark_node;
12347 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12349 bool allow_template = (parser->num_template_parameter_lists
12350 || DECL_SELF_REFERENCE_P (decl));
12351 type = check_elaborated_type_specifier (tag_type, decl,
12354 if (type == error_mark_node)
12355 return error_mark_node;
12358 /* Forward declarations of nested types, such as
12363 are invalid unless all components preceding the final '::'
12364 are complete. If all enclosing types are complete, these
12365 declarations become merely pointless.
12367 Invalid forward declarations of nested types are errors
12368 caught elsewhere in parsing. Those that are pointless arrive
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12372 && !is_friend && !processing_explicit_instantiation)
12373 warning (0, "declaration %qD does not declare anything", decl);
12375 type = TREE_TYPE (decl);
12379 /* An elaborated-type-specifier sometimes introduces a new type and
12380 sometimes names an existing type. Normally, the rule is that it
12381 introduces a new type only if there is not an existing type of
12382 the same name already in scope. For example, given:
12385 void f() { struct S s; }
12387 the `struct S' in the body of `f' is the same `struct S' as in
12388 the global scope; the existing definition is used. However, if
12389 there were no global declaration, this would introduce a new
12390 local class named `S'.
12392 An exception to this rule applies to the following code:
12394 namespace N { struct S; }
12396 Here, the elaborated-type-specifier names a new type
12397 unconditionally; even if there is already an `S' in the
12398 containing scope this declaration names a new type.
12399 This exception only applies if the elaborated-type-specifier
12400 forms the complete declaration:
12404 A declaration consisting solely of `class-key identifier ;' is
12405 either a redeclaration of the name in the current scope or a
12406 forward declaration of the identifier as a class name. It
12407 introduces the name into the current scope.
12409 We are in this situation precisely when the next token is a `;'.
12411 An exception to the exception is that a `friend' declaration does
12412 *not* name a new type; i.e., given:
12414 struct S { friend struct T; };
12416 `T' is not a new type in the scope of `S'.
12418 Also, `new struct S' or `sizeof (struct S)' never results in the
12419 definition of a new type; a new type can only be declared in a
12420 declaration context. */
12426 /* Friends have special name lookup rules. */
12427 ts = ts_within_enclosing_non_class;
12428 else if (is_declaration
12429 && cp_lexer_next_token_is (parser->lexer,
12431 /* This is a `class-key identifier ;' */
12437 (parser->num_template_parameter_lists
12438 && (cp_parser_next_token_starts_class_definition_p (parser)
12439 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12440 /* An unqualified name was used to reference this type, so
12441 there were no qualifying templates. */
12442 if (!cp_parser_check_template_parameters (parser,
12443 /*num_templates=*/0,
12445 /*declarator=*/NULL))
12446 return error_mark_node;
12447 type = xref_tag (tag_type, identifier, ts, template_p);
12451 if (type == error_mark_node)
12452 return error_mark_node;
12454 /* Allow attributes on forward declarations of classes. */
12457 if (TREE_CODE (type) == TYPENAME_TYPE)
12458 warning (OPT_Wattributes,
12459 "attributes ignored on uninstantiated type");
12460 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12461 && ! processing_explicit_instantiation)
12462 warning (OPT_Wattributes,
12463 "attributes ignored on template instantiation");
12464 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12465 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12467 warning (OPT_Wattributes,
12468 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12471 if (tag_type != enum_type)
12472 cp_parser_check_class_key (tag_type, type);
12474 /* A "<" cannot follow an elaborated type specifier. If that
12475 happens, the user was probably trying to form a template-id. */
12476 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12481 /* Parse an enum-specifier.
12484 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12489 enum struct [C++0x]
12492 : type-specifier-seq
12495 enum-key attributes[opt] identifier [opt] enum-base [opt]
12496 { enumerator-list [opt] }attributes[opt]
12498 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12499 if the token stream isn't an enum-specifier after all. */
12502 cp_parser_enum_specifier (cp_parser* parser)
12507 bool scoped_enum_p = false;
12508 bool has_underlying_type = false;
12509 tree underlying_type = NULL_TREE;
12511 /* Parse tentatively so that we can back up if we don't find a
12513 cp_parser_parse_tentatively (parser);
12515 /* Caller guarantees that the current token is 'enum', an identifier
12516 possibly follows, and the token after that is an opening brace.
12517 If we don't have an identifier, fabricate an anonymous name for
12518 the enumeration being defined. */
12519 cp_lexer_consume_token (parser->lexer);
12521 /* Parse the "class" or "struct", which indicates a scoped
12522 enumeration type in C++0x. */
12523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12524 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12526 if (cxx_dialect == cxx98)
12527 maybe_warn_cpp0x ("scoped enums");
12529 /* Consume the `struct' or `class' token. */
12530 cp_lexer_consume_token (parser->lexer);
12532 scoped_enum_p = true;
12535 attributes = cp_parser_attributes_opt (parser);
12537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12538 identifier = cp_parser_identifier (parser);
12540 identifier = make_anon_name ();
12542 /* Check for the `:' that denotes a specified underlying type in C++0x.
12543 Note that a ':' could also indicate a bitfield width, however. */
12544 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12546 cp_decl_specifier_seq type_specifiers;
12548 /* Consume the `:'. */
12549 cp_lexer_consume_token (parser->lexer);
12551 /* Parse the type-specifier-seq. */
12552 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12553 /*is_trailing_return=*/false,
12556 /* At this point this is surely not elaborated type specifier. */
12557 if (!cp_parser_parse_definitely (parser))
12560 if (cxx_dialect == cxx98)
12561 maybe_warn_cpp0x ("scoped enums");
12563 has_underlying_type = true;
12565 /* If that didn't work, stop. */
12566 if (type_specifiers.type != error_mark_node)
12568 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12569 /*initialized=*/0, NULL);
12570 if (underlying_type == error_mark_node)
12571 underlying_type = NULL_TREE;
12575 /* Look for the `{' but don't consume it yet. */
12576 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12578 cp_parser_error (parser, "expected %<{%>");
12579 if (has_underlying_type)
12583 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12586 /* Issue an error message if type-definitions are forbidden here. */
12587 if (!cp_parser_check_type_definition (parser))
12588 type = error_mark_node;
12590 /* Create the new type. We do this before consuming the opening
12591 brace so the enum will be recorded as being on the line of its
12592 tag (or the 'enum' keyword, if there is no tag). */
12593 type = start_enum (identifier, underlying_type, scoped_enum_p);
12595 /* Consume the opening brace. */
12596 cp_lexer_consume_token (parser->lexer);
12598 if (type == error_mark_node)
12600 cp_parser_skip_to_end_of_block_or_statement (parser);
12601 return error_mark_node;
12604 /* If the next token is not '}', then there are some enumerators. */
12605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12606 cp_parser_enumerator_list (parser, type);
12608 /* Consume the final '}'. */
12609 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12611 /* Look for trailing attributes to apply to this enumeration, and
12612 apply them if appropriate. */
12613 if (cp_parser_allow_gnu_extensions_p (parser))
12615 tree trailing_attr = cp_parser_attributes_opt (parser);
12616 trailing_attr = chainon (trailing_attr, attributes);
12617 cplus_decl_attributes (&type,
12619 (int) ATTR_FLAG_TYPE_IN_PLACE);
12622 /* Finish up the enumeration. */
12623 finish_enum (type);
12628 /* Parse an enumerator-list. The enumerators all have the indicated
12632 enumerator-definition
12633 enumerator-list , enumerator-definition */
12636 cp_parser_enumerator_list (cp_parser* parser, tree type)
12640 /* Parse an enumerator-definition. */
12641 cp_parser_enumerator_definition (parser, type);
12643 /* If the next token is not a ',', we've reached the end of
12645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12647 /* Otherwise, consume the `,' and keep going. */
12648 cp_lexer_consume_token (parser->lexer);
12649 /* If the next token is a `}', there is a trailing comma. */
12650 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12652 if (!in_system_header)
12653 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12659 /* Parse an enumerator-definition. The enumerator has the indicated
12662 enumerator-definition:
12664 enumerator = constant-expression
12670 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12675 /* Look for the identifier. */
12676 identifier = cp_parser_identifier (parser);
12677 if (identifier == error_mark_node)
12680 /* If the next token is an '=', then there is an explicit value. */
12681 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12683 /* Consume the `=' token. */
12684 cp_lexer_consume_token (parser->lexer);
12685 /* Parse the value. */
12686 value = cp_parser_constant_expression (parser,
12687 /*allow_non_constant_p=*/false,
12693 /* If we are processing a template, make sure the initializer of the
12694 enumerator doesn't contain any bare template parameter pack. */
12695 if (check_for_bare_parameter_packs (value))
12696 value = error_mark_node;
12698 /* Create the enumerator. */
12699 build_enumerator (identifier, value, type);
12702 /* Parse a namespace-name.
12705 original-namespace-name
12708 Returns the NAMESPACE_DECL for the namespace. */
12711 cp_parser_namespace_name (cp_parser* parser)
12714 tree namespace_decl;
12716 cp_token *token = cp_lexer_peek_token (parser->lexer);
12718 /* Get the name of the namespace. */
12719 identifier = cp_parser_identifier (parser);
12720 if (identifier == error_mark_node)
12721 return error_mark_node;
12723 /* Look up the identifier in the currently active scope. Look only
12724 for namespaces, due to:
12726 [basic.lookup.udir]
12728 When looking up a namespace-name in a using-directive or alias
12729 definition, only namespace names are considered.
12733 [basic.lookup.qual]
12735 During the lookup of a name preceding the :: scope resolution
12736 operator, object, function, and enumerator names are ignored.
12738 (Note that cp_parser_qualifying_entity only calls this
12739 function if the token after the name is the scope resolution
12741 namespace_decl = cp_parser_lookup_name (parser, identifier,
12743 /*is_template=*/false,
12744 /*is_namespace=*/true,
12745 /*check_dependency=*/true,
12746 /*ambiguous_decls=*/NULL,
12748 /* If it's not a namespace, issue an error. */
12749 if (namespace_decl == error_mark_node
12750 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12752 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12753 error_at (token->location, "%qD is not a namespace-name", identifier);
12754 cp_parser_error (parser, "expected namespace-name");
12755 namespace_decl = error_mark_node;
12758 return namespace_decl;
12761 /* Parse a namespace-definition.
12763 namespace-definition:
12764 named-namespace-definition
12765 unnamed-namespace-definition
12767 named-namespace-definition:
12768 original-namespace-definition
12769 extension-namespace-definition
12771 original-namespace-definition:
12772 namespace identifier { namespace-body }
12774 extension-namespace-definition:
12775 namespace original-namespace-name { namespace-body }
12777 unnamed-namespace-definition:
12778 namespace { namespace-body } */
12781 cp_parser_namespace_definition (cp_parser* parser)
12783 tree identifier, attribs;
12784 bool has_visibility;
12787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12790 cp_lexer_consume_token (parser->lexer);
12795 /* Look for the `namespace' keyword. */
12796 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12798 /* Get the name of the namespace. We do not attempt to distinguish
12799 between an original-namespace-definition and an
12800 extension-namespace-definition at this point. The semantic
12801 analysis routines are responsible for that. */
12802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12803 identifier = cp_parser_identifier (parser);
12805 identifier = NULL_TREE;
12807 /* Parse any specified attributes. */
12808 attribs = cp_parser_attributes_opt (parser);
12810 /* Look for the `{' to start the namespace. */
12811 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12812 /* Start the namespace. */
12813 push_namespace (identifier);
12815 /* "inline namespace" is equivalent to a stub namespace definition
12816 followed by a strong using directive. */
12819 tree name_space = current_namespace;
12820 /* Set up namespace association. */
12821 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12822 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12823 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12824 /* Import the contents of the inline namespace. */
12826 do_using_directive (name_space);
12827 push_namespace (identifier);
12830 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12832 /* Parse the body of the namespace. */
12833 cp_parser_namespace_body (parser);
12835 #ifdef HANDLE_PRAGMA_VISIBILITY
12836 if (has_visibility)
12840 /* Finish the namespace. */
12842 /* Look for the final `}'. */
12843 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12846 /* Parse a namespace-body.
12849 declaration-seq [opt] */
12852 cp_parser_namespace_body (cp_parser* parser)
12854 cp_parser_declaration_seq_opt (parser);
12857 /* Parse a namespace-alias-definition.
12859 namespace-alias-definition:
12860 namespace identifier = qualified-namespace-specifier ; */
12863 cp_parser_namespace_alias_definition (cp_parser* parser)
12866 tree namespace_specifier;
12868 cp_token *token = cp_lexer_peek_token (parser->lexer);
12870 /* Look for the `namespace' keyword. */
12871 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12872 /* Look for the identifier. */
12873 identifier = cp_parser_identifier (parser);
12874 if (identifier == error_mark_node)
12876 /* Look for the `=' token. */
12877 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12878 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12880 error_at (token->location, "%<namespace%> definition is not allowed here");
12881 /* Skip the definition. */
12882 cp_lexer_consume_token (parser->lexer);
12883 if (cp_parser_skip_to_closing_brace (parser))
12884 cp_lexer_consume_token (parser->lexer);
12887 cp_parser_require (parser, CPP_EQ, "%<=%>");
12888 /* Look for the qualified-namespace-specifier. */
12889 namespace_specifier
12890 = cp_parser_qualified_namespace_specifier (parser);
12891 /* Look for the `;' token. */
12892 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12894 /* Register the alias in the symbol table. */
12895 do_namespace_alias (identifier, namespace_specifier);
12898 /* Parse a qualified-namespace-specifier.
12900 qualified-namespace-specifier:
12901 :: [opt] nested-name-specifier [opt] namespace-name
12903 Returns a NAMESPACE_DECL corresponding to the specified
12907 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12909 /* Look for the optional `::'. */
12910 cp_parser_global_scope_opt (parser,
12911 /*current_scope_valid_p=*/false);
12913 /* Look for the optional nested-name-specifier. */
12914 cp_parser_nested_name_specifier_opt (parser,
12915 /*typename_keyword_p=*/false,
12916 /*check_dependency_p=*/true,
12918 /*is_declaration=*/true);
12920 return cp_parser_namespace_name (parser);
12923 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12924 access declaration.
12927 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12928 using :: unqualified-id ;
12930 access-declaration:
12936 cp_parser_using_declaration (cp_parser* parser,
12937 bool access_declaration_p)
12940 bool typename_p = false;
12941 bool global_scope_p;
12946 if (access_declaration_p)
12947 cp_parser_parse_tentatively (parser);
12950 /* Look for the `using' keyword. */
12951 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12953 /* Peek at the next token. */
12954 token = cp_lexer_peek_token (parser->lexer);
12955 /* See if it's `typename'. */
12956 if (token->keyword == RID_TYPENAME)
12958 /* Remember that we've seen it. */
12960 /* Consume the `typename' token. */
12961 cp_lexer_consume_token (parser->lexer);
12965 /* Look for the optional global scope qualification. */
12967 = (cp_parser_global_scope_opt (parser,
12968 /*current_scope_valid_p=*/false)
12971 /* If we saw `typename', or didn't see `::', then there must be a
12972 nested-name-specifier present. */
12973 if (typename_p || !global_scope_p)
12974 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12975 /*check_dependency_p=*/true,
12977 /*is_declaration=*/true);
12978 /* Otherwise, we could be in either of the two productions. In that
12979 case, treat the nested-name-specifier as optional. */
12981 qscope = cp_parser_nested_name_specifier_opt (parser,
12982 /*typename_keyword_p=*/false,
12983 /*check_dependency_p=*/true,
12985 /*is_declaration=*/true);
12987 qscope = global_namespace;
12989 if (access_declaration_p && cp_parser_error_occurred (parser))
12990 /* Something has already gone wrong; there's no need to parse
12991 further. Since an error has occurred, the return value of
12992 cp_parser_parse_definitely will be false, as required. */
12993 return cp_parser_parse_definitely (parser);
12995 token = cp_lexer_peek_token (parser->lexer);
12996 /* Parse the unqualified-id. */
12997 identifier = cp_parser_unqualified_id (parser,
12998 /*template_keyword_p=*/false,
12999 /*check_dependency_p=*/true,
13000 /*declarator_p=*/true,
13001 /*optional_p=*/false);
13003 if (access_declaration_p)
13005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13006 cp_parser_simulate_error (parser);
13007 if (!cp_parser_parse_definitely (parser))
13011 /* The function we call to handle a using-declaration is different
13012 depending on what scope we are in. */
13013 if (qscope == error_mark_node || identifier == error_mark_node)
13015 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13016 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13017 /* [namespace.udecl]
13019 A using declaration shall not name a template-id. */
13020 error_at (token->location,
13021 "a template-id may not appear in a using-declaration");
13024 if (at_class_scope_p ())
13026 /* Create the USING_DECL. */
13027 decl = do_class_using_decl (parser->scope, identifier);
13029 if (check_for_bare_parameter_packs (decl))
13032 /* Add it to the list of members in this class. */
13033 finish_member_declaration (decl);
13037 decl = cp_parser_lookup_name_simple (parser,
13040 if (decl == error_mark_node)
13041 cp_parser_name_lookup_error (parser, identifier,
13044 else if (check_for_bare_parameter_packs (decl))
13046 else if (!at_namespace_scope_p ())
13047 do_local_using_decl (decl, qscope, identifier);
13049 do_toplevel_using_decl (decl, qscope, identifier);
13053 /* Look for the final `;'. */
13054 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13059 /* Parse a using-directive.
13062 using namespace :: [opt] nested-name-specifier [opt]
13063 namespace-name ; */
13066 cp_parser_using_directive (cp_parser* parser)
13068 tree namespace_decl;
13071 /* Look for the `using' keyword. */
13072 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13073 /* And the `namespace' keyword. */
13074 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13075 /* Look for the optional `::' operator. */
13076 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13077 /* And the optional nested-name-specifier. */
13078 cp_parser_nested_name_specifier_opt (parser,
13079 /*typename_keyword_p=*/false,
13080 /*check_dependency_p=*/true,
13082 /*is_declaration=*/true);
13083 /* Get the namespace being used. */
13084 namespace_decl = cp_parser_namespace_name (parser);
13085 /* And any specified attributes. */
13086 attribs = cp_parser_attributes_opt (parser);
13087 /* Update the symbol table. */
13088 parse_using_directive (namespace_decl, attribs);
13089 /* Look for the final `;'. */
13090 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13093 /* Parse an asm-definition.
13096 asm ( string-literal ) ;
13101 asm volatile [opt] ( string-literal ) ;
13102 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13103 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13104 : asm-operand-list [opt] ) ;
13105 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13106 : asm-operand-list [opt]
13107 : asm-clobber-list [opt] ) ;
13108 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13109 : asm-clobber-list [opt]
13110 : asm-goto-list ) ; */
13113 cp_parser_asm_definition (cp_parser* parser)
13116 tree outputs = NULL_TREE;
13117 tree inputs = NULL_TREE;
13118 tree clobbers = NULL_TREE;
13119 tree labels = NULL_TREE;
13121 bool volatile_p = false;
13122 bool extended_p = false;
13123 bool invalid_inputs_p = false;
13124 bool invalid_outputs_p = false;
13125 bool goto_p = false;
13126 const char *missing = NULL;
13128 /* Look for the `asm' keyword. */
13129 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13130 /* See if the next token is `volatile'. */
13131 if (cp_parser_allow_gnu_extensions_p (parser)
13132 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13134 /* Remember that we saw the `volatile' keyword. */
13136 /* Consume the token. */
13137 cp_lexer_consume_token (parser->lexer);
13139 if (cp_parser_allow_gnu_extensions_p (parser)
13140 && parser->in_function_body
13141 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13143 /* Remember that we saw the `goto' keyword. */
13145 /* Consume the token. */
13146 cp_lexer_consume_token (parser->lexer);
13148 /* Look for the opening `('. */
13149 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13151 /* Look for the string. */
13152 string = cp_parser_string_literal (parser, false, false);
13153 if (string == error_mark_node)
13155 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13156 /*consume_paren=*/true);
13160 /* If we're allowing GNU extensions, check for the extended assembly
13161 syntax. Unfortunately, the `:' tokens need not be separated by
13162 a space in C, and so, for compatibility, we tolerate that here
13163 too. Doing that means that we have to treat the `::' operator as
13165 if (cp_parser_allow_gnu_extensions_p (parser)
13166 && parser->in_function_body
13167 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13168 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13170 bool inputs_p = false;
13171 bool clobbers_p = false;
13172 bool labels_p = false;
13174 /* The extended syntax was used. */
13177 /* Look for outputs. */
13178 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13180 /* Consume the `:'. */
13181 cp_lexer_consume_token (parser->lexer);
13182 /* Parse the output-operands. */
13183 if (cp_lexer_next_token_is_not (parser->lexer,
13185 && cp_lexer_next_token_is_not (parser->lexer,
13187 && cp_lexer_next_token_is_not (parser->lexer,
13190 outputs = cp_parser_asm_operand_list (parser);
13192 if (outputs == error_mark_node)
13193 invalid_outputs_p = true;
13195 /* If the next token is `::', there are no outputs, and the
13196 next token is the beginning of the inputs. */
13197 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13198 /* The inputs are coming next. */
13201 /* Look for inputs. */
13203 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13205 /* Consume the `:' or `::'. */
13206 cp_lexer_consume_token (parser->lexer);
13207 /* Parse the output-operands. */
13208 if (cp_lexer_next_token_is_not (parser->lexer,
13210 && cp_lexer_next_token_is_not (parser->lexer,
13212 && cp_lexer_next_token_is_not (parser->lexer,
13214 inputs = cp_parser_asm_operand_list (parser);
13216 if (inputs == error_mark_node)
13217 invalid_inputs_p = true;
13219 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13220 /* The clobbers are coming next. */
13223 /* Look for clobbers. */
13225 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13228 /* Consume the `:' or `::'. */
13229 cp_lexer_consume_token (parser->lexer);
13230 /* Parse the clobbers. */
13231 if (cp_lexer_next_token_is_not (parser->lexer,
13233 && cp_lexer_next_token_is_not (parser->lexer,
13235 clobbers = cp_parser_asm_clobber_list (parser);
13238 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13239 /* The labels are coming next. */
13242 /* Look for labels. */
13244 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13247 /* Consume the `:' or `::'. */
13248 cp_lexer_consume_token (parser->lexer);
13249 /* Parse the labels. */
13250 labels = cp_parser_asm_label_list (parser);
13253 if (goto_p && !labels_p)
13254 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13257 missing = "%<:%> or %<::%>";
13259 /* Look for the closing `)'. */
13260 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13261 missing ? missing : "%<)%>"))
13262 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13263 /*consume_paren=*/true);
13264 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13266 if (!invalid_inputs_p && !invalid_outputs_p)
13268 /* Create the ASM_EXPR. */
13269 if (parser->in_function_body)
13271 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13272 inputs, clobbers, labels);
13273 /* If the extended syntax was not used, mark the ASM_EXPR. */
13276 tree temp = asm_stmt;
13277 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13278 temp = TREE_OPERAND (temp, 0);
13280 ASM_INPUT_P (temp) = 1;
13284 cgraph_add_asm_node (string);
13288 /* Declarators [gram.dcl.decl] */
13290 /* Parse an init-declarator.
13293 declarator initializer [opt]
13298 declarator asm-specification [opt] attributes [opt] initializer [opt]
13300 function-definition:
13301 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13303 decl-specifier-seq [opt] declarator function-try-block
13307 function-definition:
13308 __extension__ function-definition
13310 The DECL_SPECIFIERS apply to this declarator. Returns a
13311 representation of the entity declared. If MEMBER_P is TRUE, then
13312 this declarator appears in a class scope. The new DECL created by
13313 this declarator is returned.
13315 The CHECKS are access checks that should be performed once we know
13316 what entity is being declared (and, therefore, what classes have
13319 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13320 for a function-definition here as well. If the declarator is a
13321 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13322 be TRUE upon return. By that point, the function-definition will
13323 have been completely parsed.
13325 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13329 cp_parser_init_declarator (cp_parser* parser,
13330 cp_decl_specifier_seq *decl_specifiers,
13331 VEC (deferred_access_check,gc)* checks,
13332 bool function_definition_allowed_p,
13334 int declares_class_or_enum,
13335 bool* function_definition_p)
13337 cp_token *token = NULL, *asm_spec_start_token = NULL,
13338 *attributes_start_token = NULL;
13339 cp_declarator *declarator;
13340 tree prefix_attributes;
13342 tree asm_specification;
13344 tree decl = NULL_TREE;
13346 int is_initialized;
13347 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13348 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13350 enum cpp_ttype initialization_kind;
13351 bool is_direct_init = false;
13352 bool is_non_constant_init;
13353 int ctor_dtor_or_conv_p;
13355 tree pushed_scope = NULL;
13357 /* Gather the attributes that were provided with the
13358 decl-specifiers. */
13359 prefix_attributes = decl_specifiers->attributes;
13361 /* Assume that this is not the declarator for a function
13363 if (function_definition_p)
13364 *function_definition_p = false;
13366 /* Defer access checks while parsing the declarator; we cannot know
13367 what names are accessible until we know what is being
13369 resume_deferring_access_checks ();
13371 /* Parse the declarator. */
13372 token = cp_lexer_peek_token (parser->lexer);
13374 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13375 &ctor_dtor_or_conv_p,
13376 /*parenthesized_p=*/NULL,
13377 /*member_p=*/false);
13378 /* Gather up the deferred checks. */
13379 stop_deferring_access_checks ();
13381 /* If the DECLARATOR was erroneous, there's no need to go
13383 if (declarator == cp_error_declarator)
13384 return error_mark_node;
13386 /* Check that the number of template-parameter-lists is OK. */
13387 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13389 return error_mark_node;
13391 if (declares_class_or_enum & 2)
13392 cp_parser_check_for_definition_in_return_type (declarator,
13393 decl_specifiers->type,
13394 decl_specifiers->type_location);
13396 /* Figure out what scope the entity declared by the DECLARATOR is
13397 located in. `grokdeclarator' sometimes changes the scope, so
13398 we compute it now. */
13399 scope = get_scope_of_declarator (declarator);
13401 /* If we're allowing GNU extensions, look for an asm-specification
13403 if (cp_parser_allow_gnu_extensions_p (parser))
13405 /* Look for an asm-specification. */
13406 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13407 asm_specification = cp_parser_asm_specification_opt (parser);
13408 /* And attributes. */
13409 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13410 attributes = cp_parser_attributes_opt (parser);
13414 asm_specification = NULL_TREE;
13415 attributes = NULL_TREE;
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13420 /* Check to see if the token indicates the start of a
13421 function-definition. */
13422 if (function_declarator_p (declarator)
13423 && cp_parser_token_starts_function_definition_p (token))
13425 if (!function_definition_allowed_p)
13427 /* If a function-definition should not appear here, issue an
13429 cp_parser_error (parser,
13430 "a function-definition is not allowed here");
13431 return error_mark_node;
13435 location_t func_brace_location
13436 = cp_lexer_peek_token (parser->lexer)->location;
13438 /* Neither attributes nor an asm-specification are allowed
13439 on a function-definition. */
13440 if (asm_specification)
13441 error_at (asm_spec_start_token->location,
13442 "an asm-specification is not allowed "
13443 "on a function-definition");
13445 error_at (attributes_start_token->location,
13446 "attributes are not allowed on a function-definition");
13447 /* This is a function-definition. */
13448 *function_definition_p = true;
13450 /* Parse the function definition. */
13452 decl = cp_parser_save_member_function_body (parser,
13455 prefix_attributes);
13458 = (cp_parser_function_definition_from_specifiers_and_declarator
13459 (parser, decl_specifiers, prefix_attributes, declarator));
13461 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13463 /* This is where the prologue starts... */
13464 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13465 = func_brace_location;
13474 Only in function declarations for constructors, destructors, and
13475 type conversions can the decl-specifier-seq be omitted.
13477 We explicitly postpone this check past the point where we handle
13478 function-definitions because we tolerate function-definitions
13479 that are missing their return types in some modes. */
13480 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13482 cp_parser_error (parser,
13483 "expected constructor, destructor, or type conversion");
13484 return error_mark_node;
13487 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13488 if (token->type == CPP_EQ
13489 || token->type == CPP_OPEN_PAREN
13490 || token->type == CPP_OPEN_BRACE)
13492 is_initialized = SD_INITIALIZED;
13493 initialization_kind = token->type;
13495 if (token->type == CPP_EQ
13496 && function_declarator_p (declarator))
13498 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13499 if (t2->keyword == RID_DEFAULT)
13500 is_initialized = SD_DEFAULTED;
13501 else if (t2->keyword == RID_DELETE)
13502 is_initialized = SD_DELETED;
13507 /* If the init-declarator isn't initialized and isn't followed by a
13508 `,' or `;', it's not a valid init-declarator. */
13509 if (token->type != CPP_COMMA
13510 && token->type != CPP_SEMICOLON)
13512 cp_parser_error (parser, "expected initializer");
13513 return error_mark_node;
13515 is_initialized = SD_UNINITIALIZED;
13516 initialization_kind = CPP_EOF;
13519 /* Because start_decl has side-effects, we should only call it if we
13520 know we're going ahead. By this point, we know that we cannot
13521 possibly be looking at any other construct. */
13522 cp_parser_commit_to_tentative_parse (parser);
13524 /* If the decl specifiers were bad, issue an error now that we're
13525 sure this was intended to be a declarator. Then continue
13526 declaring the variable(s), as int, to try to cut down on further
13528 if (decl_specifiers->any_specifiers_p
13529 && decl_specifiers->type == error_mark_node)
13531 cp_parser_error (parser, "invalid type in declaration");
13532 decl_specifiers->type = integer_type_node;
13535 /* Check to see whether or not this declaration is a friend. */
13536 friend_p = cp_parser_friend_p (decl_specifiers);
13538 /* Enter the newly declared entry in the symbol table. If we're
13539 processing a declaration in a class-specifier, we wait until
13540 after processing the initializer. */
13543 if (parser->in_unbraced_linkage_specification_p)
13544 decl_specifiers->storage_class = sc_extern;
13545 decl = start_decl (declarator, decl_specifiers,
13546 is_initialized, attributes, prefix_attributes,
13550 /* Enter the SCOPE. That way unqualified names appearing in the
13551 initializer will be looked up in SCOPE. */
13552 pushed_scope = push_scope (scope);
13554 /* Perform deferred access control checks, now that we know in which
13555 SCOPE the declared entity resides. */
13556 if (!member_p && decl)
13558 tree saved_current_function_decl = NULL_TREE;
13560 /* If the entity being declared is a function, pretend that we
13561 are in its scope. If it is a `friend', it may have access to
13562 things that would not otherwise be accessible. */
13563 if (TREE_CODE (decl) == FUNCTION_DECL)
13565 saved_current_function_decl = current_function_decl;
13566 current_function_decl = decl;
13569 /* Perform access checks for template parameters. */
13570 cp_parser_perform_template_parameter_access_checks (checks);
13572 /* Perform the access control checks for the declarator and the
13573 decl-specifiers. */
13574 perform_deferred_access_checks ();
13576 /* Restore the saved value. */
13577 if (TREE_CODE (decl) == FUNCTION_DECL)
13578 current_function_decl = saved_current_function_decl;
13581 /* Parse the initializer. */
13582 initializer = NULL_TREE;
13583 is_direct_init = false;
13584 is_non_constant_init = true;
13585 if (is_initialized)
13587 if (function_declarator_p (declarator))
13589 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13590 if (initialization_kind == CPP_EQ)
13591 initializer = cp_parser_pure_specifier (parser);
13594 /* If the declaration was erroneous, we don't really
13595 know what the user intended, so just silently
13596 consume the initializer. */
13597 if (decl != error_mark_node)
13598 error_at (initializer_start_token->location,
13599 "initializer provided for function");
13600 cp_parser_skip_to_closing_parenthesis (parser,
13601 /*recovering=*/true,
13602 /*or_comma=*/false,
13603 /*consume_paren=*/true);
13608 /* We want to record the extra mangling scope for in-class
13609 initializers of class members and initializers of static data
13610 member templates. The former is a C++0x feature which isn't
13611 implemented yet, and I expect it will involve deferring
13612 parsing of the initializer until end of class as with default
13613 arguments. So right here we only handle the latter. */
13614 if (!member_p && processing_template_decl)
13615 start_lambda_scope (decl);
13616 initializer = cp_parser_initializer (parser,
13618 &is_non_constant_init);
13619 if (!member_p && processing_template_decl)
13620 finish_lambda_scope ();
13624 /* The old parser allows attributes to appear after a parenthesized
13625 initializer. Mark Mitchell proposed removing this functionality
13626 on the GCC mailing lists on 2002-08-13. This parser accepts the
13627 attributes -- but ignores them. */
13628 if (cp_parser_allow_gnu_extensions_p (parser)
13629 && initialization_kind == CPP_OPEN_PAREN)
13630 if (cp_parser_attributes_opt (parser))
13631 warning (OPT_Wattributes,
13632 "attributes after parenthesized initializer ignored");
13634 /* For an in-class declaration, use `grokfield' to create the
13640 pop_scope (pushed_scope);
13641 pushed_scope = false;
13643 decl = grokfield (declarator, decl_specifiers,
13644 initializer, !is_non_constant_init,
13645 /*asmspec=*/NULL_TREE,
13646 prefix_attributes);
13647 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13648 cp_parser_save_default_args (parser, decl);
13651 /* Finish processing the declaration. But, skip friend
13653 if (!friend_p && decl && decl != error_mark_node)
13655 cp_finish_decl (decl,
13656 initializer, !is_non_constant_init,
13658 /* If the initializer is in parentheses, then this is
13659 a direct-initialization, which means that an
13660 `explicit' constructor is OK. Otherwise, an
13661 `explicit' constructor cannot be used. */
13662 ((is_direct_init || !is_initialized)
13663 ? 0 : LOOKUP_ONLYCONVERTING));
13665 else if ((cxx_dialect != cxx98) && friend_p
13666 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13667 /* Core issue #226 (C++0x only): A default template-argument
13668 shall not be specified in a friend class template
13670 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13671 /*is_partial=*/0, /*is_friend_decl=*/1);
13673 if (!friend_p && pushed_scope)
13674 pop_scope (pushed_scope);
13679 /* Parse a declarator.
13683 ptr-operator declarator
13685 abstract-declarator:
13686 ptr-operator abstract-declarator [opt]
13687 direct-abstract-declarator
13692 attributes [opt] direct-declarator
13693 attributes [opt] ptr-operator declarator
13695 abstract-declarator:
13696 attributes [opt] ptr-operator abstract-declarator [opt]
13697 attributes [opt] direct-abstract-declarator
13699 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13700 detect constructor, destructor or conversion operators. It is set
13701 to -1 if the declarator is a name, and +1 if it is a
13702 function. Otherwise it is set to zero. Usually you just want to
13703 test for >0, but internally the negative value is used.
13705 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13706 a decl-specifier-seq unless it declares a constructor, destructor,
13707 or conversion. It might seem that we could check this condition in
13708 semantic analysis, rather than parsing, but that makes it difficult
13709 to handle something like `f()'. We want to notice that there are
13710 no decl-specifiers, and therefore realize that this is an
13711 expression, not a declaration.)
13713 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13714 the declarator is a direct-declarator of the form "(...)".
13716 MEMBER_P is true iff this declarator is a member-declarator. */
13718 static cp_declarator *
13719 cp_parser_declarator (cp_parser* parser,
13720 cp_parser_declarator_kind dcl_kind,
13721 int* ctor_dtor_or_conv_p,
13722 bool* parenthesized_p,
13726 cp_declarator *declarator;
13727 enum tree_code code;
13728 cp_cv_quals cv_quals;
13730 tree attributes = NULL_TREE;
13732 /* Assume this is not a constructor, destructor, or type-conversion
13734 if (ctor_dtor_or_conv_p)
13735 *ctor_dtor_or_conv_p = 0;
13737 if (cp_parser_allow_gnu_extensions_p (parser))
13738 attributes = cp_parser_attributes_opt (parser);
13740 /* Peek at the next token. */
13741 token = cp_lexer_peek_token (parser->lexer);
13743 /* Check for the ptr-operator production. */
13744 cp_parser_parse_tentatively (parser);
13745 /* Parse the ptr-operator. */
13746 code = cp_parser_ptr_operator (parser,
13749 /* If that worked, then we have a ptr-operator. */
13750 if (cp_parser_parse_definitely (parser))
13752 /* If a ptr-operator was found, then this declarator was not
13754 if (parenthesized_p)
13755 *parenthesized_p = true;
13756 /* The dependent declarator is optional if we are parsing an
13757 abstract-declarator. */
13758 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13759 cp_parser_parse_tentatively (parser);
13761 /* Parse the dependent declarator. */
13762 declarator = cp_parser_declarator (parser, dcl_kind,
13763 /*ctor_dtor_or_conv_p=*/NULL,
13764 /*parenthesized_p=*/NULL,
13765 /*member_p=*/false);
13767 /* If we are parsing an abstract-declarator, we must handle the
13768 case where the dependent declarator is absent. */
13769 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13770 && !cp_parser_parse_definitely (parser))
13773 declarator = cp_parser_make_indirect_declarator
13774 (code, class_type, cv_quals, declarator);
13776 /* Everything else is a direct-declarator. */
13779 if (parenthesized_p)
13780 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13782 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13783 ctor_dtor_or_conv_p,
13787 if (attributes && declarator && declarator != cp_error_declarator)
13788 declarator->attributes = attributes;
13793 /* Parse a direct-declarator or direct-abstract-declarator.
13797 direct-declarator ( parameter-declaration-clause )
13798 cv-qualifier-seq [opt]
13799 exception-specification [opt]
13800 direct-declarator [ constant-expression [opt] ]
13803 direct-abstract-declarator:
13804 direct-abstract-declarator [opt]
13805 ( parameter-declaration-clause )
13806 cv-qualifier-seq [opt]
13807 exception-specification [opt]
13808 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13809 ( abstract-declarator )
13811 Returns a representation of the declarator. DCL_KIND is
13812 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13813 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13814 we are parsing a direct-declarator. It is
13815 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13816 of ambiguity we prefer an abstract declarator, as per
13817 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13818 cp_parser_declarator. */
13820 static cp_declarator *
13821 cp_parser_direct_declarator (cp_parser* parser,
13822 cp_parser_declarator_kind dcl_kind,
13823 int* ctor_dtor_or_conv_p,
13827 cp_declarator *declarator = NULL;
13828 tree scope = NULL_TREE;
13829 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13830 bool saved_in_declarator_p = parser->in_declarator_p;
13832 tree pushed_scope = NULL_TREE;
13836 /* Peek at the next token. */
13837 token = cp_lexer_peek_token (parser->lexer);
13838 if (token->type == CPP_OPEN_PAREN)
13840 /* This is either a parameter-declaration-clause, or a
13841 parenthesized declarator. When we know we are parsing a
13842 named declarator, it must be a parenthesized declarator
13843 if FIRST is true. For instance, `(int)' is a
13844 parameter-declaration-clause, with an omitted
13845 direct-abstract-declarator. But `((*))', is a
13846 parenthesized abstract declarator. Finally, when T is a
13847 template parameter `(T)' is a
13848 parameter-declaration-clause, and not a parenthesized
13851 We first try and parse a parameter-declaration-clause,
13852 and then try a nested declarator (if FIRST is true).
13854 It is not an error for it not to be a
13855 parameter-declaration-clause, even when FIRST is
13861 The first is the declaration of a function while the
13862 second is the definition of a variable, including its
13865 Having seen only the parenthesis, we cannot know which of
13866 these two alternatives should be selected. Even more
13867 complex are examples like:
13872 The former is a function-declaration; the latter is a
13873 variable initialization.
13875 Thus again, we try a parameter-declaration-clause, and if
13876 that fails, we back out and return. */
13878 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13881 unsigned saved_num_template_parameter_lists;
13882 bool is_declarator = false;
13885 /* In a member-declarator, the only valid interpretation
13886 of a parenthesis is the start of a
13887 parameter-declaration-clause. (It is invalid to
13888 initialize a static data member with a parenthesized
13889 initializer; only the "=" form of initialization is
13892 cp_parser_parse_tentatively (parser);
13894 /* Consume the `('. */
13895 cp_lexer_consume_token (parser->lexer);
13898 /* If this is going to be an abstract declarator, we're
13899 in a declarator and we can't have default args. */
13900 parser->default_arg_ok_p = false;
13901 parser->in_declarator_p = true;
13904 /* Inside the function parameter list, surrounding
13905 template-parameter-lists do not apply. */
13906 saved_num_template_parameter_lists
13907 = parser->num_template_parameter_lists;
13908 parser->num_template_parameter_lists = 0;
13910 begin_scope (sk_function_parms, NULL_TREE);
13912 /* Parse the parameter-declaration-clause. */
13913 params = cp_parser_parameter_declaration_clause (parser);
13915 parser->num_template_parameter_lists
13916 = saved_num_template_parameter_lists;
13918 /* If all went well, parse the cv-qualifier-seq and the
13919 exception-specification. */
13920 if (member_p || cp_parser_parse_definitely (parser))
13922 cp_cv_quals cv_quals;
13923 tree exception_specification;
13926 is_declarator = true;
13928 if (ctor_dtor_or_conv_p)
13929 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13931 /* Consume the `)'. */
13932 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13934 /* Parse the cv-qualifier-seq. */
13935 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13936 /* And the exception-specification. */
13937 exception_specification
13938 = cp_parser_exception_specification_opt (parser);
13941 = cp_parser_late_return_type_opt (parser);
13943 /* Create the function-declarator. */
13944 declarator = make_call_declarator (declarator,
13947 exception_specification,
13949 /* Any subsequent parameter lists are to do with
13950 return type, so are not those of the declared
13952 parser->default_arg_ok_p = false;
13955 /* Remove the function parms from scope. */
13956 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13957 pop_binding (DECL_NAME (t), t);
13961 /* Repeat the main loop. */
13965 /* If this is the first, we can try a parenthesized
13969 bool saved_in_type_id_in_expr_p;
13971 parser->default_arg_ok_p = saved_default_arg_ok_p;
13972 parser->in_declarator_p = saved_in_declarator_p;
13974 /* Consume the `('. */
13975 cp_lexer_consume_token (parser->lexer);
13976 /* Parse the nested declarator. */
13977 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13978 parser->in_type_id_in_expr_p = true;
13980 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13981 /*parenthesized_p=*/NULL,
13983 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13985 /* Expect a `)'. */
13986 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13987 declarator = cp_error_declarator;
13988 if (declarator == cp_error_declarator)
13991 goto handle_declarator;
13993 /* Otherwise, we must be done. */
13997 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13998 && token->type == CPP_OPEN_SQUARE)
14000 /* Parse an array-declarator. */
14003 if (ctor_dtor_or_conv_p)
14004 *ctor_dtor_or_conv_p = 0;
14007 parser->default_arg_ok_p = false;
14008 parser->in_declarator_p = true;
14009 /* Consume the `['. */
14010 cp_lexer_consume_token (parser->lexer);
14011 /* Peek at the next token. */
14012 token = cp_lexer_peek_token (parser->lexer);
14013 /* If the next token is `]', then there is no
14014 constant-expression. */
14015 if (token->type != CPP_CLOSE_SQUARE)
14017 bool non_constant_p;
14020 = cp_parser_constant_expression (parser,
14021 /*allow_non_constant=*/true,
14023 if (!non_constant_p)
14024 bounds = fold_non_dependent_expr (bounds);
14025 /* Normally, the array bound must be an integral constant
14026 expression. However, as an extension, we allow VLAs
14027 in function scopes. */
14028 else if (!parser->in_function_body)
14030 error_at (token->location,
14031 "array bound is not an integer constant");
14032 bounds = error_mark_node;
14034 else if (processing_template_decl && !error_operand_p (bounds))
14036 /* Remember this wasn't a constant-expression. */
14037 bounds = build_nop (TREE_TYPE (bounds), bounds);
14038 TREE_SIDE_EFFECTS (bounds) = 1;
14042 bounds = NULL_TREE;
14043 /* Look for the closing `]'. */
14044 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14046 declarator = cp_error_declarator;
14050 declarator = make_array_declarator (declarator, bounds);
14052 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14055 tree qualifying_scope;
14056 tree unqualified_name;
14057 special_function_kind sfk;
14059 bool pack_expansion_p = false;
14060 cp_token *declarator_id_start_token;
14062 /* Parse a declarator-id */
14063 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14066 cp_parser_parse_tentatively (parser);
14068 /* If we see an ellipsis, we should be looking at a
14070 if (token->type == CPP_ELLIPSIS)
14072 /* Consume the `...' */
14073 cp_lexer_consume_token (parser->lexer);
14075 pack_expansion_p = true;
14079 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14081 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14082 qualifying_scope = parser->scope;
14087 if (!unqualified_name && pack_expansion_p)
14089 /* Check whether an error occurred. */
14090 okay = !cp_parser_error_occurred (parser);
14092 /* We already consumed the ellipsis to mark a
14093 parameter pack, but we have no way to report it,
14094 so abort the tentative parse. We will be exiting
14095 immediately anyway. */
14096 cp_parser_abort_tentative_parse (parser);
14099 okay = cp_parser_parse_definitely (parser);
14102 unqualified_name = error_mark_node;
14103 else if (unqualified_name
14104 && (qualifying_scope
14105 || (TREE_CODE (unqualified_name)
14106 != IDENTIFIER_NODE)))
14108 cp_parser_error (parser, "expected unqualified-id");
14109 unqualified_name = error_mark_node;
14113 if (!unqualified_name)
14115 if (unqualified_name == error_mark_node)
14117 declarator = cp_error_declarator;
14118 pack_expansion_p = false;
14119 declarator->parameter_pack_p = false;
14123 if (qualifying_scope && at_namespace_scope_p ()
14124 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14126 /* In the declaration of a member of a template class
14127 outside of the class itself, the SCOPE will sometimes
14128 be a TYPENAME_TYPE. For example, given:
14130 template <typename T>
14131 int S<T>::R::i = 3;
14133 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14134 this context, we must resolve S<T>::R to an ordinary
14135 type, rather than a typename type.
14137 The reason we normally avoid resolving TYPENAME_TYPEs
14138 is that a specialization of `S' might render
14139 `S<T>::R' not a type. However, if `S' is
14140 specialized, then this `i' will not be used, so there
14141 is no harm in resolving the types here. */
14144 /* Resolve the TYPENAME_TYPE. */
14145 type = resolve_typename_type (qualifying_scope,
14146 /*only_current_p=*/false);
14147 /* If that failed, the declarator is invalid. */
14148 if (TREE_CODE (type) == TYPENAME_TYPE)
14149 error_at (declarator_id_start_token->location,
14150 "%<%T::%E%> is not a type",
14151 TYPE_CONTEXT (qualifying_scope),
14152 TYPE_IDENTIFIER (qualifying_scope));
14153 qualifying_scope = type;
14158 if (unqualified_name)
14162 if (qualifying_scope
14163 && CLASS_TYPE_P (qualifying_scope))
14164 class_type = qualifying_scope;
14166 class_type = current_class_type;
14168 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14170 tree name_type = TREE_TYPE (unqualified_name);
14171 if (class_type && same_type_p (name_type, class_type))
14173 if (qualifying_scope
14174 && CLASSTYPE_USE_TEMPLATE (name_type))
14176 error_at (declarator_id_start_token->location,
14177 "invalid use of constructor as a template");
14178 inform (declarator_id_start_token->location,
14179 "use %<%T::%D%> instead of %<%T::%D%> to "
14180 "name the constructor in a qualified name",
14182 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14183 class_type, name_type);
14184 declarator = cp_error_declarator;
14188 unqualified_name = constructor_name (class_type);
14192 /* We do not attempt to print the declarator
14193 here because we do not have enough
14194 information about its original syntactic
14196 cp_parser_error (parser, "invalid declarator");
14197 declarator = cp_error_declarator;
14204 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14205 sfk = sfk_destructor;
14206 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14207 sfk = sfk_conversion;
14208 else if (/* There's no way to declare a constructor
14209 for an anonymous type, even if the type
14210 got a name for linkage purposes. */
14211 !TYPE_WAS_ANONYMOUS (class_type)
14212 && constructor_name_p (unqualified_name,
14215 unqualified_name = constructor_name (class_type);
14216 sfk = sfk_constructor;
14219 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14220 *ctor_dtor_or_conv_p = -1;
14223 declarator = make_id_declarator (qualifying_scope,
14226 declarator->id_loc = token->location;
14227 declarator->parameter_pack_p = pack_expansion_p;
14229 if (pack_expansion_p)
14230 maybe_warn_variadic_templates ();
14233 handle_declarator:;
14234 scope = get_scope_of_declarator (declarator);
14236 /* Any names that appear after the declarator-id for a
14237 member are looked up in the containing scope. */
14238 pushed_scope = push_scope (scope);
14239 parser->in_declarator_p = true;
14240 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14241 || (declarator && declarator->kind == cdk_id))
14242 /* Default args are only allowed on function
14244 parser->default_arg_ok_p = saved_default_arg_ok_p;
14246 parser->default_arg_ok_p = false;
14255 /* For an abstract declarator, we might wind up with nothing at this
14256 point. That's an error; the declarator is not optional. */
14258 cp_parser_error (parser, "expected declarator");
14260 /* If we entered a scope, we must exit it now. */
14262 pop_scope (pushed_scope);
14264 parser->default_arg_ok_p = saved_default_arg_ok_p;
14265 parser->in_declarator_p = saved_in_declarator_p;
14270 /* Parse a ptr-operator.
14273 * cv-qualifier-seq [opt]
14275 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14280 & cv-qualifier-seq [opt]
14282 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14283 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14284 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14285 filled in with the TYPE containing the member. *CV_QUALS is
14286 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14287 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14288 Note that the tree codes returned by this function have nothing
14289 to do with the types of trees that will be eventually be created
14290 to represent the pointer or reference type being parsed. They are
14291 just constants with suggestive names. */
14292 static enum tree_code
14293 cp_parser_ptr_operator (cp_parser* parser,
14295 cp_cv_quals *cv_quals)
14297 enum tree_code code = ERROR_MARK;
14300 /* Assume that it's not a pointer-to-member. */
14302 /* And that there are no cv-qualifiers. */
14303 *cv_quals = TYPE_UNQUALIFIED;
14305 /* Peek at the next token. */
14306 token = cp_lexer_peek_token (parser->lexer);
14308 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14309 if (token->type == CPP_MULT)
14310 code = INDIRECT_REF;
14311 else if (token->type == CPP_AND)
14313 else if ((cxx_dialect != cxx98) &&
14314 token->type == CPP_AND_AND) /* C++0x only */
14315 code = NON_LVALUE_EXPR;
14317 if (code != ERROR_MARK)
14319 /* Consume the `*', `&' or `&&'. */
14320 cp_lexer_consume_token (parser->lexer);
14322 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14323 `&', if we are allowing GNU extensions. (The only qualifier
14324 that can legally appear after `&' is `restrict', but that is
14325 enforced during semantic analysis. */
14326 if (code == INDIRECT_REF
14327 || cp_parser_allow_gnu_extensions_p (parser))
14328 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14332 /* Try the pointer-to-member case. */
14333 cp_parser_parse_tentatively (parser);
14334 /* Look for the optional `::' operator. */
14335 cp_parser_global_scope_opt (parser,
14336 /*current_scope_valid_p=*/false);
14337 /* Look for the nested-name specifier. */
14338 token = cp_lexer_peek_token (parser->lexer);
14339 cp_parser_nested_name_specifier (parser,
14340 /*typename_keyword_p=*/false,
14341 /*check_dependency_p=*/true,
14343 /*is_declaration=*/false);
14344 /* If we found it, and the next token is a `*', then we are
14345 indeed looking at a pointer-to-member operator. */
14346 if (!cp_parser_error_occurred (parser)
14347 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14349 /* Indicate that the `*' operator was used. */
14350 code = INDIRECT_REF;
14352 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14353 error_at (token->location, "%qD is a namespace", parser->scope);
14356 /* The type of which the member is a member is given by the
14358 *type = parser->scope;
14359 /* The next name will not be qualified. */
14360 parser->scope = NULL_TREE;
14361 parser->qualifying_scope = NULL_TREE;
14362 parser->object_scope = NULL_TREE;
14363 /* Look for the optional cv-qualifier-seq. */
14364 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14367 /* If that didn't work we don't have a ptr-operator. */
14368 if (!cp_parser_parse_definitely (parser))
14369 cp_parser_error (parser, "expected ptr-operator");
14375 /* Parse an (optional) cv-qualifier-seq.
14378 cv-qualifier cv-qualifier-seq [opt]
14389 Returns a bitmask representing the cv-qualifiers. */
14392 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14394 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14399 cp_cv_quals cv_qualifier;
14401 /* Peek at the next token. */
14402 token = cp_lexer_peek_token (parser->lexer);
14403 /* See if it's a cv-qualifier. */
14404 switch (token->keyword)
14407 cv_qualifier = TYPE_QUAL_CONST;
14411 cv_qualifier = TYPE_QUAL_VOLATILE;
14415 cv_qualifier = TYPE_QUAL_RESTRICT;
14419 cv_qualifier = TYPE_UNQUALIFIED;
14426 if (cv_quals & cv_qualifier)
14428 error_at (token->location, "duplicate cv-qualifier");
14429 cp_lexer_purge_token (parser->lexer);
14433 cp_lexer_consume_token (parser->lexer);
14434 cv_quals |= cv_qualifier;
14441 /* Parse a late-specified return type, if any. This is not a separate
14442 non-terminal, but part of a function declarator, which looks like
14444 -> trailing-type-specifier-seq abstract-declarator(opt)
14446 Returns the type indicated by the type-id. */
14449 cp_parser_late_return_type_opt (cp_parser* parser)
14453 /* Peek at the next token. */
14454 token = cp_lexer_peek_token (parser->lexer);
14455 /* A late-specified return type is indicated by an initial '->'. */
14456 if (token->type != CPP_DEREF)
14459 /* Consume the ->. */
14460 cp_lexer_consume_token (parser->lexer);
14462 return cp_parser_trailing_type_id (parser);
14465 /* Parse a declarator-id.
14469 :: [opt] nested-name-specifier [opt] type-name
14471 In the `id-expression' case, the value returned is as for
14472 cp_parser_id_expression if the id-expression was an unqualified-id.
14473 If the id-expression was a qualified-id, then a SCOPE_REF is
14474 returned. The first operand is the scope (either a NAMESPACE_DECL
14475 or TREE_TYPE), but the second is still just a representation of an
14479 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14482 /* The expression must be an id-expression. Assume that qualified
14483 names are the names of types so that:
14486 int S<T>::R::i = 3;
14488 will work; we must treat `S<T>::R' as the name of a type.
14489 Similarly, assume that qualified names are templates, where
14493 int S<T>::R<T>::i = 3;
14496 id = cp_parser_id_expression (parser,
14497 /*template_keyword_p=*/false,
14498 /*check_dependency_p=*/false,
14499 /*template_p=*/NULL,
14500 /*declarator_p=*/true,
14502 if (id && BASELINK_P (id))
14503 id = BASELINK_FUNCTIONS (id);
14507 /* Parse a type-id.
14510 type-specifier-seq abstract-declarator [opt]
14512 Returns the TYPE specified. */
14515 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14516 bool is_trailing_return)
14518 cp_decl_specifier_seq type_specifier_seq;
14519 cp_declarator *abstract_declarator;
14521 /* Parse the type-specifier-seq. */
14522 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14523 is_trailing_return,
14524 &type_specifier_seq);
14525 if (type_specifier_seq.type == error_mark_node)
14526 return error_mark_node;
14528 /* There might or might not be an abstract declarator. */
14529 cp_parser_parse_tentatively (parser);
14530 /* Look for the declarator. */
14531 abstract_declarator
14532 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14533 /*parenthesized_p=*/NULL,
14534 /*member_p=*/false);
14535 /* Check to see if there really was a declarator. */
14536 if (!cp_parser_parse_definitely (parser))
14537 abstract_declarator = NULL;
14539 if (type_specifier_seq.type
14540 && type_uses_auto (type_specifier_seq.type))
14542 /* A type-id with type 'auto' is only ok if the abstract declarator
14543 is a function declarator with a late-specified return type. */
14544 if (abstract_declarator
14545 && abstract_declarator->kind == cdk_function
14546 && abstract_declarator->u.function.late_return_type)
14550 error ("invalid use of %<auto%>");
14551 return error_mark_node;
14555 return groktypename (&type_specifier_seq, abstract_declarator,
14559 static tree cp_parser_type_id (cp_parser *parser)
14561 return cp_parser_type_id_1 (parser, false, false);
14564 static tree cp_parser_template_type_arg (cp_parser *parser)
14566 return cp_parser_type_id_1 (parser, true, false);
14569 static tree cp_parser_trailing_type_id (cp_parser *parser)
14571 return cp_parser_type_id_1 (parser, false, true);
14574 /* Parse a type-specifier-seq.
14576 type-specifier-seq:
14577 type-specifier type-specifier-seq [opt]
14581 type-specifier-seq:
14582 attributes type-specifier-seq [opt]
14584 If IS_CONDITION is true, we are at the start of a "condition",
14585 e.g., we've just seen "if (".
14587 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14588 i.e. we've just seen "->".
14590 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14593 cp_parser_type_specifier_seq (cp_parser* parser,
14595 bool is_trailing_return,
14596 cp_decl_specifier_seq *type_specifier_seq)
14598 bool seen_type_specifier = false;
14599 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14600 cp_token *start_token = NULL;
14602 /* Clear the TYPE_SPECIFIER_SEQ. */
14603 clear_decl_specs (type_specifier_seq);
14605 /* In the context of a trailing return type, enum E { } is an
14606 elaborated-type-specifier followed by a function-body, not an
14608 if (is_trailing_return)
14609 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14611 /* Parse the type-specifiers and attributes. */
14614 tree type_specifier;
14615 bool is_cv_qualifier;
14617 /* Check for attributes first. */
14618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14620 type_specifier_seq->attributes =
14621 chainon (type_specifier_seq->attributes,
14622 cp_parser_attributes_opt (parser));
14626 /* record the token of the beginning of the type specifier seq,
14627 for error reporting purposes*/
14629 start_token = cp_lexer_peek_token (parser->lexer);
14631 /* Look for the type-specifier. */
14632 type_specifier = cp_parser_type_specifier (parser,
14634 type_specifier_seq,
14635 /*is_declaration=*/false,
14638 if (!type_specifier)
14640 /* If the first type-specifier could not be found, this is not a
14641 type-specifier-seq at all. */
14642 if (!seen_type_specifier)
14644 cp_parser_error (parser, "expected type-specifier");
14645 type_specifier_seq->type = error_mark_node;
14648 /* If subsequent type-specifiers could not be found, the
14649 type-specifier-seq is complete. */
14653 seen_type_specifier = true;
14654 /* The standard says that a condition can be:
14656 type-specifier-seq declarator = assignment-expression
14663 we should treat the "S" as a declarator, not as a
14664 type-specifier. The standard doesn't say that explicitly for
14665 type-specifier-seq, but it does say that for
14666 decl-specifier-seq in an ordinary declaration. Perhaps it
14667 would be clearer just to allow a decl-specifier-seq here, and
14668 then add a semantic restriction that if any decl-specifiers
14669 that are not type-specifiers appear, the program is invalid. */
14670 if (is_condition && !is_cv_qualifier)
14671 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14674 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14677 /* Parse a parameter-declaration-clause.
14679 parameter-declaration-clause:
14680 parameter-declaration-list [opt] ... [opt]
14681 parameter-declaration-list , ...
14683 Returns a representation for the parameter declarations. A return
14684 value of NULL indicates a parameter-declaration-clause consisting
14685 only of an ellipsis. */
14688 cp_parser_parameter_declaration_clause (cp_parser* parser)
14695 /* Peek at the next token. */
14696 token = cp_lexer_peek_token (parser->lexer);
14697 /* Check for trivial parameter-declaration-clauses. */
14698 if (token->type == CPP_ELLIPSIS)
14700 /* Consume the `...' token. */
14701 cp_lexer_consume_token (parser->lexer);
14704 else if (token->type == CPP_CLOSE_PAREN)
14705 /* There are no parameters. */
14707 #ifndef NO_IMPLICIT_EXTERN_C
14708 if (in_system_header && current_class_type == NULL
14709 && current_lang_name == lang_name_c)
14713 return void_list_node;
14715 /* Check for `(void)', too, which is a special case. */
14716 else if (token->keyword == RID_VOID
14717 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14718 == CPP_CLOSE_PAREN))
14720 /* Consume the `void' token. */
14721 cp_lexer_consume_token (parser->lexer);
14722 /* There are no parameters. */
14723 return void_list_node;
14726 /* Parse the parameter-declaration-list. */
14727 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14728 /* If a parse error occurred while parsing the
14729 parameter-declaration-list, then the entire
14730 parameter-declaration-clause is erroneous. */
14734 /* Peek at the next token. */
14735 token = cp_lexer_peek_token (parser->lexer);
14736 /* If it's a `,', the clause should terminate with an ellipsis. */
14737 if (token->type == CPP_COMMA)
14739 /* Consume the `,'. */
14740 cp_lexer_consume_token (parser->lexer);
14741 /* Expect an ellipsis. */
14743 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14745 /* It might also be `...' if the optional trailing `,' was
14747 else if (token->type == CPP_ELLIPSIS)
14749 /* Consume the `...' token. */
14750 cp_lexer_consume_token (parser->lexer);
14751 /* And remember that we saw it. */
14755 ellipsis_p = false;
14757 /* Finish the parameter list. */
14759 parameters = chainon (parameters, void_list_node);
14764 /* Parse a parameter-declaration-list.
14766 parameter-declaration-list:
14767 parameter-declaration
14768 parameter-declaration-list , parameter-declaration
14770 Returns a representation of the parameter-declaration-list, as for
14771 cp_parser_parameter_declaration_clause. However, the
14772 `void_list_node' is never appended to the list. Upon return,
14773 *IS_ERROR will be true iff an error occurred. */
14776 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14778 tree parameters = NULL_TREE;
14779 tree *tail = ¶meters;
14780 bool saved_in_unbraced_linkage_specification_p;
14783 /* Assume all will go well. */
14785 /* The special considerations that apply to a function within an
14786 unbraced linkage specifications do not apply to the parameters
14787 to the function. */
14788 saved_in_unbraced_linkage_specification_p
14789 = parser->in_unbraced_linkage_specification_p;
14790 parser->in_unbraced_linkage_specification_p = false;
14792 /* Look for more parameters. */
14795 cp_parameter_declarator *parameter;
14796 tree decl = error_mark_node;
14797 bool parenthesized_p;
14798 /* Parse the parameter. */
14800 = cp_parser_parameter_declaration (parser,
14801 /*template_parm_p=*/false,
14804 /* We don't know yet if the enclosing context is deprecated, so wait
14805 and warn in grokparms if appropriate. */
14806 deprecated_state = DEPRECATED_SUPPRESS;
14809 decl = grokdeclarator (parameter->declarator,
14810 ¶meter->decl_specifiers,
14812 parameter->default_argument != NULL_TREE,
14813 ¶meter->decl_specifiers.attributes);
14815 deprecated_state = DEPRECATED_NORMAL;
14817 /* If a parse error occurred parsing the parameter declaration,
14818 then the entire parameter-declaration-list is erroneous. */
14819 if (decl == error_mark_node)
14822 parameters = error_mark_node;
14826 if (parameter->decl_specifiers.attributes)
14827 cplus_decl_attributes (&decl,
14828 parameter->decl_specifiers.attributes,
14830 if (DECL_NAME (decl))
14831 decl = pushdecl (decl);
14833 if (decl != error_mark_node)
14835 retrofit_lang_decl (decl);
14836 DECL_PARM_INDEX (decl) = ++index;
14839 /* Add the new parameter to the list. */
14840 *tail = build_tree_list (parameter->default_argument, decl);
14841 tail = &TREE_CHAIN (*tail);
14843 /* Peek at the next token. */
14844 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14845 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14846 /* These are for Objective-C++ */
14847 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14848 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14849 /* The parameter-declaration-list is complete. */
14851 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14855 /* Peek at the next token. */
14856 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14857 /* If it's an ellipsis, then the list is complete. */
14858 if (token->type == CPP_ELLIPSIS)
14860 /* Otherwise, there must be more parameters. Consume the
14862 cp_lexer_consume_token (parser->lexer);
14863 /* When parsing something like:
14865 int i(float f, double d)
14867 we can tell after seeing the declaration for "f" that we
14868 are not looking at an initialization of a variable "i",
14869 but rather at the declaration of a function "i".
14871 Due to the fact that the parsing of template arguments
14872 (as specified to a template-id) requires backtracking we
14873 cannot use this technique when inside a template argument
14875 if (!parser->in_template_argument_list_p
14876 && !parser->in_type_id_in_expr_p
14877 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14878 /* However, a parameter-declaration of the form
14879 "foat(f)" (which is a valid declaration of a
14880 parameter "f") can also be interpreted as an
14881 expression (the conversion of "f" to "float"). */
14882 && !parenthesized_p)
14883 cp_parser_commit_to_tentative_parse (parser);
14887 cp_parser_error (parser, "expected %<,%> or %<...%>");
14888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14889 cp_parser_skip_to_closing_parenthesis (parser,
14890 /*recovering=*/true,
14891 /*or_comma=*/false,
14892 /*consume_paren=*/false);
14897 parser->in_unbraced_linkage_specification_p
14898 = saved_in_unbraced_linkage_specification_p;
14903 /* Parse a parameter declaration.
14905 parameter-declaration:
14906 decl-specifier-seq ... [opt] declarator
14907 decl-specifier-seq declarator = assignment-expression
14908 decl-specifier-seq ... [opt] abstract-declarator [opt]
14909 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14911 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14912 declares a template parameter. (In that case, a non-nested `>'
14913 token encountered during the parsing of the assignment-expression
14914 is not interpreted as a greater-than operator.)
14916 Returns a representation of the parameter, or NULL if an error
14917 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14918 true iff the declarator is of the form "(p)". */
14920 static cp_parameter_declarator *
14921 cp_parser_parameter_declaration (cp_parser *parser,
14922 bool template_parm_p,
14923 bool *parenthesized_p)
14925 int declares_class_or_enum;
14926 bool greater_than_is_operator_p;
14927 cp_decl_specifier_seq decl_specifiers;
14928 cp_declarator *declarator;
14929 tree default_argument;
14930 cp_token *token = NULL, *declarator_token_start = NULL;
14931 const char *saved_message;
14933 /* In a template parameter, `>' is not an operator.
14937 When parsing a default template-argument for a non-type
14938 template-parameter, the first non-nested `>' is taken as the end
14939 of the template parameter-list rather than a greater-than
14941 greater_than_is_operator_p = !template_parm_p;
14943 /* Type definitions may not appear in parameter types. */
14944 saved_message = parser->type_definition_forbidden_message;
14945 parser->type_definition_forbidden_message
14946 = "types may not be defined in parameter types";
14948 /* Parse the declaration-specifiers. */
14949 cp_parser_decl_specifier_seq (parser,
14950 CP_PARSER_FLAGS_NONE,
14952 &declares_class_or_enum);
14953 /* If an error occurred, there's no reason to attempt to parse the
14954 rest of the declaration. */
14955 if (cp_parser_error_occurred (parser))
14957 parser->type_definition_forbidden_message = saved_message;
14961 /* Peek at the next token. */
14962 token = cp_lexer_peek_token (parser->lexer);
14964 /* If the next token is a `)', `,', `=', `>', or `...', then there
14965 is no declarator. However, when variadic templates are enabled,
14966 there may be a declarator following `...'. */
14967 if (token->type == CPP_CLOSE_PAREN
14968 || token->type == CPP_COMMA
14969 || token->type == CPP_EQ
14970 || token->type == CPP_GREATER)
14973 if (parenthesized_p)
14974 *parenthesized_p = false;
14976 /* Otherwise, there should be a declarator. */
14979 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14980 parser->default_arg_ok_p = false;
14982 /* After seeing a decl-specifier-seq, if the next token is not a
14983 "(", there is no possibility that the code is a valid
14984 expression. Therefore, if parsing tentatively, we commit at
14986 if (!parser->in_template_argument_list_p
14987 /* In an expression context, having seen:
14991 we cannot be sure whether we are looking at a
14992 function-type (taking a "char" as a parameter) or a cast
14993 of some object of type "char" to "int". */
14994 && !parser->in_type_id_in_expr_p
14995 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14996 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14997 cp_parser_commit_to_tentative_parse (parser);
14998 /* Parse the declarator. */
14999 declarator_token_start = token;
15000 declarator = cp_parser_declarator (parser,
15001 CP_PARSER_DECLARATOR_EITHER,
15002 /*ctor_dtor_or_conv_p=*/NULL,
15004 /*member_p=*/false);
15005 parser->default_arg_ok_p = saved_default_arg_ok_p;
15006 /* After the declarator, allow more attributes. */
15007 decl_specifiers.attributes
15008 = chainon (decl_specifiers.attributes,
15009 cp_parser_attributes_opt (parser));
15012 /* If the next token is an ellipsis, and we have not seen a
15013 declarator name, and the type of the declarator contains parameter
15014 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15015 a parameter pack expansion expression. Otherwise, leave the
15016 ellipsis for a C-style variadic function. */
15017 token = cp_lexer_peek_token (parser->lexer);
15018 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15020 tree type = decl_specifiers.type;
15022 if (type && DECL_P (type))
15023 type = TREE_TYPE (type);
15026 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15027 && declarator_can_be_parameter_pack (declarator)
15028 && (!declarator || !declarator->parameter_pack_p)
15029 && uses_parameter_packs (type))
15031 /* Consume the `...'. */
15032 cp_lexer_consume_token (parser->lexer);
15033 maybe_warn_variadic_templates ();
15035 /* Build a pack expansion type */
15037 declarator->parameter_pack_p = true;
15039 decl_specifiers.type = make_pack_expansion (type);
15043 /* The restriction on defining new types applies only to the type
15044 of the parameter, not to the default argument. */
15045 parser->type_definition_forbidden_message = saved_message;
15047 /* If the next token is `=', then process a default argument. */
15048 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15050 /* Consume the `='. */
15051 cp_lexer_consume_token (parser->lexer);
15053 /* If we are defining a class, then the tokens that make up the
15054 default argument must be saved and processed later. */
15055 if (!template_parm_p && at_class_scope_p ()
15056 && TYPE_BEING_DEFINED (current_class_type)
15057 && !LAMBDA_TYPE_P (current_class_type))
15059 unsigned depth = 0;
15060 int maybe_template_id = 0;
15061 cp_token *first_token;
15064 /* Add tokens until we have processed the entire default
15065 argument. We add the range [first_token, token). */
15066 first_token = cp_lexer_peek_token (parser->lexer);
15071 /* Peek at the next token. */
15072 token = cp_lexer_peek_token (parser->lexer);
15073 /* What we do depends on what token we have. */
15074 switch (token->type)
15076 /* In valid code, a default argument must be
15077 immediately followed by a `,' `)', or `...'. */
15079 if (depth == 0 && maybe_template_id)
15081 /* If we've seen a '<', we might be in a
15082 template-argument-list. Until Core issue 325 is
15083 resolved, we don't know how this situation ought
15084 to be handled, so try to DTRT. We check whether
15085 what comes after the comma is a valid parameter
15086 declaration list. If it is, then the comma ends
15087 the default argument; otherwise the default
15088 argument continues. */
15089 bool error = false;
15091 /* Set ITALP so cp_parser_parameter_declaration_list
15092 doesn't decide to commit to this parse. */
15093 bool saved_italp = parser->in_template_argument_list_p;
15094 parser->in_template_argument_list_p = true;
15096 cp_parser_parse_tentatively (parser);
15097 cp_lexer_consume_token (parser->lexer);
15098 cp_parser_parameter_declaration_list (parser, &error);
15099 if (!cp_parser_error_occurred (parser) && !error)
15101 cp_parser_abort_tentative_parse (parser);
15103 parser->in_template_argument_list_p = saved_italp;
15106 case CPP_CLOSE_PAREN:
15108 /* If we run into a non-nested `;', `}', or `]',
15109 then the code is invalid -- but the default
15110 argument is certainly over. */
15111 case CPP_SEMICOLON:
15112 case CPP_CLOSE_BRACE:
15113 case CPP_CLOSE_SQUARE:
15116 /* Update DEPTH, if necessary. */
15117 else if (token->type == CPP_CLOSE_PAREN
15118 || token->type == CPP_CLOSE_BRACE
15119 || token->type == CPP_CLOSE_SQUARE)
15123 case CPP_OPEN_PAREN:
15124 case CPP_OPEN_SQUARE:
15125 case CPP_OPEN_BRACE:
15131 /* This might be the comparison operator, or it might
15132 start a template argument list. */
15133 ++maybe_template_id;
15137 if (cxx_dialect == cxx98)
15139 /* Fall through for C++0x, which treats the `>>'
15140 operator like two `>' tokens in certain
15146 /* This might be an operator, or it might close a
15147 template argument list. But if a previous '<'
15148 started a template argument list, this will have
15149 closed it, so we can't be in one anymore. */
15150 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15151 if (maybe_template_id < 0)
15152 maybe_template_id = 0;
15156 /* If we run out of tokens, issue an error message. */
15158 case CPP_PRAGMA_EOL:
15159 error_at (token->location, "file ends in default argument");
15165 /* In these cases, we should look for template-ids.
15166 For example, if the default argument is
15167 `X<int, double>()', we need to do name lookup to
15168 figure out whether or not `X' is a template; if
15169 so, the `,' does not end the default argument.
15171 That is not yet done. */
15178 /* If we've reached the end, stop. */
15182 /* Add the token to the token block. */
15183 token = cp_lexer_consume_token (parser->lexer);
15186 /* Create a DEFAULT_ARG to represent the unparsed default
15188 default_argument = make_node (DEFAULT_ARG);
15189 DEFARG_TOKENS (default_argument)
15190 = cp_token_cache_new (first_token, token);
15191 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15193 /* Outside of a class definition, we can just parse the
15194 assignment-expression. */
15197 token = cp_lexer_peek_token (parser->lexer);
15199 = cp_parser_default_argument (parser, template_parm_p);
15202 if (!parser->default_arg_ok_p)
15204 if (flag_permissive)
15205 warning (0, "deprecated use of default argument for parameter of non-function");
15208 error_at (token->location,
15209 "default arguments are only "
15210 "permitted for function parameters");
15211 default_argument = NULL_TREE;
15214 else if ((declarator && declarator->parameter_pack_p)
15215 || (decl_specifiers.type
15216 && PACK_EXPANSION_P (decl_specifiers.type)))
15218 /* Find the name of the parameter pack. */
15219 cp_declarator *id_declarator = declarator;
15220 while (id_declarator && id_declarator->kind != cdk_id)
15221 id_declarator = id_declarator->declarator;
15223 if (id_declarator && id_declarator->kind == cdk_id)
15224 error_at (declarator_token_start->location,
15226 ? "template parameter pack %qD"
15227 " cannot have a default argument"
15228 : "parameter pack %qD cannot have a default argument",
15229 id_declarator->u.id.unqualified_name);
15231 error_at (declarator_token_start->location,
15233 ? "template parameter pack cannot have a default argument"
15234 : "parameter pack cannot have a default argument");
15236 default_argument = NULL_TREE;
15240 default_argument = NULL_TREE;
15242 return make_parameter_declarator (&decl_specifiers,
15247 /* Parse a default argument and return it.
15249 TEMPLATE_PARM_P is true if this is a default argument for a
15250 non-type template parameter. */
15252 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15254 tree default_argument = NULL_TREE;
15255 bool saved_greater_than_is_operator_p;
15256 bool saved_local_variables_forbidden_p;
15258 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15260 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15261 parser->greater_than_is_operator_p = !template_parm_p;
15262 /* Local variable names (and the `this' keyword) may not
15263 appear in a default argument. */
15264 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15265 parser->local_variables_forbidden_p = true;
15266 /* Parse the assignment-expression. */
15267 if (template_parm_p)
15268 push_deferring_access_checks (dk_no_deferred);
15270 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15271 if (template_parm_p)
15272 pop_deferring_access_checks ();
15273 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15274 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15276 return default_argument;
15279 /* Parse a function-body.
15282 compound_statement */
15285 cp_parser_function_body (cp_parser *parser)
15287 cp_parser_compound_statement (parser, NULL, false);
15290 /* Parse a ctor-initializer-opt followed by a function-body. Return
15291 true if a ctor-initializer was present. */
15294 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15297 bool ctor_initializer_p;
15299 /* Begin the function body. */
15300 body = begin_function_body ();
15301 /* Parse the optional ctor-initializer. */
15302 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15303 /* Parse the function-body. */
15304 cp_parser_function_body (parser);
15305 /* Finish the function body. */
15306 finish_function_body (body);
15308 return ctor_initializer_p;
15311 /* Parse an initializer.
15314 = initializer-clause
15315 ( expression-list )
15317 Returns an expression representing the initializer. If no
15318 initializer is present, NULL_TREE is returned.
15320 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15321 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15322 set to TRUE if there is no initializer present. If there is an
15323 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15324 is set to true; otherwise it is set to false. */
15327 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15328 bool* non_constant_p)
15333 /* Peek at the next token. */
15334 token = cp_lexer_peek_token (parser->lexer);
15336 /* Let our caller know whether or not this initializer was
15338 *is_direct_init = (token->type != CPP_EQ);
15339 /* Assume that the initializer is constant. */
15340 *non_constant_p = false;
15342 if (token->type == CPP_EQ)
15344 /* Consume the `='. */
15345 cp_lexer_consume_token (parser->lexer);
15346 /* Parse the initializer-clause. */
15347 init = cp_parser_initializer_clause (parser, non_constant_p);
15349 else if (token->type == CPP_OPEN_PAREN)
15352 vec = cp_parser_parenthesized_expression_list (parser, false,
15354 /*allow_expansion_p=*/true,
15357 return error_mark_node;
15358 init = build_tree_list_vec (vec);
15359 release_tree_vector (vec);
15361 else if (token->type == CPP_OPEN_BRACE)
15363 maybe_warn_cpp0x ("extended initializer lists");
15364 init = cp_parser_braced_list (parser, non_constant_p);
15365 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15369 /* Anything else is an error. */
15370 cp_parser_error (parser, "expected initializer");
15371 init = error_mark_node;
15377 /* Parse an initializer-clause.
15379 initializer-clause:
15380 assignment-expression
15383 Returns an expression representing the initializer.
15385 If the `assignment-expression' production is used the value
15386 returned is simply a representation for the expression.
15388 Otherwise, calls cp_parser_braced_list. */
15391 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15395 /* Assume the expression is constant. */
15396 *non_constant_p = false;
15398 /* If it is not a `{', then we are looking at an
15399 assignment-expression. */
15400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15403 = cp_parser_constant_expression (parser,
15404 /*allow_non_constant_p=*/true,
15406 if (!*non_constant_p)
15407 initializer = fold_non_dependent_expr (initializer);
15410 initializer = cp_parser_braced_list (parser, non_constant_p);
15412 return initializer;
15415 /* Parse a brace-enclosed initializer list.
15418 { initializer-list , [opt] }
15421 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15422 the elements of the initializer-list (or NULL, if the last
15423 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15424 NULL_TREE. There is no way to detect whether or not the optional
15425 trailing `,' was provided. NON_CONSTANT_P is as for
15426 cp_parser_initializer. */
15429 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15433 /* Consume the `{' token. */
15434 cp_lexer_consume_token (parser->lexer);
15435 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15436 initializer = make_node (CONSTRUCTOR);
15437 /* If it's not a `}', then there is a non-trivial initializer. */
15438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15440 /* Parse the initializer list. */
15441 CONSTRUCTOR_ELTS (initializer)
15442 = cp_parser_initializer_list (parser, non_constant_p);
15443 /* A trailing `,' token is allowed. */
15444 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15445 cp_lexer_consume_token (parser->lexer);
15447 /* Now, there should be a trailing `}'. */
15448 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15449 TREE_TYPE (initializer) = init_list_type_node;
15450 return initializer;
15453 /* Parse an initializer-list.
15456 initializer-clause ... [opt]
15457 initializer-list , initializer-clause ... [opt]
15462 identifier : initializer-clause
15463 initializer-list, identifier : initializer-clause
15465 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15466 for the initializer. If the INDEX of the elt is non-NULL, it is the
15467 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15468 as for cp_parser_initializer. */
15470 static VEC(constructor_elt,gc) *
15471 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15473 VEC(constructor_elt,gc) *v = NULL;
15475 /* Assume all of the expressions are constant. */
15476 *non_constant_p = false;
15478 /* Parse the rest of the list. */
15484 bool clause_non_constant_p;
15486 /* If the next token is an identifier and the following one is a
15487 colon, we are looking at the GNU designated-initializer
15489 if (cp_parser_allow_gnu_extensions_p (parser)
15490 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15491 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15493 /* Warn the user that they are using an extension. */
15494 pedwarn (input_location, OPT_pedantic,
15495 "ISO C++ does not allow designated initializers");
15496 /* Consume the identifier. */
15497 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15498 /* Consume the `:'. */
15499 cp_lexer_consume_token (parser->lexer);
15502 identifier = NULL_TREE;
15504 /* Parse the initializer. */
15505 initializer = cp_parser_initializer_clause (parser,
15506 &clause_non_constant_p);
15507 /* If any clause is non-constant, so is the entire initializer. */
15508 if (clause_non_constant_p)
15509 *non_constant_p = true;
15511 /* If we have an ellipsis, this is an initializer pack
15513 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15515 /* Consume the `...'. */
15516 cp_lexer_consume_token (parser->lexer);
15518 /* Turn the initializer into an initializer expansion. */
15519 initializer = make_pack_expansion (initializer);
15522 /* Add it to the vector. */
15523 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15525 /* If the next token is not a comma, we have reached the end of
15527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15530 /* Peek at the next token. */
15531 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15532 /* If the next token is a `}', then we're still done. An
15533 initializer-clause can have a trailing `,' after the
15534 initializer-list and before the closing `}'. */
15535 if (token->type == CPP_CLOSE_BRACE)
15538 /* Consume the `,' token. */
15539 cp_lexer_consume_token (parser->lexer);
15545 /* Classes [gram.class] */
15547 /* Parse a class-name.
15553 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15554 to indicate that names looked up in dependent types should be
15555 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15556 keyword has been used to indicate that the name that appears next
15557 is a template. TAG_TYPE indicates the explicit tag given before
15558 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15559 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15560 is the class being defined in a class-head.
15562 Returns the TYPE_DECL representing the class. */
15565 cp_parser_class_name (cp_parser *parser,
15566 bool typename_keyword_p,
15567 bool template_keyword_p,
15568 enum tag_types tag_type,
15569 bool check_dependency_p,
15571 bool is_declaration)
15577 tree identifier = NULL_TREE;
15579 /* All class-names start with an identifier. */
15580 token = cp_lexer_peek_token (parser->lexer);
15581 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15583 cp_parser_error (parser, "expected class-name");
15584 return error_mark_node;
15587 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15588 to a template-id, so we save it here. */
15589 scope = parser->scope;
15590 if (scope == error_mark_node)
15591 return error_mark_node;
15593 /* Any name names a type if we're following the `typename' keyword
15594 in a qualified name where the enclosing scope is type-dependent. */
15595 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15596 && dependent_type_p (scope));
15597 /* Handle the common case (an identifier, but not a template-id)
15599 if (token->type == CPP_NAME
15600 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15602 cp_token *identifier_token;
15605 /* Look for the identifier. */
15606 identifier_token = cp_lexer_peek_token (parser->lexer);
15607 ambiguous_p = identifier_token->ambiguous_p;
15608 identifier = cp_parser_identifier (parser);
15609 /* If the next token isn't an identifier, we are certainly not
15610 looking at a class-name. */
15611 if (identifier == error_mark_node)
15612 decl = error_mark_node;
15613 /* If we know this is a type-name, there's no need to look it
15615 else if (typename_p)
15619 tree ambiguous_decls;
15620 /* If we already know that this lookup is ambiguous, then
15621 we've already issued an error message; there's no reason
15625 cp_parser_simulate_error (parser);
15626 return error_mark_node;
15628 /* If the next token is a `::', then the name must be a type
15631 [basic.lookup.qual]
15633 During the lookup for a name preceding the :: scope
15634 resolution operator, object, function, and enumerator
15635 names are ignored. */
15636 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15637 tag_type = typename_type;
15638 /* Look up the name. */
15639 decl = cp_parser_lookup_name (parser, identifier,
15641 /*is_template=*/false,
15642 /*is_namespace=*/false,
15643 check_dependency_p,
15645 identifier_token->location);
15646 if (ambiguous_decls)
15648 error_at (identifier_token->location,
15649 "reference to %qD is ambiguous", identifier);
15650 print_candidates (ambiguous_decls);
15651 if (cp_parser_parsing_tentatively (parser))
15653 identifier_token->ambiguous_p = true;
15654 cp_parser_simulate_error (parser);
15656 return error_mark_node;
15662 /* Try a template-id. */
15663 decl = cp_parser_template_id (parser, template_keyword_p,
15664 check_dependency_p,
15666 if (decl == error_mark_node)
15667 return error_mark_node;
15670 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15672 /* If this is a typename, create a TYPENAME_TYPE. */
15673 if (typename_p && decl != error_mark_node)
15675 decl = make_typename_type (scope, decl, typename_type,
15676 /*complain=*/tf_error);
15677 if (decl != error_mark_node)
15678 decl = TYPE_NAME (decl);
15681 /* Check to see that it is really the name of a class. */
15682 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15683 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15684 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15685 /* Situations like this:
15687 template <typename T> struct A {
15688 typename T::template X<int>::I i;
15691 are problematic. Is `T::template X<int>' a class-name? The
15692 standard does not seem to be definitive, but there is no other
15693 valid interpretation of the following `::'. Therefore, those
15694 names are considered class-names. */
15696 decl = make_typename_type (scope, decl, tag_type, tf_error);
15697 if (decl != error_mark_node)
15698 decl = TYPE_NAME (decl);
15700 else if (TREE_CODE (decl) != TYPE_DECL
15701 || TREE_TYPE (decl) == error_mark_node
15702 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15703 decl = error_mark_node;
15705 if (decl == error_mark_node)
15706 cp_parser_error (parser, "expected class-name");
15707 else if (identifier && !parser->scope)
15708 maybe_note_name_used_in_class (identifier, decl);
15713 /* Parse a class-specifier.
15716 class-head { member-specification [opt] }
15718 Returns the TREE_TYPE representing the class. */
15721 cp_parser_class_specifier (cp_parser* parser)
15724 tree attributes = NULL_TREE;
15725 bool nested_name_specifier_p;
15726 unsigned saved_num_template_parameter_lists;
15727 bool saved_in_function_body;
15728 bool saved_in_unbraced_linkage_specification_p;
15729 tree old_scope = NULL_TREE;
15730 tree scope = NULL_TREE;
15733 push_deferring_access_checks (dk_no_deferred);
15735 /* Parse the class-head. */
15736 type = cp_parser_class_head (parser,
15737 &nested_name_specifier_p,
15740 /* If the class-head was a semantic disaster, skip the entire body
15744 cp_parser_skip_to_end_of_block_or_statement (parser);
15745 pop_deferring_access_checks ();
15746 return error_mark_node;
15749 /* Look for the `{'. */
15750 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15752 pop_deferring_access_checks ();
15753 return error_mark_node;
15756 /* Process the base classes. If they're invalid, skip the
15757 entire class body. */
15758 if (!xref_basetypes (type, bases))
15760 /* Consuming the closing brace yields better error messages
15762 if (cp_parser_skip_to_closing_brace (parser))
15763 cp_lexer_consume_token (parser->lexer);
15764 pop_deferring_access_checks ();
15765 return error_mark_node;
15768 /* Issue an error message if type-definitions are forbidden here. */
15769 cp_parser_check_type_definition (parser);
15770 /* Remember that we are defining one more class. */
15771 ++parser->num_classes_being_defined;
15772 /* Inside the class, surrounding template-parameter-lists do not
15774 saved_num_template_parameter_lists
15775 = parser->num_template_parameter_lists;
15776 parser->num_template_parameter_lists = 0;
15777 /* We are not in a function body. */
15778 saved_in_function_body = parser->in_function_body;
15779 parser->in_function_body = false;
15780 /* We are not immediately inside an extern "lang" block. */
15781 saved_in_unbraced_linkage_specification_p
15782 = parser->in_unbraced_linkage_specification_p;
15783 parser->in_unbraced_linkage_specification_p = false;
15785 /* Start the class. */
15786 if (nested_name_specifier_p)
15788 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15789 old_scope = push_inner_scope (scope);
15791 type = begin_class_definition (type, attributes);
15793 if (type == error_mark_node)
15794 /* If the type is erroneous, skip the entire body of the class. */
15795 cp_parser_skip_to_closing_brace (parser);
15797 /* Parse the member-specification. */
15798 cp_parser_member_specification_opt (parser);
15800 /* Look for the trailing `}'. */
15801 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15802 /* Look for trailing attributes to apply to this class. */
15803 if (cp_parser_allow_gnu_extensions_p (parser))
15804 attributes = cp_parser_attributes_opt (parser);
15805 if (type != error_mark_node)
15806 type = finish_struct (type, attributes);
15807 if (nested_name_specifier_p)
15808 pop_inner_scope (old_scope, scope);
15809 /* If this class is not itself within the scope of another class,
15810 then we need to parse the bodies of all of the queued function
15811 definitions. Note that the queued functions defined in a class
15812 are not always processed immediately following the
15813 class-specifier for that class. Consider:
15816 struct B { void f() { sizeof (A); } };
15819 If `f' were processed before the processing of `A' were
15820 completed, there would be no way to compute the size of `A'.
15821 Note that the nesting we are interested in here is lexical --
15822 not the semantic nesting given by TYPE_CONTEXT. In particular,
15825 struct A { struct B; };
15826 struct A::B { void f() { } };
15828 there is no need to delay the parsing of `A::B::f'. */
15829 if (--parser->num_classes_being_defined == 0)
15833 tree class_type = NULL_TREE;
15834 tree pushed_scope = NULL_TREE;
15836 /* In a first pass, parse default arguments to the functions.
15837 Then, in a second pass, parse the bodies of the functions.
15838 This two-phased approach handles cases like:
15846 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15847 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15848 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15849 TREE_PURPOSE (parser->unparsed_functions_queues)
15850 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15852 fn = TREE_VALUE (queue_entry);
15853 /* If there are default arguments that have not yet been processed,
15854 take care of them now. */
15855 if (class_type != TREE_PURPOSE (queue_entry))
15858 pop_scope (pushed_scope);
15859 class_type = TREE_PURPOSE (queue_entry);
15860 pushed_scope = push_scope (class_type);
15862 /* Make sure that any template parameters are in scope. */
15863 maybe_begin_member_template_processing (fn);
15864 /* Parse the default argument expressions. */
15865 cp_parser_late_parsing_default_args (parser, fn);
15866 /* Remove any template parameters from the symbol table. */
15867 maybe_end_member_template_processing ();
15870 pop_scope (pushed_scope);
15871 /* Now parse the body of the functions. */
15872 for (TREE_VALUE (parser->unparsed_functions_queues)
15873 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15874 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15875 TREE_VALUE (parser->unparsed_functions_queues)
15876 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15878 /* Figure out which function we need to process. */
15879 fn = TREE_VALUE (queue_entry);
15880 /* Parse the function. */
15881 cp_parser_late_parsing_for_member (parser, fn);
15885 /* Put back any saved access checks. */
15886 pop_deferring_access_checks ();
15888 /* Restore saved state. */
15889 parser->in_function_body = saved_in_function_body;
15890 parser->num_template_parameter_lists
15891 = saved_num_template_parameter_lists;
15892 parser->in_unbraced_linkage_specification_p
15893 = saved_in_unbraced_linkage_specification_p;
15898 /* Parse a class-head.
15901 class-key identifier [opt] base-clause [opt]
15902 class-key nested-name-specifier identifier base-clause [opt]
15903 class-key nested-name-specifier [opt] template-id
15907 class-key attributes identifier [opt] base-clause [opt]
15908 class-key attributes nested-name-specifier identifier base-clause [opt]
15909 class-key attributes nested-name-specifier [opt] template-id
15912 Upon return BASES is initialized to the list of base classes (or
15913 NULL, if there are none) in the same form returned by
15914 cp_parser_base_clause.
15916 Returns the TYPE of the indicated class. Sets
15917 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15918 involving a nested-name-specifier was used, and FALSE otherwise.
15920 Returns error_mark_node if this is not a class-head.
15922 Returns NULL_TREE if the class-head is syntactically valid, but
15923 semantically invalid in a way that means we should skip the entire
15924 body of the class. */
15927 cp_parser_class_head (cp_parser* parser,
15928 bool* nested_name_specifier_p,
15929 tree *attributes_p,
15932 tree nested_name_specifier;
15933 enum tag_types class_key;
15934 tree id = NULL_TREE;
15935 tree type = NULL_TREE;
15937 bool template_id_p = false;
15938 bool qualified_p = false;
15939 bool invalid_nested_name_p = false;
15940 bool invalid_explicit_specialization_p = false;
15941 tree pushed_scope = NULL_TREE;
15942 unsigned num_templates;
15943 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15944 /* Assume no nested-name-specifier will be present. */
15945 *nested_name_specifier_p = false;
15946 /* Assume no template parameter lists will be used in defining the
15950 *bases = NULL_TREE;
15952 /* Look for the class-key. */
15953 class_key = cp_parser_class_key (parser);
15954 if (class_key == none_type)
15955 return error_mark_node;
15957 /* Parse the attributes. */
15958 attributes = cp_parser_attributes_opt (parser);
15960 /* If the next token is `::', that is invalid -- but sometimes
15961 people do try to write:
15965 Handle this gracefully by accepting the extra qualifier, and then
15966 issuing an error about it later if this really is a
15967 class-head. If it turns out just to be an elaborated type
15968 specifier, remain silent. */
15969 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15970 qualified_p = true;
15972 push_deferring_access_checks (dk_no_check);
15974 /* Determine the name of the class. Begin by looking for an
15975 optional nested-name-specifier. */
15976 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15977 nested_name_specifier
15978 = cp_parser_nested_name_specifier_opt (parser,
15979 /*typename_keyword_p=*/false,
15980 /*check_dependency_p=*/false,
15982 /*is_declaration=*/false);
15983 /* If there was a nested-name-specifier, then there *must* be an
15985 if (nested_name_specifier)
15987 type_start_token = cp_lexer_peek_token (parser->lexer);
15988 /* Although the grammar says `identifier', it really means
15989 `class-name' or `template-name'. You are only allowed to
15990 define a class that has already been declared with this
15993 The proposed resolution for Core Issue 180 says that wherever
15994 you see `class T::X' you should treat `X' as a type-name.
15996 It is OK to define an inaccessible class; for example:
15998 class A { class B; };
16001 We do not know if we will see a class-name, or a
16002 template-name. We look for a class-name first, in case the
16003 class-name is a template-id; if we looked for the
16004 template-name first we would stop after the template-name. */
16005 cp_parser_parse_tentatively (parser);
16006 type = cp_parser_class_name (parser,
16007 /*typename_keyword_p=*/false,
16008 /*template_keyword_p=*/false,
16010 /*check_dependency_p=*/false,
16011 /*class_head_p=*/true,
16012 /*is_declaration=*/false);
16013 /* If that didn't work, ignore the nested-name-specifier. */
16014 if (!cp_parser_parse_definitely (parser))
16016 invalid_nested_name_p = true;
16017 type_start_token = cp_lexer_peek_token (parser->lexer);
16018 id = cp_parser_identifier (parser);
16019 if (id == error_mark_node)
16022 /* If we could not find a corresponding TYPE, treat this
16023 declaration like an unqualified declaration. */
16024 if (type == error_mark_node)
16025 nested_name_specifier = NULL_TREE;
16026 /* Otherwise, count the number of templates used in TYPE and its
16027 containing scopes. */
16032 for (scope = TREE_TYPE (type);
16033 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16034 scope = (TYPE_P (scope)
16035 ? TYPE_CONTEXT (scope)
16036 : DECL_CONTEXT (scope)))
16038 && CLASS_TYPE_P (scope)
16039 && CLASSTYPE_TEMPLATE_INFO (scope)
16040 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16041 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16045 /* Otherwise, the identifier is optional. */
16048 /* We don't know whether what comes next is a template-id,
16049 an identifier, or nothing at all. */
16050 cp_parser_parse_tentatively (parser);
16051 /* Check for a template-id. */
16052 type_start_token = cp_lexer_peek_token (parser->lexer);
16053 id = cp_parser_template_id (parser,
16054 /*template_keyword_p=*/false,
16055 /*check_dependency_p=*/true,
16056 /*is_declaration=*/true);
16057 /* If that didn't work, it could still be an identifier. */
16058 if (!cp_parser_parse_definitely (parser))
16060 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16062 type_start_token = cp_lexer_peek_token (parser->lexer);
16063 id = cp_parser_identifier (parser);
16070 template_id_p = true;
16075 pop_deferring_access_checks ();
16078 cp_parser_check_for_invalid_template_id (parser, id,
16079 type_start_token->location);
16081 /* If it's not a `:' or a `{' then we can't really be looking at a
16082 class-head, since a class-head only appears as part of a
16083 class-specifier. We have to detect this situation before calling
16084 xref_tag, since that has irreversible side-effects. */
16085 if (!cp_parser_next_token_starts_class_definition_p (parser))
16087 cp_parser_error (parser, "expected %<{%> or %<:%>");
16088 return error_mark_node;
16091 /* At this point, we're going ahead with the class-specifier, even
16092 if some other problem occurs. */
16093 cp_parser_commit_to_tentative_parse (parser);
16094 /* Issue the error about the overly-qualified name now. */
16097 cp_parser_error (parser,
16098 "global qualification of class name is invalid");
16099 return error_mark_node;
16101 else if (invalid_nested_name_p)
16103 cp_parser_error (parser,
16104 "qualified name does not name a class");
16105 return error_mark_node;
16107 else if (nested_name_specifier)
16111 /* Reject typedef-names in class heads. */
16112 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16114 error_at (type_start_token->location,
16115 "invalid class name in declaration of %qD",
16121 /* Figure out in what scope the declaration is being placed. */
16122 scope = current_scope ();
16123 /* If that scope does not contain the scope in which the
16124 class was originally declared, the program is invalid. */
16125 if (scope && !is_ancestor (scope, nested_name_specifier))
16127 if (at_namespace_scope_p ())
16128 error_at (type_start_token->location,
16129 "declaration of %qD in namespace %qD which does not "
16131 type, scope, nested_name_specifier);
16133 error_at (type_start_token->location,
16134 "declaration of %qD in %qD which does not enclose %qD",
16135 type, scope, nested_name_specifier);
16141 A declarator-id shall not be qualified except for the
16142 definition of a ... nested class outside of its class
16143 ... [or] the definition or explicit instantiation of a
16144 class member of a namespace outside of its namespace. */
16145 if (scope == nested_name_specifier)
16147 permerror (nested_name_specifier_token_start->location,
16148 "extra qualification not allowed");
16149 nested_name_specifier = NULL_TREE;
16153 /* An explicit-specialization must be preceded by "template <>". If
16154 it is not, try to recover gracefully. */
16155 if (at_namespace_scope_p ()
16156 && parser->num_template_parameter_lists == 0
16159 error_at (type_start_token->location,
16160 "an explicit specialization must be preceded by %<template <>%>");
16161 invalid_explicit_specialization_p = true;
16162 /* Take the same action that would have been taken by
16163 cp_parser_explicit_specialization. */
16164 ++parser->num_template_parameter_lists;
16165 begin_specialization ();
16167 /* There must be no "return" statements between this point and the
16168 end of this function; set "type "to the correct return value and
16169 use "goto done;" to return. */
16170 /* Make sure that the right number of template parameters were
16172 if (!cp_parser_check_template_parameters (parser, num_templates,
16173 type_start_token->location,
16174 /*declarator=*/NULL))
16176 /* If something went wrong, there is no point in even trying to
16177 process the class-definition. */
16182 /* Look up the type. */
16185 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16186 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16187 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16189 error_at (type_start_token->location,
16190 "function template %qD redeclared as a class template", id);
16191 type = error_mark_node;
16195 type = TREE_TYPE (id);
16196 type = maybe_process_partial_specialization (type);
16198 if (nested_name_specifier)
16199 pushed_scope = push_scope (nested_name_specifier);
16201 else if (nested_name_specifier)
16207 template <typename T> struct S { struct T };
16208 template <typename T> struct S<T>::T { };
16210 we will get a TYPENAME_TYPE when processing the definition of
16211 `S::T'. We need to resolve it to the actual type before we
16212 try to define it. */
16213 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16215 class_type = resolve_typename_type (TREE_TYPE (type),
16216 /*only_current_p=*/false);
16217 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16218 type = TYPE_NAME (class_type);
16221 cp_parser_error (parser, "could not resolve typename type");
16222 type = error_mark_node;
16226 if (maybe_process_partial_specialization (TREE_TYPE (type))
16227 == error_mark_node)
16233 class_type = current_class_type;
16234 /* Enter the scope indicated by the nested-name-specifier. */
16235 pushed_scope = push_scope (nested_name_specifier);
16236 /* Get the canonical version of this type. */
16237 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16238 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16239 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16241 type = push_template_decl (type);
16242 if (type == error_mark_node)
16249 type = TREE_TYPE (type);
16250 *nested_name_specifier_p = true;
16252 else /* The name is not a nested name. */
16254 /* If the class was unnamed, create a dummy name. */
16256 id = make_anon_name ();
16257 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16258 parser->num_template_parameter_lists);
16261 /* Indicate whether this class was declared as a `class' or as a
16263 if (TREE_CODE (type) == RECORD_TYPE)
16264 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16265 cp_parser_check_class_key (class_key, type);
16267 /* If this type was already complete, and we see another definition,
16268 that's an error. */
16269 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16271 error_at (type_start_token->location, "redefinition of %q#T",
16273 error_at (type_start_token->location, "previous definition of %q+#T",
16278 else if (type == error_mark_node)
16281 /* We will have entered the scope containing the class; the names of
16282 base classes should be looked up in that context. For example:
16284 struct A { struct B {}; struct C; };
16285 struct A::C : B {};
16289 /* Get the list of base-classes, if there is one. */
16290 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16291 *bases = cp_parser_base_clause (parser);
16294 /* Leave the scope given by the nested-name-specifier. We will
16295 enter the class scope itself while processing the members. */
16297 pop_scope (pushed_scope);
16299 if (invalid_explicit_specialization_p)
16301 end_specialization ();
16302 --parser->num_template_parameter_lists;
16304 *attributes_p = attributes;
16308 /* Parse a class-key.
16315 Returns the kind of class-key specified, or none_type to indicate
16318 static enum tag_types
16319 cp_parser_class_key (cp_parser* parser)
16322 enum tag_types tag_type;
16324 /* Look for the class-key. */
16325 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16329 /* Check to see if the TOKEN is a class-key. */
16330 tag_type = cp_parser_token_is_class_key (token);
16332 cp_parser_error (parser, "expected class-key");
16336 /* Parse an (optional) member-specification.
16338 member-specification:
16339 member-declaration member-specification [opt]
16340 access-specifier : member-specification [opt] */
16343 cp_parser_member_specification_opt (cp_parser* parser)
16350 /* Peek at the next token. */
16351 token = cp_lexer_peek_token (parser->lexer);
16352 /* If it's a `}', or EOF then we've seen all the members. */
16353 if (token->type == CPP_CLOSE_BRACE
16354 || token->type == CPP_EOF
16355 || token->type == CPP_PRAGMA_EOL)
16358 /* See if this token is a keyword. */
16359 keyword = token->keyword;
16363 case RID_PROTECTED:
16365 /* Consume the access-specifier. */
16366 cp_lexer_consume_token (parser->lexer);
16367 /* Remember which access-specifier is active. */
16368 current_access_specifier = token->u.value;
16369 /* Look for the `:'. */
16370 cp_parser_require (parser, CPP_COLON, "%<:%>");
16374 /* Accept #pragmas at class scope. */
16375 if (token->type == CPP_PRAGMA)
16377 cp_parser_pragma (parser, pragma_external);
16381 /* Otherwise, the next construction must be a
16382 member-declaration. */
16383 cp_parser_member_declaration (parser);
16388 /* Parse a member-declaration.
16390 member-declaration:
16391 decl-specifier-seq [opt] member-declarator-list [opt] ;
16392 function-definition ; [opt]
16393 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16395 template-declaration
16397 member-declarator-list:
16399 member-declarator-list , member-declarator
16402 declarator pure-specifier [opt]
16403 declarator constant-initializer [opt]
16404 identifier [opt] : constant-expression
16408 member-declaration:
16409 __extension__ member-declaration
16412 declarator attributes [opt] pure-specifier [opt]
16413 declarator attributes [opt] constant-initializer [opt]
16414 identifier [opt] attributes [opt] : constant-expression
16418 member-declaration:
16419 static_assert-declaration */
16422 cp_parser_member_declaration (cp_parser* parser)
16424 cp_decl_specifier_seq decl_specifiers;
16425 tree prefix_attributes;
16427 int declares_class_or_enum;
16429 cp_token *token = NULL;
16430 cp_token *decl_spec_token_start = NULL;
16431 cp_token *initializer_token_start = NULL;
16432 int saved_pedantic;
16434 /* Check for the `__extension__' keyword. */
16435 if (cp_parser_extension_opt (parser, &saved_pedantic))
16438 cp_parser_member_declaration (parser);
16439 /* Restore the old value of the PEDANTIC flag. */
16440 pedantic = saved_pedantic;
16445 /* Check for a template-declaration. */
16446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16448 /* An explicit specialization here is an error condition, and we
16449 expect the specialization handler to detect and report this. */
16450 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16451 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16452 cp_parser_explicit_specialization (parser);
16454 cp_parser_template_declaration (parser, /*member_p=*/true);
16459 /* Check for a using-declaration. */
16460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16462 /* Parse the using-declaration. */
16463 cp_parser_using_declaration (parser,
16464 /*access_declaration_p=*/false);
16468 /* Check for @defs. */
16469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16472 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16473 ivar = ivar_chains;
16477 ivar = TREE_CHAIN (member);
16478 TREE_CHAIN (member) = NULL_TREE;
16479 finish_member_declaration (member);
16484 /* If the next token is `static_assert' we have a static assertion. */
16485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16487 cp_parser_static_assert (parser, /*member_p=*/true);
16491 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16494 /* Parse the decl-specifier-seq. */
16495 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16496 cp_parser_decl_specifier_seq (parser,
16497 CP_PARSER_FLAGS_OPTIONAL,
16499 &declares_class_or_enum);
16500 prefix_attributes = decl_specifiers.attributes;
16501 decl_specifiers.attributes = NULL_TREE;
16502 /* Check for an invalid type-name. */
16503 if (!decl_specifiers.type
16504 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16506 /* If there is no declarator, then the decl-specifier-seq should
16508 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16510 /* If there was no decl-specifier-seq, and the next token is a
16511 `;', then we have something like:
16517 Each member-declaration shall declare at least one member
16518 name of the class. */
16519 if (!decl_specifiers.any_specifiers_p)
16521 cp_token *token = cp_lexer_peek_token (parser->lexer);
16522 if (!in_system_header_at (token->location))
16523 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16529 /* See if this declaration is a friend. */
16530 friend_p = cp_parser_friend_p (&decl_specifiers);
16531 /* If there were decl-specifiers, check to see if there was
16532 a class-declaration. */
16533 type = check_tag_decl (&decl_specifiers);
16534 /* Nested classes have already been added to the class, but
16535 a `friend' needs to be explicitly registered. */
16538 /* If the `friend' keyword was present, the friend must
16539 be introduced with a class-key. */
16540 if (!declares_class_or_enum)
16541 error_at (decl_spec_token_start->location,
16542 "a class-key must be used when declaring a friend");
16545 template <typename T> struct A {
16546 friend struct A<T>::B;
16549 A<T>::B will be represented by a TYPENAME_TYPE, and
16550 therefore not recognized by check_tag_decl. */
16552 && decl_specifiers.type
16553 && TYPE_P (decl_specifiers.type))
16554 type = decl_specifiers.type;
16555 if (!type || !TYPE_P (type))
16556 error_at (decl_spec_token_start->location,
16557 "friend declaration does not name a class or "
16560 make_friend_class (current_class_type, type,
16561 /*complain=*/true);
16563 /* If there is no TYPE, an error message will already have
16565 else if (!type || type == error_mark_node)
16567 /* An anonymous aggregate has to be handled specially; such
16568 a declaration really declares a data member (with a
16569 particular type), as opposed to a nested class. */
16570 else if (ANON_AGGR_TYPE_P (type))
16572 /* Remove constructors and such from TYPE, now that we
16573 know it is an anonymous aggregate. */
16574 fixup_anonymous_aggr (type);
16575 /* And make the corresponding data member. */
16576 decl = build_decl (decl_spec_token_start->location,
16577 FIELD_DECL, NULL_TREE, type);
16578 /* Add it to the class. */
16579 finish_member_declaration (decl);
16582 cp_parser_check_access_in_redeclaration
16584 decl_spec_token_start->location);
16589 /* See if these declarations will be friends. */
16590 friend_p = cp_parser_friend_p (&decl_specifiers);
16592 /* Keep going until we hit the `;' at the end of the
16594 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16596 tree attributes = NULL_TREE;
16597 tree first_attribute;
16599 /* Peek at the next token. */
16600 token = cp_lexer_peek_token (parser->lexer);
16602 /* Check for a bitfield declaration. */
16603 if (token->type == CPP_COLON
16604 || (token->type == CPP_NAME
16605 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16611 /* Get the name of the bitfield. Note that we cannot just
16612 check TOKEN here because it may have been invalidated by
16613 the call to cp_lexer_peek_nth_token above. */
16614 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16615 identifier = cp_parser_identifier (parser);
16617 identifier = NULL_TREE;
16619 /* Consume the `:' token. */
16620 cp_lexer_consume_token (parser->lexer);
16621 /* Get the width of the bitfield. */
16623 = cp_parser_constant_expression (parser,
16624 /*allow_non_constant=*/false,
16627 /* Look for attributes that apply to the bitfield. */
16628 attributes = cp_parser_attributes_opt (parser);
16629 /* Remember which attributes are prefix attributes and
16631 first_attribute = attributes;
16632 /* Combine the attributes. */
16633 attributes = chainon (prefix_attributes, attributes);
16635 /* Create the bitfield declaration. */
16636 decl = grokbitfield (identifier
16637 ? make_id_declarator (NULL_TREE,
16647 cp_declarator *declarator;
16649 tree asm_specification;
16650 int ctor_dtor_or_conv_p;
16652 /* Parse the declarator. */
16654 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16655 &ctor_dtor_or_conv_p,
16656 /*parenthesized_p=*/NULL,
16657 /*member_p=*/true);
16659 /* If something went wrong parsing the declarator, make sure
16660 that we at least consume some tokens. */
16661 if (declarator == cp_error_declarator)
16663 /* Skip to the end of the statement. */
16664 cp_parser_skip_to_end_of_statement (parser);
16665 /* If the next token is not a semicolon, that is
16666 probably because we just skipped over the body of
16667 a function. So, we consume a semicolon if
16668 present, but do not issue an error message if it
16670 if (cp_lexer_next_token_is (parser->lexer,
16672 cp_lexer_consume_token (parser->lexer);
16676 if (declares_class_or_enum & 2)
16677 cp_parser_check_for_definition_in_return_type
16678 (declarator, decl_specifiers.type,
16679 decl_specifiers.type_location);
16681 /* Look for an asm-specification. */
16682 asm_specification = cp_parser_asm_specification_opt (parser);
16683 /* Look for attributes that apply to the declaration. */
16684 attributes = cp_parser_attributes_opt (parser);
16685 /* Remember which attributes are prefix attributes and
16687 first_attribute = attributes;
16688 /* Combine the attributes. */
16689 attributes = chainon (prefix_attributes, attributes);
16691 /* If it's an `=', then we have a constant-initializer or a
16692 pure-specifier. It is not correct to parse the
16693 initializer before registering the member declaration
16694 since the member declaration should be in scope while
16695 its initializer is processed. However, the rest of the
16696 front end does not yet provide an interface that allows
16697 us to handle this correctly. */
16698 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16702 A pure-specifier shall be used only in the declaration of
16703 a virtual function.
16705 A member-declarator can contain a constant-initializer
16706 only if it declares a static member of integral or
16709 Therefore, if the DECLARATOR is for a function, we look
16710 for a pure-specifier; otherwise, we look for a
16711 constant-initializer. When we call `grokfield', it will
16712 perform more stringent semantics checks. */
16713 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16714 if (function_declarator_p (declarator))
16715 initializer = cp_parser_pure_specifier (parser);
16717 /* Parse the initializer. */
16718 initializer = cp_parser_constant_initializer (parser);
16720 /* Otherwise, there is no initializer. */
16722 initializer = NULL_TREE;
16724 /* See if we are probably looking at a function
16725 definition. We are certainly not looking at a
16726 member-declarator. Calling `grokfield' has
16727 side-effects, so we must not do it unless we are sure
16728 that we are looking at a member-declarator. */
16729 if (cp_parser_token_starts_function_definition_p
16730 (cp_lexer_peek_token (parser->lexer)))
16732 /* The grammar does not allow a pure-specifier to be
16733 used when a member function is defined. (It is
16734 possible that this fact is an oversight in the
16735 standard, since a pure function may be defined
16736 outside of the class-specifier. */
16738 error_at (initializer_token_start->location,
16739 "pure-specifier on function-definition");
16740 decl = cp_parser_save_member_function_body (parser,
16744 /* If the member was not a friend, declare it here. */
16746 finish_member_declaration (decl);
16747 /* Peek at the next token. */
16748 token = cp_lexer_peek_token (parser->lexer);
16749 /* If the next token is a semicolon, consume it. */
16750 if (token->type == CPP_SEMICOLON)
16751 cp_lexer_consume_token (parser->lexer);
16755 if (declarator->kind == cdk_function)
16756 declarator->id_loc = token->location;
16757 /* Create the declaration. */
16758 decl = grokfield (declarator, &decl_specifiers,
16759 initializer, /*init_const_expr_p=*/true,
16764 /* Reset PREFIX_ATTRIBUTES. */
16765 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16766 attributes = TREE_CHAIN (attributes);
16768 TREE_CHAIN (attributes) = NULL_TREE;
16770 /* If there is any qualification still in effect, clear it
16771 now; we will be starting fresh with the next declarator. */
16772 parser->scope = NULL_TREE;
16773 parser->qualifying_scope = NULL_TREE;
16774 parser->object_scope = NULL_TREE;
16775 /* If it's a `,', then there are more declarators. */
16776 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16777 cp_lexer_consume_token (parser->lexer);
16778 /* If the next token isn't a `;', then we have a parse error. */
16779 else if (cp_lexer_next_token_is_not (parser->lexer,
16782 cp_parser_error (parser, "expected %<;%>");
16783 /* Skip tokens until we find a `;'. */
16784 cp_parser_skip_to_end_of_statement (parser);
16791 /* Add DECL to the list of members. */
16793 finish_member_declaration (decl);
16795 if (TREE_CODE (decl) == FUNCTION_DECL)
16796 cp_parser_save_default_args (parser, decl);
16801 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16804 /* Parse a pure-specifier.
16809 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16810 Otherwise, ERROR_MARK_NODE is returned. */
16813 cp_parser_pure_specifier (cp_parser* parser)
16817 /* Look for the `=' token. */
16818 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16819 return error_mark_node;
16820 /* Look for the `0' token. */
16821 token = cp_lexer_peek_token (parser->lexer);
16823 if (token->type == CPP_EOF
16824 || token->type == CPP_PRAGMA_EOL)
16825 return error_mark_node;
16827 cp_lexer_consume_token (parser->lexer);
16829 /* Accept = default or = delete in c++0x mode. */
16830 if (token->keyword == RID_DEFAULT
16831 || token->keyword == RID_DELETE)
16833 maybe_warn_cpp0x ("defaulted and deleted functions");
16834 return token->u.value;
16837 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16838 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16840 cp_parser_error (parser,
16841 "invalid pure specifier (only %<= 0%> is allowed)");
16842 cp_parser_skip_to_end_of_statement (parser);
16843 return error_mark_node;
16845 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16847 error_at (token->location, "templates may not be %<virtual%>");
16848 return error_mark_node;
16851 return integer_zero_node;
16854 /* Parse a constant-initializer.
16856 constant-initializer:
16857 = constant-expression
16859 Returns a representation of the constant-expression. */
16862 cp_parser_constant_initializer (cp_parser* parser)
16864 /* Look for the `=' token. */
16865 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16866 return error_mark_node;
16868 /* It is invalid to write:
16870 struct S { static const int i = { 7 }; };
16873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16875 cp_parser_error (parser,
16876 "a brace-enclosed initializer is not allowed here");
16877 /* Consume the opening brace. */
16878 cp_lexer_consume_token (parser->lexer);
16879 /* Skip the initializer. */
16880 cp_parser_skip_to_closing_brace (parser);
16881 /* Look for the trailing `}'. */
16882 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16884 return error_mark_node;
16887 return cp_parser_constant_expression (parser,
16888 /*allow_non_constant=*/false,
16892 /* Derived classes [gram.class.derived] */
16894 /* Parse a base-clause.
16897 : base-specifier-list
16899 base-specifier-list:
16900 base-specifier ... [opt]
16901 base-specifier-list , base-specifier ... [opt]
16903 Returns a TREE_LIST representing the base-classes, in the order in
16904 which they were declared. The representation of each node is as
16905 described by cp_parser_base_specifier.
16907 In the case that no bases are specified, this function will return
16908 NULL_TREE, not ERROR_MARK_NODE. */
16911 cp_parser_base_clause (cp_parser* parser)
16913 tree bases = NULL_TREE;
16915 /* Look for the `:' that begins the list. */
16916 cp_parser_require (parser, CPP_COLON, "%<:%>");
16918 /* Scan the base-specifier-list. */
16923 bool pack_expansion_p = false;
16925 /* Look for the base-specifier. */
16926 base = cp_parser_base_specifier (parser);
16927 /* Look for the (optional) ellipsis. */
16928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16930 /* Consume the `...'. */
16931 cp_lexer_consume_token (parser->lexer);
16933 pack_expansion_p = true;
16936 /* Add BASE to the front of the list. */
16937 if (base != error_mark_node)
16939 if (pack_expansion_p)
16940 /* Make this a pack expansion type. */
16941 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16944 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16946 TREE_CHAIN (base) = bases;
16950 /* Peek at the next token. */
16951 token = cp_lexer_peek_token (parser->lexer);
16952 /* If it's not a comma, then the list is complete. */
16953 if (token->type != CPP_COMMA)
16955 /* Consume the `,'. */
16956 cp_lexer_consume_token (parser->lexer);
16959 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16960 base class had a qualified name. However, the next name that
16961 appears is certainly not qualified. */
16962 parser->scope = NULL_TREE;
16963 parser->qualifying_scope = NULL_TREE;
16964 parser->object_scope = NULL_TREE;
16966 return nreverse (bases);
16969 /* Parse a base-specifier.
16972 :: [opt] nested-name-specifier [opt] class-name
16973 virtual access-specifier [opt] :: [opt] nested-name-specifier
16975 access-specifier virtual [opt] :: [opt] nested-name-specifier
16978 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16979 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16980 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16981 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16984 cp_parser_base_specifier (cp_parser* parser)
16988 bool virtual_p = false;
16989 bool duplicate_virtual_error_issued_p = false;
16990 bool duplicate_access_error_issued_p = false;
16991 bool class_scope_p, template_p;
16992 tree access = access_default_node;
16995 /* Process the optional `virtual' and `access-specifier'. */
16998 /* Peek at the next token. */
16999 token = cp_lexer_peek_token (parser->lexer);
17000 /* Process `virtual'. */
17001 switch (token->keyword)
17004 /* If `virtual' appears more than once, issue an error. */
17005 if (virtual_p && !duplicate_virtual_error_issued_p)
17007 cp_parser_error (parser,
17008 "%<virtual%> specified more than once in base-specified");
17009 duplicate_virtual_error_issued_p = true;
17014 /* Consume the `virtual' token. */
17015 cp_lexer_consume_token (parser->lexer);
17020 case RID_PROTECTED:
17022 /* If more than one access specifier appears, issue an
17024 if (access != access_default_node
17025 && !duplicate_access_error_issued_p)
17027 cp_parser_error (parser,
17028 "more than one access specifier in base-specified");
17029 duplicate_access_error_issued_p = true;
17032 access = ridpointers[(int) token->keyword];
17034 /* Consume the access-specifier. */
17035 cp_lexer_consume_token (parser->lexer);
17044 /* It is not uncommon to see programs mechanically, erroneously, use
17045 the 'typename' keyword to denote (dependent) qualified types
17046 as base classes. */
17047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17049 token = cp_lexer_peek_token (parser->lexer);
17050 if (!processing_template_decl)
17051 error_at (token->location,
17052 "keyword %<typename%> not allowed outside of templates");
17054 error_at (token->location,
17055 "keyword %<typename%> not allowed in this context "
17056 "(the base class is implicitly a type)");
17057 cp_lexer_consume_token (parser->lexer);
17060 /* Look for the optional `::' operator. */
17061 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17062 /* Look for the nested-name-specifier. The simplest way to
17067 The keyword `typename' is not permitted in a base-specifier or
17068 mem-initializer; in these contexts a qualified name that
17069 depends on a template-parameter is implicitly assumed to be a
17072 is to pretend that we have seen the `typename' keyword at this
17074 cp_parser_nested_name_specifier_opt (parser,
17075 /*typename_keyword_p=*/true,
17076 /*check_dependency_p=*/true,
17078 /*is_declaration=*/true);
17079 /* If the base class is given by a qualified name, assume that names
17080 we see are type names or templates, as appropriate. */
17081 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17082 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17084 /* Finally, look for the class-name. */
17085 type = cp_parser_class_name (parser,
17089 /*check_dependency_p=*/true,
17090 /*class_head_p=*/false,
17091 /*is_declaration=*/true);
17093 if (type == error_mark_node)
17094 return error_mark_node;
17096 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17099 /* Exception handling [gram.exception] */
17101 /* Parse an (optional) exception-specification.
17103 exception-specification:
17104 throw ( type-id-list [opt] )
17106 Returns a TREE_LIST representing the exception-specification. The
17107 TREE_VALUE of each node is a type. */
17110 cp_parser_exception_specification_opt (cp_parser* parser)
17115 /* Peek at the next token. */
17116 token = cp_lexer_peek_token (parser->lexer);
17117 /* If it's not `throw', then there's no exception-specification. */
17118 if (!cp_parser_is_keyword (token, RID_THROW))
17121 /* Consume the `throw'. */
17122 cp_lexer_consume_token (parser->lexer);
17124 /* Look for the `('. */
17125 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17127 /* Peek at the next token. */
17128 token = cp_lexer_peek_token (parser->lexer);
17129 /* If it's not a `)', then there is a type-id-list. */
17130 if (token->type != CPP_CLOSE_PAREN)
17132 const char *saved_message;
17134 /* Types may not be defined in an exception-specification. */
17135 saved_message = parser->type_definition_forbidden_message;
17136 parser->type_definition_forbidden_message
17137 = "types may not be defined in an exception-specification";
17138 /* Parse the type-id-list. */
17139 type_id_list = cp_parser_type_id_list (parser);
17140 /* Restore the saved message. */
17141 parser->type_definition_forbidden_message = saved_message;
17144 type_id_list = empty_except_spec;
17146 /* Look for the `)'. */
17147 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17149 return type_id_list;
17152 /* Parse an (optional) type-id-list.
17156 type-id-list , type-id ... [opt]
17158 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17159 in the order that the types were presented. */
17162 cp_parser_type_id_list (cp_parser* parser)
17164 tree types = NULL_TREE;
17171 /* Get the next type-id. */
17172 type = cp_parser_type_id (parser);
17173 /* Parse the optional ellipsis. */
17174 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17176 /* Consume the `...'. */
17177 cp_lexer_consume_token (parser->lexer);
17179 /* Turn the type into a pack expansion expression. */
17180 type = make_pack_expansion (type);
17182 /* Add it to the list. */
17183 types = add_exception_specifier (types, type, /*complain=*/1);
17184 /* Peek at the next token. */
17185 token = cp_lexer_peek_token (parser->lexer);
17186 /* If it is not a `,', we are done. */
17187 if (token->type != CPP_COMMA)
17189 /* Consume the `,'. */
17190 cp_lexer_consume_token (parser->lexer);
17193 return nreverse (types);
17196 /* Parse a try-block.
17199 try compound-statement handler-seq */
17202 cp_parser_try_block (cp_parser* parser)
17206 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17207 try_block = begin_try_block ();
17208 cp_parser_compound_statement (parser, NULL, true);
17209 finish_try_block (try_block);
17210 cp_parser_handler_seq (parser);
17211 finish_handler_sequence (try_block);
17216 /* Parse a function-try-block.
17218 function-try-block:
17219 try ctor-initializer [opt] function-body handler-seq */
17222 cp_parser_function_try_block (cp_parser* parser)
17224 tree compound_stmt;
17226 bool ctor_initializer_p;
17228 /* Look for the `try' keyword. */
17229 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17231 /* Let the rest of the front end know where we are. */
17232 try_block = begin_function_try_block (&compound_stmt);
17233 /* Parse the function-body. */
17235 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17236 /* We're done with the `try' part. */
17237 finish_function_try_block (try_block);
17238 /* Parse the handlers. */
17239 cp_parser_handler_seq (parser);
17240 /* We're done with the handlers. */
17241 finish_function_handler_sequence (try_block, compound_stmt);
17243 return ctor_initializer_p;
17246 /* Parse a handler-seq.
17249 handler handler-seq [opt] */
17252 cp_parser_handler_seq (cp_parser* parser)
17258 /* Parse the handler. */
17259 cp_parser_handler (parser);
17260 /* Peek at the next token. */
17261 token = cp_lexer_peek_token (parser->lexer);
17262 /* If it's not `catch' then there are no more handlers. */
17263 if (!cp_parser_is_keyword (token, RID_CATCH))
17268 /* Parse a handler.
17271 catch ( exception-declaration ) compound-statement */
17274 cp_parser_handler (cp_parser* parser)
17279 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17280 handler = begin_handler ();
17281 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17282 declaration = cp_parser_exception_declaration (parser);
17283 finish_handler_parms (declaration, handler);
17284 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17285 cp_parser_compound_statement (parser, NULL, false);
17286 finish_handler (handler);
17289 /* Parse an exception-declaration.
17291 exception-declaration:
17292 type-specifier-seq declarator
17293 type-specifier-seq abstract-declarator
17297 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17298 ellipsis variant is used. */
17301 cp_parser_exception_declaration (cp_parser* parser)
17303 cp_decl_specifier_seq type_specifiers;
17304 cp_declarator *declarator;
17305 const char *saved_message;
17307 /* If it's an ellipsis, it's easy to handle. */
17308 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17310 /* Consume the `...' token. */
17311 cp_lexer_consume_token (parser->lexer);
17315 /* Types may not be defined in exception-declarations. */
17316 saved_message = parser->type_definition_forbidden_message;
17317 parser->type_definition_forbidden_message
17318 = "types may not be defined in exception-declarations";
17320 /* Parse the type-specifier-seq. */
17321 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17322 /*is_trailing_return=*/false,
17324 /* If it's a `)', then there is no declarator. */
17325 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17328 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17329 /*ctor_dtor_or_conv_p=*/NULL,
17330 /*parenthesized_p=*/NULL,
17331 /*member_p=*/false);
17333 /* Restore the saved message. */
17334 parser->type_definition_forbidden_message = saved_message;
17336 if (!type_specifiers.any_specifiers_p)
17337 return error_mark_node;
17339 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17342 /* Parse a throw-expression.
17345 throw assignment-expression [opt]
17347 Returns a THROW_EXPR representing the throw-expression. */
17350 cp_parser_throw_expression (cp_parser* parser)
17355 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17356 token = cp_lexer_peek_token (parser->lexer);
17357 /* Figure out whether or not there is an assignment-expression
17358 following the "throw" keyword. */
17359 if (token->type == CPP_COMMA
17360 || token->type == CPP_SEMICOLON
17361 || token->type == CPP_CLOSE_PAREN
17362 || token->type == CPP_CLOSE_SQUARE
17363 || token->type == CPP_CLOSE_BRACE
17364 || token->type == CPP_COLON)
17365 expression = NULL_TREE;
17367 expression = cp_parser_assignment_expression (parser,
17368 /*cast_p=*/false, NULL);
17370 return build_throw (expression);
17373 /* GNU Extensions */
17375 /* Parse an (optional) asm-specification.
17378 asm ( string-literal )
17380 If the asm-specification is present, returns a STRING_CST
17381 corresponding to the string-literal. Otherwise, returns
17385 cp_parser_asm_specification_opt (cp_parser* parser)
17388 tree asm_specification;
17390 /* Peek at the next token. */
17391 token = cp_lexer_peek_token (parser->lexer);
17392 /* If the next token isn't the `asm' keyword, then there's no
17393 asm-specification. */
17394 if (!cp_parser_is_keyword (token, RID_ASM))
17397 /* Consume the `asm' token. */
17398 cp_lexer_consume_token (parser->lexer);
17399 /* Look for the `('. */
17400 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17402 /* Look for the string-literal. */
17403 asm_specification = cp_parser_string_literal (parser, false, false);
17405 /* Look for the `)'. */
17406 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17408 return asm_specification;
17411 /* Parse an asm-operand-list.
17415 asm-operand-list , asm-operand
17418 string-literal ( expression )
17419 [ string-literal ] string-literal ( expression )
17421 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17422 each node is the expression. The TREE_PURPOSE is itself a
17423 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17424 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17425 is a STRING_CST for the string literal before the parenthesis. Returns
17426 ERROR_MARK_NODE if any of the operands are invalid. */
17429 cp_parser_asm_operand_list (cp_parser* parser)
17431 tree asm_operands = NULL_TREE;
17432 bool invalid_operands = false;
17436 tree string_literal;
17440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17442 /* Consume the `[' token. */
17443 cp_lexer_consume_token (parser->lexer);
17444 /* Read the operand name. */
17445 name = cp_parser_identifier (parser);
17446 if (name != error_mark_node)
17447 name = build_string (IDENTIFIER_LENGTH (name),
17448 IDENTIFIER_POINTER (name));
17449 /* Look for the closing `]'. */
17450 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17454 /* Look for the string-literal. */
17455 string_literal = cp_parser_string_literal (parser, false, false);
17457 /* Look for the `('. */
17458 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17459 /* Parse the expression. */
17460 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17461 /* Look for the `)'. */
17462 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17464 if (name == error_mark_node
17465 || string_literal == error_mark_node
17466 || expression == error_mark_node)
17467 invalid_operands = true;
17469 /* Add this operand to the list. */
17470 asm_operands = tree_cons (build_tree_list (name, string_literal),
17473 /* If the next token is not a `,', there are no more
17475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17477 /* Consume the `,'. */
17478 cp_lexer_consume_token (parser->lexer);
17481 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17484 /* Parse an asm-clobber-list.
17488 asm-clobber-list , string-literal
17490 Returns a TREE_LIST, indicating the clobbers in the order that they
17491 appeared. The TREE_VALUE of each node is a STRING_CST. */
17494 cp_parser_asm_clobber_list (cp_parser* parser)
17496 tree clobbers = NULL_TREE;
17500 tree string_literal;
17502 /* Look for the string literal. */
17503 string_literal = cp_parser_string_literal (parser, false, false);
17504 /* Add it to the list. */
17505 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17506 /* If the next token is not a `,', then the list is
17508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17510 /* Consume the `,' token. */
17511 cp_lexer_consume_token (parser->lexer);
17517 /* Parse an asm-label-list.
17521 asm-label-list , identifier
17523 Returns a TREE_LIST, indicating the labels in the order that they
17524 appeared. The TREE_VALUE of each node is a label. */
17527 cp_parser_asm_label_list (cp_parser* parser)
17529 tree labels = NULL_TREE;
17533 tree identifier, label, name;
17535 /* Look for the identifier. */
17536 identifier = cp_parser_identifier (parser);
17537 if (!error_operand_p (identifier))
17539 label = lookup_label (identifier);
17540 if (TREE_CODE (label) == LABEL_DECL)
17542 TREE_USED (label) = 1;
17543 check_goto (label);
17544 name = build_string (IDENTIFIER_LENGTH (identifier),
17545 IDENTIFIER_POINTER (identifier));
17546 labels = tree_cons (name, label, labels);
17549 /* If the next token is not a `,', then the list is
17551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17553 /* Consume the `,' token. */
17554 cp_lexer_consume_token (parser->lexer);
17557 return nreverse (labels);
17560 /* Parse an (optional) series of attributes.
17563 attributes attribute
17566 __attribute__ (( attribute-list [opt] ))
17568 The return value is as for cp_parser_attribute_list. */
17571 cp_parser_attributes_opt (cp_parser* parser)
17573 tree attributes = NULL_TREE;
17578 tree attribute_list;
17580 /* Peek at the next token. */
17581 token = cp_lexer_peek_token (parser->lexer);
17582 /* If it's not `__attribute__', then we're done. */
17583 if (token->keyword != RID_ATTRIBUTE)
17586 /* Consume the `__attribute__' keyword. */
17587 cp_lexer_consume_token (parser->lexer);
17588 /* Look for the two `(' tokens. */
17589 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17590 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17592 /* Peek at the next token. */
17593 token = cp_lexer_peek_token (parser->lexer);
17594 if (token->type != CPP_CLOSE_PAREN)
17595 /* Parse the attribute-list. */
17596 attribute_list = cp_parser_attribute_list (parser);
17598 /* If the next token is a `)', then there is no attribute
17600 attribute_list = NULL;
17602 /* Look for the two `)' tokens. */
17603 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17604 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17606 /* Add these new attributes to the list. */
17607 attributes = chainon (attributes, attribute_list);
17613 /* Parse an attribute-list.
17617 attribute-list , attribute
17621 identifier ( identifier )
17622 identifier ( identifier , expression-list )
17623 identifier ( expression-list )
17625 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17626 to an attribute. The TREE_PURPOSE of each node is the identifier
17627 indicating which attribute is in use. The TREE_VALUE represents
17628 the arguments, if any. */
17631 cp_parser_attribute_list (cp_parser* parser)
17633 tree attribute_list = NULL_TREE;
17634 bool save_translate_strings_p = parser->translate_strings_p;
17636 parser->translate_strings_p = false;
17643 /* Look for the identifier. We also allow keywords here; for
17644 example `__attribute__ ((const))' is legal. */
17645 token = cp_lexer_peek_token (parser->lexer);
17646 if (token->type == CPP_NAME
17647 || token->type == CPP_KEYWORD)
17649 tree arguments = NULL_TREE;
17651 /* Consume the token. */
17652 token = cp_lexer_consume_token (parser->lexer);
17654 /* Save away the identifier that indicates which attribute
17656 identifier = (token->type == CPP_KEYWORD)
17657 /* For keywords, use the canonical spelling, not the
17658 parsed identifier. */
17659 ? ridpointers[(int) token->keyword]
17662 attribute = build_tree_list (identifier, NULL_TREE);
17664 /* Peek at the next token. */
17665 token = cp_lexer_peek_token (parser->lexer);
17666 /* If it's an `(', then parse the attribute arguments. */
17667 if (token->type == CPP_OPEN_PAREN)
17670 vec = cp_parser_parenthesized_expression_list
17671 (parser, true, /*cast_p=*/false,
17672 /*allow_expansion_p=*/false,
17673 /*non_constant_p=*/NULL);
17675 arguments = error_mark_node;
17678 arguments = build_tree_list_vec (vec);
17679 release_tree_vector (vec);
17681 /* Save the arguments away. */
17682 TREE_VALUE (attribute) = arguments;
17685 if (arguments != error_mark_node)
17687 /* Add this attribute to the list. */
17688 TREE_CHAIN (attribute) = attribute_list;
17689 attribute_list = attribute;
17692 token = cp_lexer_peek_token (parser->lexer);
17694 /* Now, look for more attributes. If the next token isn't a
17695 `,', we're done. */
17696 if (token->type != CPP_COMMA)
17699 /* Consume the comma and keep going. */
17700 cp_lexer_consume_token (parser->lexer);
17702 parser->translate_strings_p = save_translate_strings_p;
17704 /* We built up the list in reverse order. */
17705 return nreverse (attribute_list);
17708 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17709 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17710 current value of the PEDANTIC flag, regardless of whether or not
17711 the `__extension__' keyword is present. The caller is responsible
17712 for restoring the value of the PEDANTIC flag. */
17715 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17717 /* Save the old value of the PEDANTIC flag. */
17718 *saved_pedantic = pedantic;
17720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17722 /* Consume the `__extension__' token. */
17723 cp_lexer_consume_token (parser->lexer);
17724 /* We're not being pedantic while the `__extension__' keyword is
17734 /* Parse a label declaration.
17737 __label__ label-declarator-seq ;
17739 label-declarator-seq:
17740 identifier , label-declarator-seq
17744 cp_parser_label_declaration (cp_parser* parser)
17746 /* Look for the `__label__' keyword. */
17747 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17753 /* Look for an identifier. */
17754 identifier = cp_parser_identifier (parser);
17755 /* If we failed, stop. */
17756 if (identifier == error_mark_node)
17758 /* Declare it as a label. */
17759 finish_label_decl (identifier);
17760 /* If the next token is a `;', stop. */
17761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17763 /* Look for the `,' separating the label declarations. */
17764 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17767 /* Look for the final `;'. */
17768 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17771 /* Support Functions */
17773 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17774 NAME should have one of the representations used for an
17775 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17776 is returned. If PARSER->SCOPE is a dependent type, then a
17777 SCOPE_REF is returned.
17779 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17780 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17781 was formed. Abstractly, such entities should not be passed to this
17782 function, because they do not need to be looked up, but it is
17783 simpler to check for this special case here, rather than at the
17786 In cases not explicitly covered above, this function returns a
17787 DECL, OVERLOAD, or baselink representing the result of the lookup.
17788 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17791 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17792 (e.g., "struct") that was used. In that case bindings that do not
17793 refer to types are ignored.
17795 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17798 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17801 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17804 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17805 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17806 NULL_TREE otherwise. */
17809 cp_parser_lookup_name (cp_parser *parser, tree name,
17810 enum tag_types tag_type,
17813 bool check_dependency,
17814 tree *ambiguous_decls,
17815 location_t name_location)
17819 tree object_type = parser->context->object_type;
17821 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17822 flags |= LOOKUP_COMPLAIN;
17824 /* Assume that the lookup will be unambiguous. */
17825 if (ambiguous_decls)
17826 *ambiguous_decls = NULL_TREE;
17828 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17829 no longer valid. Note that if we are parsing tentatively, and
17830 the parse fails, OBJECT_TYPE will be automatically restored. */
17831 parser->context->object_type = NULL_TREE;
17833 if (name == error_mark_node)
17834 return error_mark_node;
17836 /* A template-id has already been resolved; there is no lookup to
17838 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17840 if (BASELINK_P (name))
17842 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17843 == TEMPLATE_ID_EXPR);
17847 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17848 it should already have been checked to make sure that the name
17849 used matches the type being destroyed. */
17850 if (TREE_CODE (name) == BIT_NOT_EXPR)
17854 /* Figure out to which type this destructor applies. */
17856 type = parser->scope;
17857 else if (object_type)
17858 type = object_type;
17860 type = current_class_type;
17861 /* If that's not a class type, there is no destructor. */
17862 if (!type || !CLASS_TYPE_P (type))
17863 return error_mark_node;
17864 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17865 lazily_declare_fn (sfk_destructor, type);
17866 if (!CLASSTYPE_DESTRUCTORS (type))
17867 return error_mark_node;
17868 /* If it was a class type, return the destructor. */
17869 return CLASSTYPE_DESTRUCTORS (type);
17872 /* By this point, the NAME should be an ordinary identifier. If
17873 the id-expression was a qualified name, the qualifying scope is
17874 stored in PARSER->SCOPE at this point. */
17875 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17877 /* Perform the lookup. */
17882 if (parser->scope == error_mark_node)
17883 return error_mark_node;
17885 /* If the SCOPE is dependent, the lookup must be deferred until
17886 the template is instantiated -- unless we are explicitly
17887 looking up names in uninstantiated templates. Even then, we
17888 cannot look up the name if the scope is not a class type; it
17889 might, for example, be a template type parameter. */
17890 dependent_p = (TYPE_P (parser->scope)
17891 && dependent_scope_p (parser->scope));
17892 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17894 /* Defer lookup. */
17895 decl = error_mark_node;
17898 tree pushed_scope = NULL_TREE;
17900 /* If PARSER->SCOPE is a dependent type, then it must be a
17901 class type, and we must not be checking dependencies;
17902 otherwise, we would have processed this lookup above. So
17903 that PARSER->SCOPE is not considered a dependent base by
17904 lookup_member, we must enter the scope here. */
17906 pushed_scope = push_scope (parser->scope);
17907 /* If the PARSER->SCOPE is a template specialization, it
17908 may be instantiated during name lookup. In that case,
17909 errors may be issued. Even if we rollback the current
17910 tentative parse, those errors are valid. */
17911 decl = lookup_qualified_name (parser->scope, name,
17912 tag_type != none_type,
17913 /*complain=*/true);
17915 /* If we have a single function from a using decl, pull it out. */
17916 if (TREE_CODE (decl) == OVERLOAD
17917 && !really_overloaded_fn (decl))
17918 decl = OVL_FUNCTION (decl);
17921 pop_scope (pushed_scope);
17924 /* If the scope is a dependent type and either we deferred lookup or
17925 we did lookup but didn't find the name, rememeber the name. */
17926 if (decl == error_mark_node && TYPE_P (parser->scope)
17927 && dependent_type_p (parser->scope))
17933 /* The resolution to Core Issue 180 says that `struct
17934 A::B' should be considered a type-name, even if `A'
17936 type = make_typename_type (parser->scope, name, tag_type,
17937 /*complain=*/tf_error);
17938 decl = TYPE_NAME (type);
17940 else if (is_template
17941 && (cp_parser_next_token_ends_template_argument_p (parser)
17942 || cp_lexer_next_token_is (parser->lexer,
17944 decl = make_unbound_class_template (parser->scope,
17946 /*complain=*/tf_error);
17948 decl = build_qualified_name (/*type=*/NULL_TREE,
17949 parser->scope, name,
17952 parser->qualifying_scope = parser->scope;
17953 parser->object_scope = NULL_TREE;
17955 else if (object_type)
17957 tree object_decl = NULL_TREE;
17958 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17959 OBJECT_TYPE is not a class. */
17960 if (CLASS_TYPE_P (object_type))
17961 /* If the OBJECT_TYPE is a template specialization, it may
17962 be instantiated during name lookup. In that case, errors
17963 may be issued. Even if we rollback the current tentative
17964 parse, those errors are valid. */
17965 object_decl = lookup_member (object_type,
17968 tag_type != none_type);
17969 /* Look it up in the enclosing context, too. */
17970 decl = lookup_name_real (name, tag_type != none_type,
17972 /*block_p=*/true, is_namespace, flags);
17973 parser->object_scope = object_type;
17974 parser->qualifying_scope = NULL_TREE;
17976 decl = object_decl;
17980 decl = lookup_name_real (name, tag_type != none_type,
17982 /*block_p=*/true, is_namespace, flags);
17983 parser->qualifying_scope = NULL_TREE;
17984 parser->object_scope = NULL_TREE;
17987 /* If the lookup failed, let our caller know. */
17988 if (!decl || decl == error_mark_node)
17989 return error_mark_node;
17991 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17992 if (TREE_CODE (decl) == TREE_LIST)
17994 if (ambiguous_decls)
17995 *ambiguous_decls = decl;
17996 /* The error message we have to print is too complicated for
17997 cp_parser_error, so we incorporate its actions directly. */
17998 if (!cp_parser_simulate_error (parser))
18000 error_at (name_location, "reference to %qD is ambiguous",
18002 print_candidates (decl);
18004 return error_mark_node;
18007 gcc_assert (DECL_P (decl)
18008 || TREE_CODE (decl) == OVERLOAD
18009 || TREE_CODE (decl) == SCOPE_REF
18010 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18011 || BASELINK_P (decl));
18013 /* If we have resolved the name of a member declaration, check to
18014 see if the declaration is accessible. When the name resolves to
18015 set of overloaded functions, accessibility is checked when
18016 overload resolution is done.
18018 During an explicit instantiation, access is not checked at all,
18019 as per [temp.explicit]. */
18021 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18026 /* Like cp_parser_lookup_name, but for use in the typical case where
18027 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18028 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18031 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18033 return cp_parser_lookup_name (parser, name,
18035 /*is_template=*/false,
18036 /*is_namespace=*/false,
18037 /*check_dependency=*/true,
18038 /*ambiguous_decls=*/NULL,
18042 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18043 the current context, return the TYPE_DECL. If TAG_NAME_P is
18044 true, the DECL indicates the class being defined in a class-head,
18045 or declared in an elaborated-type-specifier.
18047 Otherwise, return DECL. */
18050 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18052 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18053 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18056 template <typename T> struct B;
18059 template <typename T> struct A::B {};
18061 Similarly, in an elaborated-type-specifier:
18063 namespace N { struct X{}; }
18066 template <typename T> friend struct N::X;
18069 However, if the DECL refers to a class type, and we are in
18070 the scope of the class, then the name lookup automatically
18071 finds the TYPE_DECL created by build_self_reference rather
18072 than a TEMPLATE_DECL. For example, in:
18074 template <class T> struct S {
18078 there is no need to handle such case. */
18080 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18081 return DECL_TEMPLATE_RESULT (decl);
18086 /* If too many, or too few, template-parameter lists apply to the
18087 declarator, issue an error message. Returns TRUE if all went well,
18088 and FALSE otherwise. */
18091 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18092 cp_declarator *declarator,
18093 location_t declarator_location)
18095 unsigned num_templates;
18097 /* We haven't seen any classes that involve template parameters yet. */
18100 switch (declarator->kind)
18103 if (declarator->u.id.qualifying_scope)
18108 scope = declarator->u.id.qualifying_scope;
18109 member = declarator->u.id.unqualified_name;
18111 while (scope && CLASS_TYPE_P (scope))
18113 /* You're supposed to have one `template <...>'
18114 for every template class, but you don't need one
18115 for a full specialization. For example:
18117 template <class T> struct S{};
18118 template <> struct S<int> { void f(); };
18119 void S<int>::f () {}
18121 is correct; there shouldn't be a `template <>' for
18122 the definition of `S<int>::f'. */
18123 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18124 /* If SCOPE does not have template information of any
18125 kind, then it is not a template, nor is it nested
18126 within a template. */
18128 if (explicit_class_specialization_p (scope))
18130 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18133 scope = TYPE_CONTEXT (scope);
18136 else if (TREE_CODE (declarator->u.id.unqualified_name)
18137 == TEMPLATE_ID_EXPR)
18138 /* If the DECLARATOR has the form `X<y>' then it uses one
18139 additional level of template parameters. */
18142 return cp_parser_check_template_parameters
18143 (parser, num_templates, declarator_location, declarator);
18149 case cdk_reference:
18151 return (cp_parser_check_declarator_template_parameters
18152 (parser, declarator->declarator, declarator_location));
18158 gcc_unreachable ();
18163 /* NUM_TEMPLATES were used in the current declaration. If that is
18164 invalid, return FALSE and issue an error messages. Otherwise,
18165 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18166 declarator and we can print more accurate diagnostics. */
18169 cp_parser_check_template_parameters (cp_parser* parser,
18170 unsigned num_templates,
18171 location_t location,
18172 cp_declarator *declarator)
18174 /* If there are the same number of template classes and parameter
18175 lists, that's OK. */
18176 if (parser->num_template_parameter_lists == num_templates)
18178 /* If there are more, but only one more, then we are referring to a
18179 member template. That's OK too. */
18180 if (parser->num_template_parameter_lists == num_templates + 1)
18182 /* If there are more template classes than parameter lists, we have
18185 template <class T> void S<T>::R<T>::f (); */
18186 if (parser->num_template_parameter_lists < num_templates)
18189 error_at (location, "specializing member %<%T::%E%> "
18190 "requires %<template<>%> syntax",
18191 declarator->u.id.qualifying_scope,
18192 declarator->u.id.unqualified_name);
18194 error_at (location, "too few template-parameter-lists");
18197 /* Otherwise, there are too many template parameter lists. We have
18200 template <class T> template <class U> void S::f(); */
18201 error_at (location, "too many template-parameter-lists");
18205 /* Parse an optional `::' token indicating that the following name is
18206 from the global namespace. If so, PARSER->SCOPE is set to the
18207 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18208 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18209 Returns the new value of PARSER->SCOPE, if the `::' token is
18210 present, and NULL_TREE otherwise. */
18213 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18217 /* Peek at the next token. */
18218 token = cp_lexer_peek_token (parser->lexer);
18219 /* If we're looking at a `::' token then we're starting from the
18220 global namespace, not our current location. */
18221 if (token->type == CPP_SCOPE)
18223 /* Consume the `::' token. */
18224 cp_lexer_consume_token (parser->lexer);
18225 /* Set the SCOPE so that we know where to start the lookup. */
18226 parser->scope = global_namespace;
18227 parser->qualifying_scope = global_namespace;
18228 parser->object_scope = NULL_TREE;
18230 return parser->scope;
18232 else if (!current_scope_valid_p)
18234 parser->scope = NULL_TREE;
18235 parser->qualifying_scope = NULL_TREE;
18236 parser->object_scope = NULL_TREE;
18242 /* Returns TRUE if the upcoming token sequence is the start of a
18243 constructor declarator. If FRIEND_P is true, the declarator is
18244 preceded by the `friend' specifier. */
18247 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18249 bool constructor_p;
18250 tree type_decl = NULL_TREE;
18251 bool nested_name_p;
18252 cp_token *next_token;
18254 /* The common case is that this is not a constructor declarator, so
18255 try to avoid doing lots of work if at all possible. It's not
18256 valid declare a constructor at function scope. */
18257 if (parser->in_function_body)
18259 /* And only certain tokens can begin a constructor declarator. */
18260 next_token = cp_lexer_peek_token (parser->lexer);
18261 if (next_token->type != CPP_NAME
18262 && next_token->type != CPP_SCOPE
18263 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18264 && next_token->type != CPP_TEMPLATE_ID)
18267 /* Parse tentatively; we are going to roll back all of the tokens
18269 cp_parser_parse_tentatively (parser);
18270 /* Assume that we are looking at a constructor declarator. */
18271 constructor_p = true;
18273 /* Look for the optional `::' operator. */
18274 cp_parser_global_scope_opt (parser,
18275 /*current_scope_valid_p=*/false);
18276 /* Look for the nested-name-specifier. */
18278 = (cp_parser_nested_name_specifier_opt (parser,
18279 /*typename_keyword_p=*/false,
18280 /*check_dependency_p=*/false,
18282 /*is_declaration=*/false)
18284 /* Outside of a class-specifier, there must be a
18285 nested-name-specifier. */
18286 if (!nested_name_p &&
18287 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18289 constructor_p = false;
18290 /* If we still think that this might be a constructor-declarator,
18291 look for a class-name. */
18296 template <typename T> struct S { S(); };
18297 template <typename T> S<T>::S ();
18299 we must recognize that the nested `S' names a class.
18302 template <typename T> S<T>::S<T> ();
18304 we must recognize that the nested `S' names a template. */
18305 type_decl = cp_parser_class_name (parser,
18306 /*typename_keyword_p=*/false,
18307 /*template_keyword_p=*/false,
18309 /*check_dependency_p=*/false,
18310 /*class_head_p=*/false,
18311 /*is_declaration=*/false);
18312 /* If there was no class-name, then this is not a constructor. */
18313 constructor_p = !cp_parser_error_occurred (parser);
18316 /* If we're still considering a constructor, we have to see a `(',
18317 to begin the parameter-declaration-clause, followed by either a
18318 `)', an `...', or a decl-specifier. We need to check for a
18319 type-specifier to avoid being fooled into thinking that:
18323 is a constructor. (It is actually a function named `f' that
18324 takes one parameter (of type `int') and returns a value of type
18327 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18330 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18331 /* A parameter declaration begins with a decl-specifier,
18332 which is either the "attribute" keyword, a storage class
18333 specifier, or (usually) a type-specifier. */
18334 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18337 tree pushed_scope = NULL_TREE;
18338 unsigned saved_num_template_parameter_lists;
18340 /* Names appearing in the type-specifier should be looked up
18341 in the scope of the class. */
18342 if (current_class_type)
18346 type = TREE_TYPE (type_decl);
18347 if (TREE_CODE (type) == TYPENAME_TYPE)
18349 type = resolve_typename_type (type,
18350 /*only_current_p=*/false);
18351 if (TREE_CODE (type) == TYPENAME_TYPE)
18353 cp_parser_abort_tentative_parse (parser);
18357 pushed_scope = push_scope (type);
18360 /* Inside the constructor parameter list, surrounding
18361 template-parameter-lists do not apply. */
18362 saved_num_template_parameter_lists
18363 = parser->num_template_parameter_lists;
18364 parser->num_template_parameter_lists = 0;
18366 /* Look for the type-specifier. */
18367 cp_parser_type_specifier (parser,
18368 CP_PARSER_FLAGS_NONE,
18369 /*decl_specs=*/NULL,
18370 /*is_declarator=*/true,
18371 /*declares_class_or_enum=*/NULL,
18372 /*is_cv_qualifier=*/NULL);
18374 parser->num_template_parameter_lists
18375 = saved_num_template_parameter_lists;
18377 /* Leave the scope of the class. */
18379 pop_scope (pushed_scope);
18381 constructor_p = !cp_parser_error_occurred (parser);
18385 constructor_p = false;
18386 /* We did not really want to consume any tokens. */
18387 cp_parser_abort_tentative_parse (parser);
18389 return constructor_p;
18392 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18393 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18394 they must be performed once we are in the scope of the function.
18396 Returns the function defined. */
18399 cp_parser_function_definition_from_specifiers_and_declarator
18400 (cp_parser* parser,
18401 cp_decl_specifier_seq *decl_specifiers,
18403 const cp_declarator *declarator)
18408 /* Begin the function-definition. */
18409 success_p = start_function (decl_specifiers, declarator, attributes);
18411 /* The things we're about to see are not directly qualified by any
18412 template headers we've seen thus far. */
18413 reset_specialization ();
18415 /* If there were names looked up in the decl-specifier-seq that we
18416 did not check, check them now. We must wait until we are in the
18417 scope of the function to perform the checks, since the function
18418 might be a friend. */
18419 perform_deferred_access_checks ();
18423 /* Skip the entire function. */
18424 cp_parser_skip_to_end_of_block_or_statement (parser);
18425 fn = error_mark_node;
18427 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18429 /* Seen already, skip it. An error message has already been output. */
18430 cp_parser_skip_to_end_of_block_or_statement (parser);
18431 fn = current_function_decl;
18432 current_function_decl = NULL_TREE;
18433 /* If this is a function from a class, pop the nested class. */
18434 if (current_class_name)
18435 pop_nested_class ();
18438 fn = cp_parser_function_definition_after_declarator (parser,
18439 /*inline_p=*/false);
18444 /* Parse the part of a function-definition that follows the
18445 declarator. INLINE_P is TRUE iff this function is an inline
18446 function defined within a class-specifier.
18448 Returns the function defined. */
18451 cp_parser_function_definition_after_declarator (cp_parser* parser,
18455 bool ctor_initializer_p = false;
18456 bool saved_in_unbraced_linkage_specification_p;
18457 bool saved_in_function_body;
18458 unsigned saved_num_template_parameter_lists;
18461 saved_in_function_body = parser->in_function_body;
18462 parser->in_function_body = true;
18463 /* If the next token is `return', then the code may be trying to
18464 make use of the "named return value" extension that G++ used to
18466 token = cp_lexer_peek_token (parser->lexer);
18467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18469 /* Consume the `return' keyword. */
18470 cp_lexer_consume_token (parser->lexer);
18471 /* Look for the identifier that indicates what value is to be
18473 cp_parser_identifier (parser);
18474 /* Issue an error message. */
18475 error_at (token->location,
18476 "named return values are no longer supported");
18477 /* Skip tokens until we reach the start of the function body. */
18480 cp_token *token = cp_lexer_peek_token (parser->lexer);
18481 if (token->type == CPP_OPEN_BRACE
18482 || token->type == CPP_EOF
18483 || token->type == CPP_PRAGMA_EOL)
18485 cp_lexer_consume_token (parser->lexer);
18488 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18489 anything declared inside `f'. */
18490 saved_in_unbraced_linkage_specification_p
18491 = parser->in_unbraced_linkage_specification_p;
18492 parser->in_unbraced_linkage_specification_p = false;
18493 /* Inside the function, surrounding template-parameter-lists do not
18495 saved_num_template_parameter_lists
18496 = parser->num_template_parameter_lists;
18497 parser->num_template_parameter_lists = 0;
18499 start_lambda_scope (current_function_decl);
18501 /* If the next token is `try', then we are looking at a
18502 function-try-block. */
18503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18504 ctor_initializer_p = cp_parser_function_try_block (parser);
18505 /* A function-try-block includes the function-body, so we only do
18506 this next part if we're not processing a function-try-block. */
18509 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18511 finish_lambda_scope ();
18513 /* Finish the function. */
18514 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18515 (inline_p ? 2 : 0));
18516 /* Generate code for it, if necessary. */
18517 expand_or_defer_fn (fn);
18518 /* Restore the saved values. */
18519 parser->in_unbraced_linkage_specification_p
18520 = saved_in_unbraced_linkage_specification_p;
18521 parser->num_template_parameter_lists
18522 = saved_num_template_parameter_lists;
18523 parser->in_function_body = saved_in_function_body;
18528 /* Parse a template-declaration, assuming that the `export' (and
18529 `extern') keywords, if present, has already been scanned. MEMBER_P
18530 is as for cp_parser_template_declaration. */
18533 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18535 tree decl = NULL_TREE;
18536 VEC (deferred_access_check,gc) *checks;
18537 tree parameter_list;
18538 bool friend_p = false;
18539 bool need_lang_pop;
18542 /* Look for the `template' keyword. */
18543 token = cp_lexer_peek_token (parser->lexer);
18544 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18548 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18550 if (at_class_scope_p () && current_function_decl)
18552 /* 14.5.2.2 [temp.mem]
18554 A local class shall not have member templates. */
18555 error_at (token->location,
18556 "invalid declaration of member template in local class");
18557 cp_parser_skip_to_end_of_block_or_statement (parser);
18562 A template ... shall not have C linkage. */
18563 if (current_lang_name == lang_name_c)
18565 error_at (token->location, "template with C linkage");
18566 /* Give it C++ linkage to avoid confusing other parts of the
18568 push_lang_context (lang_name_cplusplus);
18569 need_lang_pop = true;
18572 need_lang_pop = false;
18574 /* We cannot perform access checks on the template parameter
18575 declarations until we know what is being declared, just as we
18576 cannot check the decl-specifier list. */
18577 push_deferring_access_checks (dk_deferred);
18579 /* If the next token is `>', then we have an invalid
18580 specialization. Rather than complain about an invalid template
18581 parameter, issue an error message here. */
18582 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18584 cp_parser_error (parser, "invalid explicit specialization");
18585 begin_specialization ();
18586 parameter_list = NULL_TREE;
18589 /* Parse the template parameters. */
18590 parameter_list = cp_parser_template_parameter_list (parser);
18592 /* Get the deferred access checks from the parameter list. These
18593 will be checked once we know what is being declared, as for a
18594 member template the checks must be performed in the scope of the
18595 class containing the member. */
18596 checks = get_deferred_access_checks ();
18598 /* Look for the `>'. */
18599 cp_parser_skip_to_end_of_template_parameter_list (parser);
18600 /* We just processed one more parameter list. */
18601 ++parser->num_template_parameter_lists;
18602 /* If the next token is `template', there are more template
18604 if (cp_lexer_next_token_is_keyword (parser->lexer,
18606 cp_parser_template_declaration_after_export (parser, member_p);
18609 /* There are no access checks when parsing a template, as we do not
18610 know if a specialization will be a friend. */
18611 push_deferring_access_checks (dk_no_check);
18612 token = cp_lexer_peek_token (parser->lexer);
18613 decl = cp_parser_single_declaration (parser,
18616 /*explicit_specialization_p=*/false,
18618 pop_deferring_access_checks ();
18620 /* If this is a member template declaration, let the front
18622 if (member_p && !friend_p && decl)
18624 if (TREE_CODE (decl) == TYPE_DECL)
18625 cp_parser_check_access_in_redeclaration (decl, token->location);
18627 decl = finish_member_template_decl (decl);
18629 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18630 make_friend_class (current_class_type, TREE_TYPE (decl),
18631 /*complain=*/true);
18633 /* We are done with the current parameter list. */
18634 --parser->num_template_parameter_lists;
18636 pop_deferring_access_checks ();
18639 finish_template_decl (parameter_list);
18641 /* Register member declarations. */
18642 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18643 finish_member_declaration (decl);
18644 /* For the erroneous case of a template with C linkage, we pushed an
18645 implicit C++ linkage scope; exit that scope now. */
18647 pop_lang_context ();
18648 /* If DECL is a function template, we must return to parse it later.
18649 (Even though there is no definition, there might be default
18650 arguments that need handling.) */
18651 if (member_p && decl
18652 && (TREE_CODE (decl) == FUNCTION_DECL
18653 || DECL_FUNCTION_TEMPLATE_P (decl)))
18654 TREE_VALUE (parser->unparsed_functions_queues)
18655 = tree_cons (NULL_TREE, decl,
18656 TREE_VALUE (parser->unparsed_functions_queues));
18659 /* Perform the deferred access checks from a template-parameter-list.
18660 CHECKS is a TREE_LIST of access checks, as returned by
18661 get_deferred_access_checks. */
18664 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18666 ++processing_template_parmlist;
18667 perform_access_checks (checks);
18668 --processing_template_parmlist;
18671 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18672 `function-definition' sequence. MEMBER_P is true, this declaration
18673 appears in a class scope.
18675 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18676 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18679 cp_parser_single_declaration (cp_parser* parser,
18680 VEC (deferred_access_check,gc)* checks,
18682 bool explicit_specialization_p,
18685 int declares_class_or_enum;
18686 tree decl = NULL_TREE;
18687 cp_decl_specifier_seq decl_specifiers;
18688 bool function_definition_p = false;
18689 cp_token *decl_spec_token_start;
18691 /* This function is only used when processing a template
18693 gcc_assert (innermost_scope_kind () == sk_template_parms
18694 || innermost_scope_kind () == sk_template_spec);
18696 /* Defer access checks until we know what is being declared. */
18697 push_deferring_access_checks (dk_deferred);
18699 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18701 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18702 cp_parser_decl_specifier_seq (parser,
18703 CP_PARSER_FLAGS_OPTIONAL,
18705 &declares_class_or_enum);
18707 *friend_p = cp_parser_friend_p (&decl_specifiers);
18709 /* There are no template typedefs. */
18710 if (decl_specifiers.specs[(int) ds_typedef])
18712 error_at (decl_spec_token_start->location,
18713 "template declaration of %<typedef%>");
18714 decl = error_mark_node;
18717 /* Gather up the access checks that occurred the
18718 decl-specifier-seq. */
18719 stop_deferring_access_checks ();
18721 /* Check for the declaration of a template class. */
18722 if (declares_class_or_enum)
18724 if (cp_parser_declares_only_class_p (parser))
18726 decl = shadow_tag (&decl_specifiers);
18731 friend template <typename T> struct A<T>::B;
18734 A<T>::B will be represented by a TYPENAME_TYPE, and
18735 therefore not recognized by shadow_tag. */
18736 if (friend_p && *friend_p
18738 && decl_specifiers.type
18739 && TYPE_P (decl_specifiers.type))
18740 decl = decl_specifiers.type;
18742 if (decl && decl != error_mark_node)
18743 decl = TYPE_NAME (decl);
18745 decl = error_mark_node;
18747 /* Perform access checks for template parameters. */
18748 cp_parser_perform_template_parameter_access_checks (checks);
18751 /* If it's not a template class, try for a template function. If
18752 the next token is a `;', then this declaration does not declare
18753 anything. But, if there were errors in the decl-specifiers, then
18754 the error might well have come from an attempted class-specifier.
18755 In that case, there's no need to warn about a missing declarator. */
18757 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18758 || decl_specifiers.type != error_mark_node))
18760 decl = cp_parser_init_declarator (parser,
18763 /*function_definition_allowed_p=*/true,
18765 declares_class_or_enum,
18766 &function_definition_p);
18768 /* 7.1.1-1 [dcl.stc]
18770 A storage-class-specifier shall not be specified in an explicit
18771 specialization... */
18773 && explicit_specialization_p
18774 && decl_specifiers.storage_class != sc_none)
18776 error_at (decl_spec_token_start->location,
18777 "explicit template specialization cannot have a storage class");
18778 decl = error_mark_node;
18782 pop_deferring_access_checks ();
18784 /* Clear any current qualification; whatever comes next is the start
18785 of something new. */
18786 parser->scope = NULL_TREE;
18787 parser->qualifying_scope = NULL_TREE;
18788 parser->object_scope = NULL_TREE;
18789 /* Look for a trailing `;' after the declaration. */
18790 if (!function_definition_p
18791 && (decl == error_mark_node
18792 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18793 cp_parser_skip_to_end_of_block_or_statement (parser);
18798 /* Parse a cast-expression that is not the operand of a unary "&". */
18801 cp_parser_simple_cast_expression (cp_parser *parser)
18803 return cp_parser_cast_expression (parser, /*address_p=*/false,
18804 /*cast_p=*/false, NULL);
18807 /* Parse a functional cast to TYPE. Returns an expression
18808 representing the cast. */
18811 cp_parser_functional_cast (cp_parser* parser, tree type)
18814 tree expression_list;
18818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18820 maybe_warn_cpp0x ("extended initializer lists");
18821 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18822 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18823 if (TREE_CODE (type) == TYPE_DECL)
18824 type = TREE_TYPE (type);
18825 return finish_compound_literal (type, expression_list);
18829 vec = cp_parser_parenthesized_expression_list (parser, false,
18831 /*allow_expansion_p=*/true,
18832 /*non_constant_p=*/NULL);
18834 expression_list = error_mark_node;
18837 expression_list = build_tree_list_vec (vec);
18838 release_tree_vector (vec);
18841 cast = build_functional_cast (type, expression_list,
18842 tf_warning_or_error);
18843 /* [expr.const]/1: In an integral constant expression "only type
18844 conversions to integral or enumeration type can be used". */
18845 if (TREE_CODE (type) == TYPE_DECL)
18846 type = TREE_TYPE (type);
18847 if (cast != error_mark_node
18848 && !cast_valid_in_integral_constant_expression_p (type)
18849 && (cp_parser_non_integral_constant_expression
18850 (parser, "a call to a constructor")))
18851 return error_mark_node;
18855 /* Save the tokens that make up the body of a member function defined
18856 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18857 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18858 specifiers applied to the declaration. Returns the FUNCTION_DECL
18859 for the member function. */
18862 cp_parser_save_member_function_body (cp_parser* parser,
18863 cp_decl_specifier_seq *decl_specifiers,
18864 cp_declarator *declarator,
18871 /* Create the FUNCTION_DECL. */
18872 fn = grokmethod (decl_specifiers, declarator, attributes);
18873 /* If something went badly wrong, bail out now. */
18874 if (fn == error_mark_node)
18876 /* If there's a function-body, skip it. */
18877 if (cp_parser_token_starts_function_definition_p
18878 (cp_lexer_peek_token (parser->lexer)))
18879 cp_parser_skip_to_end_of_block_or_statement (parser);
18880 return error_mark_node;
18883 /* Remember it, if there default args to post process. */
18884 cp_parser_save_default_args (parser, fn);
18886 /* Save away the tokens that make up the body of the
18888 first = parser->lexer->next_token;
18889 /* We can have braced-init-list mem-initializers before the fn body. */
18890 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18892 cp_lexer_consume_token (parser->lexer);
18893 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18894 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18896 /* cache_group will stop after an un-nested { } pair, too. */
18897 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18900 /* variadic mem-inits have ... after the ')'. */
18901 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18902 cp_lexer_consume_token (parser->lexer);
18905 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18906 /* Handle function try blocks. */
18907 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18908 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18909 last = parser->lexer->next_token;
18911 /* Save away the inline definition; we will process it when the
18912 class is complete. */
18913 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18914 DECL_PENDING_INLINE_P (fn) = 1;
18916 /* We need to know that this was defined in the class, so that
18917 friend templates are handled correctly. */
18918 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18920 /* Add FN to the queue of functions to be parsed later. */
18921 TREE_VALUE (parser->unparsed_functions_queues)
18922 = tree_cons (NULL_TREE, fn,
18923 TREE_VALUE (parser->unparsed_functions_queues));
18928 /* Parse a template-argument-list, as well as the trailing ">" (but
18929 not the opening ">"). See cp_parser_template_argument_list for the
18933 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18937 tree saved_qualifying_scope;
18938 tree saved_object_scope;
18939 bool saved_greater_than_is_operator_p;
18940 int saved_unevaluated_operand;
18941 int saved_inhibit_evaluation_warnings;
18945 When parsing a template-id, the first non-nested `>' is taken as
18946 the end of the template-argument-list rather than a greater-than
18948 saved_greater_than_is_operator_p
18949 = parser->greater_than_is_operator_p;
18950 parser->greater_than_is_operator_p = false;
18951 /* Parsing the argument list may modify SCOPE, so we save it
18953 saved_scope = parser->scope;
18954 saved_qualifying_scope = parser->qualifying_scope;
18955 saved_object_scope = parser->object_scope;
18956 /* We need to evaluate the template arguments, even though this
18957 template-id may be nested within a "sizeof". */
18958 saved_unevaluated_operand = cp_unevaluated_operand;
18959 cp_unevaluated_operand = 0;
18960 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18961 c_inhibit_evaluation_warnings = 0;
18962 /* Parse the template-argument-list itself. */
18963 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18964 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18965 arguments = NULL_TREE;
18967 arguments = cp_parser_template_argument_list (parser);
18968 /* Look for the `>' that ends the template-argument-list. If we find
18969 a '>>' instead, it's probably just a typo. */
18970 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18972 if (cxx_dialect != cxx98)
18974 /* In C++0x, a `>>' in a template argument list or cast
18975 expression is considered to be two separate `>'
18976 tokens. So, change the current token to a `>', but don't
18977 consume it: it will be consumed later when the outer
18978 template argument list (or cast expression) is parsed.
18979 Note that this replacement of `>' for `>>' is necessary
18980 even if we are parsing tentatively: in the tentative
18981 case, after calling
18982 cp_parser_enclosed_template_argument_list we will always
18983 throw away all of the template arguments and the first
18984 closing `>', either because the template argument list
18985 was erroneous or because we are replacing those tokens
18986 with a CPP_TEMPLATE_ID token. The second `>' (which will
18987 not have been thrown away) is needed either to close an
18988 outer template argument list or to complete a new-style
18990 cp_token *token = cp_lexer_peek_token (parser->lexer);
18991 token->type = CPP_GREATER;
18993 else if (!saved_greater_than_is_operator_p)
18995 /* If we're in a nested template argument list, the '>>' has
18996 to be a typo for '> >'. We emit the error message, but we
18997 continue parsing and we push a '>' as next token, so that
18998 the argument list will be parsed correctly. Note that the
18999 global source location is still on the token before the
19000 '>>', so we need to say explicitly where we want it. */
19001 cp_token *token = cp_lexer_peek_token (parser->lexer);
19002 error_at (token->location, "%<>>%> should be %<> >%> "
19003 "within a nested template argument list");
19005 token->type = CPP_GREATER;
19009 /* If this is not a nested template argument list, the '>>'
19010 is a typo for '>'. Emit an error message and continue.
19011 Same deal about the token location, but here we can get it
19012 right by consuming the '>>' before issuing the diagnostic. */
19013 cp_token *token = cp_lexer_consume_token (parser->lexer);
19014 error_at (token->location,
19015 "spurious %<>>%>, use %<>%> to terminate "
19016 "a template argument list");
19020 cp_parser_skip_to_end_of_template_parameter_list (parser);
19021 /* The `>' token might be a greater-than operator again now. */
19022 parser->greater_than_is_operator_p
19023 = saved_greater_than_is_operator_p;
19024 /* Restore the SAVED_SCOPE. */
19025 parser->scope = saved_scope;
19026 parser->qualifying_scope = saved_qualifying_scope;
19027 parser->object_scope = saved_object_scope;
19028 cp_unevaluated_operand = saved_unevaluated_operand;
19029 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19034 /* MEMBER_FUNCTION is a member function, or a friend. If default
19035 arguments, or the body of the function have not yet been parsed,
19039 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19041 /* If this member is a template, get the underlying
19043 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19044 member_function = DECL_TEMPLATE_RESULT (member_function);
19046 /* There should not be any class definitions in progress at this
19047 point; the bodies of members are only parsed outside of all class
19049 gcc_assert (parser->num_classes_being_defined == 0);
19050 /* While we're parsing the member functions we might encounter more
19051 classes. We want to handle them right away, but we don't want
19052 them getting mixed up with functions that are currently in the
19054 parser->unparsed_functions_queues
19055 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19057 /* Make sure that any template parameters are in scope. */
19058 maybe_begin_member_template_processing (member_function);
19060 /* If the body of the function has not yet been parsed, parse it
19062 if (DECL_PENDING_INLINE_P (member_function))
19064 tree function_scope;
19065 cp_token_cache *tokens;
19067 /* The function is no longer pending; we are processing it. */
19068 tokens = DECL_PENDING_INLINE_INFO (member_function);
19069 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19070 DECL_PENDING_INLINE_P (member_function) = 0;
19072 /* If this is a local class, enter the scope of the containing
19074 function_scope = current_function_decl;
19075 if (function_scope)
19076 push_function_context ();
19078 /* Push the body of the function onto the lexer stack. */
19079 cp_parser_push_lexer_for_tokens (parser, tokens);
19081 /* Let the front end know that we going to be defining this
19083 start_preparsed_function (member_function, NULL_TREE,
19084 SF_PRE_PARSED | SF_INCLASS_INLINE);
19086 /* Don't do access checking if it is a templated function. */
19087 if (processing_template_decl)
19088 push_deferring_access_checks (dk_no_check);
19090 /* Now, parse the body of the function. */
19091 cp_parser_function_definition_after_declarator (parser,
19092 /*inline_p=*/true);
19094 if (processing_template_decl)
19095 pop_deferring_access_checks ();
19097 /* Leave the scope of the containing function. */
19098 if (function_scope)
19099 pop_function_context ();
19100 cp_parser_pop_lexer (parser);
19103 /* Remove any template parameters from the symbol table. */
19104 maybe_end_member_template_processing ();
19106 /* Restore the queue. */
19107 parser->unparsed_functions_queues
19108 = TREE_CHAIN (parser->unparsed_functions_queues);
19111 /* If DECL contains any default args, remember it on the unparsed
19112 functions queue. */
19115 cp_parser_save_default_args (cp_parser* parser, tree decl)
19119 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19121 probe = TREE_CHAIN (probe))
19122 if (TREE_PURPOSE (probe))
19124 TREE_PURPOSE (parser->unparsed_functions_queues)
19125 = tree_cons (current_class_type, decl,
19126 TREE_PURPOSE (parser->unparsed_functions_queues));
19131 /* FN is a FUNCTION_DECL which may contains a parameter with an
19132 unparsed DEFAULT_ARG. Parse the default args now. This function
19133 assumes that the current scope is the scope in which the default
19134 argument should be processed. */
19137 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19139 bool saved_local_variables_forbidden_p;
19140 tree parm, parmdecl;
19142 /* While we're parsing the default args, we might (due to the
19143 statement expression extension) encounter more classes. We want
19144 to handle them right away, but we don't want them getting mixed
19145 up with default args that are currently in the queue. */
19146 parser->unparsed_functions_queues
19147 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19149 /* Local variable names (and the `this' keyword) may not appear
19150 in a default argument. */
19151 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19152 parser->local_variables_forbidden_p = true;
19154 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19155 parmdecl = DECL_ARGUMENTS (fn);
19156 parm && parm != void_list_node;
19157 parm = TREE_CHAIN (parm),
19158 parmdecl = TREE_CHAIN (parmdecl))
19160 cp_token_cache *tokens;
19161 tree default_arg = TREE_PURPOSE (parm);
19163 VEC(tree,gc) *insts;
19170 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19171 /* This can happen for a friend declaration for a function
19172 already declared with default arguments. */
19175 /* Push the saved tokens for the default argument onto the parser's
19177 tokens = DEFARG_TOKENS (default_arg);
19178 cp_parser_push_lexer_for_tokens (parser, tokens);
19180 start_lambda_scope (parmdecl);
19182 /* Parse the assignment-expression. */
19183 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19184 if (parsed_arg == error_mark_node)
19186 cp_parser_pop_lexer (parser);
19190 if (!processing_template_decl)
19191 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19193 TREE_PURPOSE (parm) = parsed_arg;
19195 /* Update any instantiations we've already created. */
19196 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19197 VEC_iterate (tree, insts, ix, copy); ix++)
19198 TREE_PURPOSE (copy) = parsed_arg;
19200 finish_lambda_scope ();
19202 /* If the token stream has not been completely used up, then
19203 there was extra junk after the end of the default
19205 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19206 cp_parser_error (parser, "expected %<,%>");
19208 /* Revert to the main lexer. */
19209 cp_parser_pop_lexer (parser);
19212 /* Make sure no default arg is missing. */
19213 check_default_args (fn);
19215 /* Restore the state of local_variables_forbidden_p. */
19216 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19218 /* Restore the queue. */
19219 parser->unparsed_functions_queues
19220 = TREE_CHAIN (parser->unparsed_functions_queues);
19223 /* Parse the operand of `sizeof' (or a similar operator). Returns
19224 either a TYPE or an expression, depending on the form of the
19225 input. The KEYWORD indicates which kind of expression we have
19229 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19231 tree expr = NULL_TREE;
19232 const char *saved_message;
19234 bool saved_integral_constant_expression_p;
19235 bool saved_non_integral_constant_expression_p;
19236 bool pack_expansion_p = false;
19238 /* Types cannot be defined in a `sizeof' expression. Save away the
19240 saved_message = parser->type_definition_forbidden_message;
19241 /* And create the new one. */
19242 tmp = concat ("types may not be defined in %<",
19243 IDENTIFIER_POINTER (ridpointers[keyword]),
19244 "%> expressions", NULL);
19245 parser->type_definition_forbidden_message = tmp;
19247 /* The restrictions on constant-expressions do not apply inside
19248 sizeof expressions. */
19249 saved_integral_constant_expression_p
19250 = parser->integral_constant_expression_p;
19251 saved_non_integral_constant_expression_p
19252 = parser->non_integral_constant_expression_p;
19253 parser->integral_constant_expression_p = false;
19255 /* If it's a `...', then we are computing the length of a parameter
19257 if (keyword == RID_SIZEOF
19258 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19260 /* Consume the `...'. */
19261 cp_lexer_consume_token (parser->lexer);
19262 maybe_warn_variadic_templates ();
19264 /* Note that this is an expansion. */
19265 pack_expansion_p = true;
19268 /* Do not actually evaluate the expression. */
19269 ++cp_unevaluated_operand;
19270 ++c_inhibit_evaluation_warnings;
19271 /* If it's a `(', then we might be looking at the type-id
19273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19276 bool saved_in_type_id_in_expr_p;
19278 /* We can't be sure yet whether we're looking at a type-id or an
19280 cp_parser_parse_tentatively (parser);
19281 /* Consume the `('. */
19282 cp_lexer_consume_token (parser->lexer);
19283 /* Parse the type-id. */
19284 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19285 parser->in_type_id_in_expr_p = true;
19286 type = cp_parser_type_id (parser);
19287 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19288 /* Now, look for the trailing `)'. */
19289 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19290 /* If all went well, then we're done. */
19291 if (cp_parser_parse_definitely (parser))
19293 cp_decl_specifier_seq decl_specs;
19295 /* Build a trivial decl-specifier-seq. */
19296 clear_decl_specs (&decl_specs);
19297 decl_specs.type = type;
19299 /* Call grokdeclarator to figure out what type this is. */
19300 expr = grokdeclarator (NULL,
19304 /*attrlist=*/NULL);
19308 /* If the type-id production did not work out, then we must be
19309 looking at the unary-expression production. */
19311 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19312 /*cast_p=*/false, NULL);
19314 if (pack_expansion_p)
19315 /* Build a pack expansion. */
19316 expr = make_pack_expansion (expr);
19318 /* Go back to evaluating expressions. */
19319 --cp_unevaluated_operand;
19320 --c_inhibit_evaluation_warnings;
19322 /* Free the message we created. */
19324 /* And restore the old one. */
19325 parser->type_definition_forbidden_message = saved_message;
19326 parser->integral_constant_expression_p
19327 = saved_integral_constant_expression_p;
19328 parser->non_integral_constant_expression_p
19329 = saved_non_integral_constant_expression_p;
19334 /* If the current declaration has no declarator, return true. */
19337 cp_parser_declares_only_class_p (cp_parser *parser)
19339 /* If the next token is a `;' or a `,' then there is no
19341 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19342 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19345 /* Update the DECL_SPECS to reflect the storage class indicated by
19349 cp_parser_set_storage_class (cp_parser *parser,
19350 cp_decl_specifier_seq *decl_specs,
19352 location_t location)
19354 cp_storage_class storage_class;
19356 if (parser->in_unbraced_linkage_specification_p)
19358 error_at (location, "invalid use of %qD in linkage specification",
19359 ridpointers[keyword]);
19362 else if (decl_specs->storage_class != sc_none)
19364 decl_specs->conflicting_specifiers_p = true;
19368 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19369 && decl_specs->specs[(int) ds_thread])
19371 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19372 decl_specs->specs[(int) ds_thread] = 0;
19378 storage_class = sc_auto;
19381 storage_class = sc_register;
19384 storage_class = sc_static;
19387 storage_class = sc_extern;
19390 storage_class = sc_mutable;
19393 gcc_unreachable ();
19395 decl_specs->storage_class = storage_class;
19397 /* A storage class specifier cannot be applied alongside a typedef
19398 specifier. If there is a typedef specifier present then set
19399 conflicting_specifiers_p which will trigger an error later
19400 on in grokdeclarator. */
19401 if (decl_specs->specs[(int)ds_typedef])
19402 decl_specs->conflicting_specifiers_p = true;
19405 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19406 is true, the type is a user-defined type; otherwise it is a
19407 built-in type specified by a keyword. */
19410 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19412 location_t location,
19413 bool user_defined_p)
19415 decl_specs->any_specifiers_p = true;
19417 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19418 (with, for example, in "typedef int wchar_t;") we remember that
19419 this is what happened. In system headers, we ignore these
19420 declarations so that G++ can work with system headers that are not
19422 if (decl_specs->specs[(int) ds_typedef]
19424 && (type_spec == boolean_type_node
19425 || type_spec == char16_type_node
19426 || type_spec == char32_type_node
19427 || type_spec == wchar_type_node)
19428 && (decl_specs->type
19429 || decl_specs->specs[(int) ds_long]
19430 || decl_specs->specs[(int) ds_short]
19431 || decl_specs->specs[(int) ds_unsigned]
19432 || decl_specs->specs[(int) ds_signed]))
19434 decl_specs->redefined_builtin_type = type_spec;
19435 if (!decl_specs->type)
19437 decl_specs->type = type_spec;
19438 decl_specs->user_defined_type_p = false;
19439 decl_specs->type_location = location;
19442 else if (decl_specs->type)
19443 decl_specs->multiple_types_p = true;
19446 decl_specs->type = type_spec;
19447 decl_specs->user_defined_type_p = user_defined_p;
19448 decl_specs->redefined_builtin_type = NULL_TREE;
19449 decl_specs->type_location = location;
19453 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19454 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19457 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19459 return decl_specifiers->specs[(int) ds_friend] != 0;
19462 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19463 issue an error message indicating that TOKEN_DESC was expected.
19465 Returns the token consumed, if the token had the appropriate type.
19466 Otherwise, returns NULL. */
19469 cp_parser_require (cp_parser* parser,
19470 enum cpp_ttype type,
19471 const char* token_desc)
19473 if (cp_lexer_next_token_is (parser->lexer, type))
19474 return cp_lexer_consume_token (parser->lexer);
19477 /* Output the MESSAGE -- unless we're parsing tentatively. */
19478 if (!cp_parser_simulate_error (parser))
19480 char *message = concat ("expected ", token_desc, NULL);
19481 cp_parser_error (parser, message);
19488 /* An error message is produced if the next token is not '>'.
19489 All further tokens are skipped until the desired token is
19490 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19493 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19495 /* Current level of '< ... >'. */
19496 unsigned level = 0;
19497 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19498 unsigned nesting_depth = 0;
19500 /* Are we ready, yet? If not, issue error message. */
19501 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19504 /* Skip tokens until the desired token is found. */
19507 /* Peek at the next token. */
19508 switch (cp_lexer_peek_token (parser->lexer)->type)
19511 if (!nesting_depth)
19516 if (cxx_dialect == cxx98)
19517 /* C++0x views the `>>' operator as two `>' tokens, but
19520 else if (!nesting_depth && level-- == 0)
19522 /* We've hit a `>>' where the first `>' closes the
19523 template argument list, and the second `>' is
19524 spurious. Just consume the `>>' and stop; we've
19525 already produced at least one error. */
19526 cp_lexer_consume_token (parser->lexer);
19529 /* Fall through for C++0x, so we handle the second `>' in
19533 if (!nesting_depth && level-- == 0)
19535 /* We've reached the token we want, consume it and stop. */
19536 cp_lexer_consume_token (parser->lexer);
19541 case CPP_OPEN_PAREN:
19542 case CPP_OPEN_SQUARE:
19546 case CPP_CLOSE_PAREN:
19547 case CPP_CLOSE_SQUARE:
19548 if (nesting_depth-- == 0)
19553 case CPP_PRAGMA_EOL:
19554 case CPP_SEMICOLON:
19555 case CPP_OPEN_BRACE:
19556 case CPP_CLOSE_BRACE:
19557 /* The '>' was probably forgotten, don't look further. */
19564 /* Consume this token. */
19565 cp_lexer_consume_token (parser->lexer);
19569 /* If the next token is the indicated keyword, consume it. Otherwise,
19570 issue an error message indicating that TOKEN_DESC was expected.
19572 Returns the token consumed, if the token had the appropriate type.
19573 Otherwise, returns NULL. */
19576 cp_parser_require_keyword (cp_parser* parser,
19578 const char* token_desc)
19580 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19582 if (token && token->keyword != keyword)
19584 dyn_string_t error_msg;
19586 /* Format the error message. */
19587 error_msg = dyn_string_new (0);
19588 dyn_string_append_cstr (error_msg, "expected ");
19589 dyn_string_append_cstr (error_msg, token_desc);
19590 cp_parser_error (parser, error_msg->s);
19591 dyn_string_delete (error_msg);
19598 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19599 function-definition. */
19602 cp_parser_token_starts_function_definition_p (cp_token* token)
19604 return (/* An ordinary function-body begins with an `{'. */
19605 token->type == CPP_OPEN_BRACE
19606 /* A ctor-initializer begins with a `:'. */
19607 || token->type == CPP_COLON
19608 /* A function-try-block begins with `try'. */
19609 || token->keyword == RID_TRY
19610 /* The named return value extension begins with `return'. */
19611 || token->keyword == RID_RETURN);
19614 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19618 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19622 token = cp_lexer_peek_token (parser->lexer);
19623 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19626 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19627 C++0x) ending a template-argument. */
19630 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19634 token = cp_lexer_peek_token (parser->lexer);
19635 return (token->type == CPP_COMMA
19636 || token->type == CPP_GREATER
19637 || token->type == CPP_ELLIPSIS
19638 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19641 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19642 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19645 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19650 token = cp_lexer_peek_nth_token (parser->lexer, n);
19651 if (token->type == CPP_LESS)
19653 /* Check for the sequence `<::' in the original code. It would be lexed as
19654 `[:', where `[' is a digraph, and there is no whitespace before
19656 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19659 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19660 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19666 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19667 or none_type otherwise. */
19669 static enum tag_types
19670 cp_parser_token_is_class_key (cp_token* token)
19672 switch (token->keyword)
19677 return record_type;
19686 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19689 cp_parser_check_class_key (enum tag_types class_key, tree type)
19691 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19692 permerror (input_location, "%qs tag used in naming %q#T",
19693 class_key == union_type ? "union"
19694 : class_key == record_type ? "struct" : "class",
19698 /* Issue an error message if DECL is redeclared with different
19699 access than its original declaration [class.access.spec/3].
19700 This applies to nested classes and nested class templates.
19704 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19706 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19709 if ((TREE_PRIVATE (decl)
19710 != (current_access_specifier == access_private_node))
19711 || (TREE_PROTECTED (decl)
19712 != (current_access_specifier == access_protected_node)))
19713 error_at (location, "%qD redeclared with different access", decl);
19716 /* Look for the `template' keyword, as a syntactic disambiguator.
19717 Return TRUE iff it is present, in which case it will be
19721 cp_parser_optional_template_keyword (cp_parser *parser)
19723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19725 /* The `template' keyword can only be used within templates;
19726 outside templates the parser can always figure out what is a
19727 template and what is not. */
19728 if (!processing_template_decl)
19730 cp_token *token = cp_lexer_peek_token (parser->lexer);
19731 error_at (token->location,
19732 "%<template%> (as a disambiguator) is only allowed "
19733 "within templates");
19734 /* If this part of the token stream is rescanned, the same
19735 error message would be generated. So, we purge the token
19736 from the stream. */
19737 cp_lexer_purge_token (parser->lexer);
19742 /* Consume the `template' keyword. */
19743 cp_lexer_consume_token (parser->lexer);
19751 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19752 set PARSER->SCOPE, and perform other related actions. */
19755 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19758 struct tree_check *check_value;
19759 deferred_access_check *chk;
19760 VEC (deferred_access_check,gc) *checks;
19762 /* Get the stored value. */
19763 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19764 /* Perform any access checks that were deferred. */
19765 checks = check_value->checks;
19769 VEC_iterate (deferred_access_check, checks, i, chk) ;
19772 perform_or_defer_access_check (chk->binfo,
19777 /* Set the scope from the stored value. */
19778 parser->scope = check_value->value;
19779 parser->qualifying_scope = check_value->qualifying_scope;
19780 parser->object_scope = NULL_TREE;
19783 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19784 encounter the end of a block before what we were looking for. */
19787 cp_parser_cache_group (cp_parser *parser,
19788 enum cpp_ttype end,
19793 cp_token *token = cp_lexer_peek_token (parser->lexer);
19795 /* Abort a parenthesized expression if we encounter a semicolon. */
19796 if ((end == CPP_CLOSE_PAREN || depth == 0)
19797 && token->type == CPP_SEMICOLON)
19799 /* If we've reached the end of the file, stop. */
19800 if (token->type == CPP_EOF
19801 || (end != CPP_PRAGMA_EOL
19802 && token->type == CPP_PRAGMA_EOL))
19804 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19805 /* We've hit the end of an enclosing block, so there's been some
19806 kind of syntax error. */
19809 /* Consume the token. */
19810 cp_lexer_consume_token (parser->lexer);
19811 /* See if it starts a new group. */
19812 if (token->type == CPP_OPEN_BRACE)
19814 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19815 /* In theory this should probably check end == '}', but
19816 cp_parser_save_member_function_body needs it to exit
19817 after either '}' or ')' when called with ')'. */
19821 else if (token->type == CPP_OPEN_PAREN)
19823 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19824 if (depth == 0 && end == CPP_CLOSE_PAREN)
19827 else if (token->type == CPP_PRAGMA)
19828 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19829 else if (token->type == end)
19834 /* Begin parsing tentatively. We always save tokens while parsing
19835 tentatively so that if the tentative parsing fails we can restore the
19839 cp_parser_parse_tentatively (cp_parser* parser)
19841 /* Enter a new parsing context. */
19842 parser->context = cp_parser_context_new (parser->context);
19843 /* Begin saving tokens. */
19844 cp_lexer_save_tokens (parser->lexer);
19845 /* In order to avoid repetitive access control error messages,
19846 access checks are queued up until we are no longer parsing
19848 push_deferring_access_checks (dk_deferred);
19851 /* Commit to the currently active tentative parse. */
19854 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19856 cp_parser_context *context;
19859 /* Mark all of the levels as committed. */
19860 lexer = parser->lexer;
19861 for (context = parser->context; context->next; context = context->next)
19863 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19865 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19866 while (!cp_lexer_saving_tokens (lexer))
19867 lexer = lexer->next;
19868 cp_lexer_commit_tokens (lexer);
19872 /* Abort the currently active tentative parse. All consumed tokens
19873 will be rolled back, and no diagnostics will be issued. */
19876 cp_parser_abort_tentative_parse (cp_parser* parser)
19878 cp_parser_simulate_error (parser);
19879 /* Now, pretend that we want to see if the construct was
19880 successfully parsed. */
19881 cp_parser_parse_definitely (parser);
19884 /* Stop parsing tentatively. If a parse error has occurred, restore the
19885 token stream. Otherwise, commit to the tokens we have consumed.
19886 Returns true if no error occurred; false otherwise. */
19889 cp_parser_parse_definitely (cp_parser* parser)
19891 bool error_occurred;
19892 cp_parser_context *context;
19894 /* Remember whether or not an error occurred, since we are about to
19895 destroy that information. */
19896 error_occurred = cp_parser_error_occurred (parser);
19897 /* Remove the topmost context from the stack. */
19898 context = parser->context;
19899 parser->context = context->next;
19900 /* If no parse errors occurred, commit to the tentative parse. */
19901 if (!error_occurred)
19903 /* Commit to the tokens read tentatively, unless that was
19905 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19906 cp_lexer_commit_tokens (parser->lexer);
19908 pop_to_parent_deferring_access_checks ();
19910 /* Otherwise, if errors occurred, roll back our state so that things
19911 are just as they were before we began the tentative parse. */
19914 cp_lexer_rollback_tokens (parser->lexer);
19915 pop_deferring_access_checks ();
19917 /* Add the context to the front of the free list. */
19918 context->next = cp_parser_context_free_list;
19919 cp_parser_context_free_list = context;
19921 return !error_occurred;
19924 /* Returns true if we are parsing tentatively and are not committed to
19925 this tentative parse. */
19928 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19930 return (cp_parser_parsing_tentatively (parser)
19931 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19934 /* Returns nonzero iff an error has occurred during the most recent
19935 tentative parse. */
19938 cp_parser_error_occurred (cp_parser* parser)
19940 return (cp_parser_parsing_tentatively (parser)
19941 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19944 /* Returns nonzero if GNU extensions are allowed. */
19947 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19949 return parser->allow_gnu_extensions_p;
19952 /* Objective-C++ Productions */
19955 /* Parse an Objective-C expression, which feeds into a primary-expression
19959 objc-message-expression
19960 objc-string-literal
19961 objc-encode-expression
19962 objc-protocol-expression
19963 objc-selector-expression
19965 Returns a tree representation of the expression. */
19968 cp_parser_objc_expression (cp_parser* parser)
19970 /* Try to figure out what kind of declaration is present. */
19971 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19975 case CPP_OPEN_SQUARE:
19976 return cp_parser_objc_message_expression (parser);
19978 case CPP_OBJC_STRING:
19979 kwd = cp_lexer_consume_token (parser->lexer);
19980 return objc_build_string_object (kwd->u.value);
19983 switch (kwd->keyword)
19985 case RID_AT_ENCODE:
19986 return cp_parser_objc_encode_expression (parser);
19988 case RID_AT_PROTOCOL:
19989 return cp_parser_objc_protocol_expression (parser);
19991 case RID_AT_SELECTOR:
19992 return cp_parser_objc_selector_expression (parser);
19998 error_at (kwd->location,
19999 "misplaced %<@%D%> Objective-C++ construct",
20001 cp_parser_skip_to_end_of_block_or_statement (parser);
20004 return error_mark_node;
20007 /* Parse an Objective-C message expression.
20009 objc-message-expression:
20010 [ objc-message-receiver objc-message-args ]
20012 Returns a representation of an Objective-C message. */
20015 cp_parser_objc_message_expression (cp_parser* parser)
20017 tree receiver, messageargs;
20019 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20020 receiver = cp_parser_objc_message_receiver (parser);
20021 messageargs = cp_parser_objc_message_args (parser);
20022 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20024 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20027 /* Parse an objc-message-receiver.
20029 objc-message-receiver:
20031 simple-type-specifier
20033 Returns a representation of the type or expression. */
20036 cp_parser_objc_message_receiver (cp_parser* parser)
20040 /* An Objective-C message receiver may be either (1) a type
20041 or (2) an expression. */
20042 cp_parser_parse_tentatively (parser);
20043 rcv = cp_parser_expression (parser, false, NULL);
20045 if (cp_parser_parse_definitely (parser))
20048 rcv = cp_parser_simple_type_specifier (parser,
20049 /*decl_specs=*/NULL,
20050 CP_PARSER_FLAGS_NONE);
20052 return objc_get_class_reference (rcv);
20055 /* Parse the arguments and selectors comprising an Objective-C message.
20060 objc-selector-args , objc-comma-args
20062 objc-selector-args:
20063 objc-selector [opt] : assignment-expression
20064 objc-selector-args objc-selector [opt] : assignment-expression
20067 assignment-expression
20068 objc-comma-args , assignment-expression
20070 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20071 selector arguments and TREE_VALUE containing a list of comma
20075 cp_parser_objc_message_args (cp_parser* parser)
20077 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20078 bool maybe_unary_selector_p = true;
20079 cp_token *token = cp_lexer_peek_token (parser->lexer);
20081 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20083 tree selector = NULL_TREE, arg;
20085 if (token->type != CPP_COLON)
20086 selector = cp_parser_objc_selector (parser);
20088 /* Detect if we have a unary selector. */
20089 if (maybe_unary_selector_p
20090 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20091 return build_tree_list (selector, NULL_TREE);
20093 maybe_unary_selector_p = false;
20094 cp_parser_require (parser, CPP_COLON, "%<:%>");
20095 arg = cp_parser_assignment_expression (parser, false, NULL);
20098 = chainon (sel_args,
20099 build_tree_list (selector, arg));
20101 token = cp_lexer_peek_token (parser->lexer);
20104 /* Handle non-selector arguments, if any. */
20105 while (token->type == CPP_COMMA)
20109 cp_lexer_consume_token (parser->lexer);
20110 arg = cp_parser_assignment_expression (parser, false, NULL);
20113 = chainon (addl_args,
20114 build_tree_list (NULL_TREE, arg));
20116 token = cp_lexer_peek_token (parser->lexer);
20119 return build_tree_list (sel_args, addl_args);
20122 /* Parse an Objective-C encode expression.
20124 objc-encode-expression:
20125 @encode objc-typename
20127 Returns an encoded representation of the type argument. */
20130 cp_parser_objc_encode_expression (cp_parser* parser)
20135 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20136 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20137 token = cp_lexer_peek_token (parser->lexer);
20138 type = complete_type (cp_parser_type_id (parser));
20139 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20143 error_at (token->location,
20144 "%<@encode%> must specify a type as an argument");
20145 return error_mark_node;
20148 return objc_build_encode_expr (type);
20151 /* Parse an Objective-C @defs expression. */
20154 cp_parser_objc_defs_expression (cp_parser *parser)
20158 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20159 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20160 name = cp_parser_identifier (parser);
20161 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20163 return objc_get_class_ivars (name);
20166 /* Parse an Objective-C protocol expression.
20168 objc-protocol-expression:
20169 @protocol ( identifier )
20171 Returns a representation of the protocol expression. */
20174 cp_parser_objc_protocol_expression (cp_parser* parser)
20178 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20179 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20180 proto = cp_parser_identifier (parser);
20181 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20183 return objc_build_protocol_expr (proto);
20186 /* Parse an Objective-C selector expression.
20188 objc-selector-expression:
20189 @selector ( objc-method-signature )
20191 objc-method-signature:
20197 objc-selector-seq objc-selector :
20199 Returns a representation of the method selector. */
20202 cp_parser_objc_selector_expression (cp_parser* parser)
20204 tree sel_seq = NULL_TREE;
20205 bool maybe_unary_selector_p = true;
20207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20209 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20210 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20211 token = cp_lexer_peek_token (parser->lexer);
20213 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20214 || token->type == CPP_SCOPE)
20216 tree selector = NULL_TREE;
20218 if (token->type != CPP_COLON
20219 || token->type == CPP_SCOPE)
20220 selector = cp_parser_objc_selector (parser);
20222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20223 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20225 /* Detect if we have a unary selector. */
20226 if (maybe_unary_selector_p)
20228 sel_seq = selector;
20229 goto finish_selector;
20233 cp_parser_error (parser, "expected %<:%>");
20236 maybe_unary_selector_p = false;
20237 token = cp_lexer_consume_token (parser->lexer);
20239 if (token->type == CPP_SCOPE)
20242 = chainon (sel_seq,
20243 build_tree_list (selector, NULL_TREE));
20245 = chainon (sel_seq,
20246 build_tree_list (NULL_TREE, NULL_TREE));
20250 = chainon (sel_seq,
20251 build_tree_list (selector, NULL_TREE));
20253 token = cp_lexer_peek_token (parser->lexer);
20257 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20259 return objc_build_selector_expr (loc, sel_seq);
20262 /* Parse a list of identifiers.
20264 objc-identifier-list:
20266 objc-identifier-list , identifier
20268 Returns a TREE_LIST of identifier nodes. */
20271 cp_parser_objc_identifier_list (cp_parser* parser)
20273 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20274 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20276 while (sep->type == CPP_COMMA)
20278 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20279 list = chainon (list,
20280 build_tree_list (NULL_TREE,
20281 cp_parser_identifier (parser)));
20282 sep = cp_lexer_peek_token (parser->lexer);
20288 /* Parse an Objective-C alias declaration.
20290 objc-alias-declaration:
20291 @compatibility_alias identifier identifier ;
20293 This function registers the alias mapping with the Objective-C front end.
20294 It returns nothing. */
20297 cp_parser_objc_alias_declaration (cp_parser* parser)
20301 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20302 alias = cp_parser_identifier (parser);
20303 orig = cp_parser_identifier (parser);
20304 objc_declare_alias (alias, orig);
20305 cp_parser_consume_semicolon_at_end_of_statement (parser);
20308 /* Parse an Objective-C class forward-declaration.
20310 objc-class-declaration:
20311 @class objc-identifier-list ;
20313 The function registers the forward declarations with the Objective-C
20314 front end. It returns nothing. */
20317 cp_parser_objc_class_declaration (cp_parser* parser)
20319 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20320 objc_declare_class (cp_parser_objc_identifier_list (parser));
20321 cp_parser_consume_semicolon_at_end_of_statement (parser);
20324 /* Parse a list of Objective-C protocol references.
20326 objc-protocol-refs-opt:
20327 objc-protocol-refs [opt]
20329 objc-protocol-refs:
20330 < objc-identifier-list >
20332 Returns a TREE_LIST of identifiers, if any. */
20335 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20337 tree protorefs = NULL_TREE;
20339 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20341 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20342 protorefs = cp_parser_objc_identifier_list (parser);
20343 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20349 /* Parse a Objective-C visibility specification. */
20352 cp_parser_objc_visibility_spec (cp_parser* parser)
20354 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20356 switch (vis->keyword)
20358 case RID_AT_PRIVATE:
20359 objc_set_visibility (2);
20361 case RID_AT_PROTECTED:
20362 objc_set_visibility (0);
20364 case RID_AT_PUBLIC:
20365 objc_set_visibility (1);
20371 /* Eat '@private'/'@protected'/'@public'. */
20372 cp_lexer_consume_token (parser->lexer);
20375 /* Parse an Objective-C method type. */
20378 cp_parser_objc_method_type (cp_parser* parser)
20380 objc_set_method_type
20381 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20386 /* Parse an Objective-C protocol qualifier. */
20389 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20391 tree quals = NULL_TREE, node;
20392 cp_token *token = cp_lexer_peek_token (parser->lexer);
20394 node = token->u.value;
20396 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20397 && (node == ridpointers [(int) RID_IN]
20398 || node == ridpointers [(int) RID_OUT]
20399 || node == ridpointers [(int) RID_INOUT]
20400 || node == ridpointers [(int) RID_BYCOPY]
20401 || node == ridpointers [(int) RID_BYREF]
20402 || node == ridpointers [(int) RID_ONEWAY]))
20404 quals = tree_cons (NULL_TREE, node, quals);
20405 cp_lexer_consume_token (parser->lexer);
20406 token = cp_lexer_peek_token (parser->lexer);
20407 node = token->u.value;
20413 /* Parse an Objective-C typename. */
20416 cp_parser_objc_typename (cp_parser* parser)
20418 tree type_name = NULL_TREE;
20420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20422 tree proto_quals, cp_type = NULL_TREE;
20424 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20425 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20427 /* An ObjC type name may consist of just protocol qualifiers, in which
20428 case the type shall default to 'id'. */
20429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20430 cp_type = cp_parser_type_id (parser);
20432 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20433 type_name = build_tree_list (proto_quals, cp_type);
20439 /* Check to see if TYPE refers to an Objective-C selector name. */
20442 cp_parser_objc_selector_p (enum cpp_ttype type)
20444 return (type == CPP_NAME || type == CPP_KEYWORD
20445 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20446 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20447 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20448 || type == CPP_XOR || type == CPP_XOR_EQ);
20451 /* Parse an Objective-C selector. */
20454 cp_parser_objc_selector (cp_parser* parser)
20456 cp_token *token = cp_lexer_consume_token (parser->lexer);
20458 if (!cp_parser_objc_selector_p (token->type))
20460 error_at (token->location, "invalid Objective-C++ selector name");
20461 return error_mark_node;
20464 /* C++ operator names are allowed to appear in ObjC selectors. */
20465 switch (token->type)
20467 case CPP_AND_AND: return get_identifier ("and");
20468 case CPP_AND_EQ: return get_identifier ("and_eq");
20469 case CPP_AND: return get_identifier ("bitand");
20470 case CPP_OR: return get_identifier ("bitor");
20471 case CPP_COMPL: return get_identifier ("compl");
20472 case CPP_NOT: return get_identifier ("not");
20473 case CPP_NOT_EQ: return get_identifier ("not_eq");
20474 case CPP_OR_OR: return get_identifier ("or");
20475 case CPP_OR_EQ: return get_identifier ("or_eq");
20476 case CPP_XOR: return get_identifier ("xor");
20477 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20478 default: return token->u.value;
20482 /* Parse an Objective-C params list. */
20485 cp_parser_objc_method_keyword_params (cp_parser* parser)
20487 tree params = NULL_TREE;
20488 bool maybe_unary_selector_p = true;
20489 cp_token *token = cp_lexer_peek_token (parser->lexer);
20491 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20493 tree selector = NULL_TREE, type_name, identifier;
20495 if (token->type != CPP_COLON)
20496 selector = cp_parser_objc_selector (parser);
20498 /* Detect if we have a unary selector. */
20499 if (maybe_unary_selector_p
20500 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20503 maybe_unary_selector_p = false;
20504 cp_parser_require (parser, CPP_COLON, "%<:%>");
20505 type_name = cp_parser_objc_typename (parser);
20506 identifier = cp_parser_identifier (parser);
20510 objc_build_keyword_decl (selector,
20514 token = cp_lexer_peek_token (parser->lexer);
20520 /* Parse the non-keyword Objective-C params. */
20523 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20525 tree params = make_node (TREE_LIST);
20526 cp_token *token = cp_lexer_peek_token (parser->lexer);
20527 *ellipsisp = false; /* Initially, assume no ellipsis. */
20529 while (token->type == CPP_COMMA)
20531 cp_parameter_declarator *parmdecl;
20534 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20535 token = cp_lexer_peek_token (parser->lexer);
20537 if (token->type == CPP_ELLIPSIS)
20539 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20544 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20545 parm = grokdeclarator (parmdecl->declarator,
20546 &parmdecl->decl_specifiers,
20547 PARM, /*initialized=*/0,
20548 /*attrlist=*/NULL);
20550 chainon (params, build_tree_list (NULL_TREE, parm));
20551 token = cp_lexer_peek_token (parser->lexer);
20557 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20560 cp_parser_objc_interstitial_code (cp_parser* parser)
20562 cp_token *token = cp_lexer_peek_token (parser->lexer);
20564 /* If the next token is `extern' and the following token is a string
20565 literal, then we have a linkage specification. */
20566 if (token->keyword == RID_EXTERN
20567 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20568 cp_parser_linkage_specification (parser);
20569 /* Handle #pragma, if any. */
20570 else if (token->type == CPP_PRAGMA)
20571 cp_parser_pragma (parser, pragma_external);
20572 /* Allow stray semicolons. */
20573 else if (token->type == CPP_SEMICOLON)
20574 cp_lexer_consume_token (parser->lexer);
20575 /* Finally, try to parse a block-declaration, or a function-definition. */
20577 cp_parser_block_declaration (parser, /*statement_p=*/false);
20580 /* Parse a method signature. */
20583 cp_parser_objc_method_signature (cp_parser* parser)
20585 tree rettype, kwdparms, optparms;
20586 bool ellipsis = false;
20588 cp_parser_objc_method_type (parser);
20589 rettype = cp_parser_objc_typename (parser);
20590 kwdparms = cp_parser_objc_method_keyword_params (parser);
20591 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20593 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20596 /* Pars an Objective-C method prototype list. */
20599 cp_parser_objc_method_prototype_list (cp_parser* parser)
20601 cp_token *token = cp_lexer_peek_token (parser->lexer);
20603 while (token->keyword != RID_AT_END)
20605 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20607 objc_add_method_declaration
20608 (cp_parser_objc_method_signature (parser));
20609 cp_parser_consume_semicolon_at_end_of_statement (parser);
20612 /* Allow for interspersed non-ObjC++ code. */
20613 cp_parser_objc_interstitial_code (parser);
20615 token = cp_lexer_peek_token (parser->lexer);
20618 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20619 objc_finish_interface ();
20622 /* Parse an Objective-C method definition list. */
20625 cp_parser_objc_method_definition_list (cp_parser* parser)
20627 cp_token *token = cp_lexer_peek_token (parser->lexer);
20629 while (token->keyword != RID_AT_END)
20633 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20635 push_deferring_access_checks (dk_deferred);
20636 objc_start_method_definition
20637 (cp_parser_objc_method_signature (parser));
20639 /* For historical reasons, we accept an optional semicolon. */
20640 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20641 cp_lexer_consume_token (parser->lexer);
20643 perform_deferred_access_checks ();
20644 stop_deferring_access_checks ();
20645 meth = cp_parser_function_definition_after_declarator (parser,
20647 pop_deferring_access_checks ();
20648 objc_finish_method_definition (meth);
20651 /* Allow for interspersed non-ObjC++ code. */
20652 cp_parser_objc_interstitial_code (parser);
20654 token = cp_lexer_peek_token (parser->lexer);
20657 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20658 objc_finish_implementation ();
20661 /* Parse Objective-C ivars. */
20664 cp_parser_objc_class_ivars (cp_parser* parser)
20666 cp_token *token = cp_lexer_peek_token (parser->lexer);
20668 if (token->type != CPP_OPEN_BRACE)
20669 return; /* No ivars specified. */
20671 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20672 token = cp_lexer_peek_token (parser->lexer);
20674 while (token->type != CPP_CLOSE_BRACE)
20676 cp_decl_specifier_seq declspecs;
20677 int decl_class_or_enum_p;
20678 tree prefix_attributes;
20680 cp_parser_objc_visibility_spec (parser);
20682 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20685 cp_parser_decl_specifier_seq (parser,
20686 CP_PARSER_FLAGS_OPTIONAL,
20688 &decl_class_or_enum_p);
20689 prefix_attributes = declspecs.attributes;
20690 declspecs.attributes = NULL_TREE;
20692 /* Keep going until we hit the `;' at the end of the
20694 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20696 tree width = NULL_TREE, attributes, first_attribute, decl;
20697 cp_declarator *declarator = NULL;
20698 int ctor_dtor_or_conv_p;
20700 /* Check for a (possibly unnamed) bitfield declaration. */
20701 token = cp_lexer_peek_token (parser->lexer);
20702 if (token->type == CPP_COLON)
20705 if (token->type == CPP_NAME
20706 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20709 /* Get the name of the bitfield. */
20710 declarator = make_id_declarator (NULL_TREE,
20711 cp_parser_identifier (parser),
20715 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20716 /* Get the width of the bitfield. */
20718 = cp_parser_constant_expression (parser,
20719 /*allow_non_constant=*/false,
20724 /* Parse the declarator. */
20726 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20727 &ctor_dtor_or_conv_p,
20728 /*parenthesized_p=*/NULL,
20729 /*member_p=*/false);
20732 /* Look for attributes that apply to the ivar. */
20733 attributes = cp_parser_attributes_opt (parser);
20734 /* Remember which attributes are prefix attributes and
20736 first_attribute = attributes;
20737 /* Combine the attributes. */
20738 attributes = chainon (prefix_attributes, attributes);
20741 /* Create the bitfield declaration. */
20742 decl = grokbitfield (declarator, &declspecs,
20746 decl = grokfield (declarator, &declspecs,
20747 NULL_TREE, /*init_const_expr_p=*/false,
20748 NULL_TREE, attributes);
20750 /* Add the instance variable. */
20751 objc_add_instance_variable (decl);
20753 /* Reset PREFIX_ATTRIBUTES. */
20754 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20755 attributes = TREE_CHAIN (attributes);
20757 TREE_CHAIN (attributes) = NULL_TREE;
20759 token = cp_lexer_peek_token (parser->lexer);
20761 if (token->type == CPP_COMMA)
20763 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20769 cp_parser_consume_semicolon_at_end_of_statement (parser);
20770 token = cp_lexer_peek_token (parser->lexer);
20773 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20774 /* For historical reasons, we accept an optional semicolon. */
20775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20776 cp_lexer_consume_token (parser->lexer);
20779 /* Parse an Objective-C protocol declaration. */
20782 cp_parser_objc_protocol_declaration (cp_parser* parser)
20784 tree proto, protorefs;
20787 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20790 tok = cp_lexer_peek_token (parser->lexer);
20791 error_at (tok->location, "identifier expected after %<@protocol%>");
20795 /* See if we have a forward declaration or a definition. */
20796 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20798 /* Try a forward declaration first. */
20799 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20801 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20803 cp_parser_consume_semicolon_at_end_of_statement (parser);
20806 /* Ok, we got a full-fledged definition (or at least should). */
20809 proto = cp_parser_identifier (parser);
20810 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20811 objc_start_protocol (proto, protorefs);
20812 cp_parser_objc_method_prototype_list (parser);
20816 /* Parse an Objective-C superclass or category. */
20819 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20822 cp_token *next = cp_lexer_peek_token (parser->lexer);
20824 *super = *categ = NULL_TREE;
20825 if (next->type == CPP_COLON)
20827 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20828 *super = cp_parser_identifier (parser);
20830 else if (next->type == CPP_OPEN_PAREN)
20832 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20833 *categ = cp_parser_identifier (parser);
20834 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20838 /* Parse an Objective-C class interface. */
20841 cp_parser_objc_class_interface (cp_parser* parser)
20843 tree name, super, categ, protos;
20845 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20846 name = cp_parser_identifier (parser);
20847 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20848 protos = cp_parser_objc_protocol_refs_opt (parser);
20850 /* We have either a class or a category on our hands. */
20852 objc_start_category_interface (name, categ, protos);
20855 objc_start_class_interface (name, super, protos);
20856 /* Handle instance variable declarations, if any. */
20857 cp_parser_objc_class_ivars (parser);
20858 objc_continue_interface ();
20861 cp_parser_objc_method_prototype_list (parser);
20864 /* Parse an Objective-C class implementation. */
20867 cp_parser_objc_class_implementation (cp_parser* parser)
20869 tree name, super, categ;
20871 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20872 name = cp_parser_identifier (parser);
20873 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20875 /* We have either a class or a category on our hands. */
20877 objc_start_category_implementation (name, categ);
20880 objc_start_class_implementation (name, super);
20881 /* Handle instance variable declarations, if any. */
20882 cp_parser_objc_class_ivars (parser);
20883 objc_continue_implementation ();
20886 cp_parser_objc_method_definition_list (parser);
20889 /* Consume the @end token and finish off the implementation. */
20892 cp_parser_objc_end_implementation (cp_parser* parser)
20894 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20895 objc_finish_implementation ();
20898 /* Parse an Objective-C declaration. */
20901 cp_parser_objc_declaration (cp_parser* parser)
20903 /* Try to figure out what kind of declaration is present. */
20904 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20906 switch (kwd->keyword)
20909 cp_parser_objc_alias_declaration (parser);
20912 cp_parser_objc_class_declaration (parser);
20914 case RID_AT_PROTOCOL:
20915 cp_parser_objc_protocol_declaration (parser);
20917 case RID_AT_INTERFACE:
20918 cp_parser_objc_class_interface (parser);
20920 case RID_AT_IMPLEMENTATION:
20921 cp_parser_objc_class_implementation (parser);
20924 cp_parser_objc_end_implementation (parser);
20927 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20929 cp_parser_skip_to_end_of_block_or_statement (parser);
20933 /* Parse an Objective-C try-catch-finally statement.
20935 objc-try-catch-finally-stmt:
20936 @try compound-statement objc-catch-clause-seq [opt]
20937 objc-finally-clause [opt]
20939 objc-catch-clause-seq:
20940 objc-catch-clause objc-catch-clause-seq [opt]
20943 @catch ( exception-declaration ) compound-statement
20945 objc-finally-clause
20946 @finally compound-statement
20948 Returns NULL_TREE. */
20951 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20952 location_t location;
20955 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20956 location = cp_lexer_peek_token (parser->lexer)->location;
20957 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20958 node, lest it get absorbed into the surrounding block. */
20959 stmt = push_stmt_list ();
20960 cp_parser_compound_statement (parser, NULL, false);
20961 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20963 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20965 cp_parameter_declarator *parmdecl;
20968 cp_lexer_consume_token (parser->lexer);
20969 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20970 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20971 parm = grokdeclarator (parmdecl->declarator,
20972 &parmdecl->decl_specifiers,
20973 PARM, /*initialized=*/0,
20974 /*attrlist=*/NULL);
20975 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20976 objc_begin_catch_clause (parm);
20977 cp_parser_compound_statement (parser, NULL, false);
20978 objc_finish_catch_clause ();
20981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20983 cp_lexer_consume_token (parser->lexer);
20984 location = cp_lexer_peek_token (parser->lexer)->location;
20985 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20986 node, lest it get absorbed into the surrounding block. */
20987 stmt = push_stmt_list ();
20988 cp_parser_compound_statement (parser, NULL, false);
20989 objc_build_finally_clause (location, pop_stmt_list (stmt));
20992 return objc_finish_try_stmt ();
20995 /* Parse an Objective-C synchronized statement.
20997 objc-synchronized-stmt:
20998 @synchronized ( expression ) compound-statement
21000 Returns NULL_TREE. */
21003 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21004 location_t location;
21007 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21009 location = cp_lexer_peek_token (parser->lexer)->location;
21010 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21011 lock = cp_parser_expression (parser, false, NULL);
21012 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21014 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21015 node, lest it get absorbed into the surrounding block. */
21016 stmt = push_stmt_list ();
21017 cp_parser_compound_statement (parser, NULL, false);
21019 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21022 /* Parse an Objective-C throw statement.
21025 @throw assignment-expression [opt] ;
21027 Returns a constructed '@throw' statement. */
21030 cp_parser_objc_throw_statement (cp_parser *parser) {
21031 tree expr = NULL_TREE;
21032 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21034 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21037 expr = cp_parser_assignment_expression (parser, false, NULL);
21039 cp_parser_consume_semicolon_at_end_of_statement (parser);
21041 return objc_build_throw_stmt (loc, expr);
21044 /* Parse an Objective-C statement. */
21047 cp_parser_objc_statement (cp_parser * parser) {
21048 /* Try to figure out what kind of declaration is present. */
21049 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21051 switch (kwd->keyword)
21054 return cp_parser_objc_try_catch_finally_statement (parser);
21055 case RID_AT_SYNCHRONIZED:
21056 return cp_parser_objc_synchronized_statement (parser);
21058 return cp_parser_objc_throw_statement (parser);
21060 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21062 cp_parser_skip_to_end_of_block_or_statement (parser);
21065 return error_mark_node;
21068 /* OpenMP 2.5 parsing routines. */
21070 /* Returns name of the next clause.
21071 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21072 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21073 returned and the token is consumed. */
21075 static pragma_omp_clause
21076 cp_parser_omp_clause_name (cp_parser *parser)
21078 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21080 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21081 result = PRAGMA_OMP_CLAUSE_IF;
21082 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21083 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21084 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21085 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21086 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21089 const char *p = IDENTIFIER_POINTER (id);
21094 if (!strcmp ("collapse", p))
21095 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21096 else if (!strcmp ("copyin", p))
21097 result = PRAGMA_OMP_CLAUSE_COPYIN;
21098 else if (!strcmp ("copyprivate", p))
21099 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21102 if (!strcmp ("firstprivate", p))
21103 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21106 if (!strcmp ("lastprivate", p))
21107 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21110 if (!strcmp ("nowait", p))
21111 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21112 else if (!strcmp ("num_threads", p))
21113 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21116 if (!strcmp ("ordered", p))
21117 result = PRAGMA_OMP_CLAUSE_ORDERED;
21120 if (!strcmp ("reduction", p))
21121 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21124 if (!strcmp ("schedule", p))
21125 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21126 else if (!strcmp ("shared", p))
21127 result = PRAGMA_OMP_CLAUSE_SHARED;
21130 if (!strcmp ("untied", p))
21131 result = PRAGMA_OMP_CLAUSE_UNTIED;
21136 if (result != PRAGMA_OMP_CLAUSE_NONE)
21137 cp_lexer_consume_token (parser->lexer);
21142 /* Validate that a clause of the given type does not already exist. */
21145 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21146 const char *name, location_t location)
21150 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21151 if (OMP_CLAUSE_CODE (c) == code)
21153 error_at (location, "too many %qs clauses", name);
21161 variable-list , identifier
21163 In addition, we match a closing parenthesis. An opening parenthesis
21164 will have been consumed by the caller.
21166 If KIND is nonzero, create the appropriate node and install the decl
21167 in OMP_CLAUSE_DECL and add the node to the head of the list.
21169 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21170 return the list created. */
21173 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21181 token = cp_lexer_peek_token (parser->lexer);
21182 name = cp_parser_id_expression (parser, /*template_p=*/false,
21183 /*check_dependency_p=*/true,
21184 /*template_p=*/NULL,
21185 /*declarator_p=*/false,
21186 /*optional_p=*/false);
21187 if (name == error_mark_node)
21190 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21191 if (decl == error_mark_node)
21192 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21193 else if (kind != 0)
21195 tree u = build_omp_clause (token->location, kind);
21196 OMP_CLAUSE_DECL (u) = decl;
21197 OMP_CLAUSE_CHAIN (u) = list;
21201 list = tree_cons (decl, NULL_TREE, list);
21204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21206 cp_lexer_consume_token (parser->lexer);
21209 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21213 /* Try to resync to an unnested comma. Copied from
21214 cp_parser_parenthesized_expression_list. */
21216 ending = cp_parser_skip_to_closing_parenthesis (parser,
21217 /*recovering=*/true,
21219 /*consume_paren=*/true);
21227 /* Similarly, but expect leading and trailing parenthesis. This is a very
21228 common case for omp clauses. */
21231 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21233 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21234 return cp_parser_omp_var_list_no_open (parser, kind, list);
21239 collapse ( constant-expression ) */
21242 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21248 loc = cp_lexer_peek_token (parser->lexer)->location;
21249 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21252 num = cp_parser_constant_expression (parser, false, NULL);
21254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21255 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21256 /*or_comma=*/false,
21257 /*consume_paren=*/true);
21259 if (num == error_mark_node)
21261 num = fold_non_dependent_expr (num);
21262 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21263 || !host_integerp (num, 0)
21264 || (n = tree_low_cst (num, 0)) <= 0
21267 error_at (loc, "collapse argument needs positive constant integer expression");
21271 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21272 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21273 OMP_CLAUSE_CHAIN (c) = list;
21274 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21280 default ( shared | none ) */
21283 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21285 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21288 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21293 const char *p = IDENTIFIER_POINTER (id);
21298 if (strcmp ("none", p) != 0)
21300 kind = OMP_CLAUSE_DEFAULT_NONE;
21304 if (strcmp ("shared", p) != 0)
21306 kind = OMP_CLAUSE_DEFAULT_SHARED;
21313 cp_lexer_consume_token (parser->lexer);
21318 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21321 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21322 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21323 /*or_comma=*/false,
21324 /*consume_paren=*/true);
21326 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21329 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21330 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21331 OMP_CLAUSE_CHAIN (c) = list;
21332 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21338 if ( expression ) */
21341 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21348 t = cp_parser_condition (parser);
21350 if (t == error_mark_node
21351 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21353 /*or_comma=*/false,
21354 /*consume_paren=*/true);
21356 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21358 c = build_omp_clause (location, OMP_CLAUSE_IF);
21359 OMP_CLAUSE_IF_EXPR (c) = t;
21360 OMP_CLAUSE_CHAIN (c) = list;
21369 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21370 tree list, location_t location)
21374 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21376 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21377 OMP_CLAUSE_CHAIN (c) = list;
21382 num_threads ( expression ) */
21385 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21386 location_t location)
21390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21393 t = cp_parser_expression (parser, false, NULL);
21395 if (t == error_mark_node
21396 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21397 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21398 /*or_comma=*/false,
21399 /*consume_paren=*/true);
21401 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21402 "num_threads", location);
21404 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21405 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21406 OMP_CLAUSE_CHAIN (c) = list;
21415 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21416 tree list, location_t location)
21420 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21421 "ordered", location);
21423 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21424 OMP_CLAUSE_CHAIN (c) = list;
21429 reduction ( reduction-operator : variable-list )
21431 reduction-operator:
21432 One of: + * - & ^ | && || */
21435 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21437 enum tree_code code;
21440 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21443 switch (cp_lexer_peek_token (parser->lexer)->type)
21455 code = BIT_AND_EXPR;
21458 code = BIT_XOR_EXPR;
21461 code = BIT_IOR_EXPR;
21464 code = TRUTH_ANDIF_EXPR;
21467 code = TRUTH_ORIF_EXPR;
21470 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21471 "%<|%>, %<&&%>, or %<||%>");
21473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21474 /*or_comma=*/false,
21475 /*consume_paren=*/true);
21478 cp_lexer_consume_token (parser->lexer);
21480 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21483 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21484 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21485 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21491 schedule ( schedule-kind )
21492 schedule ( schedule-kind , expression )
21495 static | dynamic | guided | runtime | auto */
21498 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21505 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21507 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21509 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21510 const char *p = IDENTIFIER_POINTER (id);
21515 if (strcmp ("dynamic", p) != 0)
21517 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21521 if (strcmp ("guided", p) != 0)
21523 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21527 if (strcmp ("runtime", p) != 0)
21529 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21536 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21537 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21538 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21539 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21542 cp_lexer_consume_token (parser->lexer);
21544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21547 cp_lexer_consume_token (parser->lexer);
21549 token = cp_lexer_peek_token (parser->lexer);
21550 t = cp_parser_assignment_expression (parser, false, NULL);
21552 if (t == error_mark_node)
21554 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21555 error_at (token->location, "schedule %<runtime%> does not take "
21556 "a %<chunk_size%> parameter");
21557 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21558 error_at (token->location, "schedule %<auto%> does not take "
21559 "a %<chunk_size%> parameter");
21561 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21563 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21566 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21569 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21570 OMP_CLAUSE_CHAIN (c) = list;
21574 cp_parser_error (parser, "invalid schedule kind");
21576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21577 /*or_comma=*/false,
21578 /*consume_paren=*/true);
21586 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21587 tree list, location_t location)
21591 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21593 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21594 OMP_CLAUSE_CHAIN (c) = list;
21598 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21599 is a bitmask in MASK. Return the list of clauses found; the result
21600 of clause default goes in *pdefault. */
21603 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21604 const char *where, cp_token *pragma_tok)
21606 tree clauses = NULL;
21608 cp_token *token = NULL;
21610 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21612 pragma_omp_clause c_kind;
21613 const char *c_name;
21614 tree prev = clauses;
21616 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21617 cp_lexer_consume_token (parser->lexer);
21619 token = cp_lexer_peek_token (parser->lexer);
21620 c_kind = cp_parser_omp_clause_name (parser);
21625 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21626 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21628 c_name = "collapse";
21630 case PRAGMA_OMP_CLAUSE_COPYIN:
21631 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21634 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21635 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21637 c_name = "copyprivate";
21639 case PRAGMA_OMP_CLAUSE_DEFAULT:
21640 clauses = cp_parser_omp_clause_default (parser, clauses,
21642 c_name = "default";
21644 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21645 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21647 c_name = "firstprivate";
21649 case PRAGMA_OMP_CLAUSE_IF:
21650 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21653 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21654 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21656 c_name = "lastprivate";
21658 case PRAGMA_OMP_CLAUSE_NOWAIT:
21659 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21662 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21663 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21665 c_name = "num_threads";
21667 case PRAGMA_OMP_CLAUSE_ORDERED:
21668 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21670 c_name = "ordered";
21672 case PRAGMA_OMP_CLAUSE_PRIVATE:
21673 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21675 c_name = "private";
21677 case PRAGMA_OMP_CLAUSE_REDUCTION:
21678 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21679 c_name = "reduction";
21681 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21682 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21684 c_name = "schedule";
21686 case PRAGMA_OMP_CLAUSE_SHARED:
21687 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21691 case PRAGMA_OMP_CLAUSE_UNTIED:
21692 clauses = cp_parser_omp_clause_untied (parser, clauses,
21697 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21701 if (((mask >> c_kind) & 1) == 0)
21703 /* Remove the invalid clause(s) from the list to avoid
21704 confusing the rest of the compiler. */
21706 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21711 return finish_omp_clauses (clauses);
21718 In practice, we're also interested in adding the statement to an
21719 outer node. So it is convenient if we work around the fact that
21720 cp_parser_statement calls add_stmt. */
21723 cp_parser_begin_omp_structured_block (cp_parser *parser)
21725 unsigned save = parser->in_statement;
21727 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21728 This preserves the "not within loop or switch" style error messages
21729 for nonsense cases like
21735 if (parser->in_statement)
21736 parser->in_statement = IN_OMP_BLOCK;
21742 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21744 parser->in_statement = save;
21748 cp_parser_omp_structured_block (cp_parser *parser)
21750 tree stmt = begin_omp_structured_block ();
21751 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21753 cp_parser_statement (parser, NULL_TREE, false, NULL);
21755 cp_parser_end_omp_structured_block (parser, save);
21756 return finish_omp_structured_block (stmt);
21760 # pragma omp atomic new-line
21764 x binop= expr | x++ | ++x | x-- | --x
21766 +, *, -, /, &, ^, |, <<, >>
21768 where x is an lvalue expression with scalar type. */
21771 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21774 enum tree_code code;
21776 cp_parser_require_pragma_eol (parser, pragma_tok);
21778 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21779 /*cast_p=*/false, NULL);
21780 switch (TREE_CODE (lhs))
21785 case PREINCREMENT_EXPR:
21786 case POSTINCREMENT_EXPR:
21787 lhs = TREE_OPERAND (lhs, 0);
21789 rhs = integer_one_node;
21792 case PREDECREMENT_EXPR:
21793 case POSTDECREMENT_EXPR:
21794 lhs = TREE_OPERAND (lhs, 0);
21796 rhs = integer_one_node;
21800 switch (cp_lexer_peek_token (parser->lexer)->type)
21806 code = TRUNC_DIV_EXPR;
21814 case CPP_LSHIFT_EQ:
21815 code = LSHIFT_EXPR;
21817 case CPP_RSHIFT_EQ:
21818 code = RSHIFT_EXPR;
21821 code = BIT_AND_EXPR;
21824 code = BIT_IOR_EXPR;
21827 code = BIT_XOR_EXPR;
21830 cp_parser_error (parser,
21831 "invalid operator for %<#pragma omp atomic%>");
21834 cp_lexer_consume_token (parser->lexer);
21836 rhs = cp_parser_expression (parser, false, NULL);
21837 if (rhs == error_mark_node)
21841 finish_omp_atomic (code, lhs, rhs);
21842 cp_parser_consume_semicolon_at_end_of_statement (parser);
21846 cp_parser_skip_to_end_of_block_or_statement (parser);
21851 # pragma omp barrier new-line */
21854 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21856 cp_parser_require_pragma_eol (parser, pragma_tok);
21857 finish_omp_barrier ();
21861 # pragma omp critical [(name)] new-line
21862 structured-block */
21865 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21867 tree stmt, name = NULL;
21869 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21871 cp_lexer_consume_token (parser->lexer);
21873 name = cp_parser_identifier (parser);
21875 if (name == error_mark_node
21876 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21878 /*or_comma=*/false,
21879 /*consume_paren=*/true);
21880 if (name == error_mark_node)
21883 cp_parser_require_pragma_eol (parser, pragma_tok);
21885 stmt = cp_parser_omp_structured_block (parser);
21886 return c_finish_omp_critical (input_location, stmt, name);
21890 # pragma omp flush flush-vars[opt] new-line
21893 ( variable-list ) */
21896 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21899 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21900 cp_parser_require_pragma_eol (parser, pragma_tok);
21902 finish_omp_flush ();
21905 /* Helper function, to parse omp for increment expression. */
21908 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21910 tree cond = cp_parser_binary_expression (parser, false, true,
21911 PREC_NOT_OPERATOR, NULL);
21914 if (cond == error_mark_node
21915 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21917 cp_parser_skip_to_end_of_statement (parser);
21918 return error_mark_node;
21921 switch (TREE_CODE (cond))
21929 return error_mark_node;
21932 /* If decl is an iterator, preserve LHS and RHS of the relational
21933 expr until finish_omp_for. */
21935 && (type_dependent_expression_p (decl)
21936 || CLASS_TYPE_P (TREE_TYPE (decl))))
21939 return build_x_binary_op (TREE_CODE (cond),
21940 TREE_OPERAND (cond, 0), ERROR_MARK,
21941 TREE_OPERAND (cond, 1), ERROR_MARK,
21942 &overloaded_p, tf_warning_or_error);
21945 /* Helper function, to parse omp for increment expression. */
21948 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21950 cp_token *token = cp_lexer_peek_token (parser->lexer);
21956 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21958 op = (token->type == CPP_PLUS_PLUS
21959 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21960 cp_lexer_consume_token (parser->lexer);
21961 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21963 return error_mark_node;
21964 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21967 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21969 return error_mark_node;
21971 token = cp_lexer_peek_token (parser->lexer);
21972 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21974 op = (token->type == CPP_PLUS_PLUS
21975 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21976 cp_lexer_consume_token (parser->lexer);
21977 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21980 op = cp_parser_assignment_operator_opt (parser);
21981 if (op == ERROR_MARK)
21982 return error_mark_node;
21984 if (op != NOP_EXPR)
21986 rhs = cp_parser_assignment_expression (parser, false, NULL);
21987 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21988 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21991 lhs = cp_parser_binary_expression (parser, false, false,
21992 PREC_ADDITIVE_EXPRESSION, NULL);
21993 token = cp_lexer_peek_token (parser->lexer);
21994 decl_first = lhs == decl;
21997 if (token->type != CPP_PLUS
21998 && token->type != CPP_MINUS)
21999 return error_mark_node;
22003 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22004 cp_lexer_consume_token (parser->lexer);
22005 rhs = cp_parser_binary_expression (parser, false, false,
22006 PREC_ADDITIVE_EXPRESSION, NULL);
22007 token = cp_lexer_peek_token (parser->lexer);
22008 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22010 if (lhs == NULL_TREE)
22012 if (op == PLUS_EXPR)
22015 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22018 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22019 NULL, tf_warning_or_error);
22022 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22026 if (rhs != decl || op == MINUS_EXPR)
22027 return error_mark_node;
22028 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22031 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22033 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22036 /* Parse the restricted form of the for statement allowed by OpenMP. */
22039 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22041 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22042 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22043 tree this_pre_body, cl;
22044 location_t loc_first;
22045 bool collapse_err = false;
22046 int i, collapse = 1, nbraces = 0;
22048 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22049 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22050 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22052 gcc_assert (collapse >= 1);
22054 declv = make_tree_vec (collapse);
22055 initv = make_tree_vec (collapse);
22056 condv = make_tree_vec (collapse);
22057 incrv = make_tree_vec (collapse);
22059 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22061 for (i = 0; i < collapse; i++)
22063 int bracecount = 0;
22064 bool add_private_clause = false;
22067 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22069 cp_parser_error (parser, "for statement expected");
22072 loc = cp_lexer_consume_token (parser->lexer)->location;
22074 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22077 init = decl = real_decl = NULL;
22078 this_pre_body = push_stmt_list ();
22079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22081 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22085 integer-type var = lb
22086 random-access-iterator-type var = lb
22087 pointer-type var = lb
22089 cp_decl_specifier_seq type_specifiers;
22091 /* First, try to parse as an initialized declaration. See
22092 cp_parser_condition, from whence the bulk of this is copied. */
22094 cp_parser_parse_tentatively (parser);
22095 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22096 /*is_trailing_return=*/false,
22098 if (cp_parser_parse_definitely (parser))
22100 /* If parsing a type specifier seq succeeded, then this
22101 MUST be a initialized declaration. */
22102 tree asm_specification, attributes;
22103 cp_declarator *declarator;
22105 declarator = cp_parser_declarator (parser,
22106 CP_PARSER_DECLARATOR_NAMED,
22107 /*ctor_dtor_or_conv_p=*/NULL,
22108 /*parenthesized_p=*/NULL,
22109 /*member_p=*/false);
22110 attributes = cp_parser_attributes_opt (parser);
22111 asm_specification = cp_parser_asm_specification_opt (parser);
22113 if (declarator == cp_error_declarator)
22114 cp_parser_skip_to_end_of_statement (parser);
22118 tree pushed_scope, auto_node;
22120 decl = start_decl (declarator, &type_specifiers,
22121 SD_INITIALIZED, attributes,
22122 /*prefix_attributes=*/NULL_TREE,
22125 auto_node = type_uses_auto (TREE_TYPE (decl));
22126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22128 if (cp_lexer_next_token_is (parser->lexer,
22130 error ("parenthesized initialization is not allowed in "
22131 "OpenMP %<for%> loop");
22133 /* Trigger an error. */
22134 cp_parser_require (parser, CPP_EQ, "%<=%>");
22136 init = error_mark_node;
22137 cp_parser_skip_to_end_of_statement (parser);
22139 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22140 || type_dependent_expression_p (decl)
22143 bool is_direct_init, is_non_constant_init;
22145 init = cp_parser_initializer (parser,
22147 &is_non_constant_init);
22149 if (auto_node && describable_type (init))
22152 = do_auto_deduction (TREE_TYPE (decl), init,
22155 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22156 && !type_dependent_expression_p (decl))
22160 cp_finish_decl (decl, init, !is_non_constant_init,
22162 LOOKUP_ONLYCONVERTING);
22163 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22166 = tree_cons (NULL, this_pre_body, for_block);
22170 init = pop_stmt_list (this_pre_body);
22171 this_pre_body = NULL_TREE;
22176 cp_lexer_consume_token (parser->lexer);
22177 init = cp_parser_assignment_expression (parser, false, NULL);
22180 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22181 init = error_mark_node;
22183 cp_finish_decl (decl, NULL_TREE,
22184 /*init_const_expr_p=*/false,
22186 LOOKUP_ONLYCONVERTING);
22190 pop_scope (pushed_scope);
22196 /* If parsing a type specifier sequence failed, then
22197 this MUST be a simple expression. */
22198 cp_parser_parse_tentatively (parser);
22199 decl = cp_parser_primary_expression (parser, false, false,
22201 if (!cp_parser_error_occurred (parser)
22204 && CLASS_TYPE_P (TREE_TYPE (decl)))
22208 cp_parser_parse_definitely (parser);
22209 cp_parser_require (parser, CPP_EQ, "%<=%>");
22210 rhs = cp_parser_assignment_expression (parser, false, NULL);
22211 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22213 tf_warning_or_error));
22214 add_private_clause = true;
22219 cp_parser_abort_tentative_parse (parser);
22220 init = cp_parser_expression (parser, false, NULL);
22223 if (TREE_CODE (init) == MODIFY_EXPR
22224 || TREE_CODE (init) == MODOP_EXPR)
22225 real_decl = TREE_OPERAND (init, 0);
22230 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22233 this_pre_body = pop_stmt_list (this_pre_body);
22237 pre_body = push_stmt_list ();
22239 add_stmt (this_pre_body);
22240 pre_body = pop_stmt_list (pre_body);
22243 pre_body = this_pre_body;
22248 if (par_clauses != NULL && real_decl != NULL_TREE)
22251 for (c = par_clauses; *c ; )
22252 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22253 && OMP_CLAUSE_DECL (*c) == real_decl)
22255 error_at (loc, "iteration variable %qD"
22256 " should not be firstprivate", real_decl);
22257 *c = OMP_CLAUSE_CHAIN (*c);
22259 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22260 && OMP_CLAUSE_DECL (*c) == real_decl)
22262 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22263 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22264 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22265 OMP_CLAUSE_DECL (l) = real_decl;
22266 OMP_CLAUSE_CHAIN (l) = clauses;
22267 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22269 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22270 CP_OMP_CLAUSE_INFO (*c) = NULL;
22271 add_private_clause = false;
22275 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22276 && OMP_CLAUSE_DECL (*c) == real_decl)
22277 add_private_clause = false;
22278 c = &OMP_CLAUSE_CHAIN (*c);
22282 if (add_private_clause)
22285 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22287 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22288 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22289 && OMP_CLAUSE_DECL (c) == decl)
22291 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22292 && OMP_CLAUSE_DECL (c) == decl)
22293 error_at (loc, "iteration variable %qD "
22294 "should not be firstprivate",
22296 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22297 && OMP_CLAUSE_DECL (c) == decl)
22298 error_at (loc, "iteration variable %qD should not be reduction",
22303 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22304 OMP_CLAUSE_DECL (c) = decl;
22305 c = finish_omp_clauses (c);
22308 OMP_CLAUSE_CHAIN (c) = clauses;
22315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22316 cond = cp_parser_omp_for_cond (parser, decl);
22317 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22322 /* If decl is an iterator, preserve the operator on decl
22323 until finish_omp_for. */
22325 && (type_dependent_expression_p (decl)
22326 || CLASS_TYPE_P (TREE_TYPE (decl))))
22327 incr = cp_parser_omp_for_incr (parser, decl);
22329 incr = cp_parser_expression (parser, false, NULL);
22332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22334 /*or_comma=*/false,
22335 /*consume_paren=*/true);
22337 TREE_VEC_ELT (declv, i) = decl;
22338 TREE_VEC_ELT (initv, i) = init;
22339 TREE_VEC_ELT (condv, i) = cond;
22340 TREE_VEC_ELT (incrv, i) = incr;
22342 if (i == collapse - 1)
22345 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22346 in between the collapsed for loops to be still considered perfectly
22347 nested. Hopefully the final version clarifies this.
22348 For now handle (multiple) {'s and empty statements. */
22349 cp_parser_parse_tentatively (parser);
22352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22354 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22356 cp_lexer_consume_token (parser->lexer);
22359 else if (bracecount
22360 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22361 cp_lexer_consume_token (parser->lexer);
22364 loc = cp_lexer_peek_token (parser->lexer)->location;
22365 error_at (loc, "not enough collapsed for loops");
22366 collapse_err = true;
22367 cp_parser_abort_tentative_parse (parser);
22376 cp_parser_parse_definitely (parser);
22377 nbraces += bracecount;
22381 /* Note that we saved the original contents of this flag when we entered
22382 the structured block, and so we don't need to re-save it here. */
22383 parser->in_statement = IN_OMP_FOR;
22385 /* Note that the grammar doesn't call for a structured block here,
22386 though the loop as a whole is a structured block. */
22387 body = push_stmt_list ();
22388 cp_parser_statement (parser, NULL_TREE, false, NULL);
22389 body = pop_stmt_list (body);
22391 if (declv == NULL_TREE)
22394 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22395 pre_body, clauses);
22399 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22401 cp_lexer_consume_token (parser->lexer);
22404 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22405 cp_lexer_consume_token (parser->lexer);
22410 error_at (cp_lexer_peek_token (parser->lexer)->location,
22411 "collapsed loops not perfectly nested");
22413 collapse_err = true;
22414 cp_parser_statement_seq_opt (parser, NULL);
22415 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22421 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22422 for_block = TREE_CHAIN (for_block);
22429 #pragma omp for for-clause[optseq] new-line
22432 #define OMP_FOR_CLAUSE_MASK \
22433 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22434 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22435 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22436 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22437 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22438 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22439 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22440 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22443 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22445 tree clauses, sb, ret;
22448 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22449 "#pragma omp for", pragma_tok);
22451 sb = begin_omp_structured_block ();
22452 save = cp_parser_begin_omp_structured_block (parser);
22454 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22456 cp_parser_end_omp_structured_block (parser, save);
22457 add_stmt (finish_omp_structured_block (sb));
22463 # pragma omp master new-line
22464 structured-block */
22467 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22469 cp_parser_require_pragma_eol (parser, pragma_tok);
22470 return c_finish_omp_master (input_location,
22471 cp_parser_omp_structured_block (parser));
22475 # pragma omp ordered new-line
22476 structured-block */
22479 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22481 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22482 cp_parser_require_pragma_eol (parser, pragma_tok);
22483 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22489 { section-sequence }
22492 section-directive[opt] structured-block
22493 section-sequence section-directive structured-block */
22496 cp_parser_omp_sections_scope (cp_parser *parser)
22498 tree stmt, substmt;
22499 bool error_suppress = false;
22502 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22505 stmt = push_stmt_list ();
22507 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22511 substmt = begin_omp_structured_block ();
22512 save = cp_parser_begin_omp_structured_block (parser);
22516 cp_parser_statement (parser, NULL_TREE, false, NULL);
22518 tok = cp_lexer_peek_token (parser->lexer);
22519 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22521 if (tok->type == CPP_CLOSE_BRACE)
22523 if (tok->type == CPP_EOF)
22527 cp_parser_end_omp_structured_block (parser, save);
22528 substmt = finish_omp_structured_block (substmt);
22529 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22530 add_stmt (substmt);
22535 tok = cp_lexer_peek_token (parser->lexer);
22536 if (tok->type == CPP_CLOSE_BRACE)
22538 if (tok->type == CPP_EOF)
22541 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22543 cp_lexer_consume_token (parser->lexer);
22544 cp_parser_require_pragma_eol (parser, tok);
22545 error_suppress = false;
22547 else if (!error_suppress)
22549 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22550 error_suppress = true;
22553 substmt = cp_parser_omp_structured_block (parser);
22554 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22555 add_stmt (substmt);
22557 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22559 substmt = pop_stmt_list (stmt);
22561 stmt = make_node (OMP_SECTIONS);
22562 TREE_TYPE (stmt) = void_type_node;
22563 OMP_SECTIONS_BODY (stmt) = substmt;
22570 # pragma omp sections sections-clause[optseq] newline
22573 #define OMP_SECTIONS_CLAUSE_MASK \
22574 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22575 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22576 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22577 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22578 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22581 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22585 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22586 "#pragma omp sections", pragma_tok);
22588 ret = cp_parser_omp_sections_scope (parser);
22590 OMP_SECTIONS_CLAUSES (ret) = clauses;
22596 # pragma parallel parallel-clause new-line
22597 # pragma parallel for parallel-for-clause new-line
22598 # pragma parallel sections parallel-sections-clause new-line */
22600 #define OMP_PARALLEL_CLAUSE_MASK \
22601 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22602 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22603 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22604 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22605 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22606 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22607 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22608 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22611 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22613 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22614 const char *p_name = "#pragma omp parallel";
22615 tree stmt, clauses, par_clause, ws_clause, block;
22616 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22618 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22622 cp_lexer_consume_token (parser->lexer);
22623 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22624 p_name = "#pragma omp parallel for";
22625 mask |= OMP_FOR_CLAUSE_MASK;
22626 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22628 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22630 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22631 const char *p = IDENTIFIER_POINTER (id);
22632 if (strcmp (p, "sections") == 0)
22634 cp_lexer_consume_token (parser->lexer);
22635 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22636 p_name = "#pragma omp parallel sections";
22637 mask |= OMP_SECTIONS_CLAUSE_MASK;
22638 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22642 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22643 block = begin_omp_parallel ();
22644 save = cp_parser_begin_omp_structured_block (parser);
22648 case PRAGMA_OMP_PARALLEL:
22649 cp_parser_statement (parser, NULL_TREE, false, NULL);
22650 par_clause = clauses;
22653 case PRAGMA_OMP_PARALLEL_FOR:
22654 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22655 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22658 case PRAGMA_OMP_PARALLEL_SECTIONS:
22659 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22660 stmt = cp_parser_omp_sections_scope (parser);
22662 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22666 gcc_unreachable ();
22669 cp_parser_end_omp_structured_block (parser, save);
22670 stmt = finish_omp_parallel (par_clause, block);
22671 if (p_kind != PRAGMA_OMP_PARALLEL)
22672 OMP_PARALLEL_COMBINED (stmt) = 1;
22677 # pragma omp single single-clause[optseq] new-line
22678 structured-block */
22680 #define OMP_SINGLE_CLAUSE_MASK \
22681 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22682 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22683 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22684 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22687 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22689 tree stmt = make_node (OMP_SINGLE);
22690 TREE_TYPE (stmt) = void_type_node;
22692 OMP_SINGLE_CLAUSES (stmt)
22693 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22694 "#pragma omp single", pragma_tok);
22695 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22697 return add_stmt (stmt);
22701 # pragma omp task task-clause[optseq] new-line
22702 structured-block */
22704 #define OMP_TASK_CLAUSE_MASK \
22705 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22706 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22707 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22708 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22709 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22710 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22713 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22715 tree clauses, block;
22718 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22719 "#pragma omp task", pragma_tok);
22720 block = begin_omp_task ();
22721 save = cp_parser_begin_omp_structured_block (parser);
22722 cp_parser_statement (parser, NULL_TREE, false, NULL);
22723 cp_parser_end_omp_structured_block (parser, save);
22724 return finish_omp_task (clauses, block);
22728 # pragma omp taskwait new-line */
22731 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22733 cp_parser_require_pragma_eol (parser, pragma_tok);
22734 finish_omp_taskwait ();
22738 # pragma omp threadprivate (variable-list) */
22741 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22745 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22746 cp_parser_require_pragma_eol (parser, pragma_tok);
22748 finish_omp_threadprivate (vars);
22751 /* Main entry point to OpenMP statement pragmas. */
22754 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22758 switch (pragma_tok->pragma_kind)
22760 case PRAGMA_OMP_ATOMIC:
22761 cp_parser_omp_atomic (parser, pragma_tok);
22763 case PRAGMA_OMP_CRITICAL:
22764 stmt = cp_parser_omp_critical (parser, pragma_tok);
22766 case PRAGMA_OMP_FOR:
22767 stmt = cp_parser_omp_for (parser, pragma_tok);
22769 case PRAGMA_OMP_MASTER:
22770 stmt = cp_parser_omp_master (parser, pragma_tok);
22772 case PRAGMA_OMP_ORDERED:
22773 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22775 case PRAGMA_OMP_PARALLEL:
22776 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22778 case PRAGMA_OMP_SECTIONS:
22779 stmt = cp_parser_omp_sections (parser, pragma_tok);
22781 case PRAGMA_OMP_SINGLE:
22782 stmt = cp_parser_omp_single (parser, pragma_tok);
22784 case PRAGMA_OMP_TASK:
22785 stmt = cp_parser_omp_task (parser, pragma_tok);
22788 gcc_unreachable ();
22792 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22797 static GTY (()) cp_parser *the_parser;
22800 /* Special handling for the first token or line in the file. The first
22801 thing in the file might be #pragma GCC pch_preprocess, which loads a
22802 PCH file, which is a GC collection point. So we need to handle this
22803 first pragma without benefit of an existing lexer structure.
22805 Always returns one token to the caller in *FIRST_TOKEN. This is
22806 either the true first token of the file, or the first token after
22807 the initial pragma. */
22810 cp_parser_initial_pragma (cp_token *first_token)
22814 cp_lexer_get_preprocessor_token (NULL, first_token);
22815 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22818 cp_lexer_get_preprocessor_token (NULL, first_token);
22819 if (first_token->type == CPP_STRING)
22821 name = first_token->u.value;
22823 cp_lexer_get_preprocessor_token (NULL, first_token);
22824 if (first_token->type != CPP_PRAGMA_EOL)
22825 error_at (first_token->location,
22826 "junk at end of %<#pragma GCC pch_preprocess%>");
22829 error_at (first_token->location, "expected string literal");
22831 /* Skip to the end of the pragma. */
22832 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22833 cp_lexer_get_preprocessor_token (NULL, first_token);
22835 /* Now actually load the PCH file. */
22837 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22839 /* Read one more token to return to our caller. We have to do this
22840 after reading the PCH file in, since its pointers have to be
22842 cp_lexer_get_preprocessor_token (NULL, first_token);
22845 /* Normal parsing of a pragma token. Here we can (and must) use the
22849 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22851 cp_token *pragma_tok;
22854 pragma_tok = cp_lexer_consume_token (parser->lexer);
22855 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22856 parser->lexer->in_pragma = true;
22858 id = pragma_tok->pragma_kind;
22861 case PRAGMA_GCC_PCH_PREPROCESS:
22862 error_at (pragma_tok->location,
22863 "%<#pragma GCC pch_preprocess%> must be first");
22866 case PRAGMA_OMP_BARRIER:
22869 case pragma_compound:
22870 cp_parser_omp_barrier (parser, pragma_tok);
22873 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22874 "used in compound statements");
22881 case PRAGMA_OMP_FLUSH:
22884 case pragma_compound:
22885 cp_parser_omp_flush (parser, pragma_tok);
22888 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22889 "used in compound statements");
22896 case PRAGMA_OMP_TASKWAIT:
22899 case pragma_compound:
22900 cp_parser_omp_taskwait (parser, pragma_tok);
22903 error_at (pragma_tok->location,
22904 "%<#pragma omp taskwait%> may only be "
22905 "used in compound statements");
22912 case PRAGMA_OMP_THREADPRIVATE:
22913 cp_parser_omp_threadprivate (parser, pragma_tok);
22916 case PRAGMA_OMP_ATOMIC:
22917 case PRAGMA_OMP_CRITICAL:
22918 case PRAGMA_OMP_FOR:
22919 case PRAGMA_OMP_MASTER:
22920 case PRAGMA_OMP_ORDERED:
22921 case PRAGMA_OMP_PARALLEL:
22922 case PRAGMA_OMP_SECTIONS:
22923 case PRAGMA_OMP_SINGLE:
22924 case PRAGMA_OMP_TASK:
22925 if (context == pragma_external)
22927 cp_parser_omp_construct (parser, pragma_tok);
22930 case PRAGMA_OMP_SECTION:
22931 error_at (pragma_tok->location,
22932 "%<#pragma omp section%> may only be used in "
22933 "%<#pragma omp sections%> construct");
22937 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22938 c_invoke_pragma_handler (id);
22942 cp_parser_error (parser, "expected declaration specifiers");
22946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22950 /* The interface the pragma parsers have to the lexer. */
22953 pragma_lex (tree *value)
22956 enum cpp_ttype ret;
22958 tok = cp_lexer_peek_token (the_parser->lexer);
22961 *value = tok->u.value;
22963 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22965 else if (ret == CPP_STRING)
22966 *value = cp_parser_string_literal (the_parser, false, false);
22969 cp_lexer_consume_token (the_parser->lexer);
22970 if (ret == CPP_KEYWORD)
22978 /* External interface. */
22980 /* Parse one entire translation unit. */
22983 c_parse_file (void)
22985 bool error_occurred;
22986 static bool already_called = false;
22988 if (already_called)
22990 sorry ("inter-module optimizations not implemented for C++");
22993 already_called = true;
22995 the_parser = cp_parser_new ();
22996 push_deferring_access_checks (flag_access_control
22997 ? dk_no_deferred : dk_no_check);
22998 error_occurred = cp_parser_translation_unit (the_parser);
23002 #include "gt-cp-parser.h"