2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
543 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
605 if (token == lexer->last_token)
611 if (token->type != CPP_PURGED)
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
678 if (tok == lexer->last_token)
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
796 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
804 /* Start emitting debugging information. */
807 cp_lexer_start_debugging (cp_lexer* lexer)
809 lexer->debugging_p = true;
812 /* Stop emitting debugging information. */
815 cp_lexer_stop_debugging (cp_lexer* lexer)
817 lexer->debugging_p = false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
827 cp_token_cache *cache = GGC_NEW (cp_token_cache);
828 cache->first = first;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator *make_call_declarator
852 (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854 (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856 (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858 (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860 (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862 (cp_cv_quals, tree, cp_declarator *);
864 /* An erroneous declarator. */
865 static cp_declarator *cp_error_declarator;
867 /* The obstack on which declarators and related data structures are
869 static struct obstack declarator_obstack;
871 /* Alloc BYTES from the declarator memory pool. */
874 alloc_declarator (size_t bytes)
876 return obstack_alloc (&declarator_obstack, bytes);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
885 cp_declarator *declarator;
887 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888 declarator->kind = kind;
889 declarator->attributes = NULL_TREE;
890 declarator->declarator = NULL;
891 declarator->parameter_pack_p = false;
896 /* Make a declarator for a generalized identifier. If
897 QUALIFYING_SCOPE is non-NULL, the identifier is
898 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904 special_function_kind sfk)
906 cp_declarator *declarator;
908 /* It is valid to write:
910 class C { void f(); };
914 The standard is not clear about whether `typedef const C D' is
915 legal; as of 2002-09-15 the committee is considering that
916 question. EDG 3.0 allows that syntax. Therefore, we do as
918 if (qualifying_scope && TYPE_P (qualifying_scope))
919 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925 declarator = make_declarator (cdk_id);
926 declarator->u.id.qualifying_scope = qualifying_scope;
927 declarator->u.id.unqualified_name = unqualified_name;
928 declarator->u.id.sfk = sfk;
933 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
934 of modifiers such as const or volatile to apply to the pointer
935 type, represented as identifiers. */
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_pointer);
943 declarator->declarator = target;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = NULL_TREE;
948 declarator->parameter_pack_p = target->parameter_pack_p;
949 target->parameter_pack_p = false;
952 declarator->parameter_pack_p = false;
957 /* Like make_pointer_declarator -- but for references. */
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 cp_declarator *declarator;
965 declarator = make_declarator (cdk_reference);
966 declarator->declarator = target;
967 declarator->u.reference.qualifiers = cv_qualifiers;
968 declarator->u.reference.rvalue_ref = rvalue_ref;
971 declarator->parameter_pack_p = target->parameter_pack_p;
972 target->parameter_pack_p = false;
975 declarator->parameter_pack_p = false;
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981 member of CLASS_TYPE. */
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985 cp_declarator *pointee)
987 cp_declarator *declarator;
989 declarator = make_declarator (cdk_ptrmem);
990 declarator->declarator = pointee;
991 declarator->u.pointer.qualifiers = cv_qualifiers;
992 declarator->u.pointer.class_type = class_type;
996 declarator->parameter_pack_p = pointee->parameter_pack_p;
997 pointee->parameter_pack_p = false;
1000 declarator->parameter_pack_p = false;
1005 /* Make a declarator for the function given by TARGET, with the
1006 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1007 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1008 indicates what exceptions can be thrown. */
1011 make_call_declarator (cp_declarator *target,
1013 cp_cv_quals cv_qualifiers,
1014 tree exception_specification,
1015 tree late_return_type)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1024 declarator->u.function.late_return_type = late_return_type;
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1031 declarator->parameter_pack_p = false;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1053 declarator->parameter_pack_p = false;
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1068 while (declarator && !found)
1070 switch ((int)declarator->kind)
1081 declarator = declarator->declarator;
1089 cp_parameter_declarator *no_parameters;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1099 cp_parameter_declarator *parameter;
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1107 clear_decl_specs (¶meter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 function_declarator_p (const cp_declarator *declarator)
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1128 declarator = declarator->declarator;
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, treat user-defined type-names
1199 as non-type identifiers. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201 /* When parsing a type-specifier, do not try to parse a class-specifier
1202 or enum-specifier. */
1203 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206 /* This type is used for parameters and variables which hold
1207 combinations of the above flags. */
1208 typedef int cp_parser_flags;
1210 /* The different kinds of declarators we want to parse. */
1212 typedef enum cp_parser_declarator_kind
1214 /* We want an abstract declarator. */
1215 CP_PARSER_DECLARATOR_ABSTRACT,
1216 /* We want a named declarator. */
1217 CP_PARSER_DECLARATOR_NAMED,
1218 /* We don't mind, but the name must be an unqualified-id. */
1219 CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1222 /* The precedence values used to parse binary expressions. The minimum value
1223 of PREC must be 1, because zero is reserved to quickly discriminate
1224 binary operators from other tokens. */
1229 PREC_LOGICAL_OR_EXPRESSION,
1230 PREC_LOGICAL_AND_EXPRESSION,
1231 PREC_INCLUSIVE_OR_EXPRESSION,
1232 PREC_EXCLUSIVE_OR_EXPRESSION,
1233 PREC_AND_EXPRESSION,
1234 PREC_EQUALITY_EXPRESSION,
1235 PREC_RELATIONAL_EXPRESSION,
1236 PREC_SHIFT_EXPRESSION,
1237 PREC_ADDITIVE_EXPRESSION,
1238 PREC_MULTIPLICATIVE_EXPRESSION,
1240 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244 precedence value. */
1246 typedef struct cp_parser_binary_operations_map_node
1248 /* The token type. */
1249 enum cpp_ttype token_type;
1250 /* The corresponding tree code. */
1251 enum tree_code tree_type;
1252 /* The precedence of this operator. */
1253 enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1256 /* The status of a tentative parse. */
1258 typedef enum cp_parser_status_kind
1260 /* No errors have occurred. */
1261 CP_PARSER_STATUS_KIND_NO_ERROR,
1262 /* An error has occurred. */
1263 CP_PARSER_STATUS_KIND_ERROR,
1264 /* We are committed to this tentative parse, whether or not an error
1266 CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1269 typedef struct cp_parser_expression_stack_entry
1271 /* Left hand side of the binary operation we are currently
1274 /* Original tree code for left hand side, if it was a binary
1275 expression itself (used for -Wparentheses). */
1276 enum tree_code lhs_type;
1277 /* Tree code for the binary operation we are parsing. */
1278 enum tree_code tree_type;
1279 /* Precedence of the binary operation we are parsing. */
1280 enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1283 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1284 entries because precedence levels on the stack are monotonically
1286 typedef struct cp_parser_expression_stack_entry
1287 cp_parser_expression_stack[NUM_PREC_VALUES];
1289 /* Context that is saved and restored when parsing tentatively. */
1290 typedef struct GTY (()) cp_parser_context {
1291 /* If this is a tentative parsing context, the status of the
1293 enum cp_parser_status_kind status;
1294 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1295 that are looked up in this context must be looked up both in the
1296 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297 the context of the containing expression. */
1300 /* The next parsing context in the stack. */
1301 struct cp_parser_context *next;
1302 } cp_parser_context;
1306 /* Constructors and destructors. */
1308 static cp_parser_context *cp_parser_context_new
1309 (cp_parser_context *);
1311 /* Class variables. */
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316 Transformed into an associative array (binops_by_token) by
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 /* The same as binops, but initialized by cp_parser_new so that
1353 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357 /* Constructors and destructors. */
1359 /* Construct a new context. The context below this one on the stack
1360 is given by NEXT. */
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1365 cp_parser_context *context;
1367 /* Allocate the storage. */
1368 if (cp_parser_context_free_list != NULL)
1370 /* Pull the first entry from the free list. */
1371 context = cp_parser_context_free_list;
1372 cp_parser_context_free_list = context->next;
1373 memset (context, 0, sizeof (*context));
1376 context = GGC_CNEW (cp_parser_context);
1378 /* No errors have occurred yet in this context. */
1379 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380 /* If this is not the bottommost context, copy information that we
1381 need from the previous context. */
1384 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385 expression, then we are parsing one in this context, too. */
1386 context->object_type = next->object_type;
1387 /* Thread the stack. */
1388 context->next = next;
1394 /* The cp_parser structure represents the C++ parser. */
1396 typedef struct GTY(()) cp_parser {
1397 /* The lexer from which we are obtaining tokens. */
1400 /* The scope in which names should be looked up. If NULL_TREE, then
1401 we look up names in the scope that is currently open in the
1402 source program. If non-NULL, this is either a TYPE or
1403 NAMESPACE_DECL for the scope in which we should look. It can
1404 also be ERROR_MARK, when we've parsed a bogus scope.
1406 This value is not cleared automatically after a name is looked
1407 up, so we must be careful to clear it before starting a new look
1408 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1409 will look up `Z' in the scope of `X', rather than the current
1410 scope.) Unfortunately, it is difficult to tell when name lookup
1411 is complete, because we sometimes peek at a token, look it up,
1412 and then decide not to consume it. */
1415 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416 last lookup took place. OBJECT_SCOPE is used if an expression
1417 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418 respectively. QUALIFYING_SCOPE is used for an expression of the
1419 form "X::Y"; it refers to X. */
1421 tree qualifying_scope;
1423 /* A stack of parsing contexts. All but the bottom entry on the
1424 stack will be tentative contexts.
1426 We parse tentatively in order to determine which construct is in
1427 use in some situations. For example, in order to determine
1428 whether a statement is an expression-statement or a
1429 declaration-statement we parse it tentatively as a
1430 declaration-statement. If that fails, we then reparse the same
1431 token stream as an expression-statement. */
1432 cp_parser_context *context;
1434 /* True if we are parsing GNU C++. If this flag is not set, then
1435 GNU extensions are not recognized. */
1436 bool allow_gnu_extensions_p;
1438 /* TRUE if the `>' token should be interpreted as the greater-than
1439 operator. FALSE if it is the end of a template-id or
1440 template-parameter-list. In C++0x mode, this flag also applies to
1441 `>>' tokens, which are viewed as two consecutive `>' tokens when
1442 this flag is FALSE. */
1443 bool greater_than_is_operator_p;
1445 /* TRUE if default arguments are allowed within a parameter list
1446 that starts at this point. FALSE if only a gnu extension makes
1447 them permissible. */
1448 bool default_arg_ok_p;
1450 /* TRUE if we are parsing an integral constant-expression. See
1451 [expr.const] for a precise definition. */
1452 bool integral_constant_expression_p;
1454 /* TRUE if we are parsing an integral constant-expression -- but a
1455 non-constant expression should be permitted as well. This flag
1456 is used when parsing an array bound so that GNU variable-length
1457 arrays are tolerated. */
1458 bool allow_non_integral_constant_expression_p;
1460 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461 been seen that makes the expression non-constant. */
1462 bool non_integral_constant_expression_p;
1464 /* TRUE if local variable names and `this' are forbidden in the
1466 bool local_variables_forbidden_p;
1468 /* TRUE if the declaration we are parsing is part of a
1469 linkage-specification of the form `extern string-literal
1471 bool in_unbraced_linkage_specification_p;
1473 /* TRUE if we are presently parsing a declarator, after the
1474 direct-declarator. */
1475 bool in_declarator_p;
1477 /* TRUE if we are presently parsing a template-argument-list. */
1478 bool in_template_argument_list_p;
1480 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481 to IN_OMP_BLOCK if parsing OpenMP structured block and
1482 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1483 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484 iteration-statement, OpenMP block or loop within that switch. */
1485 #define IN_SWITCH_STMT 1
1486 #define IN_ITERATION_STMT 2
1487 #define IN_OMP_BLOCK 4
1488 #define IN_OMP_FOR 8
1489 #define IN_IF_STMT 16
1490 unsigned char in_statement;
1492 /* TRUE if we are presently parsing the body of a switch statement.
1493 Note that this doesn't quite overlap with in_statement above.
1494 The difference relates to giving the right sets of error messages:
1495 "case not in switch" vs "break statement used with OpenMP...". */
1496 bool in_switch_statement_p;
1498 /* TRUE if we are parsing a type-id in an expression context. In
1499 such a situation, both "type (expr)" and "type (type)" are valid
1501 bool in_type_id_in_expr_p;
1503 /* TRUE if we are currently in a header file where declarations are
1504 implicitly extern "C". */
1505 bool implicit_extern_c;
1507 /* TRUE if strings in expressions should be translated to the execution
1509 bool translate_strings_p;
1511 /* TRUE if we are presently parsing the body of a function, but not
1513 bool in_function_body;
1515 /* If non-NULL, then we are parsing a construct where new type
1516 definitions are not permitted. The string stored here will be
1517 issued as an error message if a type is defined. */
1518 const char *type_definition_forbidden_message;
1520 /* A list of lists. The outer list is a stack, used for member
1521 functions of local classes. At each level there are two sub-list,
1522 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524 TREE_VALUE's. The functions are chained in reverse declaration
1527 The TREE_PURPOSE sublist contains those functions with default
1528 arguments that need post processing, and the TREE_VALUE sublist
1529 contains those functions with definitions that need post
1532 These lists can only be processed once the outermost class being
1533 defined is complete. */
1534 tree unparsed_functions_queues;
1536 /* The number of classes whose definitions are currently in
1538 unsigned num_classes_being_defined;
1540 /* The number of template parameter lists that apply directly to the
1541 current declaration. */
1542 unsigned num_template_parameter_lists;
1547 /* Constructors and destructors. */
1549 static cp_parser *cp_parser_new
1552 /* Routines to parse various constructs.
1554 Those that return `tree' will return the error_mark_node (rather
1555 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556 Sometimes, they will return an ordinary node if error-recovery was
1557 attempted, even though a parse error occurred. So, to check
1558 whether or not a parse error occurred, you should always use
1559 cp_parser_error_occurred. If the construct is optional (indicated
1560 either by an `_opt' in the name of the function that does the
1561 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562 the construct is not present. */
1564 /* Lexical conventions [gram.lex] */
1566 static tree cp_parser_identifier
1568 static tree cp_parser_string_literal
1569 (cp_parser *, bool, bool);
1571 /* Basic concepts [gram.basic] */
1573 static bool cp_parser_translation_unit
1576 /* Expressions [gram.expr] */
1578 static tree cp_parser_primary_expression
1579 (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581 (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589 (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591 (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593 (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597 (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599 (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601 (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1604 static tree cp_parser_new_expression
1606 static VEC(tree,gc) *cp_parser_new_placement
1608 static tree cp_parser_new_type_id
1609 (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1612 static cp_declarator *cp_parser_direct_new_declarator
1614 static VEC(tree,gc) *cp_parser_new_initializer
1616 static tree cp_parser_delete_expression
1618 static tree cp_parser_cast_expression
1619 (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623 (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625 (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1628 static tree cp_parser_expression
1629 (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631 (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1634 static tree cp_parser_lambda_expression
1636 static void cp_parser_lambda_introducer
1637 (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641 (cp_parser *, tree);
1643 /* Statements [gram.stmt.stmt] */
1645 static void cp_parser_statement
1646 (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1649 static tree cp_parser_expression_statement
1650 (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652 (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654 (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656 (cp_parser *, bool *);
1657 static tree cp_parser_condition
1659 static tree cp_parser_iteration_statement
1661 static void cp_parser_for_init_statement
1663 static tree cp_parser_jump_statement
1665 static void cp_parser_declaration_statement
1668 static tree cp_parser_implicitly_scoped_statement
1669 (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1673 /* Declarations [gram.dcl.dcl] */
1675 static void cp_parser_declaration_seq_opt
1677 static void cp_parser_declaration
1679 static void cp_parser_block_declaration
1680 (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1687 static tree cp_parser_function_specifier_opt
1688 (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692 static tree cp_parser_simple_type_specifier
1693 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1696 static tree cp_parser_nonclass_name
1697 (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699 (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1702 static void cp_parser_enumerator_list
1703 (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705 (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1708 static void cp_parser_namespace_definition
1710 static void cp_parser_namespace_body
1712 static tree cp_parser_qualified_namespace_specifier
1714 static void cp_parser_namespace_alias_definition
1716 static bool cp_parser_using_declaration
1717 (cp_parser *, bool);
1718 static void cp_parser_using_directive
1720 static void cp_parser_asm_definition
1722 static void cp_parser_linkage_specification
1724 static void cp_parser_static_assert
1725 (cp_parser *, bool);
1726 static tree cp_parser_decltype
1729 /* Declarators [gram.dcl.decl] */
1731 static tree cp_parser_init_declarator
1732 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738 (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741 static tree cp_parser_late_return_type_opt
1743 static tree cp_parser_declarator_id
1744 (cp_parser *, bool);
1745 static tree cp_parser_type_id
1747 static tree cp_parser_template_type_arg
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751 (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1756 static tree cp_parser_parameter_declaration_list
1757 (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759 (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument
1761 (cp_parser *, bool);
1762 static void cp_parser_function_body
1764 static tree cp_parser_initializer
1765 (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767 (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769 (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771 (cp_parser *, bool *);
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1776 /* Classes [gram.class] */
1778 static tree cp_parser_class_name
1779 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1782 static tree cp_parser_class_head
1783 (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1786 static void cp_parser_member_specification_opt
1788 static void cp_parser_member_declaration
1790 static tree cp_parser_pure_specifier
1792 static tree cp_parser_constant_initializer
1795 /* Derived classes [gram.class.derived] */
1797 static tree cp_parser_base_clause
1799 static tree cp_parser_base_specifier
1802 /* Special member functions [gram.special] */
1804 static tree cp_parser_conversion_function_id
1806 static tree cp_parser_conversion_type_id
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1810 static bool cp_parser_ctor_initializer_opt
1812 static void cp_parser_mem_initializer_list
1814 static tree cp_parser_mem_initializer
1816 static tree cp_parser_mem_initializer_id
1819 /* Overloading [gram.over] */
1821 static tree cp_parser_operator_function_id
1823 static tree cp_parser_operator
1826 /* Templates [gram.temp] */
1828 static void cp_parser_template_declaration
1829 (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1832 static tree cp_parser_template_parameter
1833 (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835 (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837 (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839 (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1842 static tree cp_parser_template_argument
1844 static void cp_parser_explicit_instantiation
1846 static void cp_parser_explicit_specialization
1849 /* Exception handling [gram.exception] */
1851 static tree cp_parser_try_block
1853 static bool cp_parser_function_try_block
1855 static void cp_parser_handler_seq
1857 static void cp_parser_handler
1859 static tree cp_parser_exception_declaration
1861 static tree cp_parser_throw_expression
1863 static tree cp_parser_exception_specification_opt
1865 static tree cp_parser_type_id_list
1868 /* GNU Extensions */
1870 static tree cp_parser_asm_specification_opt
1872 static tree cp_parser_asm_operand_list
1874 static tree cp_parser_asm_clobber_list
1876 static tree cp_parser_asm_label_list
1878 static tree cp_parser_attributes_opt
1880 static tree cp_parser_attribute_list
1882 static bool cp_parser_extension_opt
1883 (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889 (cp_parser *, enum pragma_context);
1891 /* Objective-C++ Productions */
1893 static tree cp_parser_objc_message_receiver
1895 static tree cp_parser_objc_message_args
1897 static tree cp_parser_objc_message_expression
1899 static tree cp_parser_objc_encode_expression
1901 static tree cp_parser_objc_defs_expression
1903 static tree cp_parser_objc_protocol_expression
1905 static tree cp_parser_objc_selector_expression
1907 static tree cp_parser_objc_expression
1909 static bool cp_parser_objc_selector_p
1911 static tree cp_parser_objc_selector
1913 static tree cp_parser_objc_protocol_refs_opt
1915 static void cp_parser_objc_declaration
1917 static tree cp_parser_objc_statement
1920 /* Utility Routines */
1922 static tree cp_parser_lookup_name
1923 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925 (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1928 static bool cp_parser_check_declarator_template_parameters
1929 (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931 (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1934 static tree cp_parser_global_scope_opt
1935 (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937 (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941 (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943 (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945 (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949 (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1954 static void cp_parser_save_default_args
1955 (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959 (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961 (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963 (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1966 static void cp_parser_set_storage_class
1967 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969 (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971 (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973 (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975 (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1978 static bool cp_parser_next_token_starts_class_definition_p
1980 static bool cp_parser_next_token_ends_template_argument_p
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983 (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1986 static void cp_parser_check_class_key
1987 (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989 (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1992 static void cp_parser_pre_parsed_nested_name_specifier
1994 static bool cp_parser_cache_group
1995 (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1998 static void cp_parser_commit_to_tentative_parse
2000 static void cp_parser_abort_tentative_parse
2002 static bool cp_parser_parse_definitely
2004 static inline bool cp_parser_parsing_tentatively
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2008 static void cp_parser_error
2009 (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011 (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2014 static bool cp_parser_check_type_definition
2016 static void cp_parser_check_for_definition_in_return_type
2017 (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019 (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021 (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023 (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026 static int cp_parser_skip_to_closing_parenthesis
2027 (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2032 static void cp_parser_skip_to_end_of_block_or_statement
2034 static bool cp_parser_skip_to_closing_brace
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2038 static void cp_parser_skip_to_pragma_eol
2039 (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2042 static bool cp_parser_allow_gnu_extensions_p
2044 static bool cp_parser_is_string_literal
2046 static bool cp_parser_is_keyword
2047 (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049 (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053 /* Returns nonzero if we are parsing tentatively. */
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2058 return parser->context->next != NULL;
2061 /* Returns nonzero if TOKEN is a string literal. */
2064 cp_parser_is_string_literal (cp_token* token)
2066 return (token->type == CPP_STRING ||
2067 token->type == CPP_STRING16 ||
2068 token->type == CPP_STRING32 ||
2069 token->type == CPP_WSTRING ||
2070 token->type == CPP_UTF8STRING);
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 return token->keyword == keyword;
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082 FILE:LINE: MESSAGE before TOKEN
2083 where TOKEN is the next token in the input stream. MESSAGE
2084 (specified by the caller) is usually of the form "expected
2088 cp_parser_error (cp_parser* parser, const char* message)
2090 if (!cp_parser_simulate_error (parser))
2092 cp_token *token = cp_lexer_peek_token (parser->lexer);
2093 /* This diagnostic makes more sense if it is tagged to the line
2094 of the token we just peeked at. */
2095 cp_lexer_set_source_position_from_token (token);
2097 if (token->type == CPP_PRAGMA)
2099 error_at (token->location,
2100 "%<#pragma%> is not allowed here");
2101 cp_parser_skip_to_pragma_eol (parser, token);
2105 c_parse_error (message,
2106 /* Because c_parser_error does not understand
2107 CPP_KEYWORD, keywords are treated like
2109 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110 token->u.value, token->flags);
2114 /* Issue an error about name-lookup failing. NAME is the
2115 IDENTIFIER_NODE DECL is the result of
2116 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2117 the thing that we hoped to find. */
2120 cp_parser_name_lookup_error (cp_parser* parser,
2123 const char* desired,
2124 location_t location)
2126 /* If name lookup completely failed, tell the user that NAME was not
2128 if (decl == error_mark_node)
2130 if (parser->scope && parser->scope != global_namespace)
2131 error_at (location, "%<%E::%E%> has not been declared",
2132 parser->scope, name);
2133 else if (parser->scope == global_namespace)
2134 error_at (location, "%<::%E%> has not been declared", name);
2135 else if (parser->object_scope
2136 && !CLASS_TYPE_P (parser->object_scope))
2137 error_at (location, "request for member %qE in non-class type %qT",
2138 name, parser->object_scope);
2139 else if (parser->object_scope)
2140 error_at (location, "%<%T::%E%> has not been declared",
2141 parser->object_scope, name);
2143 error_at (location, "%qE has not been declared", name);
2145 else if (parser->scope && parser->scope != global_namespace)
2146 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147 else if (parser->scope == global_namespace)
2148 error_at (location, "%<::%E%> %s", name, desired);
2150 error_at (location, "%qE %s", name, desired);
2153 /* If we are parsing tentatively, remember that an error has occurred
2154 during this tentative parse. Returns true if the error was
2155 simulated; false if a message should be issued by the caller. */
2158 cp_parser_simulate_error (cp_parser* parser)
2160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2168 /* Check for repeated decl-specifiers. */
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172 location_t location)
2176 for (ds = ds_first; ds != ds_last; ++ds)
2178 unsigned count = decl_specs->specs[ds];
2181 /* The "long" specifier is a special case because of "long long". */
2185 error_at (location, "%<long long long%> is too long for GCC");
2187 pedwarn_cxx98 (location, OPT_Wlong_long,
2188 "ISO C++ 1998 does not support %<long long%>");
2192 static const char *const decl_spec_names[] = {
2209 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2214 /* This function is called when a type is defined. If type
2215 definitions are forbidden at this point, an error message is
2219 cp_parser_check_type_definition (cp_parser* parser)
2221 /* If types are forbidden here, issue a message. */
2222 if (parser->type_definition_forbidden_message)
2224 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225 in the message need to be interpreted. */
2226 error (parser->type_definition_forbidden_message);
2232 /* This function is called when the DECLARATOR is processed. The TYPE
2233 was a type defined in the decl-specifiers. If it is invalid to
2234 define a type in the decl-specifiers for DECLARATOR, an error is
2235 issued. TYPE_LOCATION is the location of TYPE and is used
2236 for error reporting. */
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240 tree type, location_t type_location)
2242 /* [dcl.fct] forbids type definitions in return types.
2243 Unfortunately, it's not easy to know whether or not we are
2244 processing a return type until after the fact. */
2246 && (declarator->kind == cdk_pointer
2247 || declarator->kind == cdk_reference
2248 || declarator->kind == cdk_ptrmem))
2249 declarator = declarator->declarator;
2251 && declarator->kind == cdk_function)
2253 error_at (type_location,
2254 "new types may not be defined in a return type");
2255 inform (type_location,
2256 "(perhaps a semicolon is missing after the definition of %qT)",
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262 "<" in any valid C++ program. If the next token is indeed "<",
2263 issue a message warning the user about what appears to be an
2264 invalid attempt to form a template-id. LOCATION is the location
2265 of the type-specifier (TYPE) */
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269 tree type, location_t location)
2271 cp_token_position start = 0;
2273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276 error_at (location, "%qT is not a template", type);
2277 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278 error_at (location, "%qE is not a template", type);
2280 error_at (location, "invalid template-id");
2281 /* Remember the location of the invalid "<". */
2282 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283 start = cp_lexer_token_position (parser->lexer, true);
2284 /* Consume the "<". */
2285 cp_lexer_consume_token (parser->lexer);
2286 /* Parse the template arguments. */
2287 cp_parser_enclosed_template_argument_list (parser);
2288 /* Permanently remove the invalid template arguments so that
2289 this error message is not issued again. */
2291 cp_lexer_purge_tokens_after (parser->lexer, start);
2295 /* If parsing an integral constant-expression, issue an error message
2296 about the fact that THING appeared and return true. Otherwise,
2297 return false. In either case, set
2298 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2301 cp_parser_non_integral_constant_expression (cp_parser *parser,
2304 parser->non_integral_constant_expression_p = true;
2305 if (parser->integral_constant_expression_p)
2307 if (!parser->allow_non_integral_constant_expression_p)
2309 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310 in the message need to be interpreted. */
2311 char *message = concat (thing,
2312 " cannot appear in a constant-expression",
2322 /* Emit a diagnostic for an invalid type name. SCOPE is the
2323 qualifying scope (or NULL, if none) for ID. This function commits
2324 to the current active tentative parse, if any. (Otherwise, the
2325 problematic construct might be encountered again later, resulting
2326 in duplicate error messages.) LOCATION is the location of ID. */
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330 tree scope, tree id,
2331 location_t location)
2333 tree decl, old_scope;
2334 /* Try to lookup the identifier. */
2335 old_scope = parser->scope;
2336 parser->scope = scope;
2337 decl = cp_parser_lookup_name_simple (parser, id, location);
2338 parser->scope = old_scope;
2339 /* If the lookup found a template-name, it means that the user forgot
2340 to specify an argument list. Emit a useful error message. */
2341 if (TREE_CODE (decl) == TEMPLATE_DECL)
2343 "invalid use of template-name %qE without an argument list",
2345 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346 error_at (location, "invalid use of destructor %qD as a type", id);
2347 else if (TREE_CODE (decl) == TYPE_DECL)
2348 /* Something like 'unsigned A a;' */
2349 error_at (location, "invalid combination of multiple type-specifiers");
2350 else if (!parser->scope)
2352 /* Issue an error message. */
2353 error_at (location, "%qE does not name a type", id);
2354 /* If we're in a template class, it's possible that the user was
2355 referring to a type from a base class. For example:
2357 template <typename T> struct A { typedef T X; };
2358 template <typename T> struct B : public A<T> { X x; };
2360 The user should have said "typename A<T>::X". */
2361 if (processing_template_decl && current_class_type
2362 && TYPE_BINFO (current_class_type))
2366 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2370 tree base_type = BINFO_TYPE (b);
2371 if (CLASS_TYPE_P (base_type)
2372 && dependent_type_p (base_type))
2375 /* Go from a particular instantiation of the
2376 template (which will have an empty TYPE_FIELDs),
2377 to the main version. */
2378 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379 for (field = TYPE_FIELDS (base_type);
2381 field = TREE_CHAIN (field))
2382 if (TREE_CODE (field) == TYPE_DECL
2383 && DECL_NAME (field) == id)
2386 "(perhaps %<typename %T::%E%> was intended)",
2387 BINFO_TYPE (b), id);
2396 /* Here we diagnose qualified-ids where the scope is actually correct,
2397 but the identifier does not resolve to a valid type name. */
2398 else if (parser->scope != error_mark_node)
2400 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401 error_at (location, "%qE in namespace %qE does not name a type",
2403 else if (TYPE_P (parser->scope)
2404 && dependent_scope_p (parser->scope))
2405 error_at (location, "need %<typename%> before %<%T::%E%> because "
2406 "%qT is a dependent scope",
2407 parser->scope, id, parser->scope);
2408 else if (TYPE_P (parser->scope))
2409 error_at (location, "%qE in class %qT does not name a type",
2414 cp_parser_commit_to_tentative_parse (parser);
2417 /* Check for a common situation where a type-name should be present,
2418 but is not, and issue a sensible error message. Returns true if an
2419 invalid type-name was detected.
2421 The situation handled by this function are variable declarations of the
2422 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2423 Usually, `ID' should name a type, but if we got here it means that it
2424 does not. We try to emit the best possible error message depending on
2425 how exactly the id-expression looks like. */
2428 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2431 cp_token *token = cp_lexer_peek_token (parser->lexer);
2433 /* Avoid duplicate error about ambiguous lookup. */
2434 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2436 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2437 if (next->type == CPP_NAME && next->ambiguous_p)
2441 cp_parser_parse_tentatively (parser);
2442 id = cp_parser_id_expression (parser,
2443 /*template_keyword_p=*/false,
2444 /*check_dependency_p=*/true,
2445 /*template_p=*/NULL,
2446 /*declarator_p=*/true,
2447 /*optional_p=*/false);
2448 /* If the next token is a (, this is a function with no explicit return
2449 type, i.e. constructor, destructor or conversion op. */
2450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2451 || TREE_CODE (id) == TYPE_DECL)
2453 cp_parser_abort_tentative_parse (parser);
2456 if (!cp_parser_parse_definitely (parser))
2459 /* Emit a diagnostic for the invalid type. */
2460 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2461 id, token->location);
2463 /* If we aren't in the middle of a declarator (i.e. in a
2464 parameter-declaration-clause), skip to the end of the declaration;
2465 there's no point in trying to process it. */
2466 if (!parser->in_declarator_p)
2467 cp_parser_skip_to_end_of_block_or_statement (parser);
2471 /* Consume tokens up to, and including, the next non-nested closing `)'.
2472 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2473 are doing error recovery. Returns -1 if OR_COMMA is true and we
2474 found an unnested comma. */
2477 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2482 unsigned paren_depth = 0;
2483 unsigned brace_depth = 0;
2484 unsigned square_depth = 0;
2486 if (recovering && !or_comma
2487 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2492 cp_token * token = cp_lexer_peek_token (parser->lexer);
2494 switch (token->type)
2497 case CPP_PRAGMA_EOL:
2498 /* If we've run out of tokens, then there is no closing `)'. */
2501 /* This is good for lambda expression capture-lists. */
2502 case CPP_OPEN_SQUARE:
2505 case CPP_CLOSE_SQUARE:
2506 if (!square_depth--)
2511 /* This matches the processing in skip_to_end_of_statement. */
2516 case CPP_OPEN_BRACE:
2519 case CPP_CLOSE_BRACE:
2525 if (recovering && or_comma && !brace_depth && !paren_depth
2530 case CPP_OPEN_PAREN:
2535 case CPP_CLOSE_PAREN:
2536 if (!brace_depth && !paren_depth--)
2539 cp_lexer_consume_token (parser->lexer);
2548 /* Consume the token. */
2549 cp_lexer_consume_token (parser->lexer);
2553 /* Consume tokens until we reach the end of the current statement.
2554 Normally, that will be just before consuming a `;'. However, if a
2555 non-nested `}' comes first, then we stop before consuming that. */
2558 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2560 unsigned nesting_depth = 0;
2564 cp_token *token = cp_lexer_peek_token (parser->lexer);
2566 switch (token->type)
2569 case CPP_PRAGMA_EOL:
2570 /* If we've run out of tokens, stop. */
2574 /* If the next token is a `;', we have reached the end of the
2580 case CPP_CLOSE_BRACE:
2581 /* If this is a non-nested '}', stop before consuming it.
2582 That way, when confronted with something like:
2586 we stop before consuming the closing '}', even though we
2587 have not yet reached a `;'. */
2588 if (nesting_depth == 0)
2591 /* If it is the closing '}' for a block that we have
2592 scanned, stop -- but only after consuming the token.
2598 we will stop after the body of the erroneously declared
2599 function, but before consuming the following `typedef'
2601 if (--nesting_depth == 0)
2603 cp_lexer_consume_token (parser->lexer);
2607 case CPP_OPEN_BRACE:
2615 /* Consume the token. */
2616 cp_lexer_consume_token (parser->lexer);
2620 /* This function is called at the end of a statement or declaration.
2621 If the next token is a semicolon, it is consumed; otherwise, error
2622 recovery is attempted. */
2625 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2627 /* Look for the trailing `;'. */
2628 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2630 /* If there is additional (erroneous) input, skip to the end of
2632 cp_parser_skip_to_end_of_statement (parser);
2633 /* If the next token is now a `;', consume it. */
2634 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2635 cp_lexer_consume_token (parser->lexer);
2639 /* Skip tokens until we have consumed an entire block, or until we
2640 have consumed a non-nested `;'. */
2643 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2645 int nesting_depth = 0;
2647 while (nesting_depth >= 0)
2649 cp_token *token = cp_lexer_peek_token (parser->lexer);
2651 switch (token->type)
2654 case CPP_PRAGMA_EOL:
2655 /* If we've run out of tokens, stop. */
2659 /* Stop if this is an unnested ';'. */
2664 case CPP_CLOSE_BRACE:
2665 /* Stop if this is an unnested '}', or closes the outermost
2668 if (nesting_depth < 0)
2674 case CPP_OPEN_BRACE:
2683 /* Consume the token. */
2684 cp_lexer_consume_token (parser->lexer);
2688 /* Skip tokens until a non-nested closing curly brace is the next
2689 token, or there are no more tokens. Return true in the first case,
2693 cp_parser_skip_to_closing_brace (cp_parser *parser)
2695 unsigned nesting_depth = 0;
2699 cp_token *token = cp_lexer_peek_token (parser->lexer);
2701 switch (token->type)
2704 case CPP_PRAGMA_EOL:
2705 /* If we've run out of tokens, stop. */
2708 case CPP_CLOSE_BRACE:
2709 /* If the next token is a non-nested `}', then we have reached
2710 the end of the current block. */
2711 if (nesting_depth-- == 0)
2715 case CPP_OPEN_BRACE:
2716 /* If it the next token is a `{', then we are entering a new
2717 block. Consume the entire block. */
2725 /* Consume the token. */
2726 cp_lexer_consume_token (parser->lexer);
2730 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2731 parameter is the PRAGMA token, allowing us to purge the entire pragma
2735 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2739 parser->lexer->in_pragma = false;
2742 token = cp_lexer_consume_token (parser->lexer);
2743 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2745 /* Ensure that the pragma is not parsed again. */
2746 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2749 /* Require pragma end of line, resyncing with it as necessary. The
2750 arguments are as for cp_parser_skip_to_pragma_eol. */
2753 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2755 parser->lexer->in_pragma = false;
2756 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2757 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2760 /* This is a simple wrapper around make_typename_type. When the id is
2761 an unresolved identifier node, we can provide a superior diagnostic
2762 using cp_parser_diagnose_invalid_type_name. */
2765 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2766 tree id, location_t id_location)
2769 if (TREE_CODE (id) == IDENTIFIER_NODE)
2771 result = make_typename_type (scope, id, typename_type,
2772 /*complain=*/tf_none);
2773 if (result == error_mark_node)
2774 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2777 return make_typename_type (scope, id, typename_type, tf_error);
2780 /* This is a wrapper around the
2781 make_{pointer,ptrmem,reference}_declarator functions that decides
2782 which one to call based on the CODE and CLASS_TYPE arguments. The
2783 CODE argument should be one of the values returned by
2784 cp_parser_ptr_operator. */
2785 static cp_declarator *
2786 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2787 cp_cv_quals cv_qualifiers,
2788 cp_declarator *target)
2790 if (code == ERROR_MARK)
2791 return cp_error_declarator;
2793 if (code == INDIRECT_REF)
2794 if (class_type == NULL_TREE)
2795 return make_pointer_declarator (cv_qualifiers, target);
2797 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2798 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2799 return make_reference_declarator (cv_qualifiers, target, false);
2800 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2801 return make_reference_declarator (cv_qualifiers, target, true);
2805 /* Create a new C++ parser. */
2808 cp_parser_new (void)
2814 /* cp_lexer_new_main is called before calling ggc_alloc because
2815 cp_lexer_new_main might load a PCH file. */
2816 lexer = cp_lexer_new_main ();
2818 /* Initialize the binops_by_token so that we can get the tree
2819 directly from the token. */
2820 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2821 binops_by_token[binops[i].token_type] = binops[i];
2823 parser = GGC_CNEW (cp_parser);
2824 parser->lexer = lexer;
2825 parser->context = cp_parser_context_new (NULL);
2827 /* For now, we always accept GNU extensions. */
2828 parser->allow_gnu_extensions_p = 1;
2830 /* The `>' token is a greater-than operator, not the end of a
2832 parser->greater_than_is_operator_p = true;
2834 parser->default_arg_ok_p = true;
2836 /* We are not parsing a constant-expression. */
2837 parser->integral_constant_expression_p = false;
2838 parser->allow_non_integral_constant_expression_p = false;
2839 parser->non_integral_constant_expression_p = false;
2841 /* Local variable names are not forbidden. */
2842 parser->local_variables_forbidden_p = false;
2844 /* We are not processing an `extern "C"' declaration. */
2845 parser->in_unbraced_linkage_specification_p = false;
2847 /* We are not processing a declarator. */
2848 parser->in_declarator_p = false;
2850 /* We are not processing a template-argument-list. */
2851 parser->in_template_argument_list_p = false;
2853 /* We are not in an iteration statement. */
2854 parser->in_statement = 0;
2856 /* We are not in a switch statement. */
2857 parser->in_switch_statement_p = false;
2859 /* We are not parsing a type-id inside an expression. */
2860 parser->in_type_id_in_expr_p = false;
2862 /* Declarations aren't implicitly extern "C". */
2863 parser->implicit_extern_c = false;
2865 /* String literals should be translated to the execution character set. */
2866 parser->translate_strings_p = true;
2868 /* We are not parsing a function body. */
2869 parser->in_function_body = false;
2871 /* The unparsed function queue is empty. */
2872 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2874 /* There are no classes being defined. */
2875 parser->num_classes_being_defined = 0;
2877 /* No template parameters apply. */
2878 parser->num_template_parameter_lists = 0;
2883 /* Create a cp_lexer structure which will emit the tokens in CACHE
2884 and push it onto the parser's lexer stack. This is used for delayed
2885 parsing of in-class method bodies and default arguments, and should
2886 not be confused with tentative parsing. */
2888 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2890 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2891 lexer->next = parser->lexer;
2892 parser->lexer = lexer;
2894 /* Move the current source position to that of the first token in the
2896 cp_lexer_set_source_position_from_token (lexer->next_token);
2899 /* Pop the top lexer off the parser stack. This is never used for the
2900 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2902 cp_parser_pop_lexer (cp_parser *parser)
2904 cp_lexer *lexer = parser->lexer;
2905 parser->lexer = lexer->next;
2906 cp_lexer_destroy (lexer);
2908 /* Put the current source position back where it was before this
2909 lexer was pushed. */
2910 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2913 /* Lexical conventions [gram.lex] */
2915 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2919 cp_parser_identifier (cp_parser* parser)
2923 /* Look for the identifier. */
2924 token = cp_parser_require (parser, CPP_NAME, "identifier");
2925 /* Return the value. */
2926 return token ? token->u.value : error_mark_node;
2929 /* Parse a sequence of adjacent string constants. Returns a
2930 TREE_STRING representing the combined, nul-terminated string
2931 constant. If TRANSLATE is true, translate the string to the
2932 execution character set. If WIDE_OK is true, a wide string is
2935 C++98 [lex.string] says that if a narrow string literal token is
2936 adjacent to a wide string literal token, the behavior is undefined.
2937 However, C99 6.4.5p4 says that this results in a wide string literal.
2938 We follow C99 here, for consistency with the C front end.
2940 This code is largely lifted from lex_string() in c-lex.c.
2942 FUTURE: ObjC++ will need to handle @-strings here. */
2944 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2948 struct obstack str_ob;
2949 cpp_string str, istr, *strs;
2951 enum cpp_ttype type;
2953 tok = cp_lexer_peek_token (parser->lexer);
2954 if (!cp_parser_is_string_literal (tok))
2956 cp_parser_error (parser, "expected string-literal");
2957 return error_mark_node;
2962 /* Try to avoid the overhead of creating and destroying an obstack
2963 for the common case of just one string. */
2964 if (!cp_parser_is_string_literal
2965 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2967 cp_lexer_consume_token (parser->lexer);
2969 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2970 str.len = TREE_STRING_LENGTH (tok->u.value);
2977 gcc_obstack_init (&str_ob);
2982 cp_lexer_consume_token (parser->lexer);
2984 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2985 str.len = TREE_STRING_LENGTH (tok->u.value);
2987 if (type != tok->type)
2989 if (type == CPP_STRING)
2991 else if (tok->type != CPP_STRING)
2992 error_at (tok->location,
2993 "unsupported non-standard concatenation "
2994 "of string literals");
2997 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2999 tok = cp_lexer_peek_token (parser->lexer);
3001 while (cp_parser_is_string_literal (tok));
3003 strs = (cpp_string *) obstack_finish (&str_ob);
3006 if (type != CPP_STRING && !wide_ok)
3008 cp_parser_error (parser, "a wide string is invalid in this context");
3012 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3013 (parse_in, strs, count, &istr, type))
3015 value = build_string (istr.len, (const char *)istr.text);
3016 free (CONST_CAST (unsigned char *, istr.text));
3022 case CPP_UTF8STRING:
3023 TREE_TYPE (value) = char_array_type_node;
3026 TREE_TYPE (value) = char16_array_type_node;
3029 TREE_TYPE (value) = char32_array_type_node;
3032 TREE_TYPE (value) = wchar_array_type_node;
3036 value = fix_string_type (value);
3039 /* cpp_interpret_string has issued an error. */
3040 value = error_mark_node;
3043 obstack_free (&str_ob, 0);
3049 /* Basic concepts [gram.basic] */
3051 /* Parse a translation-unit.
3054 declaration-seq [opt]
3056 Returns TRUE if all went well. */
3059 cp_parser_translation_unit (cp_parser* parser)
3061 /* The address of the first non-permanent object on the declarator
3063 static void *declarator_obstack_base;
3067 /* Create the declarator obstack, if necessary. */
3068 if (!cp_error_declarator)
3070 gcc_obstack_init (&declarator_obstack);
3071 /* Create the error declarator. */
3072 cp_error_declarator = make_declarator (cdk_error);
3073 /* Create the empty parameter list. */
3074 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3075 /* Remember where the base of the declarator obstack lies. */
3076 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3079 cp_parser_declaration_seq_opt (parser);
3081 /* If there are no tokens left then all went well. */
3082 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3084 /* Get rid of the token array; we don't need it any more. */
3085 cp_lexer_destroy (parser->lexer);
3086 parser->lexer = NULL;
3088 /* This file might have been a context that's implicitly extern
3089 "C". If so, pop the lang context. (Only relevant for PCH.) */
3090 if (parser->implicit_extern_c)
3092 pop_lang_context ();
3093 parser->implicit_extern_c = false;
3097 finish_translation_unit ();
3103 cp_parser_error (parser, "expected declaration");
3107 /* Make sure the declarator obstack was fully cleaned up. */
3108 gcc_assert (obstack_next_free (&declarator_obstack)
3109 == declarator_obstack_base);
3111 /* All went well. */
3115 /* Expressions [gram.expr] */
3117 /* Parse a primary-expression.
3128 ( compound-statement )
3129 __builtin_va_arg ( assignment-expression , type-id )
3130 __builtin_offsetof ( type-id , offsetof-expression )
3133 __has_nothrow_assign ( type-id )
3134 __has_nothrow_constructor ( type-id )
3135 __has_nothrow_copy ( type-id )
3136 __has_trivial_assign ( type-id )
3137 __has_trivial_constructor ( type-id )
3138 __has_trivial_copy ( type-id )
3139 __has_trivial_destructor ( type-id )
3140 __has_virtual_destructor ( type-id )
3141 __is_abstract ( type-id )
3142 __is_base_of ( type-id , type-id )
3143 __is_class ( type-id )
3144 __is_convertible_to ( type-id , type-id )
3145 __is_empty ( type-id )
3146 __is_enum ( type-id )
3147 __is_pod ( type-id )
3148 __is_polymorphic ( type-id )
3149 __is_union ( type-id )
3151 Objective-C++ Extension:
3159 ADDRESS_P is true iff this expression was immediately preceded by
3160 "&" and therefore might denote a pointer-to-member. CAST_P is true
3161 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3162 true iff this expression is a template argument.
3164 Returns a representation of the expression. Upon return, *IDK
3165 indicates what kind of id-expression (if any) was present. */
3168 cp_parser_primary_expression (cp_parser *parser,
3171 bool template_arg_p,
3174 cp_token *token = NULL;
3176 /* Assume the primary expression is not an id-expression. */
3177 *idk = CP_ID_KIND_NONE;
3179 /* Peek at the next token. */
3180 token = cp_lexer_peek_token (parser->lexer);
3181 switch (token->type)
3194 token = cp_lexer_consume_token (parser->lexer);
3195 if (TREE_CODE (token->u.value) == FIXED_CST)
3197 error_at (token->location,
3198 "fixed-point types not supported in C++");
3199 return error_mark_node;
3201 /* Floating-point literals are only allowed in an integral
3202 constant expression if they are cast to an integral or
3203 enumeration type. */
3204 if (TREE_CODE (token->u.value) == REAL_CST
3205 && parser->integral_constant_expression_p
3208 /* CAST_P will be set even in invalid code like "int(2.7 +
3209 ...)". Therefore, we have to check that the next token
3210 is sure to end the cast. */
3213 cp_token *next_token;
3215 next_token = cp_lexer_peek_token (parser->lexer);
3216 if (/* The comma at the end of an
3217 enumerator-definition. */
3218 next_token->type != CPP_COMMA
3219 /* The curly brace at the end of an enum-specifier. */
3220 && next_token->type != CPP_CLOSE_BRACE
3221 /* The end of a statement. */
3222 && next_token->type != CPP_SEMICOLON
3223 /* The end of the cast-expression. */
3224 && next_token->type != CPP_CLOSE_PAREN
3225 /* The end of an array bound. */
3226 && next_token->type != CPP_CLOSE_SQUARE
3227 /* The closing ">" in a template-argument-list. */
3228 && (next_token->type != CPP_GREATER
3229 || parser->greater_than_is_operator_p)
3230 /* C++0x only: A ">>" treated like two ">" tokens,
3231 in a template-argument-list. */
3232 && (next_token->type != CPP_RSHIFT
3233 || (cxx_dialect == cxx98)
3234 || parser->greater_than_is_operator_p))
3238 /* If we are within a cast, then the constraint that the
3239 cast is to an integral or enumeration type will be
3240 checked at that point. If we are not within a cast, then
3241 this code is invalid. */
3243 cp_parser_non_integral_constant_expression
3244 (parser, "floating-point literal");
3246 return token->u.value;
3252 case CPP_UTF8STRING:
3253 /* ??? Should wide strings be allowed when parser->translate_strings_p
3254 is false (i.e. in attributes)? If not, we can kill the third
3255 argument to cp_parser_string_literal. */
3256 return cp_parser_string_literal (parser,
3257 parser->translate_strings_p,
3260 case CPP_OPEN_PAREN:
3263 bool saved_greater_than_is_operator_p;
3265 /* Consume the `('. */
3266 cp_lexer_consume_token (parser->lexer);
3267 /* Within a parenthesized expression, a `>' token is always
3268 the greater-than operator. */
3269 saved_greater_than_is_operator_p
3270 = parser->greater_than_is_operator_p;
3271 parser->greater_than_is_operator_p = true;
3272 /* If we see `( { ' then we are looking at the beginning of
3273 a GNU statement-expression. */
3274 if (cp_parser_allow_gnu_extensions_p (parser)
3275 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3277 /* Statement-expressions are not allowed by the standard. */
3278 pedwarn (token->location, OPT_pedantic,
3279 "ISO C++ forbids braced-groups within expressions");
3281 /* And they're not allowed outside of a function-body; you
3282 cannot, for example, write:
3284 int i = ({ int j = 3; j + 1; });
3286 at class or namespace scope. */
3287 if (!parser->in_function_body
3288 || parser->in_template_argument_list_p)
3290 error_at (token->location,
3291 "statement-expressions are not allowed outside "
3292 "functions nor in template-argument lists");
3293 cp_parser_skip_to_end_of_block_or_statement (parser);
3294 expr = error_mark_node;
3298 /* Start the statement-expression. */
3299 expr = begin_stmt_expr ();
3300 /* Parse the compound-statement. */
3301 cp_parser_compound_statement (parser, expr, false);
3303 expr = finish_stmt_expr (expr, false);
3308 /* Parse the parenthesized expression. */
3309 expr = cp_parser_expression (parser, cast_p, idk);
3310 /* Let the front end know that this expression was
3311 enclosed in parentheses. This matters in case, for
3312 example, the expression is of the form `A::B', since
3313 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3315 finish_parenthesized_expr (expr);
3317 /* The `>' token might be the end of a template-id or
3318 template-parameter-list now. */
3319 parser->greater_than_is_operator_p
3320 = saved_greater_than_is_operator_p;
3321 /* Consume the `)'. */
3322 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3323 cp_parser_skip_to_end_of_statement (parser);
3328 case CPP_OPEN_SQUARE:
3329 if (c_dialect_objc ())
3330 /* We have an Objective-C++ message. */
3331 return cp_parser_objc_expression (parser);
3332 maybe_warn_cpp0x ("lambda expressions");
3333 return cp_parser_lambda_expression (parser);
3335 case CPP_OBJC_STRING:
3336 if (c_dialect_objc ())
3337 /* We have an Objective-C++ string literal. */
3338 return cp_parser_objc_expression (parser);
3339 cp_parser_error (parser, "expected primary-expression");
3340 return error_mark_node;
3343 switch (token->keyword)
3345 /* These two are the boolean literals. */
3347 cp_lexer_consume_token (parser->lexer);
3348 return boolean_true_node;
3350 cp_lexer_consume_token (parser->lexer);
3351 return boolean_false_node;
3353 /* The `__null' literal. */
3355 cp_lexer_consume_token (parser->lexer);
3358 /* Recognize the `this' keyword. */
3360 cp_lexer_consume_token (parser->lexer);
3361 if (parser->local_variables_forbidden_p)
3363 error_at (token->location,
3364 "%<this%> may not be used in this context");
3365 return error_mark_node;
3367 /* Pointers cannot appear in constant-expressions. */
3368 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3369 return error_mark_node;
3370 return finish_this_expr ();
3372 /* The `operator' keyword can be the beginning of an
3377 case RID_FUNCTION_NAME:
3378 case RID_PRETTY_FUNCTION_NAME:
3379 case RID_C99_FUNCTION_NAME:
3383 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3384 __func__ are the names of variables -- but they are
3385 treated specially. Therefore, they are handled here,
3386 rather than relying on the generic id-expression logic
3387 below. Grammatically, these names are id-expressions.
3389 Consume the token. */
3390 token = cp_lexer_consume_token (parser->lexer);
3392 switch (token->keyword)
3394 case RID_FUNCTION_NAME:
3395 name = "%<__FUNCTION__%>";
3397 case RID_PRETTY_FUNCTION_NAME:
3398 name = "%<__PRETTY_FUNCTION__%>";
3400 case RID_C99_FUNCTION_NAME:
3401 name = "%<__func__%>";
3407 if (cp_parser_non_integral_constant_expression (parser, name))
3408 return error_mark_node;
3410 /* Look up the name. */
3411 return finish_fname (token->u.value);
3419 /* The `__builtin_va_arg' construct is used to handle
3420 `va_arg'. Consume the `__builtin_va_arg' token. */
3421 cp_lexer_consume_token (parser->lexer);
3422 /* Look for the opening `('. */
3423 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3424 /* Now, parse the assignment-expression. */
3425 expression = cp_parser_assignment_expression (parser,
3426 /*cast_p=*/false, NULL);
3427 /* Look for the `,'. */
3428 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3429 /* Parse the type-id. */
3430 type = cp_parser_type_id (parser);
3431 /* Look for the closing `)'. */
3432 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3433 /* Using `va_arg' in a constant-expression is not
3435 if (cp_parser_non_integral_constant_expression (parser,
3437 return error_mark_node;
3438 return build_x_va_arg (expression, type);
3442 return cp_parser_builtin_offsetof (parser);
3444 case RID_HAS_NOTHROW_ASSIGN:
3445 case RID_HAS_NOTHROW_CONSTRUCTOR:
3446 case RID_HAS_NOTHROW_COPY:
3447 case RID_HAS_TRIVIAL_ASSIGN:
3448 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3449 case RID_HAS_TRIVIAL_COPY:
3450 case RID_HAS_TRIVIAL_DESTRUCTOR:
3451 case RID_HAS_VIRTUAL_DESTRUCTOR:
3452 case RID_IS_ABSTRACT:
3453 case RID_IS_BASE_OF:
3455 case RID_IS_CONVERTIBLE_TO:
3459 case RID_IS_POLYMORPHIC:
3460 case RID_IS_STD_LAYOUT:
3461 case RID_IS_TRIVIAL:
3463 return cp_parser_trait_expr (parser, token->keyword);
3465 /* Objective-C++ expressions. */
3467 case RID_AT_PROTOCOL:
3468 case RID_AT_SELECTOR:
3469 return cp_parser_objc_expression (parser);
3472 cp_parser_error (parser, "expected primary-expression");
3473 return error_mark_node;
3476 /* An id-expression can start with either an identifier, a
3477 `::' as the beginning of a qualified-id, or the "operator"
3481 case CPP_TEMPLATE_ID:
3482 case CPP_NESTED_NAME_SPECIFIER:
3486 const char *error_msg;
3489 cp_token *id_expr_token;
3492 /* Parse the id-expression. */
3494 = cp_parser_id_expression (parser,
3495 /*template_keyword_p=*/false,
3496 /*check_dependency_p=*/true,
3498 /*declarator_p=*/false,
3499 /*optional_p=*/false);
3500 if (id_expression == error_mark_node)
3501 return error_mark_node;
3502 id_expr_token = token;
3503 token = cp_lexer_peek_token (parser->lexer);
3504 done = (token->type != CPP_OPEN_SQUARE
3505 && token->type != CPP_OPEN_PAREN
3506 && token->type != CPP_DOT
3507 && token->type != CPP_DEREF
3508 && token->type != CPP_PLUS_PLUS
3509 && token->type != CPP_MINUS_MINUS);
3510 /* If we have a template-id, then no further lookup is
3511 required. If the template-id was for a template-class, we
3512 will sometimes have a TYPE_DECL at this point. */
3513 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3514 || TREE_CODE (id_expression) == TYPE_DECL)
3515 decl = id_expression;
3516 /* Look up the name. */
3519 tree ambiguous_decls;
3521 /* If we already know that this lookup is ambiguous, then
3522 we've already issued an error message; there's no reason
3524 if (id_expr_token->type == CPP_NAME
3525 && id_expr_token->ambiguous_p)
3527 cp_parser_simulate_error (parser);
3528 return error_mark_node;
3531 decl = cp_parser_lookup_name (parser, id_expression,
3534 /*is_namespace=*/false,
3535 /*check_dependency=*/true,
3537 id_expr_token->location);
3538 /* If the lookup was ambiguous, an error will already have
3540 if (ambiguous_decls)
3541 return error_mark_node;
3543 /* In Objective-C++, an instance variable (ivar) may be preferred
3544 to whatever cp_parser_lookup_name() found. */
3545 decl = objc_lookup_ivar (decl, id_expression);
3547 /* If name lookup gives us a SCOPE_REF, then the
3548 qualifying scope was dependent. */
3549 if (TREE_CODE (decl) == SCOPE_REF)
3551 /* At this point, we do not know if DECL is a valid
3552 integral constant expression. We assume that it is
3553 in fact such an expression, so that code like:
3555 template <int N> struct A {
3559 is accepted. At template-instantiation time, we
3560 will check that B<N>::i is actually a constant. */
3563 /* Check to see if DECL is a local variable in a context
3564 where that is forbidden. */
3565 if (parser->local_variables_forbidden_p
3566 && local_variable_p (decl))
3568 /* It might be that we only found DECL because we are
3569 trying to be generous with pre-ISO scoping rules.
3570 For example, consider:
3574 for (int i = 0; i < 10; ++i) {}
3575 extern void f(int j = i);
3578 Here, name look up will originally find the out
3579 of scope `i'. We need to issue a warning message,
3580 but then use the global `i'. */
3581 decl = check_for_out_of_scope_variable (decl);
3582 if (local_variable_p (decl))
3584 error_at (id_expr_token->location,
3585 "local variable %qD may not appear in this context",
3587 return error_mark_node;
3592 decl = (finish_id_expression
3593 (id_expression, decl, parser->scope,
3595 parser->integral_constant_expression_p,
3596 parser->allow_non_integral_constant_expression_p,
3597 &parser->non_integral_constant_expression_p,
3598 template_p, done, address_p,
3601 id_expr_token->location));
3603 cp_parser_error (parser, error_msg);
3607 /* Anything else is an error. */
3609 cp_parser_error (parser, "expected primary-expression");
3610 return error_mark_node;
3614 /* Parse an id-expression.
3621 :: [opt] nested-name-specifier template [opt] unqualified-id
3623 :: operator-function-id
3626 Return a representation of the unqualified portion of the
3627 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3628 a `::' or nested-name-specifier.
3630 Often, if the id-expression was a qualified-id, the caller will
3631 want to make a SCOPE_REF to represent the qualified-id. This
3632 function does not do this in order to avoid wastefully creating
3633 SCOPE_REFs when they are not required.
3635 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3638 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3639 uninstantiated templates.
3641 If *TEMPLATE_P is non-NULL, it is set to true iff the
3642 `template' keyword is used to explicitly indicate that the entity
3643 named is a template.
3645 If DECLARATOR_P is true, the id-expression is appearing as part of
3646 a declarator, rather than as part of an expression. */
3649 cp_parser_id_expression (cp_parser *parser,
3650 bool template_keyword_p,
3651 bool check_dependency_p,
3656 bool global_scope_p;
3657 bool nested_name_specifier_p;
3659 /* Assume the `template' keyword was not used. */
3661 *template_p = template_keyword_p;
3663 /* Look for the optional `::' operator. */
3665 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3667 /* Look for the optional nested-name-specifier. */
3668 nested_name_specifier_p
3669 = (cp_parser_nested_name_specifier_opt (parser,
3670 /*typename_keyword_p=*/false,
3675 /* If there is a nested-name-specifier, then we are looking at
3676 the first qualified-id production. */
3677 if (nested_name_specifier_p)
3680 tree saved_object_scope;
3681 tree saved_qualifying_scope;
3682 tree unqualified_id;
3685 /* See if the next token is the `template' keyword. */
3687 template_p = &is_template;
3688 *template_p = cp_parser_optional_template_keyword (parser);
3689 /* Name lookup we do during the processing of the
3690 unqualified-id might obliterate SCOPE. */
3691 saved_scope = parser->scope;
3692 saved_object_scope = parser->object_scope;
3693 saved_qualifying_scope = parser->qualifying_scope;
3694 /* Process the final unqualified-id. */
3695 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3698 /*optional_p=*/false);
3699 /* Restore the SAVED_SCOPE for our caller. */
3700 parser->scope = saved_scope;
3701 parser->object_scope = saved_object_scope;
3702 parser->qualifying_scope = saved_qualifying_scope;
3704 return unqualified_id;
3706 /* Otherwise, if we are in global scope, then we are looking at one
3707 of the other qualified-id productions. */
3708 else if (global_scope_p)
3713 /* Peek at the next token. */
3714 token = cp_lexer_peek_token (parser->lexer);
3716 /* If it's an identifier, and the next token is not a "<", then
3717 we can avoid the template-id case. This is an optimization
3718 for this common case. */
3719 if (token->type == CPP_NAME
3720 && !cp_parser_nth_token_starts_template_argument_list_p
3722 return cp_parser_identifier (parser);
3724 cp_parser_parse_tentatively (parser);
3725 /* Try a template-id. */
3726 id = cp_parser_template_id (parser,
3727 /*template_keyword_p=*/false,
3728 /*check_dependency_p=*/true,
3730 /* If that worked, we're done. */
3731 if (cp_parser_parse_definitely (parser))
3734 /* Peek at the next token. (Changes in the token buffer may
3735 have invalidated the pointer obtained above.) */
3736 token = cp_lexer_peek_token (parser->lexer);
3738 switch (token->type)
3741 return cp_parser_identifier (parser);
3744 if (token->keyword == RID_OPERATOR)
3745 return cp_parser_operator_function_id (parser);
3749 cp_parser_error (parser, "expected id-expression");
3750 return error_mark_node;
3754 return cp_parser_unqualified_id (parser, template_keyword_p,
3755 /*check_dependency_p=*/true,
3760 /* Parse an unqualified-id.
3764 operator-function-id
3765 conversion-function-id
3769 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3770 keyword, in a construct like `A::template ...'.
3772 Returns a representation of unqualified-id. For the `identifier'
3773 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3774 production a BIT_NOT_EXPR is returned; the operand of the
3775 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3776 other productions, see the documentation accompanying the
3777 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3778 names are looked up in uninstantiated templates. If DECLARATOR_P
3779 is true, the unqualified-id is appearing as part of a declarator,
3780 rather than as part of an expression. */
3783 cp_parser_unqualified_id (cp_parser* parser,
3784 bool template_keyword_p,
3785 bool check_dependency_p,
3791 /* Peek at the next token. */
3792 token = cp_lexer_peek_token (parser->lexer);
3794 switch (token->type)
3800 /* We don't know yet whether or not this will be a
3802 cp_parser_parse_tentatively (parser);
3803 /* Try a template-id. */
3804 id = cp_parser_template_id (parser, template_keyword_p,
3807 /* If it worked, we're done. */
3808 if (cp_parser_parse_definitely (parser))
3810 /* Otherwise, it's an ordinary identifier. */
3811 return cp_parser_identifier (parser);
3814 case CPP_TEMPLATE_ID:
3815 return cp_parser_template_id (parser, template_keyword_p,
3822 tree qualifying_scope;
3827 /* Consume the `~' token. */
3828 cp_lexer_consume_token (parser->lexer);
3829 /* Parse the class-name. The standard, as written, seems to
3832 template <typename T> struct S { ~S (); };
3833 template <typename T> S<T>::~S() {}
3835 is invalid, since `~' must be followed by a class-name, but
3836 `S<T>' is dependent, and so not known to be a class.
3837 That's not right; we need to look in uninstantiated
3838 templates. A further complication arises from:
3840 template <typename T> void f(T t) {
3844 Here, it is not possible to look up `T' in the scope of `T'
3845 itself. We must look in both the current scope, and the
3846 scope of the containing complete expression.
3848 Yet another issue is:
3857 The standard does not seem to say that the `S' in `~S'
3858 should refer to the type `S' and not the data member
3861 /* DR 244 says that we look up the name after the "~" in the
3862 same scope as we looked up the qualifying name. That idea
3863 isn't fully worked out; it's more complicated than that. */
3864 scope = parser->scope;
3865 object_scope = parser->object_scope;
3866 qualifying_scope = parser->qualifying_scope;
3868 /* Check for invalid scopes. */
3869 if (scope == error_mark_node)
3871 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3872 cp_lexer_consume_token (parser->lexer);
3873 return error_mark_node;
3875 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3877 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3878 error_at (token->location,
3879 "scope %qT before %<~%> is not a class-name",
3881 cp_parser_simulate_error (parser);
3882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883 cp_lexer_consume_token (parser->lexer);
3884 return error_mark_node;
3886 gcc_assert (!scope || TYPE_P (scope));
3888 /* If the name is of the form "X::~X" it's OK. */
3889 token = cp_lexer_peek_token (parser->lexer);
3891 && token->type == CPP_NAME
3892 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3894 && constructor_name_p (token->u.value, scope))
3896 cp_lexer_consume_token (parser->lexer);
3897 return build_nt (BIT_NOT_EXPR, scope);
3900 /* If there was an explicit qualification (S::~T), first look
3901 in the scope given by the qualification (i.e., S). */
3903 type_decl = NULL_TREE;
3906 cp_parser_parse_tentatively (parser);
3907 type_decl = cp_parser_class_name (parser,
3908 /*typename_keyword_p=*/false,
3909 /*template_keyword_p=*/false,
3911 /*check_dependency=*/false,
3912 /*class_head_p=*/false,
3914 if (cp_parser_parse_definitely (parser))
3917 /* In "N::S::~S", look in "N" as well. */
3918 if (!done && scope && qualifying_scope)
3920 cp_parser_parse_tentatively (parser);
3921 parser->scope = qualifying_scope;
3922 parser->object_scope = NULL_TREE;
3923 parser->qualifying_scope = NULL_TREE;
3925 = cp_parser_class_name (parser,
3926 /*typename_keyword_p=*/false,
3927 /*template_keyword_p=*/false,
3929 /*check_dependency=*/false,
3930 /*class_head_p=*/false,
3932 if (cp_parser_parse_definitely (parser))
3935 /* In "p->S::~T", look in the scope given by "*p" as well. */
3936 else if (!done && object_scope)
3938 cp_parser_parse_tentatively (parser);
3939 parser->scope = object_scope;
3940 parser->object_scope = NULL_TREE;
3941 parser->qualifying_scope = NULL_TREE;
3943 = cp_parser_class_name (parser,
3944 /*typename_keyword_p=*/false,
3945 /*template_keyword_p=*/false,
3947 /*check_dependency=*/false,
3948 /*class_head_p=*/false,
3950 if (cp_parser_parse_definitely (parser))
3953 /* Look in the surrounding context. */
3956 parser->scope = NULL_TREE;
3957 parser->object_scope = NULL_TREE;
3958 parser->qualifying_scope = NULL_TREE;
3959 if (processing_template_decl)
3960 cp_parser_parse_tentatively (parser);
3962 = cp_parser_class_name (parser,
3963 /*typename_keyword_p=*/false,
3964 /*template_keyword_p=*/false,
3966 /*check_dependency=*/false,
3967 /*class_head_p=*/false,
3969 if (processing_template_decl
3970 && ! cp_parser_parse_definitely (parser))
3972 /* We couldn't find a type with this name, so just accept
3973 it and check for a match at instantiation time. */
3974 type_decl = cp_parser_identifier (parser);
3975 if (type_decl != error_mark_node)
3976 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3980 /* If an error occurred, assume that the name of the
3981 destructor is the same as the name of the qualifying
3982 class. That allows us to keep parsing after running
3983 into ill-formed destructor names. */
3984 if (type_decl == error_mark_node && scope)
3985 return build_nt (BIT_NOT_EXPR, scope);
3986 else if (type_decl == error_mark_node)
3987 return error_mark_node;
3989 /* Check that destructor name and scope match. */
3990 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3992 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3993 error_at (token->location,
3994 "declaration of %<~%T%> as member of %qT",
3996 cp_parser_simulate_error (parser);
3997 return error_mark_node;
4002 A typedef-name that names a class shall not be used as the
4003 identifier in the declarator for a destructor declaration. */
4005 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4006 && !DECL_SELF_REFERENCE_P (type_decl)
4007 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 error_at (token->location,
4009 "typedef-name %qD used as destructor declarator",
4012 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4016 if (token->keyword == RID_OPERATOR)
4020 /* This could be a template-id, so we try that first. */
4021 cp_parser_parse_tentatively (parser);
4022 /* Try a template-id. */
4023 id = cp_parser_template_id (parser, template_keyword_p,
4024 /*check_dependency_p=*/true,
4026 /* If that worked, we're done. */
4027 if (cp_parser_parse_definitely (parser))
4029 /* We still don't know whether we're looking at an
4030 operator-function-id or a conversion-function-id. */
4031 cp_parser_parse_tentatively (parser);
4032 /* Try an operator-function-id. */
4033 id = cp_parser_operator_function_id (parser);
4034 /* If that didn't work, try a conversion-function-id. */
4035 if (!cp_parser_parse_definitely (parser))
4036 id = cp_parser_conversion_function_id (parser);
4045 cp_parser_error (parser, "expected unqualified-id");
4046 return error_mark_node;
4050 /* Parse an (optional) nested-name-specifier.
4052 nested-name-specifier: [C++98]
4053 class-or-namespace-name :: nested-name-specifier [opt]
4054 class-or-namespace-name :: template nested-name-specifier [opt]
4056 nested-name-specifier: [C++0x]
4059 nested-name-specifier identifier ::
4060 nested-name-specifier template [opt] simple-template-id ::
4062 PARSER->SCOPE should be set appropriately before this function is
4063 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4064 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4067 Sets PARSER->SCOPE to the class (TYPE) or namespace
4068 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4069 it unchanged if there is no nested-name-specifier. Returns the new
4070 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4072 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4073 part of a declaration and/or decl-specifier. */
4076 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4077 bool typename_keyword_p,
4078 bool check_dependency_p,
4080 bool is_declaration)
4082 bool success = false;
4083 cp_token_position start = 0;
4086 /* Remember where the nested-name-specifier starts. */
4087 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4089 start = cp_lexer_token_position (parser->lexer, false);
4090 push_deferring_access_checks (dk_deferred);
4097 tree saved_qualifying_scope;
4098 bool template_keyword_p;
4100 /* Spot cases that cannot be the beginning of a
4101 nested-name-specifier. */
4102 token = cp_lexer_peek_token (parser->lexer);
4104 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4105 the already parsed nested-name-specifier. */
4106 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4108 /* Grab the nested-name-specifier and continue the loop. */
4109 cp_parser_pre_parsed_nested_name_specifier (parser);
4110 /* If we originally encountered this nested-name-specifier
4111 with IS_DECLARATION set to false, we will not have
4112 resolved TYPENAME_TYPEs, so we must do so here. */
4114 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4116 new_scope = resolve_typename_type (parser->scope,
4117 /*only_current_p=*/false);
4118 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4119 parser->scope = new_scope;
4125 /* Spot cases that cannot be the beginning of a
4126 nested-name-specifier. On the second and subsequent times
4127 through the loop, we look for the `template' keyword. */
4128 if (success && token->keyword == RID_TEMPLATE)
4130 /* A template-id can start a nested-name-specifier. */
4131 else if (token->type == CPP_TEMPLATE_ID)
4135 /* If the next token is not an identifier, then it is
4136 definitely not a type-name or namespace-name. */
4137 if (token->type != CPP_NAME)
4139 /* If the following token is neither a `<' (to begin a
4140 template-id), nor a `::', then we are not looking at a
4141 nested-name-specifier. */
4142 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4143 if (token->type != CPP_SCOPE
4144 && !cp_parser_nth_token_starts_template_argument_list_p
4149 /* The nested-name-specifier is optional, so we parse
4151 cp_parser_parse_tentatively (parser);
4153 /* Look for the optional `template' keyword, if this isn't the
4154 first time through the loop. */
4156 template_keyword_p = cp_parser_optional_template_keyword (parser);
4158 template_keyword_p = false;
4160 /* Save the old scope since the name lookup we are about to do
4161 might destroy it. */
4162 old_scope = parser->scope;
4163 saved_qualifying_scope = parser->qualifying_scope;
4164 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4165 look up names in "X<T>::I" in order to determine that "Y" is
4166 a template. So, if we have a typename at this point, we make
4167 an effort to look through it. */
4169 && !typename_keyword_p
4171 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4172 parser->scope = resolve_typename_type (parser->scope,
4173 /*only_current_p=*/false);
4174 /* Parse the qualifying entity. */
4176 = cp_parser_qualifying_entity (parser,
4182 /* Look for the `::' token. */
4183 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4185 /* If we found what we wanted, we keep going; otherwise, we're
4187 if (!cp_parser_parse_definitely (parser))
4189 bool error_p = false;
4191 /* Restore the OLD_SCOPE since it was valid before the
4192 failed attempt at finding the last
4193 class-or-namespace-name. */
4194 parser->scope = old_scope;
4195 parser->qualifying_scope = saved_qualifying_scope;
4196 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4198 /* If the next token is an identifier, and the one after
4199 that is a `::', then any valid interpretation would have
4200 found a class-or-namespace-name. */
4201 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4202 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4204 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4207 token = cp_lexer_consume_token (parser->lexer);
4210 if (!token->ambiguous_p)
4213 tree ambiguous_decls;
4215 decl = cp_parser_lookup_name (parser, token->u.value,
4217 /*is_template=*/false,
4218 /*is_namespace=*/false,
4219 /*check_dependency=*/true,
4222 if (TREE_CODE (decl) == TEMPLATE_DECL)
4223 error_at (token->location,
4224 "%qD used without template parameters",
4226 else if (ambiguous_decls)
4228 error_at (token->location,
4229 "reference to %qD is ambiguous",
4231 print_candidates (ambiguous_decls);
4232 decl = error_mark_node;
4236 const char* msg = "is not a class or namespace";
4237 if (cxx_dialect != cxx98)
4238 msg = "is not a class, namespace, or enumeration";
4239 cp_parser_name_lookup_error
4240 (parser, token->u.value, decl, msg,
4244 parser->scope = error_mark_node;
4246 /* Treat this as a successful nested-name-specifier
4251 If the name found is not a class-name (clause
4252 _class_) or namespace-name (_namespace.def_), the
4253 program is ill-formed. */
4256 cp_lexer_consume_token (parser->lexer);
4260 /* We've found one valid nested-name-specifier. */
4262 /* Name lookup always gives us a DECL. */
4263 if (TREE_CODE (new_scope) == TYPE_DECL)
4264 new_scope = TREE_TYPE (new_scope);
4265 /* Uses of "template" must be followed by actual templates. */
4266 if (template_keyword_p
4267 && !(CLASS_TYPE_P (new_scope)
4268 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4269 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4270 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4271 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4272 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4273 == TEMPLATE_ID_EXPR)))
4274 permerror (input_location, TYPE_P (new_scope)
4275 ? "%qT is not a template"
4276 : "%qD is not a template",
4278 /* If it is a class scope, try to complete it; we are about to
4279 be looking up names inside the class. */
4280 if (TYPE_P (new_scope)
4281 /* Since checking types for dependency can be expensive,
4282 avoid doing it if the type is already complete. */
4283 && !COMPLETE_TYPE_P (new_scope)
4284 /* Do not try to complete dependent types. */
4285 && !dependent_type_p (new_scope))
4287 new_scope = complete_type (new_scope);
4288 /* If it is a typedef to current class, use the current
4289 class instead, as the typedef won't have any names inside
4291 if (!COMPLETE_TYPE_P (new_scope)
4292 && currently_open_class (new_scope))
4293 new_scope = TYPE_MAIN_VARIANT (new_scope);
4295 /* Make sure we look in the right scope the next time through
4297 parser->scope = new_scope;
4300 /* If parsing tentatively, replace the sequence of tokens that makes
4301 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4302 token. That way, should we re-parse the token stream, we will
4303 not have to repeat the effort required to do the parse, nor will
4304 we issue duplicate error messages. */
4305 if (success && start)
4309 token = cp_lexer_token_at (parser->lexer, start);
4310 /* Reset the contents of the START token. */
4311 token->type = CPP_NESTED_NAME_SPECIFIER;
4312 /* Retrieve any deferred checks. Do not pop this access checks yet
4313 so the memory will not be reclaimed during token replacing below. */
4314 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4315 token->u.tree_check_value->value = parser->scope;
4316 token->u.tree_check_value->checks = get_deferred_access_checks ();
4317 token->u.tree_check_value->qualifying_scope =
4318 parser->qualifying_scope;
4319 token->keyword = RID_MAX;
4321 /* Purge all subsequent tokens. */
4322 cp_lexer_purge_tokens_after (parser->lexer, start);
4326 pop_to_parent_deferring_access_checks ();
4328 return success ? parser->scope : NULL_TREE;
4331 /* Parse a nested-name-specifier. See
4332 cp_parser_nested_name_specifier_opt for details. This function
4333 behaves identically, except that it will an issue an error if no
4334 nested-name-specifier is present. */
4337 cp_parser_nested_name_specifier (cp_parser *parser,
4338 bool typename_keyword_p,
4339 bool check_dependency_p,
4341 bool is_declaration)
4345 /* Look for the nested-name-specifier. */
4346 scope = cp_parser_nested_name_specifier_opt (parser,
4351 /* If it was not present, issue an error message. */
4354 cp_parser_error (parser, "expected nested-name-specifier");
4355 parser->scope = NULL_TREE;
4361 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4362 this is either a class-name or a namespace-name (which corresponds
4363 to the class-or-namespace-name production in the grammar). For
4364 C++0x, it can also be a type-name that refers to an enumeration
4367 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4368 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4369 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4370 TYPE_P is TRUE iff the next name should be taken as a class-name,
4371 even the same name is declared to be another entity in the same
4374 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4375 specified by the class-or-namespace-name. If neither is found the
4376 ERROR_MARK_NODE is returned. */
4379 cp_parser_qualifying_entity (cp_parser *parser,
4380 bool typename_keyword_p,
4381 bool template_keyword_p,
4382 bool check_dependency_p,
4384 bool is_declaration)
4387 tree saved_qualifying_scope;
4388 tree saved_object_scope;
4391 bool successful_parse_p;
4393 /* Before we try to parse the class-name, we must save away the
4394 current PARSER->SCOPE since cp_parser_class_name will destroy
4396 saved_scope = parser->scope;
4397 saved_qualifying_scope = parser->qualifying_scope;
4398 saved_object_scope = parser->object_scope;
4399 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4400 there is no need to look for a namespace-name. */
4401 only_class_p = template_keyword_p
4402 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4404 cp_parser_parse_tentatively (parser);
4405 scope = cp_parser_class_name (parser,
4408 type_p ? class_type : none_type,
4410 /*class_head_p=*/false,
4412 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4413 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4415 && cxx_dialect != cxx98
4416 && !successful_parse_p)
4418 /* Restore the saved scope. */
4419 parser->scope = saved_scope;
4420 parser->qualifying_scope = saved_qualifying_scope;
4421 parser->object_scope = saved_object_scope;
4423 /* Parse tentatively. */
4424 cp_parser_parse_tentatively (parser);
4426 /* Parse a typedef-name or enum-name. */
4427 scope = cp_parser_nonclass_name (parser);
4428 successful_parse_p = cp_parser_parse_definitely (parser);
4430 /* If that didn't work, try for a namespace-name. */
4431 if (!only_class_p && !successful_parse_p)
4433 /* Restore the saved scope. */
4434 parser->scope = saved_scope;
4435 parser->qualifying_scope = saved_qualifying_scope;
4436 parser->object_scope = saved_object_scope;
4437 /* If we are not looking at an identifier followed by the scope
4438 resolution operator, then this is not part of a
4439 nested-name-specifier. (Note that this function is only used
4440 to parse the components of a nested-name-specifier.) */
4441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4442 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4443 return error_mark_node;
4444 scope = cp_parser_namespace_name (parser);
4450 /* Parse a postfix-expression.
4454 postfix-expression [ expression ]
4455 postfix-expression ( expression-list [opt] )
4456 simple-type-specifier ( expression-list [opt] )
4457 typename :: [opt] nested-name-specifier identifier
4458 ( expression-list [opt] )
4459 typename :: [opt] nested-name-specifier template [opt] template-id
4460 ( expression-list [opt] )
4461 postfix-expression . template [opt] id-expression
4462 postfix-expression -> template [opt] id-expression
4463 postfix-expression . pseudo-destructor-name
4464 postfix-expression -> pseudo-destructor-name
4465 postfix-expression ++
4466 postfix-expression --
4467 dynamic_cast < type-id > ( expression )
4468 static_cast < type-id > ( expression )
4469 reinterpret_cast < type-id > ( expression )
4470 const_cast < type-id > ( expression )
4471 typeid ( expression )
4477 ( type-id ) { initializer-list , [opt] }
4479 This extension is a GNU version of the C99 compound-literal
4480 construct. (The C99 grammar uses `type-name' instead of `type-id',
4481 but they are essentially the same concept.)
4483 If ADDRESS_P is true, the postfix expression is the operand of the
4484 `&' operator. CAST_P is true if this expression is the target of a
4487 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4488 class member access expressions [expr.ref].
4490 Returns a representation of the expression. */
4493 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4494 bool member_access_only_p,
4495 cp_id_kind * pidk_return)
4499 cp_id_kind idk = CP_ID_KIND_NONE;
4500 tree postfix_expression = NULL_TREE;
4501 bool is_member_access = false;
4503 /* Peek at the next token. */
4504 token = cp_lexer_peek_token (parser->lexer);
4505 /* Some of the productions are determined by keywords. */
4506 keyword = token->keyword;
4516 const char *saved_message;
4518 /* All of these can be handled in the same way from the point
4519 of view of parsing. Begin by consuming the token
4520 identifying the cast. */
4521 cp_lexer_consume_token (parser->lexer);
4523 /* New types cannot be defined in the cast. */
4524 saved_message = parser->type_definition_forbidden_message;
4525 parser->type_definition_forbidden_message
4526 = "types may not be defined in casts";
4528 /* Look for the opening `<'. */
4529 cp_parser_require (parser, CPP_LESS, "%<<%>");
4530 /* Parse the type to which we are casting. */
4531 type = cp_parser_type_id (parser);
4532 /* Look for the closing `>'. */
4533 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4534 /* Restore the old message. */
4535 parser->type_definition_forbidden_message = saved_message;
4537 /* And the expression which is being cast. */
4538 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4539 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4540 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4542 /* Only type conversions to integral or enumeration types
4543 can be used in constant-expressions. */
4544 if (!cast_valid_in_integral_constant_expression_p (type)
4545 && (cp_parser_non_integral_constant_expression
4547 "a cast to a type other than an integral or "
4548 "enumeration type")))
4549 return error_mark_node;
4555 = build_dynamic_cast (type, expression, tf_warning_or_error);
4559 = build_static_cast (type, expression, tf_warning_or_error);
4563 = build_reinterpret_cast (type, expression,
4564 tf_warning_or_error);
4568 = build_const_cast (type, expression, tf_warning_or_error);
4579 const char *saved_message;
4580 bool saved_in_type_id_in_expr_p;
4582 /* Consume the `typeid' token. */
4583 cp_lexer_consume_token (parser->lexer);
4584 /* Look for the `(' token. */
4585 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4586 /* Types cannot be defined in a `typeid' expression. */
4587 saved_message = parser->type_definition_forbidden_message;
4588 parser->type_definition_forbidden_message
4589 = "types may not be defined in a %<typeid%> expression";
4590 /* We can't be sure yet whether we're looking at a type-id or an
4592 cp_parser_parse_tentatively (parser);
4593 /* Try a type-id first. */
4594 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4595 parser->in_type_id_in_expr_p = true;
4596 type = cp_parser_type_id (parser);
4597 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4598 /* Look for the `)' token. Otherwise, we can't be sure that
4599 we're not looking at an expression: consider `typeid (int
4600 (3))', for example. */
4601 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4602 /* If all went well, simply lookup the type-id. */
4603 if (cp_parser_parse_definitely (parser))
4604 postfix_expression = get_typeid (type);
4605 /* Otherwise, fall back to the expression variant. */
4610 /* Look for an expression. */
4611 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4612 /* Compute its typeid. */
4613 postfix_expression = build_typeid (expression);
4614 /* Look for the `)' token. */
4615 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4617 /* Restore the saved message. */
4618 parser->type_definition_forbidden_message = saved_message;
4619 /* `typeid' may not appear in an integral constant expression. */
4620 if (cp_parser_non_integral_constant_expression(parser,
4621 "%<typeid%> operator"))
4622 return error_mark_node;
4629 /* The syntax permitted here is the same permitted for an
4630 elaborated-type-specifier. */
4631 type = cp_parser_elaborated_type_specifier (parser,
4632 /*is_friend=*/false,
4633 /*is_declaration=*/false);
4634 postfix_expression = cp_parser_functional_cast (parser, type);
4642 /* If the next thing is a simple-type-specifier, we may be
4643 looking at a functional cast. We could also be looking at
4644 an id-expression. So, we try the functional cast, and if
4645 that doesn't work we fall back to the primary-expression. */
4646 cp_parser_parse_tentatively (parser);
4647 /* Look for the simple-type-specifier. */
4648 type = cp_parser_simple_type_specifier (parser,
4649 /*decl_specs=*/NULL,
4650 CP_PARSER_FLAGS_NONE);
4651 /* Parse the cast itself. */
4652 if (!cp_parser_error_occurred (parser))
4654 = cp_parser_functional_cast (parser, type);
4655 /* If that worked, we're done. */
4656 if (cp_parser_parse_definitely (parser))
4659 /* If the functional-cast didn't work out, try a
4660 compound-literal. */
4661 if (cp_parser_allow_gnu_extensions_p (parser)
4662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4664 VEC(constructor_elt,gc) *initializer_list = NULL;
4665 bool saved_in_type_id_in_expr_p;
4667 cp_parser_parse_tentatively (parser);
4668 /* Consume the `('. */
4669 cp_lexer_consume_token (parser->lexer);
4670 /* Parse the type. */
4671 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4672 parser->in_type_id_in_expr_p = true;
4673 type = cp_parser_type_id (parser);
4674 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4675 /* Look for the `)'. */
4676 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4677 /* Look for the `{'. */
4678 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4679 /* If things aren't going well, there's no need to
4681 if (!cp_parser_error_occurred (parser))
4683 bool non_constant_p;
4684 /* Parse the initializer-list. */
4686 = cp_parser_initializer_list (parser, &non_constant_p);
4687 /* Allow a trailing `,'. */
4688 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4689 cp_lexer_consume_token (parser->lexer);
4690 /* Look for the final `}'. */
4691 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4693 /* If that worked, we're definitely looking at a
4694 compound-literal expression. */
4695 if (cp_parser_parse_definitely (parser))
4697 /* Warn the user that a compound literal is not
4698 allowed in standard C++. */
4699 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4700 /* For simplicity, we disallow compound literals in
4701 constant-expressions. We could
4702 allow compound literals of integer type, whose
4703 initializer was a constant, in constant
4704 expressions. Permitting that usage, as a further
4705 extension, would not change the meaning of any
4706 currently accepted programs. (Of course, as
4707 compound literals are not part of ISO C++, the
4708 standard has nothing to say.) */
4709 if (cp_parser_non_integral_constant_expression
4710 (parser, "non-constant compound literals"))
4712 postfix_expression = error_mark_node;
4715 /* Form the representation of the compound-literal. */
4717 = (finish_compound_literal
4718 (type, build_constructor (init_list_type_node,
4719 initializer_list)));
4724 /* It must be a primary-expression. */
4726 = cp_parser_primary_expression (parser, address_p, cast_p,
4727 /*template_arg_p=*/false,
4733 /* Keep looping until the postfix-expression is complete. */
4736 if (idk == CP_ID_KIND_UNQUALIFIED
4737 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4738 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4739 /* It is not a Koenig lookup function call. */
4741 = unqualified_name_lookup_error (postfix_expression);
4743 /* Peek at the next token. */
4744 token = cp_lexer_peek_token (parser->lexer);
4746 switch (token->type)
4748 case CPP_OPEN_SQUARE:
4750 = cp_parser_postfix_open_square_expression (parser,
4753 idk = CP_ID_KIND_NONE;
4754 is_member_access = false;
4757 case CPP_OPEN_PAREN:
4758 /* postfix-expression ( expression-list [opt] ) */
4761 bool is_builtin_constant_p;
4762 bool saved_integral_constant_expression_p = false;
4763 bool saved_non_integral_constant_expression_p = false;
4766 is_member_access = false;
4768 is_builtin_constant_p
4769 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4770 if (is_builtin_constant_p)
4772 /* The whole point of __builtin_constant_p is to allow
4773 non-constant expressions to appear as arguments. */
4774 saved_integral_constant_expression_p
4775 = parser->integral_constant_expression_p;
4776 saved_non_integral_constant_expression_p
4777 = parser->non_integral_constant_expression_p;
4778 parser->integral_constant_expression_p = false;
4780 args = (cp_parser_parenthesized_expression_list
4781 (parser, /*is_attribute_list=*/false,
4782 /*cast_p=*/false, /*allow_expansion_p=*/true,
4783 /*non_constant_p=*/NULL));
4784 if (is_builtin_constant_p)
4786 parser->integral_constant_expression_p
4787 = saved_integral_constant_expression_p;
4788 parser->non_integral_constant_expression_p
4789 = saved_non_integral_constant_expression_p;
4794 postfix_expression = error_mark_node;
4798 /* Function calls are not permitted in
4799 constant-expressions. */
4800 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4801 && cp_parser_non_integral_constant_expression (parser,
4804 postfix_expression = error_mark_node;
4805 release_tree_vector (args);
4810 if (idk == CP_ID_KIND_UNQUALIFIED
4811 || idk == CP_ID_KIND_TEMPLATE_ID)
4813 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4815 if (!VEC_empty (tree, args))
4818 if (!any_type_dependent_arguments_p (args))
4820 = perform_koenig_lookup (postfix_expression, args);
4824 = unqualified_fn_lookup_error (postfix_expression);
4826 /* We do not perform argument-dependent lookup if
4827 normal lookup finds a non-function, in accordance
4828 with the expected resolution of DR 218. */
4829 else if (!VEC_empty (tree, args)
4830 && is_overloaded_fn (postfix_expression))
4832 tree fn = get_first_fn (postfix_expression);
4834 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4835 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4837 /* Only do argument dependent lookup if regular
4838 lookup does not find a set of member functions.
4839 [basic.lookup.koenig]/2a */
4840 if (!DECL_FUNCTION_MEMBER_P (fn))
4843 if (!any_type_dependent_arguments_p (args))
4845 = perform_koenig_lookup (postfix_expression, args);
4850 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4852 tree instance = TREE_OPERAND (postfix_expression, 0);
4853 tree fn = TREE_OPERAND (postfix_expression, 1);
4855 if (processing_template_decl
4856 && (type_dependent_expression_p (instance)
4857 || (!BASELINK_P (fn)
4858 && TREE_CODE (fn) != FIELD_DECL)
4859 || type_dependent_expression_p (fn)
4860 || any_type_dependent_arguments_p (args)))
4863 = build_nt_call_vec (postfix_expression, args);
4864 release_tree_vector (args);
4868 if (BASELINK_P (fn))
4871 = (build_new_method_call
4872 (instance, fn, &args, NULL_TREE,
4873 (idk == CP_ID_KIND_QUALIFIED
4874 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4876 tf_warning_or_error));
4880 = finish_call_expr (postfix_expression, &args,
4881 /*disallow_virtual=*/false,
4883 tf_warning_or_error);
4885 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4886 || TREE_CODE (postfix_expression) == MEMBER_REF
4887 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4888 postfix_expression = (build_offset_ref_call_from_tree
4889 (postfix_expression, &args));
4890 else if (idk == CP_ID_KIND_QUALIFIED)
4891 /* A call to a static class member, or a namespace-scope
4894 = finish_call_expr (postfix_expression, &args,
4895 /*disallow_virtual=*/true,
4897 tf_warning_or_error);
4899 /* All other function calls. */
4901 = finish_call_expr (postfix_expression, &args,
4902 /*disallow_virtual=*/false,
4904 tf_warning_or_error);
4906 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4907 idk = CP_ID_KIND_NONE;
4909 release_tree_vector (args);
4915 /* postfix-expression . template [opt] id-expression
4916 postfix-expression . pseudo-destructor-name
4917 postfix-expression -> template [opt] id-expression
4918 postfix-expression -> pseudo-destructor-name */
4920 /* Consume the `.' or `->' operator. */
4921 cp_lexer_consume_token (parser->lexer);
4924 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4929 is_member_access = true;
4933 /* postfix-expression ++ */
4934 /* Consume the `++' token. */
4935 cp_lexer_consume_token (parser->lexer);
4936 /* Generate a representation for the complete expression. */
4938 = finish_increment_expr (postfix_expression,
4939 POSTINCREMENT_EXPR);
4940 /* Increments may not appear in constant-expressions. */
4941 if (cp_parser_non_integral_constant_expression (parser,
4943 postfix_expression = error_mark_node;
4944 idk = CP_ID_KIND_NONE;
4945 is_member_access = false;
4948 case CPP_MINUS_MINUS:
4949 /* postfix-expression -- */
4950 /* Consume the `--' token. */
4951 cp_lexer_consume_token (parser->lexer);
4952 /* Generate a representation for the complete expression. */
4954 = finish_increment_expr (postfix_expression,
4955 POSTDECREMENT_EXPR);
4956 /* Decrements may not appear in constant-expressions. */
4957 if (cp_parser_non_integral_constant_expression (parser,
4959 postfix_expression = error_mark_node;
4960 idk = CP_ID_KIND_NONE;
4961 is_member_access = false;
4965 if (pidk_return != NULL)
4966 * pidk_return = idk;
4967 if (member_access_only_p)
4968 return is_member_access? postfix_expression : error_mark_node;
4970 return postfix_expression;
4974 /* We should never get here. */
4976 return error_mark_node;
4979 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4980 by cp_parser_builtin_offsetof. We're looking for
4982 postfix-expression [ expression ]
4984 FOR_OFFSETOF is set if we're being called in that context, which
4985 changes how we deal with integer constant expressions. */
4988 cp_parser_postfix_open_square_expression (cp_parser *parser,
4989 tree postfix_expression,
4994 /* Consume the `[' token. */
4995 cp_lexer_consume_token (parser->lexer);
4997 /* Parse the index expression. */
4998 /* ??? For offsetof, there is a question of what to allow here. If
4999 offsetof is not being used in an integral constant expression context,
5000 then we *could* get the right answer by computing the value at runtime.
5001 If we are in an integral constant expression context, then we might
5002 could accept any constant expression; hard to say without analysis.
5003 Rather than open the barn door too wide right away, allow only integer
5004 constant expressions here. */
5006 index = cp_parser_constant_expression (parser, false, NULL);
5008 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5010 /* Look for the closing `]'. */
5011 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5013 /* Build the ARRAY_REF. */
5014 postfix_expression = grok_array_decl (postfix_expression, index);
5016 /* When not doing offsetof, array references are not permitted in
5017 constant-expressions. */
5019 && (cp_parser_non_integral_constant_expression
5020 (parser, "an array reference")))
5021 postfix_expression = error_mark_node;
5023 return postfix_expression;
5026 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5027 by cp_parser_builtin_offsetof. We're looking for
5029 postfix-expression . template [opt] id-expression
5030 postfix-expression . pseudo-destructor-name
5031 postfix-expression -> template [opt] id-expression
5032 postfix-expression -> pseudo-destructor-name
5034 FOR_OFFSETOF is set if we're being called in that context. That sorta
5035 limits what of the above we'll actually accept, but nevermind.
5036 TOKEN_TYPE is the "." or "->" token, which will already have been
5037 removed from the stream. */
5040 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5041 enum cpp_ttype token_type,
5042 tree postfix_expression,
5043 bool for_offsetof, cp_id_kind *idk,
5044 location_t location)
5048 bool pseudo_destructor_p;
5049 tree scope = NULL_TREE;
5051 /* If this is a `->' operator, dereference the pointer. */
5052 if (token_type == CPP_DEREF)
5053 postfix_expression = build_x_arrow (postfix_expression);
5054 /* Check to see whether or not the expression is type-dependent. */
5055 dependent_p = type_dependent_expression_p (postfix_expression);
5056 /* The identifier following the `->' or `.' is not qualified. */
5057 parser->scope = NULL_TREE;
5058 parser->qualifying_scope = NULL_TREE;
5059 parser->object_scope = NULL_TREE;
5060 *idk = CP_ID_KIND_NONE;
5062 /* Enter the scope corresponding to the type of the object
5063 given by the POSTFIX_EXPRESSION. */
5064 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5066 scope = TREE_TYPE (postfix_expression);
5067 /* According to the standard, no expression should ever have
5068 reference type. Unfortunately, we do not currently match
5069 the standard in this respect in that our internal representation
5070 of an expression may have reference type even when the standard
5071 says it does not. Therefore, we have to manually obtain the
5072 underlying type here. */
5073 scope = non_reference (scope);
5074 /* The type of the POSTFIX_EXPRESSION must be complete. */
5075 if (scope == unknown_type_node)
5077 error_at (location, "%qE does not have class type",
5078 postfix_expression);
5082 scope = complete_type_or_else (scope, NULL_TREE);
5083 /* Let the name lookup machinery know that we are processing a
5084 class member access expression. */
5085 parser->context->object_type = scope;
5086 /* If something went wrong, we want to be able to discern that case,
5087 as opposed to the case where there was no SCOPE due to the type
5088 of expression being dependent. */
5090 scope = error_mark_node;
5091 /* If the SCOPE was erroneous, make the various semantic analysis
5092 functions exit quickly -- and without issuing additional error
5094 if (scope == error_mark_node)
5095 postfix_expression = error_mark_node;
5098 /* Assume this expression is not a pseudo-destructor access. */
5099 pseudo_destructor_p = false;
5101 /* If the SCOPE is a scalar type, then, if this is a valid program,
5102 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5103 is type dependent, it can be pseudo-destructor-name or something else.
5104 Try to parse it as pseudo-destructor-name first. */
5105 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5110 cp_parser_parse_tentatively (parser);
5111 /* Parse the pseudo-destructor-name. */
5113 cp_parser_pseudo_destructor_name (parser, &s, &type);
5115 && (cp_parser_error_occurred (parser)
5116 || TREE_CODE (type) != TYPE_DECL
5117 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5118 cp_parser_abort_tentative_parse (parser);
5119 else if (cp_parser_parse_definitely (parser))
5121 pseudo_destructor_p = true;
5123 = finish_pseudo_destructor_expr (postfix_expression,
5124 s, TREE_TYPE (type));
5128 if (!pseudo_destructor_p)
5130 /* If the SCOPE is not a scalar type, we are looking at an
5131 ordinary class member access expression, rather than a
5132 pseudo-destructor-name. */
5134 cp_token *token = cp_lexer_peek_token (parser->lexer);
5135 /* Parse the id-expression. */
5136 name = (cp_parser_id_expression
5138 cp_parser_optional_template_keyword (parser),
5139 /*check_dependency_p=*/true,
5141 /*declarator_p=*/false,
5142 /*optional_p=*/false));
5143 /* In general, build a SCOPE_REF if the member name is qualified.
5144 However, if the name was not dependent and has already been
5145 resolved; there is no need to build the SCOPE_REF. For example;
5147 struct X { void f(); };
5148 template <typename T> void f(T* t) { t->X::f(); }
5150 Even though "t" is dependent, "X::f" is not and has been resolved
5151 to a BASELINK; there is no need to include scope information. */
5153 /* But we do need to remember that there was an explicit scope for
5154 virtual function calls. */
5156 *idk = CP_ID_KIND_QUALIFIED;
5158 /* If the name is a template-id that names a type, we will get a
5159 TYPE_DECL here. That is invalid code. */
5160 if (TREE_CODE (name) == TYPE_DECL)
5162 error_at (token->location, "invalid use of %qD", name);
5163 postfix_expression = error_mark_node;
5167 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5169 name = build_qualified_name (/*type=*/NULL_TREE,
5173 parser->scope = NULL_TREE;
5174 parser->qualifying_scope = NULL_TREE;
5175 parser->object_scope = NULL_TREE;
5177 if (scope && name && BASELINK_P (name))
5178 adjust_result_of_qualified_name_lookup
5179 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5181 = finish_class_member_access_expr (postfix_expression, name,
5183 tf_warning_or_error);
5187 /* We no longer need to look up names in the scope of the object on
5188 the left-hand side of the `.' or `->' operator. */
5189 parser->context->object_type = NULL_TREE;
5191 /* Outside of offsetof, these operators may not appear in
5192 constant-expressions. */
5194 && (cp_parser_non_integral_constant_expression
5195 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5196 postfix_expression = error_mark_node;
5198 return postfix_expression;
5201 /* Parse a parenthesized expression-list.
5204 assignment-expression
5205 expression-list, assignment-expression
5210 identifier, expression-list
5212 CAST_P is true if this expression is the target of a cast.
5214 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5217 Returns a vector of trees. Each element is a representation of an
5218 assignment-expression. NULL is returned if the ( and or ) are
5219 missing. An empty, but allocated, vector is returned on no
5220 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5221 if this is really an attribute list being parsed. If
5222 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5223 not all of the expressions in the list were constant. */
5225 static VEC(tree,gc) *
5226 cp_parser_parenthesized_expression_list (cp_parser* parser,
5227 bool is_attribute_list,
5229 bool allow_expansion_p,
5230 bool *non_constant_p)
5232 VEC(tree,gc) *expression_list;
5233 bool fold_expr_p = is_attribute_list;
5234 tree identifier = NULL_TREE;
5235 bool saved_greater_than_is_operator_p;
5237 /* Assume all the expressions will be constant. */
5239 *non_constant_p = false;
5241 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5244 expression_list = make_tree_vector ();
5246 /* Within a parenthesized expression, a `>' token is always
5247 the greater-than operator. */
5248 saved_greater_than_is_operator_p
5249 = parser->greater_than_is_operator_p;
5250 parser->greater_than_is_operator_p = true;
5252 /* Consume expressions until there are no more. */
5253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5258 /* At the beginning of attribute lists, check to see if the
5259 next token is an identifier. */
5260 if (is_attribute_list
5261 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5265 /* Consume the identifier. */
5266 token = cp_lexer_consume_token (parser->lexer);
5267 /* Save the identifier. */
5268 identifier = token->u.value;
5272 bool expr_non_constant_p;
5274 /* Parse the next assignment-expression. */
5275 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5277 /* A braced-init-list. */
5278 maybe_warn_cpp0x ("extended initializer lists");
5279 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5280 if (non_constant_p && expr_non_constant_p)
5281 *non_constant_p = true;
5283 else if (non_constant_p)
5285 expr = (cp_parser_constant_expression
5286 (parser, /*allow_non_constant_p=*/true,
5287 &expr_non_constant_p));
5288 if (expr_non_constant_p)
5289 *non_constant_p = true;
5292 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5295 expr = fold_non_dependent_expr (expr);
5297 /* If we have an ellipsis, then this is an expression
5299 if (allow_expansion_p
5300 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5302 /* Consume the `...'. */
5303 cp_lexer_consume_token (parser->lexer);
5305 /* Build the argument pack. */
5306 expr = make_pack_expansion (expr);
5309 /* Add it to the list. We add error_mark_node
5310 expressions to the list, so that we can still tell if
5311 the correct form for a parenthesized expression-list
5312 is found. That gives better errors. */
5313 VEC_safe_push (tree, gc, expression_list, expr);
5315 if (expr == error_mark_node)
5319 /* After the first item, attribute lists look the same as
5320 expression lists. */
5321 is_attribute_list = false;
5324 /* If the next token isn't a `,', then we are done. */
5325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5328 /* Otherwise, consume the `,' and keep going. */
5329 cp_lexer_consume_token (parser->lexer);
5332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5337 /* We try and resync to an unnested comma, as that will give the
5338 user better diagnostics. */
5339 ending = cp_parser_skip_to_closing_parenthesis (parser,
5340 /*recovering=*/true,
5342 /*consume_paren=*/true);
5347 parser->greater_than_is_operator_p
5348 = saved_greater_than_is_operator_p;
5353 parser->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p;
5357 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5359 return expression_list;
5362 /* Parse a pseudo-destructor-name.
5364 pseudo-destructor-name:
5365 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5366 :: [opt] nested-name-specifier template template-id :: ~ type-name
5367 :: [opt] nested-name-specifier [opt] ~ type-name
5369 If either of the first two productions is used, sets *SCOPE to the
5370 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5371 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5372 or ERROR_MARK_NODE if the parse fails. */
5375 cp_parser_pseudo_destructor_name (cp_parser* parser,
5379 bool nested_name_specifier_p;
5381 /* Assume that things will not work out. */
5382 *type = error_mark_node;
5384 /* Look for the optional `::' operator. */
5385 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5386 /* Look for the optional nested-name-specifier. */
5387 nested_name_specifier_p
5388 = (cp_parser_nested_name_specifier_opt (parser,
5389 /*typename_keyword_p=*/false,
5390 /*check_dependency_p=*/true,
5392 /*is_declaration=*/false)
5394 /* Now, if we saw a nested-name-specifier, we might be doing the
5395 second production. */
5396 if (nested_name_specifier_p
5397 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5399 /* Consume the `template' keyword. */
5400 cp_lexer_consume_token (parser->lexer);
5401 /* Parse the template-id. */
5402 cp_parser_template_id (parser,
5403 /*template_keyword_p=*/true,
5404 /*check_dependency_p=*/false,
5405 /*is_declaration=*/true);
5406 /* Look for the `::' token. */
5407 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5409 /* If the next token is not a `~', then there might be some
5410 additional qualification. */
5411 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5413 /* At this point, we're looking for "type-name :: ~". The type-name
5414 must not be a class-name, since this is a pseudo-destructor. So,
5415 it must be either an enum-name, or a typedef-name -- both of which
5416 are just identifiers. So, we peek ahead to check that the "::"
5417 and "~" tokens are present; if they are not, then we can avoid
5418 calling type_name. */
5419 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5420 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5421 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5423 cp_parser_error (parser, "non-scalar type");
5427 /* Look for the type-name. */
5428 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5429 if (*scope == error_mark_node)
5432 /* Look for the `::' token. */
5433 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5438 /* Look for the `~'. */
5439 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5440 /* Look for the type-name again. We are not responsible for
5441 checking that it matches the first type-name. */
5442 *type = cp_parser_nonclass_name (parser);
5445 /* Parse a unary-expression.
5451 unary-operator cast-expression
5452 sizeof unary-expression
5460 __extension__ cast-expression
5461 __alignof__ unary-expression
5462 __alignof__ ( type-id )
5463 __real__ cast-expression
5464 __imag__ cast-expression
5467 ADDRESS_P is true iff the unary-expression is appearing as the
5468 operand of the `&' operator. CAST_P is true if this expression is
5469 the target of a cast.
5471 Returns a representation of the expression. */
5474 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5478 enum tree_code unary_operator;
5480 /* Peek at the next token. */
5481 token = cp_lexer_peek_token (parser->lexer);
5482 /* Some keywords give away the kind of expression. */
5483 if (token->type == CPP_KEYWORD)
5485 enum rid keyword = token->keyword;
5495 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5496 /* Consume the token. */
5497 cp_lexer_consume_token (parser->lexer);
5498 /* Parse the operand. */
5499 operand = cp_parser_sizeof_operand (parser, keyword);
5501 if (TYPE_P (operand))
5502 return cxx_sizeof_or_alignof_type (operand, op, true);
5504 return cxx_sizeof_or_alignof_expr (operand, op, true);
5508 return cp_parser_new_expression (parser);
5511 return cp_parser_delete_expression (parser);
5515 /* The saved value of the PEDANTIC flag. */
5519 /* Save away the PEDANTIC flag. */
5520 cp_parser_extension_opt (parser, &saved_pedantic);
5521 /* Parse the cast-expression. */
5522 expr = cp_parser_simple_cast_expression (parser);
5523 /* Restore the PEDANTIC flag. */
5524 pedantic = saved_pedantic;
5534 /* Consume the `__real__' or `__imag__' token. */
5535 cp_lexer_consume_token (parser->lexer);
5536 /* Parse the cast-expression. */
5537 expression = cp_parser_simple_cast_expression (parser);
5538 /* Create the complete representation. */
5539 return build_x_unary_op ((keyword == RID_REALPART
5540 ? REALPART_EXPR : IMAGPART_EXPR),
5542 tf_warning_or_error);
5551 /* Look for the `:: new' and `:: delete', which also signal the
5552 beginning of a new-expression, or delete-expression,
5553 respectively. If the next token is `::', then it might be one of
5555 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5559 /* See if the token after the `::' is one of the keywords in
5560 which we're interested. */
5561 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5562 /* If it's `new', we have a new-expression. */
5563 if (keyword == RID_NEW)
5564 return cp_parser_new_expression (parser);
5565 /* Similarly, for `delete'. */
5566 else if (keyword == RID_DELETE)
5567 return cp_parser_delete_expression (parser);
5570 /* Look for a unary operator. */
5571 unary_operator = cp_parser_unary_operator (token);
5572 /* The `++' and `--' operators can be handled similarly, even though
5573 they are not technically unary-operators in the grammar. */
5574 if (unary_operator == ERROR_MARK)
5576 if (token->type == CPP_PLUS_PLUS)
5577 unary_operator = PREINCREMENT_EXPR;
5578 else if (token->type == CPP_MINUS_MINUS)
5579 unary_operator = PREDECREMENT_EXPR;
5580 /* Handle the GNU address-of-label extension. */
5581 else if (cp_parser_allow_gnu_extensions_p (parser)
5582 && token->type == CPP_AND_AND)
5586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5588 /* Consume the '&&' token. */
5589 cp_lexer_consume_token (parser->lexer);
5590 /* Look for the identifier. */
5591 identifier = cp_parser_identifier (parser);
5592 /* Create an expression representing the address. */
5593 expression = finish_label_address_expr (identifier, loc);
5594 if (cp_parser_non_integral_constant_expression (parser,
5595 "the address of a label"))
5596 expression = error_mark_node;
5600 if (unary_operator != ERROR_MARK)
5602 tree cast_expression;
5603 tree expression = error_mark_node;
5604 const char *non_constant_p = NULL;
5606 /* Consume the operator token. */
5607 token = cp_lexer_consume_token (parser->lexer);
5608 /* Parse the cast-expression. */
5610 = cp_parser_cast_expression (parser,
5611 unary_operator == ADDR_EXPR,
5612 /*cast_p=*/false, pidk);
5613 /* Now, build an appropriate representation. */
5614 switch (unary_operator)
5617 non_constant_p = "%<*%>";
5618 expression = build_x_indirect_ref (cast_expression, "unary *",
5619 tf_warning_or_error);
5623 non_constant_p = "%<&%>";
5626 expression = build_x_unary_op (unary_operator, cast_expression,
5627 tf_warning_or_error);
5630 case PREINCREMENT_EXPR:
5631 case PREDECREMENT_EXPR:
5632 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5633 ? "%<++%>" : "%<--%>");
5635 case UNARY_PLUS_EXPR:
5637 case TRUTH_NOT_EXPR:
5638 expression = finish_unary_op_expr (unary_operator, cast_expression);
5646 && cp_parser_non_integral_constant_expression (parser,
5648 expression = error_mark_node;
5653 return cp_parser_postfix_expression (parser, address_p, cast_p,
5654 /*member_access_only_p=*/false,
5658 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5659 unary-operator, the corresponding tree code is returned. */
5661 static enum tree_code
5662 cp_parser_unary_operator (cp_token* token)
5664 switch (token->type)
5667 return INDIRECT_REF;
5673 return UNARY_PLUS_EXPR;
5679 return TRUTH_NOT_EXPR;
5682 return BIT_NOT_EXPR;
5689 /* Parse a new-expression.
5692 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5693 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5695 Returns a representation of the expression. */
5698 cp_parser_new_expression (cp_parser* parser)
5700 bool global_scope_p;
5701 VEC(tree,gc) *placement;
5703 VEC(tree,gc) *initializer;
5707 /* Look for the optional `::' operator. */
5709 = (cp_parser_global_scope_opt (parser,
5710 /*current_scope_valid_p=*/false)
5712 /* Look for the `new' operator. */
5713 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5714 /* There's no easy way to tell a new-placement from the
5715 `( type-id )' construct. */
5716 cp_parser_parse_tentatively (parser);
5717 /* Look for a new-placement. */
5718 placement = cp_parser_new_placement (parser);
5719 /* If that didn't work out, there's no new-placement. */
5720 if (!cp_parser_parse_definitely (parser))
5722 if (placement != NULL)
5723 release_tree_vector (placement);
5727 /* If the next token is a `(', then we have a parenthesized
5729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5732 /* Consume the `('. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Parse the type-id. */
5735 type = cp_parser_type_id (parser);
5736 /* Look for the closing `)'. */
5737 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5738 token = cp_lexer_peek_token (parser->lexer);
5739 /* There should not be a direct-new-declarator in this production,
5740 but GCC used to allowed this, so we check and emit a sensible error
5741 message for this case. */
5742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5744 error_at (token->location,
5745 "array bound forbidden after parenthesized type-id");
5746 inform (token->location,
5747 "try removing the parentheses around the type-id");
5748 cp_parser_direct_new_declarator (parser);
5752 /* Otherwise, there must be a new-type-id. */
5754 type = cp_parser_new_type_id (parser, &nelts);
5756 /* If the next token is a `(' or '{', then we have a new-initializer. */
5757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5758 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5759 initializer = cp_parser_new_initializer (parser);
5763 /* A new-expression may not appear in an integral constant
5765 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5766 ret = error_mark_node;
5769 /* Create a representation of the new-expression. */
5770 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5771 tf_warning_or_error);
5774 if (placement != NULL)
5775 release_tree_vector (placement);
5776 if (initializer != NULL)
5777 release_tree_vector (initializer);
5782 /* Parse a new-placement.
5787 Returns the same representation as for an expression-list. */
5789 static VEC(tree,gc) *
5790 cp_parser_new_placement (cp_parser* parser)
5792 VEC(tree,gc) *expression_list;
5794 /* Parse the expression-list. */
5795 expression_list = (cp_parser_parenthesized_expression_list
5796 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5797 /*non_constant_p=*/NULL));
5799 return expression_list;
5802 /* Parse a new-type-id.
5805 type-specifier-seq new-declarator [opt]
5807 Returns the TYPE allocated. If the new-type-id indicates an array
5808 type, *NELTS is set to the number of elements in the last array
5809 bound; the TYPE will not include the last array bound. */
5812 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5814 cp_decl_specifier_seq type_specifier_seq;
5815 cp_declarator *new_declarator;
5816 cp_declarator *declarator;
5817 cp_declarator *outer_declarator;
5818 const char *saved_message;
5821 /* The type-specifier sequence must not contain type definitions.
5822 (It cannot contain declarations of new types either, but if they
5823 are not definitions we will catch that because they are not
5825 saved_message = parser->type_definition_forbidden_message;
5826 parser->type_definition_forbidden_message
5827 = "types may not be defined in a new-type-id";
5828 /* Parse the type-specifier-seq. */
5829 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5830 /*is_trailing_return=*/false,
5831 &type_specifier_seq);
5832 /* Restore the old message. */
5833 parser->type_definition_forbidden_message = saved_message;
5834 /* Parse the new-declarator. */
5835 new_declarator = cp_parser_new_declarator_opt (parser);
5837 /* Determine the number of elements in the last array dimension, if
5840 /* Skip down to the last array dimension. */
5841 declarator = new_declarator;
5842 outer_declarator = NULL;
5843 while (declarator && (declarator->kind == cdk_pointer
5844 || declarator->kind == cdk_ptrmem))
5846 outer_declarator = declarator;
5847 declarator = declarator->declarator;
5850 && declarator->kind == cdk_array
5851 && declarator->declarator
5852 && declarator->declarator->kind == cdk_array)
5854 outer_declarator = declarator;
5855 declarator = declarator->declarator;
5858 if (declarator && declarator->kind == cdk_array)
5860 *nelts = declarator->u.array.bounds;
5861 if (*nelts == error_mark_node)
5862 *nelts = integer_one_node;
5864 if (outer_declarator)
5865 outer_declarator->declarator = declarator->declarator;
5867 new_declarator = NULL;
5870 type = groktypename (&type_specifier_seq, new_declarator, false);
5874 /* Parse an (optional) new-declarator.
5877 ptr-operator new-declarator [opt]
5878 direct-new-declarator
5880 Returns the declarator. */
5882 static cp_declarator *
5883 cp_parser_new_declarator_opt (cp_parser* parser)
5885 enum tree_code code;
5887 cp_cv_quals cv_quals;
5889 /* We don't know if there's a ptr-operator next, or not. */
5890 cp_parser_parse_tentatively (parser);
5891 /* Look for a ptr-operator. */
5892 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5893 /* If that worked, look for more new-declarators. */
5894 if (cp_parser_parse_definitely (parser))
5896 cp_declarator *declarator;
5898 /* Parse another optional declarator. */
5899 declarator = cp_parser_new_declarator_opt (parser);
5901 return cp_parser_make_indirect_declarator
5902 (code, type, cv_quals, declarator);
5905 /* If the next token is a `[', there is a direct-new-declarator. */
5906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5907 return cp_parser_direct_new_declarator (parser);
5912 /* Parse a direct-new-declarator.
5914 direct-new-declarator:
5916 direct-new-declarator [constant-expression]
5920 static cp_declarator *
5921 cp_parser_direct_new_declarator (cp_parser* parser)
5923 cp_declarator *declarator = NULL;
5929 /* Look for the opening `['. */
5930 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5931 /* The first expression is not required to be constant. */
5934 cp_token *token = cp_lexer_peek_token (parser->lexer);
5935 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5936 /* The standard requires that the expression have integral
5937 type. DR 74 adds enumeration types. We believe that the
5938 real intent is that these expressions be handled like the
5939 expression in a `switch' condition, which also allows
5940 classes with a single conversion to integral or
5941 enumeration type. */
5942 if (!processing_template_decl)
5945 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5950 error_at (token->location,
5951 "expression in new-declarator must have integral "
5952 "or enumeration type");
5953 expression = error_mark_node;
5957 /* But all the other expressions must be. */
5960 = cp_parser_constant_expression (parser,
5961 /*allow_non_constant=*/false,
5963 /* Look for the closing `]'. */
5964 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5966 /* Add this bound to the declarator. */
5967 declarator = make_array_declarator (declarator, expression);
5969 /* If the next token is not a `[', then there are no more
5971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5978 /* Parse a new-initializer.
5981 ( expression-list [opt] )
5984 Returns a representation of the expression-list. */
5986 static VEC(tree,gc) *
5987 cp_parser_new_initializer (cp_parser* parser)
5989 VEC(tree,gc) *expression_list;
5991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5994 bool expr_non_constant_p;
5995 maybe_warn_cpp0x ("extended initializer lists");
5996 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5997 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5998 expression_list = make_tree_vector_single (t);
6001 expression_list = (cp_parser_parenthesized_expression_list
6002 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6003 /*non_constant_p=*/NULL));
6005 return expression_list;
6008 /* Parse a delete-expression.
6011 :: [opt] delete cast-expression
6012 :: [opt] delete [ ] cast-expression
6014 Returns a representation of the expression. */
6017 cp_parser_delete_expression (cp_parser* parser)
6019 bool global_scope_p;
6023 /* Look for the optional `::' operator. */
6025 = (cp_parser_global_scope_opt (parser,
6026 /*current_scope_valid_p=*/false)
6028 /* Look for the `delete' keyword. */
6029 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6030 /* See if the array syntax is in use. */
6031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6033 /* Consume the `[' token. */
6034 cp_lexer_consume_token (parser->lexer);
6035 /* Look for the `]' token. */
6036 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6037 /* Remember that this is the `[]' construct. */
6043 /* Parse the cast-expression. */
6044 expression = cp_parser_simple_cast_expression (parser);
6046 /* A delete-expression may not appear in an integral constant
6048 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6049 return error_mark_node;
6051 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6054 /* Returns true if TOKEN may start a cast-expression and false
6058 cp_parser_token_starts_cast_expression (cp_token *token)
6060 switch (token->type)
6066 case CPP_CLOSE_SQUARE:
6067 case CPP_CLOSE_PAREN:
6068 case CPP_CLOSE_BRACE:
6072 case CPP_DEREF_STAR:
6080 case CPP_GREATER_EQ:
6100 /* '[' may start a primary-expression in obj-c++. */
6101 case CPP_OPEN_SQUARE:
6102 return c_dialect_objc ();
6109 /* Parse a cast-expression.
6113 ( type-id ) cast-expression
6115 ADDRESS_P is true iff the unary-expression is appearing as the
6116 operand of the `&' operator. CAST_P is true if this expression is
6117 the target of a cast.
6119 Returns a representation of the expression. */
6122 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6125 /* If it's a `(', then we might be looking at a cast. */
6126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6128 tree type = NULL_TREE;
6129 tree expr = NULL_TREE;
6130 bool compound_literal_p;
6131 const char *saved_message;
6133 /* There's no way to know yet whether or not this is a cast.
6134 For example, `(int (3))' is a unary-expression, while `(int)
6135 3' is a cast. So, we resort to parsing tentatively. */
6136 cp_parser_parse_tentatively (parser);
6137 /* Types may not be defined in a cast. */
6138 saved_message = parser->type_definition_forbidden_message;
6139 parser->type_definition_forbidden_message
6140 = "types may not be defined in casts";
6141 /* Consume the `('. */
6142 cp_lexer_consume_token (parser->lexer);
6143 /* A very tricky bit is that `(struct S) { 3 }' is a
6144 compound-literal (which we permit in C++ as an extension).
6145 But, that construct is not a cast-expression -- it is a
6146 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6147 is legal; if the compound-literal were a cast-expression,
6148 you'd need an extra set of parentheses.) But, if we parse
6149 the type-id, and it happens to be a class-specifier, then we
6150 will commit to the parse at that point, because we cannot
6151 undo the action that is done when creating a new class. So,
6152 then we cannot back up and do a postfix-expression.
6154 Therefore, we scan ahead to the closing `)', and check to see
6155 if the token after the `)' is a `{'. If so, we are not
6156 looking at a cast-expression.
6158 Save tokens so that we can put them back. */
6159 cp_lexer_save_tokens (parser->lexer);
6160 /* Skip tokens until the next token is a closing parenthesis.
6161 If we find the closing `)', and the next token is a `{', then
6162 we are looking at a compound-literal. */
6164 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6165 /*consume_paren=*/true)
6166 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6167 /* Roll back the tokens we skipped. */
6168 cp_lexer_rollback_tokens (parser->lexer);
6169 /* If we were looking at a compound-literal, simulate an error
6170 so that the call to cp_parser_parse_definitely below will
6172 if (compound_literal_p)
6173 cp_parser_simulate_error (parser);
6176 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6177 parser->in_type_id_in_expr_p = true;
6178 /* Look for the type-id. */
6179 type = cp_parser_type_id (parser);
6180 /* Look for the closing `)'. */
6181 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6182 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6185 /* Restore the saved message. */
6186 parser->type_definition_forbidden_message = saved_message;
6188 /* At this point this can only be either a cast or a
6189 parenthesized ctor such as `(T ())' that looks like a cast to
6190 function returning T. */
6191 if (!cp_parser_error_occurred (parser)
6192 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6195 cp_parser_parse_definitely (parser);
6196 expr = cp_parser_cast_expression (parser,
6197 /*address_p=*/false,
6198 /*cast_p=*/true, pidk);
6200 /* Warn about old-style casts, if so requested. */
6201 if (warn_old_style_cast
6202 && !in_system_header
6203 && !VOID_TYPE_P (type)
6204 && current_lang_name != lang_name_c)
6205 warning (OPT_Wold_style_cast, "use of old-style cast");
6207 /* Only type conversions to integral or enumeration types
6208 can be used in constant-expressions. */
6209 if (!cast_valid_in_integral_constant_expression_p (type)
6210 && (cp_parser_non_integral_constant_expression
6212 "a cast to a type other than an integral or "
6213 "enumeration type")))
6214 return error_mark_node;
6216 /* Perform the cast. */
6217 expr = build_c_cast (input_location, type, expr);
6221 cp_parser_abort_tentative_parse (parser);
6224 /* If we get here, then it's not a cast, so it must be a
6225 unary-expression. */
6226 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6229 /* Parse a binary expression of the general form:
6233 pm-expression .* cast-expression
6234 pm-expression ->* cast-expression
6236 multiplicative-expression:
6238 multiplicative-expression * pm-expression
6239 multiplicative-expression / pm-expression
6240 multiplicative-expression % pm-expression
6242 additive-expression:
6243 multiplicative-expression
6244 additive-expression + multiplicative-expression
6245 additive-expression - multiplicative-expression
6249 shift-expression << additive-expression
6250 shift-expression >> additive-expression
6252 relational-expression:
6254 relational-expression < shift-expression
6255 relational-expression > shift-expression
6256 relational-expression <= shift-expression
6257 relational-expression >= shift-expression
6261 relational-expression:
6262 relational-expression <? shift-expression
6263 relational-expression >? shift-expression
6265 equality-expression:
6266 relational-expression
6267 equality-expression == relational-expression
6268 equality-expression != relational-expression
6272 and-expression & equality-expression
6274 exclusive-or-expression:
6276 exclusive-or-expression ^ and-expression
6278 inclusive-or-expression:
6279 exclusive-or-expression
6280 inclusive-or-expression | exclusive-or-expression
6282 logical-and-expression:
6283 inclusive-or-expression
6284 logical-and-expression && inclusive-or-expression
6286 logical-or-expression:
6287 logical-and-expression
6288 logical-or-expression || logical-and-expression
6290 All these are implemented with a single function like:
6293 simple-cast-expression
6294 binary-expression <token> binary-expression
6296 CAST_P is true if this expression is the target of a cast.
6298 The binops_by_token map is used to get the tree codes for each <token> type.
6299 binary-expressions are associated according to a precedence table. */
6301 #define TOKEN_PRECEDENCE(token) \
6302 (((token->type == CPP_GREATER \
6303 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6304 && !parser->greater_than_is_operator_p) \
6305 ? PREC_NOT_OPERATOR \
6306 : binops_by_token[token->type].prec)
6309 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6310 bool no_toplevel_fold_p,
6311 enum cp_parser_prec prec,
6314 cp_parser_expression_stack stack;
6315 cp_parser_expression_stack_entry *sp = &stack[0];
6318 enum tree_code tree_type, lhs_type, rhs_type;
6319 enum cp_parser_prec new_prec, lookahead_prec;
6322 /* Parse the first expression. */
6323 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6324 lhs_type = ERROR_MARK;
6328 /* Get an operator token. */
6329 token = cp_lexer_peek_token (parser->lexer);
6331 if (warn_cxx0x_compat
6332 && token->type == CPP_RSHIFT
6333 && !parser->greater_than_is_operator_p)
6335 if (warning_at (token->location, OPT_Wc__0x_compat,
6336 "%<>>%> operator will be treated as"
6337 " two right angle brackets in C++0x"))
6338 inform (token->location,
6339 "suggest parentheses around %<>>%> expression");
6342 new_prec = TOKEN_PRECEDENCE (token);
6344 /* Popping an entry off the stack means we completed a subexpression:
6345 - either we found a token which is not an operator (`>' where it is not
6346 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6347 will happen repeatedly;
6348 - or, we found an operator which has lower priority. This is the case
6349 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6351 if (new_prec <= prec)
6360 tree_type = binops_by_token[token->type].tree_type;
6362 /* We used the operator token. */
6363 cp_lexer_consume_token (parser->lexer);
6365 /* For "false && x" or "true || x", x will never be executed;
6366 disable warnings while evaluating it. */
6367 if (tree_type == TRUTH_ANDIF_EXPR)
6368 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6369 else if (tree_type == TRUTH_ORIF_EXPR)
6370 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6372 /* Extract another operand. It may be the RHS of this expression
6373 or the LHS of a new, higher priority expression. */
6374 rhs = cp_parser_simple_cast_expression (parser);
6375 rhs_type = ERROR_MARK;
6377 /* Get another operator token. Look up its precedence to avoid
6378 building a useless (immediately popped) stack entry for common
6379 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6380 token = cp_lexer_peek_token (parser->lexer);
6381 lookahead_prec = TOKEN_PRECEDENCE (token);
6382 if (lookahead_prec > new_prec)
6384 /* ... and prepare to parse the RHS of the new, higher priority
6385 expression. Since precedence levels on the stack are
6386 monotonically increasing, we do not have to care about
6389 sp->tree_type = tree_type;
6391 sp->lhs_type = lhs_type;
6394 lhs_type = rhs_type;
6396 new_prec = lookahead_prec;
6400 lookahead_prec = new_prec;
6401 /* If the stack is not empty, we have parsed into LHS the right side
6402 (`4' in the example above) of an expression we had suspended.
6403 We can use the information on the stack to recover the LHS (`3')
6404 from the stack together with the tree code (`MULT_EXPR'), and
6405 the precedence of the higher level subexpression
6406 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6407 which will be used to actually build the additive expression. */
6410 tree_type = sp->tree_type;
6412 rhs_type = lhs_type;
6414 lhs_type = sp->lhs_type;
6417 /* Undo the disabling of warnings done above. */
6418 if (tree_type == TRUTH_ANDIF_EXPR)
6419 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6420 else if (tree_type == TRUTH_ORIF_EXPR)
6421 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6423 overloaded_p = false;
6424 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6425 ERROR_MARK for everything that is not a binary expression.
6426 This makes warn_about_parentheses miss some warnings that
6427 involve unary operators. For unary expressions we should
6428 pass the correct tree_code unless the unary expression was
6429 surrounded by parentheses.
6431 if (no_toplevel_fold_p
6432 && lookahead_prec <= prec
6434 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6435 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6437 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6438 &overloaded_p, tf_warning_or_error);
6439 lhs_type = tree_type;
6441 /* If the binary operator required the use of an overloaded operator,
6442 then this expression cannot be an integral constant-expression.
6443 An overloaded operator can be used even if both operands are
6444 otherwise permissible in an integral constant-expression if at
6445 least one of the operands is of enumeration type. */
6448 && (cp_parser_non_integral_constant_expression
6449 (parser, "calls to overloaded operators")))
6450 return error_mark_node;
6457 /* Parse the `? expression : assignment-expression' part of a
6458 conditional-expression. The LOGICAL_OR_EXPR is the
6459 logical-or-expression that started the conditional-expression.
6460 Returns a representation of the entire conditional-expression.
6462 This routine is used by cp_parser_assignment_expression.
6464 ? expression : assignment-expression
6468 ? : assignment-expression */
6471 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6474 tree assignment_expr;
6476 /* Consume the `?' token. */
6477 cp_lexer_consume_token (parser->lexer);
6478 if (cp_parser_allow_gnu_extensions_p (parser)
6479 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6481 /* Implicit true clause. */
6483 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6487 /* Parse the expression. */
6488 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6489 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6490 c_inhibit_evaluation_warnings +=
6491 ((logical_or_expr == truthvalue_true_node)
6492 - (logical_or_expr == truthvalue_false_node));
6495 /* The next token should be a `:'. */
6496 cp_parser_require (parser, CPP_COLON, "%<:%>");
6497 /* Parse the assignment-expression. */
6498 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6499 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6501 /* Build the conditional-expression. */
6502 return build_x_conditional_expr (logical_or_expr,
6505 tf_warning_or_error);
6508 /* Parse an assignment-expression.
6510 assignment-expression:
6511 conditional-expression
6512 logical-or-expression assignment-operator assignment_expression
6515 CAST_P is true if this expression is the target of a cast.
6517 Returns a representation for the expression. */
6520 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6525 /* If the next token is the `throw' keyword, then we're looking at
6526 a throw-expression. */
6527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6528 expr = cp_parser_throw_expression (parser);
6529 /* Otherwise, it must be that we are looking at a
6530 logical-or-expression. */
6533 /* Parse the binary expressions (logical-or-expression). */
6534 expr = cp_parser_binary_expression (parser, cast_p, false,
6535 PREC_NOT_OPERATOR, pidk);
6536 /* If the next token is a `?' then we're actually looking at a
6537 conditional-expression. */
6538 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6539 return cp_parser_question_colon_clause (parser, expr);
6542 enum tree_code assignment_operator;
6544 /* If it's an assignment-operator, we're using the second
6547 = cp_parser_assignment_operator_opt (parser);
6548 if (assignment_operator != ERROR_MARK)
6550 bool non_constant_p;
6552 /* Parse the right-hand side of the assignment. */
6553 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6555 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6556 maybe_warn_cpp0x ("extended initializer lists");
6558 /* An assignment may not appear in a
6559 constant-expression. */
6560 if (cp_parser_non_integral_constant_expression (parser,
6562 return error_mark_node;
6563 /* Build the assignment expression. */
6564 expr = build_x_modify_expr (expr,
6565 assignment_operator,
6567 tf_warning_or_error);
6575 /* Parse an (optional) assignment-operator.
6577 assignment-operator: one of
6578 = *= /= %= += -= >>= <<= &= ^= |=
6582 assignment-operator: one of
6585 If the next token is an assignment operator, the corresponding tree
6586 code is returned, and the token is consumed. For example, for
6587 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6588 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6589 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6590 operator, ERROR_MARK is returned. */
6592 static enum tree_code
6593 cp_parser_assignment_operator_opt (cp_parser* parser)
6598 /* Peek at the next token. */
6599 token = cp_lexer_peek_token (parser->lexer);
6601 switch (token->type)
6612 op = TRUNC_DIV_EXPR;
6616 op = TRUNC_MOD_EXPR;
6648 /* Nothing else is an assignment operator. */
6652 /* If it was an assignment operator, consume it. */
6653 if (op != ERROR_MARK)
6654 cp_lexer_consume_token (parser->lexer);
6659 /* Parse an expression.
6662 assignment-expression
6663 expression , assignment-expression
6665 CAST_P is true if this expression is the target of a cast.
6667 Returns a representation of the expression. */
6670 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6672 tree expression = NULL_TREE;
6676 tree assignment_expression;
6678 /* Parse the next assignment-expression. */
6679 assignment_expression
6680 = cp_parser_assignment_expression (parser, cast_p, pidk);
6681 /* If this is the first assignment-expression, we can just
6684 expression = assignment_expression;
6686 expression = build_x_compound_expr (expression,
6687 assignment_expression,
6688 tf_warning_or_error);
6689 /* If the next token is not a comma, then we are done with the
6691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6693 /* Consume the `,'. */
6694 cp_lexer_consume_token (parser->lexer);
6695 /* A comma operator cannot appear in a constant-expression. */
6696 if (cp_parser_non_integral_constant_expression (parser,
6697 "a comma operator"))
6698 expression = error_mark_node;
6704 /* Parse a constant-expression.
6706 constant-expression:
6707 conditional-expression
6709 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6710 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6711 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6712 is false, NON_CONSTANT_P should be NULL. */
6715 cp_parser_constant_expression (cp_parser* parser,
6716 bool allow_non_constant_p,
6717 bool *non_constant_p)
6719 bool saved_integral_constant_expression_p;
6720 bool saved_allow_non_integral_constant_expression_p;
6721 bool saved_non_integral_constant_expression_p;
6724 /* It might seem that we could simply parse the
6725 conditional-expression, and then check to see if it were
6726 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6727 one that the compiler can figure out is constant, possibly after
6728 doing some simplifications or optimizations. The standard has a
6729 precise definition of constant-expression, and we must honor
6730 that, even though it is somewhat more restrictive.
6736 is not a legal declaration, because `(2, 3)' is not a
6737 constant-expression. The `,' operator is forbidden in a
6738 constant-expression. However, GCC's constant-folding machinery
6739 will fold this operation to an INTEGER_CST for `3'. */
6741 /* Save the old settings. */
6742 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6743 saved_allow_non_integral_constant_expression_p
6744 = parser->allow_non_integral_constant_expression_p;
6745 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6746 /* We are now parsing a constant-expression. */
6747 parser->integral_constant_expression_p = true;
6748 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6749 parser->non_integral_constant_expression_p = false;
6750 /* Although the grammar says "conditional-expression", we parse an
6751 "assignment-expression", which also permits "throw-expression"
6752 and the use of assignment operators. In the case that
6753 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6754 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6755 actually essential that we look for an assignment-expression.
6756 For example, cp_parser_initializer_clauses uses this function to
6757 determine whether a particular assignment-expression is in fact
6759 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6760 /* Restore the old settings. */
6761 parser->integral_constant_expression_p
6762 = saved_integral_constant_expression_p;
6763 parser->allow_non_integral_constant_expression_p
6764 = saved_allow_non_integral_constant_expression_p;
6765 if (allow_non_constant_p)
6766 *non_constant_p = parser->non_integral_constant_expression_p;
6767 else if (parser->non_integral_constant_expression_p)
6768 expression = error_mark_node;
6769 parser->non_integral_constant_expression_p
6770 = saved_non_integral_constant_expression_p;
6775 /* Parse __builtin_offsetof.
6777 offsetof-expression:
6778 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6780 offsetof-member-designator:
6782 | offsetof-member-designator "." id-expression
6783 | offsetof-member-designator "[" expression "]"
6784 | offsetof-member-designator "->" id-expression */
6787 cp_parser_builtin_offsetof (cp_parser *parser)
6789 int save_ice_p, save_non_ice_p;
6794 /* We're about to accept non-integral-constant things, but will
6795 definitely yield an integral constant expression. Save and
6796 restore these values around our local parsing. */
6797 save_ice_p = parser->integral_constant_expression_p;
6798 save_non_ice_p = parser->non_integral_constant_expression_p;
6800 /* Consume the "__builtin_offsetof" token. */
6801 cp_lexer_consume_token (parser->lexer);
6802 /* Consume the opening `('. */
6803 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6804 /* Parse the type-id. */
6805 type = cp_parser_type_id (parser);
6806 /* Look for the `,'. */
6807 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6808 token = cp_lexer_peek_token (parser->lexer);
6810 /* Build the (type *)null that begins the traditional offsetof macro. */
6811 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6812 tf_warning_or_error);
6814 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6815 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6816 true, &dummy, token->location);
6819 token = cp_lexer_peek_token (parser->lexer);
6820 switch (token->type)
6822 case CPP_OPEN_SQUARE:
6823 /* offsetof-member-designator "[" expression "]" */
6824 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6828 /* offsetof-member-designator "->" identifier */
6829 expr = grok_array_decl (expr, integer_zero_node);
6833 /* offsetof-member-designator "." identifier */
6834 cp_lexer_consume_token (parser->lexer);
6835 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6840 case CPP_CLOSE_PAREN:
6841 /* Consume the ")" token. */
6842 cp_lexer_consume_token (parser->lexer);
6846 /* Error. We know the following require will fail, but
6847 that gives the proper error message. */
6848 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6849 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6850 expr = error_mark_node;
6856 /* If we're processing a template, we can't finish the semantics yet.
6857 Otherwise we can fold the entire expression now. */
6858 if (processing_template_decl)
6859 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6861 expr = finish_offsetof (expr);
6864 parser->integral_constant_expression_p = save_ice_p;
6865 parser->non_integral_constant_expression_p = save_non_ice_p;
6870 /* Parse a trait expression. */
6873 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6876 tree type1, type2 = NULL_TREE;
6877 bool binary = false;
6878 cp_decl_specifier_seq decl_specs;
6882 case RID_HAS_NOTHROW_ASSIGN:
6883 kind = CPTK_HAS_NOTHROW_ASSIGN;
6885 case RID_HAS_NOTHROW_CONSTRUCTOR:
6886 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6888 case RID_HAS_NOTHROW_COPY:
6889 kind = CPTK_HAS_NOTHROW_COPY;
6891 case RID_HAS_TRIVIAL_ASSIGN:
6892 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6894 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6895 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6897 case RID_HAS_TRIVIAL_COPY:
6898 kind = CPTK_HAS_TRIVIAL_COPY;
6900 case RID_HAS_TRIVIAL_DESTRUCTOR:
6901 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6903 case RID_HAS_VIRTUAL_DESTRUCTOR:
6904 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6906 case RID_IS_ABSTRACT:
6907 kind = CPTK_IS_ABSTRACT;
6909 case RID_IS_BASE_OF:
6910 kind = CPTK_IS_BASE_OF;
6914 kind = CPTK_IS_CLASS;
6916 case RID_IS_CONVERTIBLE_TO:
6917 kind = CPTK_IS_CONVERTIBLE_TO;
6921 kind = CPTK_IS_EMPTY;
6924 kind = CPTK_IS_ENUM;
6929 case RID_IS_POLYMORPHIC:
6930 kind = CPTK_IS_POLYMORPHIC;
6932 case RID_IS_STD_LAYOUT:
6933 kind = CPTK_IS_STD_LAYOUT;
6935 case RID_IS_TRIVIAL:
6936 kind = CPTK_IS_TRIVIAL;
6939 kind = CPTK_IS_UNION;
6945 /* Consume the token. */
6946 cp_lexer_consume_token (parser->lexer);
6948 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6950 type1 = cp_parser_type_id (parser);
6952 if (type1 == error_mark_node)
6953 return error_mark_node;
6955 /* Build a trivial decl-specifier-seq. */
6956 clear_decl_specs (&decl_specs);
6957 decl_specs.type = type1;
6959 /* Call grokdeclarator to figure out what type this is. */
6960 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6961 /*initialized=*/0, /*attrlist=*/NULL);
6965 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6967 type2 = cp_parser_type_id (parser);
6969 if (type2 == error_mark_node)
6970 return error_mark_node;
6972 /* Build a trivial decl-specifier-seq. */
6973 clear_decl_specs (&decl_specs);
6974 decl_specs.type = type2;
6976 /* Call grokdeclarator to figure out what type this is. */
6977 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6978 /*initialized=*/0, /*attrlist=*/NULL);
6981 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6983 /* Complete the trait expression, which may mean either processing
6984 the trait expr now or saving it for template instantiation. */
6985 return finish_trait_expr (kind, type1, type2);
6988 /* Lambdas that appear in variable initializer or default argument scope
6989 get that in their mangling, so we need to record it. We might as well
6990 use the count for function and namespace scopes as well. */
6991 static GTY(()) tree lambda_scope;
6992 static GTY(()) int lambda_count;
6993 typedef struct GTY(()) tree_int
6998 DEF_VEC_O(tree_int);
6999 DEF_VEC_ALLOC_O(tree_int,gc);
7000 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7003 start_lambda_scope (tree decl)
7007 /* Once we're inside a function, we ignore other scopes and just push
7008 the function again so that popping works properly. */
7009 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7010 decl = current_function_decl;
7011 ti.t = lambda_scope;
7012 ti.i = lambda_count;
7013 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7014 if (lambda_scope != decl)
7016 /* Don't reset the count if we're still in the same function. */
7017 lambda_scope = decl;
7023 record_lambda_scope (tree lambda)
7025 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7026 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7030 finish_lambda_scope (void)
7032 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7033 if (lambda_scope != p->t)
7035 lambda_scope = p->t;
7036 lambda_count = p->i;
7038 VEC_pop (tree_int, lambda_scope_stack);
7041 /* Parse a lambda expression.
7044 lambda-introducer lambda-declarator [opt] compound-statement
7046 Returns a representation of the expression. */
7049 cp_parser_lambda_expression (cp_parser* parser)
7051 tree lambda_expr = build_lambda_expr ();
7054 LAMBDA_EXPR_LOCATION (lambda_expr)
7055 = cp_lexer_peek_token (parser->lexer)->location;
7057 /* We may be in the middle of deferred access check. Disable
7059 push_deferring_access_checks (dk_no_deferred);
7061 type = begin_lambda_type (lambda_expr);
7063 record_lambda_scope (lambda_expr);
7065 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7066 determine_visibility (TYPE_NAME (type));
7069 /* Inside the class, surrounding template-parameter-lists do not apply. */
7070 unsigned int saved_num_template_parameter_lists
7071 = parser->num_template_parameter_lists;
7073 parser->num_template_parameter_lists = 0;
7075 cp_parser_lambda_introducer (parser, lambda_expr);
7077 /* By virtue of defining a local class, a lambda expression has access to
7078 the private variables of enclosing classes. */
7080 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7082 cp_parser_lambda_body (parser, lambda_expr);
7084 /* The capture list was built up in reverse order; fix that now. */
7086 tree newlist = NULL_TREE;
7089 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7092 tree field = TREE_PURPOSE (elt);
7095 next = TREE_CHAIN (elt);
7096 TREE_CHAIN (elt) = newlist;
7099 /* Also add __ to the beginning of the field name so that code
7100 outside the lambda body can't see the captured name. We could
7101 just remove the name entirely, but this is more useful for
7103 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7104 /* The 'this' capture already starts with __. */
7107 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7108 buf[1] = buf[0] = '_';
7109 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7110 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7111 DECL_NAME (field) = get_identifier (buf);
7113 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7116 maybe_add_lambda_conv_op (type);
7118 type = finish_struct (type, /*attributes=*/NULL_TREE);
7120 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7123 pop_deferring_access_checks ();
7125 return build_lambda_object (lambda_expr);
7128 /* Parse the beginning of a lambda expression.
7131 [ lambda-capture [opt] ]
7133 LAMBDA_EXPR is the current representation of the lambda expression. */
7136 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7138 /* Need commas after the first capture. */
7141 /* Eat the leading `['. */
7142 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7144 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7145 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7146 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7147 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7148 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7149 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7151 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7153 cp_lexer_consume_token (parser->lexer);
7157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7159 cp_token* capture_token;
7161 tree capture_init_expr;
7162 cp_id_kind idk = CP_ID_KIND_NONE;
7163 bool explicit_init_p = false;
7165 enum capture_kind_type
7170 enum capture_kind_type capture_kind = BY_COPY;
7172 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7174 error ("expected end of capture-list");
7181 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7183 /* Possibly capture `this'. */
7184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7186 cp_lexer_consume_token (parser->lexer);
7187 add_capture (lambda_expr,
7188 /*id=*/get_identifier ("__this"),
7189 /*initializer=*/finish_this_expr(),
7190 /*by_reference_p=*/false,
7195 /* Remember whether we want to capture as a reference or not. */
7196 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7198 capture_kind = BY_REFERENCE;
7199 cp_lexer_consume_token (parser->lexer);
7202 /* Get the identifier. */
7203 capture_token = cp_lexer_peek_token (parser->lexer);
7204 capture_id = cp_parser_identifier (parser);
7206 if (capture_id == error_mark_node)
7207 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7208 delimiters, but I modified this to stop on unnested ']' as well. It
7209 was already changed to stop on unnested '}', so the
7210 "closing_parenthesis" name is no more misleading with my change. */
7212 cp_parser_skip_to_closing_parenthesis (parser,
7213 /*recovering=*/true,
7215 /*consume_paren=*/true);
7219 /* Find the initializer for this capture. */
7220 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7222 /* An explicit expression exists. */
7223 cp_lexer_consume_token (parser->lexer);
7224 pedwarn (input_location, OPT_pedantic,
7225 "ISO C++ does not allow initializers "
7226 "in lambda expression capture lists");
7227 capture_init_expr = cp_parser_assignment_expression (parser,
7230 explicit_init_p = true;
7234 const char* error_msg;
7236 /* Turn the identifier into an id-expression. */
7238 = cp_parser_lookup_name
7242 /*is_template=*/false,
7243 /*is_namespace=*/false,
7244 /*check_dependency=*/true,
7245 /*ambiguous_decls=*/NULL,
7246 capture_token->location);
7249 = finish_id_expression
7254 /*integral_constant_expression_p=*/false,
7255 /*allow_non_integral_constant_expression_p=*/false,
7256 /*non_integral_constant_expression_p=*/NULL,
7257 /*template_p=*/false,
7259 /*address_p=*/false,
7260 /*template_arg_p=*/false,
7262 capture_token->location);
7265 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7267 = unqualified_name_lookup_error (capture_init_expr);
7269 add_capture (lambda_expr,
7272 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7276 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7279 /* Parse the (optional) middle of a lambda expression.
7282 ( parameter-declaration-clause [opt] )
7283 attribute-specifier [opt]
7285 exception-specification [opt]
7286 lambda-return-type-clause [opt]
7288 LAMBDA_EXPR is the current representation of the lambda expression. */
7291 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7293 /* 5.1.1.4 of the standard says:
7294 If a lambda-expression does not include a lambda-declarator, it is as if
7295 the lambda-declarator were ().
7296 This means an empty parameter list, no attributes, and no exception
7298 tree param_list = void_list_node;
7299 tree attributes = NULL_TREE;
7300 tree exception_spec = NULL_TREE;
7303 /* The lambda-declarator is optional, but must begin with an opening
7304 parenthesis if present. */
7305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7307 cp_lexer_consume_token (parser->lexer);
7309 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7311 /* Parse parameters. */
7312 param_list = cp_parser_parameter_declaration_clause (parser);
7314 /* Default arguments shall not be specified in the
7315 parameter-declaration-clause of a lambda-declarator. */
7316 for (t = param_list; t; t = TREE_CHAIN (t))
7317 if (TREE_PURPOSE (t))
7318 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7319 "default argument specified for lambda parameter");
7321 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7323 attributes = cp_parser_attributes_opt (parser);
7325 /* Parse optional `mutable' keyword. */
7326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7328 cp_lexer_consume_token (parser->lexer);
7329 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7332 /* Parse optional exception specification. */
7333 exception_spec = cp_parser_exception_specification_opt (parser);
7335 /* Parse optional trailing return type. */
7336 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7338 cp_lexer_consume_token (parser->lexer);
7339 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7342 /* The function parameters must be in scope all the way until after the
7343 trailing-return-type in case of decltype. */
7344 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7345 pop_binding (DECL_NAME (t), t);
7350 /* Create the function call operator.
7352 Messing with declarators like this is no uglier than building up the
7353 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7356 cp_decl_specifier_seq return_type_specs;
7357 cp_declarator* declarator;
7362 clear_decl_specs (&return_type_specs);
7363 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7364 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7366 /* Maybe we will deduce the return type later, but we can use void
7367 as a placeholder return type anyways. */
7368 return_type_specs.type = void_type_node;
7370 p = obstack_alloc (&declarator_obstack, 0);
7372 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7375 quals = TYPE_UNQUALIFIED;
7376 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7377 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7379 /* A lambda with no captures has a static op() and a conversion op
7380 to function type. */
7381 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7382 error ("lambda expression with no captures declared mutable");
7383 return_type_specs.storage_class = sc_static;
7385 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7386 quals = TYPE_QUAL_CONST;
7387 declarator = make_call_declarator (declarator, param_list, quals,
7389 /*late_return_type=*/NULL_TREE);
7391 fco = grokmethod (&return_type_specs,
7394 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7395 DECL_ARTIFICIAL (fco) = 1;
7397 finish_member_declaration (fco);
7399 obstack_free (&declarator_obstack, p);
7403 /* Parse the body of a lambda expression, which is simply
7407 but which requires special handling.
7408 LAMBDA_EXPR is the current representation of the lambda expression. */
7411 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7413 bool nested = (current_function_decl != NULL_TREE);
7415 push_function_context ();
7417 /* Finish the function call operator
7419 + late_parsing_for_member
7420 + function_definition_after_declarator
7421 + ctor_initializer_opt_and_function_body */
7423 tree fco = lambda_function (lambda_expr);
7427 /* Let the front end know that we are going to be defining this
7429 start_preparsed_function (fco,
7431 SF_PRE_PARSED | SF_INCLASS_INLINE);
7433 start_lambda_scope (fco);
7434 body = begin_function_body ();
7436 /* 5.1.1.4 of the standard says:
7437 If a lambda-expression does not include a trailing-return-type, it
7438 is as if the trailing-return-type denotes the following type:
7439 * if the compound-statement is of the form
7440 { return attribute-specifier [opt] expression ; }
7441 the type of the returned expression after lvalue-to-rvalue
7442 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7443 (_conv.array_ 4.2), and function-to-pointer conversion
7445 * otherwise, void. */
7447 /* In a lambda that has neither a lambda-return-type-clause
7448 nor a deducible form, errors should be reported for return statements
7449 in the body. Since we used void as the placeholder return type, parsing
7450 the body as usual will give such desired behavior. */
7451 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7452 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7453 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7454 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7457 tree expr = NULL_TREE;
7458 cp_id_kind idk = CP_ID_KIND_NONE;
7460 /* Parse tentatively in case there's more after the initial return
7462 cp_parser_parse_tentatively (parser);
7464 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7465 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7467 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7469 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7470 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7472 if (cp_parser_parse_definitely (parser))
7474 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7476 compound_stmt = begin_compound_stmt (0);
7477 /* Will get error here if type not deduced yet. */
7478 finish_return_stmt (expr);
7479 finish_compound_stmt (compound_stmt);
7487 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7488 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7489 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7490 cp_parser_compound_stmt does not pass it. */
7491 cp_parser_function_body (parser);
7492 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7495 finish_function_body (body);
7496 finish_lambda_scope ();
7498 /* Finish the function and generate code for it if necessary. */
7499 expand_or_defer_fn (finish_function (/*inline*/2));
7503 pop_function_context();
7506 /* Statements [gram.stmt.stmt] */
7508 /* Parse a statement.
7512 expression-statement
7517 declaration-statement
7520 IN_COMPOUND is true when the statement is nested inside a
7521 cp_parser_compound_statement; this matters for certain pragmas.
7523 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7524 is a (possibly labeled) if statement which is not enclosed in braces
7525 and has an else clause. This is used to implement -Wparentheses. */
7528 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7529 bool in_compound, bool *if_p)
7533 location_t statement_location;
7538 /* There is no statement yet. */
7539 statement = NULL_TREE;
7540 /* Peek at the next token. */
7541 token = cp_lexer_peek_token (parser->lexer);
7542 /* Remember the location of the first token in the statement. */
7543 statement_location = token->location;
7544 /* If this is a keyword, then that will often determine what kind of
7545 statement we have. */
7546 if (token->type == CPP_KEYWORD)
7548 enum rid keyword = token->keyword;
7554 /* Looks like a labeled-statement with a case label.
7555 Parse the label, and then use tail recursion to parse
7557 cp_parser_label_for_labeled_statement (parser);
7562 statement = cp_parser_selection_statement (parser, if_p);
7568 statement = cp_parser_iteration_statement (parser);
7575 statement = cp_parser_jump_statement (parser);
7578 /* Objective-C++ exception-handling constructs. */
7581 case RID_AT_FINALLY:
7582 case RID_AT_SYNCHRONIZED:
7584 statement = cp_parser_objc_statement (parser);
7588 statement = cp_parser_try_block (parser);
7592 /* This must be a namespace alias definition. */
7593 cp_parser_declaration_statement (parser);
7597 /* It might be a keyword like `int' that can start a
7598 declaration-statement. */
7602 else if (token->type == CPP_NAME)
7604 /* If the next token is a `:', then we are looking at a
7605 labeled-statement. */
7606 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7607 if (token->type == CPP_COLON)
7609 /* Looks like a labeled-statement with an ordinary label.
7610 Parse the label, and then use tail recursion to parse
7612 cp_parser_label_for_labeled_statement (parser);
7616 /* Anything that starts with a `{' must be a compound-statement. */
7617 else if (token->type == CPP_OPEN_BRACE)
7618 statement = cp_parser_compound_statement (parser, NULL, false);
7619 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7620 a statement all its own. */
7621 else if (token->type == CPP_PRAGMA)
7623 /* Only certain OpenMP pragmas are attached to statements, and thus
7624 are considered statements themselves. All others are not. In
7625 the context of a compound, accept the pragma as a "statement" and
7626 return so that we can check for a close brace. Otherwise we
7627 require a real statement and must go back and read one. */
7629 cp_parser_pragma (parser, pragma_compound);
7630 else if (!cp_parser_pragma (parser, pragma_stmt))
7634 else if (token->type == CPP_EOF)
7636 cp_parser_error (parser, "expected statement");
7640 /* Everything else must be a declaration-statement or an
7641 expression-statement. Try for the declaration-statement
7642 first, unless we are looking at a `;', in which case we know that
7643 we have an expression-statement. */
7646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7648 cp_parser_parse_tentatively (parser);
7649 /* Try to parse the declaration-statement. */
7650 cp_parser_declaration_statement (parser);
7651 /* If that worked, we're done. */
7652 if (cp_parser_parse_definitely (parser))
7655 /* Look for an expression-statement instead. */
7656 statement = cp_parser_expression_statement (parser, in_statement_expr);
7659 /* Set the line number for the statement. */
7660 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7661 SET_EXPR_LOCATION (statement, statement_location);
7664 /* Parse the label for a labeled-statement, i.e.
7667 case constant-expression :
7671 case constant-expression ... constant-expression : statement
7673 When a label is parsed without errors, the label is added to the
7674 parse tree by the finish_* functions, so this function doesn't
7675 have to return the label. */
7678 cp_parser_label_for_labeled_statement (cp_parser* parser)
7681 tree label = NULL_TREE;
7683 /* The next token should be an identifier. */
7684 token = cp_lexer_peek_token (parser->lexer);
7685 if (token->type != CPP_NAME
7686 && token->type != CPP_KEYWORD)
7688 cp_parser_error (parser, "expected labeled-statement");
7692 switch (token->keyword)
7699 /* Consume the `case' token. */
7700 cp_lexer_consume_token (parser->lexer);
7701 /* Parse the constant-expression. */
7702 expr = cp_parser_constant_expression (parser,
7703 /*allow_non_constant_p=*/false,
7706 ellipsis = cp_lexer_peek_token (parser->lexer);
7707 if (ellipsis->type == CPP_ELLIPSIS)
7709 /* Consume the `...' token. */
7710 cp_lexer_consume_token (parser->lexer);
7712 cp_parser_constant_expression (parser,
7713 /*allow_non_constant_p=*/false,
7715 /* We don't need to emit warnings here, as the common code
7716 will do this for us. */
7719 expr_hi = NULL_TREE;
7721 if (parser->in_switch_statement_p)
7722 finish_case_label (token->location, expr, expr_hi);
7724 error_at (token->location,
7725 "case label %qE not within a switch statement",
7731 /* Consume the `default' token. */
7732 cp_lexer_consume_token (parser->lexer);
7734 if (parser->in_switch_statement_p)
7735 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7737 error_at (token->location, "case label not within a switch statement");
7741 /* Anything else must be an ordinary label. */
7742 label = finish_label_stmt (cp_parser_identifier (parser));
7746 /* Require the `:' token. */
7747 cp_parser_require (parser, CPP_COLON, "%<:%>");
7749 /* An ordinary label may optionally be followed by attributes.
7750 However, this is only permitted if the attributes are then
7751 followed by a semicolon. This is because, for backward
7752 compatibility, when parsing
7753 lab: __attribute__ ((unused)) int i;
7754 we want the attribute to attach to "i", not "lab". */
7755 if (label != NULL_TREE
7756 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7760 cp_parser_parse_tentatively (parser);
7761 attrs = cp_parser_attributes_opt (parser);
7762 if (attrs == NULL_TREE
7763 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7764 cp_parser_abort_tentative_parse (parser);
7765 else if (!cp_parser_parse_definitely (parser))
7768 cplus_decl_attributes (&label, attrs, 0);
7772 /* Parse an expression-statement.
7774 expression-statement:
7777 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7778 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7779 indicates whether this expression-statement is part of an
7780 expression statement. */
7783 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7785 tree statement = NULL_TREE;
7786 cp_token *token = cp_lexer_peek_token (parser->lexer);
7788 /* If the next token is a ';', then there is no expression
7790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7791 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7793 /* Give a helpful message for "A<T>::type t;" */
7794 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7795 && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7796 && TREE_CODE (statement) == SCOPE_REF)
7797 error_at (token->location, "need %<typename%> before %qE because "
7798 "%qT is a dependent scope",
7799 statement, TREE_OPERAND (statement, 0));
7801 /* Consume the final `;'. */
7802 cp_parser_consume_semicolon_at_end_of_statement (parser);
7804 if (in_statement_expr
7805 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7806 /* This is the final expression statement of a statement
7808 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7810 statement = finish_expr_stmt (statement);
7817 /* Parse a compound-statement.
7820 { statement-seq [opt] }
7825 { label-declaration-seq [opt] statement-seq [opt] }
7827 label-declaration-seq:
7829 label-declaration-seq label-declaration
7831 Returns a tree representing the statement. */
7834 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7839 /* Consume the `{'. */
7840 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7841 return error_mark_node;
7842 /* Begin the compound-statement. */
7843 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7844 /* If the next keyword is `__label__' we have a label declaration. */
7845 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7846 cp_parser_label_declaration (parser);
7847 /* Parse an (optional) statement-seq. */
7848 cp_parser_statement_seq_opt (parser, in_statement_expr);
7849 /* Finish the compound-statement. */
7850 finish_compound_stmt (compound_stmt);
7851 /* Consume the `}'. */
7852 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7854 return compound_stmt;
7857 /* Parse an (optional) statement-seq.
7861 statement-seq [opt] statement */
7864 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7866 /* Scan statements until there aren't any more. */
7869 cp_token *token = cp_lexer_peek_token (parser->lexer);
7871 /* If we're looking at a `}', then we've run out of statements. */
7872 if (token->type == CPP_CLOSE_BRACE
7873 || token->type == CPP_EOF
7874 || token->type == CPP_PRAGMA_EOL)
7877 /* If we are in a compound statement and find 'else' then
7878 something went wrong. */
7879 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7881 if (parser->in_statement & IN_IF_STMT)
7885 token = cp_lexer_consume_token (parser->lexer);
7886 error_at (token->location, "%<else%> without a previous %<if%>");
7890 /* Parse the statement. */
7891 cp_parser_statement (parser, in_statement_expr, true, NULL);
7895 /* Parse a selection-statement.
7897 selection-statement:
7898 if ( condition ) statement
7899 if ( condition ) statement else statement
7900 switch ( condition ) statement
7902 Returns the new IF_STMT or SWITCH_STMT.
7904 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7905 is a (possibly labeled) if statement which is not enclosed in
7906 braces and has an else clause. This is used to implement
7910 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7918 /* Peek at the next token. */
7919 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7921 /* See what kind of keyword it is. */
7922 keyword = token->keyword;
7931 /* Look for the `('. */
7932 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7934 cp_parser_skip_to_end_of_statement (parser);
7935 return error_mark_node;
7938 /* Begin the selection-statement. */
7939 if (keyword == RID_IF)
7940 statement = begin_if_stmt ();
7942 statement = begin_switch_stmt ();
7944 /* Parse the condition. */
7945 condition = cp_parser_condition (parser);
7946 /* Look for the `)'. */
7947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7948 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7949 /*consume_paren=*/true);
7951 if (keyword == RID_IF)
7954 unsigned char in_statement;
7956 /* Add the condition. */
7957 finish_if_stmt_cond (condition, statement);
7959 /* Parse the then-clause. */
7960 in_statement = parser->in_statement;
7961 parser->in_statement |= IN_IF_STMT;
7962 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7965 add_stmt (build_empty_stmt (loc));
7966 cp_lexer_consume_token (parser->lexer);
7967 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7968 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7969 "empty body in an %<if%> statement");
7973 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7974 parser->in_statement = in_statement;
7976 finish_then_clause (statement);
7978 /* If the next token is `else', parse the else-clause. */
7979 if (cp_lexer_next_token_is_keyword (parser->lexer,
7982 /* Consume the `else' keyword. */
7983 cp_lexer_consume_token (parser->lexer);
7984 begin_else_clause (statement);
7985 /* Parse the else-clause. */
7986 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7989 loc = cp_lexer_peek_token (parser->lexer)->location;
7991 OPT_Wempty_body, "suggest braces around "
7992 "empty body in an %<else%> statement");
7993 add_stmt (build_empty_stmt (loc));
7994 cp_lexer_consume_token (parser->lexer);
7997 cp_parser_implicitly_scoped_statement (parser, NULL);
7999 finish_else_clause (statement);
8001 /* If we are currently parsing a then-clause, then
8002 IF_P will not be NULL. We set it to true to
8003 indicate that this if statement has an else clause.
8004 This may trigger the Wparentheses warning below
8005 when we get back up to the parent if statement. */
8011 /* This if statement does not have an else clause. If
8012 NESTED_IF is true, then the then-clause is an if
8013 statement which does have an else clause. We warn
8014 about the potential ambiguity. */
8016 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8017 "suggest explicit braces to avoid ambiguous"
8021 /* Now we're all done with the if-statement. */
8022 finish_if_stmt (statement);
8026 bool in_switch_statement_p;
8027 unsigned char in_statement;
8029 /* Add the condition. */
8030 finish_switch_cond (condition, statement);
8032 /* Parse the body of the switch-statement. */
8033 in_switch_statement_p = parser->in_switch_statement_p;
8034 in_statement = parser->in_statement;
8035 parser->in_switch_statement_p = true;
8036 parser->in_statement |= IN_SWITCH_STMT;
8037 cp_parser_implicitly_scoped_statement (parser, NULL);
8038 parser->in_switch_statement_p = in_switch_statement_p;
8039 parser->in_statement = in_statement;
8041 /* Now we're all done with the switch-statement. */
8042 finish_switch_stmt (statement);
8050 cp_parser_error (parser, "expected selection-statement");
8051 return error_mark_node;
8055 /* Parse a condition.
8059 type-specifier-seq declarator = initializer-clause
8060 type-specifier-seq declarator braced-init-list
8065 type-specifier-seq declarator asm-specification [opt]
8066 attributes [opt] = assignment-expression
8068 Returns the expression that should be tested. */
8071 cp_parser_condition (cp_parser* parser)
8073 cp_decl_specifier_seq type_specifiers;
8074 const char *saved_message;
8076 /* Try the declaration first. */
8077 cp_parser_parse_tentatively (parser);
8078 /* New types are not allowed in the type-specifier-seq for a
8080 saved_message = parser->type_definition_forbidden_message;
8081 parser->type_definition_forbidden_message
8082 = "types may not be defined in conditions";
8083 /* Parse the type-specifier-seq. */
8084 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8085 /*is_trailing_return=*/false,
8087 /* Restore the saved message. */
8088 parser->type_definition_forbidden_message = saved_message;
8089 /* If all is well, we might be looking at a declaration. */
8090 if (!cp_parser_error_occurred (parser))
8093 tree asm_specification;
8095 cp_declarator *declarator;
8096 tree initializer = NULL_TREE;
8098 /* Parse the declarator. */
8099 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8100 /*ctor_dtor_or_conv_p=*/NULL,
8101 /*parenthesized_p=*/NULL,
8102 /*member_p=*/false);
8103 /* Parse the attributes. */
8104 attributes = cp_parser_attributes_opt (parser);
8105 /* Parse the asm-specification. */
8106 asm_specification = cp_parser_asm_specification_opt (parser);
8107 /* If the next token is not an `=' or '{', then we might still be
8108 looking at an expression. For example:
8112 looks like a decl-specifier-seq and a declarator -- but then
8113 there is no `=', so this is an expression. */
8114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8115 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8116 cp_parser_simulate_error (parser);
8118 /* If we did see an `=' or '{', then we are looking at a declaration
8120 if (cp_parser_parse_definitely (parser))
8123 bool non_constant_p;
8124 bool flags = LOOKUP_ONLYCONVERTING;
8126 /* Create the declaration. */
8127 decl = start_decl (declarator, &type_specifiers,
8128 /*initialized_p=*/true,
8129 attributes, /*prefix_attributes=*/NULL_TREE,
8132 /* Parse the initializer. */
8133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8135 initializer = cp_parser_braced_list (parser, &non_constant_p);
8136 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8141 /* Consume the `='. */
8142 cp_parser_require (parser, CPP_EQ, "%<=%>");
8143 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8145 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8146 maybe_warn_cpp0x ("extended initializer lists");
8148 if (!non_constant_p)
8149 initializer = fold_non_dependent_expr (initializer);
8151 /* Process the initializer. */
8152 cp_finish_decl (decl,
8153 initializer, !non_constant_p,
8158 pop_scope (pushed_scope);
8160 return convert_from_reference (decl);
8163 /* If we didn't even get past the declarator successfully, we are
8164 definitely not looking at a declaration. */
8166 cp_parser_abort_tentative_parse (parser);
8168 /* Otherwise, we are looking at an expression. */
8169 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8172 /* Parse an iteration-statement.
8174 iteration-statement:
8175 while ( condition ) statement
8176 do statement while ( expression ) ;
8177 for ( for-init-statement condition [opt] ; expression [opt] )
8180 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8183 cp_parser_iteration_statement (cp_parser* parser)
8188 unsigned char in_statement;
8190 /* Peek at the next token. */
8191 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8193 return error_mark_node;
8195 /* Remember whether or not we are already within an iteration
8197 in_statement = parser->in_statement;
8199 /* See what kind of keyword it is. */
8200 keyword = token->keyword;
8207 /* Begin the while-statement. */
8208 statement = begin_while_stmt ();
8209 /* Look for the `('. */
8210 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8211 /* Parse the condition. */
8212 condition = cp_parser_condition (parser);
8213 finish_while_stmt_cond (condition, statement);
8214 /* Look for the `)'. */
8215 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8216 /* Parse the dependent statement. */
8217 parser->in_statement = IN_ITERATION_STMT;
8218 cp_parser_already_scoped_statement (parser);
8219 parser->in_statement = in_statement;
8220 /* We're done with the while-statement. */
8221 finish_while_stmt (statement);
8229 /* Begin the do-statement. */
8230 statement = begin_do_stmt ();
8231 /* Parse the body of the do-statement. */
8232 parser->in_statement = IN_ITERATION_STMT;
8233 cp_parser_implicitly_scoped_statement (parser, NULL);
8234 parser->in_statement = in_statement;
8235 finish_do_body (statement);
8236 /* Look for the `while' keyword. */
8237 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8238 /* Look for the `('. */
8239 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8240 /* Parse the expression. */
8241 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8242 /* We're done with the do-statement. */
8243 finish_do_stmt (expression, statement);
8244 /* Look for the `)'. */
8245 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8246 /* Look for the `;'. */
8247 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8253 tree condition = NULL_TREE;
8254 tree expression = NULL_TREE;
8256 /* Begin the for-statement. */
8257 statement = begin_for_stmt ();
8258 /* Look for the `('. */
8259 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8260 /* Parse the initialization. */
8261 cp_parser_for_init_statement (parser);
8262 finish_for_init_stmt (statement);
8264 /* If there's a condition, process it. */
8265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8266 condition = cp_parser_condition (parser);
8267 finish_for_cond (condition, statement);
8268 /* Look for the `;'. */
8269 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8271 /* If there's an expression, process it. */
8272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8273 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8274 finish_for_expr (expression, statement);
8275 /* Look for the `)'. */
8276 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8278 /* Parse the body of the for-statement. */
8279 parser->in_statement = IN_ITERATION_STMT;
8280 cp_parser_already_scoped_statement (parser);
8281 parser->in_statement = in_statement;
8283 /* We're done with the for-statement. */
8284 finish_for_stmt (statement);
8289 cp_parser_error (parser, "expected iteration-statement");
8290 statement = error_mark_node;
8297 /* Parse a for-init-statement.
8300 expression-statement
8301 simple-declaration */
8304 cp_parser_for_init_statement (cp_parser* parser)
8306 /* If the next token is a `;', then we have an empty
8307 expression-statement. Grammatically, this is also a
8308 simple-declaration, but an invalid one, because it does not
8309 declare anything. Therefore, if we did not handle this case
8310 specially, we would issue an error message about an invalid
8312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8314 /* We're going to speculatively look for a declaration, falling back
8315 to an expression, if necessary. */
8316 cp_parser_parse_tentatively (parser);
8317 /* Parse the declaration. */
8318 cp_parser_simple_declaration (parser,
8319 /*function_definition_allowed_p=*/false);
8320 /* If the tentative parse failed, then we shall need to look for an
8321 expression-statement. */
8322 if (cp_parser_parse_definitely (parser))
8326 cp_parser_expression_statement (parser, false);
8329 /* Parse a jump-statement.
8334 return expression [opt] ;
8335 return braced-init-list ;
8343 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8346 cp_parser_jump_statement (cp_parser* parser)
8348 tree statement = error_mark_node;
8351 unsigned char in_statement;
8353 /* Peek at the next token. */
8354 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8356 return error_mark_node;
8358 /* See what kind of keyword it is. */
8359 keyword = token->keyword;
8363 in_statement = parser->in_statement & ~IN_IF_STMT;
8364 switch (in_statement)
8367 error_at (token->location, "break statement not within loop or switch");
8370 gcc_assert ((in_statement & IN_SWITCH_STMT)
8371 || in_statement == IN_ITERATION_STMT);
8372 statement = finish_break_stmt ();
8375 error_at (token->location, "invalid exit from OpenMP structured block");
8378 error_at (token->location, "break statement used with OpenMP for loop");
8381 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8385 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8388 error_at (token->location, "continue statement not within a loop");
8390 case IN_ITERATION_STMT:
8392 statement = finish_continue_stmt ();
8395 error_at (token->location, "invalid exit from OpenMP structured block");
8400 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8406 bool expr_non_constant_p;
8408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8410 maybe_warn_cpp0x ("extended initializer lists");
8411 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8413 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8414 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8416 /* If the next token is a `;', then there is no
8419 /* Build the return-statement. */
8420 statement = finish_return_stmt (expr);
8421 /* Look for the final `;'. */
8422 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8427 /* Create the goto-statement. */
8428 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8430 /* Issue a warning about this use of a GNU extension. */
8431 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8432 /* Consume the '*' token. */
8433 cp_lexer_consume_token (parser->lexer);
8434 /* Parse the dependent expression. */
8435 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8438 finish_goto_stmt (cp_parser_identifier (parser));
8439 /* Look for the final `;'. */
8440 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8444 cp_parser_error (parser, "expected jump-statement");
8451 /* Parse a declaration-statement.
8453 declaration-statement:
8454 block-declaration */
8457 cp_parser_declaration_statement (cp_parser* parser)
8461 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8462 p = obstack_alloc (&declarator_obstack, 0);
8464 /* Parse the block-declaration. */
8465 cp_parser_block_declaration (parser, /*statement_p=*/true);
8467 /* Free any declarators allocated. */
8468 obstack_free (&declarator_obstack, p);
8470 /* Finish off the statement. */
8474 /* Some dependent statements (like `if (cond) statement'), are
8475 implicitly in their own scope. In other words, if the statement is
8476 a single statement (as opposed to a compound-statement), it is
8477 none-the-less treated as if it were enclosed in braces. Any
8478 declarations appearing in the dependent statement are out of scope
8479 after control passes that point. This function parses a statement,
8480 but ensures that is in its own scope, even if it is not a
8483 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8484 is a (possibly labeled) if statement which is not enclosed in
8485 braces and has an else clause. This is used to implement
8488 Returns the new statement. */
8491 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8498 /* Mark if () ; with a special NOP_EXPR. */
8499 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8501 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8502 cp_lexer_consume_token (parser->lexer);
8503 statement = add_stmt (build_empty_stmt (loc));
8505 /* if a compound is opened, we simply parse the statement directly. */
8506 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8507 statement = cp_parser_compound_statement (parser, NULL, false);
8508 /* If the token is not a `{', then we must take special action. */
8511 /* Create a compound-statement. */
8512 statement = begin_compound_stmt (0);
8513 /* Parse the dependent-statement. */
8514 cp_parser_statement (parser, NULL_TREE, false, if_p);
8515 /* Finish the dummy compound-statement. */
8516 finish_compound_stmt (statement);
8519 /* Return the statement. */
8523 /* For some dependent statements (like `while (cond) statement'), we
8524 have already created a scope. Therefore, even if the dependent
8525 statement is a compound-statement, we do not want to create another
8529 cp_parser_already_scoped_statement (cp_parser* parser)
8531 /* If the token is a `{', then we must take special action. */
8532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8533 cp_parser_statement (parser, NULL_TREE, false, NULL);
8536 /* Avoid calling cp_parser_compound_statement, so that we
8537 don't create a new scope. Do everything else by hand. */
8538 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8539 /* If the next keyword is `__label__' we have a label declaration. */
8540 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8541 cp_parser_label_declaration (parser);
8542 /* Parse an (optional) statement-seq. */
8543 cp_parser_statement_seq_opt (parser, NULL_TREE);
8544 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8548 /* Declarations [gram.dcl.dcl] */
8550 /* Parse an optional declaration-sequence.
8554 declaration-seq declaration */
8557 cp_parser_declaration_seq_opt (cp_parser* parser)
8563 token = cp_lexer_peek_token (parser->lexer);
8565 if (token->type == CPP_CLOSE_BRACE
8566 || token->type == CPP_EOF
8567 || token->type == CPP_PRAGMA_EOL)
8570 if (token->type == CPP_SEMICOLON)
8572 /* A declaration consisting of a single semicolon is
8573 invalid. Allow it unless we're being pedantic. */
8574 cp_lexer_consume_token (parser->lexer);
8575 if (!in_system_header)
8576 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8580 /* If we're entering or exiting a region that's implicitly
8581 extern "C", modify the lang context appropriately. */
8582 if (!parser->implicit_extern_c && token->implicit_extern_c)
8584 push_lang_context (lang_name_c);
8585 parser->implicit_extern_c = true;
8587 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8589 pop_lang_context ();
8590 parser->implicit_extern_c = false;
8593 if (token->type == CPP_PRAGMA)
8595 /* A top-level declaration can consist solely of a #pragma.
8596 A nested declaration cannot, so this is done here and not
8597 in cp_parser_declaration. (A #pragma at block scope is
8598 handled in cp_parser_statement.) */
8599 cp_parser_pragma (parser, pragma_external);
8603 /* Parse the declaration itself. */
8604 cp_parser_declaration (parser);
8608 /* Parse a declaration.
8613 template-declaration
8614 explicit-instantiation
8615 explicit-specialization
8616 linkage-specification
8617 namespace-definition
8622 __extension__ declaration */
8625 cp_parser_declaration (cp_parser* parser)
8632 /* Check for the `__extension__' keyword. */
8633 if (cp_parser_extension_opt (parser, &saved_pedantic))
8635 /* Parse the qualified declaration. */
8636 cp_parser_declaration (parser);
8637 /* Restore the PEDANTIC flag. */
8638 pedantic = saved_pedantic;
8643 /* Try to figure out what kind of declaration is present. */
8644 token1 = *cp_lexer_peek_token (parser->lexer);
8646 if (token1.type != CPP_EOF)
8647 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8650 token2.type = CPP_EOF;
8651 token2.keyword = RID_MAX;
8654 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8655 p = obstack_alloc (&declarator_obstack, 0);
8657 /* If the next token is `extern' and the following token is a string
8658 literal, then we have a linkage specification. */
8659 if (token1.keyword == RID_EXTERN
8660 && cp_parser_is_string_literal (&token2))
8661 cp_parser_linkage_specification (parser);
8662 /* If the next token is `template', then we have either a template
8663 declaration, an explicit instantiation, or an explicit
8665 else if (token1.keyword == RID_TEMPLATE)
8667 /* `template <>' indicates a template specialization. */
8668 if (token2.type == CPP_LESS
8669 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8670 cp_parser_explicit_specialization (parser);
8671 /* `template <' indicates a template declaration. */
8672 else if (token2.type == CPP_LESS)
8673 cp_parser_template_declaration (parser, /*member_p=*/false);
8674 /* Anything else must be an explicit instantiation. */
8676 cp_parser_explicit_instantiation (parser);
8678 /* If the next token is `export', then we have a template
8680 else if (token1.keyword == RID_EXPORT)
8681 cp_parser_template_declaration (parser, /*member_p=*/false);
8682 /* If the next token is `extern', 'static' or 'inline' and the one
8683 after that is `template', we have a GNU extended explicit
8684 instantiation directive. */
8685 else if (cp_parser_allow_gnu_extensions_p (parser)
8686 && (token1.keyword == RID_EXTERN
8687 || token1.keyword == RID_STATIC
8688 || token1.keyword == RID_INLINE)
8689 && token2.keyword == RID_TEMPLATE)
8690 cp_parser_explicit_instantiation (parser);
8691 /* If the next token is `namespace', check for a named or unnamed
8692 namespace definition. */
8693 else if (token1.keyword == RID_NAMESPACE
8694 && (/* A named namespace definition. */
8695 (token2.type == CPP_NAME
8696 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8698 /* An unnamed namespace definition. */
8699 || token2.type == CPP_OPEN_BRACE
8700 || token2.keyword == RID_ATTRIBUTE))
8701 cp_parser_namespace_definition (parser);
8702 /* An inline (associated) namespace definition. */
8703 else if (token1.keyword == RID_INLINE
8704 && token2.keyword == RID_NAMESPACE)
8705 cp_parser_namespace_definition (parser);
8706 /* Objective-C++ declaration/definition. */
8707 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8708 cp_parser_objc_declaration (parser);
8709 /* We must have either a block declaration or a function
8712 /* Try to parse a block-declaration, or a function-definition. */
8713 cp_parser_block_declaration (parser, /*statement_p=*/false);
8715 /* Free any declarators allocated. */
8716 obstack_free (&declarator_obstack, p);
8719 /* Parse a block-declaration.
8724 namespace-alias-definition
8731 __extension__ block-declaration
8736 static_assert-declaration
8738 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8739 part of a declaration-statement. */
8742 cp_parser_block_declaration (cp_parser *parser,
8748 /* Check for the `__extension__' keyword. */
8749 if (cp_parser_extension_opt (parser, &saved_pedantic))
8751 /* Parse the qualified declaration. */
8752 cp_parser_block_declaration (parser, statement_p);
8753 /* Restore the PEDANTIC flag. */
8754 pedantic = saved_pedantic;
8759 /* Peek at the next token to figure out which kind of declaration is
8761 token1 = cp_lexer_peek_token (parser->lexer);
8763 /* If the next keyword is `asm', we have an asm-definition. */
8764 if (token1->keyword == RID_ASM)
8767 cp_parser_commit_to_tentative_parse (parser);
8768 cp_parser_asm_definition (parser);
8770 /* If the next keyword is `namespace', we have a
8771 namespace-alias-definition. */
8772 else if (token1->keyword == RID_NAMESPACE)
8773 cp_parser_namespace_alias_definition (parser);
8774 /* If the next keyword is `using', we have either a
8775 using-declaration or a using-directive. */
8776 else if (token1->keyword == RID_USING)
8781 cp_parser_commit_to_tentative_parse (parser);
8782 /* If the token after `using' is `namespace', then we have a
8784 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8785 if (token2->keyword == RID_NAMESPACE)
8786 cp_parser_using_directive (parser);
8787 /* Otherwise, it's a using-declaration. */
8789 cp_parser_using_declaration (parser,
8790 /*access_declaration_p=*/false);
8792 /* If the next keyword is `__label__' we have a misplaced label
8794 else if (token1->keyword == RID_LABEL)
8796 cp_lexer_consume_token (parser->lexer);
8797 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8798 cp_parser_skip_to_end_of_statement (parser);
8799 /* If the next token is now a `;', consume it. */
8800 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8801 cp_lexer_consume_token (parser->lexer);
8803 /* If the next token is `static_assert' we have a static assertion. */
8804 else if (token1->keyword == RID_STATIC_ASSERT)
8805 cp_parser_static_assert (parser, /*member_p=*/false);
8806 /* Anything else must be a simple-declaration. */
8808 cp_parser_simple_declaration (parser, !statement_p);
8811 /* Parse a simple-declaration.
8814 decl-specifier-seq [opt] init-declarator-list [opt] ;
8816 init-declarator-list:
8818 init-declarator-list , init-declarator
8820 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8821 function-definition as a simple-declaration. */
8824 cp_parser_simple_declaration (cp_parser* parser,
8825 bool function_definition_allowed_p)
8827 cp_decl_specifier_seq decl_specifiers;
8828 int declares_class_or_enum;
8829 bool saw_declarator;
8831 /* Defer access checks until we know what is being declared; the
8832 checks for names appearing in the decl-specifier-seq should be
8833 done as if we were in the scope of the thing being declared. */
8834 push_deferring_access_checks (dk_deferred);
8836 /* Parse the decl-specifier-seq. We have to keep track of whether
8837 or not the decl-specifier-seq declares a named class or
8838 enumeration type, since that is the only case in which the
8839 init-declarator-list is allowed to be empty.
8843 In a simple-declaration, the optional init-declarator-list can be
8844 omitted only when declaring a class or enumeration, that is when
8845 the decl-specifier-seq contains either a class-specifier, an
8846 elaborated-type-specifier, or an enum-specifier. */
8847 cp_parser_decl_specifier_seq (parser,
8848 CP_PARSER_FLAGS_OPTIONAL,
8850 &declares_class_or_enum);
8851 /* We no longer need to defer access checks. */
8852 stop_deferring_access_checks ();
8854 /* In a block scope, a valid declaration must always have a
8855 decl-specifier-seq. By not trying to parse declarators, we can
8856 resolve the declaration/expression ambiguity more quickly. */
8857 if (!function_definition_allowed_p
8858 && !decl_specifiers.any_specifiers_p)
8860 cp_parser_error (parser, "expected declaration");
8864 /* If the next two tokens are both identifiers, the code is
8865 erroneous. The usual cause of this situation is code like:
8869 where "T" should name a type -- but does not. */
8870 if (!decl_specifiers.any_type_specifiers_p
8871 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8873 /* If parsing tentatively, we should commit; we really are
8874 looking at a declaration. */
8875 cp_parser_commit_to_tentative_parse (parser);
8880 /* If we have seen at least one decl-specifier, and the next token
8881 is not a parenthesis, then we must be looking at a declaration.
8882 (After "int (" we might be looking at a functional cast.) */
8883 if (decl_specifiers.any_specifiers_p
8884 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8885 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8886 && !cp_parser_error_occurred (parser))
8887 cp_parser_commit_to_tentative_parse (parser);
8889 /* Keep going until we hit the `;' at the end of the simple
8891 saw_declarator = false;
8892 while (cp_lexer_next_token_is_not (parser->lexer,
8896 bool function_definition_p;
8901 /* If we are processing next declarator, coma is expected */
8902 token = cp_lexer_peek_token (parser->lexer);
8903 gcc_assert (token->type == CPP_COMMA);
8904 cp_lexer_consume_token (parser->lexer);
8907 saw_declarator = true;
8909 /* Parse the init-declarator. */
8910 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8912 function_definition_allowed_p,
8914 declares_class_or_enum,
8915 &function_definition_p);
8916 /* If an error occurred while parsing tentatively, exit quickly.
8917 (That usually happens when in the body of a function; each
8918 statement is treated as a declaration-statement until proven
8920 if (cp_parser_error_occurred (parser))
8922 /* Handle function definitions specially. */
8923 if (function_definition_p)
8925 /* If the next token is a `,', then we are probably
8926 processing something like:
8930 which is erroneous. */
8931 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8933 cp_token *token = cp_lexer_peek_token (parser->lexer);
8934 error_at (token->location,
8936 " declarations and function-definitions is forbidden");
8938 /* Otherwise, we're done with the list of declarators. */
8941 pop_deferring_access_checks ();
8945 /* The next token should be either a `,' or a `;'. */
8946 token = cp_lexer_peek_token (parser->lexer);
8947 /* If it's a `,', there are more declarators to come. */
8948 if (token->type == CPP_COMMA)
8949 /* will be consumed next time around */;
8950 /* If it's a `;', we are done. */
8951 else if (token->type == CPP_SEMICOLON)
8953 /* Anything else is an error. */
8956 /* If we have already issued an error message we don't need
8957 to issue another one. */
8958 if (decl != error_mark_node
8959 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8960 cp_parser_error (parser, "expected %<,%> or %<;%>");
8961 /* Skip tokens until we reach the end of the statement. */
8962 cp_parser_skip_to_end_of_statement (parser);
8963 /* If the next token is now a `;', consume it. */
8964 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8965 cp_lexer_consume_token (parser->lexer);
8968 /* After the first time around, a function-definition is not
8969 allowed -- even if it was OK at first. For example:
8974 function_definition_allowed_p = false;
8977 /* Issue an error message if no declarators are present, and the
8978 decl-specifier-seq does not itself declare a class or
8980 if (!saw_declarator)
8982 if (cp_parser_declares_only_class_p (parser))
8983 shadow_tag (&decl_specifiers);
8984 /* Perform any deferred access checks. */
8985 perform_deferred_access_checks ();
8988 /* Consume the `;'. */
8989 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8992 pop_deferring_access_checks ();
8995 /* Parse a decl-specifier-seq.
8998 decl-specifier-seq [opt] decl-specifier
9001 storage-class-specifier
9012 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9014 The parser flags FLAGS is used to control type-specifier parsing.
9016 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9019 1: one of the decl-specifiers is an elaborated-type-specifier
9020 (i.e., a type declaration)
9021 2: one of the decl-specifiers is an enum-specifier or a
9022 class-specifier (i.e., a type definition)
9027 cp_parser_decl_specifier_seq (cp_parser* parser,
9028 cp_parser_flags flags,
9029 cp_decl_specifier_seq *decl_specs,
9030 int* declares_class_or_enum)
9032 bool constructor_possible_p = !parser->in_declarator_p;
9033 cp_token *start_token = NULL;
9035 /* Clear DECL_SPECS. */
9036 clear_decl_specs (decl_specs);
9038 /* Assume no class or enumeration type is declared. */
9039 *declares_class_or_enum = 0;
9041 /* Keep reading specifiers until there are no more to read. */
9045 bool found_decl_spec;
9048 /* Peek at the next token. */
9049 token = cp_lexer_peek_token (parser->lexer);
9051 /* Save the first token of the decl spec list for error
9054 start_token = token;
9055 /* Handle attributes. */
9056 if (token->keyword == RID_ATTRIBUTE)
9058 /* Parse the attributes. */
9059 decl_specs->attributes
9060 = chainon (decl_specs->attributes,
9061 cp_parser_attributes_opt (parser));
9064 /* Assume we will find a decl-specifier keyword. */
9065 found_decl_spec = true;
9066 /* If the next token is an appropriate keyword, we can simply
9067 add it to the list. */
9068 switch (token->keyword)
9074 if (!at_class_scope_p ())
9076 error_at (token->location, "%<friend%> used outside of class");
9077 cp_lexer_purge_token (parser->lexer);
9081 ++decl_specs->specs[(int) ds_friend];
9082 /* Consume the token. */
9083 cp_lexer_consume_token (parser->lexer);
9088 ++decl_specs->specs[(int) ds_constexpr];
9089 cp_lexer_consume_token (parser->lexer);
9092 /* function-specifier:
9099 cp_parser_function_specifier_opt (parser, decl_specs);
9105 ++decl_specs->specs[(int) ds_typedef];
9106 /* Consume the token. */
9107 cp_lexer_consume_token (parser->lexer);
9108 /* A constructor declarator cannot appear in a typedef. */
9109 constructor_possible_p = false;
9110 /* The "typedef" keyword can only occur in a declaration; we
9111 may as well commit at this point. */
9112 cp_parser_commit_to_tentative_parse (parser);
9114 if (decl_specs->storage_class != sc_none)
9115 decl_specs->conflicting_specifiers_p = true;
9118 /* storage-class-specifier:
9128 if (cxx_dialect == cxx98)
9130 /* Consume the token. */
9131 cp_lexer_consume_token (parser->lexer);
9133 /* Complain about `auto' as a storage specifier, if
9134 we're complaining about C++0x compatibility. */
9135 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9136 " will change meaning in C++0x; please remove it");
9138 /* Set the storage class anyway. */
9139 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9143 /* C++0x auto type-specifier. */
9144 found_decl_spec = false;
9151 /* Consume the token. */
9152 cp_lexer_consume_token (parser->lexer);
9153 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9157 /* Consume the token. */
9158 cp_lexer_consume_token (parser->lexer);
9159 ++decl_specs->specs[(int) ds_thread];
9163 /* We did not yet find a decl-specifier yet. */
9164 found_decl_spec = false;
9168 /* Constructors are a special case. The `S' in `S()' is not a
9169 decl-specifier; it is the beginning of the declarator. */
9172 && constructor_possible_p
9173 && (cp_parser_constructor_declarator_p
9174 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9176 /* If we don't have a DECL_SPEC yet, then we must be looking at
9177 a type-specifier. */
9178 if (!found_decl_spec && !constructor_p)
9180 int decl_spec_declares_class_or_enum;
9181 bool is_cv_qualifier;
9185 = cp_parser_type_specifier (parser, flags,
9187 /*is_declaration=*/true,
9188 &decl_spec_declares_class_or_enum,
9190 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9192 /* If this type-specifier referenced a user-defined type
9193 (a typedef, class-name, etc.), then we can't allow any
9194 more such type-specifiers henceforth.
9198 The longest sequence of decl-specifiers that could
9199 possibly be a type name is taken as the
9200 decl-specifier-seq of a declaration. The sequence shall
9201 be self-consistent as described below.
9205 As a general rule, at most one type-specifier is allowed
9206 in the complete decl-specifier-seq of a declaration. The
9207 only exceptions are the following:
9209 -- const or volatile can be combined with any other
9212 -- signed or unsigned can be combined with char, long,
9220 void g (const int Pc);
9222 Here, Pc is *not* part of the decl-specifier seq; it's
9223 the declarator. Therefore, once we see a type-specifier
9224 (other than a cv-qualifier), we forbid any additional
9225 user-defined types. We *do* still allow things like `int
9226 int' to be considered a decl-specifier-seq, and issue the
9227 error message later. */
9228 if (type_spec && !is_cv_qualifier)
9229 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9230 /* A constructor declarator cannot follow a type-specifier. */
9233 constructor_possible_p = false;
9234 found_decl_spec = true;
9235 if (!is_cv_qualifier)
9236 decl_specs->any_type_specifiers_p = true;
9240 /* If we still do not have a DECL_SPEC, then there are no more
9242 if (!found_decl_spec)
9245 decl_specs->any_specifiers_p = true;
9246 /* After we see one decl-specifier, further decl-specifiers are
9248 flags |= CP_PARSER_FLAGS_OPTIONAL;
9251 cp_parser_check_decl_spec (decl_specs, start_token->location);
9253 /* Don't allow a friend specifier with a class definition. */
9254 if (decl_specs->specs[(int) ds_friend] != 0
9255 && (*declares_class_or_enum & 2))
9256 error_at (start_token->location,
9257 "class definition may not be declared a friend");
9260 /* Parse an (optional) storage-class-specifier.
9262 storage-class-specifier:
9271 storage-class-specifier:
9274 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9277 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9279 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9282 if (cxx_dialect != cxx98)
9284 /* Fall through for C++98. */
9291 /* Consume the token. */
9292 return cp_lexer_consume_token (parser->lexer)->u.value;
9299 /* Parse an (optional) function-specifier.
9306 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9307 Updates DECL_SPECS, if it is non-NULL. */
9310 cp_parser_function_specifier_opt (cp_parser* parser,
9311 cp_decl_specifier_seq *decl_specs)
9313 cp_token *token = cp_lexer_peek_token (parser->lexer);
9314 switch (token->keyword)
9318 ++decl_specs->specs[(int) ds_inline];
9322 /* 14.5.2.3 [temp.mem]
9324 A member function template shall not be virtual. */
9325 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9326 error_at (token->location, "templates may not be %<virtual%>");
9327 else if (decl_specs)
9328 ++decl_specs->specs[(int) ds_virtual];
9333 ++decl_specs->specs[(int) ds_explicit];
9340 /* Consume the token. */
9341 return cp_lexer_consume_token (parser->lexer)->u.value;
9344 /* Parse a linkage-specification.
9346 linkage-specification:
9347 extern string-literal { declaration-seq [opt] }
9348 extern string-literal declaration */
9351 cp_parser_linkage_specification (cp_parser* parser)
9355 /* Look for the `extern' keyword. */
9356 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9358 /* Look for the string-literal. */
9359 linkage = cp_parser_string_literal (parser, false, false);
9361 /* Transform the literal into an identifier. If the literal is a
9362 wide-character string, or contains embedded NULs, then we can't
9363 handle it as the user wants. */
9364 if (strlen (TREE_STRING_POINTER (linkage))
9365 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9367 cp_parser_error (parser, "invalid linkage-specification");
9368 /* Assume C++ linkage. */
9369 linkage = lang_name_cplusplus;
9372 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9374 /* We're now using the new linkage. */
9375 push_lang_context (linkage);
9377 /* If the next token is a `{', then we're using the first
9379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9381 /* Consume the `{' token. */
9382 cp_lexer_consume_token (parser->lexer);
9383 /* Parse the declarations. */
9384 cp_parser_declaration_seq_opt (parser);
9385 /* Look for the closing `}'. */
9386 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9388 /* Otherwise, there's just one declaration. */
9391 bool saved_in_unbraced_linkage_specification_p;
9393 saved_in_unbraced_linkage_specification_p
9394 = parser->in_unbraced_linkage_specification_p;
9395 parser->in_unbraced_linkage_specification_p = true;
9396 cp_parser_declaration (parser);
9397 parser->in_unbraced_linkage_specification_p
9398 = saved_in_unbraced_linkage_specification_p;
9401 /* We're done with the linkage-specification. */
9402 pop_lang_context ();
9405 /* Parse a static_assert-declaration.
9407 static_assert-declaration:
9408 static_assert ( constant-expression , string-literal ) ;
9410 If MEMBER_P, this static_assert is a class member. */
9413 cp_parser_static_assert(cp_parser *parser, bool member_p)
9418 location_t saved_loc;
9420 /* Peek at the `static_assert' token so we can keep track of exactly
9421 where the static assertion started. */
9422 token = cp_lexer_peek_token (parser->lexer);
9423 saved_loc = token->location;
9425 /* Look for the `static_assert' keyword. */
9426 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9427 "%<static_assert%>"))
9430 /* We know we are in a static assertion; commit to any tentative
9432 if (cp_parser_parsing_tentatively (parser))
9433 cp_parser_commit_to_tentative_parse (parser);
9435 /* Parse the `(' starting the static assertion condition. */
9436 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9438 /* Parse the constant-expression. */
9440 cp_parser_constant_expression (parser,
9441 /*allow_non_constant_p=*/false,
9442 /*non_constant_p=*/NULL);
9444 /* Parse the separating `,'. */
9445 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9447 /* Parse the string-literal message. */
9448 message = cp_parser_string_literal (parser,
9449 /*translate=*/false,
9452 /* A `)' completes the static assertion. */
9453 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9454 cp_parser_skip_to_closing_parenthesis (parser,
9455 /*recovering=*/true,
9457 /*consume_paren=*/true);
9459 /* A semicolon terminates the declaration. */
9460 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9462 /* Complete the static assertion, which may mean either processing
9463 the static assert now or saving it for template instantiation. */
9464 finish_static_assert (condition, message, saved_loc, member_p);
9467 /* Parse a `decltype' type. Returns the type.
9469 simple-type-specifier:
9470 decltype ( expression ) */
9473 cp_parser_decltype (cp_parser *parser)
9476 bool id_expression_or_member_access_p = false;
9477 const char *saved_message;
9478 bool saved_integral_constant_expression_p;
9479 bool saved_non_integral_constant_expression_p;
9480 cp_token *id_expr_start_token;
9482 /* Look for the `decltype' token. */
9483 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9484 return error_mark_node;
9486 /* Types cannot be defined in a `decltype' expression. Save away the
9488 saved_message = parser->type_definition_forbidden_message;
9490 /* And create the new one. */
9491 parser->type_definition_forbidden_message
9492 = "types may not be defined in %<decltype%> expressions";
9494 /* The restrictions on constant-expressions do not apply inside
9495 decltype expressions. */
9496 saved_integral_constant_expression_p
9497 = parser->integral_constant_expression_p;
9498 saved_non_integral_constant_expression_p
9499 = parser->non_integral_constant_expression_p;
9500 parser->integral_constant_expression_p = false;
9502 /* Do not actually evaluate the expression. */
9503 ++cp_unevaluated_operand;
9505 /* Do not warn about problems with the expression. */
9506 ++c_inhibit_evaluation_warnings;
9508 /* Parse the opening `('. */
9509 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9510 return error_mark_node;
9512 /* First, try parsing an id-expression. */
9513 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9514 cp_parser_parse_tentatively (parser);
9515 expr = cp_parser_id_expression (parser,
9516 /*template_keyword_p=*/false,
9517 /*check_dependency_p=*/true,
9518 /*template_p=*/NULL,
9519 /*declarator_p=*/false,
9520 /*optional_p=*/false);
9522 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9524 bool non_integral_constant_expression_p = false;
9525 tree id_expression = expr;
9527 const char *error_msg;
9529 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9530 /* Lookup the name we got back from the id-expression. */
9531 expr = cp_parser_lookup_name (parser, expr,
9533 /*is_template=*/false,
9534 /*is_namespace=*/false,
9535 /*check_dependency=*/true,
9536 /*ambiguous_decls=*/NULL,
9537 id_expr_start_token->location);
9540 && expr != error_mark_node
9541 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9542 && TREE_CODE (expr) != TYPE_DECL
9543 && (TREE_CODE (expr) != BIT_NOT_EXPR
9544 || !TYPE_P (TREE_OPERAND (expr, 0)))
9545 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9547 /* Complete lookup of the id-expression. */
9548 expr = (finish_id_expression
9549 (id_expression, expr, parser->scope, &idk,
9550 /*integral_constant_expression_p=*/false,
9551 /*allow_non_integral_constant_expression_p=*/true,
9552 &non_integral_constant_expression_p,
9553 /*template_p=*/false,
9555 /*address_p=*/false,
9556 /*template_arg_p=*/false,
9558 id_expr_start_token->location));
9560 if (expr == error_mark_node)
9561 /* We found an id-expression, but it was something that we
9562 should not have found. This is an error, not something
9563 we can recover from, so note that we found an
9564 id-expression and we'll recover as gracefully as
9566 id_expression_or_member_access_p = true;
9570 && expr != error_mark_node
9571 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9572 /* We have an id-expression. */
9573 id_expression_or_member_access_p = true;
9576 if (!id_expression_or_member_access_p)
9578 /* Abort the id-expression parse. */
9579 cp_parser_abort_tentative_parse (parser);
9581 /* Parsing tentatively, again. */
9582 cp_parser_parse_tentatively (parser);
9584 /* Parse a class member access. */
9585 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9587 /*member_access_only_p=*/true, NULL);
9590 && expr != error_mark_node
9591 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9592 /* We have an id-expression. */
9593 id_expression_or_member_access_p = true;
9596 if (id_expression_or_member_access_p)
9597 /* We have parsed the complete id-expression or member access. */
9598 cp_parser_parse_definitely (parser);
9601 bool saved_greater_than_is_operator_p;
9603 /* Abort our attempt to parse an id-expression or member access
9605 cp_parser_abort_tentative_parse (parser);
9607 /* Within a parenthesized expression, a `>' token is always
9608 the greater-than operator. */
9609 saved_greater_than_is_operator_p
9610 = parser->greater_than_is_operator_p;
9611 parser->greater_than_is_operator_p = true;
9613 /* Parse a full expression. */
9614 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9616 /* The `>' token might be the end of a template-id or
9617 template-parameter-list now. */
9618 parser->greater_than_is_operator_p
9619 = saved_greater_than_is_operator_p;
9622 /* Go back to evaluating expressions. */
9623 --cp_unevaluated_operand;
9624 --c_inhibit_evaluation_warnings;
9626 /* Restore the old message and the integral constant expression
9628 parser->type_definition_forbidden_message = saved_message;
9629 parser->integral_constant_expression_p
9630 = saved_integral_constant_expression_p;
9631 parser->non_integral_constant_expression_p
9632 = saved_non_integral_constant_expression_p;
9634 if (expr == error_mark_node)
9636 /* Skip everything up to the closing `)'. */
9637 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9638 /*consume_paren=*/true);
9639 return error_mark_node;
9642 /* Parse to the closing `)'. */
9643 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9645 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9646 /*consume_paren=*/true);
9647 return error_mark_node;
9650 return finish_decltype_type (expr, id_expression_or_member_access_p);
9653 /* Special member functions [gram.special] */
9655 /* Parse a conversion-function-id.
9657 conversion-function-id:
9658 operator conversion-type-id
9660 Returns an IDENTIFIER_NODE representing the operator. */
9663 cp_parser_conversion_function_id (cp_parser* parser)
9667 tree saved_qualifying_scope;
9668 tree saved_object_scope;
9669 tree pushed_scope = NULL_TREE;
9671 /* Look for the `operator' token. */
9672 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9673 return error_mark_node;
9674 /* When we parse the conversion-type-id, the current scope will be
9675 reset. However, we need that information in able to look up the
9676 conversion function later, so we save it here. */
9677 saved_scope = parser->scope;
9678 saved_qualifying_scope = parser->qualifying_scope;
9679 saved_object_scope = parser->object_scope;
9680 /* We must enter the scope of the class so that the names of
9681 entities declared within the class are available in the
9682 conversion-type-id. For example, consider:
9689 S::operator I() { ... }
9691 In order to see that `I' is a type-name in the definition, we
9692 must be in the scope of `S'. */
9694 pushed_scope = push_scope (saved_scope);
9695 /* Parse the conversion-type-id. */
9696 type = cp_parser_conversion_type_id (parser);
9697 /* Leave the scope of the class, if any. */
9699 pop_scope (pushed_scope);
9700 /* Restore the saved scope. */
9701 parser->scope = saved_scope;
9702 parser->qualifying_scope = saved_qualifying_scope;
9703 parser->object_scope = saved_object_scope;
9704 /* If the TYPE is invalid, indicate failure. */
9705 if (type == error_mark_node)
9706 return error_mark_node;
9707 return mangle_conv_op_name_for_type (type);
9710 /* Parse a conversion-type-id:
9713 type-specifier-seq conversion-declarator [opt]
9715 Returns the TYPE specified. */
9718 cp_parser_conversion_type_id (cp_parser* parser)
9721 cp_decl_specifier_seq type_specifiers;
9722 cp_declarator *declarator;
9723 tree type_specified;
9725 /* Parse the attributes. */
9726 attributes = cp_parser_attributes_opt (parser);
9727 /* Parse the type-specifiers. */
9728 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9729 /*is_trailing_return=*/false,
9731 /* If that didn't work, stop. */
9732 if (type_specifiers.type == error_mark_node)
9733 return error_mark_node;
9734 /* Parse the conversion-declarator. */
9735 declarator = cp_parser_conversion_declarator_opt (parser);
9737 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9738 /*initialized=*/0, &attributes);
9740 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9742 /* Don't give this error when parsing tentatively. This happens to
9743 work because we always parse this definitively once. */
9744 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9745 && type_uses_auto (type_specified))
9747 error ("invalid use of %<auto%> in conversion operator");
9748 return error_mark_node;
9751 return type_specified;
9754 /* Parse an (optional) conversion-declarator.
9756 conversion-declarator:
9757 ptr-operator conversion-declarator [opt]
9761 static cp_declarator *
9762 cp_parser_conversion_declarator_opt (cp_parser* parser)
9764 enum tree_code code;
9766 cp_cv_quals cv_quals;
9768 /* We don't know if there's a ptr-operator next, or not. */
9769 cp_parser_parse_tentatively (parser);
9770 /* Try the ptr-operator. */
9771 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9772 /* If it worked, look for more conversion-declarators. */
9773 if (cp_parser_parse_definitely (parser))
9775 cp_declarator *declarator;
9777 /* Parse another optional declarator. */
9778 declarator = cp_parser_conversion_declarator_opt (parser);
9780 return cp_parser_make_indirect_declarator
9781 (code, class_type, cv_quals, declarator);
9787 /* Parse an (optional) ctor-initializer.
9790 : mem-initializer-list
9792 Returns TRUE iff the ctor-initializer was actually present. */
9795 cp_parser_ctor_initializer_opt (cp_parser* parser)
9797 /* If the next token is not a `:', then there is no
9798 ctor-initializer. */
9799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9801 /* Do default initialization of any bases and members. */
9802 if (DECL_CONSTRUCTOR_P (current_function_decl))
9803 finish_mem_initializers (NULL_TREE);
9808 /* Consume the `:' token. */
9809 cp_lexer_consume_token (parser->lexer);
9810 /* And the mem-initializer-list. */
9811 cp_parser_mem_initializer_list (parser);
9816 /* Parse a mem-initializer-list.
9818 mem-initializer-list:
9819 mem-initializer ... [opt]
9820 mem-initializer ... [opt] , mem-initializer-list */
9823 cp_parser_mem_initializer_list (cp_parser* parser)
9825 tree mem_initializer_list = NULL_TREE;
9826 cp_token *token = cp_lexer_peek_token (parser->lexer);
9828 /* Let the semantic analysis code know that we are starting the
9829 mem-initializer-list. */
9830 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9831 error_at (token->location,
9832 "only constructors take base initializers");
9834 /* Loop through the list. */
9837 tree mem_initializer;
9839 token = cp_lexer_peek_token (parser->lexer);
9840 /* Parse the mem-initializer. */
9841 mem_initializer = cp_parser_mem_initializer (parser);
9842 /* If the next token is a `...', we're expanding member initializers. */
9843 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9845 /* Consume the `...'. */
9846 cp_lexer_consume_token (parser->lexer);
9848 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9849 can be expanded but members cannot. */
9850 if (mem_initializer != error_mark_node
9851 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9853 error_at (token->location,
9854 "cannot expand initializer for member %<%D%>",
9855 TREE_PURPOSE (mem_initializer));
9856 mem_initializer = error_mark_node;
9859 /* Construct the pack expansion type. */
9860 if (mem_initializer != error_mark_node)
9861 mem_initializer = make_pack_expansion (mem_initializer);
9863 /* Add it to the list, unless it was erroneous. */
9864 if (mem_initializer != error_mark_node)
9866 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9867 mem_initializer_list = mem_initializer;
9869 /* If the next token is not a `,', we're done. */
9870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9872 /* Consume the `,' token. */
9873 cp_lexer_consume_token (parser->lexer);
9876 /* Perform semantic analysis. */
9877 if (DECL_CONSTRUCTOR_P (current_function_decl))
9878 finish_mem_initializers (mem_initializer_list);
9881 /* Parse a mem-initializer.
9884 mem-initializer-id ( expression-list [opt] )
9885 mem-initializer-id braced-init-list
9890 ( expression-list [opt] )
9892 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9893 class) or FIELD_DECL (for a non-static data member) to initialize;
9894 the TREE_VALUE is the expression-list. An empty initialization
9895 list is represented by void_list_node. */
9898 cp_parser_mem_initializer (cp_parser* parser)
9900 tree mem_initializer_id;
9901 tree expression_list;
9903 cp_token *token = cp_lexer_peek_token (parser->lexer);
9905 /* Find out what is being initialized. */
9906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9908 permerror (token->location,
9909 "anachronistic old-style base class initializer");
9910 mem_initializer_id = NULL_TREE;
9914 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9915 if (mem_initializer_id == error_mark_node)
9916 return mem_initializer_id;
9918 member = expand_member_init (mem_initializer_id);
9919 if (member && !DECL_P (member))
9920 in_base_initializer = 1;
9922 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9924 bool expr_non_constant_p;
9925 maybe_warn_cpp0x ("extended initializer lists");
9926 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9927 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9928 expression_list = build_tree_list (NULL_TREE, expression_list);
9933 vec = cp_parser_parenthesized_expression_list (parser, false,
9935 /*allow_expansion_p=*/true,
9936 /*non_constant_p=*/NULL);
9938 return error_mark_node;
9939 expression_list = build_tree_list_vec (vec);
9940 release_tree_vector (vec);
9943 if (expression_list == error_mark_node)
9944 return error_mark_node;
9945 if (!expression_list)
9946 expression_list = void_type_node;
9948 in_base_initializer = 0;
9950 return member ? build_tree_list (member, expression_list) : error_mark_node;
9953 /* Parse a mem-initializer-id.
9956 :: [opt] nested-name-specifier [opt] class-name
9959 Returns a TYPE indicating the class to be initializer for the first
9960 production. Returns an IDENTIFIER_NODE indicating the data member
9961 to be initialized for the second production. */
9964 cp_parser_mem_initializer_id (cp_parser* parser)
9966 bool global_scope_p;
9967 bool nested_name_specifier_p;
9968 bool template_p = false;
9971 cp_token *token = cp_lexer_peek_token (parser->lexer);
9973 /* `typename' is not allowed in this context ([temp.res]). */
9974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9976 error_at (token->location,
9977 "keyword %<typename%> not allowed in this context (a qualified "
9978 "member initializer is implicitly a type)");
9979 cp_lexer_consume_token (parser->lexer);
9981 /* Look for the optional `::' operator. */
9983 = (cp_parser_global_scope_opt (parser,
9984 /*current_scope_valid_p=*/false)
9986 /* Look for the optional nested-name-specifier. The simplest way to
9991 The keyword `typename' is not permitted in a base-specifier or
9992 mem-initializer; in these contexts a qualified name that
9993 depends on a template-parameter is implicitly assumed to be a
9996 is to assume that we have seen the `typename' keyword at this
9998 nested_name_specifier_p
9999 = (cp_parser_nested_name_specifier_opt (parser,
10000 /*typename_keyword_p=*/true,
10001 /*check_dependency_p=*/true,
10003 /*is_declaration=*/true)
10005 if (nested_name_specifier_p)
10006 template_p = cp_parser_optional_template_keyword (parser);
10007 /* If there is a `::' operator or a nested-name-specifier, then we
10008 are definitely looking for a class-name. */
10009 if (global_scope_p || nested_name_specifier_p)
10010 return cp_parser_class_name (parser,
10011 /*typename_keyword_p=*/true,
10012 /*template_keyword_p=*/template_p,
10014 /*check_dependency_p=*/true,
10015 /*class_head_p=*/false,
10016 /*is_declaration=*/true);
10017 /* Otherwise, we could also be looking for an ordinary identifier. */
10018 cp_parser_parse_tentatively (parser);
10019 /* Try a class-name. */
10020 id = cp_parser_class_name (parser,
10021 /*typename_keyword_p=*/true,
10022 /*template_keyword_p=*/false,
10024 /*check_dependency_p=*/true,
10025 /*class_head_p=*/false,
10026 /*is_declaration=*/true);
10027 /* If we found one, we're done. */
10028 if (cp_parser_parse_definitely (parser))
10030 /* Otherwise, look for an ordinary identifier. */
10031 return cp_parser_identifier (parser);
10034 /* Overloading [gram.over] */
10036 /* Parse an operator-function-id.
10038 operator-function-id:
10041 Returns an IDENTIFIER_NODE for the operator which is a
10042 human-readable spelling of the identifier, e.g., `operator +'. */
10045 cp_parser_operator_function_id (cp_parser* parser)
10047 /* Look for the `operator' keyword. */
10048 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10049 return error_mark_node;
10050 /* And then the name of the operator itself. */
10051 return cp_parser_operator (parser);
10054 /* Parse an operator.
10057 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10058 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10059 || ++ -- , ->* -> () []
10066 Returns an IDENTIFIER_NODE for the operator which is a
10067 human-readable spelling of the identifier, e.g., `operator +'. */
10070 cp_parser_operator (cp_parser* parser)
10072 tree id = NULL_TREE;
10075 /* Peek at the next token. */
10076 token = cp_lexer_peek_token (parser->lexer);
10077 /* Figure out which operator we have. */
10078 switch (token->type)
10084 /* The keyword should be either `new' or `delete'. */
10085 if (token->keyword == RID_NEW)
10087 else if (token->keyword == RID_DELETE)
10092 /* Consume the `new' or `delete' token. */
10093 cp_lexer_consume_token (parser->lexer);
10095 /* Peek at the next token. */
10096 token = cp_lexer_peek_token (parser->lexer);
10097 /* If it's a `[' token then this is the array variant of the
10099 if (token->type == CPP_OPEN_SQUARE)
10101 /* Consume the `[' token. */
10102 cp_lexer_consume_token (parser->lexer);
10103 /* Look for the `]' token. */
10104 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10105 id = ansi_opname (op == NEW_EXPR
10106 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10108 /* Otherwise, we have the non-array variant. */
10110 id = ansi_opname (op);
10116 id = ansi_opname (PLUS_EXPR);
10120 id = ansi_opname (MINUS_EXPR);
10124 id = ansi_opname (MULT_EXPR);
10128 id = ansi_opname (TRUNC_DIV_EXPR);
10132 id = ansi_opname (TRUNC_MOD_EXPR);
10136 id = ansi_opname (BIT_XOR_EXPR);
10140 id = ansi_opname (BIT_AND_EXPR);
10144 id = ansi_opname (BIT_IOR_EXPR);
10148 id = ansi_opname (BIT_NOT_EXPR);
10152 id = ansi_opname (TRUTH_NOT_EXPR);
10156 id = ansi_assopname (NOP_EXPR);
10160 id = ansi_opname (LT_EXPR);
10164 id = ansi_opname (GT_EXPR);
10168 id = ansi_assopname (PLUS_EXPR);
10172 id = ansi_assopname (MINUS_EXPR);
10176 id = ansi_assopname (MULT_EXPR);
10180 id = ansi_assopname (TRUNC_DIV_EXPR);
10184 id = ansi_assopname (TRUNC_MOD_EXPR);
10188 id = ansi_assopname (BIT_XOR_EXPR);
10192 id = ansi_assopname (BIT_AND_EXPR);
10196 id = ansi_assopname (BIT_IOR_EXPR);
10200 id = ansi_opname (LSHIFT_EXPR);
10204 id = ansi_opname (RSHIFT_EXPR);
10207 case CPP_LSHIFT_EQ:
10208 id = ansi_assopname (LSHIFT_EXPR);
10211 case CPP_RSHIFT_EQ:
10212 id = ansi_assopname (RSHIFT_EXPR);
10216 id = ansi_opname (EQ_EXPR);
10220 id = ansi_opname (NE_EXPR);
10224 id = ansi_opname (LE_EXPR);
10227 case CPP_GREATER_EQ:
10228 id = ansi_opname (GE_EXPR);
10232 id = ansi_opname (TRUTH_ANDIF_EXPR);
10236 id = ansi_opname (TRUTH_ORIF_EXPR);
10239 case CPP_PLUS_PLUS:
10240 id = ansi_opname (POSTINCREMENT_EXPR);
10243 case CPP_MINUS_MINUS:
10244 id = ansi_opname (PREDECREMENT_EXPR);
10248 id = ansi_opname (COMPOUND_EXPR);
10251 case CPP_DEREF_STAR:
10252 id = ansi_opname (MEMBER_REF);
10256 id = ansi_opname (COMPONENT_REF);
10259 case CPP_OPEN_PAREN:
10260 /* Consume the `('. */
10261 cp_lexer_consume_token (parser->lexer);
10262 /* Look for the matching `)'. */
10263 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10264 return ansi_opname (CALL_EXPR);
10266 case CPP_OPEN_SQUARE:
10267 /* Consume the `['. */
10268 cp_lexer_consume_token (parser->lexer);
10269 /* Look for the matching `]'. */
10270 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10271 return ansi_opname (ARRAY_REF);
10274 /* Anything else is an error. */
10278 /* If we have selected an identifier, we need to consume the
10281 cp_lexer_consume_token (parser->lexer);
10282 /* Otherwise, no valid operator name was present. */
10285 cp_parser_error (parser, "expected operator");
10286 id = error_mark_node;
10292 /* Parse a template-declaration.
10294 template-declaration:
10295 export [opt] template < template-parameter-list > declaration
10297 If MEMBER_P is TRUE, this template-declaration occurs within a
10300 The grammar rule given by the standard isn't correct. What
10301 is really meant is:
10303 template-declaration:
10304 export [opt] template-parameter-list-seq
10305 decl-specifier-seq [opt] init-declarator [opt] ;
10306 export [opt] template-parameter-list-seq
10307 function-definition
10309 template-parameter-list-seq:
10310 template-parameter-list-seq [opt]
10311 template < template-parameter-list > */
10314 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10316 /* Check for `export'. */
10317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10319 /* Consume the `export' token. */
10320 cp_lexer_consume_token (parser->lexer);
10321 /* Warn that we do not support `export'. */
10322 warning (0, "keyword %<export%> not implemented, and will be ignored");
10325 cp_parser_template_declaration_after_export (parser, member_p);
10328 /* Parse a template-parameter-list.
10330 template-parameter-list:
10332 template-parameter-list , template-parameter
10334 Returns a TREE_LIST. Each node represents a template parameter.
10335 The nodes are connected via their TREE_CHAINs. */
10338 cp_parser_template_parameter_list (cp_parser* parser)
10340 tree parameter_list = NULL_TREE;
10342 begin_template_parm_list ();
10347 bool is_parameter_pack;
10348 location_t parm_loc;
10350 /* Parse the template-parameter. */
10351 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10352 parameter = cp_parser_template_parameter (parser,
10354 &is_parameter_pack);
10355 /* Add it to the list. */
10356 if (parameter != error_mark_node)
10357 parameter_list = process_template_parm (parameter_list,
10361 is_parameter_pack);
10364 tree err_parm = build_tree_list (parameter, parameter);
10365 TREE_VALUE (err_parm) = error_mark_node;
10366 parameter_list = chainon (parameter_list, err_parm);
10369 /* If the next token is not a `,', we're done. */
10370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10372 /* Otherwise, consume the `,' token. */
10373 cp_lexer_consume_token (parser->lexer);
10376 return end_template_parm_list (parameter_list);
10379 /* Parse a template-parameter.
10381 template-parameter:
10383 parameter-declaration
10385 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10386 the parameter. The TREE_PURPOSE is the default value, if any.
10387 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10388 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10389 set to true iff this parameter is a parameter pack. */
10392 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10393 bool *is_parameter_pack)
10396 cp_parameter_declarator *parameter_declarator;
10397 cp_declarator *id_declarator;
10400 /* Assume it is a type parameter or a template parameter. */
10401 *is_non_type = false;
10402 /* Assume it not a parameter pack. */
10403 *is_parameter_pack = false;
10404 /* Peek at the next token. */
10405 token = cp_lexer_peek_token (parser->lexer);
10406 /* If it is `class' or `template', we have a type-parameter. */
10407 if (token->keyword == RID_TEMPLATE)
10408 return cp_parser_type_parameter (parser, is_parameter_pack);
10409 /* If it is `class' or `typename' we do not know yet whether it is a
10410 type parameter or a non-type parameter. Consider:
10412 template <typename T, typename T::X X> ...
10416 template <class C, class D*> ...
10418 Here, the first parameter is a type parameter, and the second is
10419 a non-type parameter. We can tell by looking at the token after
10420 the identifier -- if it is a `,', `=', or `>' then we have a type
10422 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10424 /* Peek at the token after `class' or `typename'. */
10425 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10426 /* If it's an ellipsis, we have a template type parameter
10428 if (token->type == CPP_ELLIPSIS)
10429 return cp_parser_type_parameter (parser, is_parameter_pack);
10430 /* If it's an identifier, skip it. */
10431 if (token->type == CPP_NAME)
10432 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10433 /* Now, see if the token looks like the end of a template
10435 if (token->type == CPP_COMMA
10436 || token->type == CPP_EQ
10437 || token->type == CPP_GREATER)
10438 return cp_parser_type_parameter (parser, is_parameter_pack);
10441 /* Otherwise, it is a non-type parameter.
10445 When parsing a default template-argument for a non-type
10446 template-parameter, the first non-nested `>' is taken as the end
10447 of the template parameter-list rather than a greater-than
10449 *is_non_type = true;
10450 parameter_declarator
10451 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10452 /*parenthesized_p=*/NULL);
10454 /* If the parameter declaration is marked as a parameter pack, set
10455 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10456 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10458 if (parameter_declarator
10459 && parameter_declarator->declarator
10460 && parameter_declarator->declarator->parameter_pack_p)
10462 *is_parameter_pack = true;
10463 parameter_declarator->declarator->parameter_pack_p = false;
10466 /* If the next token is an ellipsis, and we don't already have it
10467 marked as a parameter pack, then we have a parameter pack (that
10468 has no declarator). */
10469 if (!*is_parameter_pack
10470 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10471 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10473 /* Consume the `...'. */
10474 cp_lexer_consume_token (parser->lexer);
10475 maybe_warn_variadic_templates ();
10477 *is_parameter_pack = true;
10479 /* We might end up with a pack expansion as the type of the non-type
10480 template parameter, in which case this is a non-type template
10482 else if (parameter_declarator
10483 && parameter_declarator->decl_specifiers.type
10484 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10486 *is_parameter_pack = true;
10487 parameter_declarator->decl_specifiers.type =
10488 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10491 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10493 /* Parameter packs cannot have default arguments. However, a
10494 user may try to do so, so we'll parse them and give an
10495 appropriate diagnostic here. */
10497 /* Consume the `='. */
10498 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10499 cp_lexer_consume_token (parser->lexer);
10501 /* Find the name of the parameter pack. */
10502 id_declarator = parameter_declarator->declarator;
10503 while (id_declarator && id_declarator->kind != cdk_id)
10504 id_declarator = id_declarator->declarator;
10506 if (id_declarator && id_declarator->kind == cdk_id)
10507 error_at (start_token->location,
10508 "template parameter pack %qD cannot have a default argument",
10509 id_declarator->u.id.unqualified_name);
10511 error_at (start_token->location,
10512 "template parameter pack cannot have a default argument");
10514 /* Parse the default argument, but throw away the result. */
10515 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10518 parm = grokdeclarator (parameter_declarator->declarator,
10519 ¶meter_declarator->decl_specifiers,
10520 TPARM, /*initialized=*/0,
10521 /*attrlist=*/NULL);
10522 if (parm == error_mark_node)
10523 return error_mark_node;
10525 return build_tree_list (parameter_declarator->default_argument, parm);
10528 /* Parse a type-parameter.
10531 class identifier [opt]
10532 class identifier [opt] = type-id
10533 typename identifier [opt]
10534 typename identifier [opt] = type-id
10535 template < template-parameter-list > class identifier [opt]
10536 template < template-parameter-list > class identifier [opt]
10539 GNU Extension (variadic templates):
10542 class ... identifier [opt]
10543 typename ... identifier [opt]
10545 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10546 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10547 the declaration of the parameter.
10549 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10552 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10557 /* Look for a keyword to tell us what kind of parameter this is. */
10558 token = cp_parser_require (parser, CPP_KEYWORD,
10559 "%<class%>, %<typename%>, or %<template%>");
10561 return error_mark_node;
10563 switch (token->keyword)
10569 tree default_argument;
10571 /* If the next token is an ellipsis, we have a template
10573 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10575 /* Consume the `...' token. */
10576 cp_lexer_consume_token (parser->lexer);
10577 maybe_warn_variadic_templates ();
10579 *is_parameter_pack = true;
10582 /* If the next token is an identifier, then it names the
10584 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10585 identifier = cp_parser_identifier (parser);
10587 identifier = NULL_TREE;
10589 /* Create the parameter. */
10590 parameter = finish_template_type_parm (class_type_node, identifier);
10592 /* If the next token is an `=', we have a default argument. */
10593 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10595 /* Consume the `=' token. */
10596 cp_lexer_consume_token (parser->lexer);
10597 /* Parse the default-argument. */
10598 push_deferring_access_checks (dk_no_deferred);
10599 default_argument = cp_parser_type_id (parser);
10601 /* Template parameter packs cannot have default
10603 if (*is_parameter_pack)
10606 error_at (token->location,
10607 "template parameter pack %qD cannot have a "
10608 "default argument", identifier);
10610 error_at (token->location,
10611 "template parameter packs cannot have "
10612 "default arguments");
10613 default_argument = NULL_TREE;
10615 pop_deferring_access_checks ();
10618 default_argument = NULL_TREE;
10620 /* Create the combined representation of the parameter and the
10621 default argument. */
10622 parameter = build_tree_list (default_argument, parameter);
10628 tree parameter_list;
10630 tree default_argument;
10632 /* Look for the `<'. */
10633 cp_parser_require (parser, CPP_LESS, "%<<%>");
10634 /* Parse the template-parameter-list. */
10635 parameter_list = cp_parser_template_parameter_list (parser);
10636 /* Look for the `>'. */
10637 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10638 /* Look for the `class' keyword. */
10639 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10640 /* If the next token is an ellipsis, we have a template
10642 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10644 /* Consume the `...' token. */
10645 cp_lexer_consume_token (parser->lexer);
10646 maybe_warn_variadic_templates ();
10648 *is_parameter_pack = true;
10650 /* If the next token is an `=', then there is a
10651 default-argument. If the next token is a `>', we are at
10652 the end of the parameter-list. If the next token is a `,',
10653 then we are at the end of this parameter. */
10654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10655 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10656 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10658 identifier = cp_parser_identifier (parser);
10659 /* Treat invalid names as if the parameter were nameless. */
10660 if (identifier == error_mark_node)
10661 identifier = NULL_TREE;
10664 identifier = NULL_TREE;
10666 /* Create the template parameter. */
10667 parameter = finish_template_template_parm (class_type_node,
10670 /* If the next token is an `=', then there is a
10671 default-argument. */
10672 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10676 /* Consume the `='. */
10677 cp_lexer_consume_token (parser->lexer);
10678 /* Parse the id-expression. */
10679 push_deferring_access_checks (dk_no_deferred);
10680 /* save token before parsing the id-expression, for error
10682 token = cp_lexer_peek_token (parser->lexer);
10684 = cp_parser_id_expression (parser,
10685 /*template_keyword_p=*/false,
10686 /*check_dependency_p=*/true,
10687 /*template_p=*/&is_template,
10688 /*declarator_p=*/false,
10689 /*optional_p=*/false);
10690 if (TREE_CODE (default_argument) == TYPE_DECL)
10691 /* If the id-expression was a template-id that refers to
10692 a template-class, we already have the declaration here,
10693 so no further lookup is needed. */
10696 /* Look up the name. */
10698 = cp_parser_lookup_name (parser, default_argument,
10700 /*is_template=*/is_template,
10701 /*is_namespace=*/false,
10702 /*check_dependency=*/true,
10703 /*ambiguous_decls=*/NULL,
10705 /* See if the default argument is valid. */
10707 = check_template_template_default_arg (default_argument);
10709 /* Template parameter packs cannot have default
10711 if (*is_parameter_pack)
10714 error_at (token->location,
10715 "template parameter pack %qD cannot "
10716 "have a default argument",
10719 error_at (token->location, "template parameter packs cannot "
10720 "have default arguments");
10721 default_argument = NULL_TREE;
10723 pop_deferring_access_checks ();
10726 default_argument = NULL_TREE;
10728 /* Create the combined representation of the parameter and the
10729 default argument. */
10730 parameter = build_tree_list (default_argument, parameter);
10735 gcc_unreachable ();
10742 /* Parse a template-id.
10745 template-name < template-argument-list [opt] >
10747 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10748 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10749 returned. Otherwise, if the template-name names a function, or set
10750 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10751 names a class, returns a TYPE_DECL for the specialization.
10753 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10754 uninstantiated templates. */
10757 cp_parser_template_id (cp_parser *parser,
10758 bool template_keyword_p,
10759 bool check_dependency_p,
10760 bool is_declaration)
10766 cp_token_position start_of_id = 0;
10767 deferred_access_check *chk;
10768 VEC (deferred_access_check,gc) *access_check;
10769 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10770 bool is_identifier;
10772 /* If the next token corresponds to a template-id, there is no need
10774 next_token = cp_lexer_peek_token (parser->lexer);
10775 if (next_token->type == CPP_TEMPLATE_ID)
10777 struct tree_check *check_value;
10779 /* Get the stored value. */
10780 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10781 /* Perform any access checks that were deferred. */
10782 access_check = check_value->checks;
10786 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10789 perform_or_defer_access_check (chk->binfo,
10794 /* Return the stored value. */
10795 return check_value->value;
10798 /* Avoid performing name lookup if there is no possibility of
10799 finding a template-id. */
10800 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10801 || (next_token->type == CPP_NAME
10802 && !cp_parser_nth_token_starts_template_argument_list_p
10805 cp_parser_error (parser, "expected template-id");
10806 return error_mark_node;
10809 /* Remember where the template-id starts. */
10810 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10811 start_of_id = cp_lexer_token_position (parser->lexer, false);
10813 push_deferring_access_checks (dk_deferred);
10815 /* Parse the template-name. */
10816 is_identifier = false;
10817 token = cp_lexer_peek_token (parser->lexer);
10818 templ = cp_parser_template_name (parser, template_keyword_p,
10819 check_dependency_p,
10822 if (templ == error_mark_node || is_identifier)
10824 pop_deferring_access_checks ();
10828 /* If we find the sequence `[:' after a template-name, it's probably
10829 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10830 parse correctly the argument list. */
10831 next_token = cp_lexer_peek_token (parser->lexer);
10832 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10833 if (next_token->type == CPP_OPEN_SQUARE
10834 && next_token->flags & DIGRAPH
10835 && next_token_2->type == CPP_COLON
10836 && !(next_token_2->flags & PREV_WHITE))
10838 cp_parser_parse_tentatively (parser);
10839 /* Change `:' into `::'. */
10840 next_token_2->type = CPP_SCOPE;
10841 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10843 cp_lexer_consume_token (parser->lexer);
10845 /* Parse the arguments. */
10846 arguments = cp_parser_enclosed_template_argument_list (parser);
10847 if (!cp_parser_parse_definitely (parser))
10849 /* If we couldn't parse an argument list, then we revert our changes
10850 and return simply an error. Maybe this is not a template-id
10852 next_token_2->type = CPP_COLON;
10853 cp_parser_error (parser, "expected %<<%>");
10854 pop_deferring_access_checks ();
10855 return error_mark_node;
10857 /* Otherwise, emit an error about the invalid digraph, but continue
10858 parsing because we got our argument list. */
10859 if (permerror (next_token->location,
10860 "%<<::%> cannot begin a template-argument list"))
10862 static bool hint = false;
10863 inform (next_token->location,
10864 "%<<:%> is an alternate spelling for %<[%>."
10865 " Insert whitespace between %<<%> and %<::%>");
10866 if (!hint && !flag_permissive)
10868 inform (next_token->location, "(if you use %<-fpermissive%>"
10869 " G++ will accept your code)");
10876 /* Look for the `<' that starts the template-argument-list. */
10877 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10879 pop_deferring_access_checks ();
10880 return error_mark_node;
10882 /* Parse the arguments. */
10883 arguments = cp_parser_enclosed_template_argument_list (parser);
10886 /* Build a representation of the specialization. */
10887 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10888 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10889 else if (DECL_CLASS_TEMPLATE_P (templ)
10890 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10892 bool entering_scope;
10893 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10894 template (rather than some instantiation thereof) only if
10895 is not nested within some other construct. For example, in
10896 "template <typename T> void f(T) { A<T>::", A<T> is just an
10897 instantiation of A. */
10898 entering_scope = (template_parm_scope_p ()
10899 && cp_lexer_next_token_is (parser->lexer,
10902 = finish_template_type (templ, arguments, entering_scope);
10906 /* If it's not a class-template or a template-template, it should be
10907 a function-template. */
10908 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10909 || TREE_CODE (templ) == OVERLOAD
10910 || BASELINK_P (templ)));
10912 template_id = lookup_template_function (templ, arguments);
10915 /* If parsing tentatively, replace the sequence of tokens that makes
10916 up the template-id with a CPP_TEMPLATE_ID token. That way,
10917 should we re-parse the token stream, we will not have to repeat
10918 the effort required to do the parse, nor will we issue duplicate
10919 error messages about problems during instantiation of the
10923 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10925 /* Reset the contents of the START_OF_ID token. */
10926 token->type = CPP_TEMPLATE_ID;
10927 /* Retrieve any deferred checks. Do not pop this access checks yet
10928 so the memory will not be reclaimed during token replacing below. */
10929 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10930 token->u.tree_check_value->value = template_id;
10931 token->u.tree_check_value->checks = get_deferred_access_checks ();
10932 token->keyword = RID_MAX;
10934 /* Purge all subsequent tokens. */
10935 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10937 /* ??? Can we actually assume that, if template_id ==
10938 error_mark_node, we will have issued a diagnostic to the
10939 user, as opposed to simply marking the tentative parse as
10941 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10942 error_at (token->location, "parse error in template argument list");
10945 pop_deferring_access_checks ();
10946 return template_id;
10949 /* Parse a template-name.
10954 The standard should actually say:
10958 operator-function-id
10960 A defect report has been filed about this issue.
10962 A conversion-function-id cannot be a template name because they cannot
10963 be part of a template-id. In fact, looking at this code:
10965 a.operator K<int>()
10967 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10968 It is impossible to call a templated conversion-function-id with an
10969 explicit argument list, since the only allowed template parameter is
10970 the type to which it is converting.
10972 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10973 `template' keyword, in a construction like:
10977 In that case `f' is taken to be a template-name, even though there
10978 is no way of knowing for sure.
10980 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10981 name refers to a set of overloaded functions, at least one of which
10982 is a template, or an IDENTIFIER_NODE with the name of the template,
10983 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10984 names are looked up inside uninstantiated templates. */
10987 cp_parser_template_name (cp_parser* parser,
10988 bool template_keyword_p,
10989 bool check_dependency_p,
10990 bool is_declaration,
10991 bool *is_identifier)
10996 cp_token *token = cp_lexer_peek_token (parser->lexer);
10998 /* If the next token is `operator', then we have either an
10999 operator-function-id or a conversion-function-id. */
11000 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11002 /* We don't know whether we're looking at an
11003 operator-function-id or a conversion-function-id. */
11004 cp_parser_parse_tentatively (parser);
11005 /* Try an operator-function-id. */
11006 identifier = cp_parser_operator_function_id (parser);
11007 /* If that didn't work, try a conversion-function-id. */
11008 if (!cp_parser_parse_definitely (parser))
11010 cp_parser_error (parser, "expected template-name");
11011 return error_mark_node;
11014 /* Look for the identifier. */
11016 identifier = cp_parser_identifier (parser);
11018 /* If we didn't find an identifier, we don't have a template-id. */
11019 if (identifier == error_mark_node)
11020 return error_mark_node;
11022 /* If the name immediately followed the `template' keyword, then it
11023 is a template-name. However, if the next token is not `<', then
11024 we do not treat it as a template-name, since it is not being used
11025 as part of a template-id. This enables us to handle constructs
11028 template <typename T> struct S { S(); };
11029 template <typename T> S<T>::S();
11031 correctly. We would treat `S' as a template -- if it were `S<T>'
11032 -- but we do not if there is no `<'. */
11034 if (processing_template_decl
11035 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11037 /* In a declaration, in a dependent context, we pretend that the
11038 "template" keyword was present in order to improve error
11039 recovery. For example, given:
11041 template <typename T> void f(T::X<int>);
11043 we want to treat "X<int>" as a template-id. */
11045 && !template_keyword_p
11046 && parser->scope && TYPE_P (parser->scope)
11047 && check_dependency_p
11048 && dependent_scope_p (parser->scope)
11049 /* Do not do this for dtors (or ctors), since they never
11050 need the template keyword before their name. */
11051 && !constructor_name_p (identifier, parser->scope))
11053 cp_token_position start = 0;
11055 /* Explain what went wrong. */
11056 error_at (token->location, "non-template %qD used as template",
11058 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11059 parser->scope, identifier);
11060 /* If parsing tentatively, find the location of the "<" token. */
11061 if (cp_parser_simulate_error (parser))
11062 start = cp_lexer_token_position (parser->lexer, true);
11063 /* Parse the template arguments so that we can issue error
11064 messages about them. */
11065 cp_lexer_consume_token (parser->lexer);
11066 cp_parser_enclosed_template_argument_list (parser);
11067 /* Skip tokens until we find a good place from which to
11068 continue parsing. */
11069 cp_parser_skip_to_closing_parenthesis (parser,
11070 /*recovering=*/true,
11072 /*consume_paren=*/false);
11073 /* If parsing tentatively, permanently remove the
11074 template argument list. That will prevent duplicate
11075 error messages from being issued about the missing
11076 "template" keyword. */
11078 cp_lexer_purge_tokens_after (parser->lexer, start);
11080 *is_identifier = true;
11084 /* If the "template" keyword is present, then there is generally
11085 no point in doing name-lookup, so we just return IDENTIFIER.
11086 But, if the qualifying scope is non-dependent then we can
11087 (and must) do name-lookup normally. */
11088 if (template_keyword_p
11090 || (TYPE_P (parser->scope)
11091 && dependent_type_p (parser->scope))))
11095 /* Look up the name. */
11096 decl = cp_parser_lookup_name (parser, identifier,
11098 /*is_template=*/true,
11099 /*is_namespace=*/false,
11100 check_dependency_p,
11101 /*ambiguous_decls=*/NULL,
11104 /* If DECL is a template, then the name was a template-name. */
11105 if (TREE_CODE (decl) == TEMPLATE_DECL)
11109 tree fn = NULL_TREE;
11111 /* The standard does not explicitly indicate whether a name that
11112 names a set of overloaded declarations, some of which are
11113 templates, is a template-name. However, such a name should
11114 be a template-name; otherwise, there is no way to form a
11115 template-id for the overloaded templates. */
11116 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11117 if (TREE_CODE (fns) == OVERLOAD)
11118 for (fn = fns; fn; fn = OVL_NEXT (fn))
11119 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11124 /* The name does not name a template. */
11125 cp_parser_error (parser, "expected template-name");
11126 return error_mark_node;
11130 /* If DECL is dependent, and refers to a function, then just return
11131 its name; we will look it up again during template instantiation. */
11132 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11134 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11135 if (TYPE_P (scope) && dependent_type_p (scope))
11142 /* Parse a template-argument-list.
11144 template-argument-list:
11145 template-argument ... [opt]
11146 template-argument-list , template-argument ... [opt]
11148 Returns a TREE_VEC containing the arguments. */
11151 cp_parser_template_argument_list (cp_parser* parser)
11153 tree fixed_args[10];
11154 unsigned n_args = 0;
11155 unsigned alloced = 10;
11156 tree *arg_ary = fixed_args;
11158 bool saved_in_template_argument_list_p;
11160 bool saved_non_ice_p;
11162 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11163 parser->in_template_argument_list_p = true;
11164 /* Even if the template-id appears in an integral
11165 constant-expression, the contents of the argument list do
11167 saved_ice_p = parser->integral_constant_expression_p;
11168 parser->integral_constant_expression_p = false;
11169 saved_non_ice_p = parser->non_integral_constant_expression_p;
11170 parser->non_integral_constant_expression_p = false;
11171 /* Parse the arguments. */
11177 /* Consume the comma. */
11178 cp_lexer_consume_token (parser->lexer);
11180 /* Parse the template-argument. */
11181 argument = cp_parser_template_argument (parser);
11183 /* If the next token is an ellipsis, we're expanding a template
11185 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11187 if (argument == error_mark_node)
11189 cp_token *token = cp_lexer_peek_token (parser->lexer);
11190 error_at (token->location,
11191 "expected parameter pack before %<...%>");
11193 /* Consume the `...' token. */
11194 cp_lexer_consume_token (parser->lexer);
11196 /* Make the argument into a TYPE_PACK_EXPANSION or
11197 EXPR_PACK_EXPANSION. */
11198 argument = make_pack_expansion (argument);
11201 if (n_args == alloced)
11205 if (arg_ary == fixed_args)
11207 arg_ary = XNEWVEC (tree, alloced);
11208 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11211 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11213 arg_ary[n_args++] = argument;
11215 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11217 vec = make_tree_vec (n_args);
11220 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11222 if (arg_ary != fixed_args)
11224 parser->non_integral_constant_expression_p = saved_non_ice_p;
11225 parser->integral_constant_expression_p = saved_ice_p;
11226 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11230 /* Parse a template-argument.
11233 assignment-expression
11237 The representation is that of an assignment-expression, type-id, or
11238 id-expression -- except that the qualified id-expression is
11239 evaluated, so that the value returned is either a DECL or an
11242 Although the standard says "assignment-expression", it forbids
11243 throw-expressions or assignments in the template argument.
11244 Therefore, we use "conditional-expression" instead. */
11247 cp_parser_template_argument (cp_parser* parser)
11252 bool maybe_type_id = false;
11253 cp_token *token = NULL, *argument_start_token = NULL;
11256 /* There's really no way to know what we're looking at, so we just
11257 try each alternative in order.
11261 In a template-argument, an ambiguity between a type-id and an
11262 expression is resolved to a type-id, regardless of the form of
11263 the corresponding template-parameter.
11265 Therefore, we try a type-id first. */
11266 cp_parser_parse_tentatively (parser);
11267 argument = cp_parser_template_type_arg (parser);
11268 /* If there was no error parsing the type-id but the next token is a
11269 '>>', our behavior depends on which dialect of C++ we're
11270 parsing. In C++98, we probably found a typo for '> >'. But there
11271 are type-id which are also valid expressions. For instance:
11273 struct X { int operator >> (int); };
11274 template <int V> struct Foo {};
11277 Here 'X()' is a valid type-id of a function type, but the user just
11278 wanted to write the expression "X() >> 5". Thus, we remember that we
11279 found a valid type-id, but we still try to parse the argument as an
11280 expression to see what happens.
11282 In C++0x, the '>>' will be considered two separate '>'
11284 if (!cp_parser_error_occurred (parser)
11285 && cxx_dialect == cxx98
11286 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11288 maybe_type_id = true;
11289 cp_parser_abort_tentative_parse (parser);
11293 /* If the next token isn't a `,' or a `>', then this argument wasn't
11294 really finished. This means that the argument is not a valid
11296 if (!cp_parser_next_token_ends_template_argument_p (parser))
11297 cp_parser_error (parser, "expected template-argument");
11298 /* If that worked, we're done. */
11299 if (cp_parser_parse_definitely (parser))
11302 /* We're still not sure what the argument will be. */
11303 cp_parser_parse_tentatively (parser);
11304 /* Try a template. */
11305 argument_start_token = cp_lexer_peek_token (parser->lexer);
11306 argument = cp_parser_id_expression (parser,
11307 /*template_keyword_p=*/false,
11308 /*check_dependency_p=*/true,
11310 /*declarator_p=*/false,
11311 /*optional_p=*/false);
11312 /* If the next token isn't a `,' or a `>', then this argument wasn't
11313 really finished. */
11314 if (!cp_parser_next_token_ends_template_argument_p (parser))
11315 cp_parser_error (parser, "expected template-argument");
11316 if (!cp_parser_error_occurred (parser))
11318 /* Figure out what is being referred to. If the id-expression
11319 was for a class template specialization, then we will have a
11320 TYPE_DECL at this point. There is no need to do name lookup
11321 at this point in that case. */
11322 if (TREE_CODE (argument) != TYPE_DECL)
11323 argument = cp_parser_lookup_name (parser, argument,
11325 /*is_template=*/template_p,
11326 /*is_namespace=*/false,
11327 /*check_dependency=*/true,
11328 /*ambiguous_decls=*/NULL,
11329 argument_start_token->location);
11330 if (TREE_CODE (argument) != TEMPLATE_DECL
11331 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11332 cp_parser_error (parser, "expected template-name");
11334 if (cp_parser_parse_definitely (parser))
11336 /* It must be a non-type argument. There permitted cases are given
11337 in [temp.arg.nontype]:
11339 -- an integral constant-expression of integral or enumeration
11342 -- the name of a non-type template-parameter; or
11344 -- the name of an object or function with external linkage...
11346 -- the address of an object or function with external linkage...
11348 -- a pointer to member... */
11349 /* Look for a non-type template parameter. */
11350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11352 cp_parser_parse_tentatively (parser);
11353 argument = cp_parser_primary_expression (parser,
11354 /*address_p=*/false,
11356 /*template_arg_p=*/true,
11358 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11359 || !cp_parser_next_token_ends_template_argument_p (parser))
11360 cp_parser_simulate_error (parser);
11361 if (cp_parser_parse_definitely (parser))
11365 /* If the next token is "&", the argument must be the address of an
11366 object or function with external linkage. */
11367 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11369 cp_lexer_consume_token (parser->lexer);
11370 /* See if we might have an id-expression. */
11371 token = cp_lexer_peek_token (parser->lexer);
11372 if (token->type == CPP_NAME
11373 || token->keyword == RID_OPERATOR
11374 || token->type == CPP_SCOPE
11375 || token->type == CPP_TEMPLATE_ID
11376 || token->type == CPP_NESTED_NAME_SPECIFIER)
11378 cp_parser_parse_tentatively (parser);
11379 argument = cp_parser_primary_expression (parser,
11382 /*template_arg_p=*/true,
11384 if (cp_parser_error_occurred (parser)
11385 || !cp_parser_next_token_ends_template_argument_p (parser))
11386 cp_parser_abort_tentative_parse (parser);
11391 if (TREE_CODE (argument) == INDIRECT_REF)
11393 gcc_assert (REFERENCE_REF_P (argument));
11394 argument = TREE_OPERAND (argument, 0);
11397 /* If we're in a template, we represent a qualified-id referring
11398 to a static data member as a SCOPE_REF even if the scope isn't
11399 dependent so that we can check access control later. */
11401 if (TREE_CODE (probe) == SCOPE_REF)
11402 probe = TREE_OPERAND (probe, 1);
11403 if (TREE_CODE (probe) == VAR_DECL)
11405 /* A variable without external linkage might still be a
11406 valid constant-expression, so no error is issued here
11407 if the external-linkage check fails. */
11408 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11409 cp_parser_simulate_error (parser);
11411 else if (is_overloaded_fn (argument))
11412 /* All overloaded functions are allowed; if the external
11413 linkage test does not pass, an error will be issued
11417 && (TREE_CODE (argument) == OFFSET_REF
11418 || TREE_CODE (argument) == SCOPE_REF))
11419 /* A pointer-to-member. */
11421 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11424 cp_parser_simulate_error (parser);
11426 if (cp_parser_parse_definitely (parser))
11429 argument = build_x_unary_op (ADDR_EXPR, argument,
11430 tf_warning_or_error);
11435 /* If the argument started with "&", there are no other valid
11436 alternatives at this point. */
11439 cp_parser_error (parser, "invalid non-type template argument");
11440 return error_mark_node;
11443 /* If the argument wasn't successfully parsed as a type-id followed
11444 by '>>', the argument can only be a constant expression now.
11445 Otherwise, we try parsing the constant-expression tentatively,
11446 because the argument could really be a type-id. */
11448 cp_parser_parse_tentatively (parser);
11449 argument = cp_parser_constant_expression (parser,
11450 /*allow_non_constant_p=*/false,
11451 /*non_constant_p=*/NULL);
11452 argument = fold_non_dependent_expr (argument);
11453 if (!maybe_type_id)
11455 if (!cp_parser_next_token_ends_template_argument_p (parser))
11456 cp_parser_error (parser, "expected template-argument");
11457 if (cp_parser_parse_definitely (parser))
11459 /* We did our best to parse the argument as a non type-id, but that
11460 was the only alternative that matched (albeit with a '>' after
11461 it). We can assume it's just a typo from the user, and a
11462 diagnostic will then be issued. */
11463 return cp_parser_template_type_arg (parser);
11466 /* Parse an explicit-instantiation.
11468 explicit-instantiation:
11469 template declaration
11471 Although the standard says `declaration', what it really means is:
11473 explicit-instantiation:
11474 template decl-specifier-seq [opt] declarator [opt] ;
11476 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11477 supposed to be allowed. A defect report has been filed about this
11482 explicit-instantiation:
11483 storage-class-specifier template
11484 decl-specifier-seq [opt] declarator [opt] ;
11485 function-specifier template
11486 decl-specifier-seq [opt] declarator [opt] ; */
11489 cp_parser_explicit_instantiation (cp_parser* parser)
11491 int declares_class_or_enum;
11492 cp_decl_specifier_seq decl_specifiers;
11493 tree extension_specifier = NULL_TREE;
11496 /* Look for an (optional) storage-class-specifier or
11497 function-specifier. */
11498 if (cp_parser_allow_gnu_extensions_p (parser))
11500 extension_specifier
11501 = cp_parser_storage_class_specifier_opt (parser);
11502 if (!extension_specifier)
11503 extension_specifier
11504 = cp_parser_function_specifier_opt (parser,
11505 /*decl_specs=*/NULL);
11508 /* Look for the `template' keyword. */
11509 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11510 /* Let the front end know that we are processing an explicit
11512 begin_explicit_instantiation ();
11513 /* [temp.explicit] says that we are supposed to ignore access
11514 control while processing explicit instantiation directives. */
11515 push_deferring_access_checks (dk_no_check);
11516 /* Parse a decl-specifier-seq. */
11517 token = cp_lexer_peek_token (parser->lexer);
11518 cp_parser_decl_specifier_seq (parser,
11519 CP_PARSER_FLAGS_OPTIONAL,
11521 &declares_class_or_enum);
11522 /* If there was exactly one decl-specifier, and it declared a class,
11523 and there's no declarator, then we have an explicit type
11525 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11529 type = check_tag_decl (&decl_specifiers);
11530 /* Turn access control back on for names used during
11531 template instantiation. */
11532 pop_deferring_access_checks ();
11534 do_type_instantiation (type, extension_specifier,
11535 /*complain=*/tf_error);
11539 cp_declarator *declarator;
11542 /* Parse the declarator. */
11544 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11545 /*ctor_dtor_or_conv_p=*/NULL,
11546 /*parenthesized_p=*/NULL,
11547 /*member_p=*/false);
11548 if (declares_class_or_enum & 2)
11549 cp_parser_check_for_definition_in_return_type (declarator,
11550 decl_specifiers.type,
11551 decl_specifiers.type_location);
11552 if (declarator != cp_error_declarator)
11554 decl = grokdeclarator (declarator, &decl_specifiers,
11555 NORMAL, 0, &decl_specifiers.attributes);
11556 /* Turn access control back on for names used during
11557 template instantiation. */
11558 pop_deferring_access_checks ();
11559 /* Do the explicit instantiation. */
11560 do_decl_instantiation (decl, extension_specifier);
11564 pop_deferring_access_checks ();
11565 /* Skip the body of the explicit instantiation. */
11566 cp_parser_skip_to_end_of_statement (parser);
11569 /* We're done with the instantiation. */
11570 end_explicit_instantiation ();
11572 cp_parser_consume_semicolon_at_end_of_statement (parser);
11575 /* Parse an explicit-specialization.
11577 explicit-specialization:
11578 template < > declaration
11580 Although the standard says `declaration', what it really means is:
11582 explicit-specialization:
11583 template <> decl-specifier [opt] init-declarator [opt] ;
11584 template <> function-definition
11585 template <> explicit-specialization
11586 template <> template-declaration */
11589 cp_parser_explicit_specialization (cp_parser* parser)
11591 bool need_lang_pop;
11592 cp_token *token = cp_lexer_peek_token (parser->lexer);
11594 /* Look for the `template' keyword. */
11595 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11596 /* Look for the `<'. */
11597 cp_parser_require (parser, CPP_LESS, "%<<%>");
11598 /* Look for the `>'. */
11599 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11600 /* We have processed another parameter list. */
11601 ++parser->num_template_parameter_lists;
11604 A template ... explicit specialization ... shall not have C
11606 if (current_lang_name == lang_name_c)
11608 error_at (token->location, "template specialization with C linkage");
11609 /* Give it C++ linkage to avoid confusing other parts of the
11611 push_lang_context (lang_name_cplusplus);
11612 need_lang_pop = true;
11615 need_lang_pop = false;
11616 /* Let the front end know that we are beginning a specialization. */
11617 if (!begin_specialization ())
11619 end_specialization ();
11623 /* If the next keyword is `template', we need to figure out whether
11624 or not we're looking a template-declaration. */
11625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11627 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11628 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11629 cp_parser_template_declaration_after_export (parser,
11630 /*member_p=*/false);
11632 cp_parser_explicit_specialization (parser);
11635 /* Parse the dependent declaration. */
11636 cp_parser_single_declaration (parser,
11638 /*member_p=*/false,
11639 /*explicit_specialization_p=*/true,
11640 /*friend_p=*/NULL);
11641 /* We're done with the specialization. */
11642 end_specialization ();
11643 /* For the erroneous case of a template with C linkage, we pushed an
11644 implicit C++ linkage scope; exit that scope now. */
11646 pop_lang_context ();
11647 /* We're done with this parameter list. */
11648 --parser->num_template_parameter_lists;
11651 /* Parse a type-specifier.
11654 simple-type-specifier
11657 elaborated-type-specifier
11665 Returns a representation of the type-specifier. For a
11666 class-specifier, enum-specifier, or elaborated-type-specifier, a
11667 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11669 The parser flags FLAGS is used to control type-specifier parsing.
11671 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11672 in a decl-specifier-seq.
11674 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11675 class-specifier, enum-specifier, or elaborated-type-specifier, then
11676 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11677 if a type is declared; 2 if it is defined. Otherwise, it is set to
11680 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11681 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11682 is set to FALSE. */
11685 cp_parser_type_specifier (cp_parser* parser,
11686 cp_parser_flags flags,
11687 cp_decl_specifier_seq *decl_specs,
11688 bool is_declaration,
11689 int* declares_class_or_enum,
11690 bool* is_cv_qualifier)
11692 tree type_spec = NULL_TREE;
11695 cp_decl_spec ds = ds_last;
11697 /* Assume this type-specifier does not declare a new type. */
11698 if (declares_class_or_enum)
11699 *declares_class_or_enum = 0;
11700 /* And that it does not specify a cv-qualifier. */
11701 if (is_cv_qualifier)
11702 *is_cv_qualifier = false;
11703 /* Peek at the next token. */
11704 token = cp_lexer_peek_token (parser->lexer);
11706 /* If we're looking at a keyword, we can use that to guide the
11707 production we choose. */
11708 keyword = token->keyword;
11712 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11713 goto elaborated_type_specifier;
11715 /* Look for the enum-specifier. */
11716 type_spec = cp_parser_enum_specifier (parser);
11717 /* If that worked, we're done. */
11720 if (declares_class_or_enum)
11721 *declares_class_or_enum = 2;
11723 cp_parser_set_decl_spec_type (decl_specs,
11726 /*user_defined_p=*/true);
11730 goto elaborated_type_specifier;
11732 /* Any of these indicate either a class-specifier, or an
11733 elaborated-type-specifier. */
11737 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11738 goto elaborated_type_specifier;
11740 /* Parse tentatively so that we can back up if we don't find a
11741 class-specifier. */
11742 cp_parser_parse_tentatively (parser);
11743 /* Look for the class-specifier. */
11744 type_spec = cp_parser_class_specifier (parser);
11745 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11746 /* If that worked, we're done. */
11747 if (cp_parser_parse_definitely (parser))
11749 if (declares_class_or_enum)
11750 *declares_class_or_enum = 2;
11752 cp_parser_set_decl_spec_type (decl_specs,
11755 /*user_defined_p=*/true);
11759 /* Fall through. */
11760 elaborated_type_specifier:
11761 /* We're declaring (not defining) a class or enum. */
11762 if (declares_class_or_enum)
11763 *declares_class_or_enum = 1;
11765 /* Fall through. */
11767 /* Look for an elaborated-type-specifier. */
11769 = (cp_parser_elaborated_type_specifier
11771 decl_specs && decl_specs->specs[(int) ds_friend],
11774 cp_parser_set_decl_spec_type (decl_specs,
11777 /*user_defined_p=*/true);
11782 if (is_cv_qualifier)
11783 *is_cv_qualifier = true;
11788 if (is_cv_qualifier)
11789 *is_cv_qualifier = true;
11794 if (is_cv_qualifier)
11795 *is_cv_qualifier = true;
11799 /* The `__complex__' keyword is a GNU extension. */
11807 /* Handle simple keywords. */
11812 ++decl_specs->specs[(int)ds];
11813 decl_specs->any_specifiers_p = true;
11815 return cp_lexer_consume_token (parser->lexer)->u.value;
11818 /* If we do not already have a type-specifier, assume we are looking
11819 at a simple-type-specifier. */
11820 type_spec = cp_parser_simple_type_specifier (parser,
11824 /* If we didn't find a type-specifier, and a type-specifier was not
11825 optional in this context, issue an error message. */
11826 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11828 cp_parser_error (parser, "expected type specifier");
11829 return error_mark_node;
11835 /* Parse a simple-type-specifier.
11837 simple-type-specifier:
11838 :: [opt] nested-name-specifier [opt] type-name
11839 :: [opt] nested-name-specifier template template-id
11854 simple-type-specifier:
11856 decltype ( expression )
11862 simple-type-specifier:
11863 __typeof__ unary-expression
11864 __typeof__ ( type-id )
11866 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11867 appropriately updated. */
11870 cp_parser_simple_type_specifier (cp_parser* parser,
11871 cp_decl_specifier_seq *decl_specs,
11872 cp_parser_flags flags)
11874 tree type = NULL_TREE;
11877 /* Peek at the next token. */
11878 token = cp_lexer_peek_token (parser->lexer);
11880 /* If we're looking at a keyword, things are easy. */
11881 switch (token->keyword)
11885 decl_specs->explicit_char_p = true;
11886 type = char_type_node;
11889 type = char16_type_node;
11892 type = char32_type_node;
11895 type = wchar_type_node;
11898 type = boolean_type_node;
11902 ++decl_specs->specs[(int) ds_short];
11903 type = short_integer_type_node;
11907 decl_specs->explicit_int_p = true;
11908 type = integer_type_node;
11912 ++decl_specs->specs[(int) ds_long];
11913 type = long_integer_type_node;
11917 ++decl_specs->specs[(int) ds_signed];
11918 type = integer_type_node;
11922 ++decl_specs->specs[(int) ds_unsigned];
11923 type = unsigned_type_node;
11926 type = float_type_node;
11929 type = double_type_node;
11932 type = void_type_node;
11936 maybe_warn_cpp0x ("C++0x auto");
11937 type = make_auto ();
11941 /* Parse the `decltype' type. */
11942 type = cp_parser_decltype (parser);
11945 cp_parser_set_decl_spec_type (decl_specs, type,
11947 /*user_defined_p=*/true);
11952 /* Consume the `typeof' token. */
11953 cp_lexer_consume_token (parser->lexer);
11954 /* Parse the operand to `typeof'. */
11955 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11956 /* If it is not already a TYPE, take its type. */
11957 if (!TYPE_P (type))
11958 type = finish_typeof (type);
11961 cp_parser_set_decl_spec_type (decl_specs, type,
11963 /*user_defined_p=*/true);
11971 /* If the type-specifier was for a built-in type, we're done. */
11976 /* Record the type. */
11978 && (token->keyword != RID_SIGNED
11979 && token->keyword != RID_UNSIGNED
11980 && token->keyword != RID_SHORT
11981 && token->keyword != RID_LONG))
11982 cp_parser_set_decl_spec_type (decl_specs,
11985 /*user_defined=*/false);
11987 decl_specs->any_specifiers_p = true;
11989 /* Consume the token. */
11990 id = cp_lexer_consume_token (parser->lexer)->u.value;
11992 /* There is no valid C++ program where a non-template type is
11993 followed by a "<". That usually indicates that the user thought
11994 that the type was a template. */
11995 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11997 return TYPE_NAME (type);
12000 /* The type-specifier must be a user-defined type. */
12001 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12006 /* Don't gobble tokens or issue error messages if this is an
12007 optional type-specifier. */
12008 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12009 cp_parser_parse_tentatively (parser);
12011 /* Look for the optional `::' operator. */
12013 = (cp_parser_global_scope_opt (parser,
12014 /*current_scope_valid_p=*/false)
12016 /* Look for the nested-name specifier. */
12018 = (cp_parser_nested_name_specifier_opt (parser,
12019 /*typename_keyword_p=*/false,
12020 /*check_dependency_p=*/true,
12022 /*is_declaration=*/false)
12024 token = cp_lexer_peek_token (parser->lexer);
12025 /* If we have seen a nested-name-specifier, and the next token
12026 is `template', then we are using the template-id production. */
12028 && cp_parser_optional_template_keyword (parser))
12030 /* Look for the template-id. */
12031 type = cp_parser_template_id (parser,
12032 /*template_keyword_p=*/true,
12033 /*check_dependency_p=*/true,
12034 /*is_declaration=*/false);
12035 /* If the template-id did not name a type, we are out of
12037 if (TREE_CODE (type) != TYPE_DECL)
12039 cp_parser_error (parser, "expected template-id for type");
12043 /* Otherwise, look for a type-name. */
12045 type = cp_parser_type_name (parser);
12046 /* Keep track of all name-lookups performed in class scopes. */
12050 && TREE_CODE (type) == TYPE_DECL
12051 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12052 maybe_note_name_used_in_class (DECL_NAME (type), type);
12053 /* If it didn't work out, we don't have a TYPE. */
12054 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12055 && !cp_parser_parse_definitely (parser))
12057 if (type && decl_specs)
12058 cp_parser_set_decl_spec_type (decl_specs, type,
12060 /*user_defined=*/true);
12063 /* If we didn't get a type-name, issue an error message. */
12064 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12066 cp_parser_error (parser, "expected type-name");
12067 return error_mark_node;
12070 /* There is no valid C++ program where a non-template type is
12071 followed by a "<". That usually indicates that the user thought
12072 that the type was a template. */
12073 if (type && type != error_mark_node)
12075 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12076 If it is, then the '<'...'>' enclose protocol names rather than
12077 template arguments, and so everything is fine. */
12078 if (c_dialect_objc ()
12079 && (objc_is_id (type) || objc_is_class_name (type)))
12081 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12082 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12084 /* Clobber the "unqualified" type previously entered into
12085 DECL_SPECS with the new, improved protocol-qualified version. */
12087 decl_specs->type = qual_type;
12092 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12099 /* Parse a type-name.
12112 Returns a TYPE_DECL for the type. */
12115 cp_parser_type_name (cp_parser* parser)
12119 /* We can't know yet whether it is a class-name or not. */
12120 cp_parser_parse_tentatively (parser);
12121 /* Try a class-name. */
12122 type_decl = cp_parser_class_name (parser,
12123 /*typename_keyword_p=*/false,
12124 /*template_keyword_p=*/false,
12126 /*check_dependency_p=*/true,
12127 /*class_head_p=*/false,
12128 /*is_declaration=*/false);
12129 /* If it's not a class-name, keep looking. */
12130 if (!cp_parser_parse_definitely (parser))
12132 /* It must be a typedef-name or an enum-name. */
12133 return cp_parser_nonclass_name (parser);
12139 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12147 Returns a TYPE_DECL for the type. */
12150 cp_parser_nonclass_name (cp_parser* parser)
12155 cp_token *token = cp_lexer_peek_token (parser->lexer);
12156 identifier = cp_parser_identifier (parser);
12157 if (identifier == error_mark_node)
12158 return error_mark_node;
12160 /* Look up the type-name. */
12161 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12163 if (TREE_CODE (type_decl) != TYPE_DECL
12164 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12166 /* See if this is an Objective-C type. */
12167 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12168 tree type = objc_get_protocol_qualified_type (identifier, protos);
12170 type_decl = TYPE_NAME (type);
12173 /* Issue an error if we did not find a type-name. */
12174 if (TREE_CODE (type_decl) != TYPE_DECL)
12176 if (!cp_parser_simulate_error (parser))
12177 cp_parser_name_lookup_error (parser, identifier, type_decl,
12178 "is not a type", token->location);
12179 return error_mark_node;
12181 /* Remember that the name was used in the definition of the
12182 current class so that we can check later to see if the
12183 meaning would have been different after the class was
12184 entirely defined. */
12185 else if (type_decl != error_mark_node
12187 maybe_note_name_used_in_class (identifier, type_decl);
12192 /* Parse an elaborated-type-specifier. Note that the grammar given
12193 here incorporates the resolution to DR68.
12195 elaborated-type-specifier:
12196 class-key :: [opt] nested-name-specifier [opt] identifier
12197 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12198 enum-key :: [opt] nested-name-specifier [opt] identifier
12199 typename :: [opt] nested-name-specifier identifier
12200 typename :: [opt] nested-name-specifier template [opt]
12205 elaborated-type-specifier:
12206 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12207 class-key attributes :: [opt] nested-name-specifier [opt]
12208 template [opt] template-id
12209 enum attributes :: [opt] nested-name-specifier [opt] identifier
12211 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12212 declared `friend'. If IS_DECLARATION is TRUE, then this
12213 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12214 something is being declared.
12216 Returns the TYPE specified. */
12219 cp_parser_elaborated_type_specifier (cp_parser* parser,
12221 bool is_declaration)
12223 enum tag_types tag_type;
12225 tree type = NULL_TREE;
12226 tree attributes = NULL_TREE;
12228 cp_token *token = NULL;
12230 /* See if we're looking at the `enum' keyword. */
12231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12233 /* Consume the `enum' token. */
12234 cp_lexer_consume_token (parser->lexer);
12235 /* Remember that it's an enumeration type. */
12236 tag_type = enum_type;
12237 /* Parse the optional `struct' or `class' key (for C++0x scoped
12239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12240 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12242 if (cxx_dialect == cxx98)
12243 maybe_warn_cpp0x ("scoped enums");
12245 /* Consume the `struct' or `class'. */
12246 cp_lexer_consume_token (parser->lexer);
12248 /* Parse the attributes. */
12249 attributes = cp_parser_attributes_opt (parser);
12251 /* Or, it might be `typename'. */
12252 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12255 /* Consume the `typename' token. */
12256 cp_lexer_consume_token (parser->lexer);
12257 /* Remember that it's a `typename' type. */
12258 tag_type = typename_type;
12260 /* Otherwise it must be a class-key. */
12263 tag_type = cp_parser_class_key (parser);
12264 if (tag_type == none_type)
12265 return error_mark_node;
12266 /* Parse the attributes. */
12267 attributes = cp_parser_attributes_opt (parser);
12270 /* Look for the `::' operator. */
12271 globalscope = cp_parser_global_scope_opt (parser,
12272 /*current_scope_valid_p=*/false);
12273 /* Look for the nested-name-specifier. */
12274 if (tag_type == typename_type && !globalscope)
12276 if (!cp_parser_nested_name_specifier (parser,
12277 /*typename_keyword_p=*/true,
12278 /*check_dependency_p=*/true,
12281 return error_mark_node;
12284 /* Even though `typename' is not present, the proposed resolution
12285 to Core Issue 180 says that in `class A<T>::B', `B' should be
12286 considered a type-name, even if `A<T>' is dependent. */
12287 cp_parser_nested_name_specifier_opt (parser,
12288 /*typename_keyword_p=*/true,
12289 /*check_dependency_p=*/true,
12292 /* For everything but enumeration types, consider a template-id.
12293 For an enumeration type, consider only a plain identifier. */
12294 if (tag_type != enum_type)
12296 bool template_p = false;
12299 /* Allow the `template' keyword. */
12300 template_p = cp_parser_optional_template_keyword (parser);
12301 /* If we didn't see `template', we don't know if there's a
12302 template-id or not. */
12304 cp_parser_parse_tentatively (parser);
12305 /* Parse the template-id. */
12306 token = cp_lexer_peek_token (parser->lexer);
12307 decl = cp_parser_template_id (parser, template_p,
12308 /*check_dependency_p=*/true,
12310 /* If we didn't find a template-id, look for an ordinary
12312 if (!template_p && !cp_parser_parse_definitely (parser))
12314 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12315 in effect, then we must assume that, upon instantiation, the
12316 template will correspond to a class. */
12317 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12318 && tag_type == typename_type)
12319 type = make_typename_type (parser->scope, decl,
12321 /*complain=*/tf_error);
12322 /* If the `typename' keyword is in effect and DECL is not a type
12323 decl. Then type is non existant. */
12324 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12327 type = TREE_TYPE (decl);
12332 token = cp_lexer_peek_token (parser->lexer);
12333 identifier = cp_parser_identifier (parser);
12335 if (identifier == error_mark_node)
12337 parser->scope = NULL_TREE;
12338 return error_mark_node;
12341 /* For a `typename', we needn't call xref_tag. */
12342 if (tag_type == typename_type
12343 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12344 return cp_parser_make_typename_type (parser, parser->scope,
12347 /* Look up a qualified name in the usual way. */
12351 tree ambiguous_decls;
12353 decl = cp_parser_lookup_name (parser, identifier,
12355 /*is_template=*/false,
12356 /*is_namespace=*/false,
12357 /*check_dependency=*/true,
12361 /* If the lookup was ambiguous, an error will already have been
12363 if (ambiguous_decls)
12364 return error_mark_node;
12366 /* If we are parsing friend declaration, DECL may be a
12367 TEMPLATE_DECL tree node here. However, we need to check
12368 whether this TEMPLATE_DECL results in valid code. Consider
12369 the following example:
12372 template <class T> class C {};
12375 template <class T> friend class N::C; // #1, valid code
12377 template <class T> class Y {
12378 friend class N::C; // #2, invalid code
12381 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12382 name lookup of `N::C'. We see that friend declaration must
12383 be template for the code to be valid. Note that
12384 processing_template_decl does not work here since it is
12385 always 1 for the above two cases. */
12387 decl = (cp_parser_maybe_treat_template_as_class
12388 (decl, /*tag_name_p=*/is_friend
12389 && parser->num_template_parameter_lists));
12391 if (TREE_CODE (decl) != TYPE_DECL)
12393 cp_parser_diagnose_invalid_type_name (parser,
12397 return error_mark_node;
12400 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12402 bool allow_template = (parser->num_template_parameter_lists
12403 || DECL_SELF_REFERENCE_P (decl));
12404 type = check_elaborated_type_specifier (tag_type, decl,
12407 if (type == error_mark_node)
12408 return error_mark_node;
12411 /* Forward declarations of nested types, such as
12416 are invalid unless all components preceding the final '::'
12417 are complete. If all enclosing types are complete, these
12418 declarations become merely pointless.
12420 Invalid forward declarations of nested types are errors
12421 caught elsewhere in parsing. Those that are pointless arrive
12424 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12425 && !is_friend && !processing_explicit_instantiation)
12426 warning (0, "declaration %qD does not declare anything", decl);
12428 type = TREE_TYPE (decl);
12432 /* An elaborated-type-specifier sometimes introduces a new type and
12433 sometimes names an existing type. Normally, the rule is that it
12434 introduces a new type only if there is not an existing type of
12435 the same name already in scope. For example, given:
12438 void f() { struct S s; }
12440 the `struct S' in the body of `f' is the same `struct S' as in
12441 the global scope; the existing definition is used. However, if
12442 there were no global declaration, this would introduce a new
12443 local class named `S'.
12445 An exception to this rule applies to the following code:
12447 namespace N { struct S; }
12449 Here, the elaborated-type-specifier names a new type
12450 unconditionally; even if there is already an `S' in the
12451 containing scope this declaration names a new type.
12452 This exception only applies if the elaborated-type-specifier
12453 forms the complete declaration:
12457 A declaration consisting solely of `class-key identifier ;' is
12458 either a redeclaration of the name in the current scope or a
12459 forward declaration of the identifier as a class name. It
12460 introduces the name into the current scope.
12462 We are in this situation precisely when the next token is a `;'.
12464 An exception to the exception is that a `friend' declaration does
12465 *not* name a new type; i.e., given:
12467 struct S { friend struct T; };
12469 `T' is not a new type in the scope of `S'.
12471 Also, `new struct S' or `sizeof (struct S)' never results in the
12472 definition of a new type; a new type can only be declared in a
12473 declaration context. */
12479 /* Friends have special name lookup rules. */
12480 ts = ts_within_enclosing_non_class;
12481 else if (is_declaration
12482 && cp_lexer_next_token_is (parser->lexer,
12484 /* This is a `class-key identifier ;' */
12490 (parser->num_template_parameter_lists
12491 && (cp_parser_next_token_starts_class_definition_p (parser)
12492 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12493 /* An unqualified name was used to reference this type, so
12494 there were no qualifying templates. */
12495 if (!cp_parser_check_template_parameters (parser,
12496 /*num_templates=*/0,
12498 /*declarator=*/NULL))
12499 return error_mark_node;
12500 type = xref_tag (tag_type, identifier, ts, template_p);
12504 if (type == error_mark_node)
12505 return error_mark_node;
12507 /* Allow attributes on forward declarations of classes. */
12510 if (TREE_CODE (type) == TYPENAME_TYPE)
12511 warning (OPT_Wattributes,
12512 "attributes ignored on uninstantiated type");
12513 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12514 && ! processing_explicit_instantiation)
12515 warning (OPT_Wattributes,
12516 "attributes ignored on template instantiation");
12517 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12518 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12520 warning (OPT_Wattributes,
12521 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12524 if (tag_type != enum_type)
12525 cp_parser_check_class_key (tag_type, type);
12527 /* A "<" cannot follow an elaborated type specifier. If that
12528 happens, the user was probably trying to form a template-id. */
12529 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12534 /* Parse an enum-specifier.
12537 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12542 enum struct [C++0x]
12545 : type-specifier-seq
12548 enum-key attributes[opt] identifier [opt] enum-base [opt]
12549 { enumerator-list [opt] }attributes[opt]
12551 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12552 if the token stream isn't an enum-specifier after all. */
12555 cp_parser_enum_specifier (cp_parser* parser)
12560 bool scoped_enum_p = false;
12561 bool has_underlying_type = false;
12562 tree underlying_type = NULL_TREE;
12564 /* Parse tentatively so that we can back up if we don't find a
12566 cp_parser_parse_tentatively (parser);
12568 /* Caller guarantees that the current token is 'enum', an identifier
12569 possibly follows, and the token after that is an opening brace.
12570 If we don't have an identifier, fabricate an anonymous name for
12571 the enumeration being defined. */
12572 cp_lexer_consume_token (parser->lexer);
12574 /* Parse the "class" or "struct", which indicates a scoped
12575 enumeration type in C++0x. */
12576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12577 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12579 if (cxx_dialect == cxx98)
12580 maybe_warn_cpp0x ("scoped enums");
12582 /* Consume the `struct' or `class' token. */
12583 cp_lexer_consume_token (parser->lexer);
12585 scoped_enum_p = true;
12588 attributes = cp_parser_attributes_opt (parser);
12590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12591 identifier = cp_parser_identifier (parser);
12593 identifier = make_anon_name ();
12595 /* Check for the `:' that denotes a specified underlying type in C++0x.
12596 Note that a ':' could also indicate a bitfield width, however. */
12597 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12599 cp_decl_specifier_seq type_specifiers;
12601 /* Consume the `:'. */
12602 cp_lexer_consume_token (parser->lexer);
12604 /* Parse the type-specifier-seq. */
12605 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12606 /*is_trailing_return=*/false,
12609 /* At this point this is surely not elaborated type specifier. */
12610 if (!cp_parser_parse_definitely (parser))
12613 if (cxx_dialect == cxx98)
12614 maybe_warn_cpp0x ("scoped enums");
12616 has_underlying_type = true;
12618 /* If that didn't work, stop. */
12619 if (type_specifiers.type != error_mark_node)
12621 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12622 /*initialized=*/0, NULL);
12623 if (underlying_type == error_mark_node)
12624 underlying_type = NULL_TREE;
12628 /* Look for the `{' but don't consume it yet. */
12629 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12631 cp_parser_error (parser, "expected %<{%>");
12632 if (has_underlying_type)
12636 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12639 /* Issue an error message if type-definitions are forbidden here. */
12640 if (!cp_parser_check_type_definition (parser))
12641 type = error_mark_node;
12643 /* Create the new type. We do this before consuming the opening
12644 brace so the enum will be recorded as being on the line of its
12645 tag (or the 'enum' keyword, if there is no tag). */
12646 type = start_enum (identifier, underlying_type, scoped_enum_p);
12648 /* Consume the opening brace. */
12649 cp_lexer_consume_token (parser->lexer);
12651 if (type == error_mark_node)
12653 cp_parser_skip_to_end_of_block_or_statement (parser);
12654 return error_mark_node;
12657 /* If the next token is not '}', then there are some enumerators. */
12658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12659 cp_parser_enumerator_list (parser, type);
12661 /* Consume the final '}'. */
12662 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12664 /* Look for trailing attributes to apply to this enumeration, and
12665 apply them if appropriate. */
12666 if (cp_parser_allow_gnu_extensions_p (parser))
12668 tree trailing_attr = cp_parser_attributes_opt (parser);
12669 trailing_attr = chainon (trailing_attr, attributes);
12670 cplus_decl_attributes (&type,
12672 (int) ATTR_FLAG_TYPE_IN_PLACE);
12675 /* Finish up the enumeration. */
12676 finish_enum (type);
12681 /* Parse an enumerator-list. The enumerators all have the indicated
12685 enumerator-definition
12686 enumerator-list , enumerator-definition */
12689 cp_parser_enumerator_list (cp_parser* parser, tree type)
12693 /* Parse an enumerator-definition. */
12694 cp_parser_enumerator_definition (parser, type);
12696 /* If the next token is not a ',', we've reached the end of
12698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12700 /* Otherwise, consume the `,' and keep going. */
12701 cp_lexer_consume_token (parser->lexer);
12702 /* If the next token is a `}', there is a trailing comma. */
12703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12705 if (!in_system_header)
12706 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12712 /* Parse an enumerator-definition. The enumerator has the indicated
12715 enumerator-definition:
12717 enumerator = constant-expression
12723 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12728 /* Look for the identifier. */
12729 identifier = cp_parser_identifier (parser);
12730 if (identifier == error_mark_node)
12733 /* If the next token is an '=', then there is an explicit value. */
12734 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12736 /* Consume the `=' token. */
12737 cp_lexer_consume_token (parser->lexer);
12738 /* Parse the value. */
12739 value = cp_parser_constant_expression (parser,
12740 /*allow_non_constant_p=*/false,
12746 /* If we are processing a template, make sure the initializer of the
12747 enumerator doesn't contain any bare template parameter pack. */
12748 if (check_for_bare_parameter_packs (value))
12749 value = error_mark_node;
12751 /* Create the enumerator. */
12752 build_enumerator (identifier, value, type);
12755 /* Parse a namespace-name.
12758 original-namespace-name
12761 Returns the NAMESPACE_DECL for the namespace. */
12764 cp_parser_namespace_name (cp_parser* parser)
12767 tree namespace_decl;
12769 cp_token *token = cp_lexer_peek_token (parser->lexer);
12771 /* Get the name of the namespace. */
12772 identifier = cp_parser_identifier (parser);
12773 if (identifier == error_mark_node)
12774 return error_mark_node;
12776 /* Look up the identifier in the currently active scope. Look only
12777 for namespaces, due to:
12779 [basic.lookup.udir]
12781 When looking up a namespace-name in a using-directive or alias
12782 definition, only namespace names are considered.
12786 [basic.lookup.qual]
12788 During the lookup of a name preceding the :: scope resolution
12789 operator, object, function, and enumerator names are ignored.
12791 (Note that cp_parser_qualifying_entity only calls this
12792 function if the token after the name is the scope resolution
12794 namespace_decl = cp_parser_lookup_name (parser, identifier,
12796 /*is_template=*/false,
12797 /*is_namespace=*/true,
12798 /*check_dependency=*/true,
12799 /*ambiguous_decls=*/NULL,
12801 /* If it's not a namespace, issue an error. */
12802 if (namespace_decl == error_mark_node
12803 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12805 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12806 error_at (token->location, "%qD is not a namespace-name", identifier);
12807 cp_parser_error (parser, "expected namespace-name");
12808 namespace_decl = error_mark_node;
12811 return namespace_decl;
12814 /* Parse a namespace-definition.
12816 namespace-definition:
12817 named-namespace-definition
12818 unnamed-namespace-definition
12820 named-namespace-definition:
12821 original-namespace-definition
12822 extension-namespace-definition
12824 original-namespace-definition:
12825 namespace identifier { namespace-body }
12827 extension-namespace-definition:
12828 namespace original-namespace-name { namespace-body }
12830 unnamed-namespace-definition:
12831 namespace { namespace-body } */
12834 cp_parser_namespace_definition (cp_parser* parser)
12836 tree identifier, attribs;
12837 bool has_visibility;
12840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12843 cp_lexer_consume_token (parser->lexer);
12848 /* Look for the `namespace' keyword. */
12849 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12851 /* Get the name of the namespace. We do not attempt to distinguish
12852 between an original-namespace-definition and an
12853 extension-namespace-definition at this point. The semantic
12854 analysis routines are responsible for that. */
12855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12856 identifier = cp_parser_identifier (parser);
12858 identifier = NULL_TREE;
12860 /* Parse any specified attributes. */
12861 attribs = cp_parser_attributes_opt (parser);
12863 /* Look for the `{' to start the namespace. */
12864 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12865 /* Start the namespace. */
12866 push_namespace (identifier);
12868 /* "inline namespace" is equivalent to a stub namespace definition
12869 followed by a strong using directive. */
12872 tree name_space = current_namespace;
12873 /* Set up namespace association. */
12874 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12875 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12876 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12877 /* Import the contents of the inline namespace. */
12879 do_using_directive (name_space);
12880 push_namespace (identifier);
12883 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12885 /* Parse the body of the namespace. */
12886 cp_parser_namespace_body (parser);
12888 #ifdef HANDLE_PRAGMA_VISIBILITY
12889 if (has_visibility)
12890 pop_visibility (1);
12893 /* Finish the namespace. */
12895 /* Look for the final `}'. */
12896 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12899 /* Parse a namespace-body.
12902 declaration-seq [opt] */
12905 cp_parser_namespace_body (cp_parser* parser)
12907 cp_parser_declaration_seq_opt (parser);
12910 /* Parse a namespace-alias-definition.
12912 namespace-alias-definition:
12913 namespace identifier = qualified-namespace-specifier ; */
12916 cp_parser_namespace_alias_definition (cp_parser* parser)
12919 tree namespace_specifier;
12921 cp_token *token = cp_lexer_peek_token (parser->lexer);
12923 /* Look for the `namespace' keyword. */
12924 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12925 /* Look for the identifier. */
12926 identifier = cp_parser_identifier (parser);
12927 if (identifier == error_mark_node)
12929 /* Look for the `=' token. */
12930 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12931 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12933 error_at (token->location, "%<namespace%> definition is not allowed here");
12934 /* Skip the definition. */
12935 cp_lexer_consume_token (parser->lexer);
12936 if (cp_parser_skip_to_closing_brace (parser))
12937 cp_lexer_consume_token (parser->lexer);
12940 cp_parser_require (parser, CPP_EQ, "%<=%>");
12941 /* Look for the qualified-namespace-specifier. */
12942 namespace_specifier
12943 = cp_parser_qualified_namespace_specifier (parser);
12944 /* Look for the `;' token. */
12945 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12947 /* Register the alias in the symbol table. */
12948 do_namespace_alias (identifier, namespace_specifier);
12951 /* Parse a qualified-namespace-specifier.
12953 qualified-namespace-specifier:
12954 :: [opt] nested-name-specifier [opt] namespace-name
12956 Returns a NAMESPACE_DECL corresponding to the specified
12960 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12962 /* Look for the optional `::'. */
12963 cp_parser_global_scope_opt (parser,
12964 /*current_scope_valid_p=*/false);
12966 /* Look for the optional nested-name-specifier. */
12967 cp_parser_nested_name_specifier_opt (parser,
12968 /*typename_keyword_p=*/false,
12969 /*check_dependency_p=*/true,
12971 /*is_declaration=*/true);
12973 return cp_parser_namespace_name (parser);
12976 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12977 access declaration.
12980 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12981 using :: unqualified-id ;
12983 access-declaration:
12989 cp_parser_using_declaration (cp_parser* parser,
12990 bool access_declaration_p)
12993 bool typename_p = false;
12994 bool global_scope_p;
12999 if (access_declaration_p)
13000 cp_parser_parse_tentatively (parser);
13003 /* Look for the `using' keyword. */
13004 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13006 /* Peek at the next token. */
13007 token = cp_lexer_peek_token (parser->lexer);
13008 /* See if it's `typename'. */
13009 if (token->keyword == RID_TYPENAME)
13011 /* Remember that we've seen it. */
13013 /* Consume the `typename' token. */
13014 cp_lexer_consume_token (parser->lexer);
13018 /* Look for the optional global scope qualification. */
13020 = (cp_parser_global_scope_opt (parser,
13021 /*current_scope_valid_p=*/false)
13024 /* If we saw `typename', or didn't see `::', then there must be a
13025 nested-name-specifier present. */
13026 if (typename_p || !global_scope_p)
13027 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13028 /*check_dependency_p=*/true,
13030 /*is_declaration=*/true);
13031 /* Otherwise, we could be in either of the two productions. In that
13032 case, treat the nested-name-specifier as optional. */
13034 qscope = cp_parser_nested_name_specifier_opt (parser,
13035 /*typename_keyword_p=*/false,
13036 /*check_dependency_p=*/true,
13038 /*is_declaration=*/true);
13040 qscope = global_namespace;
13042 if (access_declaration_p && cp_parser_error_occurred (parser))
13043 /* Something has already gone wrong; there's no need to parse
13044 further. Since an error has occurred, the return value of
13045 cp_parser_parse_definitely will be false, as required. */
13046 return cp_parser_parse_definitely (parser);
13048 token = cp_lexer_peek_token (parser->lexer);
13049 /* Parse the unqualified-id. */
13050 identifier = cp_parser_unqualified_id (parser,
13051 /*template_keyword_p=*/false,
13052 /*check_dependency_p=*/true,
13053 /*declarator_p=*/true,
13054 /*optional_p=*/false);
13056 if (access_declaration_p)
13058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13059 cp_parser_simulate_error (parser);
13060 if (!cp_parser_parse_definitely (parser))
13064 /* The function we call to handle a using-declaration is different
13065 depending on what scope we are in. */
13066 if (qscope == error_mark_node || identifier == error_mark_node)
13068 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13069 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13070 /* [namespace.udecl]
13072 A using declaration shall not name a template-id. */
13073 error_at (token->location,
13074 "a template-id may not appear in a using-declaration");
13077 if (at_class_scope_p ())
13079 /* Create the USING_DECL. */
13080 decl = do_class_using_decl (parser->scope, identifier);
13082 if (check_for_bare_parameter_packs (decl))
13085 /* Add it to the list of members in this class. */
13086 finish_member_declaration (decl);
13090 decl = cp_parser_lookup_name_simple (parser,
13093 if (decl == error_mark_node)
13094 cp_parser_name_lookup_error (parser, identifier,
13097 else if (check_for_bare_parameter_packs (decl))
13099 else if (!at_namespace_scope_p ())
13100 do_local_using_decl (decl, qscope, identifier);
13102 do_toplevel_using_decl (decl, qscope, identifier);
13106 /* Look for the final `;'. */
13107 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13112 /* Parse a using-directive.
13115 using namespace :: [opt] nested-name-specifier [opt]
13116 namespace-name ; */
13119 cp_parser_using_directive (cp_parser* parser)
13121 tree namespace_decl;
13124 /* Look for the `using' keyword. */
13125 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13126 /* And the `namespace' keyword. */
13127 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13128 /* Look for the optional `::' operator. */
13129 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13130 /* And the optional nested-name-specifier. */
13131 cp_parser_nested_name_specifier_opt (parser,
13132 /*typename_keyword_p=*/false,
13133 /*check_dependency_p=*/true,
13135 /*is_declaration=*/true);
13136 /* Get the namespace being used. */
13137 namespace_decl = cp_parser_namespace_name (parser);
13138 /* And any specified attributes. */
13139 attribs = cp_parser_attributes_opt (parser);
13140 /* Update the symbol table. */
13141 parse_using_directive (namespace_decl, attribs);
13142 /* Look for the final `;'. */
13143 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13146 /* Parse an asm-definition.
13149 asm ( string-literal ) ;
13154 asm volatile [opt] ( string-literal ) ;
13155 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13156 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13157 : asm-operand-list [opt] ) ;
13158 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13159 : asm-operand-list [opt]
13160 : asm-clobber-list [opt] ) ;
13161 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13162 : asm-clobber-list [opt]
13163 : asm-goto-list ) ; */
13166 cp_parser_asm_definition (cp_parser* parser)
13169 tree outputs = NULL_TREE;
13170 tree inputs = NULL_TREE;
13171 tree clobbers = NULL_TREE;
13172 tree labels = NULL_TREE;
13174 bool volatile_p = false;
13175 bool extended_p = false;
13176 bool invalid_inputs_p = false;
13177 bool invalid_outputs_p = false;
13178 bool goto_p = false;
13179 const char *missing = NULL;
13181 /* Look for the `asm' keyword. */
13182 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13183 /* See if the next token is `volatile'. */
13184 if (cp_parser_allow_gnu_extensions_p (parser)
13185 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13187 /* Remember that we saw the `volatile' keyword. */
13189 /* Consume the token. */
13190 cp_lexer_consume_token (parser->lexer);
13192 if (cp_parser_allow_gnu_extensions_p (parser)
13193 && parser->in_function_body
13194 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13196 /* Remember that we saw the `goto' keyword. */
13198 /* Consume the token. */
13199 cp_lexer_consume_token (parser->lexer);
13201 /* Look for the opening `('. */
13202 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13204 /* Look for the string. */
13205 string = cp_parser_string_literal (parser, false, false);
13206 if (string == error_mark_node)
13208 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13209 /*consume_paren=*/true);
13213 /* If we're allowing GNU extensions, check for the extended assembly
13214 syntax. Unfortunately, the `:' tokens need not be separated by
13215 a space in C, and so, for compatibility, we tolerate that here
13216 too. Doing that means that we have to treat the `::' operator as
13218 if (cp_parser_allow_gnu_extensions_p (parser)
13219 && parser->in_function_body
13220 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13221 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13223 bool inputs_p = false;
13224 bool clobbers_p = false;
13225 bool labels_p = false;
13227 /* The extended syntax was used. */
13230 /* Look for outputs. */
13231 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13233 /* Consume the `:'. */
13234 cp_lexer_consume_token (parser->lexer);
13235 /* Parse the output-operands. */
13236 if (cp_lexer_next_token_is_not (parser->lexer,
13238 && cp_lexer_next_token_is_not (parser->lexer,
13240 && cp_lexer_next_token_is_not (parser->lexer,
13243 outputs = cp_parser_asm_operand_list (parser);
13245 if (outputs == error_mark_node)
13246 invalid_outputs_p = true;
13248 /* If the next token is `::', there are no outputs, and the
13249 next token is the beginning of the inputs. */
13250 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13251 /* The inputs are coming next. */
13254 /* Look for inputs. */
13256 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13258 /* Consume the `:' or `::'. */
13259 cp_lexer_consume_token (parser->lexer);
13260 /* Parse the output-operands. */
13261 if (cp_lexer_next_token_is_not (parser->lexer,
13263 && cp_lexer_next_token_is_not (parser->lexer,
13265 && cp_lexer_next_token_is_not (parser->lexer,
13267 inputs = cp_parser_asm_operand_list (parser);
13269 if (inputs == error_mark_node)
13270 invalid_inputs_p = true;
13272 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13273 /* The clobbers are coming next. */
13276 /* Look for clobbers. */
13278 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13281 /* Consume the `:' or `::'. */
13282 cp_lexer_consume_token (parser->lexer);
13283 /* Parse the clobbers. */
13284 if (cp_lexer_next_token_is_not (parser->lexer,
13286 && cp_lexer_next_token_is_not (parser->lexer,
13288 clobbers = cp_parser_asm_clobber_list (parser);
13291 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13292 /* The labels are coming next. */
13295 /* Look for labels. */
13297 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13300 /* Consume the `:' or `::'. */
13301 cp_lexer_consume_token (parser->lexer);
13302 /* Parse the labels. */
13303 labels = cp_parser_asm_label_list (parser);
13306 if (goto_p && !labels_p)
13307 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13310 missing = "%<:%> or %<::%>";
13312 /* Look for the closing `)'. */
13313 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13314 missing ? missing : "%<)%>"))
13315 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13316 /*consume_paren=*/true);
13317 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13319 if (!invalid_inputs_p && !invalid_outputs_p)
13321 /* Create the ASM_EXPR. */
13322 if (parser->in_function_body)
13324 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13325 inputs, clobbers, labels);
13326 /* If the extended syntax was not used, mark the ASM_EXPR. */
13329 tree temp = asm_stmt;
13330 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13331 temp = TREE_OPERAND (temp, 0);
13333 ASM_INPUT_P (temp) = 1;
13337 cgraph_add_asm_node (string);
13341 /* Declarators [gram.dcl.decl] */
13343 /* Parse an init-declarator.
13346 declarator initializer [opt]
13351 declarator asm-specification [opt] attributes [opt] initializer [opt]
13353 function-definition:
13354 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13356 decl-specifier-seq [opt] declarator function-try-block
13360 function-definition:
13361 __extension__ function-definition
13363 The DECL_SPECIFIERS apply to this declarator. Returns a
13364 representation of the entity declared. If MEMBER_P is TRUE, then
13365 this declarator appears in a class scope. The new DECL created by
13366 this declarator is returned.
13368 The CHECKS are access checks that should be performed once we know
13369 what entity is being declared (and, therefore, what classes have
13372 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13373 for a function-definition here as well. If the declarator is a
13374 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13375 be TRUE upon return. By that point, the function-definition will
13376 have been completely parsed.
13378 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13382 cp_parser_init_declarator (cp_parser* parser,
13383 cp_decl_specifier_seq *decl_specifiers,
13384 VEC (deferred_access_check,gc)* checks,
13385 bool function_definition_allowed_p,
13387 int declares_class_or_enum,
13388 bool* function_definition_p)
13390 cp_token *token = NULL, *asm_spec_start_token = NULL,
13391 *attributes_start_token = NULL;
13392 cp_declarator *declarator;
13393 tree prefix_attributes;
13395 tree asm_specification;
13397 tree decl = NULL_TREE;
13399 int is_initialized;
13400 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13401 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13403 enum cpp_ttype initialization_kind;
13404 bool is_direct_init = false;
13405 bool is_non_constant_init;
13406 int ctor_dtor_or_conv_p;
13408 tree pushed_scope = NULL;
13410 /* Gather the attributes that were provided with the
13411 decl-specifiers. */
13412 prefix_attributes = decl_specifiers->attributes;
13414 /* Assume that this is not the declarator for a function
13416 if (function_definition_p)
13417 *function_definition_p = false;
13419 /* Defer access checks while parsing the declarator; we cannot know
13420 what names are accessible until we know what is being
13422 resume_deferring_access_checks ();
13424 /* Parse the declarator. */
13425 token = cp_lexer_peek_token (parser->lexer);
13427 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13428 &ctor_dtor_or_conv_p,
13429 /*parenthesized_p=*/NULL,
13430 /*member_p=*/false);
13431 /* Gather up the deferred checks. */
13432 stop_deferring_access_checks ();
13434 /* If the DECLARATOR was erroneous, there's no need to go
13436 if (declarator == cp_error_declarator)
13437 return error_mark_node;
13439 /* Check that the number of template-parameter-lists is OK. */
13440 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13442 return error_mark_node;
13444 if (declares_class_or_enum & 2)
13445 cp_parser_check_for_definition_in_return_type (declarator,
13446 decl_specifiers->type,
13447 decl_specifiers->type_location);
13449 /* Figure out what scope the entity declared by the DECLARATOR is
13450 located in. `grokdeclarator' sometimes changes the scope, so
13451 we compute it now. */
13452 scope = get_scope_of_declarator (declarator);
13454 /* If we're allowing GNU extensions, look for an asm-specification
13456 if (cp_parser_allow_gnu_extensions_p (parser))
13458 /* Look for an asm-specification. */
13459 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13460 asm_specification = cp_parser_asm_specification_opt (parser);
13461 /* And attributes. */
13462 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13463 attributes = cp_parser_attributes_opt (parser);
13467 asm_specification = NULL_TREE;
13468 attributes = NULL_TREE;
13471 /* Peek at the next token. */
13472 token = cp_lexer_peek_token (parser->lexer);
13473 /* Check to see if the token indicates the start of a
13474 function-definition. */
13475 if (function_declarator_p (declarator)
13476 && cp_parser_token_starts_function_definition_p (token))
13478 if (!function_definition_allowed_p)
13480 /* If a function-definition should not appear here, issue an
13482 cp_parser_error (parser,
13483 "a function-definition is not allowed here");
13484 return error_mark_node;
13488 location_t func_brace_location
13489 = cp_lexer_peek_token (parser->lexer)->location;
13491 /* Neither attributes nor an asm-specification are allowed
13492 on a function-definition. */
13493 if (asm_specification)
13494 error_at (asm_spec_start_token->location,
13495 "an asm-specification is not allowed "
13496 "on a function-definition");
13498 error_at (attributes_start_token->location,
13499 "attributes are not allowed on a function-definition");
13500 /* This is a function-definition. */
13501 *function_definition_p = true;
13503 /* Parse the function definition. */
13505 decl = cp_parser_save_member_function_body (parser,
13508 prefix_attributes);
13511 = (cp_parser_function_definition_from_specifiers_and_declarator
13512 (parser, decl_specifiers, prefix_attributes, declarator));
13514 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13516 /* This is where the prologue starts... */
13517 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13518 = func_brace_location;
13527 Only in function declarations for constructors, destructors, and
13528 type conversions can the decl-specifier-seq be omitted.
13530 We explicitly postpone this check past the point where we handle
13531 function-definitions because we tolerate function-definitions
13532 that are missing their return types in some modes. */
13533 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13535 cp_parser_error (parser,
13536 "expected constructor, destructor, or type conversion");
13537 return error_mark_node;
13540 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13541 if (token->type == CPP_EQ
13542 || token->type == CPP_OPEN_PAREN
13543 || token->type == CPP_OPEN_BRACE)
13545 is_initialized = SD_INITIALIZED;
13546 initialization_kind = token->type;
13548 if (token->type == CPP_EQ
13549 && function_declarator_p (declarator))
13551 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13552 if (t2->keyword == RID_DEFAULT)
13553 is_initialized = SD_DEFAULTED;
13554 else if (t2->keyword == RID_DELETE)
13555 is_initialized = SD_DELETED;
13560 /* If the init-declarator isn't initialized and isn't followed by a
13561 `,' or `;', it's not a valid init-declarator. */
13562 if (token->type != CPP_COMMA
13563 && token->type != CPP_SEMICOLON)
13565 cp_parser_error (parser, "expected initializer");
13566 return error_mark_node;
13568 is_initialized = SD_UNINITIALIZED;
13569 initialization_kind = CPP_EOF;
13572 /* Because start_decl has side-effects, we should only call it if we
13573 know we're going ahead. By this point, we know that we cannot
13574 possibly be looking at any other construct. */
13575 cp_parser_commit_to_tentative_parse (parser);
13577 /* If the decl specifiers were bad, issue an error now that we're
13578 sure this was intended to be a declarator. Then continue
13579 declaring the variable(s), as int, to try to cut down on further
13581 if (decl_specifiers->any_specifiers_p
13582 && decl_specifiers->type == error_mark_node)
13584 cp_parser_error (parser, "invalid type in declaration");
13585 decl_specifiers->type = integer_type_node;
13588 /* Check to see whether or not this declaration is a friend. */
13589 friend_p = cp_parser_friend_p (decl_specifiers);
13591 /* Enter the newly declared entry in the symbol table. If we're
13592 processing a declaration in a class-specifier, we wait until
13593 after processing the initializer. */
13596 if (parser->in_unbraced_linkage_specification_p)
13597 decl_specifiers->storage_class = sc_extern;
13598 decl = start_decl (declarator, decl_specifiers,
13599 is_initialized, attributes, prefix_attributes,
13603 /* Enter the SCOPE. That way unqualified names appearing in the
13604 initializer will be looked up in SCOPE. */
13605 pushed_scope = push_scope (scope);
13607 /* Perform deferred access control checks, now that we know in which
13608 SCOPE the declared entity resides. */
13609 if (!member_p && decl)
13611 tree saved_current_function_decl = NULL_TREE;
13613 /* If the entity being declared is a function, pretend that we
13614 are in its scope. If it is a `friend', it may have access to
13615 things that would not otherwise be accessible. */
13616 if (TREE_CODE (decl) == FUNCTION_DECL)
13618 saved_current_function_decl = current_function_decl;
13619 current_function_decl = decl;
13622 /* Perform access checks for template parameters. */
13623 cp_parser_perform_template_parameter_access_checks (checks);
13625 /* Perform the access control checks for the declarator and the
13626 decl-specifiers. */
13627 perform_deferred_access_checks ();
13629 /* Restore the saved value. */
13630 if (TREE_CODE (decl) == FUNCTION_DECL)
13631 current_function_decl = saved_current_function_decl;
13634 /* Parse the initializer. */
13635 initializer = NULL_TREE;
13636 is_direct_init = false;
13637 is_non_constant_init = true;
13638 if (is_initialized)
13640 if (function_declarator_p (declarator))
13642 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13643 if (initialization_kind == CPP_EQ)
13644 initializer = cp_parser_pure_specifier (parser);
13647 /* If the declaration was erroneous, we don't really
13648 know what the user intended, so just silently
13649 consume the initializer. */
13650 if (decl != error_mark_node)
13651 error_at (initializer_start_token->location,
13652 "initializer provided for function");
13653 cp_parser_skip_to_closing_parenthesis (parser,
13654 /*recovering=*/true,
13655 /*or_comma=*/false,
13656 /*consume_paren=*/true);
13661 /* We want to record the extra mangling scope for in-class
13662 initializers of class members and initializers of static data
13663 member templates. The former is a C++0x feature which isn't
13664 implemented yet, and I expect it will involve deferring
13665 parsing of the initializer until end of class as with default
13666 arguments. So right here we only handle the latter. */
13667 if (!member_p && processing_template_decl)
13668 start_lambda_scope (decl);
13669 initializer = cp_parser_initializer (parser,
13671 &is_non_constant_init);
13672 if (!member_p && processing_template_decl)
13673 finish_lambda_scope ();
13677 /* The old parser allows attributes to appear after a parenthesized
13678 initializer. Mark Mitchell proposed removing this functionality
13679 on the GCC mailing lists on 2002-08-13. This parser accepts the
13680 attributes -- but ignores them. */
13681 if (cp_parser_allow_gnu_extensions_p (parser)
13682 && initialization_kind == CPP_OPEN_PAREN)
13683 if (cp_parser_attributes_opt (parser))
13684 warning (OPT_Wattributes,
13685 "attributes after parenthesized initializer ignored");
13687 /* For an in-class declaration, use `grokfield' to create the
13693 pop_scope (pushed_scope);
13694 pushed_scope = false;
13696 decl = grokfield (declarator, decl_specifiers,
13697 initializer, !is_non_constant_init,
13698 /*asmspec=*/NULL_TREE,
13699 prefix_attributes);
13700 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13701 cp_parser_save_default_args (parser, decl);
13704 /* Finish processing the declaration. But, skip friend
13706 if (!friend_p && decl && decl != error_mark_node)
13708 cp_finish_decl (decl,
13709 initializer, !is_non_constant_init,
13711 /* If the initializer is in parentheses, then this is
13712 a direct-initialization, which means that an
13713 `explicit' constructor is OK. Otherwise, an
13714 `explicit' constructor cannot be used. */
13715 ((is_direct_init || !is_initialized)
13716 ? 0 : LOOKUP_ONLYCONVERTING));
13718 else if ((cxx_dialect != cxx98) && friend_p
13719 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13720 /* Core issue #226 (C++0x only): A default template-argument
13721 shall not be specified in a friend class template
13723 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13724 /*is_partial=*/0, /*is_friend_decl=*/1);
13726 if (!friend_p && pushed_scope)
13727 pop_scope (pushed_scope);
13732 /* Parse a declarator.
13736 ptr-operator declarator
13738 abstract-declarator:
13739 ptr-operator abstract-declarator [opt]
13740 direct-abstract-declarator
13745 attributes [opt] direct-declarator
13746 attributes [opt] ptr-operator declarator
13748 abstract-declarator:
13749 attributes [opt] ptr-operator abstract-declarator [opt]
13750 attributes [opt] direct-abstract-declarator
13752 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13753 detect constructor, destructor or conversion operators. It is set
13754 to -1 if the declarator is a name, and +1 if it is a
13755 function. Otherwise it is set to zero. Usually you just want to
13756 test for >0, but internally the negative value is used.
13758 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13759 a decl-specifier-seq unless it declares a constructor, destructor,
13760 or conversion. It might seem that we could check this condition in
13761 semantic analysis, rather than parsing, but that makes it difficult
13762 to handle something like `f()'. We want to notice that there are
13763 no decl-specifiers, and therefore realize that this is an
13764 expression, not a declaration.)
13766 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13767 the declarator is a direct-declarator of the form "(...)".
13769 MEMBER_P is true iff this declarator is a member-declarator. */
13771 static cp_declarator *
13772 cp_parser_declarator (cp_parser* parser,
13773 cp_parser_declarator_kind dcl_kind,
13774 int* ctor_dtor_or_conv_p,
13775 bool* parenthesized_p,
13779 cp_declarator *declarator;
13780 enum tree_code code;
13781 cp_cv_quals cv_quals;
13783 tree attributes = NULL_TREE;
13785 /* Assume this is not a constructor, destructor, or type-conversion
13787 if (ctor_dtor_or_conv_p)
13788 *ctor_dtor_or_conv_p = 0;
13790 if (cp_parser_allow_gnu_extensions_p (parser))
13791 attributes = cp_parser_attributes_opt (parser);
13793 /* Peek at the next token. */
13794 token = cp_lexer_peek_token (parser->lexer);
13796 /* Check for the ptr-operator production. */
13797 cp_parser_parse_tentatively (parser);
13798 /* Parse the ptr-operator. */
13799 code = cp_parser_ptr_operator (parser,
13802 /* If that worked, then we have a ptr-operator. */
13803 if (cp_parser_parse_definitely (parser))
13805 /* If a ptr-operator was found, then this declarator was not
13807 if (parenthesized_p)
13808 *parenthesized_p = true;
13809 /* The dependent declarator is optional if we are parsing an
13810 abstract-declarator. */
13811 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13812 cp_parser_parse_tentatively (parser);
13814 /* Parse the dependent declarator. */
13815 declarator = cp_parser_declarator (parser, dcl_kind,
13816 /*ctor_dtor_or_conv_p=*/NULL,
13817 /*parenthesized_p=*/NULL,
13818 /*member_p=*/false);
13820 /* If we are parsing an abstract-declarator, we must handle the
13821 case where the dependent declarator is absent. */
13822 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13823 && !cp_parser_parse_definitely (parser))
13826 declarator = cp_parser_make_indirect_declarator
13827 (code, class_type, cv_quals, declarator);
13829 /* Everything else is a direct-declarator. */
13832 if (parenthesized_p)
13833 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13835 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13836 ctor_dtor_or_conv_p,
13840 if (attributes && declarator && declarator != cp_error_declarator)
13841 declarator->attributes = attributes;
13846 /* Parse a direct-declarator or direct-abstract-declarator.
13850 direct-declarator ( parameter-declaration-clause )
13851 cv-qualifier-seq [opt]
13852 exception-specification [opt]
13853 direct-declarator [ constant-expression [opt] ]
13856 direct-abstract-declarator:
13857 direct-abstract-declarator [opt]
13858 ( parameter-declaration-clause )
13859 cv-qualifier-seq [opt]
13860 exception-specification [opt]
13861 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13862 ( abstract-declarator )
13864 Returns a representation of the declarator. DCL_KIND is
13865 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13866 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13867 we are parsing a direct-declarator. It is
13868 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13869 of ambiguity we prefer an abstract declarator, as per
13870 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13871 cp_parser_declarator. */
13873 static cp_declarator *
13874 cp_parser_direct_declarator (cp_parser* parser,
13875 cp_parser_declarator_kind dcl_kind,
13876 int* ctor_dtor_or_conv_p,
13880 cp_declarator *declarator = NULL;
13881 tree scope = NULL_TREE;
13882 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13883 bool saved_in_declarator_p = parser->in_declarator_p;
13885 tree pushed_scope = NULL_TREE;
13889 /* Peek at the next token. */
13890 token = cp_lexer_peek_token (parser->lexer);
13891 if (token->type == CPP_OPEN_PAREN)
13893 /* This is either a parameter-declaration-clause, or a
13894 parenthesized declarator. When we know we are parsing a
13895 named declarator, it must be a parenthesized declarator
13896 if FIRST is true. For instance, `(int)' is a
13897 parameter-declaration-clause, with an omitted
13898 direct-abstract-declarator. But `((*))', is a
13899 parenthesized abstract declarator. Finally, when T is a
13900 template parameter `(T)' is a
13901 parameter-declaration-clause, and not a parenthesized
13904 We first try and parse a parameter-declaration-clause,
13905 and then try a nested declarator (if FIRST is true).
13907 It is not an error for it not to be a
13908 parameter-declaration-clause, even when FIRST is
13914 The first is the declaration of a function while the
13915 second is the definition of a variable, including its
13918 Having seen only the parenthesis, we cannot know which of
13919 these two alternatives should be selected. Even more
13920 complex are examples like:
13925 The former is a function-declaration; the latter is a
13926 variable initialization.
13928 Thus again, we try a parameter-declaration-clause, and if
13929 that fails, we back out and return. */
13931 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13934 unsigned saved_num_template_parameter_lists;
13935 bool is_declarator = false;
13938 /* In a member-declarator, the only valid interpretation
13939 of a parenthesis is the start of a
13940 parameter-declaration-clause. (It is invalid to
13941 initialize a static data member with a parenthesized
13942 initializer; only the "=" form of initialization is
13945 cp_parser_parse_tentatively (parser);
13947 /* Consume the `('. */
13948 cp_lexer_consume_token (parser->lexer);
13951 /* If this is going to be an abstract declarator, we're
13952 in a declarator and we can't have default args. */
13953 parser->default_arg_ok_p = false;
13954 parser->in_declarator_p = true;
13957 /* Inside the function parameter list, surrounding
13958 template-parameter-lists do not apply. */
13959 saved_num_template_parameter_lists
13960 = parser->num_template_parameter_lists;
13961 parser->num_template_parameter_lists = 0;
13963 begin_scope (sk_function_parms, NULL_TREE);
13965 /* Parse the parameter-declaration-clause. */
13966 params = cp_parser_parameter_declaration_clause (parser);
13968 parser->num_template_parameter_lists
13969 = saved_num_template_parameter_lists;
13971 /* If all went well, parse the cv-qualifier-seq and the
13972 exception-specification. */
13973 if (member_p || cp_parser_parse_definitely (parser))
13975 cp_cv_quals cv_quals;
13976 tree exception_specification;
13979 is_declarator = true;
13981 if (ctor_dtor_or_conv_p)
13982 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13984 /* Consume the `)'. */
13985 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13987 /* Parse the cv-qualifier-seq. */
13988 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13989 /* And the exception-specification. */
13990 exception_specification
13991 = cp_parser_exception_specification_opt (parser);
13994 = cp_parser_late_return_type_opt (parser);
13996 /* Create the function-declarator. */
13997 declarator = make_call_declarator (declarator,
14000 exception_specification,
14002 /* Any subsequent parameter lists are to do with
14003 return type, so are not those of the declared
14005 parser->default_arg_ok_p = false;
14008 /* Remove the function parms from scope. */
14009 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14010 pop_binding (DECL_NAME (t), t);
14014 /* Repeat the main loop. */
14018 /* If this is the first, we can try a parenthesized
14022 bool saved_in_type_id_in_expr_p;
14024 parser->default_arg_ok_p = saved_default_arg_ok_p;
14025 parser->in_declarator_p = saved_in_declarator_p;
14027 /* Consume the `('. */
14028 cp_lexer_consume_token (parser->lexer);
14029 /* Parse the nested declarator. */
14030 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14031 parser->in_type_id_in_expr_p = true;
14033 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14034 /*parenthesized_p=*/NULL,
14036 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14038 /* Expect a `)'. */
14039 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14040 declarator = cp_error_declarator;
14041 if (declarator == cp_error_declarator)
14044 goto handle_declarator;
14046 /* Otherwise, we must be done. */
14050 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14051 && token->type == CPP_OPEN_SQUARE)
14053 /* Parse an array-declarator. */
14056 if (ctor_dtor_or_conv_p)
14057 *ctor_dtor_or_conv_p = 0;
14060 parser->default_arg_ok_p = false;
14061 parser->in_declarator_p = true;
14062 /* Consume the `['. */
14063 cp_lexer_consume_token (parser->lexer);
14064 /* Peek at the next token. */
14065 token = cp_lexer_peek_token (parser->lexer);
14066 /* If the next token is `]', then there is no
14067 constant-expression. */
14068 if (token->type != CPP_CLOSE_SQUARE)
14070 bool non_constant_p;
14073 = cp_parser_constant_expression (parser,
14074 /*allow_non_constant=*/true,
14076 if (!non_constant_p)
14077 bounds = fold_non_dependent_expr (bounds);
14078 /* Normally, the array bound must be an integral constant
14079 expression. However, as an extension, we allow VLAs
14080 in function scopes. */
14081 else if (!parser->in_function_body)
14083 error_at (token->location,
14084 "array bound is not an integer constant");
14085 bounds = error_mark_node;
14087 else if (processing_template_decl && !error_operand_p (bounds))
14089 /* Remember this wasn't a constant-expression. */
14090 bounds = build_nop (TREE_TYPE (bounds), bounds);
14091 TREE_SIDE_EFFECTS (bounds) = 1;
14095 bounds = NULL_TREE;
14096 /* Look for the closing `]'. */
14097 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14099 declarator = cp_error_declarator;
14103 declarator = make_array_declarator (declarator, bounds);
14105 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14108 tree qualifying_scope;
14109 tree unqualified_name;
14110 special_function_kind sfk;
14112 bool pack_expansion_p = false;
14113 cp_token *declarator_id_start_token;
14115 /* Parse a declarator-id */
14116 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14119 cp_parser_parse_tentatively (parser);
14121 /* If we see an ellipsis, we should be looking at a
14123 if (token->type == CPP_ELLIPSIS)
14125 /* Consume the `...' */
14126 cp_lexer_consume_token (parser->lexer);
14128 pack_expansion_p = true;
14132 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14134 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14135 qualifying_scope = parser->scope;
14140 if (!unqualified_name && pack_expansion_p)
14142 /* Check whether an error occurred. */
14143 okay = !cp_parser_error_occurred (parser);
14145 /* We already consumed the ellipsis to mark a
14146 parameter pack, but we have no way to report it,
14147 so abort the tentative parse. We will be exiting
14148 immediately anyway. */
14149 cp_parser_abort_tentative_parse (parser);
14152 okay = cp_parser_parse_definitely (parser);
14155 unqualified_name = error_mark_node;
14156 else if (unqualified_name
14157 && (qualifying_scope
14158 || (TREE_CODE (unqualified_name)
14159 != IDENTIFIER_NODE)))
14161 cp_parser_error (parser, "expected unqualified-id");
14162 unqualified_name = error_mark_node;
14166 if (!unqualified_name)
14168 if (unqualified_name == error_mark_node)
14170 declarator = cp_error_declarator;
14171 pack_expansion_p = false;
14172 declarator->parameter_pack_p = false;
14176 if (qualifying_scope && at_namespace_scope_p ()
14177 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14179 /* In the declaration of a member of a template class
14180 outside of the class itself, the SCOPE will sometimes
14181 be a TYPENAME_TYPE. For example, given:
14183 template <typename T>
14184 int S<T>::R::i = 3;
14186 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14187 this context, we must resolve S<T>::R to an ordinary
14188 type, rather than a typename type.
14190 The reason we normally avoid resolving TYPENAME_TYPEs
14191 is that a specialization of `S' might render
14192 `S<T>::R' not a type. However, if `S' is
14193 specialized, then this `i' will not be used, so there
14194 is no harm in resolving the types here. */
14197 /* Resolve the TYPENAME_TYPE. */
14198 type = resolve_typename_type (qualifying_scope,
14199 /*only_current_p=*/false);
14200 /* If that failed, the declarator is invalid. */
14201 if (TREE_CODE (type) == TYPENAME_TYPE)
14203 if (typedef_variant_p (type))
14204 error_at (declarator_id_start_token->location,
14205 "cannot define member of dependent typedef "
14208 error_at (declarator_id_start_token->location,
14209 "%<%T::%E%> is not a type",
14210 TYPE_CONTEXT (qualifying_scope),
14211 TYPE_IDENTIFIER (qualifying_scope));
14213 qualifying_scope = type;
14218 if (unqualified_name)
14222 if (qualifying_scope
14223 && CLASS_TYPE_P (qualifying_scope))
14224 class_type = qualifying_scope;
14226 class_type = current_class_type;
14228 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14230 tree name_type = TREE_TYPE (unqualified_name);
14231 if (class_type && same_type_p (name_type, class_type))
14233 if (qualifying_scope
14234 && CLASSTYPE_USE_TEMPLATE (name_type))
14236 error_at (declarator_id_start_token->location,
14237 "invalid use of constructor as a template");
14238 inform (declarator_id_start_token->location,
14239 "use %<%T::%D%> instead of %<%T::%D%> to "
14240 "name the constructor in a qualified name",
14242 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14243 class_type, name_type);
14244 declarator = cp_error_declarator;
14248 unqualified_name = constructor_name (class_type);
14252 /* We do not attempt to print the declarator
14253 here because we do not have enough
14254 information about its original syntactic
14256 cp_parser_error (parser, "invalid declarator");
14257 declarator = cp_error_declarator;
14264 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14265 sfk = sfk_destructor;
14266 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14267 sfk = sfk_conversion;
14268 else if (/* There's no way to declare a constructor
14269 for an anonymous type, even if the type
14270 got a name for linkage purposes. */
14271 !TYPE_WAS_ANONYMOUS (class_type)
14272 && constructor_name_p (unqualified_name,
14275 unqualified_name = constructor_name (class_type);
14276 sfk = sfk_constructor;
14279 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14280 *ctor_dtor_or_conv_p = -1;
14283 declarator = make_id_declarator (qualifying_scope,
14286 declarator->id_loc = token->location;
14287 declarator->parameter_pack_p = pack_expansion_p;
14289 if (pack_expansion_p)
14290 maybe_warn_variadic_templates ();
14293 handle_declarator:;
14294 scope = get_scope_of_declarator (declarator);
14296 /* Any names that appear after the declarator-id for a
14297 member are looked up in the containing scope. */
14298 pushed_scope = push_scope (scope);
14299 parser->in_declarator_p = true;
14300 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14301 || (declarator && declarator->kind == cdk_id))
14302 /* Default args are only allowed on function
14304 parser->default_arg_ok_p = saved_default_arg_ok_p;
14306 parser->default_arg_ok_p = false;
14315 /* For an abstract declarator, we might wind up with nothing at this
14316 point. That's an error; the declarator is not optional. */
14318 cp_parser_error (parser, "expected declarator");
14320 /* If we entered a scope, we must exit it now. */
14322 pop_scope (pushed_scope);
14324 parser->default_arg_ok_p = saved_default_arg_ok_p;
14325 parser->in_declarator_p = saved_in_declarator_p;
14330 /* Parse a ptr-operator.
14333 * cv-qualifier-seq [opt]
14335 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14340 & cv-qualifier-seq [opt]
14342 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14343 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14344 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14345 filled in with the TYPE containing the member. *CV_QUALS is
14346 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14347 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14348 Note that the tree codes returned by this function have nothing
14349 to do with the types of trees that will be eventually be created
14350 to represent the pointer or reference type being parsed. They are
14351 just constants with suggestive names. */
14352 static enum tree_code
14353 cp_parser_ptr_operator (cp_parser* parser,
14355 cp_cv_quals *cv_quals)
14357 enum tree_code code = ERROR_MARK;
14360 /* Assume that it's not a pointer-to-member. */
14362 /* And that there are no cv-qualifiers. */
14363 *cv_quals = TYPE_UNQUALIFIED;
14365 /* Peek at the next token. */
14366 token = cp_lexer_peek_token (parser->lexer);
14368 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14369 if (token->type == CPP_MULT)
14370 code = INDIRECT_REF;
14371 else if (token->type == CPP_AND)
14373 else if ((cxx_dialect != cxx98) &&
14374 token->type == CPP_AND_AND) /* C++0x only */
14375 code = NON_LVALUE_EXPR;
14377 if (code != ERROR_MARK)
14379 /* Consume the `*', `&' or `&&'. */
14380 cp_lexer_consume_token (parser->lexer);
14382 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14383 `&', if we are allowing GNU extensions. (The only qualifier
14384 that can legally appear after `&' is `restrict', but that is
14385 enforced during semantic analysis. */
14386 if (code == INDIRECT_REF
14387 || cp_parser_allow_gnu_extensions_p (parser))
14388 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14392 /* Try the pointer-to-member case. */
14393 cp_parser_parse_tentatively (parser);
14394 /* Look for the optional `::' operator. */
14395 cp_parser_global_scope_opt (parser,
14396 /*current_scope_valid_p=*/false);
14397 /* Look for the nested-name specifier. */
14398 token = cp_lexer_peek_token (parser->lexer);
14399 cp_parser_nested_name_specifier (parser,
14400 /*typename_keyword_p=*/false,
14401 /*check_dependency_p=*/true,
14403 /*is_declaration=*/false);
14404 /* If we found it, and the next token is a `*', then we are
14405 indeed looking at a pointer-to-member operator. */
14406 if (!cp_parser_error_occurred (parser)
14407 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14409 /* Indicate that the `*' operator was used. */
14410 code = INDIRECT_REF;
14412 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14413 error_at (token->location, "%qD is a namespace", parser->scope);
14416 /* The type of which the member is a member is given by the
14418 *type = parser->scope;
14419 /* The next name will not be qualified. */
14420 parser->scope = NULL_TREE;
14421 parser->qualifying_scope = NULL_TREE;
14422 parser->object_scope = NULL_TREE;
14423 /* Look for the optional cv-qualifier-seq. */
14424 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14427 /* If that didn't work we don't have a ptr-operator. */
14428 if (!cp_parser_parse_definitely (parser))
14429 cp_parser_error (parser, "expected ptr-operator");
14435 /* Parse an (optional) cv-qualifier-seq.
14438 cv-qualifier cv-qualifier-seq [opt]
14449 Returns a bitmask representing the cv-qualifiers. */
14452 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14454 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14459 cp_cv_quals cv_qualifier;
14461 /* Peek at the next token. */
14462 token = cp_lexer_peek_token (parser->lexer);
14463 /* See if it's a cv-qualifier. */
14464 switch (token->keyword)
14467 cv_qualifier = TYPE_QUAL_CONST;
14471 cv_qualifier = TYPE_QUAL_VOLATILE;
14475 cv_qualifier = TYPE_QUAL_RESTRICT;
14479 cv_qualifier = TYPE_UNQUALIFIED;
14486 if (cv_quals & cv_qualifier)
14488 error_at (token->location, "duplicate cv-qualifier");
14489 cp_lexer_purge_token (parser->lexer);
14493 cp_lexer_consume_token (parser->lexer);
14494 cv_quals |= cv_qualifier;
14501 /* Parse a late-specified return type, if any. This is not a separate
14502 non-terminal, but part of a function declarator, which looks like
14504 -> trailing-type-specifier-seq abstract-declarator(opt)
14506 Returns the type indicated by the type-id. */
14509 cp_parser_late_return_type_opt (cp_parser* parser)
14513 /* Peek at the next token. */
14514 token = cp_lexer_peek_token (parser->lexer);
14515 /* A late-specified return type is indicated by an initial '->'. */
14516 if (token->type != CPP_DEREF)
14519 /* Consume the ->. */
14520 cp_lexer_consume_token (parser->lexer);
14522 return cp_parser_trailing_type_id (parser);
14525 /* Parse a declarator-id.
14529 :: [opt] nested-name-specifier [opt] type-name
14531 In the `id-expression' case, the value returned is as for
14532 cp_parser_id_expression if the id-expression was an unqualified-id.
14533 If the id-expression was a qualified-id, then a SCOPE_REF is
14534 returned. The first operand is the scope (either a NAMESPACE_DECL
14535 or TREE_TYPE), but the second is still just a representation of an
14539 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14542 /* The expression must be an id-expression. Assume that qualified
14543 names are the names of types so that:
14546 int S<T>::R::i = 3;
14548 will work; we must treat `S<T>::R' as the name of a type.
14549 Similarly, assume that qualified names are templates, where
14553 int S<T>::R<T>::i = 3;
14556 id = cp_parser_id_expression (parser,
14557 /*template_keyword_p=*/false,
14558 /*check_dependency_p=*/false,
14559 /*template_p=*/NULL,
14560 /*declarator_p=*/true,
14562 if (id && BASELINK_P (id))
14563 id = BASELINK_FUNCTIONS (id);
14567 /* Parse a type-id.
14570 type-specifier-seq abstract-declarator [opt]
14572 Returns the TYPE specified. */
14575 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14576 bool is_trailing_return)
14578 cp_decl_specifier_seq type_specifier_seq;
14579 cp_declarator *abstract_declarator;
14581 /* Parse the type-specifier-seq. */
14582 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14583 is_trailing_return,
14584 &type_specifier_seq);
14585 if (type_specifier_seq.type == error_mark_node)
14586 return error_mark_node;
14588 /* There might or might not be an abstract declarator. */
14589 cp_parser_parse_tentatively (parser);
14590 /* Look for the declarator. */
14591 abstract_declarator
14592 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14593 /*parenthesized_p=*/NULL,
14594 /*member_p=*/false);
14595 /* Check to see if there really was a declarator. */
14596 if (!cp_parser_parse_definitely (parser))
14597 abstract_declarator = NULL;
14599 if (type_specifier_seq.type
14600 && type_uses_auto (type_specifier_seq.type))
14602 /* A type-id with type 'auto' is only ok if the abstract declarator
14603 is a function declarator with a late-specified return type. */
14604 if (abstract_declarator
14605 && abstract_declarator->kind == cdk_function
14606 && abstract_declarator->u.function.late_return_type)
14610 error ("invalid use of %<auto%>");
14611 return error_mark_node;
14615 return groktypename (&type_specifier_seq, abstract_declarator,
14619 static tree cp_parser_type_id (cp_parser *parser)
14621 return cp_parser_type_id_1 (parser, false, false);
14624 static tree cp_parser_template_type_arg (cp_parser *parser)
14626 return cp_parser_type_id_1 (parser, true, false);
14629 static tree cp_parser_trailing_type_id (cp_parser *parser)
14631 return cp_parser_type_id_1 (parser, false, true);
14634 /* Parse a type-specifier-seq.
14636 type-specifier-seq:
14637 type-specifier type-specifier-seq [opt]
14641 type-specifier-seq:
14642 attributes type-specifier-seq [opt]
14644 If IS_DECLARATION is true, we are at the start of a "condition" or
14645 exception-declaration, so we might be followed by a declarator-id.
14647 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14648 i.e. we've just seen "->".
14650 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14653 cp_parser_type_specifier_seq (cp_parser* parser,
14654 bool is_declaration,
14655 bool is_trailing_return,
14656 cp_decl_specifier_seq *type_specifier_seq)
14658 bool seen_type_specifier = false;
14659 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14660 cp_token *start_token = NULL;
14662 /* Clear the TYPE_SPECIFIER_SEQ. */
14663 clear_decl_specs (type_specifier_seq);
14665 /* In the context of a trailing return type, enum E { } is an
14666 elaborated-type-specifier followed by a function-body, not an
14668 if (is_trailing_return)
14669 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14671 /* Parse the type-specifiers and attributes. */
14674 tree type_specifier;
14675 bool is_cv_qualifier;
14677 /* Check for attributes first. */
14678 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14680 type_specifier_seq->attributes =
14681 chainon (type_specifier_seq->attributes,
14682 cp_parser_attributes_opt (parser));
14686 /* record the token of the beginning of the type specifier seq,
14687 for error reporting purposes*/
14689 start_token = cp_lexer_peek_token (parser->lexer);
14691 /* Look for the type-specifier. */
14692 type_specifier = cp_parser_type_specifier (parser,
14694 type_specifier_seq,
14695 /*is_declaration=*/false,
14698 if (!type_specifier)
14700 /* If the first type-specifier could not be found, this is not a
14701 type-specifier-seq at all. */
14702 if (!seen_type_specifier)
14704 cp_parser_error (parser, "expected type-specifier");
14705 type_specifier_seq->type = error_mark_node;
14708 /* If subsequent type-specifiers could not be found, the
14709 type-specifier-seq is complete. */
14713 seen_type_specifier = true;
14714 /* The standard says that a condition can be:
14716 type-specifier-seq declarator = assignment-expression
14723 we should treat the "S" as a declarator, not as a
14724 type-specifier. The standard doesn't say that explicitly for
14725 type-specifier-seq, but it does say that for
14726 decl-specifier-seq in an ordinary declaration. Perhaps it
14727 would be clearer just to allow a decl-specifier-seq here, and
14728 then add a semantic restriction that if any decl-specifiers
14729 that are not type-specifiers appear, the program is invalid. */
14730 if (is_declaration && !is_cv_qualifier)
14731 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14734 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14737 /* Parse a parameter-declaration-clause.
14739 parameter-declaration-clause:
14740 parameter-declaration-list [opt] ... [opt]
14741 parameter-declaration-list , ...
14743 Returns a representation for the parameter declarations. A return
14744 value of NULL indicates a parameter-declaration-clause consisting
14745 only of an ellipsis. */
14748 cp_parser_parameter_declaration_clause (cp_parser* parser)
14755 /* Peek at the next token. */
14756 token = cp_lexer_peek_token (parser->lexer);
14757 /* Check for trivial parameter-declaration-clauses. */
14758 if (token->type == CPP_ELLIPSIS)
14760 /* Consume the `...' token. */
14761 cp_lexer_consume_token (parser->lexer);
14764 else if (token->type == CPP_CLOSE_PAREN)
14765 /* There are no parameters. */
14767 #ifndef NO_IMPLICIT_EXTERN_C
14768 if (in_system_header && current_class_type == NULL
14769 && current_lang_name == lang_name_c)
14773 return void_list_node;
14775 /* Check for `(void)', too, which is a special case. */
14776 else if (token->keyword == RID_VOID
14777 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14778 == CPP_CLOSE_PAREN))
14780 /* Consume the `void' token. */
14781 cp_lexer_consume_token (parser->lexer);
14782 /* There are no parameters. */
14783 return void_list_node;
14786 /* Parse the parameter-declaration-list. */
14787 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14788 /* If a parse error occurred while parsing the
14789 parameter-declaration-list, then the entire
14790 parameter-declaration-clause is erroneous. */
14794 /* Peek at the next token. */
14795 token = cp_lexer_peek_token (parser->lexer);
14796 /* If it's a `,', the clause should terminate with an ellipsis. */
14797 if (token->type == CPP_COMMA)
14799 /* Consume the `,'. */
14800 cp_lexer_consume_token (parser->lexer);
14801 /* Expect an ellipsis. */
14803 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14805 /* It might also be `...' if the optional trailing `,' was
14807 else if (token->type == CPP_ELLIPSIS)
14809 /* Consume the `...' token. */
14810 cp_lexer_consume_token (parser->lexer);
14811 /* And remember that we saw it. */
14815 ellipsis_p = false;
14817 /* Finish the parameter list. */
14819 parameters = chainon (parameters, void_list_node);
14824 /* Parse a parameter-declaration-list.
14826 parameter-declaration-list:
14827 parameter-declaration
14828 parameter-declaration-list , parameter-declaration
14830 Returns a representation of the parameter-declaration-list, as for
14831 cp_parser_parameter_declaration_clause. However, the
14832 `void_list_node' is never appended to the list. Upon return,
14833 *IS_ERROR will be true iff an error occurred. */
14836 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14838 tree parameters = NULL_TREE;
14839 tree *tail = ¶meters;
14840 bool saved_in_unbraced_linkage_specification_p;
14843 /* Assume all will go well. */
14845 /* The special considerations that apply to a function within an
14846 unbraced linkage specifications do not apply to the parameters
14847 to the function. */
14848 saved_in_unbraced_linkage_specification_p
14849 = parser->in_unbraced_linkage_specification_p;
14850 parser->in_unbraced_linkage_specification_p = false;
14852 /* Look for more parameters. */
14855 cp_parameter_declarator *parameter;
14856 tree decl = error_mark_node;
14857 bool parenthesized_p;
14858 /* Parse the parameter. */
14860 = cp_parser_parameter_declaration (parser,
14861 /*template_parm_p=*/false,
14864 /* We don't know yet if the enclosing context is deprecated, so wait
14865 and warn in grokparms if appropriate. */
14866 deprecated_state = DEPRECATED_SUPPRESS;
14869 decl = grokdeclarator (parameter->declarator,
14870 ¶meter->decl_specifiers,
14872 parameter->default_argument != NULL_TREE,
14873 ¶meter->decl_specifiers.attributes);
14875 deprecated_state = DEPRECATED_NORMAL;
14877 /* If a parse error occurred parsing the parameter declaration,
14878 then the entire parameter-declaration-list is erroneous. */
14879 if (decl == error_mark_node)
14882 parameters = error_mark_node;
14886 if (parameter->decl_specifiers.attributes)
14887 cplus_decl_attributes (&decl,
14888 parameter->decl_specifiers.attributes,
14890 if (DECL_NAME (decl))
14891 decl = pushdecl (decl);
14893 if (decl != error_mark_node)
14895 retrofit_lang_decl (decl);
14896 DECL_PARM_INDEX (decl) = ++index;
14899 /* Add the new parameter to the list. */
14900 *tail = build_tree_list (parameter->default_argument, decl);
14901 tail = &TREE_CHAIN (*tail);
14903 /* Peek at the next token. */
14904 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14905 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14906 /* These are for Objective-C++ */
14907 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14908 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14909 /* The parameter-declaration-list is complete. */
14911 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14915 /* Peek at the next token. */
14916 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14917 /* If it's an ellipsis, then the list is complete. */
14918 if (token->type == CPP_ELLIPSIS)
14920 /* Otherwise, there must be more parameters. Consume the
14922 cp_lexer_consume_token (parser->lexer);
14923 /* When parsing something like:
14925 int i(float f, double d)
14927 we can tell after seeing the declaration for "f" that we
14928 are not looking at an initialization of a variable "i",
14929 but rather at the declaration of a function "i".
14931 Due to the fact that the parsing of template arguments
14932 (as specified to a template-id) requires backtracking we
14933 cannot use this technique when inside a template argument
14935 if (!parser->in_template_argument_list_p
14936 && !parser->in_type_id_in_expr_p
14937 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14938 /* However, a parameter-declaration of the form
14939 "foat(f)" (which is a valid declaration of a
14940 parameter "f") can also be interpreted as an
14941 expression (the conversion of "f" to "float"). */
14942 && !parenthesized_p)
14943 cp_parser_commit_to_tentative_parse (parser);
14947 cp_parser_error (parser, "expected %<,%> or %<...%>");
14948 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14949 cp_parser_skip_to_closing_parenthesis (parser,
14950 /*recovering=*/true,
14951 /*or_comma=*/false,
14952 /*consume_paren=*/false);
14957 parser->in_unbraced_linkage_specification_p
14958 = saved_in_unbraced_linkage_specification_p;
14963 /* Parse a parameter declaration.
14965 parameter-declaration:
14966 decl-specifier-seq ... [opt] declarator
14967 decl-specifier-seq declarator = assignment-expression
14968 decl-specifier-seq ... [opt] abstract-declarator [opt]
14969 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14971 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14972 declares a template parameter. (In that case, a non-nested `>'
14973 token encountered during the parsing of the assignment-expression
14974 is not interpreted as a greater-than operator.)
14976 Returns a representation of the parameter, or NULL if an error
14977 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14978 true iff the declarator is of the form "(p)". */
14980 static cp_parameter_declarator *
14981 cp_parser_parameter_declaration (cp_parser *parser,
14982 bool template_parm_p,
14983 bool *parenthesized_p)
14985 int declares_class_or_enum;
14986 bool greater_than_is_operator_p;
14987 cp_decl_specifier_seq decl_specifiers;
14988 cp_declarator *declarator;
14989 tree default_argument;
14990 cp_token *token = NULL, *declarator_token_start = NULL;
14991 const char *saved_message;
14993 /* In a template parameter, `>' is not an operator.
14997 When parsing a default template-argument for a non-type
14998 template-parameter, the first non-nested `>' is taken as the end
14999 of the template parameter-list rather than a greater-than
15001 greater_than_is_operator_p = !template_parm_p;
15003 /* Type definitions may not appear in parameter types. */
15004 saved_message = parser->type_definition_forbidden_message;
15005 parser->type_definition_forbidden_message
15006 = "types may not be defined in parameter types";
15008 /* Parse the declaration-specifiers. */
15009 cp_parser_decl_specifier_seq (parser,
15010 CP_PARSER_FLAGS_NONE,
15012 &declares_class_or_enum);
15014 /* Complain about missing 'typename' or other invalid type names. */
15015 if (!decl_specifiers.any_type_specifiers_p)
15016 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15018 /* If an error occurred, there's no reason to attempt to parse the
15019 rest of the declaration. */
15020 if (cp_parser_error_occurred (parser))
15022 parser->type_definition_forbidden_message = saved_message;
15026 /* Peek at the next token. */
15027 token = cp_lexer_peek_token (parser->lexer);
15029 /* If the next token is a `)', `,', `=', `>', or `...', then there
15030 is no declarator. However, when variadic templates are enabled,
15031 there may be a declarator following `...'. */
15032 if (token->type == CPP_CLOSE_PAREN
15033 || token->type == CPP_COMMA
15034 || token->type == CPP_EQ
15035 || token->type == CPP_GREATER)
15038 if (parenthesized_p)
15039 *parenthesized_p = false;
15041 /* Otherwise, there should be a declarator. */
15044 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15045 parser->default_arg_ok_p = false;
15047 /* After seeing a decl-specifier-seq, if the next token is not a
15048 "(", there is no possibility that the code is a valid
15049 expression. Therefore, if parsing tentatively, we commit at
15051 if (!parser->in_template_argument_list_p
15052 /* In an expression context, having seen:
15056 we cannot be sure whether we are looking at a
15057 function-type (taking a "char" as a parameter) or a cast
15058 of some object of type "char" to "int". */
15059 && !parser->in_type_id_in_expr_p
15060 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15061 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15062 cp_parser_commit_to_tentative_parse (parser);
15063 /* Parse the declarator. */
15064 declarator_token_start = token;
15065 declarator = cp_parser_declarator (parser,
15066 CP_PARSER_DECLARATOR_EITHER,
15067 /*ctor_dtor_or_conv_p=*/NULL,
15069 /*member_p=*/false);
15070 parser->default_arg_ok_p = saved_default_arg_ok_p;
15071 /* After the declarator, allow more attributes. */
15072 decl_specifiers.attributes
15073 = chainon (decl_specifiers.attributes,
15074 cp_parser_attributes_opt (parser));
15077 /* If the next token is an ellipsis, and we have not seen a
15078 declarator name, and the type of the declarator contains parameter
15079 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15080 a parameter pack expansion expression. Otherwise, leave the
15081 ellipsis for a C-style variadic function. */
15082 token = cp_lexer_peek_token (parser->lexer);
15083 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15085 tree type = decl_specifiers.type;
15087 if (type && DECL_P (type))
15088 type = TREE_TYPE (type);
15091 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15092 && declarator_can_be_parameter_pack (declarator)
15093 && (!declarator || !declarator->parameter_pack_p)
15094 && uses_parameter_packs (type))
15096 /* Consume the `...'. */
15097 cp_lexer_consume_token (parser->lexer);
15098 maybe_warn_variadic_templates ();
15100 /* Build a pack expansion type */
15102 declarator->parameter_pack_p = true;
15104 decl_specifiers.type = make_pack_expansion (type);
15108 /* The restriction on defining new types applies only to the type
15109 of the parameter, not to the default argument. */
15110 parser->type_definition_forbidden_message = saved_message;
15112 /* If the next token is `=', then process a default argument. */
15113 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15115 /* Consume the `='. */
15116 cp_lexer_consume_token (parser->lexer);
15118 /* If we are defining a class, then the tokens that make up the
15119 default argument must be saved and processed later. */
15120 if (!template_parm_p && at_class_scope_p ()
15121 && TYPE_BEING_DEFINED (current_class_type)
15122 && !LAMBDA_TYPE_P (current_class_type))
15124 unsigned depth = 0;
15125 int maybe_template_id = 0;
15126 cp_token *first_token;
15129 /* Add tokens until we have processed the entire default
15130 argument. We add the range [first_token, token). */
15131 first_token = cp_lexer_peek_token (parser->lexer);
15136 /* Peek at the next token. */
15137 token = cp_lexer_peek_token (parser->lexer);
15138 /* What we do depends on what token we have. */
15139 switch (token->type)
15141 /* In valid code, a default argument must be
15142 immediately followed by a `,' `)', or `...'. */
15144 if (depth == 0 && maybe_template_id)
15146 /* If we've seen a '<', we might be in a
15147 template-argument-list. Until Core issue 325 is
15148 resolved, we don't know how this situation ought
15149 to be handled, so try to DTRT. We check whether
15150 what comes after the comma is a valid parameter
15151 declaration list. If it is, then the comma ends
15152 the default argument; otherwise the default
15153 argument continues. */
15154 bool error = false;
15156 /* Set ITALP so cp_parser_parameter_declaration_list
15157 doesn't decide to commit to this parse. */
15158 bool saved_italp = parser->in_template_argument_list_p;
15159 parser->in_template_argument_list_p = true;
15161 cp_parser_parse_tentatively (parser);
15162 cp_lexer_consume_token (parser->lexer);
15163 cp_parser_parameter_declaration_list (parser, &error);
15164 if (!cp_parser_error_occurred (parser) && !error)
15166 cp_parser_abort_tentative_parse (parser);
15168 parser->in_template_argument_list_p = saved_italp;
15171 case CPP_CLOSE_PAREN:
15173 /* If we run into a non-nested `;', `}', or `]',
15174 then the code is invalid -- but the default
15175 argument is certainly over. */
15176 case CPP_SEMICOLON:
15177 case CPP_CLOSE_BRACE:
15178 case CPP_CLOSE_SQUARE:
15181 /* Update DEPTH, if necessary. */
15182 else if (token->type == CPP_CLOSE_PAREN
15183 || token->type == CPP_CLOSE_BRACE
15184 || token->type == CPP_CLOSE_SQUARE)
15188 case CPP_OPEN_PAREN:
15189 case CPP_OPEN_SQUARE:
15190 case CPP_OPEN_BRACE:
15196 /* This might be the comparison operator, or it might
15197 start a template argument list. */
15198 ++maybe_template_id;
15202 if (cxx_dialect == cxx98)
15204 /* Fall through for C++0x, which treats the `>>'
15205 operator like two `>' tokens in certain
15211 /* This might be an operator, or it might close a
15212 template argument list. But if a previous '<'
15213 started a template argument list, this will have
15214 closed it, so we can't be in one anymore. */
15215 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15216 if (maybe_template_id < 0)
15217 maybe_template_id = 0;
15221 /* If we run out of tokens, issue an error message. */
15223 case CPP_PRAGMA_EOL:
15224 error_at (token->location, "file ends in default argument");
15230 /* In these cases, we should look for template-ids.
15231 For example, if the default argument is
15232 `X<int, double>()', we need to do name lookup to
15233 figure out whether or not `X' is a template; if
15234 so, the `,' does not end the default argument.
15236 That is not yet done. */
15243 /* If we've reached the end, stop. */
15247 /* Add the token to the token block. */
15248 token = cp_lexer_consume_token (parser->lexer);
15251 /* Create a DEFAULT_ARG to represent the unparsed default
15253 default_argument = make_node (DEFAULT_ARG);
15254 DEFARG_TOKENS (default_argument)
15255 = cp_token_cache_new (first_token, token);
15256 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15258 /* Outside of a class definition, we can just parse the
15259 assignment-expression. */
15262 token = cp_lexer_peek_token (parser->lexer);
15264 = cp_parser_default_argument (parser, template_parm_p);
15267 if (!parser->default_arg_ok_p)
15269 if (flag_permissive)
15270 warning (0, "deprecated use of default argument for parameter of non-function");
15273 error_at (token->location,
15274 "default arguments are only "
15275 "permitted for function parameters");
15276 default_argument = NULL_TREE;
15279 else if ((declarator && declarator->parameter_pack_p)
15280 || (decl_specifiers.type
15281 && PACK_EXPANSION_P (decl_specifiers.type)))
15283 /* Find the name of the parameter pack. */
15284 cp_declarator *id_declarator = declarator;
15285 while (id_declarator && id_declarator->kind != cdk_id)
15286 id_declarator = id_declarator->declarator;
15288 if (id_declarator && id_declarator->kind == cdk_id)
15289 error_at (declarator_token_start->location,
15291 ? "template parameter pack %qD"
15292 " cannot have a default argument"
15293 : "parameter pack %qD cannot have a default argument",
15294 id_declarator->u.id.unqualified_name);
15296 error_at (declarator_token_start->location,
15298 ? "template parameter pack cannot have a default argument"
15299 : "parameter pack cannot have a default argument");
15301 default_argument = NULL_TREE;
15305 default_argument = NULL_TREE;
15307 return make_parameter_declarator (&decl_specifiers,
15312 /* Parse a default argument and return it.
15314 TEMPLATE_PARM_P is true if this is a default argument for a
15315 non-type template parameter. */
15317 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15319 tree default_argument = NULL_TREE;
15320 bool saved_greater_than_is_operator_p;
15321 bool saved_local_variables_forbidden_p;
15323 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15325 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15326 parser->greater_than_is_operator_p = !template_parm_p;
15327 /* Local variable names (and the `this' keyword) may not
15328 appear in a default argument. */
15329 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15330 parser->local_variables_forbidden_p = true;
15331 /* Parse the assignment-expression. */
15332 if (template_parm_p)
15333 push_deferring_access_checks (dk_no_deferred);
15335 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15336 if (template_parm_p)
15337 pop_deferring_access_checks ();
15338 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15339 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15341 return default_argument;
15344 /* Parse a function-body.
15347 compound_statement */
15350 cp_parser_function_body (cp_parser *parser)
15352 cp_parser_compound_statement (parser, NULL, false);
15355 /* Parse a ctor-initializer-opt followed by a function-body. Return
15356 true if a ctor-initializer was present. */
15359 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15362 bool ctor_initializer_p;
15364 /* Begin the function body. */
15365 body = begin_function_body ();
15366 /* Parse the optional ctor-initializer. */
15367 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15368 /* Parse the function-body. */
15369 cp_parser_function_body (parser);
15370 /* Finish the function body. */
15371 finish_function_body (body);
15373 return ctor_initializer_p;
15376 /* Parse an initializer.
15379 = initializer-clause
15380 ( expression-list )
15382 Returns an expression representing the initializer. If no
15383 initializer is present, NULL_TREE is returned.
15385 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15386 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15387 set to TRUE if there is no initializer present. If there is an
15388 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15389 is set to true; otherwise it is set to false. */
15392 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15393 bool* non_constant_p)
15398 /* Peek at the next token. */
15399 token = cp_lexer_peek_token (parser->lexer);
15401 /* Let our caller know whether or not this initializer was
15403 *is_direct_init = (token->type != CPP_EQ);
15404 /* Assume that the initializer is constant. */
15405 *non_constant_p = false;
15407 if (token->type == CPP_EQ)
15409 /* Consume the `='. */
15410 cp_lexer_consume_token (parser->lexer);
15411 /* Parse the initializer-clause. */
15412 init = cp_parser_initializer_clause (parser, non_constant_p);
15414 else if (token->type == CPP_OPEN_PAREN)
15417 vec = cp_parser_parenthesized_expression_list (parser, false,
15419 /*allow_expansion_p=*/true,
15422 return error_mark_node;
15423 init = build_tree_list_vec (vec);
15424 release_tree_vector (vec);
15426 else if (token->type == CPP_OPEN_BRACE)
15428 maybe_warn_cpp0x ("extended initializer lists");
15429 init = cp_parser_braced_list (parser, non_constant_p);
15430 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15434 /* Anything else is an error. */
15435 cp_parser_error (parser, "expected initializer");
15436 init = error_mark_node;
15442 /* Parse an initializer-clause.
15444 initializer-clause:
15445 assignment-expression
15448 Returns an expression representing the initializer.
15450 If the `assignment-expression' production is used the value
15451 returned is simply a representation for the expression.
15453 Otherwise, calls cp_parser_braced_list. */
15456 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15460 /* Assume the expression is constant. */
15461 *non_constant_p = false;
15463 /* If it is not a `{', then we are looking at an
15464 assignment-expression. */
15465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15468 = cp_parser_constant_expression (parser,
15469 /*allow_non_constant_p=*/true,
15471 if (!*non_constant_p)
15472 initializer = fold_non_dependent_expr (initializer);
15475 initializer = cp_parser_braced_list (parser, non_constant_p);
15477 return initializer;
15480 /* Parse a brace-enclosed initializer list.
15483 { initializer-list , [opt] }
15486 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15487 the elements of the initializer-list (or NULL, if the last
15488 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15489 NULL_TREE. There is no way to detect whether or not the optional
15490 trailing `,' was provided. NON_CONSTANT_P is as for
15491 cp_parser_initializer. */
15494 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15498 /* Consume the `{' token. */
15499 cp_lexer_consume_token (parser->lexer);
15500 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15501 initializer = make_node (CONSTRUCTOR);
15502 /* If it's not a `}', then there is a non-trivial initializer. */
15503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15505 /* Parse the initializer list. */
15506 CONSTRUCTOR_ELTS (initializer)
15507 = cp_parser_initializer_list (parser, non_constant_p);
15508 /* A trailing `,' token is allowed. */
15509 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15510 cp_lexer_consume_token (parser->lexer);
15512 /* Now, there should be a trailing `}'. */
15513 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15514 TREE_TYPE (initializer) = init_list_type_node;
15515 return initializer;
15518 /* Parse an initializer-list.
15521 initializer-clause ... [opt]
15522 initializer-list , initializer-clause ... [opt]
15527 identifier : initializer-clause
15528 initializer-list, identifier : initializer-clause
15530 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15531 for the initializer. If the INDEX of the elt is non-NULL, it is the
15532 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15533 as for cp_parser_initializer. */
15535 static VEC(constructor_elt,gc) *
15536 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15538 VEC(constructor_elt,gc) *v = NULL;
15540 /* Assume all of the expressions are constant. */
15541 *non_constant_p = false;
15543 /* Parse the rest of the list. */
15549 bool clause_non_constant_p;
15551 /* If the next token is an identifier and the following one is a
15552 colon, we are looking at the GNU designated-initializer
15554 if (cp_parser_allow_gnu_extensions_p (parser)
15555 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15558 /* Warn the user that they are using an extension. */
15559 pedwarn (input_location, OPT_pedantic,
15560 "ISO C++ does not allow designated initializers");
15561 /* Consume the identifier. */
15562 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15563 /* Consume the `:'. */
15564 cp_lexer_consume_token (parser->lexer);
15567 identifier = NULL_TREE;
15569 /* Parse the initializer. */
15570 initializer = cp_parser_initializer_clause (parser,
15571 &clause_non_constant_p);
15572 /* If any clause is non-constant, so is the entire initializer. */
15573 if (clause_non_constant_p)
15574 *non_constant_p = true;
15576 /* If we have an ellipsis, this is an initializer pack
15578 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15580 /* Consume the `...'. */
15581 cp_lexer_consume_token (parser->lexer);
15583 /* Turn the initializer into an initializer expansion. */
15584 initializer = make_pack_expansion (initializer);
15587 /* Add it to the vector. */
15588 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15590 /* If the next token is not a comma, we have reached the end of
15592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15595 /* Peek at the next token. */
15596 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15597 /* If the next token is a `}', then we're still done. An
15598 initializer-clause can have a trailing `,' after the
15599 initializer-list and before the closing `}'. */
15600 if (token->type == CPP_CLOSE_BRACE)
15603 /* Consume the `,' token. */
15604 cp_lexer_consume_token (parser->lexer);
15610 /* Classes [gram.class] */
15612 /* Parse a class-name.
15618 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15619 to indicate that names looked up in dependent types should be
15620 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15621 keyword has been used to indicate that the name that appears next
15622 is a template. TAG_TYPE indicates the explicit tag given before
15623 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15624 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15625 is the class being defined in a class-head.
15627 Returns the TYPE_DECL representing the class. */
15630 cp_parser_class_name (cp_parser *parser,
15631 bool typename_keyword_p,
15632 bool template_keyword_p,
15633 enum tag_types tag_type,
15634 bool check_dependency_p,
15636 bool is_declaration)
15642 tree identifier = NULL_TREE;
15644 /* All class-names start with an identifier. */
15645 token = cp_lexer_peek_token (parser->lexer);
15646 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15648 cp_parser_error (parser, "expected class-name");
15649 return error_mark_node;
15652 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15653 to a template-id, so we save it here. */
15654 scope = parser->scope;
15655 if (scope == error_mark_node)
15656 return error_mark_node;
15658 /* Any name names a type if we're following the `typename' keyword
15659 in a qualified name where the enclosing scope is type-dependent. */
15660 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15661 && dependent_type_p (scope));
15662 /* Handle the common case (an identifier, but not a template-id)
15664 if (token->type == CPP_NAME
15665 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15667 cp_token *identifier_token;
15670 /* Look for the identifier. */
15671 identifier_token = cp_lexer_peek_token (parser->lexer);
15672 ambiguous_p = identifier_token->ambiguous_p;
15673 identifier = cp_parser_identifier (parser);
15674 /* If the next token isn't an identifier, we are certainly not
15675 looking at a class-name. */
15676 if (identifier == error_mark_node)
15677 decl = error_mark_node;
15678 /* If we know this is a type-name, there's no need to look it
15680 else if (typename_p)
15684 tree ambiguous_decls;
15685 /* If we already know that this lookup is ambiguous, then
15686 we've already issued an error message; there's no reason
15690 cp_parser_simulate_error (parser);
15691 return error_mark_node;
15693 /* If the next token is a `::', then the name must be a type
15696 [basic.lookup.qual]
15698 During the lookup for a name preceding the :: scope
15699 resolution operator, object, function, and enumerator
15700 names are ignored. */
15701 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15702 tag_type = typename_type;
15703 /* Look up the name. */
15704 decl = cp_parser_lookup_name (parser, identifier,
15706 /*is_template=*/false,
15707 /*is_namespace=*/false,
15708 check_dependency_p,
15710 identifier_token->location);
15711 if (ambiguous_decls)
15713 error_at (identifier_token->location,
15714 "reference to %qD is ambiguous", identifier);
15715 print_candidates (ambiguous_decls);
15716 if (cp_parser_parsing_tentatively (parser))
15718 identifier_token->ambiguous_p = true;
15719 cp_parser_simulate_error (parser);
15721 return error_mark_node;
15727 /* Try a template-id. */
15728 decl = cp_parser_template_id (parser, template_keyword_p,
15729 check_dependency_p,
15731 if (decl == error_mark_node)
15732 return error_mark_node;
15735 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15737 /* If this is a typename, create a TYPENAME_TYPE. */
15738 if (typename_p && decl != error_mark_node)
15740 decl = make_typename_type (scope, decl, typename_type,
15741 /*complain=*/tf_error);
15742 if (decl != error_mark_node)
15743 decl = TYPE_NAME (decl);
15746 /* Check to see that it is really the name of a class. */
15747 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15748 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15749 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15750 /* Situations like this:
15752 template <typename T> struct A {
15753 typename T::template X<int>::I i;
15756 are problematic. Is `T::template X<int>' a class-name? The
15757 standard does not seem to be definitive, but there is no other
15758 valid interpretation of the following `::'. Therefore, those
15759 names are considered class-names. */
15761 decl = make_typename_type (scope, decl, tag_type, tf_error);
15762 if (decl != error_mark_node)
15763 decl = TYPE_NAME (decl);
15765 else if (TREE_CODE (decl) != TYPE_DECL
15766 || TREE_TYPE (decl) == error_mark_node
15767 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15768 decl = error_mark_node;
15770 if (decl == error_mark_node)
15771 cp_parser_error (parser, "expected class-name");
15772 else if (identifier && !parser->scope)
15773 maybe_note_name_used_in_class (identifier, decl);
15778 /* Parse a class-specifier.
15781 class-head { member-specification [opt] }
15783 Returns the TREE_TYPE representing the class. */
15786 cp_parser_class_specifier (cp_parser* parser)
15789 tree attributes = NULL_TREE;
15790 bool nested_name_specifier_p;
15791 unsigned saved_num_template_parameter_lists;
15792 bool saved_in_function_body;
15793 bool saved_in_unbraced_linkage_specification_p;
15794 tree old_scope = NULL_TREE;
15795 tree scope = NULL_TREE;
15798 push_deferring_access_checks (dk_no_deferred);
15800 /* Parse the class-head. */
15801 type = cp_parser_class_head (parser,
15802 &nested_name_specifier_p,
15805 /* If the class-head was a semantic disaster, skip the entire body
15809 cp_parser_skip_to_end_of_block_or_statement (parser);
15810 pop_deferring_access_checks ();
15811 return error_mark_node;
15814 /* Look for the `{'. */
15815 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15817 pop_deferring_access_checks ();
15818 return error_mark_node;
15821 /* Process the base classes. If they're invalid, skip the
15822 entire class body. */
15823 if (!xref_basetypes (type, bases))
15825 /* Consuming the closing brace yields better error messages
15827 if (cp_parser_skip_to_closing_brace (parser))
15828 cp_lexer_consume_token (parser->lexer);
15829 pop_deferring_access_checks ();
15830 return error_mark_node;
15833 /* Issue an error message if type-definitions are forbidden here. */
15834 cp_parser_check_type_definition (parser);
15835 /* Remember that we are defining one more class. */
15836 ++parser->num_classes_being_defined;
15837 /* Inside the class, surrounding template-parameter-lists do not
15839 saved_num_template_parameter_lists
15840 = parser->num_template_parameter_lists;
15841 parser->num_template_parameter_lists = 0;
15842 /* We are not in a function body. */
15843 saved_in_function_body = parser->in_function_body;
15844 parser->in_function_body = false;
15845 /* We are not immediately inside an extern "lang" block. */
15846 saved_in_unbraced_linkage_specification_p
15847 = parser->in_unbraced_linkage_specification_p;
15848 parser->in_unbraced_linkage_specification_p = false;
15850 /* Start the class. */
15851 if (nested_name_specifier_p)
15853 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15854 old_scope = push_inner_scope (scope);
15856 type = begin_class_definition (type, attributes);
15858 if (type == error_mark_node)
15859 /* If the type is erroneous, skip the entire body of the class. */
15860 cp_parser_skip_to_closing_brace (parser);
15862 /* Parse the member-specification. */
15863 cp_parser_member_specification_opt (parser);
15865 /* Look for the trailing `}'. */
15866 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15867 /* Look for trailing attributes to apply to this class. */
15868 if (cp_parser_allow_gnu_extensions_p (parser))
15869 attributes = cp_parser_attributes_opt (parser);
15870 if (type != error_mark_node)
15871 type = finish_struct (type, attributes);
15872 if (nested_name_specifier_p)
15873 pop_inner_scope (old_scope, scope);
15874 /* If this class is not itself within the scope of another class,
15875 then we need to parse the bodies of all of the queued function
15876 definitions. Note that the queued functions defined in a class
15877 are not always processed immediately following the
15878 class-specifier for that class. Consider:
15881 struct B { void f() { sizeof (A); } };
15884 If `f' were processed before the processing of `A' were
15885 completed, there would be no way to compute the size of `A'.
15886 Note that the nesting we are interested in here is lexical --
15887 not the semantic nesting given by TYPE_CONTEXT. In particular,
15890 struct A { struct B; };
15891 struct A::B { void f() { } };
15893 there is no need to delay the parsing of `A::B::f'. */
15894 if (--parser->num_classes_being_defined == 0)
15898 tree class_type = NULL_TREE;
15899 tree pushed_scope = NULL_TREE;
15901 /* In a first pass, parse default arguments to the functions.
15902 Then, in a second pass, parse the bodies of the functions.
15903 This two-phased approach handles cases like:
15911 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15912 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15913 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15914 TREE_PURPOSE (parser->unparsed_functions_queues)
15915 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15917 fn = TREE_VALUE (queue_entry);
15918 /* If there are default arguments that have not yet been processed,
15919 take care of them now. */
15920 if (class_type != TREE_PURPOSE (queue_entry))
15923 pop_scope (pushed_scope);
15924 class_type = TREE_PURPOSE (queue_entry);
15925 pushed_scope = push_scope (class_type);
15927 /* Make sure that any template parameters are in scope. */
15928 maybe_begin_member_template_processing (fn);
15929 /* Parse the default argument expressions. */
15930 cp_parser_late_parsing_default_args (parser, fn);
15931 /* Remove any template parameters from the symbol table. */
15932 maybe_end_member_template_processing ();
15935 pop_scope (pushed_scope);
15936 /* Now parse the body of the functions. */
15937 for (TREE_VALUE (parser->unparsed_functions_queues)
15938 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15939 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15940 TREE_VALUE (parser->unparsed_functions_queues)
15941 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15943 /* Figure out which function we need to process. */
15944 fn = TREE_VALUE (queue_entry);
15945 /* Parse the function. */
15946 cp_parser_late_parsing_for_member (parser, fn);
15950 /* Put back any saved access checks. */
15951 pop_deferring_access_checks ();
15953 /* Restore saved state. */
15954 parser->in_function_body = saved_in_function_body;
15955 parser->num_template_parameter_lists
15956 = saved_num_template_parameter_lists;
15957 parser->in_unbraced_linkage_specification_p
15958 = saved_in_unbraced_linkage_specification_p;
15963 /* Parse a class-head.
15966 class-key identifier [opt] base-clause [opt]
15967 class-key nested-name-specifier identifier base-clause [opt]
15968 class-key nested-name-specifier [opt] template-id
15972 class-key attributes identifier [opt] base-clause [opt]
15973 class-key attributes nested-name-specifier identifier base-clause [opt]
15974 class-key attributes nested-name-specifier [opt] template-id
15977 Upon return BASES is initialized to the list of base classes (or
15978 NULL, if there are none) in the same form returned by
15979 cp_parser_base_clause.
15981 Returns the TYPE of the indicated class. Sets
15982 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15983 involving a nested-name-specifier was used, and FALSE otherwise.
15985 Returns error_mark_node if this is not a class-head.
15987 Returns NULL_TREE if the class-head is syntactically valid, but
15988 semantically invalid in a way that means we should skip the entire
15989 body of the class. */
15992 cp_parser_class_head (cp_parser* parser,
15993 bool* nested_name_specifier_p,
15994 tree *attributes_p,
15997 tree nested_name_specifier;
15998 enum tag_types class_key;
15999 tree id = NULL_TREE;
16000 tree type = NULL_TREE;
16002 bool template_id_p = false;
16003 bool qualified_p = false;
16004 bool invalid_nested_name_p = false;
16005 bool invalid_explicit_specialization_p = false;
16006 tree pushed_scope = NULL_TREE;
16007 unsigned num_templates;
16008 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16009 /* Assume no nested-name-specifier will be present. */
16010 *nested_name_specifier_p = false;
16011 /* Assume no template parameter lists will be used in defining the
16015 *bases = NULL_TREE;
16017 /* Look for the class-key. */
16018 class_key = cp_parser_class_key (parser);
16019 if (class_key == none_type)
16020 return error_mark_node;
16022 /* Parse the attributes. */
16023 attributes = cp_parser_attributes_opt (parser);
16025 /* If the next token is `::', that is invalid -- but sometimes
16026 people do try to write:
16030 Handle this gracefully by accepting the extra qualifier, and then
16031 issuing an error about it later if this really is a
16032 class-head. If it turns out just to be an elaborated type
16033 specifier, remain silent. */
16034 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16035 qualified_p = true;
16037 push_deferring_access_checks (dk_no_check);
16039 /* Determine the name of the class. Begin by looking for an
16040 optional nested-name-specifier. */
16041 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16042 nested_name_specifier
16043 = cp_parser_nested_name_specifier_opt (parser,
16044 /*typename_keyword_p=*/false,
16045 /*check_dependency_p=*/false,
16047 /*is_declaration=*/false);
16048 /* If there was a nested-name-specifier, then there *must* be an
16050 if (nested_name_specifier)
16052 type_start_token = cp_lexer_peek_token (parser->lexer);
16053 /* Although the grammar says `identifier', it really means
16054 `class-name' or `template-name'. You are only allowed to
16055 define a class that has already been declared with this
16058 The proposed resolution for Core Issue 180 says that wherever
16059 you see `class T::X' you should treat `X' as a type-name.
16061 It is OK to define an inaccessible class; for example:
16063 class A { class B; };
16066 We do not know if we will see a class-name, or a
16067 template-name. We look for a class-name first, in case the
16068 class-name is a template-id; if we looked for the
16069 template-name first we would stop after the template-name. */
16070 cp_parser_parse_tentatively (parser);
16071 type = cp_parser_class_name (parser,
16072 /*typename_keyword_p=*/false,
16073 /*template_keyword_p=*/false,
16075 /*check_dependency_p=*/false,
16076 /*class_head_p=*/true,
16077 /*is_declaration=*/false);
16078 /* If that didn't work, ignore the nested-name-specifier. */
16079 if (!cp_parser_parse_definitely (parser))
16081 invalid_nested_name_p = true;
16082 type_start_token = cp_lexer_peek_token (parser->lexer);
16083 id = cp_parser_identifier (parser);
16084 if (id == error_mark_node)
16087 /* If we could not find a corresponding TYPE, treat this
16088 declaration like an unqualified declaration. */
16089 if (type == error_mark_node)
16090 nested_name_specifier = NULL_TREE;
16091 /* Otherwise, count the number of templates used in TYPE and its
16092 containing scopes. */
16097 for (scope = TREE_TYPE (type);
16098 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16099 scope = (TYPE_P (scope)
16100 ? TYPE_CONTEXT (scope)
16101 : DECL_CONTEXT (scope)))
16103 && CLASS_TYPE_P (scope)
16104 && CLASSTYPE_TEMPLATE_INFO (scope)
16105 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16106 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16110 /* Otherwise, the identifier is optional. */
16113 /* We don't know whether what comes next is a template-id,
16114 an identifier, or nothing at all. */
16115 cp_parser_parse_tentatively (parser);
16116 /* Check for a template-id. */
16117 type_start_token = cp_lexer_peek_token (parser->lexer);
16118 id = cp_parser_template_id (parser,
16119 /*template_keyword_p=*/false,
16120 /*check_dependency_p=*/true,
16121 /*is_declaration=*/true);
16122 /* If that didn't work, it could still be an identifier. */
16123 if (!cp_parser_parse_definitely (parser))
16125 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16127 type_start_token = cp_lexer_peek_token (parser->lexer);
16128 id = cp_parser_identifier (parser);
16135 template_id_p = true;
16140 pop_deferring_access_checks ();
16143 cp_parser_check_for_invalid_template_id (parser, id,
16144 type_start_token->location);
16146 /* If it's not a `:' or a `{' then we can't really be looking at a
16147 class-head, since a class-head only appears as part of a
16148 class-specifier. We have to detect this situation before calling
16149 xref_tag, since that has irreversible side-effects. */
16150 if (!cp_parser_next_token_starts_class_definition_p (parser))
16152 cp_parser_error (parser, "expected %<{%> or %<:%>");
16153 return error_mark_node;
16156 /* At this point, we're going ahead with the class-specifier, even
16157 if some other problem occurs. */
16158 cp_parser_commit_to_tentative_parse (parser);
16159 /* Issue the error about the overly-qualified name now. */
16162 cp_parser_error (parser,
16163 "global qualification of class name is invalid");
16164 return error_mark_node;
16166 else if (invalid_nested_name_p)
16168 cp_parser_error (parser,
16169 "qualified name does not name a class");
16170 return error_mark_node;
16172 else if (nested_name_specifier)
16176 /* Reject typedef-names in class heads. */
16177 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16179 error_at (type_start_token->location,
16180 "invalid class name in declaration of %qD",
16186 /* Figure out in what scope the declaration is being placed. */
16187 scope = current_scope ();
16188 /* If that scope does not contain the scope in which the
16189 class was originally declared, the program is invalid. */
16190 if (scope && !is_ancestor (scope, nested_name_specifier))
16192 if (at_namespace_scope_p ())
16193 error_at (type_start_token->location,
16194 "declaration of %qD in namespace %qD which does not "
16196 type, scope, nested_name_specifier);
16198 error_at (type_start_token->location,
16199 "declaration of %qD in %qD which does not enclose %qD",
16200 type, scope, nested_name_specifier);
16206 A declarator-id shall not be qualified except for the
16207 definition of a ... nested class outside of its class
16208 ... [or] the definition or explicit instantiation of a
16209 class member of a namespace outside of its namespace. */
16210 if (scope == nested_name_specifier)
16212 permerror (nested_name_specifier_token_start->location,
16213 "extra qualification not allowed");
16214 nested_name_specifier = NULL_TREE;
16218 /* An explicit-specialization must be preceded by "template <>". If
16219 it is not, try to recover gracefully. */
16220 if (at_namespace_scope_p ()
16221 && parser->num_template_parameter_lists == 0
16224 error_at (type_start_token->location,
16225 "an explicit specialization must be preceded by %<template <>%>");
16226 invalid_explicit_specialization_p = true;
16227 /* Take the same action that would have been taken by
16228 cp_parser_explicit_specialization. */
16229 ++parser->num_template_parameter_lists;
16230 begin_specialization ();
16232 /* There must be no "return" statements between this point and the
16233 end of this function; set "type "to the correct return value and
16234 use "goto done;" to return. */
16235 /* Make sure that the right number of template parameters were
16237 if (!cp_parser_check_template_parameters (parser, num_templates,
16238 type_start_token->location,
16239 /*declarator=*/NULL))
16241 /* If something went wrong, there is no point in even trying to
16242 process the class-definition. */
16247 /* Look up the type. */
16250 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16251 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16252 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16254 error_at (type_start_token->location,
16255 "function template %qD redeclared as a class template", id);
16256 type = error_mark_node;
16260 type = TREE_TYPE (id);
16261 type = maybe_process_partial_specialization (type);
16263 if (nested_name_specifier)
16264 pushed_scope = push_scope (nested_name_specifier);
16266 else if (nested_name_specifier)
16272 template <typename T> struct S { struct T };
16273 template <typename T> struct S<T>::T { };
16275 we will get a TYPENAME_TYPE when processing the definition of
16276 `S::T'. We need to resolve it to the actual type before we
16277 try to define it. */
16278 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16280 class_type = resolve_typename_type (TREE_TYPE (type),
16281 /*only_current_p=*/false);
16282 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16283 type = TYPE_NAME (class_type);
16286 cp_parser_error (parser, "could not resolve typename type");
16287 type = error_mark_node;
16291 if (maybe_process_partial_specialization (TREE_TYPE (type))
16292 == error_mark_node)
16298 class_type = current_class_type;
16299 /* Enter the scope indicated by the nested-name-specifier. */
16300 pushed_scope = push_scope (nested_name_specifier);
16301 /* Get the canonical version of this type. */
16302 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16303 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16304 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16306 type = push_template_decl (type);
16307 if (type == error_mark_node)
16314 type = TREE_TYPE (type);
16315 *nested_name_specifier_p = true;
16317 else /* The name is not a nested name. */
16319 /* If the class was unnamed, create a dummy name. */
16321 id = make_anon_name ();
16322 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16323 parser->num_template_parameter_lists);
16326 /* Indicate whether this class was declared as a `class' or as a
16328 if (TREE_CODE (type) == RECORD_TYPE)
16329 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16330 cp_parser_check_class_key (class_key, type);
16332 /* If this type was already complete, and we see another definition,
16333 that's an error. */
16334 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16336 error_at (type_start_token->location, "redefinition of %q#T",
16338 error_at (type_start_token->location, "previous definition of %q+#T",
16343 else if (type == error_mark_node)
16346 /* We will have entered the scope containing the class; the names of
16347 base classes should be looked up in that context. For example:
16349 struct A { struct B {}; struct C; };
16350 struct A::C : B {};
16354 /* Get the list of base-classes, if there is one. */
16355 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16356 *bases = cp_parser_base_clause (parser);
16359 /* Leave the scope given by the nested-name-specifier. We will
16360 enter the class scope itself while processing the members. */
16362 pop_scope (pushed_scope);
16364 if (invalid_explicit_specialization_p)
16366 end_specialization ();
16367 --parser->num_template_parameter_lists;
16369 *attributes_p = attributes;
16373 /* Parse a class-key.
16380 Returns the kind of class-key specified, or none_type to indicate
16383 static enum tag_types
16384 cp_parser_class_key (cp_parser* parser)
16387 enum tag_types tag_type;
16389 /* Look for the class-key. */
16390 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16394 /* Check to see if the TOKEN is a class-key. */
16395 tag_type = cp_parser_token_is_class_key (token);
16397 cp_parser_error (parser, "expected class-key");
16401 /* Parse an (optional) member-specification.
16403 member-specification:
16404 member-declaration member-specification [opt]
16405 access-specifier : member-specification [opt] */
16408 cp_parser_member_specification_opt (cp_parser* parser)
16415 /* Peek at the next token. */
16416 token = cp_lexer_peek_token (parser->lexer);
16417 /* If it's a `}', or EOF then we've seen all the members. */
16418 if (token->type == CPP_CLOSE_BRACE
16419 || token->type == CPP_EOF
16420 || token->type == CPP_PRAGMA_EOL)
16423 /* See if this token is a keyword. */
16424 keyword = token->keyword;
16428 case RID_PROTECTED:
16430 /* Consume the access-specifier. */
16431 cp_lexer_consume_token (parser->lexer);
16432 /* Remember which access-specifier is active. */
16433 current_access_specifier = token->u.value;
16434 /* Look for the `:'. */
16435 cp_parser_require (parser, CPP_COLON, "%<:%>");
16439 /* Accept #pragmas at class scope. */
16440 if (token->type == CPP_PRAGMA)
16442 cp_parser_pragma (parser, pragma_external);
16446 /* Otherwise, the next construction must be a
16447 member-declaration. */
16448 cp_parser_member_declaration (parser);
16453 /* Parse a member-declaration.
16455 member-declaration:
16456 decl-specifier-seq [opt] member-declarator-list [opt] ;
16457 function-definition ; [opt]
16458 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16460 template-declaration
16462 member-declarator-list:
16464 member-declarator-list , member-declarator
16467 declarator pure-specifier [opt]
16468 declarator constant-initializer [opt]
16469 identifier [opt] : constant-expression
16473 member-declaration:
16474 __extension__ member-declaration
16477 declarator attributes [opt] pure-specifier [opt]
16478 declarator attributes [opt] constant-initializer [opt]
16479 identifier [opt] attributes [opt] : constant-expression
16483 member-declaration:
16484 static_assert-declaration */
16487 cp_parser_member_declaration (cp_parser* parser)
16489 cp_decl_specifier_seq decl_specifiers;
16490 tree prefix_attributes;
16492 int declares_class_or_enum;
16494 cp_token *token = NULL;
16495 cp_token *decl_spec_token_start = NULL;
16496 cp_token *initializer_token_start = NULL;
16497 int saved_pedantic;
16499 /* Check for the `__extension__' keyword. */
16500 if (cp_parser_extension_opt (parser, &saved_pedantic))
16503 cp_parser_member_declaration (parser);
16504 /* Restore the old value of the PEDANTIC flag. */
16505 pedantic = saved_pedantic;
16510 /* Check for a template-declaration. */
16511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16513 /* An explicit specialization here is an error condition, and we
16514 expect the specialization handler to detect and report this. */
16515 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16516 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16517 cp_parser_explicit_specialization (parser);
16519 cp_parser_template_declaration (parser, /*member_p=*/true);
16524 /* Check for a using-declaration. */
16525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16527 /* Parse the using-declaration. */
16528 cp_parser_using_declaration (parser,
16529 /*access_declaration_p=*/false);
16533 /* Check for @defs. */
16534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16537 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16538 ivar = ivar_chains;
16542 ivar = TREE_CHAIN (member);
16543 TREE_CHAIN (member) = NULL_TREE;
16544 finish_member_declaration (member);
16549 /* If the next token is `static_assert' we have a static assertion. */
16550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16552 cp_parser_static_assert (parser, /*member_p=*/true);
16556 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16559 /* Parse the decl-specifier-seq. */
16560 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16561 cp_parser_decl_specifier_seq (parser,
16562 CP_PARSER_FLAGS_OPTIONAL,
16564 &declares_class_or_enum);
16565 prefix_attributes = decl_specifiers.attributes;
16566 decl_specifiers.attributes = NULL_TREE;
16567 /* Check for an invalid type-name. */
16568 if (!decl_specifiers.any_type_specifiers_p
16569 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16571 /* If there is no declarator, then the decl-specifier-seq should
16573 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16575 /* If there was no decl-specifier-seq, and the next token is a
16576 `;', then we have something like:
16582 Each member-declaration shall declare at least one member
16583 name of the class. */
16584 if (!decl_specifiers.any_specifiers_p)
16586 cp_token *token = cp_lexer_peek_token (parser->lexer);
16587 if (!in_system_header_at (token->location))
16588 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16594 /* See if this declaration is a friend. */
16595 friend_p = cp_parser_friend_p (&decl_specifiers);
16596 /* If there were decl-specifiers, check to see if there was
16597 a class-declaration. */
16598 type = check_tag_decl (&decl_specifiers);
16599 /* Nested classes have already been added to the class, but
16600 a `friend' needs to be explicitly registered. */
16603 /* If the `friend' keyword was present, the friend must
16604 be introduced with a class-key. */
16605 if (!declares_class_or_enum)
16606 error_at (decl_spec_token_start->location,
16607 "a class-key must be used when declaring a friend");
16610 template <typename T> struct A {
16611 friend struct A<T>::B;
16614 A<T>::B will be represented by a TYPENAME_TYPE, and
16615 therefore not recognized by check_tag_decl. */
16617 && decl_specifiers.type
16618 && TYPE_P (decl_specifiers.type))
16619 type = decl_specifiers.type;
16620 if (!type || !TYPE_P (type))
16621 error_at (decl_spec_token_start->location,
16622 "friend declaration does not name a class or "
16625 make_friend_class (current_class_type, type,
16626 /*complain=*/true);
16628 /* If there is no TYPE, an error message will already have
16630 else if (!type || type == error_mark_node)
16632 /* An anonymous aggregate has to be handled specially; such
16633 a declaration really declares a data member (with a
16634 particular type), as opposed to a nested class. */
16635 else if (ANON_AGGR_TYPE_P (type))
16637 /* Remove constructors and such from TYPE, now that we
16638 know it is an anonymous aggregate. */
16639 fixup_anonymous_aggr (type);
16640 /* And make the corresponding data member. */
16641 decl = build_decl (decl_spec_token_start->location,
16642 FIELD_DECL, NULL_TREE, type);
16643 /* Add it to the class. */
16644 finish_member_declaration (decl);
16647 cp_parser_check_access_in_redeclaration
16649 decl_spec_token_start->location);
16654 /* See if these declarations will be friends. */
16655 friend_p = cp_parser_friend_p (&decl_specifiers);
16657 /* Keep going until we hit the `;' at the end of the
16659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16661 tree attributes = NULL_TREE;
16662 tree first_attribute;
16664 /* Peek at the next token. */
16665 token = cp_lexer_peek_token (parser->lexer);
16667 /* Check for a bitfield declaration. */
16668 if (token->type == CPP_COLON
16669 || (token->type == CPP_NAME
16670 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16676 /* Get the name of the bitfield. Note that we cannot just
16677 check TOKEN here because it may have been invalidated by
16678 the call to cp_lexer_peek_nth_token above. */
16679 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16680 identifier = cp_parser_identifier (parser);
16682 identifier = NULL_TREE;
16684 /* Consume the `:' token. */
16685 cp_lexer_consume_token (parser->lexer);
16686 /* Get the width of the bitfield. */
16688 = cp_parser_constant_expression (parser,
16689 /*allow_non_constant=*/false,
16692 /* Look for attributes that apply to the bitfield. */
16693 attributes = cp_parser_attributes_opt (parser);
16694 /* Remember which attributes are prefix attributes and
16696 first_attribute = attributes;
16697 /* Combine the attributes. */
16698 attributes = chainon (prefix_attributes, attributes);
16700 /* Create the bitfield declaration. */
16701 decl = grokbitfield (identifier
16702 ? make_id_declarator (NULL_TREE,
16712 cp_declarator *declarator;
16714 tree asm_specification;
16715 int ctor_dtor_or_conv_p;
16717 /* Parse the declarator. */
16719 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16720 &ctor_dtor_or_conv_p,
16721 /*parenthesized_p=*/NULL,
16722 /*member_p=*/true);
16724 /* If something went wrong parsing the declarator, make sure
16725 that we at least consume some tokens. */
16726 if (declarator == cp_error_declarator)
16728 /* Skip to the end of the statement. */
16729 cp_parser_skip_to_end_of_statement (parser);
16730 /* If the next token is not a semicolon, that is
16731 probably because we just skipped over the body of
16732 a function. So, we consume a semicolon if
16733 present, but do not issue an error message if it
16735 if (cp_lexer_next_token_is (parser->lexer,
16737 cp_lexer_consume_token (parser->lexer);
16741 if (declares_class_or_enum & 2)
16742 cp_parser_check_for_definition_in_return_type
16743 (declarator, decl_specifiers.type,
16744 decl_specifiers.type_location);
16746 /* Look for an asm-specification. */
16747 asm_specification = cp_parser_asm_specification_opt (parser);
16748 /* Look for attributes that apply to the declaration. */
16749 attributes = cp_parser_attributes_opt (parser);
16750 /* Remember which attributes are prefix attributes and
16752 first_attribute = attributes;
16753 /* Combine the attributes. */
16754 attributes = chainon (prefix_attributes, attributes);
16756 /* If it's an `=', then we have a constant-initializer or a
16757 pure-specifier. It is not correct to parse the
16758 initializer before registering the member declaration
16759 since the member declaration should be in scope while
16760 its initializer is processed. However, the rest of the
16761 front end does not yet provide an interface that allows
16762 us to handle this correctly. */
16763 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16767 A pure-specifier shall be used only in the declaration of
16768 a virtual function.
16770 A member-declarator can contain a constant-initializer
16771 only if it declares a static member of integral or
16774 Therefore, if the DECLARATOR is for a function, we look
16775 for a pure-specifier; otherwise, we look for a
16776 constant-initializer. When we call `grokfield', it will
16777 perform more stringent semantics checks. */
16778 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16779 if (function_declarator_p (declarator))
16780 initializer = cp_parser_pure_specifier (parser);
16782 /* Parse the initializer. */
16783 initializer = cp_parser_constant_initializer (parser);
16785 /* Otherwise, there is no initializer. */
16787 initializer = NULL_TREE;
16789 /* See if we are probably looking at a function
16790 definition. We are certainly not looking at a
16791 member-declarator. Calling `grokfield' has
16792 side-effects, so we must not do it unless we are sure
16793 that we are looking at a member-declarator. */
16794 if (cp_parser_token_starts_function_definition_p
16795 (cp_lexer_peek_token (parser->lexer)))
16797 /* The grammar does not allow a pure-specifier to be
16798 used when a member function is defined. (It is
16799 possible that this fact is an oversight in the
16800 standard, since a pure function may be defined
16801 outside of the class-specifier. */
16803 error_at (initializer_token_start->location,
16804 "pure-specifier on function-definition");
16805 decl = cp_parser_save_member_function_body (parser,
16809 /* If the member was not a friend, declare it here. */
16811 finish_member_declaration (decl);
16812 /* Peek at the next token. */
16813 token = cp_lexer_peek_token (parser->lexer);
16814 /* If the next token is a semicolon, consume it. */
16815 if (token->type == CPP_SEMICOLON)
16816 cp_lexer_consume_token (parser->lexer);
16820 if (declarator->kind == cdk_function)
16821 declarator->id_loc = token->location;
16822 /* Create the declaration. */
16823 decl = grokfield (declarator, &decl_specifiers,
16824 initializer, /*init_const_expr_p=*/true,
16829 /* Reset PREFIX_ATTRIBUTES. */
16830 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16831 attributes = TREE_CHAIN (attributes);
16833 TREE_CHAIN (attributes) = NULL_TREE;
16835 /* If there is any qualification still in effect, clear it
16836 now; we will be starting fresh with the next declarator. */
16837 parser->scope = NULL_TREE;
16838 parser->qualifying_scope = NULL_TREE;
16839 parser->object_scope = NULL_TREE;
16840 /* If it's a `,', then there are more declarators. */
16841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16842 cp_lexer_consume_token (parser->lexer);
16843 /* If the next token isn't a `;', then we have a parse error. */
16844 else if (cp_lexer_next_token_is_not (parser->lexer,
16847 cp_parser_error (parser, "expected %<;%>");
16848 /* Skip tokens until we find a `;'. */
16849 cp_parser_skip_to_end_of_statement (parser);
16856 /* Add DECL to the list of members. */
16858 finish_member_declaration (decl);
16860 if (TREE_CODE (decl) == FUNCTION_DECL)
16861 cp_parser_save_default_args (parser, decl);
16866 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16869 /* Parse a pure-specifier.
16874 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16875 Otherwise, ERROR_MARK_NODE is returned. */
16878 cp_parser_pure_specifier (cp_parser* parser)
16882 /* Look for the `=' token. */
16883 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16884 return error_mark_node;
16885 /* Look for the `0' token. */
16886 token = cp_lexer_peek_token (parser->lexer);
16888 if (token->type == CPP_EOF
16889 || token->type == CPP_PRAGMA_EOL)
16890 return error_mark_node;
16892 cp_lexer_consume_token (parser->lexer);
16894 /* Accept = default or = delete in c++0x mode. */
16895 if (token->keyword == RID_DEFAULT
16896 || token->keyword == RID_DELETE)
16898 maybe_warn_cpp0x ("defaulted and deleted functions");
16899 return token->u.value;
16902 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16903 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16905 cp_parser_error (parser,
16906 "invalid pure specifier (only %<= 0%> is allowed)");
16907 cp_parser_skip_to_end_of_statement (parser);
16908 return error_mark_node;
16910 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16912 error_at (token->location, "templates may not be %<virtual%>");
16913 return error_mark_node;
16916 return integer_zero_node;
16919 /* Parse a constant-initializer.
16921 constant-initializer:
16922 = constant-expression
16924 Returns a representation of the constant-expression. */
16927 cp_parser_constant_initializer (cp_parser* parser)
16929 /* Look for the `=' token. */
16930 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16931 return error_mark_node;
16933 /* It is invalid to write:
16935 struct S { static const int i = { 7 }; };
16938 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16940 cp_parser_error (parser,
16941 "a brace-enclosed initializer is not allowed here");
16942 /* Consume the opening brace. */
16943 cp_lexer_consume_token (parser->lexer);
16944 /* Skip the initializer. */
16945 cp_parser_skip_to_closing_brace (parser);
16946 /* Look for the trailing `}'. */
16947 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16949 return error_mark_node;
16952 return cp_parser_constant_expression (parser,
16953 /*allow_non_constant=*/false,
16957 /* Derived classes [gram.class.derived] */
16959 /* Parse a base-clause.
16962 : base-specifier-list
16964 base-specifier-list:
16965 base-specifier ... [opt]
16966 base-specifier-list , base-specifier ... [opt]
16968 Returns a TREE_LIST representing the base-classes, in the order in
16969 which they were declared. The representation of each node is as
16970 described by cp_parser_base_specifier.
16972 In the case that no bases are specified, this function will return
16973 NULL_TREE, not ERROR_MARK_NODE. */
16976 cp_parser_base_clause (cp_parser* parser)
16978 tree bases = NULL_TREE;
16980 /* Look for the `:' that begins the list. */
16981 cp_parser_require (parser, CPP_COLON, "%<:%>");
16983 /* Scan the base-specifier-list. */
16988 bool pack_expansion_p = false;
16990 /* Look for the base-specifier. */
16991 base = cp_parser_base_specifier (parser);
16992 /* Look for the (optional) ellipsis. */
16993 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16995 /* Consume the `...'. */
16996 cp_lexer_consume_token (parser->lexer);
16998 pack_expansion_p = true;
17001 /* Add BASE to the front of the list. */
17002 if (base != error_mark_node)
17004 if (pack_expansion_p)
17005 /* Make this a pack expansion type. */
17006 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17009 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17011 TREE_CHAIN (base) = bases;
17015 /* Peek at the next token. */
17016 token = cp_lexer_peek_token (parser->lexer);
17017 /* If it's not a comma, then the list is complete. */
17018 if (token->type != CPP_COMMA)
17020 /* Consume the `,'. */
17021 cp_lexer_consume_token (parser->lexer);
17024 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17025 base class had a qualified name. However, the next name that
17026 appears is certainly not qualified. */
17027 parser->scope = NULL_TREE;
17028 parser->qualifying_scope = NULL_TREE;
17029 parser->object_scope = NULL_TREE;
17031 return nreverse (bases);
17034 /* Parse a base-specifier.
17037 :: [opt] nested-name-specifier [opt] class-name
17038 virtual access-specifier [opt] :: [opt] nested-name-specifier
17040 access-specifier virtual [opt] :: [opt] nested-name-specifier
17043 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17044 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17045 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17046 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17049 cp_parser_base_specifier (cp_parser* parser)
17053 bool virtual_p = false;
17054 bool duplicate_virtual_error_issued_p = false;
17055 bool duplicate_access_error_issued_p = false;
17056 bool class_scope_p, template_p;
17057 tree access = access_default_node;
17060 /* Process the optional `virtual' and `access-specifier'. */
17063 /* Peek at the next token. */
17064 token = cp_lexer_peek_token (parser->lexer);
17065 /* Process `virtual'. */
17066 switch (token->keyword)
17069 /* If `virtual' appears more than once, issue an error. */
17070 if (virtual_p && !duplicate_virtual_error_issued_p)
17072 cp_parser_error (parser,
17073 "%<virtual%> specified more than once in base-specified");
17074 duplicate_virtual_error_issued_p = true;
17079 /* Consume the `virtual' token. */
17080 cp_lexer_consume_token (parser->lexer);
17085 case RID_PROTECTED:
17087 /* If more than one access specifier appears, issue an
17089 if (access != access_default_node
17090 && !duplicate_access_error_issued_p)
17092 cp_parser_error (parser,
17093 "more than one access specifier in base-specified");
17094 duplicate_access_error_issued_p = true;
17097 access = ridpointers[(int) token->keyword];
17099 /* Consume the access-specifier. */
17100 cp_lexer_consume_token (parser->lexer);
17109 /* It is not uncommon to see programs mechanically, erroneously, use
17110 the 'typename' keyword to denote (dependent) qualified types
17111 as base classes. */
17112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17114 token = cp_lexer_peek_token (parser->lexer);
17115 if (!processing_template_decl)
17116 error_at (token->location,
17117 "keyword %<typename%> not allowed outside of templates");
17119 error_at (token->location,
17120 "keyword %<typename%> not allowed in this context "
17121 "(the base class is implicitly a type)");
17122 cp_lexer_consume_token (parser->lexer);
17125 /* Look for the optional `::' operator. */
17126 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17127 /* Look for the nested-name-specifier. The simplest way to
17132 The keyword `typename' is not permitted in a base-specifier or
17133 mem-initializer; in these contexts a qualified name that
17134 depends on a template-parameter is implicitly assumed to be a
17137 is to pretend that we have seen the `typename' keyword at this
17139 cp_parser_nested_name_specifier_opt (parser,
17140 /*typename_keyword_p=*/true,
17141 /*check_dependency_p=*/true,
17143 /*is_declaration=*/true);
17144 /* If the base class is given by a qualified name, assume that names
17145 we see are type names or templates, as appropriate. */
17146 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17147 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17149 /* Finally, look for the class-name. */
17150 type = cp_parser_class_name (parser,
17154 /*check_dependency_p=*/true,
17155 /*class_head_p=*/false,
17156 /*is_declaration=*/true);
17158 if (type == error_mark_node)
17159 return error_mark_node;
17161 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17164 /* Exception handling [gram.exception] */
17166 /* Parse an (optional) exception-specification.
17168 exception-specification:
17169 throw ( type-id-list [opt] )
17171 Returns a TREE_LIST representing the exception-specification. The
17172 TREE_VALUE of each node is a type. */
17175 cp_parser_exception_specification_opt (cp_parser* parser)
17180 /* Peek at the next token. */
17181 token = cp_lexer_peek_token (parser->lexer);
17182 /* If it's not `throw', then there's no exception-specification. */
17183 if (!cp_parser_is_keyword (token, RID_THROW))
17186 /* Consume the `throw'. */
17187 cp_lexer_consume_token (parser->lexer);
17189 /* Look for the `('. */
17190 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17192 /* Peek at the next token. */
17193 token = cp_lexer_peek_token (parser->lexer);
17194 /* If it's not a `)', then there is a type-id-list. */
17195 if (token->type != CPP_CLOSE_PAREN)
17197 const char *saved_message;
17199 /* Types may not be defined in an exception-specification. */
17200 saved_message = parser->type_definition_forbidden_message;
17201 parser->type_definition_forbidden_message
17202 = "types may not be defined in an exception-specification";
17203 /* Parse the type-id-list. */
17204 type_id_list = cp_parser_type_id_list (parser);
17205 /* Restore the saved message. */
17206 parser->type_definition_forbidden_message = saved_message;
17209 type_id_list = empty_except_spec;
17211 /* Look for the `)'. */
17212 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17214 return type_id_list;
17217 /* Parse an (optional) type-id-list.
17221 type-id-list , type-id ... [opt]
17223 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17224 in the order that the types were presented. */
17227 cp_parser_type_id_list (cp_parser* parser)
17229 tree types = NULL_TREE;
17236 /* Get the next type-id. */
17237 type = cp_parser_type_id (parser);
17238 /* Parse the optional ellipsis. */
17239 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17241 /* Consume the `...'. */
17242 cp_lexer_consume_token (parser->lexer);
17244 /* Turn the type into a pack expansion expression. */
17245 type = make_pack_expansion (type);
17247 /* Add it to the list. */
17248 types = add_exception_specifier (types, type, /*complain=*/1);
17249 /* Peek at the next token. */
17250 token = cp_lexer_peek_token (parser->lexer);
17251 /* If it is not a `,', we are done. */
17252 if (token->type != CPP_COMMA)
17254 /* Consume the `,'. */
17255 cp_lexer_consume_token (parser->lexer);
17258 return nreverse (types);
17261 /* Parse a try-block.
17264 try compound-statement handler-seq */
17267 cp_parser_try_block (cp_parser* parser)
17271 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17272 try_block = begin_try_block ();
17273 cp_parser_compound_statement (parser, NULL, true);
17274 finish_try_block (try_block);
17275 cp_parser_handler_seq (parser);
17276 finish_handler_sequence (try_block);
17281 /* Parse a function-try-block.
17283 function-try-block:
17284 try ctor-initializer [opt] function-body handler-seq */
17287 cp_parser_function_try_block (cp_parser* parser)
17289 tree compound_stmt;
17291 bool ctor_initializer_p;
17293 /* Look for the `try' keyword. */
17294 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17296 /* Let the rest of the front end know where we are. */
17297 try_block = begin_function_try_block (&compound_stmt);
17298 /* Parse the function-body. */
17300 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17301 /* We're done with the `try' part. */
17302 finish_function_try_block (try_block);
17303 /* Parse the handlers. */
17304 cp_parser_handler_seq (parser);
17305 /* We're done with the handlers. */
17306 finish_function_handler_sequence (try_block, compound_stmt);
17308 return ctor_initializer_p;
17311 /* Parse a handler-seq.
17314 handler handler-seq [opt] */
17317 cp_parser_handler_seq (cp_parser* parser)
17323 /* Parse the handler. */
17324 cp_parser_handler (parser);
17325 /* Peek at the next token. */
17326 token = cp_lexer_peek_token (parser->lexer);
17327 /* If it's not `catch' then there are no more handlers. */
17328 if (!cp_parser_is_keyword (token, RID_CATCH))
17333 /* Parse a handler.
17336 catch ( exception-declaration ) compound-statement */
17339 cp_parser_handler (cp_parser* parser)
17344 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17345 handler = begin_handler ();
17346 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17347 declaration = cp_parser_exception_declaration (parser);
17348 finish_handler_parms (declaration, handler);
17349 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17350 cp_parser_compound_statement (parser, NULL, false);
17351 finish_handler (handler);
17354 /* Parse an exception-declaration.
17356 exception-declaration:
17357 type-specifier-seq declarator
17358 type-specifier-seq abstract-declarator
17362 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17363 ellipsis variant is used. */
17366 cp_parser_exception_declaration (cp_parser* parser)
17368 cp_decl_specifier_seq type_specifiers;
17369 cp_declarator *declarator;
17370 const char *saved_message;
17372 /* If it's an ellipsis, it's easy to handle. */
17373 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17375 /* Consume the `...' token. */
17376 cp_lexer_consume_token (parser->lexer);
17380 /* Types may not be defined in exception-declarations. */
17381 saved_message = parser->type_definition_forbidden_message;
17382 parser->type_definition_forbidden_message
17383 = "types may not be defined in exception-declarations";
17385 /* Parse the type-specifier-seq. */
17386 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17387 /*is_trailing_return=*/false,
17389 /* If it's a `)', then there is no declarator. */
17390 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17393 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17394 /*ctor_dtor_or_conv_p=*/NULL,
17395 /*parenthesized_p=*/NULL,
17396 /*member_p=*/false);
17398 /* Restore the saved message. */
17399 parser->type_definition_forbidden_message = saved_message;
17401 if (!type_specifiers.any_specifiers_p)
17402 return error_mark_node;
17404 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17407 /* Parse a throw-expression.
17410 throw assignment-expression [opt]
17412 Returns a THROW_EXPR representing the throw-expression. */
17415 cp_parser_throw_expression (cp_parser* parser)
17420 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17421 token = cp_lexer_peek_token (parser->lexer);
17422 /* Figure out whether or not there is an assignment-expression
17423 following the "throw" keyword. */
17424 if (token->type == CPP_COMMA
17425 || token->type == CPP_SEMICOLON
17426 || token->type == CPP_CLOSE_PAREN
17427 || token->type == CPP_CLOSE_SQUARE
17428 || token->type == CPP_CLOSE_BRACE
17429 || token->type == CPP_COLON)
17430 expression = NULL_TREE;
17432 expression = cp_parser_assignment_expression (parser,
17433 /*cast_p=*/false, NULL);
17435 return build_throw (expression);
17438 /* GNU Extensions */
17440 /* Parse an (optional) asm-specification.
17443 asm ( string-literal )
17445 If the asm-specification is present, returns a STRING_CST
17446 corresponding to the string-literal. Otherwise, returns
17450 cp_parser_asm_specification_opt (cp_parser* parser)
17453 tree asm_specification;
17455 /* Peek at the next token. */
17456 token = cp_lexer_peek_token (parser->lexer);
17457 /* If the next token isn't the `asm' keyword, then there's no
17458 asm-specification. */
17459 if (!cp_parser_is_keyword (token, RID_ASM))
17462 /* Consume the `asm' token. */
17463 cp_lexer_consume_token (parser->lexer);
17464 /* Look for the `('. */
17465 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17467 /* Look for the string-literal. */
17468 asm_specification = cp_parser_string_literal (parser, false, false);
17470 /* Look for the `)'. */
17471 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17473 return asm_specification;
17476 /* Parse an asm-operand-list.
17480 asm-operand-list , asm-operand
17483 string-literal ( expression )
17484 [ string-literal ] string-literal ( expression )
17486 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17487 each node is the expression. The TREE_PURPOSE is itself a
17488 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17489 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17490 is a STRING_CST for the string literal before the parenthesis. Returns
17491 ERROR_MARK_NODE if any of the operands are invalid. */
17494 cp_parser_asm_operand_list (cp_parser* parser)
17496 tree asm_operands = NULL_TREE;
17497 bool invalid_operands = false;
17501 tree string_literal;
17505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17507 /* Consume the `[' token. */
17508 cp_lexer_consume_token (parser->lexer);
17509 /* Read the operand name. */
17510 name = cp_parser_identifier (parser);
17511 if (name != error_mark_node)
17512 name = build_string (IDENTIFIER_LENGTH (name),
17513 IDENTIFIER_POINTER (name));
17514 /* Look for the closing `]'. */
17515 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17519 /* Look for the string-literal. */
17520 string_literal = cp_parser_string_literal (parser, false, false);
17522 /* Look for the `('. */
17523 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17524 /* Parse the expression. */
17525 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17526 /* Look for the `)'. */
17527 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17529 if (name == error_mark_node
17530 || string_literal == error_mark_node
17531 || expression == error_mark_node)
17532 invalid_operands = true;
17534 /* Add this operand to the list. */
17535 asm_operands = tree_cons (build_tree_list (name, string_literal),
17538 /* If the next token is not a `,', there are no more
17540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17542 /* Consume the `,'. */
17543 cp_lexer_consume_token (parser->lexer);
17546 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17549 /* Parse an asm-clobber-list.
17553 asm-clobber-list , string-literal
17555 Returns a TREE_LIST, indicating the clobbers in the order that they
17556 appeared. The TREE_VALUE of each node is a STRING_CST. */
17559 cp_parser_asm_clobber_list (cp_parser* parser)
17561 tree clobbers = NULL_TREE;
17565 tree string_literal;
17567 /* Look for the string literal. */
17568 string_literal = cp_parser_string_literal (parser, false, false);
17569 /* Add it to the list. */
17570 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17571 /* If the next token is not a `,', then the list is
17573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17575 /* Consume the `,' token. */
17576 cp_lexer_consume_token (parser->lexer);
17582 /* Parse an asm-label-list.
17586 asm-label-list , identifier
17588 Returns a TREE_LIST, indicating the labels in the order that they
17589 appeared. The TREE_VALUE of each node is a label. */
17592 cp_parser_asm_label_list (cp_parser* parser)
17594 tree labels = NULL_TREE;
17598 tree identifier, label, name;
17600 /* Look for the identifier. */
17601 identifier = cp_parser_identifier (parser);
17602 if (!error_operand_p (identifier))
17604 label = lookup_label (identifier);
17605 if (TREE_CODE (label) == LABEL_DECL)
17607 TREE_USED (label) = 1;
17608 check_goto (label);
17609 name = build_string (IDENTIFIER_LENGTH (identifier),
17610 IDENTIFIER_POINTER (identifier));
17611 labels = tree_cons (name, label, labels);
17614 /* If the next token is not a `,', then the list is
17616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17618 /* Consume the `,' token. */
17619 cp_lexer_consume_token (parser->lexer);
17622 return nreverse (labels);
17625 /* Parse an (optional) series of attributes.
17628 attributes attribute
17631 __attribute__ (( attribute-list [opt] ))
17633 The return value is as for cp_parser_attribute_list. */
17636 cp_parser_attributes_opt (cp_parser* parser)
17638 tree attributes = NULL_TREE;
17643 tree attribute_list;
17645 /* Peek at the next token. */
17646 token = cp_lexer_peek_token (parser->lexer);
17647 /* If it's not `__attribute__', then we're done. */
17648 if (token->keyword != RID_ATTRIBUTE)
17651 /* Consume the `__attribute__' keyword. */
17652 cp_lexer_consume_token (parser->lexer);
17653 /* Look for the two `(' tokens. */
17654 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17655 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17657 /* Peek at the next token. */
17658 token = cp_lexer_peek_token (parser->lexer);
17659 if (token->type != CPP_CLOSE_PAREN)
17660 /* Parse the attribute-list. */
17661 attribute_list = cp_parser_attribute_list (parser);
17663 /* If the next token is a `)', then there is no attribute
17665 attribute_list = NULL;
17667 /* Look for the two `)' tokens. */
17668 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17669 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17671 /* Add these new attributes to the list. */
17672 attributes = chainon (attributes, attribute_list);
17678 /* Parse an attribute-list.
17682 attribute-list , attribute
17686 identifier ( identifier )
17687 identifier ( identifier , expression-list )
17688 identifier ( expression-list )
17690 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17691 to an attribute. The TREE_PURPOSE of each node is the identifier
17692 indicating which attribute is in use. The TREE_VALUE represents
17693 the arguments, if any. */
17696 cp_parser_attribute_list (cp_parser* parser)
17698 tree attribute_list = NULL_TREE;
17699 bool save_translate_strings_p = parser->translate_strings_p;
17701 parser->translate_strings_p = false;
17708 /* Look for the identifier. We also allow keywords here; for
17709 example `__attribute__ ((const))' is legal. */
17710 token = cp_lexer_peek_token (parser->lexer);
17711 if (token->type == CPP_NAME
17712 || token->type == CPP_KEYWORD)
17714 tree arguments = NULL_TREE;
17716 /* Consume the token. */
17717 token = cp_lexer_consume_token (parser->lexer);
17719 /* Save away the identifier that indicates which attribute
17721 identifier = (token->type == CPP_KEYWORD)
17722 /* For keywords, use the canonical spelling, not the
17723 parsed identifier. */
17724 ? ridpointers[(int) token->keyword]
17727 attribute = build_tree_list (identifier, NULL_TREE);
17729 /* Peek at the next token. */
17730 token = cp_lexer_peek_token (parser->lexer);
17731 /* If it's an `(', then parse the attribute arguments. */
17732 if (token->type == CPP_OPEN_PAREN)
17735 vec = cp_parser_parenthesized_expression_list
17736 (parser, true, /*cast_p=*/false,
17737 /*allow_expansion_p=*/false,
17738 /*non_constant_p=*/NULL);
17740 arguments = error_mark_node;
17743 arguments = build_tree_list_vec (vec);
17744 release_tree_vector (vec);
17746 /* Save the arguments away. */
17747 TREE_VALUE (attribute) = arguments;
17750 if (arguments != error_mark_node)
17752 /* Add this attribute to the list. */
17753 TREE_CHAIN (attribute) = attribute_list;
17754 attribute_list = attribute;
17757 token = cp_lexer_peek_token (parser->lexer);
17759 /* Now, look for more attributes. If the next token isn't a
17760 `,', we're done. */
17761 if (token->type != CPP_COMMA)
17764 /* Consume the comma and keep going. */
17765 cp_lexer_consume_token (parser->lexer);
17767 parser->translate_strings_p = save_translate_strings_p;
17769 /* We built up the list in reverse order. */
17770 return nreverse (attribute_list);
17773 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17774 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17775 current value of the PEDANTIC flag, regardless of whether or not
17776 the `__extension__' keyword is present. The caller is responsible
17777 for restoring the value of the PEDANTIC flag. */
17780 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17782 /* Save the old value of the PEDANTIC flag. */
17783 *saved_pedantic = pedantic;
17785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17787 /* Consume the `__extension__' token. */
17788 cp_lexer_consume_token (parser->lexer);
17789 /* We're not being pedantic while the `__extension__' keyword is
17799 /* Parse a label declaration.
17802 __label__ label-declarator-seq ;
17804 label-declarator-seq:
17805 identifier , label-declarator-seq
17809 cp_parser_label_declaration (cp_parser* parser)
17811 /* Look for the `__label__' keyword. */
17812 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17818 /* Look for an identifier. */
17819 identifier = cp_parser_identifier (parser);
17820 /* If we failed, stop. */
17821 if (identifier == error_mark_node)
17823 /* Declare it as a label. */
17824 finish_label_decl (identifier);
17825 /* If the next token is a `;', stop. */
17826 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17828 /* Look for the `,' separating the label declarations. */
17829 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17832 /* Look for the final `;'. */
17833 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17836 /* Support Functions */
17838 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17839 NAME should have one of the representations used for an
17840 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17841 is returned. If PARSER->SCOPE is a dependent type, then a
17842 SCOPE_REF is returned.
17844 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17845 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17846 was formed. Abstractly, such entities should not be passed to this
17847 function, because they do not need to be looked up, but it is
17848 simpler to check for this special case here, rather than at the
17851 In cases not explicitly covered above, this function returns a
17852 DECL, OVERLOAD, or baselink representing the result of the lookup.
17853 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17856 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17857 (e.g., "struct") that was used. In that case bindings that do not
17858 refer to types are ignored.
17860 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17863 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17866 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17869 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17870 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17871 NULL_TREE otherwise. */
17874 cp_parser_lookup_name (cp_parser *parser, tree name,
17875 enum tag_types tag_type,
17878 bool check_dependency,
17879 tree *ambiguous_decls,
17880 location_t name_location)
17884 tree object_type = parser->context->object_type;
17886 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17887 flags |= LOOKUP_COMPLAIN;
17889 /* Assume that the lookup will be unambiguous. */
17890 if (ambiguous_decls)
17891 *ambiguous_decls = NULL_TREE;
17893 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17894 no longer valid. Note that if we are parsing tentatively, and
17895 the parse fails, OBJECT_TYPE will be automatically restored. */
17896 parser->context->object_type = NULL_TREE;
17898 if (name == error_mark_node)
17899 return error_mark_node;
17901 /* A template-id has already been resolved; there is no lookup to
17903 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17905 if (BASELINK_P (name))
17907 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17908 == TEMPLATE_ID_EXPR);
17912 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17913 it should already have been checked to make sure that the name
17914 used matches the type being destroyed. */
17915 if (TREE_CODE (name) == BIT_NOT_EXPR)
17919 /* Figure out to which type this destructor applies. */
17921 type = parser->scope;
17922 else if (object_type)
17923 type = object_type;
17925 type = current_class_type;
17926 /* If that's not a class type, there is no destructor. */
17927 if (!type || !CLASS_TYPE_P (type))
17928 return error_mark_node;
17929 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17930 lazily_declare_fn (sfk_destructor, type);
17931 if (!CLASSTYPE_DESTRUCTORS (type))
17932 return error_mark_node;
17933 /* If it was a class type, return the destructor. */
17934 return CLASSTYPE_DESTRUCTORS (type);
17937 /* By this point, the NAME should be an ordinary identifier. If
17938 the id-expression was a qualified name, the qualifying scope is
17939 stored in PARSER->SCOPE at this point. */
17940 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17942 /* Perform the lookup. */
17947 if (parser->scope == error_mark_node)
17948 return error_mark_node;
17950 /* If the SCOPE is dependent, the lookup must be deferred until
17951 the template is instantiated -- unless we are explicitly
17952 looking up names in uninstantiated templates. Even then, we
17953 cannot look up the name if the scope is not a class type; it
17954 might, for example, be a template type parameter. */
17955 dependent_p = (TYPE_P (parser->scope)
17956 && dependent_scope_p (parser->scope));
17957 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17959 /* Defer lookup. */
17960 decl = error_mark_node;
17963 tree pushed_scope = NULL_TREE;
17965 /* If PARSER->SCOPE is a dependent type, then it must be a
17966 class type, and we must not be checking dependencies;
17967 otherwise, we would have processed this lookup above. So
17968 that PARSER->SCOPE is not considered a dependent base by
17969 lookup_member, we must enter the scope here. */
17971 pushed_scope = push_scope (parser->scope);
17972 /* If the PARSER->SCOPE is a template specialization, it
17973 may be instantiated during name lookup. In that case,
17974 errors may be issued. Even if we rollback the current
17975 tentative parse, those errors are valid. */
17976 decl = lookup_qualified_name (parser->scope, name,
17977 tag_type != none_type,
17978 /*complain=*/true);
17980 /* If we have a single function from a using decl, pull it out. */
17981 if (TREE_CODE (decl) == OVERLOAD
17982 && !really_overloaded_fn (decl))
17983 decl = OVL_FUNCTION (decl);
17986 pop_scope (pushed_scope);
17989 /* If the scope is a dependent type and either we deferred lookup or
17990 we did lookup but didn't find the name, rememeber the name. */
17991 if (decl == error_mark_node && TYPE_P (parser->scope)
17992 && dependent_type_p (parser->scope))
17998 /* The resolution to Core Issue 180 says that `struct
17999 A::B' should be considered a type-name, even if `A'
18001 type = make_typename_type (parser->scope, name, tag_type,
18002 /*complain=*/tf_error);
18003 decl = TYPE_NAME (type);
18005 else if (is_template
18006 && (cp_parser_next_token_ends_template_argument_p (parser)
18007 || cp_lexer_next_token_is (parser->lexer,
18009 decl = make_unbound_class_template (parser->scope,
18011 /*complain=*/tf_error);
18013 decl = build_qualified_name (/*type=*/NULL_TREE,
18014 parser->scope, name,
18017 parser->qualifying_scope = parser->scope;
18018 parser->object_scope = NULL_TREE;
18020 else if (object_type)
18022 tree object_decl = NULL_TREE;
18023 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18024 OBJECT_TYPE is not a class. */
18025 if (CLASS_TYPE_P (object_type))
18026 /* If the OBJECT_TYPE is a template specialization, it may
18027 be instantiated during name lookup. In that case, errors
18028 may be issued. Even if we rollback the current tentative
18029 parse, those errors are valid. */
18030 object_decl = lookup_member (object_type,
18033 tag_type != none_type);
18034 /* Look it up in the enclosing context, too. */
18035 decl = lookup_name_real (name, tag_type != none_type,
18037 /*block_p=*/true, is_namespace, flags);
18038 parser->object_scope = object_type;
18039 parser->qualifying_scope = NULL_TREE;
18041 decl = object_decl;
18045 decl = lookup_name_real (name, tag_type != none_type,
18047 /*block_p=*/true, is_namespace, flags);
18048 parser->qualifying_scope = NULL_TREE;
18049 parser->object_scope = NULL_TREE;
18052 /* If the lookup failed, let our caller know. */
18053 if (!decl || decl == error_mark_node)
18054 return error_mark_node;
18056 /* Pull out the template from an injected-class-name (or multiple). */
18058 decl = maybe_get_template_decl_from_type_decl (decl);
18060 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18061 if (TREE_CODE (decl) == TREE_LIST)
18063 if (ambiguous_decls)
18064 *ambiguous_decls = decl;
18065 /* The error message we have to print is too complicated for
18066 cp_parser_error, so we incorporate its actions directly. */
18067 if (!cp_parser_simulate_error (parser))
18069 error_at (name_location, "reference to %qD is ambiguous",
18071 print_candidates (decl);
18073 return error_mark_node;
18076 gcc_assert (DECL_P (decl)
18077 || TREE_CODE (decl) == OVERLOAD
18078 || TREE_CODE (decl) == SCOPE_REF
18079 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18080 || BASELINK_P (decl));
18082 /* If we have resolved the name of a member declaration, check to
18083 see if the declaration is accessible. When the name resolves to
18084 set of overloaded functions, accessibility is checked when
18085 overload resolution is done.
18087 During an explicit instantiation, access is not checked at all,
18088 as per [temp.explicit]. */
18090 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18095 /* Like cp_parser_lookup_name, but for use in the typical case where
18096 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18097 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18100 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18102 return cp_parser_lookup_name (parser, name,
18104 /*is_template=*/false,
18105 /*is_namespace=*/false,
18106 /*check_dependency=*/true,
18107 /*ambiguous_decls=*/NULL,
18111 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18112 the current context, return the TYPE_DECL. If TAG_NAME_P is
18113 true, the DECL indicates the class being defined in a class-head,
18114 or declared in an elaborated-type-specifier.
18116 Otherwise, return DECL. */
18119 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18121 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18122 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18125 template <typename T> struct B;
18128 template <typename T> struct A::B {};
18130 Similarly, in an elaborated-type-specifier:
18132 namespace N { struct X{}; }
18135 template <typename T> friend struct N::X;
18138 However, if the DECL refers to a class type, and we are in
18139 the scope of the class, then the name lookup automatically
18140 finds the TYPE_DECL created by build_self_reference rather
18141 than a TEMPLATE_DECL. For example, in:
18143 template <class T> struct S {
18147 there is no need to handle such case. */
18149 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18150 return DECL_TEMPLATE_RESULT (decl);
18155 /* If too many, or too few, template-parameter lists apply to the
18156 declarator, issue an error message. Returns TRUE if all went well,
18157 and FALSE otherwise. */
18160 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18161 cp_declarator *declarator,
18162 location_t declarator_location)
18164 unsigned num_templates;
18166 /* We haven't seen any classes that involve template parameters yet. */
18169 switch (declarator->kind)
18172 if (declarator->u.id.qualifying_scope)
18177 scope = declarator->u.id.qualifying_scope;
18178 member = declarator->u.id.unqualified_name;
18180 while (scope && CLASS_TYPE_P (scope))
18182 /* You're supposed to have one `template <...>'
18183 for every template class, but you don't need one
18184 for a full specialization. For example:
18186 template <class T> struct S{};
18187 template <> struct S<int> { void f(); };
18188 void S<int>::f () {}
18190 is correct; there shouldn't be a `template <>' for
18191 the definition of `S<int>::f'. */
18192 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18193 /* If SCOPE does not have template information of any
18194 kind, then it is not a template, nor is it nested
18195 within a template. */
18197 if (explicit_class_specialization_p (scope))
18199 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18202 scope = TYPE_CONTEXT (scope);
18205 else if (TREE_CODE (declarator->u.id.unqualified_name)
18206 == TEMPLATE_ID_EXPR)
18207 /* If the DECLARATOR has the form `X<y>' then it uses one
18208 additional level of template parameters. */
18211 return cp_parser_check_template_parameters
18212 (parser, num_templates, declarator_location, declarator);
18218 case cdk_reference:
18220 return (cp_parser_check_declarator_template_parameters
18221 (parser, declarator->declarator, declarator_location));
18227 gcc_unreachable ();
18232 /* NUM_TEMPLATES were used in the current declaration. If that is
18233 invalid, return FALSE and issue an error messages. Otherwise,
18234 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18235 declarator and we can print more accurate diagnostics. */
18238 cp_parser_check_template_parameters (cp_parser* parser,
18239 unsigned num_templates,
18240 location_t location,
18241 cp_declarator *declarator)
18243 /* If there are the same number of template classes and parameter
18244 lists, that's OK. */
18245 if (parser->num_template_parameter_lists == num_templates)
18247 /* If there are more, but only one more, then we are referring to a
18248 member template. That's OK too. */
18249 if (parser->num_template_parameter_lists == num_templates + 1)
18251 /* If there are more template classes than parameter lists, we have
18254 template <class T> void S<T>::R<T>::f (); */
18255 if (parser->num_template_parameter_lists < num_templates)
18257 if (declarator && !current_function_decl)
18258 error_at (location, "specializing member %<%T::%E%> "
18259 "requires %<template<>%> syntax",
18260 declarator->u.id.qualifying_scope,
18261 declarator->u.id.unqualified_name);
18262 else if (declarator)
18263 error_at (location, "invalid declaration of %<%T::%E%>",
18264 declarator->u.id.qualifying_scope,
18265 declarator->u.id.unqualified_name);
18267 error_at (location, "too few template-parameter-lists");
18270 /* Otherwise, there are too many template parameter lists. We have
18273 template <class T> template <class U> void S::f(); */
18274 error_at (location, "too many template-parameter-lists");
18278 /* Parse an optional `::' token indicating that the following name is
18279 from the global namespace. If so, PARSER->SCOPE is set to the
18280 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18281 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18282 Returns the new value of PARSER->SCOPE, if the `::' token is
18283 present, and NULL_TREE otherwise. */
18286 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18290 /* Peek at the next token. */
18291 token = cp_lexer_peek_token (parser->lexer);
18292 /* If we're looking at a `::' token then we're starting from the
18293 global namespace, not our current location. */
18294 if (token->type == CPP_SCOPE)
18296 /* Consume the `::' token. */
18297 cp_lexer_consume_token (parser->lexer);
18298 /* Set the SCOPE so that we know where to start the lookup. */
18299 parser->scope = global_namespace;
18300 parser->qualifying_scope = global_namespace;
18301 parser->object_scope = NULL_TREE;
18303 return parser->scope;
18305 else if (!current_scope_valid_p)
18307 parser->scope = NULL_TREE;
18308 parser->qualifying_scope = NULL_TREE;
18309 parser->object_scope = NULL_TREE;
18315 /* Returns TRUE if the upcoming token sequence is the start of a
18316 constructor declarator. If FRIEND_P is true, the declarator is
18317 preceded by the `friend' specifier. */
18320 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18322 bool constructor_p;
18323 tree type_decl = NULL_TREE;
18324 bool nested_name_p;
18325 cp_token *next_token;
18327 /* The common case is that this is not a constructor declarator, so
18328 try to avoid doing lots of work if at all possible. It's not
18329 valid declare a constructor at function scope. */
18330 if (parser->in_function_body)
18332 /* And only certain tokens can begin a constructor declarator. */
18333 next_token = cp_lexer_peek_token (parser->lexer);
18334 if (next_token->type != CPP_NAME
18335 && next_token->type != CPP_SCOPE
18336 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18337 && next_token->type != CPP_TEMPLATE_ID)
18340 /* Parse tentatively; we are going to roll back all of the tokens
18342 cp_parser_parse_tentatively (parser);
18343 /* Assume that we are looking at a constructor declarator. */
18344 constructor_p = true;
18346 /* Look for the optional `::' operator. */
18347 cp_parser_global_scope_opt (parser,
18348 /*current_scope_valid_p=*/false);
18349 /* Look for the nested-name-specifier. */
18351 = (cp_parser_nested_name_specifier_opt (parser,
18352 /*typename_keyword_p=*/false,
18353 /*check_dependency_p=*/false,
18355 /*is_declaration=*/false)
18357 /* Outside of a class-specifier, there must be a
18358 nested-name-specifier. */
18359 if (!nested_name_p &&
18360 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18362 constructor_p = false;
18363 /* If we still think that this might be a constructor-declarator,
18364 look for a class-name. */
18369 template <typename T> struct S { S(); };
18370 template <typename T> S<T>::S ();
18372 we must recognize that the nested `S' names a class.
18375 template <typename T> S<T>::S<T> ();
18377 we must recognize that the nested `S' names a template. */
18378 type_decl = cp_parser_class_name (parser,
18379 /*typename_keyword_p=*/false,
18380 /*template_keyword_p=*/false,
18382 /*check_dependency_p=*/false,
18383 /*class_head_p=*/false,
18384 /*is_declaration=*/false);
18385 /* If there was no class-name, then this is not a constructor. */
18386 constructor_p = !cp_parser_error_occurred (parser);
18389 /* If we're still considering a constructor, we have to see a `(',
18390 to begin the parameter-declaration-clause, followed by either a
18391 `)', an `...', or a decl-specifier. We need to check for a
18392 type-specifier to avoid being fooled into thinking that:
18396 is a constructor. (It is actually a function named `f' that
18397 takes one parameter (of type `int') and returns a value of type
18400 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18403 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18404 /* A parameter declaration begins with a decl-specifier,
18405 which is either the "attribute" keyword, a storage class
18406 specifier, or (usually) a type-specifier. */
18407 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18410 tree pushed_scope = NULL_TREE;
18411 unsigned saved_num_template_parameter_lists;
18413 /* Names appearing in the type-specifier should be looked up
18414 in the scope of the class. */
18415 if (current_class_type)
18419 type = TREE_TYPE (type_decl);
18420 if (TREE_CODE (type) == TYPENAME_TYPE)
18422 type = resolve_typename_type (type,
18423 /*only_current_p=*/false);
18424 if (TREE_CODE (type) == TYPENAME_TYPE)
18426 cp_parser_abort_tentative_parse (parser);
18430 pushed_scope = push_scope (type);
18433 /* Inside the constructor parameter list, surrounding
18434 template-parameter-lists do not apply. */
18435 saved_num_template_parameter_lists
18436 = parser->num_template_parameter_lists;
18437 parser->num_template_parameter_lists = 0;
18439 /* Look for the type-specifier. */
18440 cp_parser_type_specifier (parser,
18441 CP_PARSER_FLAGS_NONE,
18442 /*decl_specs=*/NULL,
18443 /*is_declarator=*/true,
18444 /*declares_class_or_enum=*/NULL,
18445 /*is_cv_qualifier=*/NULL);
18447 parser->num_template_parameter_lists
18448 = saved_num_template_parameter_lists;
18450 /* Leave the scope of the class. */
18452 pop_scope (pushed_scope);
18454 constructor_p = !cp_parser_error_occurred (parser);
18458 constructor_p = false;
18459 /* We did not really want to consume any tokens. */
18460 cp_parser_abort_tentative_parse (parser);
18462 return constructor_p;
18465 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18466 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18467 they must be performed once we are in the scope of the function.
18469 Returns the function defined. */
18472 cp_parser_function_definition_from_specifiers_and_declarator
18473 (cp_parser* parser,
18474 cp_decl_specifier_seq *decl_specifiers,
18476 const cp_declarator *declarator)
18481 /* Begin the function-definition. */
18482 success_p = start_function (decl_specifiers, declarator, attributes);
18484 /* The things we're about to see are not directly qualified by any
18485 template headers we've seen thus far. */
18486 reset_specialization ();
18488 /* If there were names looked up in the decl-specifier-seq that we
18489 did not check, check them now. We must wait until we are in the
18490 scope of the function to perform the checks, since the function
18491 might be a friend. */
18492 perform_deferred_access_checks ();
18496 /* Skip the entire function. */
18497 cp_parser_skip_to_end_of_block_or_statement (parser);
18498 fn = error_mark_node;
18500 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18502 /* Seen already, skip it. An error message has already been output. */
18503 cp_parser_skip_to_end_of_block_or_statement (parser);
18504 fn = current_function_decl;
18505 current_function_decl = NULL_TREE;
18506 /* If this is a function from a class, pop the nested class. */
18507 if (current_class_name)
18508 pop_nested_class ();
18511 fn = cp_parser_function_definition_after_declarator (parser,
18512 /*inline_p=*/false);
18517 /* Parse the part of a function-definition that follows the
18518 declarator. INLINE_P is TRUE iff this function is an inline
18519 function defined within a class-specifier.
18521 Returns the function defined. */
18524 cp_parser_function_definition_after_declarator (cp_parser* parser,
18528 bool ctor_initializer_p = false;
18529 bool saved_in_unbraced_linkage_specification_p;
18530 bool saved_in_function_body;
18531 unsigned saved_num_template_parameter_lists;
18534 saved_in_function_body = parser->in_function_body;
18535 parser->in_function_body = true;
18536 /* If the next token is `return', then the code may be trying to
18537 make use of the "named return value" extension that G++ used to
18539 token = cp_lexer_peek_token (parser->lexer);
18540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18542 /* Consume the `return' keyword. */
18543 cp_lexer_consume_token (parser->lexer);
18544 /* Look for the identifier that indicates what value is to be
18546 cp_parser_identifier (parser);
18547 /* Issue an error message. */
18548 error_at (token->location,
18549 "named return values are no longer supported");
18550 /* Skip tokens until we reach the start of the function body. */
18553 cp_token *token = cp_lexer_peek_token (parser->lexer);
18554 if (token->type == CPP_OPEN_BRACE
18555 || token->type == CPP_EOF
18556 || token->type == CPP_PRAGMA_EOL)
18558 cp_lexer_consume_token (parser->lexer);
18561 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18562 anything declared inside `f'. */
18563 saved_in_unbraced_linkage_specification_p
18564 = parser->in_unbraced_linkage_specification_p;
18565 parser->in_unbraced_linkage_specification_p = false;
18566 /* Inside the function, surrounding template-parameter-lists do not
18568 saved_num_template_parameter_lists
18569 = parser->num_template_parameter_lists;
18570 parser->num_template_parameter_lists = 0;
18572 start_lambda_scope (current_function_decl);
18574 /* If the next token is `try', then we are looking at a
18575 function-try-block. */
18576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18577 ctor_initializer_p = cp_parser_function_try_block (parser);
18578 /* A function-try-block includes the function-body, so we only do
18579 this next part if we're not processing a function-try-block. */
18582 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18584 finish_lambda_scope ();
18586 /* Finish the function. */
18587 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18588 (inline_p ? 2 : 0));
18589 /* Generate code for it, if necessary. */
18590 expand_or_defer_fn (fn);
18591 /* Restore the saved values. */
18592 parser->in_unbraced_linkage_specification_p
18593 = saved_in_unbraced_linkage_specification_p;
18594 parser->num_template_parameter_lists
18595 = saved_num_template_parameter_lists;
18596 parser->in_function_body = saved_in_function_body;
18601 /* Parse a template-declaration, assuming that the `export' (and
18602 `extern') keywords, if present, has already been scanned. MEMBER_P
18603 is as for cp_parser_template_declaration. */
18606 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18608 tree decl = NULL_TREE;
18609 VEC (deferred_access_check,gc) *checks;
18610 tree parameter_list;
18611 bool friend_p = false;
18612 bool need_lang_pop;
18615 /* Look for the `template' keyword. */
18616 token = cp_lexer_peek_token (parser->lexer);
18617 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18621 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18623 if (at_class_scope_p () && current_function_decl)
18625 /* 14.5.2.2 [temp.mem]
18627 A local class shall not have member templates. */
18628 error_at (token->location,
18629 "invalid declaration of member template in local class");
18630 cp_parser_skip_to_end_of_block_or_statement (parser);
18635 A template ... shall not have C linkage. */
18636 if (current_lang_name == lang_name_c)
18638 error_at (token->location, "template with C linkage");
18639 /* Give it C++ linkage to avoid confusing other parts of the
18641 push_lang_context (lang_name_cplusplus);
18642 need_lang_pop = true;
18645 need_lang_pop = false;
18647 /* We cannot perform access checks on the template parameter
18648 declarations until we know what is being declared, just as we
18649 cannot check the decl-specifier list. */
18650 push_deferring_access_checks (dk_deferred);
18652 /* If the next token is `>', then we have an invalid
18653 specialization. Rather than complain about an invalid template
18654 parameter, issue an error message here. */
18655 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18657 cp_parser_error (parser, "invalid explicit specialization");
18658 begin_specialization ();
18659 parameter_list = NULL_TREE;
18662 /* Parse the template parameters. */
18663 parameter_list = cp_parser_template_parameter_list (parser);
18665 /* Get the deferred access checks from the parameter list. These
18666 will be checked once we know what is being declared, as for a
18667 member template the checks must be performed in the scope of the
18668 class containing the member. */
18669 checks = get_deferred_access_checks ();
18671 /* Look for the `>'. */
18672 cp_parser_skip_to_end_of_template_parameter_list (parser);
18673 /* We just processed one more parameter list. */
18674 ++parser->num_template_parameter_lists;
18675 /* If the next token is `template', there are more template
18677 if (cp_lexer_next_token_is_keyword (parser->lexer,
18679 cp_parser_template_declaration_after_export (parser, member_p);
18682 /* There are no access checks when parsing a template, as we do not
18683 know if a specialization will be a friend. */
18684 push_deferring_access_checks (dk_no_check);
18685 token = cp_lexer_peek_token (parser->lexer);
18686 decl = cp_parser_single_declaration (parser,
18689 /*explicit_specialization_p=*/false,
18691 pop_deferring_access_checks ();
18693 /* If this is a member template declaration, let the front
18695 if (member_p && !friend_p && decl)
18697 if (TREE_CODE (decl) == TYPE_DECL)
18698 cp_parser_check_access_in_redeclaration (decl, token->location);
18700 decl = finish_member_template_decl (decl);
18702 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18703 make_friend_class (current_class_type, TREE_TYPE (decl),
18704 /*complain=*/true);
18706 /* We are done with the current parameter list. */
18707 --parser->num_template_parameter_lists;
18709 pop_deferring_access_checks ();
18712 finish_template_decl (parameter_list);
18714 /* Register member declarations. */
18715 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18716 finish_member_declaration (decl);
18717 /* For the erroneous case of a template with C linkage, we pushed an
18718 implicit C++ linkage scope; exit that scope now. */
18720 pop_lang_context ();
18721 /* If DECL is a function template, we must return to parse it later.
18722 (Even though there is no definition, there might be default
18723 arguments that need handling.) */
18724 if (member_p && decl
18725 && (TREE_CODE (decl) == FUNCTION_DECL
18726 || DECL_FUNCTION_TEMPLATE_P (decl)))
18727 TREE_VALUE (parser->unparsed_functions_queues)
18728 = tree_cons (NULL_TREE, decl,
18729 TREE_VALUE (parser->unparsed_functions_queues));
18732 /* Perform the deferred access checks from a template-parameter-list.
18733 CHECKS is a TREE_LIST of access checks, as returned by
18734 get_deferred_access_checks. */
18737 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18739 ++processing_template_parmlist;
18740 perform_access_checks (checks);
18741 --processing_template_parmlist;
18744 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18745 `function-definition' sequence. MEMBER_P is true, this declaration
18746 appears in a class scope.
18748 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18749 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18752 cp_parser_single_declaration (cp_parser* parser,
18753 VEC (deferred_access_check,gc)* checks,
18755 bool explicit_specialization_p,
18758 int declares_class_or_enum;
18759 tree decl = NULL_TREE;
18760 cp_decl_specifier_seq decl_specifiers;
18761 bool function_definition_p = false;
18762 cp_token *decl_spec_token_start;
18764 /* This function is only used when processing a template
18766 gcc_assert (innermost_scope_kind () == sk_template_parms
18767 || innermost_scope_kind () == sk_template_spec);
18769 /* Defer access checks until we know what is being declared. */
18770 push_deferring_access_checks (dk_deferred);
18772 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18774 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18775 cp_parser_decl_specifier_seq (parser,
18776 CP_PARSER_FLAGS_OPTIONAL,
18778 &declares_class_or_enum);
18780 *friend_p = cp_parser_friend_p (&decl_specifiers);
18782 /* There are no template typedefs. */
18783 if (decl_specifiers.specs[(int) ds_typedef])
18785 error_at (decl_spec_token_start->location,
18786 "template declaration of %<typedef%>");
18787 decl = error_mark_node;
18790 /* Gather up the access checks that occurred the
18791 decl-specifier-seq. */
18792 stop_deferring_access_checks ();
18794 /* Check for the declaration of a template class. */
18795 if (declares_class_or_enum)
18797 if (cp_parser_declares_only_class_p (parser))
18799 decl = shadow_tag (&decl_specifiers);
18804 friend template <typename T> struct A<T>::B;
18807 A<T>::B will be represented by a TYPENAME_TYPE, and
18808 therefore not recognized by shadow_tag. */
18809 if (friend_p && *friend_p
18811 && decl_specifiers.type
18812 && TYPE_P (decl_specifiers.type))
18813 decl = decl_specifiers.type;
18815 if (decl && decl != error_mark_node)
18816 decl = TYPE_NAME (decl);
18818 decl = error_mark_node;
18820 /* Perform access checks for template parameters. */
18821 cp_parser_perform_template_parameter_access_checks (checks);
18825 /* Complain about missing 'typename' or other invalid type names. */
18826 if (!decl_specifiers.any_type_specifiers_p)
18827 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18829 /* If it's not a template class, try for a template function. If
18830 the next token is a `;', then this declaration does not declare
18831 anything. But, if there were errors in the decl-specifiers, then
18832 the error might well have come from an attempted class-specifier.
18833 In that case, there's no need to warn about a missing declarator. */
18835 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18836 || decl_specifiers.type != error_mark_node))
18838 decl = cp_parser_init_declarator (parser,
18841 /*function_definition_allowed_p=*/true,
18843 declares_class_or_enum,
18844 &function_definition_p);
18846 /* 7.1.1-1 [dcl.stc]
18848 A storage-class-specifier shall not be specified in an explicit
18849 specialization... */
18851 && explicit_specialization_p
18852 && decl_specifiers.storage_class != sc_none)
18854 error_at (decl_spec_token_start->location,
18855 "explicit template specialization cannot have a storage class");
18856 decl = error_mark_node;
18860 pop_deferring_access_checks ();
18862 /* Clear any current qualification; whatever comes next is the start
18863 of something new. */
18864 parser->scope = NULL_TREE;
18865 parser->qualifying_scope = NULL_TREE;
18866 parser->object_scope = NULL_TREE;
18867 /* Look for a trailing `;' after the declaration. */
18868 if (!function_definition_p
18869 && (decl == error_mark_node
18870 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18871 cp_parser_skip_to_end_of_block_or_statement (parser);
18876 /* Parse a cast-expression that is not the operand of a unary "&". */
18879 cp_parser_simple_cast_expression (cp_parser *parser)
18881 return cp_parser_cast_expression (parser, /*address_p=*/false,
18882 /*cast_p=*/false, NULL);
18885 /* Parse a functional cast to TYPE. Returns an expression
18886 representing the cast. */
18889 cp_parser_functional_cast (cp_parser* parser, tree type)
18892 tree expression_list;
18896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18898 maybe_warn_cpp0x ("extended initializer lists");
18899 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18900 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18901 if (TREE_CODE (type) == TYPE_DECL)
18902 type = TREE_TYPE (type);
18903 return finish_compound_literal (type, expression_list);
18907 vec = cp_parser_parenthesized_expression_list (parser, false,
18909 /*allow_expansion_p=*/true,
18910 /*non_constant_p=*/NULL);
18912 expression_list = error_mark_node;
18915 expression_list = build_tree_list_vec (vec);
18916 release_tree_vector (vec);
18919 cast = build_functional_cast (type, expression_list,
18920 tf_warning_or_error);
18921 /* [expr.const]/1: In an integral constant expression "only type
18922 conversions to integral or enumeration type can be used". */
18923 if (TREE_CODE (type) == TYPE_DECL)
18924 type = TREE_TYPE (type);
18925 if (cast != error_mark_node
18926 && !cast_valid_in_integral_constant_expression_p (type)
18927 && (cp_parser_non_integral_constant_expression
18928 (parser, "a call to a constructor")))
18929 return error_mark_node;
18933 /* Save the tokens that make up the body of a member function defined
18934 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18935 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18936 specifiers applied to the declaration. Returns the FUNCTION_DECL
18937 for the member function. */
18940 cp_parser_save_member_function_body (cp_parser* parser,
18941 cp_decl_specifier_seq *decl_specifiers,
18942 cp_declarator *declarator,
18949 /* Create the FUNCTION_DECL. */
18950 fn = grokmethod (decl_specifiers, declarator, attributes);
18951 /* If something went badly wrong, bail out now. */
18952 if (fn == error_mark_node)
18954 /* If there's a function-body, skip it. */
18955 if (cp_parser_token_starts_function_definition_p
18956 (cp_lexer_peek_token (parser->lexer)))
18957 cp_parser_skip_to_end_of_block_or_statement (parser);
18958 return error_mark_node;
18961 /* Remember it, if there default args to post process. */
18962 cp_parser_save_default_args (parser, fn);
18964 /* Save away the tokens that make up the body of the
18966 first = parser->lexer->next_token;
18967 /* We can have braced-init-list mem-initializers before the fn body. */
18968 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18970 cp_lexer_consume_token (parser->lexer);
18971 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18972 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18974 /* cache_group will stop after an un-nested { } pair, too. */
18975 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18978 /* variadic mem-inits have ... after the ')'. */
18979 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18980 cp_lexer_consume_token (parser->lexer);
18983 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18984 /* Handle function try blocks. */
18985 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18986 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18987 last = parser->lexer->next_token;
18989 /* Save away the inline definition; we will process it when the
18990 class is complete. */
18991 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18992 DECL_PENDING_INLINE_P (fn) = 1;
18994 /* We need to know that this was defined in the class, so that
18995 friend templates are handled correctly. */
18996 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18998 /* Add FN to the queue of functions to be parsed later. */
18999 TREE_VALUE (parser->unparsed_functions_queues)
19000 = tree_cons (NULL_TREE, fn,
19001 TREE_VALUE (parser->unparsed_functions_queues));
19006 /* Parse a template-argument-list, as well as the trailing ">" (but
19007 not the opening ">"). See cp_parser_template_argument_list for the
19011 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19015 tree saved_qualifying_scope;
19016 tree saved_object_scope;
19017 bool saved_greater_than_is_operator_p;
19018 int saved_unevaluated_operand;
19019 int saved_inhibit_evaluation_warnings;
19023 When parsing a template-id, the first non-nested `>' is taken as
19024 the end of the template-argument-list rather than a greater-than
19026 saved_greater_than_is_operator_p
19027 = parser->greater_than_is_operator_p;
19028 parser->greater_than_is_operator_p = false;
19029 /* Parsing the argument list may modify SCOPE, so we save it
19031 saved_scope = parser->scope;
19032 saved_qualifying_scope = parser->qualifying_scope;
19033 saved_object_scope = parser->object_scope;
19034 /* We need to evaluate the template arguments, even though this
19035 template-id may be nested within a "sizeof". */
19036 saved_unevaluated_operand = cp_unevaluated_operand;
19037 cp_unevaluated_operand = 0;
19038 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19039 c_inhibit_evaluation_warnings = 0;
19040 /* Parse the template-argument-list itself. */
19041 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19042 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19043 arguments = NULL_TREE;
19045 arguments = cp_parser_template_argument_list (parser);
19046 /* Look for the `>' that ends the template-argument-list. If we find
19047 a '>>' instead, it's probably just a typo. */
19048 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19050 if (cxx_dialect != cxx98)
19052 /* In C++0x, a `>>' in a template argument list or cast
19053 expression is considered to be two separate `>'
19054 tokens. So, change the current token to a `>', but don't
19055 consume it: it will be consumed later when the outer
19056 template argument list (or cast expression) is parsed.
19057 Note that this replacement of `>' for `>>' is necessary
19058 even if we are parsing tentatively: in the tentative
19059 case, after calling
19060 cp_parser_enclosed_template_argument_list we will always
19061 throw away all of the template arguments and the first
19062 closing `>', either because the template argument list
19063 was erroneous or because we are replacing those tokens
19064 with a CPP_TEMPLATE_ID token. The second `>' (which will
19065 not have been thrown away) is needed either to close an
19066 outer template argument list or to complete a new-style
19068 cp_token *token = cp_lexer_peek_token (parser->lexer);
19069 token->type = CPP_GREATER;
19071 else if (!saved_greater_than_is_operator_p)
19073 /* If we're in a nested template argument list, the '>>' has
19074 to be a typo for '> >'. We emit the error message, but we
19075 continue parsing and we push a '>' as next token, so that
19076 the argument list will be parsed correctly. Note that the
19077 global source location is still on the token before the
19078 '>>', so we need to say explicitly where we want it. */
19079 cp_token *token = cp_lexer_peek_token (parser->lexer);
19080 error_at (token->location, "%<>>%> should be %<> >%> "
19081 "within a nested template argument list");
19083 token->type = CPP_GREATER;
19087 /* If this is not a nested template argument list, the '>>'
19088 is a typo for '>'. Emit an error message and continue.
19089 Same deal about the token location, but here we can get it
19090 right by consuming the '>>' before issuing the diagnostic. */
19091 cp_token *token = cp_lexer_consume_token (parser->lexer);
19092 error_at (token->location,
19093 "spurious %<>>%>, use %<>%> to terminate "
19094 "a template argument list");
19098 cp_parser_skip_to_end_of_template_parameter_list (parser);
19099 /* The `>' token might be a greater-than operator again now. */
19100 parser->greater_than_is_operator_p
19101 = saved_greater_than_is_operator_p;
19102 /* Restore the SAVED_SCOPE. */
19103 parser->scope = saved_scope;
19104 parser->qualifying_scope = saved_qualifying_scope;
19105 parser->object_scope = saved_object_scope;
19106 cp_unevaluated_operand = saved_unevaluated_operand;
19107 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19112 /* MEMBER_FUNCTION is a member function, or a friend. If default
19113 arguments, or the body of the function have not yet been parsed,
19117 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19119 /* If this member is a template, get the underlying
19121 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19122 member_function = DECL_TEMPLATE_RESULT (member_function);
19124 /* There should not be any class definitions in progress at this
19125 point; the bodies of members are only parsed outside of all class
19127 gcc_assert (parser->num_classes_being_defined == 0);
19128 /* While we're parsing the member functions we might encounter more
19129 classes. We want to handle them right away, but we don't want
19130 them getting mixed up with functions that are currently in the
19132 parser->unparsed_functions_queues
19133 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19135 /* Make sure that any template parameters are in scope. */
19136 maybe_begin_member_template_processing (member_function);
19138 /* If the body of the function has not yet been parsed, parse it
19140 if (DECL_PENDING_INLINE_P (member_function))
19142 tree function_scope;
19143 cp_token_cache *tokens;
19145 /* The function is no longer pending; we are processing it. */
19146 tokens = DECL_PENDING_INLINE_INFO (member_function);
19147 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19148 DECL_PENDING_INLINE_P (member_function) = 0;
19150 /* If this is a local class, enter the scope of the containing
19152 function_scope = current_function_decl;
19153 if (function_scope)
19154 push_function_context ();
19156 /* Push the body of the function onto the lexer stack. */
19157 cp_parser_push_lexer_for_tokens (parser, tokens);
19159 /* Let the front end know that we going to be defining this
19161 start_preparsed_function (member_function, NULL_TREE,
19162 SF_PRE_PARSED | SF_INCLASS_INLINE);
19164 /* Don't do access checking if it is a templated function. */
19165 if (processing_template_decl)
19166 push_deferring_access_checks (dk_no_check);
19168 /* Now, parse the body of the function. */
19169 cp_parser_function_definition_after_declarator (parser,
19170 /*inline_p=*/true);
19172 if (processing_template_decl)
19173 pop_deferring_access_checks ();
19175 /* Leave the scope of the containing function. */
19176 if (function_scope)
19177 pop_function_context ();
19178 cp_parser_pop_lexer (parser);
19181 /* Remove any template parameters from the symbol table. */
19182 maybe_end_member_template_processing ();
19184 /* Restore the queue. */
19185 parser->unparsed_functions_queues
19186 = TREE_CHAIN (parser->unparsed_functions_queues);
19189 /* If DECL contains any default args, remember it on the unparsed
19190 functions queue. */
19193 cp_parser_save_default_args (cp_parser* parser, tree decl)
19197 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19199 probe = TREE_CHAIN (probe))
19200 if (TREE_PURPOSE (probe))
19202 TREE_PURPOSE (parser->unparsed_functions_queues)
19203 = tree_cons (current_class_type, decl,
19204 TREE_PURPOSE (parser->unparsed_functions_queues));
19209 /* FN is a FUNCTION_DECL which may contains a parameter with an
19210 unparsed DEFAULT_ARG. Parse the default args now. This function
19211 assumes that the current scope is the scope in which the default
19212 argument should be processed. */
19215 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19217 bool saved_local_variables_forbidden_p;
19218 tree parm, parmdecl;
19220 /* While we're parsing the default args, we might (due to the
19221 statement expression extension) encounter more classes. We want
19222 to handle them right away, but we don't want them getting mixed
19223 up with default args that are currently in the queue. */
19224 parser->unparsed_functions_queues
19225 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19227 /* Local variable names (and the `this' keyword) may not appear
19228 in a default argument. */
19229 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19230 parser->local_variables_forbidden_p = true;
19232 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19233 parmdecl = DECL_ARGUMENTS (fn);
19234 parm && parm != void_list_node;
19235 parm = TREE_CHAIN (parm),
19236 parmdecl = TREE_CHAIN (parmdecl))
19238 cp_token_cache *tokens;
19239 tree default_arg = TREE_PURPOSE (parm);
19241 VEC(tree,gc) *insts;
19248 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19249 /* This can happen for a friend declaration for a function
19250 already declared with default arguments. */
19253 /* Push the saved tokens for the default argument onto the parser's
19255 tokens = DEFARG_TOKENS (default_arg);
19256 cp_parser_push_lexer_for_tokens (parser, tokens);
19258 start_lambda_scope (parmdecl);
19260 /* Parse the assignment-expression. */
19261 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19262 if (parsed_arg == error_mark_node)
19264 cp_parser_pop_lexer (parser);
19268 if (!processing_template_decl)
19269 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19271 TREE_PURPOSE (parm) = parsed_arg;
19273 /* Update any instantiations we've already created. */
19274 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19275 VEC_iterate (tree, insts, ix, copy); ix++)
19276 TREE_PURPOSE (copy) = parsed_arg;
19278 finish_lambda_scope ();
19280 /* If the token stream has not been completely used up, then
19281 there was extra junk after the end of the default
19283 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19284 cp_parser_error (parser, "expected %<,%>");
19286 /* Revert to the main lexer. */
19287 cp_parser_pop_lexer (parser);
19290 /* Make sure no default arg is missing. */
19291 check_default_args (fn);
19293 /* Restore the state of local_variables_forbidden_p. */
19294 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19296 /* Restore the queue. */
19297 parser->unparsed_functions_queues
19298 = TREE_CHAIN (parser->unparsed_functions_queues);
19301 /* Parse the operand of `sizeof' (or a similar operator). Returns
19302 either a TYPE or an expression, depending on the form of the
19303 input. The KEYWORD indicates which kind of expression we have
19307 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19309 tree expr = NULL_TREE;
19310 const char *saved_message;
19312 bool saved_integral_constant_expression_p;
19313 bool saved_non_integral_constant_expression_p;
19314 bool pack_expansion_p = false;
19316 /* Types cannot be defined in a `sizeof' expression. Save away the
19318 saved_message = parser->type_definition_forbidden_message;
19319 /* And create the new one. */
19320 tmp = concat ("types may not be defined in %<",
19321 IDENTIFIER_POINTER (ridpointers[keyword]),
19322 "%> expressions", NULL);
19323 parser->type_definition_forbidden_message = tmp;
19325 /* The restrictions on constant-expressions do not apply inside
19326 sizeof expressions. */
19327 saved_integral_constant_expression_p
19328 = parser->integral_constant_expression_p;
19329 saved_non_integral_constant_expression_p
19330 = parser->non_integral_constant_expression_p;
19331 parser->integral_constant_expression_p = false;
19333 /* If it's a `...', then we are computing the length of a parameter
19335 if (keyword == RID_SIZEOF
19336 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19338 /* Consume the `...'. */
19339 cp_lexer_consume_token (parser->lexer);
19340 maybe_warn_variadic_templates ();
19342 /* Note that this is an expansion. */
19343 pack_expansion_p = true;
19346 /* Do not actually evaluate the expression. */
19347 ++cp_unevaluated_operand;
19348 ++c_inhibit_evaluation_warnings;
19349 /* If it's a `(', then we might be looking at the type-id
19351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19354 bool saved_in_type_id_in_expr_p;
19356 /* We can't be sure yet whether we're looking at a type-id or an
19358 cp_parser_parse_tentatively (parser);
19359 /* Consume the `('. */
19360 cp_lexer_consume_token (parser->lexer);
19361 /* Parse the type-id. */
19362 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19363 parser->in_type_id_in_expr_p = true;
19364 type = cp_parser_type_id (parser);
19365 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19366 /* Now, look for the trailing `)'. */
19367 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19368 /* If all went well, then we're done. */
19369 if (cp_parser_parse_definitely (parser))
19371 cp_decl_specifier_seq decl_specs;
19373 /* Build a trivial decl-specifier-seq. */
19374 clear_decl_specs (&decl_specs);
19375 decl_specs.type = type;
19377 /* Call grokdeclarator to figure out what type this is. */
19378 expr = grokdeclarator (NULL,
19382 /*attrlist=*/NULL);
19386 /* If the type-id production did not work out, then we must be
19387 looking at the unary-expression production. */
19389 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19390 /*cast_p=*/false, NULL);
19392 if (pack_expansion_p)
19393 /* Build a pack expansion. */
19394 expr = make_pack_expansion (expr);
19396 /* Go back to evaluating expressions. */
19397 --cp_unevaluated_operand;
19398 --c_inhibit_evaluation_warnings;
19400 /* Free the message we created. */
19402 /* And restore the old one. */
19403 parser->type_definition_forbidden_message = saved_message;
19404 parser->integral_constant_expression_p
19405 = saved_integral_constant_expression_p;
19406 parser->non_integral_constant_expression_p
19407 = saved_non_integral_constant_expression_p;
19412 /* If the current declaration has no declarator, return true. */
19415 cp_parser_declares_only_class_p (cp_parser *parser)
19417 /* If the next token is a `;' or a `,' then there is no
19419 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19420 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19423 /* Update the DECL_SPECS to reflect the storage class indicated by
19427 cp_parser_set_storage_class (cp_parser *parser,
19428 cp_decl_specifier_seq *decl_specs,
19430 location_t location)
19432 cp_storage_class storage_class;
19434 if (parser->in_unbraced_linkage_specification_p)
19436 error_at (location, "invalid use of %qD in linkage specification",
19437 ridpointers[keyword]);
19440 else if (decl_specs->storage_class != sc_none)
19442 decl_specs->conflicting_specifiers_p = true;
19446 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19447 && decl_specs->specs[(int) ds_thread])
19449 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19450 decl_specs->specs[(int) ds_thread] = 0;
19456 storage_class = sc_auto;
19459 storage_class = sc_register;
19462 storage_class = sc_static;
19465 storage_class = sc_extern;
19468 storage_class = sc_mutable;
19471 gcc_unreachable ();
19473 decl_specs->storage_class = storage_class;
19475 /* A storage class specifier cannot be applied alongside a typedef
19476 specifier. If there is a typedef specifier present then set
19477 conflicting_specifiers_p which will trigger an error later
19478 on in grokdeclarator. */
19479 if (decl_specs->specs[(int)ds_typedef])
19480 decl_specs->conflicting_specifiers_p = true;
19483 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19484 is true, the type is a user-defined type; otherwise it is a
19485 built-in type specified by a keyword. */
19488 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19490 location_t location,
19491 bool user_defined_p)
19493 decl_specs->any_specifiers_p = true;
19495 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19496 (with, for example, in "typedef int wchar_t;") we remember that
19497 this is what happened. In system headers, we ignore these
19498 declarations so that G++ can work with system headers that are not
19500 if (decl_specs->specs[(int) ds_typedef]
19502 && (type_spec == boolean_type_node
19503 || type_spec == char16_type_node
19504 || type_spec == char32_type_node
19505 || type_spec == wchar_type_node)
19506 && (decl_specs->type
19507 || decl_specs->specs[(int) ds_long]
19508 || decl_specs->specs[(int) ds_short]
19509 || decl_specs->specs[(int) ds_unsigned]
19510 || decl_specs->specs[(int) ds_signed]))
19512 decl_specs->redefined_builtin_type = type_spec;
19513 if (!decl_specs->type)
19515 decl_specs->type = type_spec;
19516 decl_specs->user_defined_type_p = false;
19517 decl_specs->type_location = location;
19520 else if (decl_specs->type)
19521 decl_specs->multiple_types_p = true;
19524 decl_specs->type = type_spec;
19525 decl_specs->user_defined_type_p = user_defined_p;
19526 decl_specs->redefined_builtin_type = NULL_TREE;
19527 decl_specs->type_location = location;
19531 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19532 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19535 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19537 return decl_specifiers->specs[(int) ds_friend] != 0;
19540 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19541 issue an error message indicating that TOKEN_DESC was expected.
19543 Returns the token consumed, if the token had the appropriate type.
19544 Otherwise, returns NULL. */
19547 cp_parser_require (cp_parser* parser,
19548 enum cpp_ttype type,
19549 const char* token_desc)
19551 if (cp_lexer_next_token_is (parser->lexer, type))
19552 return cp_lexer_consume_token (parser->lexer);
19555 /* Output the MESSAGE -- unless we're parsing tentatively. */
19556 if (!cp_parser_simulate_error (parser))
19558 char *message = concat ("expected ", token_desc, NULL);
19559 cp_parser_error (parser, message);
19566 /* An error message is produced if the next token is not '>'.
19567 All further tokens are skipped until the desired token is
19568 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19571 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19573 /* Current level of '< ... >'. */
19574 unsigned level = 0;
19575 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19576 unsigned nesting_depth = 0;
19578 /* Are we ready, yet? If not, issue error message. */
19579 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19582 /* Skip tokens until the desired token is found. */
19585 /* Peek at the next token. */
19586 switch (cp_lexer_peek_token (parser->lexer)->type)
19589 if (!nesting_depth)
19594 if (cxx_dialect == cxx98)
19595 /* C++0x views the `>>' operator as two `>' tokens, but
19598 else if (!nesting_depth && level-- == 0)
19600 /* We've hit a `>>' where the first `>' closes the
19601 template argument list, and the second `>' is
19602 spurious. Just consume the `>>' and stop; we've
19603 already produced at least one error. */
19604 cp_lexer_consume_token (parser->lexer);
19607 /* Fall through for C++0x, so we handle the second `>' in
19611 if (!nesting_depth && level-- == 0)
19613 /* We've reached the token we want, consume it and stop. */
19614 cp_lexer_consume_token (parser->lexer);
19619 case CPP_OPEN_PAREN:
19620 case CPP_OPEN_SQUARE:
19624 case CPP_CLOSE_PAREN:
19625 case CPP_CLOSE_SQUARE:
19626 if (nesting_depth-- == 0)
19631 case CPP_PRAGMA_EOL:
19632 case CPP_SEMICOLON:
19633 case CPP_OPEN_BRACE:
19634 case CPP_CLOSE_BRACE:
19635 /* The '>' was probably forgotten, don't look further. */
19642 /* Consume this token. */
19643 cp_lexer_consume_token (parser->lexer);
19647 /* If the next token is the indicated keyword, consume it. Otherwise,
19648 issue an error message indicating that TOKEN_DESC was expected.
19650 Returns the token consumed, if the token had the appropriate type.
19651 Otherwise, returns NULL. */
19654 cp_parser_require_keyword (cp_parser* parser,
19656 const char* token_desc)
19658 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19660 if (token && token->keyword != keyword)
19662 dyn_string_t error_msg;
19664 /* Format the error message. */
19665 error_msg = dyn_string_new (0);
19666 dyn_string_append_cstr (error_msg, "expected ");
19667 dyn_string_append_cstr (error_msg, token_desc);
19668 cp_parser_error (parser, error_msg->s);
19669 dyn_string_delete (error_msg);
19676 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19677 function-definition. */
19680 cp_parser_token_starts_function_definition_p (cp_token* token)
19682 return (/* An ordinary function-body begins with an `{'. */
19683 token->type == CPP_OPEN_BRACE
19684 /* A ctor-initializer begins with a `:'. */
19685 || token->type == CPP_COLON
19686 /* A function-try-block begins with `try'. */
19687 || token->keyword == RID_TRY
19688 /* The named return value extension begins with `return'. */
19689 || token->keyword == RID_RETURN);
19692 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19696 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19700 token = cp_lexer_peek_token (parser->lexer);
19701 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19704 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19705 C++0x) ending a template-argument. */
19708 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19712 token = cp_lexer_peek_token (parser->lexer);
19713 return (token->type == CPP_COMMA
19714 || token->type == CPP_GREATER
19715 || token->type == CPP_ELLIPSIS
19716 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19719 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19720 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19723 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19728 token = cp_lexer_peek_nth_token (parser->lexer, n);
19729 if (token->type == CPP_LESS)
19731 /* Check for the sequence `<::' in the original code. It would be lexed as
19732 `[:', where `[' is a digraph, and there is no whitespace before
19734 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19737 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19738 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19744 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19745 or none_type otherwise. */
19747 static enum tag_types
19748 cp_parser_token_is_class_key (cp_token* token)
19750 switch (token->keyword)
19755 return record_type;
19764 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19767 cp_parser_check_class_key (enum tag_types class_key, tree type)
19769 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19770 permerror (input_location, "%qs tag used in naming %q#T",
19771 class_key == union_type ? "union"
19772 : class_key == record_type ? "struct" : "class",
19776 /* Issue an error message if DECL is redeclared with different
19777 access than its original declaration [class.access.spec/3].
19778 This applies to nested classes and nested class templates.
19782 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19784 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19787 if ((TREE_PRIVATE (decl)
19788 != (current_access_specifier == access_private_node))
19789 || (TREE_PROTECTED (decl)
19790 != (current_access_specifier == access_protected_node)))
19791 error_at (location, "%qD redeclared with different access", decl);
19794 /* Look for the `template' keyword, as a syntactic disambiguator.
19795 Return TRUE iff it is present, in which case it will be
19799 cp_parser_optional_template_keyword (cp_parser *parser)
19801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19803 /* The `template' keyword can only be used within templates;
19804 outside templates the parser can always figure out what is a
19805 template and what is not. */
19806 if (!processing_template_decl)
19808 cp_token *token = cp_lexer_peek_token (parser->lexer);
19809 error_at (token->location,
19810 "%<template%> (as a disambiguator) is only allowed "
19811 "within templates");
19812 /* If this part of the token stream is rescanned, the same
19813 error message would be generated. So, we purge the token
19814 from the stream. */
19815 cp_lexer_purge_token (parser->lexer);
19820 /* Consume the `template' keyword. */
19821 cp_lexer_consume_token (parser->lexer);
19829 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19830 set PARSER->SCOPE, and perform other related actions. */
19833 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19836 struct tree_check *check_value;
19837 deferred_access_check *chk;
19838 VEC (deferred_access_check,gc) *checks;
19840 /* Get the stored value. */
19841 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19842 /* Perform any access checks that were deferred. */
19843 checks = check_value->checks;
19847 VEC_iterate (deferred_access_check, checks, i, chk) ;
19850 perform_or_defer_access_check (chk->binfo,
19855 /* Set the scope from the stored value. */
19856 parser->scope = check_value->value;
19857 parser->qualifying_scope = check_value->qualifying_scope;
19858 parser->object_scope = NULL_TREE;
19861 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19862 encounter the end of a block before what we were looking for. */
19865 cp_parser_cache_group (cp_parser *parser,
19866 enum cpp_ttype end,
19871 cp_token *token = cp_lexer_peek_token (parser->lexer);
19873 /* Abort a parenthesized expression if we encounter a semicolon. */
19874 if ((end == CPP_CLOSE_PAREN || depth == 0)
19875 && token->type == CPP_SEMICOLON)
19877 /* If we've reached the end of the file, stop. */
19878 if (token->type == CPP_EOF
19879 || (end != CPP_PRAGMA_EOL
19880 && token->type == CPP_PRAGMA_EOL))
19882 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19883 /* We've hit the end of an enclosing block, so there's been some
19884 kind of syntax error. */
19887 /* Consume the token. */
19888 cp_lexer_consume_token (parser->lexer);
19889 /* See if it starts a new group. */
19890 if (token->type == CPP_OPEN_BRACE)
19892 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19893 /* In theory this should probably check end == '}', but
19894 cp_parser_save_member_function_body needs it to exit
19895 after either '}' or ')' when called with ')'. */
19899 else if (token->type == CPP_OPEN_PAREN)
19901 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19902 if (depth == 0 && end == CPP_CLOSE_PAREN)
19905 else if (token->type == CPP_PRAGMA)
19906 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19907 else if (token->type == end)
19912 /* Begin parsing tentatively. We always save tokens while parsing
19913 tentatively so that if the tentative parsing fails we can restore the
19917 cp_parser_parse_tentatively (cp_parser* parser)
19919 /* Enter a new parsing context. */
19920 parser->context = cp_parser_context_new (parser->context);
19921 /* Begin saving tokens. */
19922 cp_lexer_save_tokens (parser->lexer);
19923 /* In order to avoid repetitive access control error messages,
19924 access checks are queued up until we are no longer parsing
19926 push_deferring_access_checks (dk_deferred);
19929 /* Commit to the currently active tentative parse. */
19932 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19934 cp_parser_context *context;
19937 /* Mark all of the levels as committed. */
19938 lexer = parser->lexer;
19939 for (context = parser->context; context->next; context = context->next)
19941 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19943 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19944 while (!cp_lexer_saving_tokens (lexer))
19945 lexer = lexer->next;
19946 cp_lexer_commit_tokens (lexer);
19950 /* Abort the currently active tentative parse. All consumed tokens
19951 will be rolled back, and no diagnostics will be issued. */
19954 cp_parser_abort_tentative_parse (cp_parser* parser)
19956 cp_parser_simulate_error (parser);
19957 /* Now, pretend that we want to see if the construct was
19958 successfully parsed. */
19959 cp_parser_parse_definitely (parser);
19962 /* Stop parsing tentatively. If a parse error has occurred, restore the
19963 token stream. Otherwise, commit to the tokens we have consumed.
19964 Returns true if no error occurred; false otherwise. */
19967 cp_parser_parse_definitely (cp_parser* parser)
19969 bool error_occurred;
19970 cp_parser_context *context;
19972 /* Remember whether or not an error occurred, since we are about to
19973 destroy that information. */
19974 error_occurred = cp_parser_error_occurred (parser);
19975 /* Remove the topmost context from the stack. */
19976 context = parser->context;
19977 parser->context = context->next;
19978 /* If no parse errors occurred, commit to the tentative parse. */
19979 if (!error_occurred)
19981 /* Commit to the tokens read tentatively, unless that was
19983 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19984 cp_lexer_commit_tokens (parser->lexer);
19986 pop_to_parent_deferring_access_checks ();
19988 /* Otherwise, if errors occurred, roll back our state so that things
19989 are just as they were before we began the tentative parse. */
19992 cp_lexer_rollback_tokens (parser->lexer);
19993 pop_deferring_access_checks ();
19995 /* Add the context to the front of the free list. */
19996 context->next = cp_parser_context_free_list;
19997 cp_parser_context_free_list = context;
19999 return !error_occurred;
20002 /* Returns true if we are parsing tentatively and are not committed to
20003 this tentative parse. */
20006 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20008 return (cp_parser_parsing_tentatively (parser)
20009 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20012 /* Returns nonzero iff an error has occurred during the most recent
20013 tentative parse. */
20016 cp_parser_error_occurred (cp_parser* parser)
20018 return (cp_parser_parsing_tentatively (parser)
20019 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20022 /* Returns nonzero if GNU extensions are allowed. */
20025 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20027 return parser->allow_gnu_extensions_p;
20030 /* Objective-C++ Productions */
20033 /* Parse an Objective-C expression, which feeds into a primary-expression
20037 objc-message-expression
20038 objc-string-literal
20039 objc-encode-expression
20040 objc-protocol-expression
20041 objc-selector-expression
20043 Returns a tree representation of the expression. */
20046 cp_parser_objc_expression (cp_parser* parser)
20048 /* Try to figure out what kind of declaration is present. */
20049 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20053 case CPP_OPEN_SQUARE:
20054 return cp_parser_objc_message_expression (parser);
20056 case CPP_OBJC_STRING:
20057 kwd = cp_lexer_consume_token (parser->lexer);
20058 return objc_build_string_object (kwd->u.value);
20061 switch (kwd->keyword)
20063 case RID_AT_ENCODE:
20064 return cp_parser_objc_encode_expression (parser);
20066 case RID_AT_PROTOCOL:
20067 return cp_parser_objc_protocol_expression (parser);
20069 case RID_AT_SELECTOR:
20070 return cp_parser_objc_selector_expression (parser);
20076 error_at (kwd->location,
20077 "misplaced %<@%D%> Objective-C++ construct",
20079 cp_parser_skip_to_end_of_block_or_statement (parser);
20082 return error_mark_node;
20085 /* Parse an Objective-C message expression.
20087 objc-message-expression:
20088 [ objc-message-receiver objc-message-args ]
20090 Returns a representation of an Objective-C message. */
20093 cp_parser_objc_message_expression (cp_parser* parser)
20095 tree receiver, messageargs;
20097 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20098 receiver = cp_parser_objc_message_receiver (parser);
20099 messageargs = cp_parser_objc_message_args (parser);
20100 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20102 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20105 /* Parse an objc-message-receiver.
20107 objc-message-receiver:
20109 simple-type-specifier
20111 Returns a representation of the type or expression. */
20114 cp_parser_objc_message_receiver (cp_parser* parser)
20118 /* An Objective-C message receiver may be either (1) a type
20119 or (2) an expression. */
20120 cp_parser_parse_tentatively (parser);
20121 rcv = cp_parser_expression (parser, false, NULL);
20123 if (cp_parser_parse_definitely (parser))
20126 rcv = cp_parser_simple_type_specifier (parser,
20127 /*decl_specs=*/NULL,
20128 CP_PARSER_FLAGS_NONE);
20130 return objc_get_class_reference (rcv);
20133 /* Parse the arguments and selectors comprising an Objective-C message.
20138 objc-selector-args , objc-comma-args
20140 objc-selector-args:
20141 objc-selector [opt] : assignment-expression
20142 objc-selector-args objc-selector [opt] : assignment-expression
20145 assignment-expression
20146 objc-comma-args , assignment-expression
20148 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20149 selector arguments and TREE_VALUE containing a list of comma
20153 cp_parser_objc_message_args (cp_parser* parser)
20155 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20156 bool maybe_unary_selector_p = true;
20157 cp_token *token = cp_lexer_peek_token (parser->lexer);
20159 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20161 tree selector = NULL_TREE, arg;
20163 if (token->type != CPP_COLON)
20164 selector = cp_parser_objc_selector (parser);
20166 /* Detect if we have a unary selector. */
20167 if (maybe_unary_selector_p
20168 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20169 return build_tree_list (selector, NULL_TREE);
20171 maybe_unary_selector_p = false;
20172 cp_parser_require (parser, CPP_COLON, "%<:%>");
20173 arg = cp_parser_assignment_expression (parser, false, NULL);
20176 = chainon (sel_args,
20177 build_tree_list (selector, arg));
20179 token = cp_lexer_peek_token (parser->lexer);
20182 /* Handle non-selector arguments, if any. */
20183 while (token->type == CPP_COMMA)
20187 cp_lexer_consume_token (parser->lexer);
20188 arg = cp_parser_assignment_expression (parser, false, NULL);
20191 = chainon (addl_args,
20192 build_tree_list (NULL_TREE, arg));
20194 token = cp_lexer_peek_token (parser->lexer);
20197 return build_tree_list (sel_args, addl_args);
20200 /* Parse an Objective-C encode expression.
20202 objc-encode-expression:
20203 @encode objc-typename
20205 Returns an encoded representation of the type argument. */
20208 cp_parser_objc_encode_expression (cp_parser* parser)
20213 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20214 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20215 token = cp_lexer_peek_token (parser->lexer);
20216 type = complete_type (cp_parser_type_id (parser));
20217 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20221 error_at (token->location,
20222 "%<@encode%> must specify a type as an argument");
20223 return error_mark_node;
20226 return objc_build_encode_expr (type);
20229 /* Parse an Objective-C @defs expression. */
20232 cp_parser_objc_defs_expression (cp_parser *parser)
20236 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20237 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20238 name = cp_parser_identifier (parser);
20239 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20241 return objc_get_class_ivars (name);
20244 /* Parse an Objective-C protocol expression.
20246 objc-protocol-expression:
20247 @protocol ( identifier )
20249 Returns a representation of the protocol expression. */
20252 cp_parser_objc_protocol_expression (cp_parser* parser)
20256 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20257 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20258 proto = cp_parser_identifier (parser);
20259 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20261 return objc_build_protocol_expr (proto);
20264 /* Parse an Objective-C selector expression.
20266 objc-selector-expression:
20267 @selector ( objc-method-signature )
20269 objc-method-signature:
20275 objc-selector-seq objc-selector :
20277 Returns a representation of the method selector. */
20280 cp_parser_objc_selector_expression (cp_parser* parser)
20282 tree sel_seq = NULL_TREE;
20283 bool maybe_unary_selector_p = true;
20285 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20287 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20288 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20289 token = cp_lexer_peek_token (parser->lexer);
20291 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20292 || token->type == CPP_SCOPE)
20294 tree selector = NULL_TREE;
20296 if (token->type != CPP_COLON
20297 || token->type == CPP_SCOPE)
20298 selector = cp_parser_objc_selector (parser);
20300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20301 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20303 /* Detect if we have a unary selector. */
20304 if (maybe_unary_selector_p)
20306 sel_seq = selector;
20307 goto finish_selector;
20311 cp_parser_error (parser, "expected %<:%>");
20314 maybe_unary_selector_p = false;
20315 token = cp_lexer_consume_token (parser->lexer);
20317 if (token->type == CPP_SCOPE)
20320 = chainon (sel_seq,
20321 build_tree_list (selector, NULL_TREE));
20323 = chainon (sel_seq,
20324 build_tree_list (NULL_TREE, NULL_TREE));
20328 = chainon (sel_seq,
20329 build_tree_list (selector, NULL_TREE));
20331 token = cp_lexer_peek_token (parser->lexer);
20335 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20337 return objc_build_selector_expr (loc, sel_seq);
20340 /* Parse a list of identifiers.
20342 objc-identifier-list:
20344 objc-identifier-list , identifier
20346 Returns a TREE_LIST of identifier nodes. */
20349 cp_parser_objc_identifier_list (cp_parser* parser)
20351 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20352 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20354 while (sep->type == CPP_COMMA)
20356 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20357 list = chainon (list,
20358 build_tree_list (NULL_TREE,
20359 cp_parser_identifier (parser)));
20360 sep = cp_lexer_peek_token (parser->lexer);
20366 /* Parse an Objective-C alias declaration.
20368 objc-alias-declaration:
20369 @compatibility_alias identifier identifier ;
20371 This function registers the alias mapping with the Objective-C front end.
20372 It returns nothing. */
20375 cp_parser_objc_alias_declaration (cp_parser* parser)
20379 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20380 alias = cp_parser_identifier (parser);
20381 orig = cp_parser_identifier (parser);
20382 objc_declare_alias (alias, orig);
20383 cp_parser_consume_semicolon_at_end_of_statement (parser);
20386 /* Parse an Objective-C class forward-declaration.
20388 objc-class-declaration:
20389 @class objc-identifier-list ;
20391 The function registers the forward declarations with the Objective-C
20392 front end. It returns nothing. */
20395 cp_parser_objc_class_declaration (cp_parser* parser)
20397 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20398 objc_declare_class (cp_parser_objc_identifier_list (parser));
20399 cp_parser_consume_semicolon_at_end_of_statement (parser);
20402 /* Parse a list of Objective-C protocol references.
20404 objc-protocol-refs-opt:
20405 objc-protocol-refs [opt]
20407 objc-protocol-refs:
20408 < objc-identifier-list >
20410 Returns a TREE_LIST of identifiers, if any. */
20413 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20415 tree protorefs = NULL_TREE;
20417 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20419 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20420 protorefs = cp_parser_objc_identifier_list (parser);
20421 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20427 /* Parse a Objective-C visibility specification. */
20430 cp_parser_objc_visibility_spec (cp_parser* parser)
20432 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20434 switch (vis->keyword)
20436 case RID_AT_PRIVATE:
20437 objc_set_visibility (2);
20439 case RID_AT_PROTECTED:
20440 objc_set_visibility (0);
20442 case RID_AT_PUBLIC:
20443 objc_set_visibility (1);
20449 /* Eat '@private'/'@protected'/'@public'. */
20450 cp_lexer_consume_token (parser->lexer);
20453 /* Parse an Objective-C method type. */
20456 cp_parser_objc_method_type (cp_parser* parser)
20458 objc_set_method_type
20459 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20464 /* Parse an Objective-C protocol qualifier. */
20467 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20469 tree quals = NULL_TREE, node;
20470 cp_token *token = cp_lexer_peek_token (parser->lexer);
20472 node = token->u.value;
20474 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20475 && (node == ridpointers [(int) RID_IN]
20476 || node == ridpointers [(int) RID_OUT]
20477 || node == ridpointers [(int) RID_INOUT]
20478 || node == ridpointers [(int) RID_BYCOPY]
20479 || node == ridpointers [(int) RID_BYREF]
20480 || node == ridpointers [(int) RID_ONEWAY]))
20482 quals = tree_cons (NULL_TREE, node, quals);
20483 cp_lexer_consume_token (parser->lexer);
20484 token = cp_lexer_peek_token (parser->lexer);
20485 node = token->u.value;
20491 /* Parse an Objective-C typename. */
20494 cp_parser_objc_typename (cp_parser* parser)
20496 tree type_name = NULL_TREE;
20498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20500 tree proto_quals, cp_type = NULL_TREE;
20502 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20503 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20505 /* An ObjC type name may consist of just protocol qualifiers, in which
20506 case the type shall default to 'id'. */
20507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20508 cp_type = cp_parser_type_id (parser);
20510 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20511 type_name = build_tree_list (proto_quals, cp_type);
20517 /* Check to see if TYPE refers to an Objective-C selector name. */
20520 cp_parser_objc_selector_p (enum cpp_ttype type)
20522 return (type == CPP_NAME || type == CPP_KEYWORD
20523 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20524 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20525 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20526 || type == CPP_XOR || type == CPP_XOR_EQ);
20529 /* Parse an Objective-C selector. */
20532 cp_parser_objc_selector (cp_parser* parser)
20534 cp_token *token = cp_lexer_consume_token (parser->lexer);
20536 if (!cp_parser_objc_selector_p (token->type))
20538 error_at (token->location, "invalid Objective-C++ selector name");
20539 return error_mark_node;
20542 /* C++ operator names are allowed to appear in ObjC selectors. */
20543 switch (token->type)
20545 case CPP_AND_AND: return get_identifier ("and");
20546 case CPP_AND_EQ: return get_identifier ("and_eq");
20547 case CPP_AND: return get_identifier ("bitand");
20548 case CPP_OR: return get_identifier ("bitor");
20549 case CPP_COMPL: return get_identifier ("compl");
20550 case CPP_NOT: return get_identifier ("not");
20551 case CPP_NOT_EQ: return get_identifier ("not_eq");
20552 case CPP_OR_OR: return get_identifier ("or");
20553 case CPP_OR_EQ: return get_identifier ("or_eq");
20554 case CPP_XOR: return get_identifier ("xor");
20555 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20556 default: return token->u.value;
20560 /* Parse an Objective-C params list. */
20563 cp_parser_objc_method_keyword_params (cp_parser* parser)
20565 tree params = NULL_TREE;
20566 bool maybe_unary_selector_p = true;
20567 cp_token *token = cp_lexer_peek_token (parser->lexer);
20569 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20571 tree selector = NULL_TREE, type_name, identifier;
20573 if (token->type != CPP_COLON)
20574 selector = cp_parser_objc_selector (parser);
20576 /* Detect if we have a unary selector. */
20577 if (maybe_unary_selector_p
20578 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20581 maybe_unary_selector_p = false;
20582 cp_parser_require (parser, CPP_COLON, "%<:%>");
20583 type_name = cp_parser_objc_typename (parser);
20584 identifier = cp_parser_identifier (parser);
20588 objc_build_keyword_decl (selector,
20592 token = cp_lexer_peek_token (parser->lexer);
20598 /* Parse the non-keyword Objective-C params. */
20601 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20603 tree params = make_node (TREE_LIST);
20604 cp_token *token = cp_lexer_peek_token (parser->lexer);
20605 *ellipsisp = false; /* Initially, assume no ellipsis. */
20607 while (token->type == CPP_COMMA)
20609 cp_parameter_declarator *parmdecl;
20612 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20613 token = cp_lexer_peek_token (parser->lexer);
20615 if (token->type == CPP_ELLIPSIS)
20617 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20622 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20623 parm = grokdeclarator (parmdecl->declarator,
20624 &parmdecl->decl_specifiers,
20625 PARM, /*initialized=*/0,
20626 /*attrlist=*/NULL);
20628 chainon (params, build_tree_list (NULL_TREE, parm));
20629 token = cp_lexer_peek_token (parser->lexer);
20635 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20638 cp_parser_objc_interstitial_code (cp_parser* parser)
20640 cp_token *token = cp_lexer_peek_token (parser->lexer);
20642 /* If the next token is `extern' and the following token is a string
20643 literal, then we have a linkage specification. */
20644 if (token->keyword == RID_EXTERN
20645 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20646 cp_parser_linkage_specification (parser);
20647 /* Handle #pragma, if any. */
20648 else if (token->type == CPP_PRAGMA)
20649 cp_parser_pragma (parser, pragma_external);
20650 /* Allow stray semicolons. */
20651 else if (token->type == CPP_SEMICOLON)
20652 cp_lexer_consume_token (parser->lexer);
20653 /* Finally, try to parse a block-declaration, or a function-definition. */
20655 cp_parser_block_declaration (parser, /*statement_p=*/false);
20658 /* Parse a method signature. */
20661 cp_parser_objc_method_signature (cp_parser* parser)
20663 tree rettype, kwdparms, optparms;
20664 bool ellipsis = false;
20666 cp_parser_objc_method_type (parser);
20667 rettype = cp_parser_objc_typename (parser);
20668 kwdparms = cp_parser_objc_method_keyword_params (parser);
20669 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20671 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20674 /* Pars an Objective-C method prototype list. */
20677 cp_parser_objc_method_prototype_list (cp_parser* parser)
20679 cp_token *token = cp_lexer_peek_token (parser->lexer);
20681 while (token->keyword != RID_AT_END)
20683 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20685 objc_add_method_declaration
20686 (cp_parser_objc_method_signature (parser));
20687 cp_parser_consume_semicolon_at_end_of_statement (parser);
20690 /* Allow for interspersed non-ObjC++ code. */
20691 cp_parser_objc_interstitial_code (parser);
20693 token = cp_lexer_peek_token (parser->lexer);
20696 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20697 objc_finish_interface ();
20700 /* Parse an Objective-C method definition list. */
20703 cp_parser_objc_method_definition_list (cp_parser* parser)
20705 cp_token *token = cp_lexer_peek_token (parser->lexer);
20707 while (token->keyword != RID_AT_END)
20711 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20713 push_deferring_access_checks (dk_deferred);
20714 objc_start_method_definition
20715 (cp_parser_objc_method_signature (parser));
20717 /* For historical reasons, we accept an optional semicolon. */
20718 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20719 cp_lexer_consume_token (parser->lexer);
20721 perform_deferred_access_checks ();
20722 stop_deferring_access_checks ();
20723 meth = cp_parser_function_definition_after_declarator (parser,
20725 pop_deferring_access_checks ();
20726 objc_finish_method_definition (meth);
20729 /* Allow for interspersed non-ObjC++ code. */
20730 cp_parser_objc_interstitial_code (parser);
20732 token = cp_lexer_peek_token (parser->lexer);
20735 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20736 objc_finish_implementation ();
20739 /* Parse Objective-C ivars. */
20742 cp_parser_objc_class_ivars (cp_parser* parser)
20744 cp_token *token = cp_lexer_peek_token (parser->lexer);
20746 if (token->type != CPP_OPEN_BRACE)
20747 return; /* No ivars specified. */
20749 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20750 token = cp_lexer_peek_token (parser->lexer);
20752 while (token->type != CPP_CLOSE_BRACE)
20754 cp_decl_specifier_seq declspecs;
20755 int decl_class_or_enum_p;
20756 tree prefix_attributes;
20758 cp_parser_objc_visibility_spec (parser);
20760 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20763 cp_parser_decl_specifier_seq (parser,
20764 CP_PARSER_FLAGS_OPTIONAL,
20766 &decl_class_or_enum_p);
20767 prefix_attributes = declspecs.attributes;
20768 declspecs.attributes = NULL_TREE;
20770 /* Keep going until we hit the `;' at the end of the
20772 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20774 tree width = NULL_TREE, attributes, first_attribute, decl;
20775 cp_declarator *declarator = NULL;
20776 int ctor_dtor_or_conv_p;
20778 /* Check for a (possibly unnamed) bitfield declaration. */
20779 token = cp_lexer_peek_token (parser->lexer);
20780 if (token->type == CPP_COLON)
20783 if (token->type == CPP_NAME
20784 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20787 /* Get the name of the bitfield. */
20788 declarator = make_id_declarator (NULL_TREE,
20789 cp_parser_identifier (parser),
20793 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20794 /* Get the width of the bitfield. */
20796 = cp_parser_constant_expression (parser,
20797 /*allow_non_constant=*/false,
20802 /* Parse the declarator. */
20804 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20805 &ctor_dtor_or_conv_p,
20806 /*parenthesized_p=*/NULL,
20807 /*member_p=*/false);
20810 /* Look for attributes that apply to the ivar. */
20811 attributes = cp_parser_attributes_opt (parser);
20812 /* Remember which attributes are prefix attributes and
20814 first_attribute = attributes;
20815 /* Combine the attributes. */
20816 attributes = chainon (prefix_attributes, attributes);
20819 /* Create the bitfield declaration. */
20820 decl = grokbitfield (declarator, &declspecs,
20824 decl = grokfield (declarator, &declspecs,
20825 NULL_TREE, /*init_const_expr_p=*/false,
20826 NULL_TREE, attributes);
20828 /* Add the instance variable. */
20829 objc_add_instance_variable (decl);
20831 /* Reset PREFIX_ATTRIBUTES. */
20832 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20833 attributes = TREE_CHAIN (attributes);
20835 TREE_CHAIN (attributes) = NULL_TREE;
20837 token = cp_lexer_peek_token (parser->lexer);
20839 if (token->type == CPP_COMMA)
20841 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20847 cp_parser_consume_semicolon_at_end_of_statement (parser);
20848 token = cp_lexer_peek_token (parser->lexer);
20851 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20852 /* For historical reasons, we accept an optional semicolon. */
20853 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20854 cp_lexer_consume_token (parser->lexer);
20857 /* Parse an Objective-C protocol declaration. */
20860 cp_parser_objc_protocol_declaration (cp_parser* parser)
20862 tree proto, protorefs;
20865 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20868 tok = cp_lexer_peek_token (parser->lexer);
20869 error_at (tok->location, "identifier expected after %<@protocol%>");
20873 /* See if we have a forward declaration or a definition. */
20874 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20876 /* Try a forward declaration first. */
20877 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20879 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20881 cp_parser_consume_semicolon_at_end_of_statement (parser);
20884 /* Ok, we got a full-fledged definition (or at least should). */
20887 proto = cp_parser_identifier (parser);
20888 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20889 objc_start_protocol (proto, protorefs);
20890 cp_parser_objc_method_prototype_list (parser);
20894 /* Parse an Objective-C superclass or category. */
20897 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20900 cp_token *next = cp_lexer_peek_token (parser->lexer);
20902 *super = *categ = NULL_TREE;
20903 if (next->type == CPP_COLON)
20905 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20906 *super = cp_parser_identifier (parser);
20908 else if (next->type == CPP_OPEN_PAREN)
20910 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20911 *categ = cp_parser_identifier (parser);
20912 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20916 /* Parse an Objective-C class interface. */
20919 cp_parser_objc_class_interface (cp_parser* parser)
20921 tree name, super, categ, protos;
20923 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20924 name = cp_parser_identifier (parser);
20925 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20926 protos = cp_parser_objc_protocol_refs_opt (parser);
20928 /* We have either a class or a category on our hands. */
20930 objc_start_category_interface (name, categ, protos);
20933 objc_start_class_interface (name, super, protos);
20934 /* Handle instance variable declarations, if any. */
20935 cp_parser_objc_class_ivars (parser);
20936 objc_continue_interface ();
20939 cp_parser_objc_method_prototype_list (parser);
20942 /* Parse an Objective-C class implementation. */
20945 cp_parser_objc_class_implementation (cp_parser* parser)
20947 tree name, super, categ;
20949 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20950 name = cp_parser_identifier (parser);
20951 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20953 /* We have either a class or a category on our hands. */
20955 objc_start_category_implementation (name, categ);
20958 objc_start_class_implementation (name, super);
20959 /* Handle instance variable declarations, if any. */
20960 cp_parser_objc_class_ivars (parser);
20961 objc_continue_implementation ();
20964 cp_parser_objc_method_definition_list (parser);
20967 /* Consume the @end token and finish off the implementation. */
20970 cp_parser_objc_end_implementation (cp_parser* parser)
20972 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20973 objc_finish_implementation ();
20976 /* Parse an Objective-C declaration. */
20979 cp_parser_objc_declaration (cp_parser* parser)
20981 /* Try to figure out what kind of declaration is present. */
20982 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20984 switch (kwd->keyword)
20987 cp_parser_objc_alias_declaration (parser);
20990 cp_parser_objc_class_declaration (parser);
20992 case RID_AT_PROTOCOL:
20993 cp_parser_objc_protocol_declaration (parser);
20995 case RID_AT_INTERFACE:
20996 cp_parser_objc_class_interface (parser);
20998 case RID_AT_IMPLEMENTATION:
20999 cp_parser_objc_class_implementation (parser);
21002 cp_parser_objc_end_implementation (parser);
21005 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21007 cp_parser_skip_to_end_of_block_or_statement (parser);
21011 /* Parse an Objective-C try-catch-finally statement.
21013 objc-try-catch-finally-stmt:
21014 @try compound-statement objc-catch-clause-seq [opt]
21015 objc-finally-clause [opt]
21017 objc-catch-clause-seq:
21018 objc-catch-clause objc-catch-clause-seq [opt]
21021 @catch ( exception-declaration ) compound-statement
21023 objc-finally-clause
21024 @finally compound-statement
21026 Returns NULL_TREE. */
21029 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21030 location_t location;
21033 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21034 location = cp_lexer_peek_token (parser->lexer)->location;
21035 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21036 node, lest it get absorbed into the surrounding block. */
21037 stmt = push_stmt_list ();
21038 cp_parser_compound_statement (parser, NULL, false);
21039 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21041 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21043 cp_parameter_declarator *parmdecl;
21046 cp_lexer_consume_token (parser->lexer);
21047 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21048 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21049 parm = grokdeclarator (parmdecl->declarator,
21050 &parmdecl->decl_specifiers,
21051 PARM, /*initialized=*/0,
21052 /*attrlist=*/NULL);
21053 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21054 objc_begin_catch_clause (parm);
21055 cp_parser_compound_statement (parser, NULL, false);
21056 objc_finish_catch_clause ();
21059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21061 cp_lexer_consume_token (parser->lexer);
21062 location = cp_lexer_peek_token (parser->lexer)->location;
21063 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21064 node, lest it get absorbed into the surrounding block. */
21065 stmt = push_stmt_list ();
21066 cp_parser_compound_statement (parser, NULL, false);
21067 objc_build_finally_clause (location, pop_stmt_list (stmt));
21070 return objc_finish_try_stmt ();
21073 /* Parse an Objective-C synchronized statement.
21075 objc-synchronized-stmt:
21076 @synchronized ( expression ) compound-statement
21078 Returns NULL_TREE. */
21081 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21082 location_t location;
21085 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21087 location = cp_lexer_peek_token (parser->lexer)->location;
21088 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21089 lock = cp_parser_expression (parser, false, NULL);
21090 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21092 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21093 node, lest it get absorbed into the surrounding block. */
21094 stmt = push_stmt_list ();
21095 cp_parser_compound_statement (parser, NULL, false);
21097 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21100 /* Parse an Objective-C throw statement.
21103 @throw assignment-expression [opt] ;
21105 Returns a constructed '@throw' statement. */
21108 cp_parser_objc_throw_statement (cp_parser *parser) {
21109 tree expr = NULL_TREE;
21110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21112 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21115 expr = cp_parser_assignment_expression (parser, false, NULL);
21117 cp_parser_consume_semicolon_at_end_of_statement (parser);
21119 return objc_build_throw_stmt (loc, expr);
21122 /* Parse an Objective-C statement. */
21125 cp_parser_objc_statement (cp_parser * parser) {
21126 /* Try to figure out what kind of declaration is present. */
21127 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21129 switch (kwd->keyword)
21132 return cp_parser_objc_try_catch_finally_statement (parser);
21133 case RID_AT_SYNCHRONIZED:
21134 return cp_parser_objc_synchronized_statement (parser);
21136 return cp_parser_objc_throw_statement (parser);
21138 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21140 cp_parser_skip_to_end_of_block_or_statement (parser);
21143 return error_mark_node;
21146 /* OpenMP 2.5 parsing routines. */
21148 /* Returns name of the next clause.
21149 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21150 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21151 returned and the token is consumed. */
21153 static pragma_omp_clause
21154 cp_parser_omp_clause_name (cp_parser *parser)
21156 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21159 result = PRAGMA_OMP_CLAUSE_IF;
21160 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21161 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21162 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21163 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21164 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21166 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21167 const char *p = IDENTIFIER_POINTER (id);
21172 if (!strcmp ("collapse", p))
21173 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21174 else if (!strcmp ("copyin", p))
21175 result = PRAGMA_OMP_CLAUSE_COPYIN;
21176 else if (!strcmp ("copyprivate", p))
21177 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21180 if (!strcmp ("firstprivate", p))
21181 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21184 if (!strcmp ("lastprivate", p))
21185 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21188 if (!strcmp ("nowait", p))
21189 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21190 else if (!strcmp ("num_threads", p))
21191 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21194 if (!strcmp ("ordered", p))
21195 result = PRAGMA_OMP_CLAUSE_ORDERED;
21198 if (!strcmp ("reduction", p))
21199 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21202 if (!strcmp ("schedule", p))
21203 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21204 else if (!strcmp ("shared", p))
21205 result = PRAGMA_OMP_CLAUSE_SHARED;
21208 if (!strcmp ("untied", p))
21209 result = PRAGMA_OMP_CLAUSE_UNTIED;
21214 if (result != PRAGMA_OMP_CLAUSE_NONE)
21215 cp_lexer_consume_token (parser->lexer);
21220 /* Validate that a clause of the given type does not already exist. */
21223 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21224 const char *name, location_t location)
21228 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21229 if (OMP_CLAUSE_CODE (c) == code)
21231 error_at (location, "too many %qs clauses", name);
21239 variable-list , identifier
21241 In addition, we match a closing parenthesis. An opening parenthesis
21242 will have been consumed by the caller.
21244 If KIND is nonzero, create the appropriate node and install the decl
21245 in OMP_CLAUSE_DECL and add the node to the head of the list.
21247 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21248 return the list created. */
21251 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21259 token = cp_lexer_peek_token (parser->lexer);
21260 name = cp_parser_id_expression (parser, /*template_p=*/false,
21261 /*check_dependency_p=*/true,
21262 /*template_p=*/NULL,
21263 /*declarator_p=*/false,
21264 /*optional_p=*/false);
21265 if (name == error_mark_node)
21268 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21269 if (decl == error_mark_node)
21270 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21271 else if (kind != 0)
21273 tree u = build_omp_clause (token->location, kind);
21274 OMP_CLAUSE_DECL (u) = decl;
21275 OMP_CLAUSE_CHAIN (u) = list;
21279 list = tree_cons (decl, NULL_TREE, list);
21282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21284 cp_lexer_consume_token (parser->lexer);
21287 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21291 /* Try to resync to an unnested comma. Copied from
21292 cp_parser_parenthesized_expression_list. */
21294 ending = cp_parser_skip_to_closing_parenthesis (parser,
21295 /*recovering=*/true,
21297 /*consume_paren=*/true);
21305 /* Similarly, but expect leading and trailing parenthesis. This is a very
21306 common case for omp clauses. */
21309 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21311 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21312 return cp_parser_omp_var_list_no_open (parser, kind, list);
21317 collapse ( constant-expression ) */
21320 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21326 loc = cp_lexer_peek_token (parser->lexer)->location;
21327 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21330 num = cp_parser_constant_expression (parser, false, NULL);
21332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21334 /*or_comma=*/false,
21335 /*consume_paren=*/true);
21337 if (num == error_mark_node)
21339 num = fold_non_dependent_expr (num);
21340 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21341 || !host_integerp (num, 0)
21342 || (n = tree_low_cst (num, 0)) <= 0
21345 error_at (loc, "collapse argument needs positive constant integer expression");
21349 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21350 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21351 OMP_CLAUSE_CHAIN (c) = list;
21352 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21358 default ( shared | none ) */
21361 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21363 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21366 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21368 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21370 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21371 const char *p = IDENTIFIER_POINTER (id);
21376 if (strcmp ("none", p) != 0)
21378 kind = OMP_CLAUSE_DEFAULT_NONE;
21382 if (strcmp ("shared", p) != 0)
21384 kind = OMP_CLAUSE_DEFAULT_SHARED;
21391 cp_lexer_consume_token (parser->lexer);
21396 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21399 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21400 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21401 /*or_comma=*/false,
21402 /*consume_paren=*/true);
21404 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21407 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21408 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21409 OMP_CLAUSE_CHAIN (c) = list;
21410 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21416 if ( expression ) */
21419 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21423 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21426 t = cp_parser_condition (parser);
21428 if (t == error_mark_node
21429 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21430 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21431 /*or_comma=*/false,
21432 /*consume_paren=*/true);
21434 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21436 c = build_omp_clause (location, OMP_CLAUSE_IF);
21437 OMP_CLAUSE_IF_EXPR (c) = t;
21438 OMP_CLAUSE_CHAIN (c) = list;
21447 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21448 tree list, location_t location)
21452 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21454 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21455 OMP_CLAUSE_CHAIN (c) = list;
21460 num_threads ( expression ) */
21463 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21464 location_t location)
21468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21471 t = cp_parser_expression (parser, false, NULL);
21473 if (t == error_mark_node
21474 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21475 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21476 /*or_comma=*/false,
21477 /*consume_paren=*/true);
21479 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21480 "num_threads", location);
21482 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21483 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21484 OMP_CLAUSE_CHAIN (c) = list;
21493 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21494 tree list, location_t location)
21498 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21499 "ordered", location);
21501 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21502 OMP_CLAUSE_CHAIN (c) = list;
21507 reduction ( reduction-operator : variable-list )
21509 reduction-operator:
21510 One of: + * - & ^ | && || */
21513 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21515 enum tree_code code;
21518 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21521 switch (cp_lexer_peek_token (parser->lexer)->type)
21533 code = BIT_AND_EXPR;
21536 code = BIT_XOR_EXPR;
21539 code = BIT_IOR_EXPR;
21542 code = TRUTH_ANDIF_EXPR;
21545 code = TRUTH_ORIF_EXPR;
21548 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21549 "%<|%>, %<&&%>, or %<||%>");
21551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21552 /*or_comma=*/false,
21553 /*consume_paren=*/true);
21556 cp_lexer_consume_token (parser->lexer);
21558 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21561 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21562 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21563 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21569 schedule ( schedule-kind )
21570 schedule ( schedule-kind , expression )
21573 static | dynamic | guided | runtime | auto */
21576 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21580 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21583 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21585 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21587 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21588 const char *p = IDENTIFIER_POINTER (id);
21593 if (strcmp ("dynamic", p) != 0)
21595 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21599 if (strcmp ("guided", p) != 0)
21601 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21605 if (strcmp ("runtime", p) != 0)
21607 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21614 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21615 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21616 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21617 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21620 cp_lexer_consume_token (parser->lexer);
21622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21625 cp_lexer_consume_token (parser->lexer);
21627 token = cp_lexer_peek_token (parser->lexer);
21628 t = cp_parser_assignment_expression (parser, false, NULL);
21630 if (t == error_mark_node)
21632 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21633 error_at (token->location, "schedule %<runtime%> does not take "
21634 "a %<chunk_size%> parameter");
21635 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21636 error_at (token->location, "schedule %<auto%> does not take "
21637 "a %<chunk_size%> parameter");
21639 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21641 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21644 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21647 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21648 OMP_CLAUSE_CHAIN (c) = list;
21652 cp_parser_error (parser, "invalid schedule kind");
21654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21655 /*or_comma=*/false,
21656 /*consume_paren=*/true);
21664 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21665 tree list, location_t location)
21669 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21671 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21672 OMP_CLAUSE_CHAIN (c) = list;
21676 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21677 is a bitmask in MASK. Return the list of clauses found; the result
21678 of clause default goes in *pdefault. */
21681 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21682 const char *where, cp_token *pragma_tok)
21684 tree clauses = NULL;
21686 cp_token *token = NULL;
21688 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21690 pragma_omp_clause c_kind;
21691 const char *c_name;
21692 tree prev = clauses;
21694 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21695 cp_lexer_consume_token (parser->lexer);
21697 token = cp_lexer_peek_token (parser->lexer);
21698 c_kind = cp_parser_omp_clause_name (parser);
21703 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21704 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21706 c_name = "collapse";
21708 case PRAGMA_OMP_CLAUSE_COPYIN:
21709 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21712 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21713 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21715 c_name = "copyprivate";
21717 case PRAGMA_OMP_CLAUSE_DEFAULT:
21718 clauses = cp_parser_omp_clause_default (parser, clauses,
21720 c_name = "default";
21722 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21723 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21725 c_name = "firstprivate";
21727 case PRAGMA_OMP_CLAUSE_IF:
21728 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21731 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21734 c_name = "lastprivate";
21736 case PRAGMA_OMP_CLAUSE_NOWAIT:
21737 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21740 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21741 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21743 c_name = "num_threads";
21745 case PRAGMA_OMP_CLAUSE_ORDERED:
21746 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21748 c_name = "ordered";
21750 case PRAGMA_OMP_CLAUSE_PRIVATE:
21751 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21753 c_name = "private";
21755 case PRAGMA_OMP_CLAUSE_REDUCTION:
21756 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21757 c_name = "reduction";
21759 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21760 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21762 c_name = "schedule";
21764 case PRAGMA_OMP_CLAUSE_SHARED:
21765 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21769 case PRAGMA_OMP_CLAUSE_UNTIED:
21770 clauses = cp_parser_omp_clause_untied (parser, clauses,
21775 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21779 if (((mask >> c_kind) & 1) == 0)
21781 /* Remove the invalid clause(s) from the list to avoid
21782 confusing the rest of the compiler. */
21784 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21789 return finish_omp_clauses (clauses);
21796 In practice, we're also interested in adding the statement to an
21797 outer node. So it is convenient if we work around the fact that
21798 cp_parser_statement calls add_stmt. */
21801 cp_parser_begin_omp_structured_block (cp_parser *parser)
21803 unsigned save = parser->in_statement;
21805 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21806 This preserves the "not within loop or switch" style error messages
21807 for nonsense cases like
21813 if (parser->in_statement)
21814 parser->in_statement = IN_OMP_BLOCK;
21820 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21822 parser->in_statement = save;
21826 cp_parser_omp_structured_block (cp_parser *parser)
21828 tree stmt = begin_omp_structured_block ();
21829 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21831 cp_parser_statement (parser, NULL_TREE, false, NULL);
21833 cp_parser_end_omp_structured_block (parser, save);
21834 return finish_omp_structured_block (stmt);
21838 # pragma omp atomic new-line
21842 x binop= expr | x++ | ++x | x-- | --x
21844 +, *, -, /, &, ^, |, <<, >>
21846 where x is an lvalue expression with scalar type. */
21849 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21852 enum tree_code code;
21854 cp_parser_require_pragma_eol (parser, pragma_tok);
21856 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21857 /*cast_p=*/false, NULL);
21858 switch (TREE_CODE (lhs))
21863 case PREINCREMENT_EXPR:
21864 case POSTINCREMENT_EXPR:
21865 lhs = TREE_OPERAND (lhs, 0);
21867 rhs = integer_one_node;
21870 case PREDECREMENT_EXPR:
21871 case POSTDECREMENT_EXPR:
21872 lhs = TREE_OPERAND (lhs, 0);
21874 rhs = integer_one_node;
21878 switch (cp_lexer_peek_token (parser->lexer)->type)
21884 code = TRUNC_DIV_EXPR;
21892 case CPP_LSHIFT_EQ:
21893 code = LSHIFT_EXPR;
21895 case CPP_RSHIFT_EQ:
21896 code = RSHIFT_EXPR;
21899 code = BIT_AND_EXPR;
21902 code = BIT_IOR_EXPR;
21905 code = BIT_XOR_EXPR;
21908 cp_parser_error (parser,
21909 "invalid operator for %<#pragma omp atomic%>");
21912 cp_lexer_consume_token (parser->lexer);
21914 rhs = cp_parser_expression (parser, false, NULL);
21915 if (rhs == error_mark_node)
21919 finish_omp_atomic (code, lhs, rhs);
21920 cp_parser_consume_semicolon_at_end_of_statement (parser);
21924 cp_parser_skip_to_end_of_block_or_statement (parser);
21929 # pragma omp barrier new-line */
21932 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21934 cp_parser_require_pragma_eol (parser, pragma_tok);
21935 finish_omp_barrier ();
21939 # pragma omp critical [(name)] new-line
21940 structured-block */
21943 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21945 tree stmt, name = NULL;
21947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21949 cp_lexer_consume_token (parser->lexer);
21951 name = cp_parser_identifier (parser);
21953 if (name == error_mark_node
21954 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21956 /*or_comma=*/false,
21957 /*consume_paren=*/true);
21958 if (name == error_mark_node)
21961 cp_parser_require_pragma_eol (parser, pragma_tok);
21963 stmt = cp_parser_omp_structured_block (parser);
21964 return c_finish_omp_critical (input_location, stmt, name);
21968 # pragma omp flush flush-vars[opt] new-line
21971 ( variable-list ) */
21974 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21977 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21978 cp_parser_require_pragma_eol (parser, pragma_tok);
21980 finish_omp_flush ();
21983 /* Helper function, to parse omp for increment expression. */
21986 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21988 tree cond = cp_parser_binary_expression (parser, false, true,
21989 PREC_NOT_OPERATOR, NULL);
21992 if (cond == error_mark_node
21993 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21995 cp_parser_skip_to_end_of_statement (parser);
21996 return error_mark_node;
21999 switch (TREE_CODE (cond))
22007 return error_mark_node;
22010 /* If decl is an iterator, preserve LHS and RHS of the relational
22011 expr until finish_omp_for. */
22013 && (type_dependent_expression_p (decl)
22014 || CLASS_TYPE_P (TREE_TYPE (decl))))
22017 return build_x_binary_op (TREE_CODE (cond),
22018 TREE_OPERAND (cond, 0), ERROR_MARK,
22019 TREE_OPERAND (cond, 1), ERROR_MARK,
22020 &overloaded_p, tf_warning_or_error);
22023 /* Helper function, to parse omp for increment expression. */
22026 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22028 cp_token *token = cp_lexer_peek_token (parser->lexer);
22034 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22036 op = (token->type == CPP_PLUS_PLUS
22037 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22038 cp_lexer_consume_token (parser->lexer);
22039 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22041 return error_mark_node;
22042 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22045 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22047 return error_mark_node;
22049 token = cp_lexer_peek_token (parser->lexer);
22050 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22052 op = (token->type == CPP_PLUS_PLUS
22053 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22054 cp_lexer_consume_token (parser->lexer);
22055 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22058 op = cp_parser_assignment_operator_opt (parser);
22059 if (op == ERROR_MARK)
22060 return error_mark_node;
22062 if (op != NOP_EXPR)
22064 rhs = cp_parser_assignment_expression (parser, false, NULL);
22065 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22066 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22069 lhs = cp_parser_binary_expression (parser, false, false,
22070 PREC_ADDITIVE_EXPRESSION, NULL);
22071 token = cp_lexer_peek_token (parser->lexer);
22072 decl_first = lhs == decl;
22075 if (token->type != CPP_PLUS
22076 && token->type != CPP_MINUS)
22077 return error_mark_node;
22081 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22082 cp_lexer_consume_token (parser->lexer);
22083 rhs = cp_parser_binary_expression (parser, false, false,
22084 PREC_ADDITIVE_EXPRESSION, NULL);
22085 token = cp_lexer_peek_token (parser->lexer);
22086 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22088 if (lhs == NULL_TREE)
22090 if (op == PLUS_EXPR)
22093 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22096 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22097 NULL, tf_warning_or_error);
22100 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22104 if (rhs != decl || op == MINUS_EXPR)
22105 return error_mark_node;
22106 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22109 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22111 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22114 /* Parse the restricted form of the for statement allowed by OpenMP. */
22117 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22119 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22120 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22121 tree this_pre_body, cl;
22122 location_t loc_first;
22123 bool collapse_err = false;
22124 int i, collapse = 1, nbraces = 0;
22126 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22127 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22128 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22130 gcc_assert (collapse >= 1);
22132 declv = make_tree_vec (collapse);
22133 initv = make_tree_vec (collapse);
22134 condv = make_tree_vec (collapse);
22135 incrv = make_tree_vec (collapse);
22137 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22139 for (i = 0; i < collapse; i++)
22141 int bracecount = 0;
22142 bool add_private_clause = false;
22145 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22147 cp_parser_error (parser, "for statement expected");
22150 loc = cp_lexer_consume_token (parser->lexer)->location;
22152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22155 init = decl = real_decl = NULL;
22156 this_pre_body = push_stmt_list ();
22157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22159 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22163 integer-type var = lb
22164 random-access-iterator-type var = lb
22165 pointer-type var = lb
22167 cp_decl_specifier_seq type_specifiers;
22169 /* First, try to parse as an initialized declaration. See
22170 cp_parser_condition, from whence the bulk of this is copied. */
22172 cp_parser_parse_tentatively (parser);
22173 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22174 /*is_trailing_return=*/false,
22176 if (cp_parser_parse_definitely (parser))
22178 /* If parsing a type specifier seq succeeded, then this
22179 MUST be a initialized declaration. */
22180 tree asm_specification, attributes;
22181 cp_declarator *declarator;
22183 declarator = cp_parser_declarator (parser,
22184 CP_PARSER_DECLARATOR_NAMED,
22185 /*ctor_dtor_or_conv_p=*/NULL,
22186 /*parenthesized_p=*/NULL,
22187 /*member_p=*/false);
22188 attributes = cp_parser_attributes_opt (parser);
22189 asm_specification = cp_parser_asm_specification_opt (parser);
22191 if (declarator == cp_error_declarator)
22192 cp_parser_skip_to_end_of_statement (parser);
22196 tree pushed_scope, auto_node;
22198 decl = start_decl (declarator, &type_specifiers,
22199 SD_INITIALIZED, attributes,
22200 /*prefix_attributes=*/NULL_TREE,
22203 auto_node = type_uses_auto (TREE_TYPE (decl));
22204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22206 if (cp_lexer_next_token_is (parser->lexer,
22208 error ("parenthesized initialization is not allowed in "
22209 "OpenMP %<for%> loop");
22211 /* Trigger an error. */
22212 cp_parser_require (parser, CPP_EQ, "%<=%>");
22214 init = error_mark_node;
22215 cp_parser_skip_to_end_of_statement (parser);
22217 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22218 || type_dependent_expression_p (decl)
22221 bool is_direct_init, is_non_constant_init;
22223 init = cp_parser_initializer (parser,
22225 &is_non_constant_init);
22227 if (auto_node && describable_type (init))
22230 = do_auto_deduction (TREE_TYPE (decl), init,
22233 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22234 && !type_dependent_expression_p (decl))
22238 cp_finish_decl (decl, init, !is_non_constant_init,
22240 LOOKUP_ONLYCONVERTING);
22241 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22244 = tree_cons (NULL, this_pre_body, for_block);
22248 init = pop_stmt_list (this_pre_body);
22249 this_pre_body = NULL_TREE;
22254 cp_lexer_consume_token (parser->lexer);
22255 init = cp_parser_assignment_expression (parser, false, NULL);
22258 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22259 init = error_mark_node;
22261 cp_finish_decl (decl, NULL_TREE,
22262 /*init_const_expr_p=*/false,
22264 LOOKUP_ONLYCONVERTING);
22268 pop_scope (pushed_scope);
22274 /* If parsing a type specifier sequence failed, then
22275 this MUST be a simple expression. */
22276 cp_parser_parse_tentatively (parser);
22277 decl = cp_parser_primary_expression (parser, false, false,
22279 if (!cp_parser_error_occurred (parser)
22282 && CLASS_TYPE_P (TREE_TYPE (decl)))
22286 cp_parser_parse_definitely (parser);
22287 cp_parser_require (parser, CPP_EQ, "%<=%>");
22288 rhs = cp_parser_assignment_expression (parser, false, NULL);
22289 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22291 tf_warning_or_error));
22292 add_private_clause = true;
22297 cp_parser_abort_tentative_parse (parser);
22298 init = cp_parser_expression (parser, false, NULL);
22301 if (TREE_CODE (init) == MODIFY_EXPR
22302 || TREE_CODE (init) == MODOP_EXPR)
22303 real_decl = TREE_OPERAND (init, 0);
22308 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22311 this_pre_body = pop_stmt_list (this_pre_body);
22315 pre_body = push_stmt_list ();
22317 add_stmt (this_pre_body);
22318 pre_body = pop_stmt_list (pre_body);
22321 pre_body = this_pre_body;
22326 if (par_clauses != NULL && real_decl != NULL_TREE)
22329 for (c = par_clauses; *c ; )
22330 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22331 && OMP_CLAUSE_DECL (*c) == real_decl)
22333 error_at (loc, "iteration variable %qD"
22334 " should not be firstprivate", real_decl);
22335 *c = OMP_CLAUSE_CHAIN (*c);
22337 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22338 && OMP_CLAUSE_DECL (*c) == real_decl)
22340 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22341 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22342 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22343 OMP_CLAUSE_DECL (l) = real_decl;
22344 OMP_CLAUSE_CHAIN (l) = clauses;
22345 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22347 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22348 CP_OMP_CLAUSE_INFO (*c) = NULL;
22349 add_private_clause = false;
22353 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22354 && OMP_CLAUSE_DECL (*c) == real_decl)
22355 add_private_clause = false;
22356 c = &OMP_CLAUSE_CHAIN (*c);
22360 if (add_private_clause)
22363 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22365 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22366 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22367 && OMP_CLAUSE_DECL (c) == decl)
22369 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22370 && OMP_CLAUSE_DECL (c) == decl)
22371 error_at (loc, "iteration variable %qD "
22372 "should not be firstprivate",
22374 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22375 && OMP_CLAUSE_DECL (c) == decl)
22376 error_at (loc, "iteration variable %qD should not be reduction",
22381 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22382 OMP_CLAUSE_DECL (c) = decl;
22383 c = finish_omp_clauses (c);
22386 OMP_CLAUSE_CHAIN (c) = clauses;
22393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22394 cond = cp_parser_omp_for_cond (parser, decl);
22395 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22400 /* If decl is an iterator, preserve the operator on decl
22401 until finish_omp_for. */
22403 && (type_dependent_expression_p (decl)
22404 || CLASS_TYPE_P (TREE_TYPE (decl))))
22405 incr = cp_parser_omp_for_incr (parser, decl);
22407 incr = cp_parser_expression (parser, false, NULL);
22410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22412 /*or_comma=*/false,
22413 /*consume_paren=*/true);
22415 TREE_VEC_ELT (declv, i) = decl;
22416 TREE_VEC_ELT (initv, i) = init;
22417 TREE_VEC_ELT (condv, i) = cond;
22418 TREE_VEC_ELT (incrv, i) = incr;
22420 if (i == collapse - 1)
22423 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22424 in between the collapsed for loops to be still considered perfectly
22425 nested. Hopefully the final version clarifies this.
22426 For now handle (multiple) {'s and empty statements. */
22427 cp_parser_parse_tentatively (parser);
22430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22432 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22434 cp_lexer_consume_token (parser->lexer);
22437 else if (bracecount
22438 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22439 cp_lexer_consume_token (parser->lexer);
22442 loc = cp_lexer_peek_token (parser->lexer)->location;
22443 error_at (loc, "not enough collapsed for loops");
22444 collapse_err = true;
22445 cp_parser_abort_tentative_parse (parser);
22454 cp_parser_parse_definitely (parser);
22455 nbraces += bracecount;
22459 /* Note that we saved the original contents of this flag when we entered
22460 the structured block, and so we don't need to re-save it here. */
22461 parser->in_statement = IN_OMP_FOR;
22463 /* Note that the grammar doesn't call for a structured block here,
22464 though the loop as a whole is a structured block. */
22465 body = push_stmt_list ();
22466 cp_parser_statement (parser, NULL_TREE, false, NULL);
22467 body = pop_stmt_list (body);
22469 if (declv == NULL_TREE)
22472 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22473 pre_body, clauses);
22477 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22479 cp_lexer_consume_token (parser->lexer);
22482 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22483 cp_lexer_consume_token (parser->lexer);
22488 error_at (cp_lexer_peek_token (parser->lexer)->location,
22489 "collapsed loops not perfectly nested");
22491 collapse_err = true;
22492 cp_parser_statement_seq_opt (parser, NULL);
22493 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22500 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22501 for_block = TREE_CHAIN (for_block);
22508 #pragma omp for for-clause[optseq] new-line
22511 #define OMP_FOR_CLAUSE_MASK \
22512 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22513 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22514 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22515 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22516 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22517 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22518 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22519 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22522 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22524 tree clauses, sb, ret;
22527 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22528 "#pragma omp for", pragma_tok);
22530 sb = begin_omp_structured_block ();
22531 save = cp_parser_begin_omp_structured_block (parser);
22533 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22535 cp_parser_end_omp_structured_block (parser, save);
22536 add_stmt (finish_omp_structured_block (sb));
22542 # pragma omp master new-line
22543 structured-block */
22546 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22548 cp_parser_require_pragma_eol (parser, pragma_tok);
22549 return c_finish_omp_master (input_location,
22550 cp_parser_omp_structured_block (parser));
22554 # pragma omp ordered new-line
22555 structured-block */
22558 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22560 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22561 cp_parser_require_pragma_eol (parser, pragma_tok);
22562 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22568 { section-sequence }
22571 section-directive[opt] structured-block
22572 section-sequence section-directive structured-block */
22575 cp_parser_omp_sections_scope (cp_parser *parser)
22577 tree stmt, substmt;
22578 bool error_suppress = false;
22581 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22584 stmt = push_stmt_list ();
22586 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22590 substmt = begin_omp_structured_block ();
22591 save = cp_parser_begin_omp_structured_block (parser);
22595 cp_parser_statement (parser, NULL_TREE, false, NULL);
22597 tok = cp_lexer_peek_token (parser->lexer);
22598 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22600 if (tok->type == CPP_CLOSE_BRACE)
22602 if (tok->type == CPP_EOF)
22606 cp_parser_end_omp_structured_block (parser, save);
22607 substmt = finish_omp_structured_block (substmt);
22608 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22609 add_stmt (substmt);
22614 tok = cp_lexer_peek_token (parser->lexer);
22615 if (tok->type == CPP_CLOSE_BRACE)
22617 if (tok->type == CPP_EOF)
22620 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22622 cp_lexer_consume_token (parser->lexer);
22623 cp_parser_require_pragma_eol (parser, tok);
22624 error_suppress = false;
22626 else if (!error_suppress)
22628 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22629 error_suppress = true;
22632 substmt = cp_parser_omp_structured_block (parser);
22633 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22634 add_stmt (substmt);
22636 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22638 substmt = pop_stmt_list (stmt);
22640 stmt = make_node (OMP_SECTIONS);
22641 TREE_TYPE (stmt) = void_type_node;
22642 OMP_SECTIONS_BODY (stmt) = substmt;
22649 # pragma omp sections sections-clause[optseq] newline
22652 #define OMP_SECTIONS_CLAUSE_MASK \
22653 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22654 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22655 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22656 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22657 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22660 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22664 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22665 "#pragma omp sections", pragma_tok);
22667 ret = cp_parser_omp_sections_scope (parser);
22669 OMP_SECTIONS_CLAUSES (ret) = clauses;
22675 # pragma parallel parallel-clause new-line
22676 # pragma parallel for parallel-for-clause new-line
22677 # pragma parallel sections parallel-sections-clause new-line */
22679 #define OMP_PARALLEL_CLAUSE_MASK \
22680 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22681 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22682 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22683 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22684 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22685 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22686 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22687 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22690 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22692 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22693 const char *p_name = "#pragma omp parallel";
22694 tree stmt, clauses, par_clause, ws_clause, block;
22695 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22697 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22701 cp_lexer_consume_token (parser->lexer);
22702 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22703 p_name = "#pragma omp parallel for";
22704 mask |= OMP_FOR_CLAUSE_MASK;
22705 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22707 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22709 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22710 const char *p = IDENTIFIER_POINTER (id);
22711 if (strcmp (p, "sections") == 0)
22713 cp_lexer_consume_token (parser->lexer);
22714 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22715 p_name = "#pragma omp parallel sections";
22716 mask |= OMP_SECTIONS_CLAUSE_MASK;
22717 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22721 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22722 block = begin_omp_parallel ();
22723 save = cp_parser_begin_omp_structured_block (parser);
22727 case PRAGMA_OMP_PARALLEL:
22728 cp_parser_statement (parser, NULL_TREE, false, NULL);
22729 par_clause = clauses;
22732 case PRAGMA_OMP_PARALLEL_FOR:
22733 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22734 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22737 case PRAGMA_OMP_PARALLEL_SECTIONS:
22738 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22739 stmt = cp_parser_omp_sections_scope (parser);
22741 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22745 gcc_unreachable ();
22748 cp_parser_end_omp_structured_block (parser, save);
22749 stmt = finish_omp_parallel (par_clause, block);
22750 if (p_kind != PRAGMA_OMP_PARALLEL)
22751 OMP_PARALLEL_COMBINED (stmt) = 1;
22756 # pragma omp single single-clause[optseq] new-line
22757 structured-block */
22759 #define OMP_SINGLE_CLAUSE_MASK \
22760 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22761 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22762 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22763 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22766 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22768 tree stmt = make_node (OMP_SINGLE);
22769 TREE_TYPE (stmt) = void_type_node;
22771 OMP_SINGLE_CLAUSES (stmt)
22772 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22773 "#pragma omp single", pragma_tok);
22774 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22776 return add_stmt (stmt);
22780 # pragma omp task task-clause[optseq] new-line
22781 structured-block */
22783 #define OMP_TASK_CLAUSE_MASK \
22784 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22785 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22786 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22787 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22788 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22789 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22792 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22794 tree clauses, block;
22797 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22798 "#pragma omp task", pragma_tok);
22799 block = begin_omp_task ();
22800 save = cp_parser_begin_omp_structured_block (parser);
22801 cp_parser_statement (parser, NULL_TREE, false, NULL);
22802 cp_parser_end_omp_structured_block (parser, save);
22803 return finish_omp_task (clauses, block);
22807 # pragma omp taskwait new-line */
22810 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22812 cp_parser_require_pragma_eol (parser, pragma_tok);
22813 finish_omp_taskwait ();
22817 # pragma omp threadprivate (variable-list) */
22820 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22824 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22825 cp_parser_require_pragma_eol (parser, pragma_tok);
22827 finish_omp_threadprivate (vars);
22830 /* Main entry point to OpenMP statement pragmas. */
22833 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22837 switch (pragma_tok->pragma_kind)
22839 case PRAGMA_OMP_ATOMIC:
22840 cp_parser_omp_atomic (parser, pragma_tok);
22842 case PRAGMA_OMP_CRITICAL:
22843 stmt = cp_parser_omp_critical (parser, pragma_tok);
22845 case PRAGMA_OMP_FOR:
22846 stmt = cp_parser_omp_for (parser, pragma_tok);
22848 case PRAGMA_OMP_MASTER:
22849 stmt = cp_parser_omp_master (parser, pragma_tok);
22851 case PRAGMA_OMP_ORDERED:
22852 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22854 case PRAGMA_OMP_PARALLEL:
22855 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22857 case PRAGMA_OMP_SECTIONS:
22858 stmt = cp_parser_omp_sections (parser, pragma_tok);
22860 case PRAGMA_OMP_SINGLE:
22861 stmt = cp_parser_omp_single (parser, pragma_tok);
22863 case PRAGMA_OMP_TASK:
22864 stmt = cp_parser_omp_task (parser, pragma_tok);
22867 gcc_unreachable ();
22871 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22876 static GTY (()) cp_parser *the_parser;
22879 /* Special handling for the first token or line in the file. The first
22880 thing in the file might be #pragma GCC pch_preprocess, which loads a
22881 PCH file, which is a GC collection point. So we need to handle this
22882 first pragma without benefit of an existing lexer structure.
22884 Always returns one token to the caller in *FIRST_TOKEN. This is
22885 either the true first token of the file, or the first token after
22886 the initial pragma. */
22889 cp_parser_initial_pragma (cp_token *first_token)
22893 cp_lexer_get_preprocessor_token (NULL, first_token);
22894 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22897 cp_lexer_get_preprocessor_token (NULL, first_token);
22898 if (first_token->type == CPP_STRING)
22900 name = first_token->u.value;
22902 cp_lexer_get_preprocessor_token (NULL, first_token);
22903 if (first_token->type != CPP_PRAGMA_EOL)
22904 error_at (first_token->location,
22905 "junk at end of %<#pragma GCC pch_preprocess%>");
22908 error_at (first_token->location, "expected string literal");
22910 /* Skip to the end of the pragma. */
22911 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22912 cp_lexer_get_preprocessor_token (NULL, first_token);
22914 /* Now actually load the PCH file. */
22916 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22918 /* Read one more token to return to our caller. We have to do this
22919 after reading the PCH file in, since its pointers have to be
22921 cp_lexer_get_preprocessor_token (NULL, first_token);
22924 /* Normal parsing of a pragma token. Here we can (and must) use the
22928 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22930 cp_token *pragma_tok;
22933 pragma_tok = cp_lexer_consume_token (parser->lexer);
22934 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22935 parser->lexer->in_pragma = true;
22937 id = pragma_tok->pragma_kind;
22940 case PRAGMA_GCC_PCH_PREPROCESS:
22941 error_at (pragma_tok->location,
22942 "%<#pragma GCC pch_preprocess%> must be first");
22945 case PRAGMA_OMP_BARRIER:
22948 case pragma_compound:
22949 cp_parser_omp_barrier (parser, pragma_tok);
22952 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22953 "used in compound statements");
22960 case PRAGMA_OMP_FLUSH:
22963 case pragma_compound:
22964 cp_parser_omp_flush (parser, pragma_tok);
22967 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22968 "used in compound statements");
22975 case PRAGMA_OMP_TASKWAIT:
22978 case pragma_compound:
22979 cp_parser_omp_taskwait (parser, pragma_tok);
22982 error_at (pragma_tok->location,
22983 "%<#pragma omp taskwait%> may only be "
22984 "used in compound statements");
22991 case PRAGMA_OMP_THREADPRIVATE:
22992 cp_parser_omp_threadprivate (parser, pragma_tok);
22995 case PRAGMA_OMP_ATOMIC:
22996 case PRAGMA_OMP_CRITICAL:
22997 case PRAGMA_OMP_FOR:
22998 case PRAGMA_OMP_MASTER:
22999 case PRAGMA_OMP_ORDERED:
23000 case PRAGMA_OMP_PARALLEL:
23001 case PRAGMA_OMP_SECTIONS:
23002 case PRAGMA_OMP_SINGLE:
23003 case PRAGMA_OMP_TASK:
23004 if (context == pragma_external)
23006 cp_parser_omp_construct (parser, pragma_tok);
23009 case PRAGMA_OMP_SECTION:
23010 error_at (pragma_tok->location,
23011 "%<#pragma omp section%> may only be used in "
23012 "%<#pragma omp sections%> construct");
23016 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23017 c_invoke_pragma_handler (id);
23021 cp_parser_error (parser, "expected declaration specifiers");
23025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23029 /* The interface the pragma parsers have to the lexer. */
23032 pragma_lex (tree *value)
23035 enum cpp_ttype ret;
23037 tok = cp_lexer_peek_token (the_parser->lexer);
23040 *value = tok->u.value;
23042 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23044 else if (ret == CPP_STRING)
23045 *value = cp_parser_string_literal (the_parser, false, false);
23048 cp_lexer_consume_token (the_parser->lexer);
23049 if (ret == CPP_KEYWORD)
23057 /* External interface. */
23059 /* Parse one entire translation unit. */
23062 c_parse_file (void)
23064 bool error_occurred;
23065 static bool already_called = false;
23067 if (already_called)
23069 sorry ("inter-module optimizations not implemented for C++");
23072 already_called = true;
23074 the_parser = cp_parser_new ();
23075 push_deferring_access_checks (flag_access_control
23076 ? dk_no_deferred : dk_no_check);
23077 error_occurred = cp_parser_translation_unit (the_parser);
23081 #include "gt-cp-parser.h"