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 tree_check GTY(())
53 /* The value associated with the token. */
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
64 typedef struct cp_token GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a context where it is implicitly extern "C" */
76 BOOL_BITFIELD implicit_extern_c : 1;
77 /* True for a CPP_NAME token that is not a keyword (i.e., for which
78 KEYWORD is RID_MAX) iff this name was looked up and found to be
79 ambiguous. An error has already been reported. */
80 BOOL_BITFIELD ambiguous_p : 1;
81 /* The location at which this token was found. */
83 /* The value associated with this token, if any. */
84 union cp_token_value {
85 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
86 struct tree_check* GTY((tag ("1"))) tree_check_value;
87 /* Use for all other tokens. */
88 tree GTY((tag ("0"))) value;
89 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* We use a stack of token pointer for saving token sets. */
93 typedef struct cp_token *cp_token_position;
94 DEF_VEC_P (cp_token_position);
95 DEF_VEC_ALLOC_P (cp_token_position,heap);
97 static cp_token eof_token =
99 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
158 static cp_lexer *cp_lexer_new_main
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
425 /* Update the value. Some keywords are mapped to particular
426 entities, rather than simply having the value of the
427 corresponding IDENTIFIER_NODE. For example, `__const' is
428 mapped to `const'. */
429 token->u.value = ridpointers[token->keyword];
433 if (warn_cxx0x_compat
434 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
435 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437 /* Warn about the C++0x keyword (but still treat it as
439 warning (OPT_Wc__0x_compat,
440 "identifier %<%s%> will become a keyword in C++0x",
441 IDENTIFIER_POINTER (token->u.value));
443 /* Clear out the C_RID_CODE so we don't warn about this
444 particular identifier-turned-keyword again. */
445 C_SET_RID_CODE (token->u.value, RID_MAX);
448 token->ambiguous_p = false;
449 token->keyword = RID_MAX;
452 /* Handle Objective-C++ keywords. */
453 else if (token->type == CPP_AT_NAME)
455 token->type = CPP_KEYWORD;
456 switch (C_RID_CODE (token->u.value))
458 /* Map 'class' to '@class', 'private' to '@private', etc. */
459 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
460 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
461 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
462 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
463 case RID_THROW: token->keyword = RID_AT_THROW; break;
464 case RID_TRY: token->keyword = RID_AT_TRY; break;
465 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
466 default: token->keyword = C_RID_CODE (token->u.value);
469 else if (token->type == CPP_PRAGMA)
471 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
472 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
473 token->u.value = NULL_TREE;
477 /* Update the globals input_location and the input file stack from TOKEN. */
479 cp_lexer_set_source_position_from_token (cp_token *token)
481 if (token->type != CPP_EOF)
483 input_location = token->location;
487 /* Return a pointer to the next token in the token stream, but do not
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
493 if (cp_lexer_debugging_p (lexer))
495 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
496 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
497 putc ('\n', cp_lexer_debug_stream);
499 return lexer->next_token;
502 /* Return true if the next token has the indicated TYPE. */
505 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 return cp_lexer_peek_token (lexer)->type == type;
510 /* Return true if the next token does not have the indicated TYPE. */
513 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 return !cp_lexer_next_token_is (lexer, type);
518 /* Return true if the next token is the indicated KEYWORD. */
521 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 return cp_lexer_peek_token (lexer)->keyword == keyword;
526 /* Return true if the next token is not the indicated KEYWORD. */
529 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 return cp_lexer_peek_token (lexer)->keyword != keyword;
534 /* Return true if the next token is a keyword for a decl-specifier. */
537 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
541 token = cp_lexer_peek_token (lexer);
542 switch (token->keyword)
544 /* auto specifier: storage-class-specifier in C++,
545 simple-type-specifier in C++0x. */
547 /* Storage classes. */
553 /* Elaborated type specifiers. */
559 /* Simple type specifiers. */
573 /* GNU extensions. */
576 /* C++0x extensions. */
585 /* Return a pointer to the Nth token in the token stream. If N is 1,
586 then this is precisely equivalent to cp_lexer_peek_token (except
587 that it is not inline). One would like to disallow that case, but
588 there is one case (cp_parser_nth_token_starts_template_id) where
589 the caller passes a variable for N and it might be 1. */
592 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
596 /* N is 1-based, not zero-based. */
599 if (cp_lexer_debugging_p (lexer))
600 fprintf (cp_lexer_debug_stream,
601 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604 token = lexer->next_token;
605 gcc_assert (!n || token != &eof_token);
609 if (token == lexer->last_token)
615 if (token->type != CPP_PURGED)
619 if (cp_lexer_debugging_p (lexer))
621 cp_lexer_print_token (cp_lexer_debug_stream, token);
622 putc ('\n', cp_lexer_debug_stream);
628 /* Return the next token, and advance the lexer's next_token pointer
629 to point to the next non-purged token. */
632 cp_lexer_consume_token (cp_lexer* lexer)
634 cp_token *token = lexer->next_token;
636 gcc_assert (token != &eof_token);
637 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
642 if (lexer->next_token == lexer->last_token)
644 lexer->next_token = &eof_token;
649 while (lexer->next_token->type == CPP_PURGED);
651 cp_lexer_set_source_position_from_token (token);
653 /* Provide debugging output. */
654 if (cp_lexer_debugging_p (lexer))
656 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
657 cp_lexer_print_token (cp_lexer_debug_stream, token);
658 putc ('\n', cp_lexer_debug_stream);
664 /* Permanently remove the next token from the token stream, and
665 advance the next_token pointer to refer to the next non-purged
669 cp_lexer_purge_token (cp_lexer *lexer)
671 cp_token *tok = lexer->next_token;
673 gcc_assert (tok != &eof_token);
674 tok->type = CPP_PURGED;
675 tok->location = UNKNOWN_LOCATION;
676 tok->u.value = NULL_TREE;
677 tok->keyword = RID_MAX;
682 if (tok == lexer->last_token)
688 while (tok->type == CPP_PURGED);
689 lexer->next_token = tok;
692 /* Permanently remove all tokens after TOK, up to, but not
693 including, the token that will be returned next by
694 cp_lexer_peek_token. */
697 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 cp_token *peek = lexer->next_token;
701 if (peek == &eof_token)
702 peek = lexer->last_token;
704 gcc_assert (tok < peek);
706 for ( tok += 1; tok != peek; tok += 1)
708 tok->type = CPP_PURGED;
709 tok->location = UNKNOWN_LOCATION;
710 tok->u.value = NULL_TREE;
711 tok->keyword = RID_MAX;
715 /* Begin saving tokens. All tokens consumed after this point will be
719 cp_lexer_save_tokens (cp_lexer* lexer)
721 /* Provide debugging output. */
722 if (cp_lexer_debugging_p (lexer))
723 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725 VEC_safe_push (cp_token_position, heap,
726 lexer->saved_tokens, lexer->next_token);
729 /* Commit to the portion of the token stream most recently saved. */
732 cp_lexer_commit_tokens (cp_lexer* lexer)
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738 VEC_pop (cp_token_position, lexer->saved_tokens);
741 /* Return all tokens saved since the last call to cp_lexer_save_tokens
742 to the token stream. Stop saving tokens. */
745 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 /* Provide debugging output. */
748 if (cp_lexer_debugging_p (lexer))
749 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 /* Print a representation of the TOKEN on the STREAM. */
756 #ifdef ENABLE_CHECKING
759 cp_lexer_print_token (FILE * stream, cp_token *token)
761 /* We don't use cpp_type2name here because the parser defines
762 a few tokens of its own. */
763 static const char *const token_names[] = {
764 /* cpplib-defined token types */
770 /* C++ parser token types - see "Manifest constants", above. */
773 "NESTED_NAME_SPECIFIER",
777 /* If we have a name for the token, print it out. Otherwise, we
778 simply give the numeric code. */
779 gcc_assert (token->type < ARRAY_SIZE(token_names));
780 fputs (token_names[token->type], stream);
782 /* For some tokens, print the associated data. */
786 /* Some keywords have a value that is not an IDENTIFIER_NODE.
787 For example, `struct' is mapped to an INTEGER_CST. */
788 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790 /* else fall through */
792 fputs (IDENTIFIER_POINTER (token->u.value), stream);
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
807 /* Start emitting debugging information. */
810 cp_lexer_start_debugging (cp_lexer* lexer)
812 lexer->debugging_p = true;
815 /* Stop emitting debugging information. */
818 cp_lexer_stop_debugging (cp_lexer* lexer)
820 lexer->debugging_p = false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, tree, cp_cv_quals, tree, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *, bool);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
870 /* The obstack on which declarators and related data structures are
872 static struct obstack declarator_obstack;
874 /* Alloc BYTES from the declarator memory pool. */
877 alloc_declarator (size_t bytes)
879 return obstack_alloc (&declarator_obstack, bytes);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
888 cp_declarator *declarator;
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
909 cp_declarator *declarator;
911 /* It is valid to write:
913 class C { void f(); };
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
955 declarator->parameter_pack_p = false;
960 /* Like make_pointer_declarator -- but for references. */
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
966 cp_declarator *declarator;
968 declarator = make_declarator (cdk_reference);
969 declarator->declarator = target;
970 declarator->u.reference.qualifiers = cv_qualifiers;
971 declarator->u.reference.rvalue_ref = rvalue_ref;
974 declarator->parameter_pack_p = target->parameter_pack_p;
975 target->parameter_pack_p = false;
978 declarator->parameter_pack_p = false;
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988 cp_declarator *pointee)
990 cp_declarator *declarator;
992 declarator = make_declarator (cdk_ptrmem);
993 declarator->declarator = pointee;
994 declarator->u.pointer.qualifiers = cv_qualifiers;
995 declarator->u.pointer.class_type = class_type;
999 declarator->parameter_pack_p = pointee->parameter_pack_p;
1000 pointee->parameter_pack_p = false;
1003 declarator->parameter_pack_p = false;
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1014 make_call_declarator (cp_declarator *target,
1016 cp_cv_quals cv_qualifiers,
1017 tree exception_specification,
1018 tree late_return_type)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1027 declarator->u.function.late_return_type = late_return_type;
1030 declarator->parameter_pack_p = target->parameter_pack_p;
1031 target->parameter_pack_p = false;
1034 declarator->parameter_pack_p = false;
1039 /* Make a declarator for an array of BOUNDS elements, each of which is
1040 defined by ELEMENT. */
1043 make_array_declarator (cp_declarator *element, tree bounds)
1045 cp_declarator *declarator;
1047 declarator = make_declarator (cdk_array);
1048 declarator->declarator = element;
1049 declarator->u.array.bounds = bounds;
1052 declarator->parameter_pack_p = element->parameter_pack_p;
1053 element->parameter_pack_p = false;
1056 declarator->parameter_pack_p = false;
1061 /* Determine whether the declarator we've seen so far can be a
1062 parameter pack, when followed by an ellipsis. */
1064 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 /* Search for a declarator name, or any other declarator that goes
1067 after the point where the ellipsis could appear in a parameter
1068 pack. If we find any of these, then this declarator can not be
1069 made into a parameter pack. */
1071 while (declarator && !found)
1073 switch ((int)declarator->kind)
1084 declarator = declarator->declarator;
1092 cp_parameter_declarator *no_parameters;
1094 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1095 DECLARATOR and DEFAULT_ARGUMENT. */
1097 cp_parameter_declarator *
1098 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1099 cp_declarator *declarator,
1100 tree default_argument)
1102 cp_parameter_declarator *parameter;
1104 parameter = ((cp_parameter_declarator *)
1105 alloc_declarator (sizeof (cp_parameter_declarator)));
1106 parameter->next = NULL;
1107 if (decl_specifiers)
1108 parameter->decl_specifiers = *decl_specifiers;
1110 clear_decl_specs (¶meter->decl_specifiers);
1111 parameter->declarator = declarator;
1112 parameter->default_argument = default_argument;
1113 parameter->ellipsis_p = false;
1118 /* Returns true iff DECLARATOR is a declaration for a function. */
1121 function_declarator_p (const cp_declarator *declarator)
1125 if (declarator->kind == cdk_function
1126 && declarator->declarator->kind == cdk_id)
1128 if (declarator->kind == cdk_id
1129 || declarator->kind == cdk_error)
1131 declarator = declarator->declarator;
1141 A cp_parser parses the token stream as specified by the C++
1142 grammar. Its job is purely parsing, not semantic analysis. For
1143 example, the parser breaks the token stream into declarators,
1144 expressions, statements, and other similar syntactic constructs.
1145 It does not check that the types of the expressions on either side
1146 of an assignment-statement are compatible, or that a function is
1147 not declared with a parameter of type `void'.
1149 The parser invokes routines elsewhere in the compiler to perform
1150 semantic analysis and to build up the abstract syntax tree for the
1153 The parser (and the template instantiation code, which is, in a
1154 way, a close relative of parsing) are the only parts of the
1155 compiler that should be calling push_scope and pop_scope, or
1156 related functions. The parser (and template instantiation code)
1157 keeps track of what scope is presently active; everything else
1158 should simply honor that. (The code that generates static
1159 initializers may also need to set the scope, in order to check
1160 access control correctly when emitting the initializers.)
1165 The parser is of the standard recursive-descent variety. Upcoming
1166 tokens in the token stream are examined in order to determine which
1167 production to use when parsing a non-terminal. Some C++ constructs
1168 require arbitrary look ahead to disambiguate. For example, it is
1169 impossible, in the general case, to tell whether a statement is an
1170 expression or declaration without scanning the entire statement.
1171 Therefore, the parser is capable of "parsing tentatively." When the
1172 parser is not sure what construct comes next, it enters this mode.
1173 Then, while we attempt to parse the construct, the parser queues up
1174 error messages, rather than issuing them immediately, and saves the
1175 tokens it consumes. If the construct is parsed successfully, the
1176 parser "commits", i.e., it issues any queued error messages and
1177 the tokens that were being preserved are permanently discarded.
1178 If, however, the construct is not parsed successfully, the parser
1179 rolls back its state completely so that it can resume parsing using
1180 a different alternative.
1185 The performance of the parser could probably be improved substantially.
1186 We could often eliminate the need to parse tentatively by looking ahead
1187 a little bit. In some places, this approach might not entirely eliminate
1188 the need to parse tentatively, but it might still speed up the average
1191 /* Flags that are passed to some parsing functions. These values can
1192 be bitwise-ored together. */
1194 typedef enum cp_parser_flags
1197 CP_PARSER_FLAGS_NONE = 0x0,
1198 /* The construct is optional. If it is not present, then no error
1199 should be issued. */
1200 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1201 /* When parsing a type-specifier, do not allow user-defined types. */
1202 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1205 /* The different kinds of declarators we want to parse. */
1207 typedef enum cp_parser_declarator_kind
1209 /* We want an abstract declarator. */
1210 CP_PARSER_DECLARATOR_ABSTRACT,
1211 /* We want a named declarator. */
1212 CP_PARSER_DECLARATOR_NAMED,
1213 /* We don't mind, but the name must be an unqualified-id. */
1214 CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1217 /* The precedence values used to parse binary expressions. The minimum value
1218 of PREC must be 1, because zero is reserved to quickly discriminate
1219 binary operators from other tokens. */
1224 PREC_LOGICAL_OR_EXPRESSION,
1225 PREC_LOGICAL_AND_EXPRESSION,
1226 PREC_INCLUSIVE_OR_EXPRESSION,
1227 PREC_EXCLUSIVE_OR_EXPRESSION,
1228 PREC_AND_EXPRESSION,
1229 PREC_EQUALITY_EXPRESSION,
1230 PREC_RELATIONAL_EXPRESSION,
1231 PREC_SHIFT_EXPRESSION,
1232 PREC_ADDITIVE_EXPRESSION,
1233 PREC_MULTIPLICATIVE_EXPRESSION,
1235 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239 precedence value. */
1241 typedef struct cp_parser_binary_operations_map_node
1243 /* The token type. */
1244 enum cpp_ttype token_type;
1245 /* The corresponding tree code. */
1246 enum tree_code tree_type;
1247 /* The precedence of this operator. */
1248 enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1251 /* The status of a tentative parse. */
1253 typedef enum cp_parser_status_kind
1255 /* No errors have occurred. */
1256 CP_PARSER_STATUS_KIND_NO_ERROR,
1257 /* An error has occurred. */
1258 CP_PARSER_STATUS_KIND_ERROR,
1259 /* We are committed to this tentative parse, whether or not an error
1261 CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1264 typedef struct cp_parser_expression_stack_entry
1266 /* Left hand side of the binary operation we are currently
1269 /* Original tree code for left hand side, if it was a binary
1270 expression itself (used for -Wparentheses). */
1271 enum tree_code lhs_type;
1272 /* Tree code for the binary operation we are parsing. */
1273 enum tree_code tree_type;
1274 /* Precedence of the binary operation we are parsing. */
1276 } cp_parser_expression_stack_entry;
1278 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1279 entries because precedence levels on the stack are monotonically
1281 typedef struct cp_parser_expression_stack_entry
1282 cp_parser_expression_stack[NUM_PREC_VALUES];
1284 /* Context that is saved and restored when parsing tentatively. */
1285 typedef struct cp_parser_context GTY (())
1287 /* If this is a tentative parsing context, the status of the
1289 enum cp_parser_status_kind status;
1290 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1291 that are looked up in this context must be looked up both in the
1292 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1293 the context of the containing expression. */
1296 /* The next parsing context in the stack. */
1297 struct cp_parser_context *next;
1298 } cp_parser_context;
1302 /* Constructors and destructors. */
1304 static cp_parser_context *cp_parser_context_new
1305 (cp_parser_context *);
1307 /* Class variables. */
1309 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311 /* The operator-precedence table used by cp_parser_binary_expression.
1312 Transformed into an associative array (binops_by_token) by
1315 static const cp_parser_binary_operations_map_node binops[] = {
1316 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1317 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1335 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1348 /* The same as binops, but initialized by cp_parser_new so that
1349 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1351 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353 /* Constructors and destructors. */
1355 /* Construct a new context. The context below this one on the stack
1356 is given by NEXT. */
1358 static cp_parser_context *
1359 cp_parser_context_new (cp_parser_context* next)
1361 cp_parser_context *context;
1363 /* Allocate the storage. */
1364 if (cp_parser_context_free_list != NULL)
1366 /* Pull the first entry from the free list. */
1367 context = cp_parser_context_free_list;
1368 cp_parser_context_free_list = context->next;
1369 memset (context, 0, sizeof (*context));
1372 context = GGC_CNEW (cp_parser_context);
1374 /* No errors have occurred yet in this context. */
1375 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1376 /* If this is not the bottommost context, copy information that we
1377 need from the previous context. */
1380 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1381 expression, then we are parsing one in this context, too. */
1382 context->object_type = next->object_type;
1383 /* Thread the stack. */
1384 context->next = next;
1390 /* The cp_parser structure represents the C++ parser. */
1392 typedef struct cp_parser GTY(())
1394 /* The lexer from which we are obtaining tokens. */
1397 /* The scope in which names should be looked up. If NULL_TREE, then
1398 we look up names in the scope that is currently open in the
1399 source program. If non-NULL, this is either a TYPE or
1400 NAMESPACE_DECL for the scope in which we should look. It can
1401 also be ERROR_MARK, when we've parsed a bogus scope.
1403 This value is not cleared automatically after a name is looked
1404 up, so we must be careful to clear it before starting a new look
1405 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1406 will look up `Z' in the scope of `X', rather than the current
1407 scope.) Unfortunately, it is difficult to tell when name lookup
1408 is complete, because we sometimes peek at a token, look it up,
1409 and then decide not to consume it. */
1412 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1413 last lookup took place. OBJECT_SCOPE is used if an expression
1414 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1415 respectively. QUALIFYING_SCOPE is used for an expression of the
1416 form "X::Y"; it refers to X. */
1418 tree qualifying_scope;
1420 /* A stack of parsing contexts. All but the bottom entry on the
1421 stack will be tentative contexts.
1423 We parse tentatively in order to determine which construct is in
1424 use in some situations. For example, in order to determine
1425 whether a statement is an expression-statement or a
1426 declaration-statement we parse it tentatively as a
1427 declaration-statement. If that fails, we then reparse the same
1428 token stream as an expression-statement. */
1429 cp_parser_context *context;
1431 /* True if we are parsing GNU C++. If this flag is not set, then
1432 GNU extensions are not recognized. */
1433 bool allow_gnu_extensions_p;
1435 /* TRUE if the `>' token should be interpreted as the greater-than
1436 operator. FALSE if it is the end of a template-id or
1437 template-parameter-list. In C++0x mode, this flag also applies to
1438 `>>' tokens, which are viewed as two consecutive `>' tokens when
1439 this flag is FALSE. */
1440 bool greater_than_is_operator_p;
1442 /* TRUE if default arguments are allowed within a parameter list
1443 that starts at this point. FALSE if only a gnu extension makes
1444 them permissible. */
1445 bool default_arg_ok_p;
1447 /* TRUE if we are parsing an integral constant-expression. See
1448 [expr.const] for a precise definition. */
1449 bool integral_constant_expression_p;
1451 /* TRUE if we are parsing an integral constant-expression -- but a
1452 non-constant expression should be permitted as well. This flag
1453 is used when parsing an array bound so that GNU variable-length
1454 arrays are tolerated. */
1455 bool allow_non_integral_constant_expression_p;
1457 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1458 been seen that makes the expression non-constant. */
1459 bool non_integral_constant_expression_p;
1461 /* TRUE if local variable names and `this' are forbidden in the
1463 bool local_variables_forbidden_p;
1465 /* TRUE if the declaration we are parsing is part of a
1466 linkage-specification of the form `extern string-literal
1468 bool in_unbraced_linkage_specification_p;
1470 /* TRUE if we are presently parsing a declarator, after the
1471 direct-declarator. */
1472 bool in_declarator_p;
1474 /* TRUE if we are presently parsing a template-argument-list. */
1475 bool in_template_argument_list_p;
1477 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1478 to IN_OMP_BLOCK if parsing OpenMP structured block and
1479 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1480 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1481 iteration-statement, OpenMP block or loop within that switch. */
1482 #define IN_SWITCH_STMT 1
1483 #define IN_ITERATION_STMT 2
1484 #define IN_OMP_BLOCK 4
1485 #define IN_OMP_FOR 8
1486 #define IN_IF_STMT 16
1487 unsigned char in_statement;
1489 /* TRUE if we are presently parsing the body of a switch statement.
1490 Note that this doesn't quite overlap with in_statement above.
1491 The difference relates to giving the right sets of error messages:
1492 "case not in switch" vs "break statement used with OpenMP...". */
1493 bool in_switch_statement_p;
1495 /* TRUE if we are parsing a type-id in an expression context. In
1496 such a situation, both "type (expr)" and "type (type)" are valid
1498 bool in_type_id_in_expr_p;
1500 /* TRUE if we are currently in a header file where declarations are
1501 implicitly extern "C". */
1502 bool implicit_extern_c;
1504 /* TRUE if strings in expressions should be translated to the execution
1506 bool translate_strings_p;
1508 /* TRUE if we are presently parsing the body of a function, but not
1510 bool in_function_body;
1512 /* If non-NULL, then we are parsing a construct where new type
1513 definitions are not permitted. The string stored here will be
1514 issued as an error message if a type is defined. */
1515 const char *type_definition_forbidden_message;
1517 /* A list of lists. The outer list is a stack, used for member
1518 functions of local classes. At each level there are two sub-list,
1519 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1520 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1521 TREE_VALUE's. The functions are chained in reverse declaration
1524 The TREE_PURPOSE sublist contains those functions with default
1525 arguments that need post processing, and the TREE_VALUE sublist
1526 contains those functions with definitions that need post
1529 These lists can only be processed once the outermost class being
1530 defined is complete. */
1531 tree unparsed_functions_queues;
1533 /* The number of classes whose definitions are currently in
1535 unsigned num_classes_being_defined;
1537 /* The number of template parameter lists that apply directly to the
1538 current declaration. */
1539 unsigned num_template_parameter_lists;
1544 /* Constructors and destructors. */
1546 static cp_parser *cp_parser_new
1549 /* Routines to parse various constructs.
1551 Those that return `tree' will return the error_mark_node (rather
1552 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1553 Sometimes, they will return an ordinary node if error-recovery was
1554 attempted, even though a parse error occurred. So, to check
1555 whether or not a parse error occurred, you should always use
1556 cp_parser_error_occurred. If the construct is optional (indicated
1557 either by an `_opt' in the name of the function that does the
1558 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1559 the construct is not present. */
1561 /* Lexical conventions [gram.lex] */
1563 static tree cp_parser_identifier
1565 static tree cp_parser_string_literal
1566 (cp_parser *, bool, bool);
1568 /* Basic concepts [gram.basic] */
1570 static bool cp_parser_translation_unit
1573 /* Expressions [gram.expr] */
1575 static tree cp_parser_primary_expression
1576 (cp_parser *, bool, bool, bool, cp_id_kind *);
1577 static tree cp_parser_id_expression
1578 (cp_parser *, bool, bool, bool *, bool, bool);
1579 static tree cp_parser_unqualified_id
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier_opt
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_nested_name_specifier
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_qualifying_entity
1586 (cp_parser *, bool, bool, bool, bool, bool);
1587 static tree cp_parser_postfix_expression
1588 (cp_parser *, bool, bool, bool, cp_id_kind *);
1589 static tree cp_parser_postfix_open_square_expression
1590 (cp_parser *, tree, bool);
1591 static tree cp_parser_postfix_dot_deref_expression
1592 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1593 static tree cp_parser_parenthesized_expression_list
1594 (cp_parser *, bool, bool, bool, bool *);
1595 static void cp_parser_pseudo_destructor_name
1596 (cp_parser *, tree *, tree *);
1597 static tree cp_parser_unary_expression
1598 (cp_parser *, bool, bool, cp_id_kind *);
1599 static enum tree_code cp_parser_unary_operator
1601 static tree cp_parser_new_expression
1603 static tree cp_parser_new_placement
1605 static tree cp_parser_new_type_id
1606 (cp_parser *, tree *);
1607 static cp_declarator *cp_parser_new_declarator_opt
1609 static cp_declarator *cp_parser_direct_new_declarator
1611 static tree cp_parser_new_initializer
1613 static tree cp_parser_delete_expression
1615 static tree cp_parser_cast_expression
1616 (cp_parser *, bool, bool, cp_id_kind *);
1617 static tree cp_parser_binary_expression
1618 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1619 static tree cp_parser_question_colon_clause
1620 (cp_parser *, tree);
1621 static tree cp_parser_assignment_expression
1622 (cp_parser *, bool, cp_id_kind *);
1623 static enum tree_code cp_parser_assignment_operator_opt
1625 static tree cp_parser_expression
1626 (cp_parser *, bool, cp_id_kind *);
1627 static tree cp_parser_constant_expression
1628 (cp_parser *, bool, bool *);
1629 static tree cp_parser_builtin_offsetof
1632 /* Statements [gram.stmt.stmt] */
1634 static void cp_parser_statement
1635 (cp_parser *, tree, bool, bool *);
1636 static void cp_parser_label_for_labeled_statement
1638 static tree cp_parser_expression_statement
1639 (cp_parser *, tree);
1640 static tree cp_parser_compound_statement
1641 (cp_parser *, tree, bool);
1642 static void cp_parser_statement_seq_opt
1643 (cp_parser *, tree);
1644 static tree cp_parser_selection_statement
1645 (cp_parser *, bool *);
1646 static tree cp_parser_condition
1648 static tree cp_parser_iteration_statement
1650 static void cp_parser_for_init_statement
1652 static tree cp_parser_jump_statement
1654 static void cp_parser_declaration_statement
1657 static tree cp_parser_implicitly_scoped_statement
1658 (cp_parser *, bool *);
1659 static void cp_parser_already_scoped_statement
1662 /* Declarations [gram.dcl.dcl] */
1664 static void cp_parser_declaration_seq_opt
1666 static void cp_parser_declaration
1668 static void cp_parser_block_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_simple_declaration
1671 (cp_parser *, bool);
1672 static void cp_parser_decl_specifier_seq
1673 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1674 static tree cp_parser_storage_class_specifier_opt
1676 static tree cp_parser_function_specifier_opt
1677 (cp_parser *, cp_decl_specifier_seq *);
1678 static tree cp_parser_type_specifier
1679 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681 static tree cp_parser_simple_type_specifier
1682 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1683 static tree cp_parser_type_name
1685 static tree cp_parser_nonclass_name
1686 (cp_parser* parser);
1687 static tree cp_parser_elaborated_type_specifier
1688 (cp_parser *, bool, bool);
1689 static tree cp_parser_enum_specifier
1691 static void cp_parser_enumerator_list
1692 (cp_parser *, tree);
1693 static void cp_parser_enumerator_definition
1694 (cp_parser *, tree);
1695 static tree cp_parser_namespace_name
1697 static void cp_parser_namespace_definition
1699 static void cp_parser_namespace_body
1701 static tree cp_parser_qualified_namespace_specifier
1703 static void cp_parser_namespace_alias_definition
1705 static bool cp_parser_using_declaration
1706 (cp_parser *, bool);
1707 static void cp_parser_using_directive
1709 static void cp_parser_asm_definition
1711 static void cp_parser_linkage_specification
1713 static void cp_parser_static_assert
1714 (cp_parser *, bool);
1715 static tree cp_parser_decltype
1718 /* Declarators [gram.dcl.decl] */
1720 static tree cp_parser_init_declarator
1721 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1722 static cp_declarator *cp_parser_declarator
1723 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1724 static cp_declarator *cp_parser_direct_declarator
1725 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1726 static enum tree_code cp_parser_ptr_operator
1727 (cp_parser *, tree *, cp_cv_quals *);
1728 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730 static tree cp_parser_late_return_type_opt
1732 static tree cp_parser_declarator_id
1733 (cp_parser *, bool);
1734 static tree cp_parser_type_id
1736 static tree cp_parser_template_type_arg
1738 static tree cp_parser_type_id_1
1739 (cp_parser *, bool);
1740 static void cp_parser_type_specifier_seq
1741 (cp_parser *, bool, cp_decl_specifier_seq *);
1742 static tree cp_parser_parameter_declaration_clause
1744 static tree cp_parser_parameter_declaration_list
1745 (cp_parser *, bool *);
1746 static cp_parameter_declarator *cp_parser_parameter_declaration
1747 (cp_parser *, bool, bool *);
1748 static tree cp_parser_default_argument
1749 (cp_parser *, bool);
1750 static void cp_parser_function_body
1752 static tree cp_parser_initializer
1753 (cp_parser *, bool *, bool *);
1754 static tree cp_parser_initializer_clause
1755 (cp_parser *, bool *);
1756 static tree cp_parser_braced_list
1757 (cp_parser*, bool*);
1758 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1759 (cp_parser *, bool *);
1761 static bool cp_parser_ctor_initializer_opt_and_function_body
1764 /* Classes [gram.class] */
1766 static tree cp_parser_class_name
1767 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1768 static tree cp_parser_class_specifier
1770 static tree cp_parser_class_head
1771 (cp_parser *, bool *, tree *, tree *);
1772 static enum tag_types cp_parser_class_key
1774 static void cp_parser_member_specification_opt
1776 static void cp_parser_member_declaration
1778 static tree cp_parser_pure_specifier
1780 static tree cp_parser_constant_initializer
1783 /* Derived classes [gram.class.derived] */
1785 static tree cp_parser_base_clause
1787 static tree cp_parser_base_specifier
1790 /* Special member functions [gram.special] */
1792 static tree cp_parser_conversion_function_id
1794 static tree cp_parser_conversion_type_id
1796 static cp_declarator *cp_parser_conversion_declarator_opt
1798 static bool cp_parser_ctor_initializer_opt
1800 static void cp_parser_mem_initializer_list
1802 static tree cp_parser_mem_initializer
1804 static tree cp_parser_mem_initializer_id
1807 /* Overloading [gram.over] */
1809 static tree cp_parser_operator_function_id
1811 static tree cp_parser_operator
1814 /* Templates [gram.temp] */
1816 static void cp_parser_template_declaration
1817 (cp_parser *, bool);
1818 static tree cp_parser_template_parameter_list
1820 static tree cp_parser_template_parameter
1821 (cp_parser *, bool *, bool *);
1822 static tree cp_parser_type_parameter
1823 (cp_parser *, bool *);
1824 static tree cp_parser_template_id
1825 (cp_parser *, bool, bool, bool);
1826 static tree cp_parser_template_name
1827 (cp_parser *, bool, bool, bool, bool *);
1828 static tree cp_parser_template_argument_list
1830 static tree cp_parser_template_argument
1832 static void cp_parser_explicit_instantiation
1834 static void cp_parser_explicit_specialization
1837 /* Exception handling [gram.exception] */
1839 static tree cp_parser_try_block
1841 static bool cp_parser_function_try_block
1843 static void cp_parser_handler_seq
1845 static void cp_parser_handler
1847 static tree cp_parser_exception_declaration
1849 static tree cp_parser_throw_expression
1851 static tree cp_parser_exception_specification_opt
1853 static tree cp_parser_type_id_list
1856 /* GNU Extensions */
1858 static tree cp_parser_asm_specification_opt
1860 static tree cp_parser_asm_operand_list
1862 static tree cp_parser_asm_clobber_list
1864 static tree cp_parser_attributes_opt
1866 static tree cp_parser_attribute_list
1868 static bool cp_parser_extension_opt
1869 (cp_parser *, int *);
1870 static void cp_parser_label_declaration
1873 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1874 static bool cp_parser_pragma
1875 (cp_parser *, enum pragma_context);
1877 /* Objective-C++ Productions */
1879 static tree cp_parser_objc_message_receiver
1881 static tree cp_parser_objc_message_args
1883 static tree cp_parser_objc_message_expression
1885 static tree cp_parser_objc_encode_expression
1887 static tree cp_parser_objc_defs_expression
1889 static tree cp_parser_objc_protocol_expression
1891 static tree cp_parser_objc_selector_expression
1893 static tree cp_parser_objc_expression
1895 static bool cp_parser_objc_selector_p
1897 static tree cp_parser_objc_selector
1899 static tree cp_parser_objc_protocol_refs_opt
1901 static void cp_parser_objc_declaration
1903 static tree cp_parser_objc_statement
1906 /* Utility Routines */
1908 static tree cp_parser_lookup_name
1909 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1910 static tree cp_parser_lookup_name_simple
1911 (cp_parser *, tree, location_t);
1912 static tree cp_parser_maybe_treat_template_as_class
1914 static bool cp_parser_check_declarator_template_parameters
1915 (cp_parser *, cp_declarator *, location_t);
1916 static bool cp_parser_check_template_parameters
1917 (cp_parser *, unsigned, location_t, cp_declarator *);
1918 static tree cp_parser_simple_cast_expression
1920 static tree cp_parser_global_scope_opt
1921 (cp_parser *, bool);
1922 static bool cp_parser_constructor_declarator_p
1923 (cp_parser *, bool);
1924 static tree cp_parser_function_definition_from_specifiers_and_declarator
1925 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1926 static tree cp_parser_function_definition_after_declarator
1927 (cp_parser *, bool);
1928 static void cp_parser_template_declaration_after_export
1929 (cp_parser *, bool);
1930 static void cp_parser_perform_template_parameter_access_checks
1931 (VEC (deferred_access_check,gc)*);
1932 static tree cp_parser_single_declaration
1933 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1934 static tree cp_parser_functional_cast
1935 (cp_parser *, tree);
1936 static tree cp_parser_save_member_function_body
1937 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1938 static tree cp_parser_enclosed_template_argument_list
1940 static void cp_parser_save_default_args
1941 (cp_parser *, tree);
1942 static void cp_parser_late_parsing_for_member
1943 (cp_parser *, tree);
1944 static void cp_parser_late_parsing_default_args
1945 (cp_parser *, tree);
1946 static tree cp_parser_sizeof_operand
1947 (cp_parser *, enum rid);
1948 static tree cp_parser_trait_expr
1949 (cp_parser *, enum rid);
1950 static bool cp_parser_declares_only_class_p
1952 static void cp_parser_set_storage_class
1953 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1954 static void cp_parser_set_decl_spec_type
1955 (cp_decl_specifier_seq *, tree, location_t, bool);
1956 static bool cp_parser_friend_p
1957 (const cp_decl_specifier_seq *);
1958 static cp_token *cp_parser_require
1959 (cp_parser *, enum cpp_ttype, const char *);
1960 static cp_token *cp_parser_require_keyword
1961 (cp_parser *, enum rid, const char *);
1962 static bool cp_parser_token_starts_function_definition_p
1964 static bool cp_parser_next_token_starts_class_definition_p
1966 static bool cp_parser_next_token_ends_template_argument_p
1968 static bool cp_parser_nth_token_starts_template_argument_list_p
1969 (cp_parser *, size_t);
1970 static enum tag_types cp_parser_token_is_class_key
1972 static void cp_parser_check_class_key
1973 (enum tag_types, tree type);
1974 static void cp_parser_check_access_in_redeclaration
1975 (tree type, location_t location);
1976 static bool cp_parser_optional_template_keyword
1978 static void cp_parser_pre_parsed_nested_name_specifier
1980 static bool cp_parser_cache_group
1981 (cp_parser *, enum cpp_ttype, unsigned);
1982 static void cp_parser_parse_tentatively
1984 static void cp_parser_commit_to_tentative_parse
1986 static void cp_parser_abort_tentative_parse
1988 static bool cp_parser_parse_definitely
1990 static inline bool cp_parser_parsing_tentatively
1992 static bool cp_parser_uncommitted_to_tentative_parse_p
1994 static void cp_parser_error
1995 (cp_parser *, const char *);
1996 static void cp_parser_name_lookup_error
1997 (cp_parser *, tree, tree, const char *, location_t);
1998 static bool cp_parser_simulate_error
2000 static bool cp_parser_check_type_definition
2002 static void cp_parser_check_for_definition_in_return_type
2003 (cp_declarator *, tree, location_t type_location);
2004 static void cp_parser_check_for_invalid_template_id
2005 (cp_parser *, tree, location_t location);
2006 static bool cp_parser_non_integral_constant_expression
2007 (cp_parser *, const char *);
2008 static void cp_parser_diagnose_invalid_type_name
2009 (cp_parser *, tree, tree, location_t);
2010 static bool cp_parser_parse_and_diagnose_invalid_type_name
2012 static int cp_parser_skip_to_closing_parenthesis
2013 (cp_parser *, bool, bool, bool);
2014 static void cp_parser_skip_to_end_of_statement
2016 static void cp_parser_consume_semicolon_at_end_of_statement
2018 static void cp_parser_skip_to_end_of_block_or_statement
2020 static bool cp_parser_skip_to_closing_brace
2022 static void cp_parser_skip_to_end_of_template_parameter_list
2024 static void cp_parser_skip_to_pragma_eol
2025 (cp_parser*, cp_token *);
2026 static bool cp_parser_error_occurred
2028 static bool cp_parser_allow_gnu_extensions_p
2030 static bool cp_parser_is_string_literal
2032 static bool cp_parser_is_keyword
2033 (cp_token *, enum rid);
2034 static tree cp_parser_make_typename_type
2035 (cp_parser *, tree, tree, location_t location);
2036 static cp_declarator * cp_parser_make_indirect_declarator
2037 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2039 /* Returns nonzero if we are parsing tentatively. */
2042 cp_parser_parsing_tentatively (cp_parser* parser)
2044 return parser->context->next != NULL;
2047 /* Returns nonzero if TOKEN is a string literal. */
2050 cp_parser_is_string_literal (cp_token* token)
2052 return (token->type == CPP_STRING ||
2053 token->type == CPP_STRING16 ||
2054 token->type == CPP_STRING32 ||
2055 token->type == CPP_WSTRING);
2058 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2061 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2063 return token->keyword == keyword;
2066 /* If not parsing tentatively, issue a diagnostic of the form
2067 FILE:LINE: MESSAGE before TOKEN
2068 where TOKEN is the next token in the input stream. MESSAGE
2069 (specified by the caller) is usually of the form "expected
2073 cp_parser_error (cp_parser* parser, const char* message)
2075 if (!cp_parser_simulate_error (parser))
2077 cp_token *token = cp_lexer_peek_token (parser->lexer);
2078 /* This diagnostic makes more sense if it is tagged to the line
2079 of the token we just peeked at. */
2080 cp_lexer_set_source_position_from_token (token);
2082 if (token->type == CPP_PRAGMA)
2084 error ("%H%<#pragma%> is not allowed here", &token->location);
2085 cp_parser_skip_to_pragma_eol (parser, token);
2089 c_parse_error (message,
2090 /* Because c_parser_error does not understand
2091 CPP_KEYWORD, keywords are treated like
2093 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2098 /* Issue an error about name-lookup failing. NAME is the
2099 IDENTIFIER_NODE DECL is the result of
2100 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2101 the thing that we hoped to find. */
2104 cp_parser_name_lookup_error (cp_parser* parser,
2107 const char* desired,
2108 location_t location)
2110 /* If name lookup completely failed, tell the user that NAME was not
2112 if (decl == error_mark_node)
2114 if (parser->scope && parser->scope != global_namespace)
2115 error ("%H%<%E::%E%> has not been declared",
2116 &location, parser->scope, name);
2117 else if (parser->scope == global_namespace)
2118 error ("%H%<::%E%> has not been declared", &location, name);
2119 else if (parser->object_scope
2120 && !CLASS_TYPE_P (parser->object_scope))
2121 error ("%Hrequest for member %qE in non-class type %qT",
2122 &location, name, parser->object_scope);
2123 else if (parser->object_scope)
2124 error ("%H%<%T::%E%> has not been declared",
2125 &location, parser->object_scope, name);
2127 error ("%H%qE has not been declared", &location, name);
2129 else if (parser->scope && parser->scope != global_namespace)
2130 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2131 else if (parser->scope == global_namespace)
2132 error ("%H%<::%E%> %s", &location, name, desired);
2134 error ("%H%qE %s", &location, name, desired);
2137 /* If we are parsing tentatively, remember that an error has occurred
2138 during this tentative parse. Returns true if the error was
2139 simulated; false if a message should be issued by the caller. */
2142 cp_parser_simulate_error (cp_parser* parser)
2144 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2146 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2152 /* Check for repeated decl-specifiers. */
2155 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2156 location_t location)
2160 for (ds = ds_first; ds != ds_last; ++ds)
2162 unsigned count = decl_specs->specs[(int)ds];
2165 /* The "long" specifier is a special case because of "long long". */
2169 error ("%H%<long long long%> is too long for GCC", &location);
2170 else if (pedantic && !in_system_header && warn_long_long
2171 && cxx_dialect == cxx98)
2172 pedwarn (location, OPT_Wlong_long,
2173 "ISO C++ 1998 does not support %<long long%>");
2177 static const char *const decl_spec_names[] = {
2193 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2198 /* This function is called when a type is defined. If type
2199 definitions are forbidden at this point, an error message is
2203 cp_parser_check_type_definition (cp_parser* parser)
2205 /* If types are forbidden here, issue a message. */
2206 if (parser->type_definition_forbidden_message)
2208 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2209 in the message need to be interpreted. */
2210 error (parser->type_definition_forbidden_message);
2216 /* This function is called when the DECLARATOR is processed. The TYPE
2217 was a type defined in the decl-specifiers. If it is invalid to
2218 define a type in the decl-specifiers for DECLARATOR, an error is
2219 issued. TYPE_LOCATION is the location of TYPE and is used
2220 for error reporting. */
2223 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2224 tree type, location_t type_location)
2226 /* [dcl.fct] forbids type definitions in return types.
2227 Unfortunately, it's not easy to know whether or not we are
2228 processing a return type until after the fact. */
2230 && (declarator->kind == cdk_pointer
2231 || declarator->kind == cdk_reference
2232 || declarator->kind == cdk_ptrmem))
2233 declarator = declarator->declarator;
2235 && declarator->kind == cdk_function)
2237 error ("%Hnew types may not be defined in a return type", &type_location);
2238 inform (type_location,
2239 "(perhaps a semicolon is missing after the definition of %qT)",
2244 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2245 "<" in any valid C++ program. If the next token is indeed "<",
2246 issue a message warning the user about what appears to be an
2247 invalid attempt to form a template-id. LOCATION is the location
2248 of the type-specifier (TYPE) */
2251 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2252 tree type, location_t location)
2254 cp_token_position start = 0;
2256 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2259 error ("%H%qT is not a template", &location, type);
2260 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2261 error ("%H%qE is not a template", &location, type);
2263 error ("%Hinvalid template-id", &location);
2264 /* Remember the location of the invalid "<". */
2265 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2266 start = cp_lexer_token_position (parser->lexer, true);
2267 /* Consume the "<". */
2268 cp_lexer_consume_token (parser->lexer);
2269 /* Parse the template arguments. */
2270 cp_parser_enclosed_template_argument_list (parser);
2271 /* Permanently remove the invalid template arguments so that
2272 this error message is not issued again. */
2274 cp_lexer_purge_tokens_after (parser->lexer, start);
2278 /* If parsing an integral constant-expression, issue an error message
2279 about the fact that THING appeared and return true. Otherwise,
2280 return false. In either case, set
2281 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2284 cp_parser_non_integral_constant_expression (cp_parser *parser,
2287 parser->non_integral_constant_expression_p = true;
2288 if (parser->integral_constant_expression_p)
2290 if (!parser->allow_non_integral_constant_expression_p)
2292 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2293 in the message need to be interpreted. */
2294 char *message = concat (thing,
2295 " cannot appear in a constant-expression",
2305 /* Emit a diagnostic for an invalid type name. SCOPE is the
2306 qualifying scope (or NULL, if none) for ID. This function commits
2307 to the current active tentative parse, if any. (Otherwise, the
2308 problematic construct might be encountered again later, resulting
2309 in duplicate error messages.) LOCATION is the location of ID. */
2312 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2313 tree scope, tree id,
2314 location_t location)
2316 tree decl, old_scope;
2317 /* Try to lookup the identifier. */
2318 old_scope = parser->scope;
2319 parser->scope = scope;
2320 decl = cp_parser_lookup_name_simple (parser, id, location);
2321 parser->scope = old_scope;
2322 /* If the lookup found a template-name, it means that the user forgot
2323 to specify an argument list. Emit a useful error message. */
2324 if (TREE_CODE (decl) == TEMPLATE_DECL)
2325 error ("%Hinvalid use of template-name %qE without an argument list",
2327 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2328 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2329 else if (TREE_CODE (decl) == TYPE_DECL)
2330 /* Something like 'unsigned A a;' */
2331 error ("%Hinvalid combination of multiple type-specifiers",
2333 else if (!parser->scope)
2335 /* Issue an error message. */
2336 error ("%H%qE does not name a type", &location, id);
2337 /* If we're in a template class, it's possible that the user was
2338 referring to a type from a base class. For example:
2340 template <typename T> struct A { typedef T X; };
2341 template <typename T> struct B : public A<T> { X x; };
2343 The user should have said "typename A<T>::X". */
2344 if (processing_template_decl && current_class_type
2345 && TYPE_BINFO (current_class_type))
2349 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2353 tree base_type = BINFO_TYPE (b);
2354 if (CLASS_TYPE_P (base_type)
2355 && dependent_type_p (base_type))
2358 /* Go from a particular instantiation of the
2359 template (which will have an empty TYPE_FIELDs),
2360 to the main version. */
2361 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2362 for (field = TYPE_FIELDS (base_type);
2364 field = TREE_CHAIN (field))
2365 if (TREE_CODE (field) == TYPE_DECL
2366 && DECL_NAME (field) == id)
2369 "(perhaps %<typename %T::%E%> was intended)",
2370 BINFO_TYPE (b), id);
2379 /* Here we diagnose qualified-ids where the scope is actually correct,
2380 but the identifier does not resolve to a valid type name. */
2381 else if (parser->scope != error_mark_node)
2383 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2384 error ("%H%qE in namespace %qE does not name a type",
2385 &location, id, parser->scope);
2386 else if (TYPE_P (parser->scope))
2387 error ("%H%qE in class %qT does not name a type",
2388 &location, id, parser->scope);
2392 cp_parser_commit_to_tentative_parse (parser);
2395 /* Check for a common situation where a type-name should be present,
2396 but is not, and issue a sensible error message. Returns true if an
2397 invalid type-name was detected.
2399 The situation handled by this function are variable declarations of the
2400 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2401 Usually, `ID' should name a type, but if we got here it means that it
2402 does not. We try to emit the best possible error message depending on
2403 how exactly the id-expression looks like. */
2406 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2409 cp_token *token = cp_lexer_peek_token (parser->lexer);
2411 cp_parser_parse_tentatively (parser);
2412 id = cp_parser_id_expression (parser,
2413 /*template_keyword_p=*/false,
2414 /*check_dependency_p=*/true,
2415 /*template_p=*/NULL,
2416 /*declarator_p=*/true,
2417 /*optional_p=*/false);
2418 /* After the id-expression, there should be a plain identifier,
2419 otherwise this is not a simple variable declaration. Also, if
2420 the scope is dependent, we cannot do much. */
2421 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2422 || (parser->scope && TYPE_P (parser->scope)
2423 && dependent_type_p (parser->scope))
2424 || TREE_CODE (id) == TYPE_DECL)
2426 cp_parser_abort_tentative_parse (parser);
2429 if (!cp_parser_parse_definitely (parser))
2432 /* Emit a diagnostic for the invalid type. */
2433 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2434 id, token->location);
2435 /* Skip to the end of the declaration; there's no point in
2436 trying to process it. */
2437 cp_parser_skip_to_end_of_block_or_statement (parser);
2441 /* Consume tokens up to, and including, the next non-nested closing `)'.
2442 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2443 are doing error recovery. Returns -1 if OR_COMMA is true and we
2444 found an unnested comma. */
2447 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2452 unsigned paren_depth = 0;
2453 unsigned brace_depth = 0;
2455 if (recovering && !or_comma
2456 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2461 cp_token * token = cp_lexer_peek_token (parser->lexer);
2463 switch (token->type)
2466 case CPP_PRAGMA_EOL:
2467 /* If we've run out of tokens, then there is no closing `)'. */
2471 /* This matches the processing in skip_to_end_of_statement. */
2476 case CPP_OPEN_BRACE:
2479 case CPP_CLOSE_BRACE:
2485 if (recovering && or_comma && !brace_depth && !paren_depth)
2489 case CPP_OPEN_PAREN:
2494 case CPP_CLOSE_PAREN:
2495 if (!brace_depth && !paren_depth--)
2498 cp_lexer_consume_token (parser->lexer);
2507 /* Consume the token. */
2508 cp_lexer_consume_token (parser->lexer);
2512 /* Consume tokens until we reach the end of the current statement.
2513 Normally, that will be just before consuming a `;'. However, if a
2514 non-nested `}' comes first, then we stop before consuming that. */
2517 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2519 unsigned nesting_depth = 0;
2523 cp_token *token = cp_lexer_peek_token (parser->lexer);
2525 switch (token->type)
2528 case CPP_PRAGMA_EOL:
2529 /* If we've run out of tokens, stop. */
2533 /* If the next token is a `;', we have reached the end of the
2539 case CPP_CLOSE_BRACE:
2540 /* If this is a non-nested '}', stop before consuming it.
2541 That way, when confronted with something like:
2545 we stop before consuming the closing '}', even though we
2546 have not yet reached a `;'. */
2547 if (nesting_depth == 0)
2550 /* If it is the closing '}' for a block that we have
2551 scanned, stop -- but only after consuming the token.
2557 we will stop after the body of the erroneously declared
2558 function, but before consuming the following `typedef'
2560 if (--nesting_depth == 0)
2562 cp_lexer_consume_token (parser->lexer);
2566 case CPP_OPEN_BRACE:
2574 /* Consume the token. */
2575 cp_lexer_consume_token (parser->lexer);
2579 /* This function is called at the end of a statement or declaration.
2580 If the next token is a semicolon, it is consumed; otherwise, error
2581 recovery is attempted. */
2584 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2586 /* Look for the trailing `;'. */
2587 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2589 /* If there is additional (erroneous) input, skip to the end of
2591 cp_parser_skip_to_end_of_statement (parser);
2592 /* If the next token is now a `;', consume it. */
2593 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2594 cp_lexer_consume_token (parser->lexer);
2598 /* Skip tokens until we have consumed an entire block, or until we
2599 have consumed a non-nested `;'. */
2602 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2604 int nesting_depth = 0;
2606 while (nesting_depth >= 0)
2608 cp_token *token = cp_lexer_peek_token (parser->lexer);
2610 switch (token->type)
2613 case CPP_PRAGMA_EOL:
2614 /* If we've run out of tokens, stop. */
2618 /* Stop if this is an unnested ';'. */
2623 case CPP_CLOSE_BRACE:
2624 /* Stop if this is an unnested '}', or closes the outermost
2627 if (nesting_depth < 0)
2633 case CPP_OPEN_BRACE:
2642 /* Consume the token. */
2643 cp_lexer_consume_token (parser->lexer);
2647 /* Skip tokens until a non-nested closing curly brace is the next
2648 token, or there are no more tokens. Return true in the first case,
2652 cp_parser_skip_to_closing_brace (cp_parser *parser)
2654 unsigned nesting_depth = 0;
2658 cp_token *token = cp_lexer_peek_token (parser->lexer);
2660 switch (token->type)
2663 case CPP_PRAGMA_EOL:
2664 /* If we've run out of tokens, stop. */
2667 case CPP_CLOSE_BRACE:
2668 /* If the next token is a non-nested `}', then we have reached
2669 the end of the current block. */
2670 if (nesting_depth-- == 0)
2674 case CPP_OPEN_BRACE:
2675 /* If it the next token is a `{', then we are entering a new
2676 block. Consume the entire block. */
2684 /* Consume the token. */
2685 cp_lexer_consume_token (parser->lexer);
2689 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2690 parameter is the PRAGMA token, allowing us to purge the entire pragma
2694 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2698 parser->lexer->in_pragma = false;
2701 token = cp_lexer_consume_token (parser->lexer);
2702 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2704 /* Ensure that the pragma is not parsed again. */
2705 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2708 /* Require pragma end of line, resyncing with it as necessary. The
2709 arguments are as for cp_parser_skip_to_pragma_eol. */
2712 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2714 parser->lexer->in_pragma = false;
2715 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2716 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2719 /* This is a simple wrapper around make_typename_type. When the id is
2720 an unresolved identifier node, we can provide a superior diagnostic
2721 using cp_parser_diagnose_invalid_type_name. */
2724 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2725 tree id, location_t id_location)
2728 if (TREE_CODE (id) == IDENTIFIER_NODE)
2730 result = make_typename_type (scope, id, typename_type,
2731 /*complain=*/tf_none);
2732 if (result == error_mark_node)
2733 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2736 return make_typename_type (scope, id, typename_type, tf_error);
2739 /* This is a wrapper around the
2740 make_{pointer,ptrmem,reference}_declarator functions that decides
2741 which one to call based on the CODE and CLASS_TYPE arguments. The
2742 CODE argument should be one of the values returned by
2743 cp_parser_ptr_operator. */
2744 static cp_declarator *
2745 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2746 cp_cv_quals cv_qualifiers,
2747 cp_declarator *target)
2749 if (code == ERROR_MARK)
2750 return cp_error_declarator;
2752 if (code == INDIRECT_REF)
2753 if (class_type == NULL_TREE)
2754 return make_pointer_declarator (cv_qualifiers, target);
2756 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2757 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2758 return make_reference_declarator (cv_qualifiers, target, false);
2759 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2760 return make_reference_declarator (cv_qualifiers, target, true);
2764 /* Create a new C++ parser. */
2767 cp_parser_new (void)
2773 /* cp_lexer_new_main is called before calling ggc_alloc because
2774 cp_lexer_new_main might load a PCH file. */
2775 lexer = cp_lexer_new_main ();
2777 /* Initialize the binops_by_token so that we can get the tree
2778 directly from the token. */
2779 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2780 binops_by_token[binops[i].token_type] = binops[i];
2782 parser = GGC_CNEW (cp_parser);
2783 parser->lexer = lexer;
2784 parser->context = cp_parser_context_new (NULL);
2786 /* For now, we always accept GNU extensions. */
2787 parser->allow_gnu_extensions_p = 1;
2789 /* The `>' token is a greater-than operator, not the end of a
2791 parser->greater_than_is_operator_p = true;
2793 parser->default_arg_ok_p = true;
2795 /* We are not parsing a constant-expression. */
2796 parser->integral_constant_expression_p = false;
2797 parser->allow_non_integral_constant_expression_p = false;
2798 parser->non_integral_constant_expression_p = false;
2800 /* Local variable names are not forbidden. */
2801 parser->local_variables_forbidden_p = false;
2803 /* We are not processing an `extern "C"' declaration. */
2804 parser->in_unbraced_linkage_specification_p = false;
2806 /* We are not processing a declarator. */
2807 parser->in_declarator_p = false;
2809 /* We are not processing a template-argument-list. */
2810 parser->in_template_argument_list_p = false;
2812 /* We are not in an iteration statement. */
2813 parser->in_statement = 0;
2815 /* We are not in a switch statement. */
2816 parser->in_switch_statement_p = false;
2818 /* We are not parsing a type-id inside an expression. */
2819 parser->in_type_id_in_expr_p = false;
2821 /* Declarations aren't implicitly extern "C". */
2822 parser->implicit_extern_c = false;
2824 /* String literals should be translated to the execution character set. */
2825 parser->translate_strings_p = true;
2827 /* We are not parsing a function body. */
2828 parser->in_function_body = false;
2830 /* The unparsed function queue is empty. */
2831 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2833 /* There are no classes being defined. */
2834 parser->num_classes_being_defined = 0;
2836 /* No template parameters apply. */
2837 parser->num_template_parameter_lists = 0;
2842 /* Create a cp_lexer structure which will emit the tokens in CACHE
2843 and push it onto the parser's lexer stack. This is used for delayed
2844 parsing of in-class method bodies and default arguments, and should
2845 not be confused with tentative parsing. */
2847 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2849 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2850 lexer->next = parser->lexer;
2851 parser->lexer = lexer;
2853 /* Move the current source position to that of the first token in the
2855 cp_lexer_set_source_position_from_token (lexer->next_token);
2858 /* Pop the top lexer off the parser stack. This is never used for the
2859 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2861 cp_parser_pop_lexer (cp_parser *parser)
2863 cp_lexer *lexer = parser->lexer;
2864 parser->lexer = lexer->next;
2865 cp_lexer_destroy (lexer);
2867 /* Put the current source position back where it was before this
2868 lexer was pushed. */
2869 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2872 /* Lexical conventions [gram.lex] */
2874 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2878 cp_parser_identifier (cp_parser* parser)
2882 /* Look for the identifier. */
2883 token = cp_parser_require (parser, CPP_NAME, "identifier");
2884 /* Return the value. */
2885 return token ? token->u.value : error_mark_node;
2888 /* Parse a sequence of adjacent string constants. Returns a
2889 TREE_STRING representing the combined, nul-terminated string
2890 constant. If TRANSLATE is true, translate the string to the
2891 execution character set. If WIDE_OK is true, a wide string is
2894 C++98 [lex.string] says that if a narrow string literal token is
2895 adjacent to a wide string literal token, the behavior is undefined.
2896 However, C99 6.4.5p4 says that this results in a wide string literal.
2897 We follow C99 here, for consistency with the C front end.
2899 This code is largely lifted from lex_string() in c-lex.c.
2901 FUTURE: ObjC++ will need to handle @-strings here. */
2903 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2907 struct obstack str_ob;
2908 cpp_string str, istr, *strs;
2910 enum cpp_ttype type;
2912 tok = cp_lexer_peek_token (parser->lexer);
2913 if (!cp_parser_is_string_literal (tok))
2915 cp_parser_error (parser, "expected string-literal");
2916 return error_mark_node;
2921 /* Try to avoid the overhead of creating and destroying an obstack
2922 for the common case of just one string. */
2923 if (!cp_parser_is_string_literal
2924 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2926 cp_lexer_consume_token (parser->lexer);
2928 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2929 str.len = TREE_STRING_LENGTH (tok->u.value);
2936 gcc_obstack_init (&str_ob);
2941 cp_lexer_consume_token (parser->lexer);
2943 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2944 str.len = TREE_STRING_LENGTH (tok->u.value);
2946 if (type != tok->type)
2948 if (type == CPP_STRING)
2950 else if (tok->type != CPP_STRING)
2951 error ("%Hunsupported non-standard concatenation "
2952 "of string literals", &tok->location);
2955 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2957 tok = cp_lexer_peek_token (parser->lexer);
2959 while (cp_parser_is_string_literal (tok));
2961 strs = (cpp_string *) obstack_finish (&str_ob);
2964 if (type != CPP_STRING && !wide_ok)
2966 cp_parser_error (parser, "a wide string is invalid in this context");
2970 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2971 (parse_in, strs, count, &istr, type))
2973 value = build_string (istr.len, (const char *)istr.text);
2974 free (CONST_CAST (unsigned char *, istr.text));
2980 TREE_TYPE (value) = char_array_type_node;
2983 TREE_TYPE (value) = char16_array_type_node;
2986 TREE_TYPE (value) = char32_array_type_node;
2989 TREE_TYPE (value) = wchar_array_type_node;
2993 value = fix_string_type (value);
2996 /* cpp_interpret_string has issued an error. */
2997 value = error_mark_node;
3000 obstack_free (&str_ob, 0);
3006 /* Basic concepts [gram.basic] */
3008 /* Parse a translation-unit.
3011 declaration-seq [opt]
3013 Returns TRUE if all went well. */
3016 cp_parser_translation_unit (cp_parser* parser)
3018 /* The address of the first non-permanent object on the declarator
3020 static void *declarator_obstack_base;
3024 /* Create the declarator obstack, if necessary. */
3025 if (!cp_error_declarator)
3027 gcc_obstack_init (&declarator_obstack);
3028 /* Create the error declarator. */
3029 cp_error_declarator = make_declarator (cdk_error);
3030 /* Create the empty parameter list. */
3031 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3032 /* Remember where the base of the declarator obstack lies. */
3033 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3036 cp_parser_declaration_seq_opt (parser);
3038 /* If there are no tokens left then all went well. */
3039 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3041 /* Get rid of the token array; we don't need it any more. */
3042 cp_lexer_destroy (parser->lexer);
3043 parser->lexer = NULL;
3045 /* This file might have been a context that's implicitly extern
3046 "C". If so, pop the lang context. (Only relevant for PCH.) */
3047 if (parser->implicit_extern_c)
3049 pop_lang_context ();
3050 parser->implicit_extern_c = false;
3054 finish_translation_unit ();
3060 cp_parser_error (parser, "expected declaration");
3064 /* Make sure the declarator obstack was fully cleaned up. */
3065 gcc_assert (obstack_next_free (&declarator_obstack)
3066 == declarator_obstack_base);
3068 /* All went well. */
3072 /* Expressions [gram.expr] */
3074 /* Parse a primary-expression.
3085 ( compound-statement )
3086 __builtin_va_arg ( assignment-expression , type-id )
3087 __builtin_offsetof ( type-id , offsetof-expression )
3090 __has_nothrow_assign ( type-id )
3091 __has_nothrow_constructor ( type-id )
3092 __has_nothrow_copy ( type-id )
3093 __has_trivial_assign ( type-id )
3094 __has_trivial_constructor ( type-id )
3095 __has_trivial_copy ( type-id )
3096 __has_trivial_destructor ( type-id )
3097 __has_virtual_destructor ( type-id )
3098 __is_abstract ( type-id )
3099 __is_base_of ( type-id , type-id )
3100 __is_class ( type-id )
3101 __is_convertible_to ( type-id , type-id )
3102 __is_empty ( type-id )
3103 __is_enum ( type-id )
3104 __is_pod ( type-id )
3105 __is_polymorphic ( type-id )
3106 __is_union ( type-id )
3108 Objective-C++ Extension:
3116 ADDRESS_P is true iff this expression was immediately preceded by
3117 "&" and therefore might denote a pointer-to-member. CAST_P is true
3118 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3119 true iff this expression is a template argument.
3121 Returns a representation of the expression. Upon return, *IDK
3122 indicates what kind of id-expression (if any) was present. */
3125 cp_parser_primary_expression (cp_parser *parser,
3128 bool template_arg_p,
3131 cp_token *token = NULL;
3133 /* Assume the primary expression is not an id-expression. */
3134 *idk = CP_ID_KIND_NONE;
3136 /* Peek at the next token. */
3137 token = cp_lexer_peek_token (parser->lexer);
3138 switch (token->type)
3151 token = cp_lexer_consume_token (parser->lexer);
3152 if (TREE_CODE (token->u.value) == FIXED_CST)
3154 error ("%Hfixed-point types not supported in C++",
3156 return error_mark_node;
3158 /* Floating-point literals are only allowed in an integral
3159 constant expression if they are cast to an integral or
3160 enumeration type. */
3161 if (TREE_CODE (token->u.value) == REAL_CST
3162 && parser->integral_constant_expression_p
3165 /* CAST_P will be set even in invalid code like "int(2.7 +
3166 ...)". Therefore, we have to check that the next token
3167 is sure to end the cast. */
3170 cp_token *next_token;
3172 next_token = cp_lexer_peek_token (parser->lexer);
3173 if (/* The comma at the end of an
3174 enumerator-definition. */
3175 next_token->type != CPP_COMMA
3176 /* The curly brace at the end of an enum-specifier. */
3177 && next_token->type != CPP_CLOSE_BRACE
3178 /* The end of a statement. */
3179 && next_token->type != CPP_SEMICOLON
3180 /* The end of the cast-expression. */
3181 && next_token->type != CPP_CLOSE_PAREN
3182 /* The end of an array bound. */
3183 && next_token->type != CPP_CLOSE_SQUARE
3184 /* The closing ">" in a template-argument-list. */
3185 && (next_token->type != CPP_GREATER
3186 || parser->greater_than_is_operator_p)
3187 /* C++0x only: A ">>" treated like two ">" tokens,
3188 in a template-argument-list. */
3189 && (next_token->type != CPP_RSHIFT
3190 || (cxx_dialect == cxx98)
3191 || parser->greater_than_is_operator_p))
3195 /* If we are within a cast, then the constraint that the
3196 cast is to an integral or enumeration type will be
3197 checked at that point. If we are not within a cast, then
3198 this code is invalid. */
3200 cp_parser_non_integral_constant_expression
3201 (parser, "floating-point literal");
3203 return token->u.value;
3209 /* ??? Should wide strings be allowed when parser->translate_strings_p
3210 is false (i.e. in attributes)? If not, we can kill the third
3211 argument to cp_parser_string_literal. */
3212 return cp_parser_string_literal (parser,
3213 parser->translate_strings_p,
3216 case CPP_OPEN_PAREN:
3219 bool saved_greater_than_is_operator_p;
3221 /* Consume the `('. */
3222 cp_lexer_consume_token (parser->lexer);
3223 /* Within a parenthesized expression, a `>' token is always
3224 the greater-than operator. */
3225 saved_greater_than_is_operator_p
3226 = parser->greater_than_is_operator_p;
3227 parser->greater_than_is_operator_p = true;
3228 /* If we see `( { ' then we are looking at the beginning of
3229 a GNU statement-expression. */
3230 if (cp_parser_allow_gnu_extensions_p (parser)
3231 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3233 /* Statement-expressions are not allowed by the standard. */
3234 pedwarn (token->location, OPT_pedantic,
3235 "ISO C++ forbids braced-groups within expressions");
3237 /* And they're not allowed outside of a function-body; you
3238 cannot, for example, write:
3240 int i = ({ int j = 3; j + 1; });
3242 at class or namespace scope. */
3243 if (!parser->in_function_body
3244 || parser->in_template_argument_list_p)
3246 error ("%Hstatement-expressions are not allowed outside "
3247 "functions nor in template-argument lists",
3249 cp_parser_skip_to_end_of_block_or_statement (parser);
3250 expr = error_mark_node;
3254 /* Start the statement-expression. */
3255 expr = begin_stmt_expr ();
3256 /* Parse the compound-statement. */
3257 cp_parser_compound_statement (parser, expr, false);
3259 expr = finish_stmt_expr (expr, false);
3264 /* Parse the parenthesized expression. */
3265 expr = cp_parser_expression (parser, cast_p, idk);
3266 /* Let the front end know that this expression was
3267 enclosed in parentheses. This matters in case, for
3268 example, the expression is of the form `A::B', since
3269 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3271 finish_parenthesized_expr (expr);
3273 /* The `>' token might be the end of a template-id or
3274 template-parameter-list now. */
3275 parser->greater_than_is_operator_p
3276 = saved_greater_than_is_operator_p;
3277 /* Consume the `)'. */
3278 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3279 cp_parser_skip_to_end_of_statement (parser);
3285 switch (token->keyword)
3287 /* These two are the boolean literals. */
3289 cp_lexer_consume_token (parser->lexer);
3290 return boolean_true_node;
3292 cp_lexer_consume_token (parser->lexer);
3293 return boolean_false_node;
3295 /* The `__null' literal. */
3297 cp_lexer_consume_token (parser->lexer);
3300 /* Recognize the `this' keyword. */
3302 cp_lexer_consume_token (parser->lexer);
3303 if (parser->local_variables_forbidden_p)
3305 error ("%H%<this%> may not be used in this context",
3307 return error_mark_node;
3309 /* Pointers cannot appear in constant-expressions. */
3310 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3311 return error_mark_node;
3312 return finish_this_expr ();
3314 /* The `operator' keyword can be the beginning of an
3319 case RID_FUNCTION_NAME:
3320 case RID_PRETTY_FUNCTION_NAME:
3321 case RID_C99_FUNCTION_NAME:
3325 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3326 __func__ are the names of variables -- but they are
3327 treated specially. Therefore, they are handled here,
3328 rather than relying on the generic id-expression logic
3329 below. Grammatically, these names are id-expressions.
3331 Consume the token. */
3332 token = cp_lexer_consume_token (parser->lexer);
3334 switch (token->keyword)
3336 case RID_FUNCTION_NAME:
3337 name = "%<__FUNCTION__%>";
3339 case RID_PRETTY_FUNCTION_NAME:
3340 name = "%<__PRETTY_FUNCTION__%>";
3342 case RID_C99_FUNCTION_NAME:
3343 name = "%<__func__%>";
3349 if (cp_parser_non_integral_constant_expression (parser, name))
3350 return error_mark_node;
3352 /* Look up the name. */
3353 return finish_fname (token->u.value);
3361 /* The `__builtin_va_arg' construct is used to handle
3362 `va_arg'. Consume the `__builtin_va_arg' token. */
3363 cp_lexer_consume_token (parser->lexer);
3364 /* Look for the opening `('. */
3365 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3366 /* Now, parse the assignment-expression. */
3367 expression = cp_parser_assignment_expression (parser,
3368 /*cast_p=*/false, NULL);
3369 /* Look for the `,'. */
3370 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3371 /* Parse the type-id. */
3372 type = cp_parser_type_id (parser);
3373 /* Look for the closing `)'. */
3374 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3375 /* Using `va_arg' in a constant-expression is not
3377 if (cp_parser_non_integral_constant_expression (parser,
3379 return error_mark_node;
3380 return build_x_va_arg (expression, type);
3384 return cp_parser_builtin_offsetof (parser);
3386 case RID_HAS_NOTHROW_ASSIGN:
3387 case RID_HAS_NOTHROW_CONSTRUCTOR:
3388 case RID_HAS_NOTHROW_COPY:
3389 case RID_HAS_TRIVIAL_ASSIGN:
3390 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3391 case RID_HAS_TRIVIAL_COPY:
3392 case RID_HAS_TRIVIAL_DESTRUCTOR:
3393 case RID_HAS_VIRTUAL_DESTRUCTOR:
3394 case RID_IS_ABSTRACT:
3395 case RID_IS_BASE_OF:
3397 case RID_IS_CONVERTIBLE_TO:
3401 case RID_IS_POLYMORPHIC:
3403 return cp_parser_trait_expr (parser, token->keyword);
3405 /* Objective-C++ expressions. */
3407 case RID_AT_PROTOCOL:
3408 case RID_AT_SELECTOR:
3409 return cp_parser_objc_expression (parser);
3412 cp_parser_error (parser, "expected primary-expression");
3413 return error_mark_node;
3416 /* An id-expression can start with either an identifier, a
3417 `::' as the beginning of a qualified-id, or the "operator"
3421 case CPP_TEMPLATE_ID:
3422 case CPP_NESTED_NAME_SPECIFIER:
3426 const char *error_msg;
3429 cp_token *id_expr_token;
3432 /* Parse the id-expression. */
3434 = cp_parser_id_expression (parser,
3435 /*template_keyword_p=*/false,
3436 /*check_dependency_p=*/true,
3438 /*declarator_p=*/false,
3439 /*optional_p=*/false);
3440 if (id_expression == error_mark_node)
3441 return error_mark_node;
3442 id_expr_token = token;
3443 token = cp_lexer_peek_token (parser->lexer);
3444 done = (token->type != CPP_OPEN_SQUARE
3445 && token->type != CPP_OPEN_PAREN
3446 && token->type != CPP_DOT
3447 && token->type != CPP_DEREF
3448 && token->type != CPP_PLUS_PLUS
3449 && token->type != CPP_MINUS_MINUS);
3450 /* If we have a template-id, then no further lookup is
3451 required. If the template-id was for a template-class, we
3452 will sometimes have a TYPE_DECL at this point. */
3453 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3454 || TREE_CODE (id_expression) == TYPE_DECL)
3455 decl = id_expression;
3456 /* Look up the name. */
3459 tree ambiguous_decls;
3461 decl = cp_parser_lookup_name (parser, id_expression,
3464 /*is_namespace=*/false,
3465 /*check_dependency=*/true,
3467 id_expr_token->location);
3468 /* If the lookup was ambiguous, an error will already have
3470 if (ambiguous_decls)
3471 return error_mark_node;
3473 /* In Objective-C++, an instance variable (ivar) may be preferred
3474 to whatever cp_parser_lookup_name() found. */
3475 decl = objc_lookup_ivar (decl, id_expression);
3477 /* If name lookup gives us a SCOPE_REF, then the
3478 qualifying scope was dependent. */
3479 if (TREE_CODE (decl) == SCOPE_REF)
3481 /* At this point, we do not know if DECL is a valid
3482 integral constant expression. We assume that it is
3483 in fact such an expression, so that code like:
3485 template <int N> struct A {
3489 is accepted. At template-instantiation time, we
3490 will check that B<N>::i is actually a constant. */
3493 /* Check to see if DECL is a local variable in a context
3494 where that is forbidden. */
3495 if (parser->local_variables_forbidden_p
3496 && local_variable_p (decl))
3498 /* It might be that we only found DECL because we are
3499 trying to be generous with pre-ISO scoping rules.
3500 For example, consider:
3504 for (int i = 0; i < 10; ++i) {}
3505 extern void f(int j = i);
3508 Here, name look up will originally find the out
3509 of scope `i'. We need to issue a warning message,
3510 but then use the global `i'. */
3511 decl = check_for_out_of_scope_variable (decl);
3512 if (local_variable_p (decl))
3514 error ("%Hlocal variable %qD may not appear in this context",
3515 &id_expr_token->location, decl);
3516 return error_mark_node;
3521 decl = (finish_id_expression
3522 (id_expression, decl, parser->scope,
3524 parser->integral_constant_expression_p,
3525 parser->allow_non_integral_constant_expression_p,
3526 &parser->non_integral_constant_expression_p,
3527 template_p, done, address_p,
3530 id_expr_token->location));
3532 cp_parser_error (parser, error_msg);
3536 /* Anything else is an error. */
3538 /* ...unless we have an Objective-C++ message or string literal,
3540 if (c_dialect_objc ()
3541 && (token->type == CPP_OPEN_SQUARE
3542 || token->type == CPP_OBJC_STRING))
3543 return cp_parser_objc_expression (parser);
3545 cp_parser_error (parser, "expected primary-expression");
3546 return error_mark_node;
3550 /* Parse an id-expression.
3557 :: [opt] nested-name-specifier template [opt] unqualified-id
3559 :: operator-function-id
3562 Return a representation of the unqualified portion of the
3563 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3564 a `::' or nested-name-specifier.
3566 Often, if the id-expression was a qualified-id, the caller will
3567 want to make a SCOPE_REF to represent the qualified-id. This
3568 function does not do this in order to avoid wastefully creating
3569 SCOPE_REFs when they are not required.
3571 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3574 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3575 uninstantiated templates.
3577 If *TEMPLATE_P is non-NULL, it is set to true iff the
3578 `template' keyword is used to explicitly indicate that the entity
3579 named is a template.
3581 If DECLARATOR_P is true, the id-expression is appearing as part of
3582 a declarator, rather than as part of an expression. */
3585 cp_parser_id_expression (cp_parser *parser,
3586 bool template_keyword_p,
3587 bool check_dependency_p,
3592 bool global_scope_p;
3593 bool nested_name_specifier_p;
3595 /* Assume the `template' keyword was not used. */
3597 *template_p = template_keyword_p;
3599 /* Look for the optional `::' operator. */
3601 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3603 /* Look for the optional nested-name-specifier. */
3604 nested_name_specifier_p
3605 = (cp_parser_nested_name_specifier_opt (parser,
3606 /*typename_keyword_p=*/false,
3611 /* If there is a nested-name-specifier, then we are looking at
3612 the first qualified-id production. */
3613 if (nested_name_specifier_p)
3616 tree saved_object_scope;
3617 tree saved_qualifying_scope;
3618 tree unqualified_id;
3621 /* See if the next token is the `template' keyword. */
3623 template_p = &is_template;
3624 *template_p = cp_parser_optional_template_keyword (parser);
3625 /* Name lookup we do during the processing of the
3626 unqualified-id might obliterate SCOPE. */
3627 saved_scope = parser->scope;
3628 saved_object_scope = parser->object_scope;
3629 saved_qualifying_scope = parser->qualifying_scope;
3630 /* Process the final unqualified-id. */
3631 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3634 /*optional_p=*/false);
3635 /* Restore the SAVED_SCOPE for our caller. */
3636 parser->scope = saved_scope;
3637 parser->object_scope = saved_object_scope;
3638 parser->qualifying_scope = saved_qualifying_scope;
3640 return unqualified_id;
3642 /* Otherwise, if we are in global scope, then we are looking at one
3643 of the other qualified-id productions. */
3644 else if (global_scope_p)
3649 /* Peek at the next token. */
3650 token = cp_lexer_peek_token (parser->lexer);
3652 /* If it's an identifier, and the next token is not a "<", then
3653 we can avoid the template-id case. This is an optimization
3654 for this common case. */
3655 if (token->type == CPP_NAME
3656 && !cp_parser_nth_token_starts_template_argument_list_p
3658 return cp_parser_identifier (parser);
3660 cp_parser_parse_tentatively (parser);
3661 /* Try a template-id. */
3662 id = cp_parser_template_id (parser,
3663 /*template_keyword_p=*/false,
3664 /*check_dependency_p=*/true,
3666 /* If that worked, we're done. */
3667 if (cp_parser_parse_definitely (parser))
3670 /* Peek at the next token. (Changes in the token buffer may
3671 have invalidated the pointer obtained above.) */
3672 token = cp_lexer_peek_token (parser->lexer);
3674 switch (token->type)
3677 return cp_parser_identifier (parser);
3680 if (token->keyword == RID_OPERATOR)
3681 return cp_parser_operator_function_id (parser);
3685 cp_parser_error (parser, "expected id-expression");
3686 return error_mark_node;
3690 return cp_parser_unqualified_id (parser, template_keyword_p,
3691 /*check_dependency_p=*/true,
3696 /* Parse an unqualified-id.
3700 operator-function-id
3701 conversion-function-id
3705 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3706 keyword, in a construct like `A::template ...'.
3708 Returns a representation of unqualified-id. For the `identifier'
3709 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3710 production a BIT_NOT_EXPR is returned; the operand of the
3711 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3712 other productions, see the documentation accompanying the
3713 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3714 names are looked up in uninstantiated templates. If DECLARATOR_P
3715 is true, the unqualified-id is appearing as part of a declarator,
3716 rather than as part of an expression. */
3719 cp_parser_unqualified_id (cp_parser* parser,
3720 bool template_keyword_p,
3721 bool check_dependency_p,
3727 /* Peek at the next token. */
3728 token = cp_lexer_peek_token (parser->lexer);
3730 switch (token->type)
3736 /* We don't know yet whether or not this will be a
3738 cp_parser_parse_tentatively (parser);
3739 /* Try a template-id. */
3740 id = cp_parser_template_id (parser, template_keyword_p,
3743 /* If it worked, we're done. */
3744 if (cp_parser_parse_definitely (parser))
3746 /* Otherwise, it's an ordinary identifier. */
3747 return cp_parser_identifier (parser);
3750 case CPP_TEMPLATE_ID:
3751 return cp_parser_template_id (parser, template_keyword_p,
3758 tree qualifying_scope;
3763 /* Consume the `~' token. */
3764 cp_lexer_consume_token (parser->lexer);
3765 /* Parse the class-name. The standard, as written, seems to
3768 template <typename T> struct S { ~S (); };
3769 template <typename T> S<T>::~S() {}
3771 is invalid, since `~' must be followed by a class-name, but
3772 `S<T>' is dependent, and so not known to be a class.
3773 That's not right; we need to look in uninstantiated
3774 templates. A further complication arises from:
3776 template <typename T> void f(T t) {
3780 Here, it is not possible to look up `T' in the scope of `T'
3781 itself. We must look in both the current scope, and the
3782 scope of the containing complete expression.
3784 Yet another issue is:
3793 The standard does not seem to say that the `S' in `~S'
3794 should refer to the type `S' and not the data member
3797 /* DR 244 says that we look up the name after the "~" in the
3798 same scope as we looked up the qualifying name. That idea
3799 isn't fully worked out; it's more complicated than that. */
3800 scope = parser->scope;
3801 object_scope = parser->object_scope;
3802 qualifying_scope = parser->qualifying_scope;
3804 /* Check for invalid scopes. */
3805 if (scope == error_mark_node)
3807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3808 cp_lexer_consume_token (parser->lexer);
3809 return error_mark_node;
3811 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3813 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3814 error ("%Hscope %qT before %<~%> is not a class-name",
3815 &token->location, scope);
3816 cp_parser_simulate_error (parser);
3817 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3818 cp_lexer_consume_token (parser->lexer);
3819 return error_mark_node;
3821 gcc_assert (!scope || TYPE_P (scope));
3823 /* If the name is of the form "X::~X" it's OK. */
3824 token = cp_lexer_peek_token (parser->lexer);
3826 && token->type == CPP_NAME
3827 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3829 && constructor_name_p (token->u.value, scope))
3831 cp_lexer_consume_token (parser->lexer);
3832 return build_nt (BIT_NOT_EXPR, scope);
3835 /* If there was an explicit qualification (S::~T), first look
3836 in the scope given by the qualification (i.e., S). */
3838 type_decl = NULL_TREE;
3841 cp_parser_parse_tentatively (parser);
3842 type_decl = cp_parser_class_name (parser,
3843 /*typename_keyword_p=*/false,
3844 /*template_keyword_p=*/false,
3846 /*check_dependency=*/false,
3847 /*class_head_p=*/false,
3849 if (cp_parser_parse_definitely (parser))
3852 /* In "N::S::~S", look in "N" as well. */
3853 if (!done && scope && qualifying_scope)
3855 cp_parser_parse_tentatively (parser);
3856 parser->scope = qualifying_scope;
3857 parser->object_scope = NULL_TREE;
3858 parser->qualifying_scope = NULL_TREE;
3860 = cp_parser_class_name (parser,
3861 /*typename_keyword_p=*/false,
3862 /*template_keyword_p=*/false,
3864 /*check_dependency=*/false,
3865 /*class_head_p=*/false,
3867 if (cp_parser_parse_definitely (parser))
3870 /* In "p->S::~T", look in the scope given by "*p" as well. */
3871 else if (!done && object_scope)
3873 cp_parser_parse_tentatively (parser);
3874 parser->scope = object_scope;
3875 parser->object_scope = NULL_TREE;
3876 parser->qualifying_scope = NULL_TREE;
3878 = cp_parser_class_name (parser,
3879 /*typename_keyword_p=*/false,
3880 /*template_keyword_p=*/false,
3882 /*check_dependency=*/false,
3883 /*class_head_p=*/false,
3885 if (cp_parser_parse_definitely (parser))
3888 /* Look in the surrounding context. */
3891 parser->scope = NULL_TREE;
3892 parser->object_scope = NULL_TREE;
3893 parser->qualifying_scope = NULL_TREE;
3894 if (processing_template_decl)
3895 cp_parser_parse_tentatively (parser);
3897 = cp_parser_class_name (parser,
3898 /*typename_keyword_p=*/false,
3899 /*template_keyword_p=*/false,
3901 /*check_dependency=*/false,
3902 /*class_head_p=*/false,
3904 if (processing_template_decl
3905 && ! cp_parser_parse_definitely (parser))
3907 /* We couldn't find a type with this name, so just accept
3908 it and check for a match at instantiation time. */
3909 type_decl = cp_parser_identifier (parser);
3910 if (type_decl != error_mark_node)
3911 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3915 /* If an error occurred, assume that the name of the
3916 destructor is the same as the name of the qualifying
3917 class. That allows us to keep parsing after running
3918 into ill-formed destructor names. */
3919 if (type_decl == error_mark_node && scope)
3920 return build_nt (BIT_NOT_EXPR, scope);
3921 else if (type_decl == error_mark_node)
3922 return error_mark_node;
3924 /* Check that destructor name and scope match. */
3925 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3927 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3928 error ("%Hdeclaration of %<~%T%> as member of %qT",
3929 &token->location, type_decl, scope);
3930 cp_parser_simulate_error (parser);
3931 return error_mark_node;
3936 A typedef-name that names a class shall not be used as the
3937 identifier in the declarator for a destructor declaration. */
3939 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3940 && !DECL_SELF_REFERENCE_P (type_decl)
3941 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3942 error ("%Htypedef-name %qD used as destructor declarator",
3943 &token->location, type_decl);
3945 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3949 if (token->keyword == RID_OPERATOR)
3953 /* This could be a template-id, so we try that first. */
3954 cp_parser_parse_tentatively (parser);
3955 /* Try a template-id. */
3956 id = cp_parser_template_id (parser, template_keyword_p,
3957 /*check_dependency_p=*/true,
3959 /* If that worked, we're done. */
3960 if (cp_parser_parse_definitely (parser))
3962 /* We still don't know whether we're looking at an
3963 operator-function-id or a conversion-function-id. */
3964 cp_parser_parse_tentatively (parser);
3965 /* Try an operator-function-id. */
3966 id = cp_parser_operator_function_id (parser);
3967 /* If that didn't work, try a conversion-function-id. */
3968 if (!cp_parser_parse_definitely (parser))
3969 id = cp_parser_conversion_function_id (parser);
3978 cp_parser_error (parser, "expected unqualified-id");
3979 return error_mark_node;
3983 /* Parse an (optional) nested-name-specifier.
3985 nested-name-specifier: [C++98]
3986 class-or-namespace-name :: nested-name-specifier [opt]
3987 class-or-namespace-name :: template nested-name-specifier [opt]
3989 nested-name-specifier: [C++0x]
3992 nested-name-specifier identifier ::
3993 nested-name-specifier template [opt] simple-template-id ::
3995 PARSER->SCOPE should be set appropriately before this function is
3996 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3997 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4000 Sets PARSER->SCOPE to the class (TYPE) or namespace
4001 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4002 it unchanged if there is no nested-name-specifier. Returns the new
4003 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4005 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4006 part of a declaration and/or decl-specifier. */
4009 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4010 bool typename_keyword_p,
4011 bool check_dependency_p,
4013 bool is_declaration)
4015 bool success = false;
4016 cp_token_position start = 0;
4019 /* Remember where the nested-name-specifier starts. */
4020 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4022 start = cp_lexer_token_position (parser->lexer, false);
4023 push_deferring_access_checks (dk_deferred);
4030 tree saved_qualifying_scope;
4031 bool template_keyword_p;
4033 /* Spot cases that cannot be the beginning of a
4034 nested-name-specifier. */
4035 token = cp_lexer_peek_token (parser->lexer);
4037 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4038 the already parsed nested-name-specifier. */
4039 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4041 /* Grab the nested-name-specifier and continue the loop. */
4042 cp_parser_pre_parsed_nested_name_specifier (parser);
4043 /* If we originally encountered this nested-name-specifier
4044 with IS_DECLARATION set to false, we will not have
4045 resolved TYPENAME_TYPEs, so we must do so here. */
4047 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4049 new_scope = resolve_typename_type (parser->scope,
4050 /*only_current_p=*/false);
4051 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4052 parser->scope = new_scope;
4058 /* Spot cases that cannot be the beginning of a
4059 nested-name-specifier. On the second and subsequent times
4060 through the loop, we look for the `template' keyword. */
4061 if (success && token->keyword == RID_TEMPLATE)
4063 /* A template-id can start a nested-name-specifier. */
4064 else if (token->type == CPP_TEMPLATE_ID)
4068 /* If the next token is not an identifier, then it is
4069 definitely not a type-name or namespace-name. */
4070 if (token->type != CPP_NAME)
4072 /* If the following token is neither a `<' (to begin a
4073 template-id), nor a `::', then we are not looking at a
4074 nested-name-specifier. */
4075 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4076 if (token->type != CPP_SCOPE
4077 && !cp_parser_nth_token_starts_template_argument_list_p
4082 /* The nested-name-specifier is optional, so we parse
4084 cp_parser_parse_tentatively (parser);
4086 /* Look for the optional `template' keyword, if this isn't the
4087 first time through the loop. */
4089 template_keyword_p = cp_parser_optional_template_keyword (parser);
4091 template_keyword_p = false;
4093 /* Save the old scope since the name lookup we are about to do
4094 might destroy it. */
4095 old_scope = parser->scope;
4096 saved_qualifying_scope = parser->qualifying_scope;
4097 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4098 look up names in "X<T>::I" in order to determine that "Y" is
4099 a template. So, if we have a typename at this point, we make
4100 an effort to look through it. */
4102 && !typename_keyword_p
4104 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4105 parser->scope = resolve_typename_type (parser->scope,
4106 /*only_current_p=*/false);
4107 /* Parse the qualifying entity. */
4109 = cp_parser_qualifying_entity (parser,
4115 /* Look for the `::' token. */
4116 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4118 /* If we found what we wanted, we keep going; otherwise, we're
4120 if (!cp_parser_parse_definitely (parser))
4122 bool error_p = false;
4124 /* Restore the OLD_SCOPE since it was valid before the
4125 failed attempt at finding the last
4126 class-or-namespace-name. */
4127 parser->scope = old_scope;
4128 parser->qualifying_scope = saved_qualifying_scope;
4129 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4131 /* If the next token is an identifier, and the one after
4132 that is a `::', then any valid interpretation would have
4133 found a class-or-namespace-name. */
4134 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4135 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4137 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4140 token = cp_lexer_consume_token (parser->lexer);
4143 if (!token->ambiguous_p)
4146 tree ambiguous_decls;
4148 decl = cp_parser_lookup_name (parser, token->u.value,
4150 /*is_template=*/false,
4151 /*is_namespace=*/false,
4152 /*check_dependency=*/true,
4155 if (TREE_CODE (decl) == TEMPLATE_DECL)
4156 error ("%H%qD used without template parameters",
4157 &token->location, decl);
4158 else if (ambiguous_decls)
4160 error ("%Hreference to %qD is ambiguous",
4161 &token->location, token->u.value);
4162 print_candidates (ambiguous_decls);
4163 decl = error_mark_node;
4167 const char* msg = "is not a class or namespace";
4168 if (cxx_dialect != cxx98)
4169 msg = "is not a class, namespace, or enumeration";
4170 cp_parser_name_lookup_error
4171 (parser, token->u.value, decl, msg,
4175 parser->scope = error_mark_node;
4177 /* Treat this as a successful nested-name-specifier
4182 If the name found is not a class-name (clause
4183 _class_) or namespace-name (_namespace.def_), the
4184 program is ill-formed. */
4187 cp_lexer_consume_token (parser->lexer);
4191 /* We've found one valid nested-name-specifier. */
4193 /* Name lookup always gives us a DECL. */
4194 if (TREE_CODE (new_scope) == TYPE_DECL)
4195 new_scope = TREE_TYPE (new_scope);
4196 /* Uses of "template" must be followed by actual templates. */
4197 if (template_keyword_p
4198 && !(CLASS_TYPE_P (new_scope)
4199 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4200 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4201 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4202 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4203 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4204 == TEMPLATE_ID_EXPR)))
4205 permerror (input_location, TYPE_P (new_scope)
4206 ? "%qT is not a template"
4207 : "%qD is not a template",
4209 /* If it is a class scope, try to complete it; we are about to
4210 be looking up names inside the class. */
4211 if (TYPE_P (new_scope)
4212 /* Since checking types for dependency can be expensive,
4213 avoid doing it if the type is already complete. */
4214 && !COMPLETE_TYPE_P (new_scope)
4215 /* Do not try to complete dependent types. */
4216 && !dependent_type_p (new_scope))
4218 new_scope = complete_type (new_scope);
4219 /* If it is a typedef to current class, use the current
4220 class instead, as the typedef won't have any names inside
4222 if (!COMPLETE_TYPE_P (new_scope)
4223 && currently_open_class (new_scope))
4224 new_scope = TYPE_MAIN_VARIANT (new_scope);
4226 /* Make sure we look in the right scope the next time through
4228 parser->scope = new_scope;
4231 /* If parsing tentatively, replace the sequence of tokens that makes
4232 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4233 token. That way, should we re-parse the token stream, we will
4234 not have to repeat the effort required to do the parse, nor will
4235 we issue duplicate error messages. */
4236 if (success && start)
4240 token = cp_lexer_token_at (parser->lexer, start);
4241 /* Reset the contents of the START token. */
4242 token->type = CPP_NESTED_NAME_SPECIFIER;
4243 /* Retrieve any deferred checks. Do not pop this access checks yet
4244 so the memory will not be reclaimed during token replacing below. */
4245 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4246 token->u.tree_check_value->value = parser->scope;
4247 token->u.tree_check_value->checks = get_deferred_access_checks ();
4248 token->u.tree_check_value->qualifying_scope =
4249 parser->qualifying_scope;
4250 token->keyword = RID_MAX;
4252 /* Purge all subsequent tokens. */
4253 cp_lexer_purge_tokens_after (parser->lexer, start);
4257 pop_to_parent_deferring_access_checks ();
4259 return success ? parser->scope : NULL_TREE;
4262 /* Parse a nested-name-specifier. See
4263 cp_parser_nested_name_specifier_opt for details. This function
4264 behaves identically, except that it will an issue an error if no
4265 nested-name-specifier is present. */
4268 cp_parser_nested_name_specifier (cp_parser *parser,
4269 bool typename_keyword_p,
4270 bool check_dependency_p,
4272 bool is_declaration)
4276 /* Look for the nested-name-specifier. */
4277 scope = cp_parser_nested_name_specifier_opt (parser,
4282 /* If it was not present, issue an error message. */
4285 cp_parser_error (parser, "expected nested-name-specifier");
4286 parser->scope = NULL_TREE;
4292 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4293 this is either a class-name or a namespace-name (which corresponds
4294 to the class-or-namespace-name production in the grammar). For
4295 C++0x, it can also be a type-name that refers to an enumeration
4298 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4299 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4300 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4301 TYPE_P is TRUE iff the next name should be taken as a class-name,
4302 even the same name is declared to be another entity in the same
4305 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4306 specified by the class-or-namespace-name. If neither is found the
4307 ERROR_MARK_NODE is returned. */
4310 cp_parser_qualifying_entity (cp_parser *parser,
4311 bool typename_keyword_p,
4312 bool template_keyword_p,
4313 bool check_dependency_p,
4315 bool is_declaration)
4318 tree saved_qualifying_scope;
4319 tree saved_object_scope;
4322 bool successful_parse_p;
4324 /* Before we try to parse the class-name, we must save away the
4325 current PARSER->SCOPE since cp_parser_class_name will destroy
4327 saved_scope = parser->scope;
4328 saved_qualifying_scope = parser->qualifying_scope;
4329 saved_object_scope = parser->object_scope;
4330 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4331 there is no need to look for a namespace-name. */
4332 only_class_p = template_keyword_p
4333 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4335 cp_parser_parse_tentatively (parser);
4336 scope = cp_parser_class_name (parser,
4339 type_p ? class_type : none_type,
4341 /*class_head_p=*/false,
4343 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4344 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4346 && cxx_dialect != cxx98
4347 && !successful_parse_p)
4349 /* Restore the saved scope. */
4350 parser->scope = saved_scope;
4351 parser->qualifying_scope = saved_qualifying_scope;
4352 parser->object_scope = saved_object_scope;
4354 /* Parse tentatively. */
4355 cp_parser_parse_tentatively (parser);
4357 /* Parse a typedef-name or enum-name. */
4358 scope = cp_parser_nonclass_name (parser);
4359 successful_parse_p = cp_parser_parse_definitely (parser);
4361 /* If that didn't work, try for a namespace-name. */
4362 if (!only_class_p && !successful_parse_p)
4364 /* Restore the saved scope. */
4365 parser->scope = saved_scope;
4366 parser->qualifying_scope = saved_qualifying_scope;
4367 parser->object_scope = saved_object_scope;
4368 /* If we are not looking at an identifier followed by the scope
4369 resolution operator, then this is not part of a
4370 nested-name-specifier. (Note that this function is only used
4371 to parse the components of a nested-name-specifier.) */
4372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4373 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4374 return error_mark_node;
4375 scope = cp_parser_namespace_name (parser);
4381 /* Parse a postfix-expression.
4385 postfix-expression [ expression ]
4386 postfix-expression ( expression-list [opt] )
4387 simple-type-specifier ( expression-list [opt] )
4388 typename :: [opt] nested-name-specifier identifier
4389 ( expression-list [opt] )
4390 typename :: [opt] nested-name-specifier template [opt] template-id
4391 ( expression-list [opt] )
4392 postfix-expression . template [opt] id-expression
4393 postfix-expression -> template [opt] id-expression
4394 postfix-expression . pseudo-destructor-name
4395 postfix-expression -> pseudo-destructor-name
4396 postfix-expression ++
4397 postfix-expression --
4398 dynamic_cast < type-id > ( expression )
4399 static_cast < type-id > ( expression )
4400 reinterpret_cast < type-id > ( expression )
4401 const_cast < type-id > ( expression )
4402 typeid ( expression )
4408 ( type-id ) { initializer-list , [opt] }
4410 This extension is a GNU version of the C99 compound-literal
4411 construct. (The C99 grammar uses `type-name' instead of `type-id',
4412 but they are essentially the same concept.)
4414 If ADDRESS_P is true, the postfix expression is the operand of the
4415 `&' operator. CAST_P is true if this expression is the target of a
4418 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4419 class member access expressions [expr.ref].
4421 Returns a representation of the expression. */
4424 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4425 bool member_access_only_p,
4426 cp_id_kind * pidk_return)
4430 cp_id_kind idk = CP_ID_KIND_NONE;
4431 tree postfix_expression = NULL_TREE;
4432 bool is_member_access = false;
4434 /* Peek at the next token. */
4435 token = cp_lexer_peek_token (parser->lexer);
4436 /* Some of the productions are determined by keywords. */
4437 keyword = token->keyword;
4447 const char *saved_message;
4449 /* All of these can be handled in the same way from the point
4450 of view of parsing. Begin by consuming the token
4451 identifying the cast. */
4452 cp_lexer_consume_token (parser->lexer);
4454 /* New types cannot be defined in the cast. */
4455 saved_message = parser->type_definition_forbidden_message;
4456 parser->type_definition_forbidden_message
4457 = "types may not be defined in casts";
4459 /* Look for the opening `<'. */
4460 cp_parser_require (parser, CPP_LESS, "%<<%>");
4461 /* Parse the type to which we are casting. */
4462 type = cp_parser_type_id (parser);
4463 /* Look for the closing `>'. */
4464 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4465 /* Restore the old message. */
4466 parser->type_definition_forbidden_message = saved_message;
4468 /* And the expression which is being cast. */
4469 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4471 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4473 /* Only type conversions to integral or enumeration types
4474 can be used in constant-expressions. */
4475 if (!cast_valid_in_integral_constant_expression_p (type)
4476 && (cp_parser_non_integral_constant_expression
4478 "a cast to a type other than an integral or "
4479 "enumeration type")))
4480 return error_mark_node;
4486 = build_dynamic_cast (type, expression, tf_warning_or_error);
4490 = build_static_cast (type, expression, tf_warning_or_error);
4494 = build_reinterpret_cast (type, expression,
4495 tf_warning_or_error);
4499 = build_const_cast (type, expression, tf_warning_or_error);
4510 const char *saved_message;
4511 bool saved_in_type_id_in_expr_p;
4513 /* Consume the `typeid' token. */
4514 cp_lexer_consume_token (parser->lexer);
4515 /* Look for the `(' token. */
4516 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4517 /* Types cannot be defined in a `typeid' expression. */
4518 saved_message = parser->type_definition_forbidden_message;
4519 parser->type_definition_forbidden_message
4520 = "types may not be defined in a %<typeid%> expression";
4521 /* We can't be sure yet whether we're looking at a type-id or an
4523 cp_parser_parse_tentatively (parser);
4524 /* Try a type-id first. */
4525 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4526 parser->in_type_id_in_expr_p = true;
4527 type = cp_parser_type_id (parser);
4528 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4529 /* Look for the `)' token. Otherwise, we can't be sure that
4530 we're not looking at an expression: consider `typeid (int
4531 (3))', for example. */
4532 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4533 /* If all went well, simply lookup the type-id. */
4534 if (cp_parser_parse_definitely (parser))
4535 postfix_expression = get_typeid (type);
4536 /* Otherwise, fall back to the expression variant. */
4541 /* Look for an expression. */
4542 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4543 /* Compute its typeid. */
4544 postfix_expression = build_typeid (expression);
4545 /* Look for the `)' token. */
4546 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4548 /* Restore the saved message. */
4549 parser->type_definition_forbidden_message = saved_message;
4550 /* `typeid' may not appear in an integral constant expression. */
4551 if (cp_parser_non_integral_constant_expression(parser,
4552 "%<typeid%> operator"))
4553 return error_mark_node;
4560 /* The syntax permitted here is the same permitted for an
4561 elaborated-type-specifier. */
4562 type = cp_parser_elaborated_type_specifier (parser,
4563 /*is_friend=*/false,
4564 /*is_declaration=*/false);
4565 postfix_expression = cp_parser_functional_cast (parser, type);
4573 /* If the next thing is a simple-type-specifier, we may be
4574 looking at a functional cast. We could also be looking at
4575 an id-expression. So, we try the functional cast, and if
4576 that doesn't work we fall back to the primary-expression. */
4577 cp_parser_parse_tentatively (parser);
4578 /* Look for the simple-type-specifier. */
4579 type = cp_parser_simple_type_specifier (parser,
4580 /*decl_specs=*/NULL,
4581 CP_PARSER_FLAGS_NONE);
4582 /* Parse the cast itself. */
4583 if (!cp_parser_error_occurred (parser))
4585 = cp_parser_functional_cast (parser, type);
4586 /* If that worked, we're done. */
4587 if (cp_parser_parse_definitely (parser))
4590 /* If the functional-cast didn't work out, try a
4591 compound-literal. */
4592 if (cp_parser_allow_gnu_extensions_p (parser)
4593 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4595 VEC(constructor_elt,gc) *initializer_list = NULL;
4596 bool saved_in_type_id_in_expr_p;
4598 cp_parser_parse_tentatively (parser);
4599 /* Consume the `('. */
4600 cp_lexer_consume_token (parser->lexer);
4601 /* Parse the type. */
4602 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4603 parser->in_type_id_in_expr_p = true;
4604 type = cp_parser_type_id (parser);
4605 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4606 /* Look for the `)'. */
4607 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4608 /* Look for the `{'. */
4609 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4610 /* If things aren't going well, there's no need to
4612 if (!cp_parser_error_occurred (parser))
4614 bool non_constant_p;
4615 /* Parse the initializer-list. */
4617 = cp_parser_initializer_list (parser, &non_constant_p);
4618 /* Allow a trailing `,'. */
4619 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4620 cp_lexer_consume_token (parser->lexer);
4621 /* Look for the final `}'. */
4622 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4624 /* If that worked, we're definitely looking at a
4625 compound-literal expression. */
4626 if (cp_parser_parse_definitely (parser))
4628 /* Warn the user that a compound literal is not
4629 allowed in standard C++. */
4630 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4631 /* For simplicity, we disallow compound literals in
4632 constant-expressions. We could
4633 allow compound literals of integer type, whose
4634 initializer was a constant, in constant
4635 expressions. Permitting that usage, as a further
4636 extension, would not change the meaning of any
4637 currently accepted programs. (Of course, as
4638 compound literals are not part of ISO C++, the
4639 standard has nothing to say.) */
4640 if (cp_parser_non_integral_constant_expression
4641 (parser, "non-constant compound literals"))
4643 postfix_expression = error_mark_node;
4646 /* Form the representation of the compound-literal. */
4648 = (finish_compound_literal
4649 (type, build_constructor (init_list_type_node,
4650 initializer_list)));
4655 /* It must be a primary-expression. */
4657 = cp_parser_primary_expression (parser, address_p, cast_p,
4658 /*template_arg_p=*/false,
4664 /* Keep looping until the postfix-expression is complete. */
4667 if (idk == CP_ID_KIND_UNQUALIFIED
4668 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4669 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4670 /* It is not a Koenig lookup function call. */
4672 = unqualified_name_lookup_error (postfix_expression);
4674 /* Peek at the next token. */
4675 token = cp_lexer_peek_token (parser->lexer);
4677 switch (token->type)
4679 case CPP_OPEN_SQUARE:
4681 = cp_parser_postfix_open_square_expression (parser,
4684 idk = CP_ID_KIND_NONE;
4685 is_member_access = false;
4688 case CPP_OPEN_PAREN:
4689 /* postfix-expression ( expression-list [opt] ) */
4692 bool is_builtin_constant_p;
4693 bool saved_integral_constant_expression_p = false;
4694 bool saved_non_integral_constant_expression_p = false;
4697 is_member_access = false;
4699 is_builtin_constant_p
4700 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4701 if (is_builtin_constant_p)
4703 /* The whole point of __builtin_constant_p is to allow
4704 non-constant expressions to appear as arguments. */
4705 saved_integral_constant_expression_p
4706 = parser->integral_constant_expression_p;
4707 saved_non_integral_constant_expression_p
4708 = parser->non_integral_constant_expression_p;
4709 parser->integral_constant_expression_p = false;
4711 args = (cp_parser_parenthesized_expression_list
4712 (parser, /*is_attribute_list=*/false,
4713 /*cast_p=*/false, /*allow_expansion_p=*/true,
4714 /*non_constant_p=*/NULL));
4715 if (is_builtin_constant_p)
4717 parser->integral_constant_expression_p
4718 = saved_integral_constant_expression_p;
4719 parser->non_integral_constant_expression_p
4720 = saved_non_integral_constant_expression_p;
4723 if (args == error_mark_node)
4725 postfix_expression = error_mark_node;
4729 /* Function calls are not permitted in
4730 constant-expressions. */
4731 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4732 && cp_parser_non_integral_constant_expression (parser,
4735 postfix_expression = error_mark_node;
4740 if (idk == CP_ID_KIND_UNQUALIFIED
4741 || idk == CP_ID_KIND_TEMPLATE_ID)
4743 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4748 if (!any_type_dependent_arguments_p (args))
4750 = perform_koenig_lookup (postfix_expression, args);
4754 = unqualified_fn_lookup_error (postfix_expression);
4756 /* We do not perform argument-dependent lookup if
4757 normal lookup finds a non-function, in accordance
4758 with the expected resolution of DR 218. */
4759 else if (args && is_overloaded_fn (postfix_expression))
4761 tree fn = get_first_fn (postfix_expression);
4763 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4764 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4766 /* Only do argument dependent lookup if regular
4767 lookup does not find a set of member functions.
4768 [basic.lookup.koenig]/2a */
4769 if (!DECL_FUNCTION_MEMBER_P (fn))
4772 if (!any_type_dependent_arguments_p (args))
4774 = perform_koenig_lookup (postfix_expression, args);
4779 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4781 tree instance = TREE_OPERAND (postfix_expression, 0);
4782 tree fn = TREE_OPERAND (postfix_expression, 1);
4784 if (processing_template_decl
4785 && (type_dependent_expression_p (instance)
4786 || (!BASELINK_P (fn)
4787 && TREE_CODE (fn) != FIELD_DECL)
4788 || type_dependent_expression_p (fn)
4789 || any_type_dependent_arguments_p (args)))
4792 = build_nt_call_list (postfix_expression, args);
4796 if (BASELINK_P (fn))
4799 = (build_new_method_call
4800 (instance, fn, args, NULL_TREE,
4801 (idk == CP_ID_KIND_QUALIFIED
4802 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4804 tf_warning_or_error));
4808 = finish_call_expr (postfix_expression, args,
4809 /*disallow_virtual=*/false,
4811 tf_warning_or_error);
4813 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4814 || TREE_CODE (postfix_expression) == MEMBER_REF
4815 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4816 postfix_expression = (build_offset_ref_call_from_tree
4817 (postfix_expression, args));
4818 else if (idk == CP_ID_KIND_QUALIFIED)
4819 /* A call to a static class member, or a namespace-scope
4822 = finish_call_expr (postfix_expression, args,
4823 /*disallow_virtual=*/true,
4825 tf_warning_or_error);
4827 /* All other function calls. */
4829 = finish_call_expr (postfix_expression, args,
4830 /*disallow_virtual=*/false,
4832 tf_warning_or_error);
4834 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4835 idk = CP_ID_KIND_NONE;
4841 /* postfix-expression . template [opt] id-expression
4842 postfix-expression . pseudo-destructor-name
4843 postfix-expression -> template [opt] id-expression
4844 postfix-expression -> pseudo-destructor-name */
4846 /* Consume the `.' or `->' operator. */
4847 cp_lexer_consume_token (parser->lexer);
4850 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4855 is_member_access = true;
4859 /* postfix-expression ++ */
4860 /* Consume the `++' token. */
4861 cp_lexer_consume_token (parser->lexer);
4862 /* Generate a representation for the complete expression. */
4864 = finish_increment_expr (postfix_expression,
4865 POSTINCREMENT_EXPR);
4866 /* Increments may not appear in constant-expressions. */
4867 if (cp_parser_non_integral_constant_expression (parser,
4869 postfix_expression = error_mark_node;
4870 idk = CP_ID_KIND_NONE;
4871 is_member_access = false;
4874 case CPP_MINUS_MINUS:
4875 /* postfix-expression -- */
4876 /* Consume the `--' token. */
4877 cp_lexer_consume_token (parser->lexer);
4878 /* Generate a representation for the complete expression. */
4880 = finish_increment_expr (postfix_expression,
4881 POSTDECREMENT_EXPR);
4882 /* Decrements may not appear in constant-expressions. */
4883 if (cp_parser_non_integral_constant_expression (parser,
4885 postfix_expression = error_mark_node;
4886 idk = CP_ID_KIND_NONE;
4887 is_member_access = false;
4891 if (pidk_return != NULL)
4892 * pidk_return = idk;
4893 if (member_access_only_p)
4894 return is_member_access? postfix_expression : error_mark_node;
4896 return postfix_expression;
4900 /* We should never get here. */
4902 return error_mark_node;
4905 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4906 by cp_parser_builtin_offsetof. We're looking for
4908 postfix-expression [ expression ]
4910 FOR_OFFSETOF is set if we're being called in that context, which
4911 changes how we deal with integer constant expressions. */
4914 cp_parser_postfix_open_square_expression (cp_parser *parser,
4915 tree postfix_expression,
4920 /* Consume the `[' token. */
4921 cp_lexer_consume_token (parser->lexer);
4923 /* Parse the index expression. */
4924 /* ??? For offsetof, there is a question of what to allow here. If
4925 offsetof is not being used in an integral constant expression context,
4926 then we *could* get the right answer by computing the value at runtime.
4927 If we are in an integral constant expression context, then we might
4928 could accept any constant expression; hard to say without analysis.
4929 Rather than open the barn door too wide right away, allow only integer
4930 constant expressions here. */
4932 index = cp_parser_constant_expression (parser, false, NULL);
4934 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4936 /* Look for the closing `]'. */
4937 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4939 /* Build the ARRAY_REF. */
4940 postfix_expression = grok_array_decl (postfix_expression, index);
4942 /* When not doing offsetof, array references are not permitted in
4943 constant-expressions. */
4945 && (cp_parser_non_integral_constant_expression
4946 (parser, "an array reference")))
4947 postfix_expression = error_mark_node;
4949 return postfix_expression;
4952 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4953 by cp_parser_builtin_offsetof. We're looking for
4955 postfix-expression . template [opt] id-expression
4956 postfix-expression . pseudo-destructor-name
4957 postfix-expression -> template [opt] id-expression
4958 postfix-expression -> pseudo-destructor-name
4960 FOR_OFFSETOF is set if we're being called in that context. That sorta
4961 limits what of the above we'll actually accept, but nevermind.
4962 TOKEN_TYPE is the "." or "->" token, which will already have been
4963 removed from the stream. */
4966 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4967 enum cpp_ttype token_type,
4968 tree postfix_expression,
4969 bool for_offsetof, cp_id_kind *idk,
4970 location_t location)
4974 bool pseudo_destructor_p;
4975 tree scope = NULL_TREE;
4977 /* If this is a `->' operator, dereference the pointer. */
4978 if (token_type == CPP_DEREF)
4979 postfix_expression = build_x_arrow (postfix_expression);
4980 /* Check to see whether or not the expression is type-dependent. */
4981 dependent_p = type_dependent_expression_p (postfix_expression);
4982 /* The identifier following the `->' or `.' is not qualified. */
4983 parser->scope = NULL_TREE;
4984 parser->qualifying_scope = NULL_TREE;
4985 parser->object_scope = NULL_TREE;
4986 *idk = CP_ID_KIND_NONE;
4988 /* Enter the scope corresponding to the type of the object
4989 given by the POSTFIX_EXPRESSION. */
4990 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4992 scope = TREE_TYPE (postfix_expression);
4993 /* According to the standard, no expression should ever have
4994 reference type. Unfortunately, we do not currently match
4995 the standard in this respect in that our internal representation
4996 of an expression may have reference type even when the standard
4997 says it does not. Therefore, we have to manually obtain the
4998 underlying type here. */
4999 scope = non_reference (scope);
5000 /* The type of the POSTFIX_EXPRESSION must be complete. */
5001 if (scope == unknown_type_node)
5003 error ("%H%qE does not have class type", &location, postfix_expression);
5007 scope = complete_type_or_else (scope, NULL_TREE);
5008 /* Let the name lookup machinery know that we are processing a
5009 class member access expression. */
5010 parser->context->object_type = scope;
5011 /* If something went wrong, we want to be able to discern that case,
5012 as opposed to the case where there was no SCOPE due to the type
5013 of expression being dependent. */
5015 scope = error_mark_node;
5016 /* If the SCOPE was erroneous, make the various semantic analysis
5017 functions exit quickly -- and without issuing additional error
5019 if (scope == error_mark_node)
5020 postfix_expression = error_mark_node;
5023 /* Assume this expression is not a pseudo-destructor access. */
5024 pseudo_destructor_p = false;
5026 /* If the SCOPE is a scalar type, then, if this is a valid program,
5027 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5028 is type dependent, it can be pseudo-destructor-name or something else.
5029 Try to parse it as pseudo-destructor-name first. */
5030 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5035 cp_parser_parse_tentatively (parser);
5036 /* Parse the pseudo-destructor-name. */
5038 cp_parser_pseudo_destructor_name (parser, &s, &type);
5040 && (cp_parser_error_occurred (parser)
5041 || TREE_CODE (type) != TYPE_DECL
5042 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5043 cp_parser_abort_tentative_parse (parser);
5044 else if (cp_parser_parse_definitely (parser))
5046 pseudo_destructor_p = true;
5048 = finish_pseudo_destructor_expr (postfix_expression,
5049 s, TREE_TYPE (type));
5053 if (!pseudo_destructor_p)
5055 /* If the SCOPE is not a scalar type, we are looking at an
5056 ordinary class member access expression, rather than a
5057 pseudo-destructor-name. */
5059 cp_token *token = cp_lexer_peek_token (parser->lexer);
5060 /* Parse the id-expression. */
5061 name = (cp_parser_id_expression
5063 cp_parser_optional_template_keyword (parser),
5064 /*check_dependency_p=*/true,
5066 /*declarator_p=*/false,
5067 /*optional_p=*/false));
5068 /* In general, build a SCOPE_REF if the member name is qualified.
5069 However, if the name was not dependent and has already been
5070 resolved; there is no need to build the SCOPE_REF. For example;
5072 struct X { void f(); };
5073 template <typename T> void f(T* t) { t->X::f(); }
5075 Even though "t" is dependent, "X::f" is not and has been resolved
5076 to a BASELINK; there is no need to include scope information. */
5078 /* But we do need to remember that there was an explicit scope for
5079 virtual function calls. */
5081 *idk = CP_ID_KIND_QUALIFIED;
5083 /* If the name is a template-id that names a type, we will get a
5084 TYPE_DECL here. That is invalid code. */
5085 if (TREE_CODE (name) == TYPE_DECL)
5087 error ("%Hinvalid use of %qD", &token->location, name);
5088 postfix_expression = error_mark_node;
5092 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5094 name = build_qualified_name (/*type=*/NULL_TREE,
5098 parser->scope = NULL_TREE;
5099 parser->qualifying_scope = NULL_TREE;
5100 parser->object_scope = NULL_TREE;
5102 if (scope && name && BASELINK_P (name))
5103 adjust_result_of_qualified_name_lookup
5104 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5106 = finish_class_member_access_expr (postfix_expression, name,
5108 tf_warning_or_error);
5112 /* We no longer need to look up names in the scope of the object on
5113 the left-hand side of the `.' or `->' operator. */
5114 parser->context->object_type = NULL_TREE;
5116 /* Outside of offsetof, these operators may not appear in
5117 constant-expressions. */
5119 && (cp_parser_non_integral_constant_expression
5120 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5121 postfix_expression = error_mark_node;
5123 return postfix_expression;
5126 /* Parse a parenthesized expression-list.
5129 assignment-expression
5130 expression-list, assignment-expression
5135 identifier, expression-list
5137 CAST_P is true if this expression is the target of a cast.
5139 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5142 Returns a TREE_LIST. The TREE_VALUE of each node is a
5143 representation of an assignment-expression. Note that a TREE_LIST
5144 is returned even if there is only a single expression in the list.
5145 error_mark_node is returned if the ( and or ) are
5146 missing. NULL_TREE is returned on no expressions. The parentheses
5147 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5148 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5149 indicates whether or not all of the expressions in the list were
5153 cp_parser_parenthesized_expression_list (cp_parser* parser,
5154 bool is_attribute_list,
5156 bool allow_expansion_p,
5157 bool *non_constant_p)
5159 tree expression_list = NULL_TREE;
5160 bool fold_expr_p = is_attribute_list;
5161 tree identifier = NULL_TREE;
5162 bool saved_greater_than_is_operator_p;
5164 /* Assume all the expressions will be constant. */
5166 *non_constant_p = false;
5168 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5169 return error_mark_node;
5171 /* Within a parenthesized expression, a `>' token is always
5172 the greater-than operator. */
5173 saved_greater_than_is_operator_p
5174 = parser->greater_than_is_operator_p;
5175 parser->greater_than_is_operator_p = true;
5177 /* Consume expressions until there are no more. */
5178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5183 /* At the beginning of attribute lists, check to see if the
5184 next token is an identifier. */
5185 if (is_attribute_list
5186 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5190 /* Consume the identifier. */
5191 token = cp_lexer_consume_token (parser->lexer);
5192 /* Save the identifier. */
5193 identifier = token->u.value;
5197 bool expr_non_constant_p;
5199 /* Parse the next assignment-expression. */
5200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5202 /* A braced-init-list. */
5203 maybe_warn_cpp0x ("extended initializer lists");
5204 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5205 if (non_constant_p && expr_non_constant_p)
5206 *non_constant_p = true;
5208 else if (non_constant_p)
5210 expr = (cp_parser_constant_expression
5211 (parser, /*allow_non_constant_p=*/true,
5212 &expr_non_constant_p));
5213 if (expr_non_constant_p)
5214 *non_constant_p = true;
5217 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5220 expr = fold_non_dependent_expr (expr);
5222 /* If we have an ellipsis, then this is an expression
5224 if (allow_expansion_p
5225 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5227 /* Consume the `...'. */
5228 cp_lexer_consume_token (parser->lexer);
5230 /* Build the argument pack. */
5231 expr = make_pack_expansion (expr);
5234 /* Add it to the list. We add error_mark_node
5235 expressions to the list, so that we can still tell if
5236 the correct form for a parenthesized expression-list
5237 is found. That gives better errors. */
5238 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5240 if (expr == error_mark_node)
5244 /* After the first item, attribute lists look the same as
5245 expression lists. */
5246 is_attribute_list = false;
5249 /* If the next token isn't a `,', then we are done. */
5250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5253 /* Otherwise, consume the `,' and keep going. */
5254 cp_lexer_consume_token (parser->lexer);
5257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5262 /* We try and resync to an unnested comma, as that will give the
5263 user better diagnostics. */
5264 ending = cp_parser_skip_to_closing_parenthesis (parser,
5265 /*recovering=*/true,
5267 /*consume_paren=*/true);
5272 parser->greater_than_is_operator_p
5273 = saved_greater_than_is_operator_p;
5274 return error_mark_node;
5278 parser->greater_than_is_operator_p
5279 = saved_greater_than_is_operator_p;
5281 /* We built up the list in reverse order so we must reverse it now. */
5282 expression_list = nreverse (expression_list);
5284 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5286 return expression_list;
5289 /* Parse a pseudo-destructor-name.
5291 pseudo-destructor-name:
5292 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5293 :: [opt] nested-name-specifier template template-id :: ~ type-name
5294 :: [opt] nested-name-specifier [opt] ~ type-name
5296 If either of the first two productions is used, sets *SCOPE to the
5297 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5298 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5299 or ERROR_MARK_NODE if the parse fails. */
5302 cp_parser_pseudo_destructor_name (cp_parser* parser,
5306 bool nested_name_specifier_p;
5308 /* Assume that things will not work out. */
5309 *type = error_mark_node;
5311 /* Look for the optional `::' operator. */
5312 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5313 /* Look for the optional nested-name-specifier. */
5314 nested_name_specifier_p
5315 = (cp_parser_nested_name_specifier_opt (parser,
5316 /*typename_keyword_p=*/false,
5317 /*check_dependency_p=*/true,
5319 /*is_declaration=*/false)
5321 /* Now, if we saw a nested-name-specifier, we might be doing the
5322 second production. */
5323 if (nested_name_specifier_p
5324 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5326 /* Consume the `template' keyword. */
5327 cp_lexer_consume_token (parser->lexer);
5328 /* Parse the template-id. */
5329 cp_parser_template_id (parser,
5330 /*template_keyword_p=*/true,
5331 /*check_dependency_p=*/false,
5332 /*is_declaration=*/true);
5333 /* Look for the `::' token. */
5334 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5336 /* If the next token is not a `~', then there might be some
5337 additional qualification. */
5338 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5340 /* At this point, we're looking for "type-name :: ~". The type-name
5341 must not be a class-name, since this is a pseudo-destructor. So,
5342 it must be either an enum-name, or a typedef-name -- both of which
5343 are just identifiers. So, we peek ahead to check that the "::"
5344 and "~" tokens are present; if they are not, then we can avoid
5345 calling type_name. */
5346 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5347 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5348 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5350 cp_parser_error (parser, "non-scalar type");
5354 /* Look for the type-name. */
5355 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5356 if (*scope == error_mark_node)
5359 /* Look for the `::' token. */
5360 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5365 /* Look for the `~'. */
5366 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5367 /* Look for the type-name again. We are not responsible for
5368 checking that it matches the first type-name. */
5369 *type = cp_parser_nonclass_name (parser);
5372 /* Parse a unary-expression.
5378 unary-operator cast-expression
5379 sizeof unary-expression
5387 __extension__ cast-expression
5388 __alignof__ unary-expression
5389 __alignof__ ( type-id )
5390 __real__ cast-expression
5391 __imag__ cast-expression
5394 ADDRESS_P is true iff the unary-expression is appearing as the
5395 operand of the `&' operator. CAST_P is true if this expression is
5396 the target of a cast.
5398 Returns a representation of the expression. */
5401 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5405 enum tree_code unary_operator;
5407 /* Peek at the next token. */
5408 token = cp_lexer_peek_token (parser->lexer);
5409 /* Some keywords give away the kind of expression. */
5410 if (token->type == CPP_KEYWORD)
5412 enum rid keyword = token->keyword;
5422 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5423 /* Consume the token. */
5424 cp_lexer_consume_token (parser->lexer);
5425 /* Parse the operand. */
5426 operand = cp_parser_sizeof_operand (parser, keyword);
5428 if (TYPE_P (operand))
5429 return cxx_sizeof_or_alignof_type (operand, op, true);
5431 return cxx_sizeof_or_alignof_expr (operand, op, true);
5435 return cp_parser_new_expression (parser);
5438 return cp_parser_delete_expression (parser);
5442 /* The saved value of the PEDANTIC flag. */
5446 /* Save away the PEDANTIC flag. */
5447 cp_parser_extension_opt (parser, &saved_pedantic);
5448 /* Parse the cast-expression. */
5449 expr = cp_parser_simple_cast_expression (parser);
5450 /* Restore the PEDANTIC flag. */
5451 pedantic = saved_pedantic;
5461 /* Consume the `__real__' or `__imag__' token. */
5462 cp_lexer_consume_token (parser->lexer);
5463 /* Parse the cast-expression. */
5464 expression = cp_parser_simple_cast_expression (parser);
5465 /* Create the complete representation. */
5466 return build_x_unary_op ((keyword == RID_REALPART
5467 ? REALPART_EXPR : IMAGPART_EXPR),
5469 tf_warning_or_error);
5478 /* Look for the `:: new' and `:: delete', which also signal the
5479 beginning of a new-expression, or delete-expression,
5480 respectively. If the next token is `::', then it might be one of
5482 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5486 /* See if the token after the `::' is one of the keywords in
5487 which we're interested. */
5488 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5489 /* If it's `new', we have a new-expression. */
5490 if (keyword == RID_NEW)
5491 return cp_parser_new_expression (parser);
5492 /* Similarly, for `delete'. */
5493 else if (keyword == RID_DELETE)
5494 return cp_parser_delete_expression (parser);
5497 /* Look for a unary operator. */
5498 unary_operator = cp_parser_unary_operator (token);
5499 /* The `++' and `--' operators can be handled similarly, even though
5500 they are not technically unary-operators in the grammar. */
5501 if (unary_operator == ERROR_MARK)
5503 if (token->type == CPP_PLUS_PLUS)
5504 unary_operator = PREINCREMENT_EXPR;
5505 else if (token->type == CPP_MINUS_MINUS)
5506 unary_operator = PREDECREMENT_EXPR;
5507 /* Handle the GNU address-of-label extension. */
5508 else if (cp_parser_allow_gnu_extensions_p (parser)
5509 && token->type == CPP_AND_AND)
5513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5515 /* Consume the '&&' token. */
5516 cp_lexer_consume_token (parser->lexer);
5517 /* Look for the identifier. */
5518 identifier = cp_parser_identifier (parser);
5519 /* Create an expression representing the address. */
5520 expression = finish_label_address_expr (identifier, loc);
5521 if (cp_parser_non_integral_constant_expression (parser,
5522 "the address of a label"))
5523 expression = error_mark_node;
5527 if (unary_operator != ERROR_MARK)
5529 tree cast_expression;
5530 tree expression = error_mark_node;
5531 const char *non_constant_p = NULL;
5533 /* Consume the operator token. */
5534 token = cp_lexer_consume_token (parser->lexer);
5535 /* Parse the cast-expression. */
5537 = cp_parser_cast_expression (parser,
5538 unary_operator == ADDR_EXPR,
5539 /*cast_p=*/false, pidk);
5540 /* Now, build an appropriate representation. */
5541 switch (unary_operator)
5544 non_constant_p = "%<*%>";
5545 expression = build_x_indirect_ref (cast_expression, "unary *",
5546 tf_warning_or_error);
5550 non_constant_p = "%<&%>";
5553 expression = build_x_unary_op (unary_operator, cast_expression,
5554 tf_warning_or_error);
5557 case PREINCREMENT_EXPR:
5558 case PREDECREMENT_EXPR:
5559 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5560 ? "%<++%>" : "%<--%>");
5562 case UNARY_PLUS_EXPR:
5564 case TRUTH_NOT_EXPR:
5565 expression = finish_unary_op_expr (unary_operator, cast_expression);
5573 && cp_parser_non_integral_constant_expression (parser,
5575 expression = error_mark_node;
5580 return cp_parser_postfix_expression (parser, address_p, cast_p,
5581 /*member_access_only_p=*/false,
5585 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5586 unary-operator, the corresponding tree code is returned. */
5588 static enum tree_code
5589 cp_parser_unary_operator (cp_token* token)
5591 switch (token->type)
5594 return INDIRECT_REF;
5600 return UNARY_PLUS_EXPR;
5606 return TRUTH_NOT_EXPR;
5609 return BIT_NOT_EXPR;
5616 /* Parse a new-expression.
5619 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5620 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5622 Returns a representation of the expression. */
5625 cp_parser_new_expression (cp_parser* parser)
5627 bool global_scope_p;
5633 /* Look for the optional `::' operator. */
5635 = (cp_parser_global_scope_opt (parser,
5636 /*current_scope_valid_p=*/false)
5638 /* Look for the `new' operator. */
5639 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5640 /* There's no easy way to tell a new-placement from the
5641 `( type-id )' construct. */
5642 cp_parser_parse_tentatively (parser);
5643 /* Look for a new-placement. */
5644 placement = cp_parser_new_placement (parser);
5645 /* If that didn't work out, there's no new-placement. */
5646 if (!cp_parser_parse_definitely (parser))
5647 placement = NULL_TREE;
5649 /* If the next token is a `(', then we have a parenthesized
5651 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5654 /* Consume the `('. */
5655 cp_lexer_consume_token (parser->lexer);
5656 /* Parse the type-id. */
5657 type = cp_parser_type_id (parser);
5658 /* Look for the closing `)'. */
5659 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5660 token = cp_lexer_peek_token (parser->lexer);
5661 /* There should not be a direct-new-declarator in this production,
5662 but GCC used to allowed this, so we check and emit a sensible error
5663 message for this case. */
5664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5666 error ("%Harray bound forbidden after parenthesized type-id",
5668 inform (token->location,
5669 "try removing the parentheses around the type-id");
5670 cp_parser_direct_new_declarator (parser);
5674 /* Otherwise, there must be a new-type-id. */
5676 type = cp_parser_new_type_id (parser, &nelts);
5678 /* If the next token is a `(' or '{', then we have a new-initializer. */
5679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5680 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5681 initializer = cp_parser_new_initializer (parser);
5683 initializer = NULL_TREE;
5685 /* A new-expression may not appear in an integral constant
5687 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5688 return error_mark_node;
5690 /* Create a representation of the new-expression. */
5691 return build_new (placement, type, nelts, initializer, global_scope_p,
5692 tf_warning_or_error);
5695 /* Parse a new-placement.
5700 Returns the same representation as for an expression-list. */
5703 cp_parser_new_placement (cp_parser* parser)
5705 tree expression_list;
5707 /* Parse the expression-list. */
5708 expression_list = (cp_parser_parenthesized_expression_list
5709 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5710 /*non_constant_p=*/NULL));
5712 return expression_list;
5715 /* Parse a new-type-id.
5718 type-specifier-seq new-declarator [opt]
5720 Returns the TYPE allocated. If the new-type-id indicates an array
5721 type, *NELTS is set to the number of elements in the last array
5722 bound; the TYPE will not include the last array bound. */
5725 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5727 cp_decl_specifier_seq type_specifier_seq;
5728 cp_declarator *new_declarator;
5729 cp_declarator *declarator;
5730 cp_declarator *outer_declarator;
5731 const char *saved_message;
5734 /* The type-specifier sequence must not contain type definitions.
5735 (It cannot contain declarations of new types either, but if they
5736 are not definitions we will catch that because they are not
5738 saved_message = parser->type_definition_forbidden_message;
5739 parser->type_definition_forbidden_message
5740 = "types may not be defined in a new-type-id";
5741 /* Parse the type-specifier-seq. */
5742 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5743 &type_specifier_seq);
5744 /* Restore the old message. */
5745 parser->type_definition_forbidden_message = saved_message;
5746 /* Parse the new-declarator. */
5747 new_declarator = cp_parser_new_declarator_opt (parser);
5749 /* Determine the number of elements in the last array dimension, if
5752 /* Skip down to the last array dimension. */
5753 declarator = new_declarator;
5754 outer_declarator = NULL;
5755 while (declarator && (declarator->kind == cdk_pointer
5756 || declarator->kind == cdk_ptrmem))
5758 outer_declarator = declarator;
5759 declarator = declarator->declarator;
5762 && declarator->kind == cdk_array
5763 && declarator->declarator
5764 && declarator->declarator->kind == cdk_array)
5766 outer_declarator = declarator;
5767 declarator = declarator->declarator;
5770 if (declarator && declarator->kind == cdk_array)
5772 *nelts = declarator->u.array.bounds;
5773 if (*nelts == error_mark_node)
5774 *nelts = integer_one_node;
5776 if (outer_declarator)
5777 outer_declarator->declarator = declarator->declarator;
5779 new_declarator = NULL;
5782 type = groktypename (&type_specifier_seq, new_declarator, false);
5786 /* Parse an (optional) new-declarator.
5789 ptr-operator new-declarator [opt]
5790 direct-new-declarator
5792 Returns the declarator. */
5794 static cp_declarator *
5795 cp_parser_new_declarator_opt (cp_parser* parser)
5797 enum tree_code code;
5799 cp_cv_quals cv_quals;
5801 /* We don't know if there's a ptr-operator next, or not. */
5802 cp_parser_parse_tentatively (parser);
5803 /* Look for a ptr-operator. */
5804 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5805 /* If that worked, look for more new-declarators. */
5806 if (cp_parser_parse_definitely (parser))
5808 cp_declarator *declarator;
5810 /* Parse another optional declarator. */
5811 declarator = cp_parser_new_declarator_opt (parser);
5813 return cp_parser_make_indirect_declarator
5814 (code, type, cv_quals, declarator);
5817 /* If the next token is a `[', there is a direct-new-declarator. */
5818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5819 return cp_parser_direct_new_declarator (parser);
5824 /* Parse a direct-new-declarator.
5826 direct-new-declarator:
5828 direct-new-declarator [constant-expression]
5832 static cp_declarator *
5833 cp_parser_direct_new_declarator (cp_parser* parser)
5835 cp_declarator *declarator = NULL;
5841 /* Look for the opening `['. */
5842 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5843 /* The first expression is not required to be constant. */
5846 cp_token *token = cp_lexer_peek_token (parser->lexer);
5847 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5848 /* The standard requires that the expression have integral
5849 type. DR 74 adds enumeration types. We believe that the
5850 real intent is that these expressions be handled like the
5851 expression in a `switch' condition, which also allows
5852 classes with a single conversion to integral or
5853 enumeration type. */
5854 if (!processing_template_decl)
5857 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5862 error ("%Hexpression in new-declarator must have integral "
5863 "or enumeration type", &token->location);
5864 expression = error_mark_node;
5868 /* But all the other expressions must be. */
5871 = cp_parser_constant_expression (parser,
5872 /*allow_non_constant=*/false,
5874 /* Look for the closing `]'. */
5875 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5877 /* Add this bound to the declarator. */
5878 declarator = make_array_declarator (declarator, expression);
5880 /* If the next token is not a `[', then there are no more
5882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5889 /* Parse a new-initializer.
5892 ( expression-list [opt] )
5895 Returns a representation of the expression-list. If there is no
5896 expression-list, VOID_ZERO_NODE is returned. */
5899 cp_parser_new_initializer (cp_parser* parser)
5901 tree expression_list;
5903 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5905 bool expr_non_constant_p;
5906 maybe_warn_cpp0x ("extended initializer lists");
5907 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5908 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5909 expression_list = build_tree_list (NULL_TREE, expression_list);
5912 expression_list = (cp_parser_parenthesized_expression_list
5913 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5914 /*non_constant_p=*/NULL));
5915 if (!expression_list)
5916 expression_list = void_zero_node;
5918 return expression_list;
5921 /* Parse a delete-expression.
5924 :: [opt] delete cast-expression
5925 :: [opt] delete [ ] cast-expression
5927 Returns a representation of the expression. */
5930 cp_parser_delete_expression (cp_parser* parser)
5932 bool global_scope_p;
5936 /* Look for the optional `::' operator. */
5938 = (cp_parser_global_scope_opt (parser,
5939 /*current_scope_valid_p=*/false)
5941 /* Look for the `delete' keyword. */
5942 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5943 /* See if the array syntax is in use. */
5944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5946 /* Consume the `[' token. */
5947 cp_lexer_consume_token (parser->lexer);
5948 /* Look for the `]' token. */
5949 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5950 /* Remember that this is the `[]' construct. */
5956 /* Parse the cast-expression. */
5957 expression = cp_parser_simple_cast_expression (parser);
5959 /* A delete-expression may not appear in an integral constant
5961 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5962 return error_mark_node;
5964 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5967 /* Returns true if TOKEN may start a cast-expression and false
5971 cp_parser_token_starts_cast_expression (cp_token *token)
5973 switch (token->type)
5979 case CPP_CLOSE_SQUARE:
5980 case CPP_CLOSE_PAREN:
5981 case CPP_CLOSE_BRACE:
5985 case CPP_DEREF_STAR:
5993 case CPP_GREATER_EQ:
6013 /* '[' may start a primary-expression in obj-c++. */
6014 case CPP_OPEN_SQUARE:
6015 return c_dialect_objc ();
6022 /* Parse a cast-expression.
6026 ( type-id ) cast-expression
6028 ADDRESS_P is true iff the unary-expression is appearing as the
6029 operand of the `&' operator. CAST_P is true if this expression is
6030 the target of a cast.
6032 Returns a representation of the expression. */
6035 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6038 /* If it's a `(', then we might be looking at a cast. */
6039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6041 tree type = NULL_TREE;
6042 tree expr = NULL_TREE;
6043 bool compound_literal_p;
6044 const char *saved_message;
6046 /* There's no way to know yet whether or not this is a cast.
6047 For example, `(int (3))' is a unary-expression, while `(int)
6048 3' is a cast. So, we resort to parsing tentatively. */
6049 cp_parser_parse_tentatively (parser);
6050 /* Types may not be defined in a cast. */
6051 saved_message = parser->type_definition_forbidden_message;
6052 parser->type_definition_forbidden_message
6053 = "types may not be defined in casts";
6054 /* Consume the `('. */
6055 cp_lexer_consume_token (parser->lexer);
6056 /* A very tricky bit is that `(struct S) { 3 }' is a
6057 compound-literal (which we permit in C++ as an extension).
6058 But, that construct is not a cast-expression -- it is a
6059 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6060 is legal; if the compound-literal were a cast-expression,
6061 you'd need an extra set of parentheses.) But, if we parse
6062 the type-id, and it happens to be a class-specifier, then we
6063 will commit to the parse at that point, because we cannot
6064 undo the action that is done when creating a new class. So,
6065 then we cannot back up and do a postfix-expression.
6067 Therefore, we scan ahead to the closing `)', and check to see
6068 if the token after the `)' is a `{'. If so, we are not
6069 looking at a cast-expression.
6071 Save tokens so that we can put them back. */
6072 cp_lexer_save_tokens (parser->lexer);
6073 /* Skip tokens until the next token is a closing parenthesis.
6074 If we find the closing `)', and the next token is a `{', then
6075 we are looking at a compound-literal. */
6077 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6078 /*consume_paren=*/true)
6079 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6080 /* Roll back the tokens we skipped. */
6081 cp_lexer_rollback_tokens (parser->lexer);
6082 /* If we were looking at a compound-literal, simulate an error
6083 so that the call to cp_parser_parse_definitely below will
6085 if (compound_literal_p)
6086 cp_parser_simulate_error (parser);
6089 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6090 parser->in_type_id_in_expr_p = true;
6091 /* Look for the type-id. */
6092 type = cp_parser_type_id (parser);
6093 /* Look for the closing `)'. */
6094 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6095 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6098 /* Restore the saved message. */
6099 parser->type_definition_forbidden_message = saved_message;
6101 /* At this point this can only be either a cast or a
6102 parenthesized ctor such as `(T ())' that looks like a cast to
6103 function returning T. */
6104 if (!cp_parser_error_occurred (parser)
6105 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6108 cp_parser_parse_definitely (parser);
6109 expr = cp_parser_cast_expression (parser,
6110 /*address_p=*/false,
6111 /*cast_p=*/true, pidk);
6113 /* Warn about old-style casts, if so requested. */
6114 if (warn_old_style_cast
6115 && !in_system_header
6116 && !VOID_TYPE_P (type)
6117 && current_lang_name != lang_name_c)
6118 warning (OPT_Wold_style_cast, "use of old-style cast");
6120 /* Only type conversions to integral or enumeration types
6121 can be used in constant-expressions. */
6122 if (!cast_valid_in_integral_constant_expression_p (type)
6123 && (cp_parser_non_integral_constant_expression
6125 "a cast to a type other than an integral or "
6126 "enumeration type")))
6127 return error_mark_node;
6129 /* Perform the cast. */
6130 expr = build_c_cast (type, expr);
6134 cp_parser_abort_tentative_parse (parser);
6137 /* If we get here, then it's not a cast, so it must be a
6138 unary-expression. */
6139 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6142 /* Parse a binary expression of the general form:
6146 pm-expression .* cast-expression
6147 pm-expression ->* cast-expression
6149 multiplicative-expression:
6151 multiplicative-expression * pm-expression
6152 multiplicative-expression / pm-expression
6153 multiplicative-expression % pm-expression
6155 additive-expression:
6156 multiplicative-expression
6157 additive-expression + multiplicative-expression
6158 additive-expression - multiplicative-expression
6162 shift-expression << additive-expression
6163 shift-expression >> additive-expression
6165 relational-expression:
6167 relational-expression < shift-expression
6168 relational-expression > shift-expression
6169 relational-expression <= shift-expression
6170 relational-expression >= shift-expression
6174 relational-expression:
6175 relational-expression <? shift-expression
6176 relational-expression >? shift-expression
6178 equality-expression:
6179 relational-expression
6180 equality-expression == relational-expression
6181 equality-expression != relational-expression
6185 and-expression & equality-expression
6187 exclusive-or-expression:
6189 exclusive-or-expression ^ and-expression
6191 inclusive-or-expression:
6192 exclusive-or-expression
6193 inclusive-or-expression | exclusive-or-expression
6195 logical-and-expression:
6196 inclusive-or-expression
6197 logical-and-expression && inclusive-or-expression
6199 logical-or-expression:
6200 logical-and-expression
6201 logical-or-expression || logical-and-expression
6203 All these are implemented with a single function like:
6206 simple-cast-expression
6207 binary-expression <token> binary-expression
6209 CAST_P is true if this expression is the target of a cast.
6211 The binops_by_token map is used to get the tree codes for each <token> type.
6212 binary-expressions are associated according to a precedence table. */
6214 #define TOKEN_PRECEDENCE(token) \
6215 (((token->type == CPP_GREATER \
6216 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6217 && !parser->greater_than_is_operator_p) \
6218 ? PREC_NOT_OPERATOR \
6219 : binops_by_token[token->type].prec)
6222 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6223 bool no_toplevel_fold_p,
6224 enum cp_parser_prec prec,
6227 cp_parser_expression_stack stack;
6228 cp_parser_expression_stack_entry *sp = &stack[0];
6231 enum tree_code tree_type, lhs_type, rhs_type;
6232 enum cp_parser_prec new_prec, lookahead_prec;
6235 /* Parse the first expression. */
6236 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6237 lhs_type = ERROR_MARK;
6241 /* Get an operator token. */
6242 token = cp_lexer_peek_token (parser->lexer);
6244 if (warn_cxx0x_compat
6245 && token->type == CPP_RSHIFT
6246 && !parser->greater_than_is_operator_p)
6248 warning (OPT_Wc__0x_compat,
6249 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6251 warning (OPT_Wc__0x_compat,
6252 "suggest parentheses around %<>>%> expression");
6255 new_prec = TOKEN_PRECEDENCE (token);
6257 /* Popping an entry off the stack means we completed a subexpression:
6258 - either we found a token which is not an operator (`>' where it is not
6259 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6260 will happen repeatedly;
6261 - or, we found an operator which has lower priority. This is the case
6262 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6264 if (new_prec <= prec)
6273 tree_type = binops_by_token[token->type].tree_type;
6275 /* We used the operator token. */
6276 cp_lexer_consume_token (parser->lexer);
6278 /* Extract another operand. It may be the RHS of this expression
6279 or the LHS of a new, higher priority expression. */
6280 rhs = cp_parser_simple_cast_expression (parser);
6281 rhs_type = ERROR_MARK;
6283 /* Get another operator token. Look up its precedence to avoid
6284 building a useless (immediately popped) stack entry for common
6285 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6286 token = cp_lexer_peek_token (parser->lexer);
6287 lookahead_prec = TOKEN_PRECEDENCE (token);
6288 if (lookahead_prec > new_prec)
6290 /* ... and prepare to parse the RHS of the new, higher priority
6291 expression. Since precedence levels on the stack are
6292 monotonically increasing, we do not have to care about
6295 sp->tree_type = tree_type;
6297 sp->lhs_type = lhs_type;
6300 lhs_type = rhs_type;
6302 new_prec = lookahead_prec;
6306 lookahead_prec = new_prec;
6307 /* If the stack is not empty, we have parsed into LHS the right side
6308 (`4' in the example above) of an expression we had suspended.
6309 We can use the information on the stack to recover the LHS (`3')
6310 from the stack together with the tree code (`MULT_EXPR'), and
6311 the precedence of the higher level subexpression
6312 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6313 which will be used to actually build the additive expression. */
6316 tree_type = sp->tree_type;
6318 rhs_type = lhs_type;
6320 lhs_type = sp->lhs_type;
6323 overloaded_p = false;
6324 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6325 ERROR_MARK for everything that is not a binary expression.
6326 This makes warn_about_parentheses miss some warnings that
6327 involve unary operators. For unary expressions we should
6328 pass the correct tree_code unless the unary expression was
6329 surrounded by parentheses.
6331 if (no_toplevel_fold_p
6332 && lookahead_prec <= prec
6334 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6335 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6337 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6338 &overloaded_p, tf_warning_or_error);
6339 lhs_type = tree_type;
6341 /* If the binary operator required the use of an overloaded operator,
6342 then this expression cannot be an integral constant-expression.
6343 An overloaded operator can be used even if both operands are
6344 otherwise permissible in an integral constant-expression if at
6345 least one of the operands is of enumeration type. */
6348 && (cp_parser_non_integral_constant_expression
6349 (parser, "calls to overloaded operators")))
6350 return error_mark_node;
6357 /* Parse the `? expression : assignment-expression' part of a
6358 conditional-expression. The LOGICAL_OR_EXPR is the
6359 logical-or-expression that started the conditional-expression.
6360 Returns a representation of the entire conditional-expression.
6362 This routine is used by cp_parser_assignment_expression.
6364 ? expression : assignment-expression
6368 ? : assignment-expression */
6371 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6374 tree assignment_expr;
6376 /* Consume the `?' token. */
6377 cp_lexer_consume_token (parser->lexer);
6378 if (cp_parser_allow_gnu_extensions_p (parser)
6379 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6380 /* Implicit true clause. */
6383 /* Parse the expression. */
6384 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6386 /* The next token should be a `:'. */
6387 cp_parser_require (parser, CPP_COLON, "%<:%>");
6388 /* Parse the assignment-expression. */
6389 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6391 /* Build the conditional-expression. */
6392 return build_x_conditional_expr (logical_or_expr,
6395 tf_warning_or_error);
6398 /* Parse an assignment-expression.
6400 assignment-expression:
6401 conditional-expression
6402 logical-or-expression assignment-operator assignment_expression
6405 CAST_P is true if this expression is the target of a cast.
6407 Returns a representation for the expression. */
6410 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6415 /* If the next token is the `throw' keyword, then we're looking at
6416 a throw-expression. */
6417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6418 expr = cp_parser_throw_expression (parser);
6419 /* Otherwise, it must be that we are looking at a
6420 logical-or-expression. */
6423 /* Parse the binary expressions (logical-or-expression). */
6424 expr = cp_parser_binary_expression (parser, cast_p, false,
6425 PREC_NOT_OPERATOR, pidk);
6426 /* If the next token is a `?' then we're actually looking at a
6427 conditional-expression. */
6428 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6429 return cp_parser_question_colon_clause (parser, expr);
6432 enum tree_code assignment_operator;
6434 /* If it's an assignment-operator, we're using the second
6437 = cp_parser_assignment_operator_opt (parser);
6438 if (assignment_operator != ERROR_MARK)
6440 bool non_constant_p;
6442 /* Parse the right-hand side of the assignment. */
6443 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6445 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6446 maybe_warn_cpp0x ("extended initializer lists");
6448 /* An assignment may not appear in a
6449 constant-expression. */
6450 if (cp_parser_non_integral_constant_expression (parser,
6452 return error_mark_node;
6453 /* Build the assignment expression. */
6454 expr = build_x_modify_expr (expr,
6455 assignment_operator,
6457 tf_warning_or_error);
6465 /* Parse an (optional) assignment-operator.
6467 assignment-operator: one of
6468 = *= /= %= += -= >>= <<= &= ^= |=
6472 assignment-operator: one of
6475 If the next token is an assignment operator, the corresponding tree
6476 code is returned, and the token is consumed. For example, for
6477 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6478 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6479 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6480 operator, ERROR_MARK is returned. */
6482 static enum tree_code
6483 cp_parser_assignment_operator_opt (cp_parser* parser)
6488 /* Peek at the next token. */
6489 token = cp_lexer_peek_token (parser->lexer);
6491 switch (token->type)
6502 op = TRUNC_DIV_EXPR;
6506 op = TRUNC_MOD_EXPR;
6538 /* Nothing else is an assignment operator. */
6542 /* If it was an assignment operator, consume it. */
6543 if (op != ERROR_MARK)
6544 cp_lexer_consume_token (parser->lexer);
6549 /* Parse an expression.
6552 assignment-expression
6553 expression , assignment-expression
6555 CAST_P is true if this expression is the target of a cast.
6557 Returns a representation of the expression. */
6560 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6562 tree expression = NULL_TREE;
6566 tree assignment_expression;
6568 /* Parse the next assignment-expression. */
6569 assignment_expression
6570 = cp_parser_assignment_expression (parser, cast_p, pidk);
6571 /* If this is the first assignment-expression, we can just
6574 expression = assignment_expression;
6576 expression = build_x_compound_expr (expression,
6577 assignment_expression,
6578 tf_warning_or_error);
6579 /* If the next token is not a comma, then we are done with the
6581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6583 /* Consume the `,'. */
6584 cp_lexer_consume_token (parser->lexer);
6585 /* A comma operator cannot appear in a constant-expression. */
6586 if (cp_parser_non_integral_constant_expression (parser,
6587 "a comma operator"))
6588 expression = error_mark_node;
6594 /* Parse a constant-expression.
6596 constant-expression:
6597 conditional-expression
6599 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6600 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6601 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6602 is false, NON_CONSTANT_P should be NULL. */
6605 cp_parser_constant_expression (cp_parser* parser,
6606 bool allow_non_constant_p,
6607 bool *non_constant_p)
6609 bool saved_integral_constant_expression_p;
6610 bool saved_allow_non_integral_constant_expression_p;
6611 bool saved_non_integral_constant_expression_p;
6614 /* It might seem that we could simply parse the
6615 conditional-expression, and then check to see if it were
6616 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6617 one that the compiler can figure out is constant, possibly after
6618 doing some simplifications or optimizations. The standard has a
6619 precise definition of constant-expression, and we must honor
6620 that, even though it is somewhat more restrictive.
6626 is not a legal declaration, because `(2, 3)' is not a
6627 constant-expression. The `,' operator is forbidden in a
6628 constant-expression. However, GCC's constant-folding machinery
6629 will fold this operation to an INTEGER_CST for `3'. */
6631 /* Save the old settings. */
6632 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6633 saved_allow_non_integral_constant_expression_p
6634 = parser->allow_non_integral_constant_expression_p;
6635 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6636 /* We are now parsing a constant-expression. */
6637 parser->integral_constant_expression_p = true;
6638 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6639 parser->non_integral_constant_expression_p = false;
6640 /* Although the grammar says "conditional-expression", we parse an
6641 "assignment-expression", which also permits "throw-expression"
6642 and the use of assignment operators. In the case that
6643 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6644 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6645 actually essential that we look for an assignment-expression.
6646 For example, cp_parser_initializer_clauses uses this function to
6647 determine whether a particular assignment-expression is in fact
6649 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6650 /* Restore the old settings. */
6651 parser->integral_constant_expression_p
6652 = saved_integral_constant_expression_p;
6653 parser->allow_non_integral_constant_expression_p
6654 = saved_allow_non_integral_constant_expression_p;
6655 if (allow_non_constant_p)
6656 *non_constant_p = parser->non_integral_constant_expression_p;
6657 else if (parser->non_integral_constant_expression_p)
6658 expression = error_mark_node;
6659 parser->non_integral_constant_expression_p
6660 = saved_non_integral_constant_expression_p;
6665 /* Parse __builtin_offsetof.
6667 offsetof-expression:
6668 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6670 offsetof-member-designator:
6672 | offsetof-member-designator "." id-expression
6673 | offsetof-member-designator "[" expression "]"
6674 | offsetof-member-designator "->" id-expression */
6677 cp_parser_builtin_offsetof (cp_parser *parser)
6679 int save_ice_p, save_non_ice_p;
6684 /* We're about to accept non-integral-constant things, but will
6685 definitely yield an integral constant expression. Save and
6686 restore these values around our local parsing. */
6687 save_ice_p = parser->integral_constant_expression_p;
6688 save_non_ice_p = parser->non_integral_constant_expression_p;
6690 /* Consume the "__builtin_offsetof" token. */
6691 cp_lexer_consume_token (parser->lexer);
6692 /* Consume the opening `('. */
6693 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6694 /* Parse the type-id. */
6695 type = cp_parser_type_id (parser);
6696 /* Look for the `,'. */
6697 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6698 token = cp_lexer_peek_token (parser->lexer);
6700 /* Build the (type *)null that begins the traditional offsetof macro. */
6701 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6702 tf_warning_or_error);
6704 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6705 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6706 true, &dummy, token->location);
6709 token = cp_lexer_peek_token (parser->lexer);
6710 switch (token->type)
6712 case CPP_OPEN_SQUARE:
6713 /* offsetof-member-designator "[" expression "]" */
6714 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6718 /* offsetof-member-designator "->" identifier */
6719 expr = grok_array_decl (expr, integer_zero_node);
6723 /* offsetof-member-designator "." identifier */
6724 cp_lexer_consume_token (parser->lexer);
6725 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6730 case CPP_CLOSE_PAREN:
6731 /* Consume the ")" token. */
6732 cp_lexer_consume_token (parser->lexer);
6736 /* Error. We know the following require will fail, but
6737 that gives the proper error message. */
6738 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6739 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6740 expr = error_mark_node;
6746 /* If we're processing a template, we can't finish the semantics yet.
6747 Otherwise we can fold the entire expression now. */
6748 if (processing_template_decl)
6749 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6751 expr = finish_offsetof (expr);
6754 parser->integral_constant_expression_p = save_ice_p;
6755 parser->non_integral_constant_expression_p = save_non_ice_p;
6760 /* Parse a trait expression. */
6763 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6766 tree type1, type2 = NULL_TREE;
6767 bool binary = false;
6768 cp_decl_specifier_seq decl_specs;
6772 case RID_HAS_NOTHROW_ASSIGN:
6773 kind = CPTK_HAS_NOTHROW_ASSIGN;
6775 case RID_HAS_NOTHROW_CONSTRUCTOR:
6776 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6778 case RID_HAS_NOTHROW_COPY:
6779 kind = CPTK_HAS_NOTHROW_COPY;
6781 case RID_HAS_TRIVIAL_ASSIGN:
6782 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6784 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6785 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6787 case RID_HAS_TRIVIAL_COPY:
6788 kind = CPTK_HAS_TRIVIAL_COPY;
6790 case RID_HAS_TRIVIAL_DESTRUCTOR:
6791 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6793 case RID_HAS_VIRTUAL_DESTRUCTOR:
6794 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6796 case RID_IS_ABSTRACT:
6797 kind = CPTK_IS_ABSTRACT;
6799 case RID_IS_BASE_OF:
6800 kind = CPTK_IS_BASE_OF;
6804 kind = CPTK_IS_CLASS;
6806 case RID_IS_CONVERTIBLE_TO:
6807 kind = CPTK_IS_CONVERTIBLE_TO;
6811 kind = CPTK_IS_EMPTY;
6814 kind = CPTK_IS_ENUM;
6819 case RID_IS_POLYMORPHIC:
6820 kind = CPTK_IS_POLYMORPHIC;
6823 kind = CPTK_IS_UNION;
6829 /* Consume the token. */
6830 cp_lexer_consume_token (parser->lexer);
6832 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6834 type1 = cp_parser_type_id (parser);
6836 if (type1 == error_mark_node)
6837 return error_mark_node;
6839 /* Build a trivial decl-specifier-seq. */
6840 clear_decl_specs (&decl_specs);
6841 decl_specs.type = type1;
6843 /* Call grokdeclarator to figure out what type this is. */
6844 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6845 /*initialized=*/0, /*attrlist=*/NULL);
6849 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6851 type2 = cp_parser_type_id (parser);
6853 if (type2 == error_mark_node)
6854 return error_mark_node;
6856 /* Build a trivial decl-specifier-seq. */
6857 clear_decl_specs (&decl_specs);
6858 decl_specs.type = type2;
6860 /* Call grokdeclarator to figure out what type this is. */
6861 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6862 /*initialized=*/0, /*attrlist=*/NULL);
6865 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6867 /* Complete the trait expression, which may mean either processing
6868 the trait expr now or saving it for template instantiation. */
6869 return finish_trait_expr (kind, type1, type2);
6872 /* Statements [gram.stmt.stmt] */
6874 /* Parse a statement.
6878 expression-statement
6883 declaration-statement
6886 IN_COMPOUND is true when the statement is nested inside a
6887 cp_parser_compound_statement; this matters for certain pragmas.
6889 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6890 is a (possibly labeled) if statement which is not enclosed in braces
6891 and has an else clause. This is used to implement -Wparentheses. */
6894 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6895 bool in_compound, bool *if_p)
6899 location_t statement_location;
6904 /* There is no statement yet. */
6905 statement = NULL_TREE;
6906 /* Peek at the next token. */
6907 token = cp_lexer_peek_token (parser->lexer);
6908 /* Remember the location of the first token in the statement. */
6909 statement_location = token->location;
6910 /* If this is a keyword, then that will often determine what kind of
6911 statement we have. */
6912 if (token->type == CPP_KEYWORD)
6914 enum rid keyword = token->keyword;
6920 /* Looks like a labeled-statement with a case label.
6921 Parse the label, and then use tail recursion to parse
6923 cp_parser_label_for_labeled_statement (parser);
6928 statement = cp_parser_selection_statement (parser, if_p);
6934 statement = cp_parser_iteration_statement (parser);
6941 statement = cp_parser_jump_statement (parser);
6944 /* Objective-C++ exception-handling constructs. */
6947 case RID_AT_FINALLY:
6948 case RID_AT_SYNCHRONIZED:
6950 statement = cp_parser_objc_statement (parser);
6954 statement = cp_parser_try_block (parser);
6958 /* This must be a namespace alias definition. */
6959 cp_parser_declaration_statement (parser);
6963 /* It might be a keyword like `int' that can start a
6964 declaration-statement. */
6968 else if (token->type == CPP_NAME)
6970 /* If the next token is a `:', then we are looking at a
6971 labeled-statement. */
6972 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6973 if (token->type == CPP_COLON)
6975 /* Looks like a labeled-statement with an ordinary label.
6976 Parse the label, and then use tail recursion to parse
6978 cp_parser_label_for_labeled_statement (parser);
6982 /* Anything that starts with a `{' must be a compound-statement. */
6983 else if (token->type == CPP_OPEN_BRACE)
6984 statement = cp_parser_compound_statement (parser, NULL, false);
6985 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6986 a statement all its own. */
6987 else if (token->type == CPP_PRAGMA)
6989 /* Only certain OpenMP pragmas are attached to statements, and thus
6990 are considered statements themselves. All others are not. In
6991 the context of a compound, accept the pragma as a "statement" and
6992 return so that we can check for a close brace. Otherwise we
6993 require a real statement and must go back and read one. */
6995 cp_parser_pragma (parser, pragma_compound);
6996 else if (!cp_parser_pragma (parser, pragma_stmt))
7000 else if (token->type == CPP_EOF)
7002 cp_parser_error (parser, "expected statement");
7006 /* Everything else must be a declaration-statement or an
7007 expression-statement. Try for the declaration-statement
7008 first, unless we are looking at a `;', in which case we know that
7009 we have an expression-statement. */
7012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7014 cp_parser_parse_tentatively (parser);
7015 /* Try to parse the declaration-statement. */
7016 cp_parser_declaration_statement (parser);
7017 /* If that worked, we're done. */
7018 if (cp_parser_parse_definitely (parser))
7021 /* Look for an expression-statement instead. */
7022 statement = cp_parser_expression_statement (parser, in_statement_expr);
7025 /* Set the line number for the statement. */
7026 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7027 SET_EXPR_LOCATION (statement, statement_location);
7030 /* Parse the label for a labeled-statement, i.e.
7033 case constant-expression :
7037 case constant-expression ... constant-expression : statement
7039 When a label is parsed without errors, the label is added to the
7040 parse tree by the finish_* functions, so this function doesn't
7041 have to return the label. */
7044 cp_parser_label_for_labeled_statement (cp_parser* parser)
7048 /* The next token should be an identifier. */
7049 token = cp_lexer_peek_token (parser->lexer);
7050 if (token->type != CPP_NAME
7051 && token->type != CPP_KEYWORD)
7053 cp_parser_error (parser, "expected labeled-statement");
7057 switch (token->keyword)
7064 /* Consume the `case' token. */
7065 cp_lexer_consume_token (parser->lexer);
7066 /* Parse the constant-expression. */
7067 expr = cp_parser_constant_expression (parser,
7068 /*allow_non_constant_p=*/false,
7071 ellipsis = cp_lexer_peek_token (parser->lexer);
7072 if (ellipsis->type == CPP_ELLIPSIS)
7074 /* Consume the `...' token. */
7075 cp_lexer_consume_token (parser->lexer);
7077 cp_parser_constant_expression (parser,
7078 /*allow_non_constant_p=*/false,
7080 /* We don't need to emit warnings here, as the common code
7081 will do this for us. */
7084 expr_hi = NULL_TREE;
7086 if (parser->in_switch_statement_p)
7087 finish_case_label (expr, expr_hi);
7089 error ("%Hcase label %qE not within a switch statement",
7090 &token->location, expr);
7095 /* Consume the `default' token. */
7096 cp_lexer_consume_token (parser->lexer);
7098 if (parser->in_switch_statement_p)
7099 finish_case_label (NULL_TREE, NULL_TREE);
7101 error ("%Hcase label not within a switch statement", &token->location);
7105 /* Anything else must be an ordinary label. */
7106 finish_label_stmt (cp_parser_identifier (parser));
7110 /* Require the `:' token. */
7111 cp_parser_require (parser, CPP_COLON, "%<:%>");
7114 /* Parse an expression-statement.
7116 expression-statement:
7119 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7120 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7121 indicates whether this expression-statement is part of an
7122 expression statement. */
7125 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7127 tree statement = NULL_TREE;
7129 /* If the next token is a ';', then there is no expression
7131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7132 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7134 /* Consume the final `;'. */
7135 cp_parser_consume_semicolon_at_end_of_statement (parser);
7137 if (in_statement_expr
7138 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7139 /* This is the final expression statement of a statement
7141 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7143 statement = finish_expr_stmt (statement);
7150 /* Parse a compound-statement.
7153 { statement-seq [opt] }
7158 { label-declaration-seq [opt] statement-seq [opt] }
7160 label-declaration-seq:
7162 label-declaration-seq label-declaration
7164 Returns a tree representing the statement. */
7167 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7172 /* Consume the `{'. */
7173 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7174 return error_mark_node;
7175 /* Begin the compound-statement. */
7176 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7177 /* If the next keyword is `__label__' we have a label declaration. */
7178 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7179 cp_parser_label_declaration (parser);
7180 /* Parse an (optional) statement-seq. */
7181 cp_parser_statement_seq_opt (parser, in_statement_expr);
7182 /* Finish the compound-statement. */
7183 finish_compound_stmt (compound_stmt);
7184 /* Consume the `}'. */
7185 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7187 return compound_stmt;
7190 /* Parse an (optional) statement-seq.
7194 statement-seq [opt] statement */
7197 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7199 /* Scan statements until there aren't any more. */
7202 cp_token *token = cp_lexer_peek_token (parser->lexer);
7204 /* If we're looking at a `}', then we've run out of statements. */
7205 if (token->type == CPP_CLOSE_BRACE
7206 || token->type == CPP_EOF
7207 || token->type == CPP_PRAGMA_EOL)
7210 /* If we are in a compound statement and find 'else' then
7211 something went wrong. */
7212 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7214 if (parser->in_statement & IN_IF_STMT)
7218 token = cp_lexer_consume_token (parser->lexer);
7219 error ("%H%<else%> without a previous %<if%>", &token->location);
7223 /* Parse the statement. */
7224 cp_parser_statement (parser, in_statement_expr, true, NULL);
7228 /* Parse a selection-statement.
7230 selection-statement:
7231 if ( condition ) statement
7232 if ( condition ) statement else statement
7233 switch ( condition ) statement
7235 Returns the new IF_STMT or SWITCH_STMT.
7237 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7238 is a (possibly labeled) if statement which is not enclosed in
7239 braces and has an else clause. This is used to implement
7243 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7251 /* Peek at the next token. */
7252 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7254 /* See what kind of keyword it is. */
7255 keyword = token->keyword;
7264 /* Look for the `('. */
7265 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7267 cp_parser_skip_to_end_of_statement (parser);
7268 return error_mark_node;
7271 /* Begin the selection-statement. */
7272 if (keyword == RID_IF)
7273 statement = begin_if_stmt ();
7275 statement = begin_switch_stmt ();
7277 /* Parse the condition. */
7278 condition = cp_parser_condition (parser);
7279 /* Look for the `)'. */
7280 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7281 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7282 /*consume_paren=*/true);
7284 if (keyword == RID_IF)
7287 unsigned char in_statement;
7289 /* Add the condition. */
7290 finish_if_stmt_cond (condition, statement);
7292 /* Parse the then-clause. */
7293 in_statement = parser->in_statement;
7294 parser->in_statement |= IN_IF_STMT;
7295 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7298 add_stmt (build_empty_stmt ());
7299 cp_lexer_consume_token (parser->lexer);
7300 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7301 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7302 "empty body in an %<if%> statement");
7306 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7307 parser->in_statement = in_statement;
7309 finish_then_clause (statement);
7311 /* If the next token is `else', parse the else-clause. */
7312 if (cp_lexer_next_token_is_keyword (parser->lexer,
7315 /* Consume the `else' keyword. */
7316 cp_lexer_consume_token (parser->lexer);
7317 begin_else_clause (statement);
7318 /* Parse the else-clause. */
7319 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7321 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7322 OPT_Wempty_body, "suggest braces around "
7323 "empty body in an %<else%> statement");
7324 add_stmt (build_empty_stmt ());
7325 cp_lexer_consume_token (parser->lexer);
7328 cp_parser_implicitly_scoped_statement (parser, NULL);
7330 finish_else_clause (statement);
7332 /* If we are currently parsing a then-clause, then
7333 IF_P will not be NULL. We set it to true to
7334 indicate that this if statement has an else clause.
7335 This may trigger the Wparentheses warning below
7336 when we get back up to the parent if statement. */
7342 /* This if statement does not have an else clause. If
7343 NESTED_IF is true, then the then-clause is an if
7344 statement which does have an else clause. We warn
7345 about the potential ambiguity. */
7347 warning (OPT_Wparentheses,
7348 ("%Hsuggest explicit braces "
7349 "to avoid ambiguous %<else%>"),
7350 EXPR_LOCUS (statement));
7353 /* Now we're all done with the if-statement. */
7354 finish_if_stmt (statement);
7358 bool in_switch_statement_p;
7359 unsigned char in_statement;
7361 /* Add the condition. */
7362 finish_switch_cond (condition, statement);
7364 /* Parse the body of the switch-statement. */
7365 in_switch_statement_p = parser->in_switch_statement_p;
7366 in_statement = parser->in_statement;
7367 parser->in_switch_statement_p = true;
7368 parser->in_statement |= IN_SWITCH_STMT;
7369 cp_parser_implicitly_scoped_statement (parser, NULL);
7370 parser->in_switch_statement_p = in_switch_statement_p;
7371 parser->in_statement = in_statement;
7373 /* Now we're all done with the switch-statement. */
7374 finish_switch_stmt (statement);
7382 cp_parser_error (parser, "expected selection-statement");
7383 return error_mark_node;
7387 /* Parse a condition.
7391 type-specifier-seq declarator = initializer-clause
7392 type-specifier-seq declarator braced-init-list
7397 type-specifier-seq declarator asm-specification [opt]
7398 attributes [opt] = assignment-expression
7400 Returns the expression that should be tested. */
7403 cp_parser_condition (cp_parser* parser)
7405 cp_decl_specifier_seq type_specifiers;
7406 const char *saved_message;
7408 /* Try the declaration first. */
7409 cp_parser_parse_tentatively (parser);
7410 /* New types are not allowed in the type-specifier-seq for a
7412 saved_message = parser->type_definition_forbidden_message;
7413 parser->type_definition_forbidden_message
7414 = "types may not be defined in conditions";
7415 /* Parse the type-specifier-seq. */
7416 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7418 /* Restore the saved message. */
7419 parser->type_definition_forbidden_message = saved_message;
7420 /* If all is well, we might be looking at a declaration. */
7421 if (!cp_parser_error_occurred (parser))
7424 tree asm_specification;
7426 cp_declarator *declarator;
7427 tree initializer = NULL_TREE;
7429 /* Parse the declarator. */
7430 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7431 /*ctor_dtor_or_conv_p=*/NULL,
7432 /*parenthesized_p=*/NULL,
7433 /*member_p=*/false);
7434 /* Parse the attributes. */
7435 attributes = cp_parser_attributes_opt (parser);
7436 /* Parse the asm-specification. */
7437 asm_specification = cp_parser_asm_specification_opt (parser);
7438 /* If the next token is not an `=' or '{', then we might still be
7439 looking at an expression. For example:
7443 looks like a decl-specifier-seq and a declarator -- but then
7444 there is no `=', so this is an expression. */
7445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7446 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7447 cp_parser_simulate_error (parser);
7449 /* If we did see an `=' or '{', then we are looking at a declaration
7451 if (cp_parser_parse_definitely (parser))
7454 bool non_constant_p;
7455 bool flags = LOOKUP_ONLYCONVERTING;
7457 /* Create the declaration. */
7458 decl = start_decl (declarator, &type_specifiers,
7459 /*initialized_p=*/true,
7460 attributes, /*prefix_attributes=*/NULL_TREE,
7463 /* Parse the initializer. */
7464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7466 initializer = cp_parser_braced_list (parser, &non_constant_p);
7467 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7472 /* Consume the `='. */
7473 cp_parser_require (parser, CPP_EQ, "%<=%>");
7474 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7476 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7477 maybe_warn_cpp0x ("extended initializer lists");
7479 if (!non_constant_p)
7480 initializer = fold_non_dependent_expr (initializer);
7482 /* Process the initializer. */
7483 cp_finish_decl (decl,
7484 initializer, !non_constant_p,
7489 pop_scope (pushed_scope);
7491 return convert_from_reference (decl);
7494 /* If we didn't even get past the declarator successfully, we are
7495 definitely not looking at a declaration. */
7497 cp_parser_abort_tentative_parse (parser);
7499 /* Otherwise, we are looking at an expression. */
7500 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7503 /* Parse an iteration-statement.
7505 iteration-statement:
7506 while ( condition ) statement
7507 do statement while ( expression ) ;
7508 for ( for-init-statement condition [opt] ; expression [opt] )
7511 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7514 cp_parser_iteration_statement (cp_parser* parser)
7519 unsigned char in_statement;
7521 /* Peek at the next token. */
7522 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7524 return error_mark_node;
7526 /* Remember whether or not we are already within an iteration
7528 in_statement = parser->in_statement;
7530 /* See what kind of keyword it is. */
7531 keyword = token->keyword;
7538 /* Begin the while-statement. */
7539 statement = begin_while_stmt ();
7540 /* Look for the `('. */
7541 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7542 /* Parse the condition. */
7543 condition = cp_parser_condition (parser);
7544 finish_while_stmt_cond (condition, statement);
7545 /* Look for the `)'. */
7546 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7547 /* Parse the dependent statement. */
7548 parser->in_statement = IN_ITERATION_STMT;
7549 cp_parser_already_scoped_statement (parser);
7550 parser->in_statement = in_statement;
7551 /* We're done with the while-statement. */
7552 finish_while_stmt (statement);
7560 /* Begin the do-statement. */
7561 statement = begin_do_stmt ();
7562 /* Parse the body of the do-statement. */
7563 parser->in_statement = IN_ITERATION_STMT;
7564 cp_parser_implicitly_scoped_statement (parser, NULL);
7565 parser->in_statement = in_statement;
7566 finish_do_body (statement);
7567 /* Look for the `while' keyword. */
7568 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7569 /* Look for the `('. */
7570 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7571 /* Parse the expression. */
7572 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7573 /* We're done with the do-statement. */
7574 finish_do_stmt (expression, statement);
7575 /* Look for the `)'. */
7576 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7577 /* Look for the `;'. */
7578 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7584 tree condition = NULL_TREE;
7585 tree expression = NULL_TREE;
7587 /* Begin the for-statement. */
7588 statement = begin_for_stmt ();
7589 /* Look for the `('. */
7590 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7591 /* Parse the initialization. */
7592 cp_parser_for_init_statement (parser);
7593 finish_for_init_stmt (statement);
7595 /* If there's a condition, process it. */
7596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7597 condition = cp_parser_condition (parser);
7598 finish_for_cond (condition, statement);
7599 /* Look for the `;'. */
7600 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7602 /* If there's an expression, process it. */
7603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7604 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7605 finish_for_expr (expression, statement);
7606 /* Look for the `)'. */
7607 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7609 /* Parse the body of the for-statement. */
7610 parser->in_statement = IN_ITERATION_STMT;
7611 cp_parser_already_scoped_statement (parser);
7612 parser->in_statement = in_statement;
7614 /* We're done with the for-statement. */
7615 finish_for_stmt (statement);
7620 cp_parser_error (parser, "expected iteration-statement");
7621 statement = error_mark_node;
7628 /* Parse a for-init-statement.
7631 expression-statement
7632 simple-declaration */
7635 cp_parser_for_init_statement (cp_parser* parser)
7637 /* If the next token is a `;', then we have an empty
7638 expression-statement. Grammatically, this is also a
7639 simple-declaration, but an invalid one, because it does not
7640 declare anything. Therefore, if we did not handle this case
7641 specially, we would issue an error message about an invalid
7643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7645 /* We're going to speculatively look for a declaration, falling back
7646 to an expression, if necessary. */
7647 cp_parser_parse_tentatively (parser);
7648 /* Parse the declaration. */
7649 cp_parser_simple_declaration (parser,
7650 /*function_definition_allowed_p=*/false);
7651 /* If the tentative parse failed, then we shall need to look for an
7652 expression-statement. */
7653 if (cp_parser_parse_definitely (parser))
7657 cp_parser_expression_statement (parser, false);
7660 /* Parse a jump-statement.
7665 return expression [opt] ;
7666 return braced-init-list ;
7674 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7677 cp_parser_jump_statement (cp_parser* parser)
7679 tree statement = error_mark_node;
7682 unsigned char in_statement;
7684 /* Peek at the next token. */
7685 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7687 return error_mark_node;
7689 /* See what kind of keyword it is. */
7690 keyword = token->keyword;
7694 in_statement = parser->in_statement & ~IN_IF_STMT;
7695 switch (in_statement)
7698 error ("%Hbreak statement not within loop or switch", &token->location);
7701 gcc_assert ((in_statement & IN_SWITCH_STMT)
7702 || in_statement == IN_ITERATION_STMT);
7703 statement = finish_break_stmt ();
7706 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7709 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7712 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7716 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7719 error ("%Hcontinue statement not within a loop", &token->location);
7721 case IN_ITERATION_STMT:
7723 statement = finish_continue_stmt ();
7726 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7731 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7737 bool expr_non_constant_p;
7739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7741 maybe_warn_cpp0x ("extended initializer lists");
7742 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7744 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7745 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7747 /* If the next token is a `;', then there is no
7750 /* Build the return-statement. */
7751 statement = finish_return_stmt (expr);
7752 /* Look for the final `;'. */
7753 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7758 /* Create the goto-statement. */
7759 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7761 /* Issue a warning about this use of a GNU extension. */
7762 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7763 /* Consume the '*' token. */
7764 cp_lexer_consume_token (parser->lexer);
7765 /* Parse the dependent expression. */
7766 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7769 finish_goto_stmt (cp_parser_identifier (parser));
7770 /* Look for the final `;'. */
7771 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7775 cp_parser_error (parser, "expected jump-statement");
7782 /* Parse a declaration-statement.
7784 declaration-statement:
7785 block-declaration */
7788 cp_parser_declaration_statement (cp_parser* parser)
7792 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7793 p = obstack_alloc (&declarator_obstack, 0);
7795 /* Parse the block-declaration. */
7796 cp_parser_block_declaration (parser, /*statement_p=*/true);
7798 /* Free any declarators allocated. */
7799 obstack_free (&declarator_obstack, p);
7801 /* Finish off the statement. */
7805 /* Some dependent statements (like `if (cond) statement'), are
7806 implicitly in their own scope. In other words, if the statement is
7807 a single statement (as opposed to a compound-statement), it is
7808 none-the-less treated as if it were enclosed in braces. Any
7809 declarations appearing in the dependent statement are out of scope
7810 after control passes that point. This function parses a statement,
7811 but ensures that is in its own scope, even if it is not a
7814 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7815 is a (possibly labeled) if statement which is not enclosed in
7816 braces and has an else clause. This is used to implement
7819 Returns the new statement. */
7822 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7829 /* Mark if () ; with a special NOP_EXPR. */
7830 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7832 cp_lexer_consume_token (parser->lexer);
7833 statement = add_stmt (build_empty_stmt ());
7835 /* if a compound is opened, we simply parse the statement directly. */
7836 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7837 statement = cp_parser_compound_statement (parser, NULL, false);
7838 /* If the token is not a `{', then we must take special action. */
7841 /* Create a compound-statement. */
7842 statement = begin_compound_stmt (0);
7843 /* Parse the dependent-statement. */
7844 cp_parser_statement (parser, NULL_TREE, false, if_p);
7845 /* Finish the dummy compound-statement. */
7846 finish_compound_stmt (statement);
7849 /* Return the statement. */
7853 /* For some dependent statements (like `while (cond) statement'), we
7854 have already created a scope. Therefore, even if the dependent
7855 statement is a compound-statement, we do not want to create another
7859 cp_parser_already_scoped_statement (cp_parser* parser)
7861 /* If the token is a `{', then we must take special action. */
7862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7863 cp_parser_statement (parser, NULL_TREE, false, NULL);
7866 /* Avoid calling cp_parser_compound_statement, so that we
7867 don't create a new scope. Do everything else by hand. */
7868 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7869 /* If the next keyword is `__label__' we have a label declaration. */
7870 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7871 cp_parser_label_declaration (parser);
7872 /* Parse an (optional) statement-seq. */
7873 cp_parser_statement_seq_opt (parser, NULL_TREE);
7874 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7878 /* Declarations [gram.dcl.dcl] */
7880 /* Parse an optional declaration-sequence.
7884 declaration-seq declaration */
7887 cp_parser_declaration_seq_opt (cp_parser* parser)
7893 token = cp_lexer_peek_token (parser->lexer);
7895 if (token->type == CPP_CLOSE_BRACE
7896 || token->type == CPP_EOF
7897 || token->type == CPP_PRAGMA_EOL)
7900 if (token->type == CPP_SEMICOLON)
7902 /* A declaration consisting of a single semicolon is
7903 invalid. Allow it unless we're being pedantic. */
7904 cp_lexer_consume_token (parser->lexer);
7905 if (!in_system_header)
7906 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7910 /* If we're entering or exiting a region that's implicitly
7911 extern "C", modify the lang context appropriately. */
7912 if (!parser->implicit_extern_c && token->implicit_extern_c)
7914 push_lang_context (lang_name_c);
7915 parser->implicit_extern_c = true;
7917 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7919 pop_lang_context ();
7920 parser->implicit_extern_c = false;
7923 if (token->type == CPP_PRAGMA)
7925 /* A top-level declaration can consist solely of a #pragma.
7926 A nested declaration cannot, so this is done here and not
7927 in cp_parser_declaration. (A #pragma at block scope is
7928 handled in cp_parser_statement.) */
7929 cp_parser_pragma (parser, pragma_external);
7933 /* Parse the declaration itself. */
7934 cp_parser_declaration (parser);
7938 /* Parse a declaration.
7943 template-declaration
7944 explicit-instantiation
7945 explicit-specialization
7946 linkage-specification
7947 namespace-definition
7952 __extension__ declaration */
7955 cp_parser_declaration (cp_parser* parser)
7962 /* Check for the `__extension__' keyword. */
7963 if (cp_parser_extension_opt (parser, &saved_pedantic))
7965 /* Parse the qualified declaration. */
7966 cp_parser_declaration (parser);
7967 /* Restore the PEDANTIC flag. */
7968 pedantic = saved_pedantic;
7973 /* Try to figure out what kind of declaration is present. */
7974 token1 = *cp_lexer_peek_token (parser->lexer);
7976 if (token1.type != CPP_EOF)
7977 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7980 token2.type = CPP_EOF;
7981 token2.keyword = RID_MAX;
7984 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7985 p = obstack_alloc (&declarator_obstack, 0);
7987 /* If the next token is `extern' and the following token is a string
7988 literal, then we have a linkage specification. */
7989 if (token1.keyword == RID_EXTERN
7990 && cp_parser_is_string_literal (&token2))
7991 cp_parser_linkage_specification (parser);
7992 /* If the next token is `template', then we have either a template
7993 declaration, an explicit instantiation, or an explicit
7995 else if (token1.keyword == RID_TEMPLATE)
7997 /* `template <>' indicates a template specialization. */
7998 if (token2.type == CPP_LESS
7999 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8000 cp_parser_explicit_specialization (parser);
8001 /* `template <' indicates a template declaration. */
8002 else if (token2.type == CPP_LESS)
8003 cp_parser_template_declaration (parser, /*member_p=*/false);
8004 /* Anything else must be an explicit instantiation. */
8006 cp_parser_explicit_instantiation (parser);
8008 /* If the next token is `export', then we have a template
8010 else if (token1.keyword == RID_EXPORT)
8011 cp_parser_template_declaration (parser, /*member_p=*/false);
8012 /* If the next token is `extern', 'static' or 'inline' and the one
8013 after that is `template', we have a GNU extended explicit
8014 instantiation directive. */
8015 else if (cp_parser_allow_gnu_extensions_p (parser)
8016 && (token1.keyword == RID_EXTERN
8017 || token1.keyword == RID_STATIC
8018 || token1.keyword == RID_INLINE)
8019 && token2.keyword == RID_TEMPLATE)
8020 cp_parser_explicit_instantiation (parser);
8021 /* If the next token is `namespace', check for a named or unnamed
8022 namespace definition. */
8023 else if (token1.keyword == RID_NAMESPACE
8024 && (/* A named namespace definition. */
8025 (token2.type == CPP_NAME
8026 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8028 /* An unnamed namespace definition. */
8029 || token2.type == CPP_OPEN_BRACE
8030 || token2.keyword == RID_ATTRIBUTE))
8031 cp_parser_namespace_definition (parser);
8032 /* An inline (associated) namespace definition. */
8033 else if (token1.keyword == RID_INLINE
8034 && token2.keyword == RID_NAMESPACE)
8035 cp_parser_namespace_definition (parser);
8036 /* Objective-C++ declaration/definition. */
8037 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8038 cp_parser_objc_declaration (parser);
8039 /* We must have either a block declaration or a function
8042 /* Try to parse a block-declaration, or a function-definition. */
8043 cp_parser_block_declaration (parser, /*statement_p=*/false);
8045 /* Free any declarators allocated. */
8046 obstack_free (&declarator_obstack, p);
8049 /* Parse a block-declaration.
8054 namespace-alias-definition
8061 __extension__ block-declaration
8066 static_assert-declaration
8068 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8069 part of a declaration-statement. */
8072 cp_parser_block_declaration (cp_parser *parser,
8078 /* Check for the `__extension__' keyword. */
8079 if (cp_parser_extension_opt (parser, &saved_pedantic))
8081 /* Parse the qualified declaration. */
8082 cp_parser_block_declaration (parser, statement_p);
8083 /* Restore the PEDANTIC flag. */
8084 pedantic = saved_pedantic;
8089 /* Peek at the next token to figure out which kind of declaration is
8091 token1 = cp_lexer_peek_token (parser->lexer);
8093 /* If the next keyword is `asm', we have an asm-definition. */
8094 if (token1->keyword == RID_ASM)
8097 cp_parser_commit_to_tentative_parse (parser);
8098 cp_parser_asm_definition (parser);
8100 /* If the next keyword is `namespace', we have a
8101 namespace-alias-definition. */
8102 else if (token1->keyword == RID_NAMESPACE)
8103 cp_parser_namespace_alias_definition (parser);
8104 /* If the next keyword is `using', we have either a
8105 using-declaration or a using-directive. */
8106 else if (token1->keyword == RID_USING)
8111 cp_parser_commit_to_tentative_parse (parser);
8112 /* If the token after `using' is `namespace', then we have a
8114 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8115 if (token2->keyword == RID_NAMESPACE)
8116 cp_parser_using_directive (parser);
8117 /* Otherwise, it's a using-declaration. */
8119 cp_parser_using_declaration (parser,
8120 /*access_declaration_p=*/false);
8122 /* If the next keyword is `__label__' we have a misplaced label
8124 else if (token1->keyword == RID_LABEL)
8126 cp_lexer_consume_token (parser->lexer);
8127 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8128 cp_parser_skip_to_end_of_statement (parser);
8129 /* If the next token is now a `;', consume it. */
8130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8131 cp_lexer_consume_token (parser->lexer);
8133 /* If the next token is `static_assert' we have a static assertion. */
8134 else if (token1->keyword == RID_STATIC_ASSERT)
8135 cp_parser_static_assert (parser, /*member_p=*/false);
8136 /* Anything else must be a simple-declaration. */
8138 cp_parser_simple_declaration (parser, !statement_p);
8141 /* Parse a simple-declaration.
8144 decl-specifier-seq [opt] init-declarator-list [opt] ;
8146 init-declarator-list:
8148 init-declarator-list , init-declarator
8150 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8151 function-definition as a simple-declaration. */
8154 cp_parser_simple_declaration (cp_parser* parser,
8155 bool function_definition_allowed_p)
8157 cp_decl_specifier_seq decl_specifiers;
8158 int declares_class_or_enum;
8159 bool saw_declarator;
8161 /* Defer access checks until we know what is being declared; the
8162 checks for names appearing in the decl-specifier-seq should be
8163 done as if we were in the scope of the thing being declared. */
8164 push_deferring_access_checks (dk_deferred);
8166 /* Parse the decl-specifier-seq. We have to keep track of whether
8167 or not the decl-specifier-seq declares a named class or
8168 enumeration type, since that is the only case in which the
8169 init-declarator-list is allowed to be empty.
8173 In a simple-declaration, the optional init-declarator-list can be
8174 omitted only when declaring a class or enumeration, that is when
8175 the decl-specifier-seq contains either a class-specifier, an
8176 elaborated-type-specifier, or an enum-specifier. */
8177 cp_parser_decl_specifier_seq (parser,
8178 CP_PARSER_FLAGS_OPTIONAL,
8180 &declares_class_or_enum);
8181 /* We no longer need to defer access checks. */
8182 stop_deferring_access_checks ();
8184 /* In a block scope, a valid declaration must always have a
8185 decl-specifier-seq. By not trying to parse declarators, we can
8186 resolve the declaration/expression ambiguity more quickly. */
8187 if (!function_definition_allowed_p
8188 && !decl_specifiers.any_specifiers_p)
8190 cp_parser_error (parser, "expected declaration");
8194 /* If the next two tokens are both identifiers, the code is
8195 erroneous. The usual cause of this situation is code like:
8199 where "T" should name a type -- but does not. */
8200 if (!decl_specifiers.type
8201 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8203 /* If parsing tentatively, we should commit; we really are
8204 looking at a declaration. */
8205 cp_parser_commit_to_tentative_parse (parser);
8210 /* If we have seen at least one decl-specifier, and the next token
8211 is not a parenthesis, then we must be looking at a declaration.
8212 (After "int (" we might be looking at a functional cast.) */
8213 if (decl_specifiers.any_specifiers_p
8214 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8215 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8216 && !cp_parser_error_occurred (parser))
8217 cp_parser_commit_to_tentative_parse (parser);
8219 /* Keep going until we hit the `;' at the end of the simple
8221 saw_declarator = false;
8222 while (cp_lexer_next_token_is_not (parser->lexer,
8226 bool function_definition_p;
8231 /* If we are processing next declarator, coma is expected */
8232 token = cp_lexer_peek_token (parser->lexer);
8233 gcc_assert (token->type == CPP_COMMA);
8234 cp_lexer_consume_token (parser->lexer);
8237 saw_declarator = true;
8239 /* Parse the init-declarator. */
8240 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8242 function_definition_allowed_p,
8244 declares_class_or_enum,
8245 &function_definition_p);
8246 /* If an error occurred while parsing tentatively, exit quickly.
8247 (That usually happens when in the body of a function; each
8248 statement is treated as a declaration-statement until proven
8250 if (cp_parser_error_occurred (parser))
8252 /* Handle function definitions specially. */
8253 if (function_definition_p)
8255 /* If the next token is a `,', then we are probably
8256 processing something like:
8260 which is erroneous. */
8261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8263 cp_token *token = cp_lexer_peek_token (parser->lexer);
8264 error ("%Hmixing declarations and function-definitions is forbidden",
8267 /* Otherwise, we're done with the list of declarators. */
8270 pop_deferring_access_checks ();
8274 /* The next token should be either a `,' or a `;'. */
8275 token = cp_lexer_peek_token (parser->lexer);
8276 /* If it's a `,', there are more declarators to come. */
8277 if (token->type == CPP_COMMA)
8278 /* will be consumed next time around */;
8279 /* If it's a `;', we are done. */
8280 else if (token->type == CPP_SEMICOLON)
8282 /* Anything else is an error. */
8285 /* If we have already issued an error message we don't need
8286 to issue another one. */
8287 if (decl != error_mark_node
8288 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8289 cp_parser_error (parser, "expected %<,%> or %<;%>");
8290 /* Skip tokens until we reach the end of the statement. */
8291 cp_parser_skip_to_end_of_statement (parser);
8292 /* If the next token is now a `;', consume it. */
8293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8294 cp_lexer_consume_token (parser->lexer);
8297 /* After the first time around, a function-definition is not
8298 allowed -- even if it was OK at first. For example:
8303 function_definition_allowed_p = false;
8306 /* Issue an error message if no declarators are present, and the
8307 decl-specifier-seq does not itself declare a class or
8309 if (!saw_declarator)
8311 if (cp_parser_declares_only_class_p (parser))
8312 shadow_tag (&decl_specifiers);
8313 /* Perform any deferred access checks. */
8314 perform_deferred_access_checks ();
8317 /* Consume the `;'. */
8318 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8321 pop_deferring_access_checks ();
8324 /* Parse a decl-specifier-seq.
8327 decl-specifier-seq [opt] decl-specifier
8330 storage-class-specifier
8341 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8343 The parser flags FLAGS is used to control type-specifier parsing.
8345 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8348 1: one of the decl-specifiers is an elaborated-type-specifier
8349 (i.e., a type declaration)
8350 2: one of the decl-specifiers is an enum-specifier or a
8351 class-specifier (i.e., a type definition)
8356 cp_parser_decl_specifier_seq (cp_parser* parser,
8357 cp_parser_flags flags,
8358 cp_decl_specifier_seq *decl_specs,
8359 int* declares_class_or_enum)
8361 bool constructor_possible_p = !parser->in_declarator_p;
8362 cp_token *start_token = NULL;
8364 /* Clear DECL_SPECS. */
8365 clear_decl_specs (decl_specs);
8367 /* Assume no class or enumeration type is declared. */
8368 *declares_class_or_enum = 0;
8370 /* Keep reading specifiers until there are no more to read. */
8374 bool found_decl_spec;
8377 /* Peek at the next token. */
8378 token = cp_lexer_peek_token (parser->lexer);
8380 /* Save the first token of the decl spec list for error
8383 start_token = token;
8384 /* Handle attributes. */
8385 if (token->keyword == RID_ATTRIBUTE)
8387 /* Parse the attributes. */
8388 decl_specs->attributes
8389 = chainon (decl_specs->attributes,
8390 cp_parser_attributes_opt (parser));
8393 /* Assume we will find a decl-specifier keyword. */
8394 found_decl_spec = true;
8395 /* If the next token is an appropriate keyword, we can simply
8396 add it to the list. */
8397 switch (token->keyword)
8402 if (!at_class_scope_p ())
8404 error ("%H%<friend%> used outside of class", &token->location);
8405 cp_lexer_purge_token (parser->lexer);
8409 ++decl_specs->specs[(int) ds_friend];
8410 /* Consume the token. */
8411 cp_lexer_consume_token (parser->lexer);
8415 /* function-specifier:
8422 cp_parser_function_specifier_opt (parser, decl_specs);
8428 ++decl_specs->specs[(int) ds_typedef];
8429 /* Consume the token. */
8430 cp_lexer_consume_token (parser->lexer);
8431 /* A constructor declarator cannot appear in a typedef. */
8432 constructor_possible_p = false;
8433 /* The "typedef" keyword can only occur in a declaration; we
8434 may as well commit at this point. */
8435 cp_parser_commit_to_tentative_parse (parser);
8437 if (decl_specs->storage_class != sc_none)
8438 decl_specs->conflicting_specifiers_p = true;
8441 /* storage-class-specifier:
8451 if (cxx_dialect == cxx98)
8453 /* Consume the token. */
8454 cp_lexer_consume_token (parser->lexer);
8456 /* Complain about `auto' as a storage specifier, if
8457 we're complaining about C++0x compatibility. */
8460 "%H%<auto%> will change meaning in C++0x; please remove it",
8463 /* Set the storage class anyway. */
8464 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8468 /* C++0x auto type-specifier. */
8469 found_decl_spec = false;
8476 /* Consume the token. */
8477 cp_lexer_consume_token (parser->lexer);
8478 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8482 /* Consume the token. */
8483 cp_lexer_consume_token (parser->lexer);
8484 ++decl_specs->specs[(int) ds_thread];
8488 /* We did not yet find a decl-specifier yet. */
8489 found_decl_spec = false;
8493 /* Constructors are a special case. The `S' in `S()' is not a
8494 decl-specifier; it is the beginning of the declarator. */
8497 && constructor_possible_p
8498 && (cp_parser_constructor_declarator_p
8499 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8501 /* If we don't have a DECL_SPEC yet, then we must be looking at
8502 a type-specifier. */
8503 if (!found_decl_spec && !constructor_p)
8505 int decl_spec_declares_class_or_enum;
8506 bool is_cv_qualifier;
8510 = cp_parser_type_specifier (parser, flags,
8512 /*is_declaration=*/true,
8513 &decl_spec_declares_class_or_enum,
8515 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8517 /* If this type-specifier referenced a user-defined type
8518 (a typedef, class-name, etc.), then we can't allow any
8519 more such type-specifiers henceforth.
8523 The longest sequence of decl-specifiers that could
8524 possibly be a type name is taken as the
8525 decl-specifier-seq of a declaration. The sequence shall
8526 be self-consistent as described below.
8530 As a general rule, at most one type-specifier is allowed
8531 in the complete decl-specifier-seq of a declaration. The
8532 only exceptions are the following:
8534 -- const or volatile can be combined with any other
8537 -- signed or unsigned can be combined with char, long,
8545 void g (const int Pc);
8547 Here, Pc is *not* part of the decl-specifier seq; it's
8548 the declarator. Therefore, once we see a type-specifier
8549 (other than a cv-qualifier), we forbid any additional
8550 user-defined types. We *do* still allow things like `int
8551 int' to be considered a decl-specifier-seq, and issue the
8552 error message later. */
8553 if (type_spec && !is_cv_qualifier)
8554 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8555 /* A constructor declarator cannot follow a type-specifier. */
8558 constructor_possible_p = false;
8559 found_decl_spec = true;
8563 /* If we still do not have a DECL_SPEC, then there are no more
8565 if (!found_decl_spec)
8568 decl_specs->any_specifiers_p = true;
8569 /* After we see one decl-specifier, further decl-specifiers are
8571 flags |= CP_PARSER_FLAGS_OPTIONAL;
8574 cp_parser_check_decl_spec (decl_specs, start_token->location);
8576 /* Don't allow a friend specifier with a class definition. */
8577 if (decl_specs->specs[(int) ds_friend] != 0
8578 && (*declares_class_or_enum & 2))
8579 error ("%Hclass definition may not be declared a friend",
8580 &start_token->location);
8583 /* Parse an (optional) storage-class-specifier.
8585 storage-class-specifier:
8594 storage-class-specifier:
8597 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8600 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8602 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8605 if (cxx_dialect != cxx98)
8607 /* Fall through for C++98. */
8614 /* Consume the token. */
8615 return cp_lexer_consume_token (parser->lexer)->u.value;
8622 /* Parse an (optional) function-specifier.
8629 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8630 Updates DECL_SPECS, if it is non-NULL. */
8633 cp_parser_function_specifier_opt (cp_parser* parser,
8634 cp_decl_specifier_seq *decl_specs)
8636 cp_token *token = cp_lexer_peek_token (parser->lexer);
8637 switch (token->keyword)
8641 ++decl_specs->specs[(int) ds_inline];
8645 /* 14.5.2.3 [temp.mem]
8647 A member function template shall not be virtual. */
8648 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8649 error ("%Htemplates may not be %<virtual%>", &token->location);
8650 else if (decl_specs)
8651 ++decl_specs->specs[(int) ds_virtual];
8656 ++decl_specs->specs[(int) ds_explicit];
8663 /* Consume the token. */
8664 return cp_lexer_consume_token (parser->lexer)->u.value;
8667 /* Parse a linkage-specification.
8669 linkage-specification:
8670 extern string-literal { declaration-seq [opt] }
8671 extern string-literal declaration */
8674 cp_parser_linkage_specification (cp_parser* parser)
8678 /* Look for the `extern' keyword. */
8679 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8681 /* Look for the string-literal. */
8682 linkage = cp_parser_string_literal (parser, false, false);
8684 /* Transform the literal into an identifier. If the literal is a
8685 wide-character string, or contains embedded NULs, then we can't
8686 handle it as the user wants. */
8687 if (strlen (TREE_STRING_POINTER (linkage))
8688 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8690 cp_parser_error (parser, "invalid linkage-specification");
8691 /* Assume C++ linkage. */
8692 linkage = lang_name_cplusplus;
8695 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8697 /* We're now using the new linkage. */
8698 push_lang_context (linkage);
8700 /* If the next token is a `{', then we're using the first
8702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8704 /* Consume the `{' token. */
8705 cp_lexer_consume_token (parser->lexer);
8706 /* Parse the declarations. */
8707 cp_parser_declaration_seq_opt (parser);
8708 /* Look for the closing `}'. */
8709 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8711 /* Otherwise, there's just one declaration. */
8714 bool saved_in_unbraced_linkage_specification_p;
8716 saved_in_unbraced_linkage_specification_p
8717 = parser->in_unbraced_linkage_specification_p;
8718 parser->in_unbraced_linkage_specification_p = true;
8719 cp_parser_declaration (parser);
8720 parser->in_unbraced_linkage_specification_p
8721 = saved_in_unbraced_linkage_specification_p;
8724 /* We're done with the linkage-specification. */
8725 pop_lang_context ();
8728 /* Parse a static_assert-declaration.
8730 static_assert-declaration:
8731 static_assert ( constant-expression , string-literal ) ;
8733 If MEMBER_P, this static_assert is a class member. */
8736 cp_parser_static_assert(cp_parser *parser, bool member_p)
8741 location_t saved_loc;
8743 /* Peek at the `static_assert' token so we can keep track of exactly
8744 where the static assertion started. */
8745 token = cp_lexer_peek_token (parser->lexer);
8746 saved_loc = token->location;
8748 /* Look for the `static_assert' keyword. */
8749 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8750 "%<static_assert%>"))
8753 /* We know we are in a static assertion; commit to any tentative
8755 if (cp_parser_parsing_tentatively (parser))
8756 cp_parser_commit_to_tentative_parse (parser);
8758 /* Parse the `(' starting the static assertion condition. */
8759 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8761 /* Parse the constant-expression. */
8763 cp_parser_constant_expression (parser,
8764 /*allow_non_constant_p=*/false,
8765 /*non_constant_p=*/NULL);
8767 /* Parse the separating `,'. */
8768 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8770 /* Parse the string-literal message. */
8771 message = cp_parser_string_literal (parser,
8772 /*translate=*/false,
8775 /* A `)' completes the static assertion. */
8776 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8777 cp_parser_skip_to_closing_parenthesis (parser,
8778 /*recovering=*/true,
8780 /*consume_paren=*/true);
8782 /* A semicolon terminates the declaration. */
8783 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8785 /* Complete the static assertion, which may mean either processing
8786 the static assert now or saving it for template instantiation. */
8787 finish_static_assert (condition, message, saved_loc, member_p);
8790 /* Parse a `decltype' type. Returns the type.
8792 simple-type-specifier:
8793 decltype ( expression ) */
8796 cp_parser_decltype (cp_parser *parser)
8799 bool id_expression_or_member_access_p = false;
8800 const char *saved_message;
8801 bool saved_integral_constant_expression_p;
8802 bool saved_non_integral_constant_expression_p;
8803 cp_token *id_expr_start_token;
8805 /* Look for the `decltype' token. */
8806 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8807 return error_mark_node;
8809 /* Types cannot be defined in a `decltype' expression. Save away the
8811 saved_message = parser->type_definition_forbidden_message;
8813 /* And create the new one. */
8814 parser->type_definition_forbidden_message
8815 = "types may not be defined in %<decltype%> expressions";
8817 /* The restrictions on constant-expressions do not apply inside
8818 decltype expressions. */
8819 saved_integral_constant_expression_p
8820 = parser->integral_constant_expression_p;
8821 saved_non_integral_constant_expression_p
8822 = parser->non_integral_constant_expression_p;
8823 parser->integral_constant_expression_p = false;
8825 /* Do not actually evaluate the expression. */
8828 /* Parse the opening `('. */
8829 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8830 return error_mark_node;
8832 /* First, try parsing an id-expression. */
8833 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8834 cp_parser_parse_tentatively (parser);
8835 expr = cp_parser_id_expression (parser,
8836 /*template_keyword_p=*/false,
8837 /*check_dependency_p=*/true,
8838 /*template_p=*/NULL,
8839 /*declarator_p=*/false,
8840 /*optional_p=*/false);
8842 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8844 bool non_integral_constant_expression_p = false;
8845 tree id_expression = expr;
8847 const char *error_msg;
8849 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8850 /* Lookup the name we got back from the id-expression. */
8851 expr = cp_parser_lookup_name (parser, expr,
8853 /*is_template=*/false,
8854 /*is_namespace=*/false,
8855 /*check_dependency=*/true,
8856 /*ambiguous_decls=*/NULL,
8857 id_expr_start_token->location);
8860 && expr != error_mark_node
8861 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8862 && TREE_CODE (expr) != TYPE_DECL
8863 && (TREE_CODE (expr) != BIT_NOT_EXPR
8864 || !TYPE_P (TREE_OPERAND (expr, 0)))
8865 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8867 /* Complete lookup of the id-expression. */
8868 expr = (finish_id_expression
8869 (id_expression, expr, parser->scope, &idk,
8870 /*integral_constant_expression_p=*/false,
8871 /*allow_non_integral_constant_expression_p=*/true,
8872 &non_integral_constant_expression_p,
8873 /*template_p=*/false,
8875 /*address_p=*/false,
8876 /*template_arg_p=*/false,
8878 id_expr_start_token->location));
8880 if (expr == error_mark_node)
8881 /* We found an id-expression, but it was something that we
8882 should not have found. This is an error, not something
8883 we can recover from, so note that we found an
8884 id-expression and we'll recover as gracefully as
8886 id_expression_or_member_access_p = true;
8890 && expr != error_mark_node
8891 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8892 /* We have an id-expression. */
8893 id_expression_or_member_access_p = true;
8896 if (!id_expression_or_member_access_p)
8898 /* Abort the id-expression parse. */
8899 cp_parser_abort_tentative_parse (parser);
8901 /* Parsing tentatively, again. */
8902 cp_parser_parse_tentatively (parser);
8904 /* Parse a class member access. */
8905 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8907 /*member_access_only_p=*/true, NULL);
8910 && expr != error_mark_node
8911 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8912 /* We have an id-expression. */
8913 id_expression_or_member_access_p = true;
8916 if (id_expression_or_member_access_p)
8917 /* We have parsed the complete id-expression or member access. */
8918 cp_parser_parse_definitely (parser);
8921 /* Abort our attempt to parse an id-expression or member access
8923 cp_parser_abort_tentative_parse (parser);
8925 /* Parse a full expression. */
8926 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8929 /* Go back to evaluating expressions. */
8932 /* Restore the old message and the integral constant expression
8934 parser->type_definition_forbidden_message = saved_message;
8935 parser->integral_constant_expression_p
8936 = saved_integral_constant_expression_p;
8937 parser->non_integral_constant_expression_p
8938 = saved_non_integral_constant_expression_p;
8940 if (expr == error_mark_node)
8942 /* Skip everything up to the closing `)'. */
8943 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8944 /*consume_paren=*/true);
8945 return error_mark_node;
8948 /* Parse to the closing `)'. */
8949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8951 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8952 /*consume_paren=*/true);
8953 return error_mark_node;
8956 return finish_decltype_type (expr, id_expression_or_member_access_p);
8959 /* Special member functions [gram.special] */
8961 /* Parse a conversion-function-id.
8963 conversion-function-id:
8964 operator conversion-type-id
8966 Returns an IDENTIFIER_NODE representing the operator. */
8969 cp_parser_conversion_function_id (cp_parser* parser)
8973 tree saved_qualifying_scope;
8974 tree saved_object_scope;
8975 tree pushed_scope = NULL_TREE;
8977 /* Look for the `operator' token. */
8978 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8979 return error_mark_node;
8980 /* When we parse the conversion-type-id, the current scope will be
8981 reset. However, we need that information in able to look up the
8982 conversion function later, so we save it here. */
8983 saved_scope = parser->scope;
8984 saved_qualifying_scope = parser->qualifying_scope;
8985 saved_object_scope = parser->object_scope;
8986 /* We must enter the scope of the class so that the names of
8987 entities declared within the class are available in the
8988 conversion-type-id. For example, consider:
8995 S::operator I() { ... }
8997 In order to see that `I' is a type-name in the definition, we
8998 must be in the scope of `S'. */
9000 pushed_scope = push_scope (saved_scope);
9001 /* Parse the conversion-type-id. */
9002 type = cp_parser_conversion_type_id (parser);
9003 /* Leave the scope of the class, if any. */
9005 pop_scope (pushed_scope);
9006 /* Restore the saved scope. */
9007 parser->scope = saved_scope;
9008 parser->qualifying_scope = saved_qualifying_scope;
9009 parser->object_scope = saved_object_scope;
9010 /* If the TYPE is invalid, indicate failure. */
9011 if (type == error_mark_node)
9012 return error_mark_node;
9013 return mangle_conv_op_name_for_type (type);
9016 /* Parse a conversion-type-id:
9019 type-specifier-seq conversion-declarator [opt]
9021 Returns the TYPE specified. */
9024 cp_parser_conversion_type_id (cp_parser* parser)
9027 cp_decl_specifier_seq type_specifiers;
9028 cp_declarator *declarator;
9029 tree type_specified;
9031 /* Parse the attributes. */
9032 attributes = cp_parser_attributes_opt (parser);
9033 /* Parse the type-specifiers. */
9034 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9036 /* If that didn't work, stop. */
9037 if (type_specifiers.type == error_mark_node)
9038 return error_mark_node;
9039 /* Parse the conversion-declarator. */
9040 declarator = cp_parser_conversion_declarator_opt (parser);
9042 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9043 /*initialized=*/0, &attributes);
9045 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9047 /* Don't give this error when parsing tentatively. This happens to
9048 work because we always parse this definitively once. */
9049 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9050 && type_uses_auto (type_specified))
9052 error ("invalid use of %<auto%> in conversion operator");
9053 return error_mark_node;
9056 return type_specified;
9059 /* Parse an (optional) conversion-declarator.
9061 conversion-declarator:
9062 ptr-operator conversion-declarator [opt]
9066 static cp_declarator *
9067 cp_parser_conversion_declarator_opt (cp_parser* parser)
9069 enum tree_code code;
9071 cp_cv_quals cv_quals;
9073 /* We don't know if there's a ptr-operator next, or not. */
9074 cp_parser_parse_tentatively (parser);
9075 /* Try the ptr-operator. */
9076 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9077 /* If it worked, look for more conversion-declarators. */
9078 if (cp_parser_parse_definitely (parser))
9080 cp_declarator *declarator;
9082 /* Parse another optional declarator. */
9083 declarator = cp_parser_conversion_declarator_opt (parser);
9085 return cp_parser_make_indirect_declarator
9086 (code, class_type, cv_quals, declarator);
9092 /* Parse an (optional) ctor-initializer.
9095 : mem-initializer-list
9097 Returns TRUE iff the ctor-initializer was actually present. */
9100 cp_parser_ctor_initializer_opt (cp_parser* parser)
9102 /* If the next token is not a `:', then there is no
9103 ctor-initializer. */
9104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9106 /* Do default initialization of any bases and members. */
9107 if (DECL_CONSTRUCTOR_P (current_function_decl))
9108 finish_mem_initializers (NULL_TREE);
9113 /* Consume the `:' token. */
9114 cp_lexer_consume_token (parser->lexer);
9115 /* And the mem-initializer-list. */
9116 cp_parser_mem_initializer_list (parser);
9121 /* Parse a mem-initializer-list.
9123 mem-initializer-list:
9124 mem-initializer ... [opt]
9125 mem-initializer ... [opt] , mem-initializer-list */
9128 cp_parser_mem_initializer_list (cp_parser* parser)
9130 tree mem_initializer_list = NULL_TREE;
9131 cp_token *token = cp_lexer_peek_token (parser->lexer);
9133 /* Let the semantic analysis code know that we are starting the
9134 mem-initializer-list. */
9135 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9136 error ("%Honly constructors take base initializers",
9139 /* Loop through the list. */
9142 tree mem_initializer;
9144 token = cp_lexer_peek_token (parser->lexer);
9145 /* Parse the mem-initializer. */
9146 mem_initializer = cp_parser_mem_initializer (parser);
9147 /* If the next token is a `...', we're expanding member initializers. */
9148 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9150 /* Consume the `...'. */
9151 cp_lexer_consume_token (parser->lexer);
9153 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9154 can be expanded but members cannot. */
9155 if (mem_initializer != error_mark_node
9156 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9158 error ("%Hcannot expand initializer for member %<%D%>",
9159 &token->location, TREE_PURPOSE (mem_initializer));
9160 mem_initializer = error_mark_node;
9163 /* Construct the pack expansion type. */
9164 if (mem_initializer != error_mark_node)
9165 mem_initializer = make_pack_expansion (mem_initializer);
9167 /* Add it to the list, unless it was erroneous. */
9168 if (mem_initializer != error_mark_node)
9170 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9171 mem_initializer_list = mem_initializer;
9173 /* If the next token is not a `,', we're done. */
9174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9176 /* Consume the `,' token. */
9177 cp_lexer_consume_token (parser->lexer);
9180 /* Perform semantic analysis. */
9181 if (DECL_CONSTRUCTOR_P (current_function_decl))
9182 finish_mem_initializers (mem_initializer_list);
9185 /* Parse a mem-initializer.
9188 mem-initializer-id ( expression-list [opt] )
9189 mem-initializer-id braced-init-list
9194 ( expression-list [opt] )
9196 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9197 class) or FIELD_DECL (for a non-static data member) to initialize;
9198 the TREE_VALUE is the expression-list. An empty initialization
9199 list is represented by void_list_node. */
9202 cp_parser_mem_initializer (cp_parser* parser)
9204 tree mem_initializer_id;
9205 tree expression_list;
9207 cp_token *token = cp_lexer_peek_token (parser->lexer);
9209 /* Find out what is being initialized. */
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9212 permerror (token->location,
9213 "anachronistic old-style base class initializer");
9214 mem_initializer_id = NULL_TREE;
9218 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9219 if (mem_initializer_id == error_mark_node)
9220 return mem_initializer_id;
9222 member = expand_member_init (mem_initializer_id);
9223 if (member && !DECL_P (member))
9224 in_base_initializer = 1;
9226 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9228 bool expr_non_constant_p;
9229 maybe_warn_cpp0x ("extended initializer lists");
9230 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9231 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9232 expression_list = build_tree_list (NULL_TREE, expression_list);
9236 = cp_parser_parenthesized_expression_list (parser, false,
9238 /*allow_expansion_p=*/true,
9239 /*non_constant_p=*/NULL);
9240 if (expression_list == error_mark_node)
9241 return error_mark_node;
9242 if (!expression_list)
9243 expression_list = void_type_node;
9245 in_base_initializer = 0;
9247 return member ? build_tree_list (member, expression_list) : error_mark_node;
9250 /* Parse a mem-initializer-id.
9253 :: [opt] nested-name-specifier [opt] class-name
9256 Returns a TYPE indicating the class to be initializer for the first
9257 production. Returns an IDENTIFIER_NODE indicating the data member
9258 to be initialized for the second production. */
9261 cp_parser_mem_initializer_id (cp_parser* parser)
9263 bool global_scope_p;
9264 bool nested_name_specifier_p;
9265 bool template_p = false;
9268 cp_token *token = cp_lexer_peek_token (parser->lexer);
9270 /* `typename' is not allowed in this context ([temp.res]). */
9271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9273 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9274 "member initializer is implicitly a type)",
9276 cp_lexer_consume_token (parser->lexer);
9278 /* Look for the optional `::' operator. */
9280 = (cp_parser_global_scope_opt (parser,
9281 /*current_scope_valid_p=*/false)
9283 /* Look for the optional nested-name-specifier. The simplest way to
9288 The keyword `typename' is not permitted in a base-specifier or
9289 mem-initializer; in these contexts a qualified name that
9290 depends on a template-parameter is implicitly assumed to be a
9293 is to assume that we have seen the `typename' keyword at this
9295 nested_name_specifier_p
9296 = (cp_parser_nested_name_specifier_opt (parser,
9297 /*typename_keyword_p=*/true,
9298 /*check_dependency_p=*/true,
9300 /*is_declaration=*/true)
9302 if (nested_name_specifier_p)
9303 template_p = cp_parser_optional_template_keyword (parser);
9304 /* If there is a `::' operator or a nested-name-specifier, then we
9305 are definitely looking for a class-name. */
9306 if (global_scope_p || nested_name_specifier_p)
9307 return cp_parser_class_name (parser,
9308 /*typename_keyword_p=*/true,
9309 /*template_keyword_p=*/template_p,
9311 /*check_dependency_p=*/true,
9312 /*class_head_p=*/false,
9313 /*is_declaration=*/true);
9314 /* Otherwise, we could also be looking for an ordinary identifier. */
9315 cp_parser_parse_tentatively (parser);
9316 /* Try a class-name. */
9317 id = cp_parser_class_name (parser,
9318 /*typename_keyword_p=*/true,
9319 /*template_keyword_p=*/false,
9321 /*check_dependency_p=*/true,
9322 /*class_head_p=*/false,
9323 /*is_declaration=*/true);
9324 /* If we found one, we're done. */
9325 if (cp_parser_parse_definitely (parser))
9327 /* Otherwise, look for an ordinary identifier. */
9328 return cp_parser_identifier (parser);
9331 /* Overloading [gram.over] */
9333 /* Parse an operator-function-id.
9335 operator-function-id:
9338 Returns an IDENTIFIER_NODE for the operator which is a
9339 human-readable spelling of the identifier, e.g., `operator +'. */
9342 cp_parser_operator_function_id (cp_parser* parser)
9344 /* Look for the `operator' keyword. */
9345 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9346 return error_mark_node;
9347 /* And then the name of the operator itself. */
9348 return cp_parser_operator (parser);
9351 /* Parse an operator.
9354 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9355 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9356 || ++ -- , ->* -> () []
9363 Returns an IDENTIFIER_NODE for the operator which is a
9364 human-readable spelling of the identifier, e.g., `operator +'. */
9367 cp_parser_operator (cp_parser* parser)
9369 tree id = NULL_TREE;
9372 /* Peek at the next token. */
9373 token = cp_lexer_peek_token (parser->lexer);
9374 /* Figure out which operator we have. */
9375 switch (token->type)
9381 /* The keyword should be either `new' or `delete'. */
9382 if (token->keyword == RID_NEW)
9384 else if (token->keyword == RID_DELETE)
9389 /* Consume the `new' or `delete' token. */
9390 cp_lexer_consume_token (parser->lexer);
9392 /* Peek at the next token. */
9393 token = cp_lexer_peek_token (parser->lexer);
9394 /* If it's a `[' token then this is the array variant of the
9396 if (token->type == CPP_OPEN_SQUARE)
9398 /* Consume the `[' token. */
9399 cp_lexer_consume_token (parser->lexer);
9400 /* Look for the `]' token. */
9401 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9402 id = ansi_opname (op == NEW_EXPR
9403 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9405 /* Otherwise, we have the non-array variant. */
9407 id = ansi_opname (op);
9413 id = ansi_opname (PLUS_EXPR);
9417 id = ansi_opname (MINUS_EXPR);
9421 id = ansi_opname (MULT_EXPR);
9425 id = ansi_opname (TRUNC_DIV_EXPR);
9429 id = ansi_opname (TRUNC_MOD_EXPR);
9433 id = ansi_opname (BIT_XOR_EXPR);
9437 id = ansi_opname (BIT_AND_EXPR);
9441 id = ansi_opname (BIT_IOR_EXPR);
9445 id = ansi_opname (BIT_NOT_EXPR);
9449 id = ansi_opname (TRUTH_NOT_EXPR);
9453 id = ansi_assopname (NOP_EXPR);
9457 id = ansi_opname (LT_EXPR);
9461 id = ansi_opname (GT_EXPR);
9465 id = ansi_assopname (PLUS_EXPR);
9469 id = ansi_assopname (MINUS_EXPR);
9473 id = ansi_assopname (MULT_EXPR);
9477 id = ansi_assopname (TRUNC_DIV_EXPR);
9481 id = ansi_assopname (TRUNC_MOD_EXPR);
9485 id = ansi_assopname (BIT_XOR_EXPR);
9489 id = ansi_assopname (BIT_AND_EXPR);
9493 id = ansi_assopname (BIT_IOR_EXPR);
9497 id = ansi_opname (LSHIFT_EXPR);
9501 id = ansi_opname (RSHIFT_EXPR);
9505 id = ansi_assopname (LSHIFT_EXPR);
9509 id = ansi_assopname (RSHIFT_EXPR);
9513 id = ansi_opname (EQ_EXPR);
9517 id = ansi_opname (NE_EXPR);
9521 id = ansi_opname (LE_EXPR);
9524 case CPP_GREATER_EQ:
9525 id = ansi_opname (GE_EXPR);
9529 id = ansi_opname (TRUTH_ANDIF_EXPR);
9533 id = ansi_opname (TRUTH_ORIF_EXPR);
9537 id = ansi_opname (POSTINCREMENT_EXPR);
9540 case CPP_MINUS_MINUS:
9541 id = ansi_opname (PREDECREMENT_EXPR);
9545 id = ansi_opname (COMPOUND_EXPR);
9548 case CPP_DEREF_STAR:
9549 id = ansi_opname (MEMBER_REF);
9553 id = ansi_opname (COMPONENT_REF);
9556 case CPP_OPEN_PAREN:
9557 /* Consume the `('. */
9558 cp_lexer_consume_token (parser->lexer);
9559 /* Look for the matching `)'. */
9560 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9561 return ansi_opname (CALL_EXPR);
9563 case CPP_OPEN_SQUARE:
9564 /* Consume the `['. */
9565 cp_lexer_consume_token (parser->lexer);
9566 /* Look for the matching `]'. */
9567 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9568 return ansi_opname (ARRAY_REF);
9571 /* Anything else is an error. */
9575 /* If we have selected an identifier, we need to consume the
9578 cp_lexer_consume_token (parser->lexer);
9579 /* Otherwise, no valid operator name was present. */
9582 cp_parser_error (parser, "expected operator");
9583 id = error_mark_node;
9589 /* Parse a template-declaration.
9591 template-declaration:
9592 export [opt] template < template-parameter-list > declaration
9594 If MEMBER_P is TRUE, this template-declaration occurs within a
9597 The grammar rule given by the standard isn't correct. What
9600 template-declaration:
9601 export [opt] template-parameter-list-seq
9602 decl-specifier-seq [opt] init-declarator [opt] ;
9603 export [opt] template-parameter-list-seq
9606 template-parameter-list-seq:
9607 template-parameter-list-seq [opt]
9608 template < template-parameter-list > */
9611 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9613 /* Check for `export'. */
9614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9616 /* Consume the `export' token. */
9617 cp_lexer_consume_token (parser->lexer);
9618 /* Warn that we do not support `export'. */
9619 warning (0, "keyword %<export%> not implemented, and will be ignored");
9622 cp_parser_template_declaration_after_export (parser, member_p);
9625 /* Parse a template-parameter-list.
9627 template-parameter-list:
9629 template-parameter-list , template-parameter
9631 Returns a TREE_LIST. Each node represents a template parameter.
9632 The nodes are connected via their TREE_CHAINs. */
9635 cp_parser_template_parameter_list (cp_parser* parser)
9637 tree parameter_list = NULL_TREE;
9639 begin_template_parm_list ();
9644 bool is_parameter_pack;
9646 /* Parse the template-parameter. */
9647 parameter = cp_parser_template_parameter (parser,
9649 &is_parameter_pack);
9650 /* Add it to the list. */
9651 if (parameter != error_mark_node)
9652 parameter_list = process_template_parm (parameter_list,
9658 tree err_parm = build_tree_list (parameter, parameter);
9659 TREE_VALUE (err_parm) = error_mark_node;
9660 parameter_list = chainon (parameter_list, err_parm);
9663 /* If the next token is not a `,', we're done. */
9664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9666 /* Otherwise, consume the `,' token. */
9667 cp_lexer_consume_token (parser->lexer);
9670 return end_template_parm_list (parameter_list);
9673 /* Parse a template-parameter.
9677 parameter-declaration
9679 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9680 the parameter. The TREE_PURPOSE is the default value, if any.
9681 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9682 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9683 set to true iff this parameter is a parameter pack. */
9686 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9687 bool *is_parameter_pack)
9690 cp_parameter_declarator *parameter_declarator;
9691 cp_declarator *id_declarator;
9694 /* Assume it is a type parameter or a template parameter. */
9695 *is_non_type = false;
9696 /* Assume it not a parameter pack. */
9697 *is_parameter_pack = false;
9698 /* Peek at the next token. */
9699 token = cp_lexer_peek_token (parser->lexer);
9700 /* If it is `class' or `template', we have a type-parameter. */
9701 if (token->keyword == RID_TEMPLATE)
9702 return cp_parser_type_parameter (parser, is_parameter_pack);
9703 /* If it is `class' or `typename' we do not know yet whether it is a
9704 type parameter or a non-type parameter. Consider:
9706 template <typename T, typename T::X X> ...
9710 template <class C, class D*> ...
9712 Here, the first parameter is a type parameter, and the second is
9713 a non-type parameter. We can tell by looking at the token after
9714 the identifier -- if it is a `,', `=', or `>' then we have a type
9716 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9718 /* Peek at the token after `class' or `typename'. */
9719 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9720 /* If it's an ellipsis, we have a template type parameter
9722 if (token->type == CPP_ELLIPSIS)
9723 return cp_parser_type_parameter (parser, is_parameter_pack);
9724 /* If it's an identifier, skip it. */
9725 if (token->type == CPP_NAME)
9726 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9727 /* Now, see if the token looks like the end of a template
9729 if (token->type == CPP_COMMA
9730 || token->type == CPP_EQ
9731 || token->type == CPP_GREATER)
9732 return cp_parser_type_parameter (parser, is_parameter_pack);
9735 /* Otherwise, it is a non-type parameter.
9739 When parsing a default template-argument for a non-type
9740 template-parameter, the first non-nested `>' is taken as the end
9741 of the template parameter-list rather than a greater-than
9743 *is_non_type = true;
9744 parameter_declarator
9745 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9746 /*parenthesized_p=*/NULL);
9748 /* If the parameter declaration is marked as a parameter pack, set
9749 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9750 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9752 if (parameter_declarator
9753 && parameter_declarator->declarator
9754 && parameter_declarator->declarator->parameter_pack_p)
9756 *is_parameter_pack = true;
9757 parameter_declarator->declarator->parameter_pack_p = false;
9760 /* If the next token is an ellipsis, and we don't already have it
9761 marked as a parameter pack, then we have a parameter pack (that
9762 has no declarator). */
9763 if (!*is_parameter_pack
9764 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9765 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9767 /* Consume the `...'. */
9768 cp_lexer_consume_token (parser->lexer);
9769 maybe_warn_variadic_templates ();
9771 *is_parameter_pack = true;
9773 /* We might end up with a pack expansion as the type of the non-type
9774 template parameter, in which case this is a non-type template
9776 else if (parameter_declarator
9777 && parameter_declarator->decl_specifiers.type
9778 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9780 *is_parameter_pack = true;
9781 parameter_declarator->decl_specifiers.type =
9782 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9785 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9787 /* Parameter packs cannot have default arguments. However, a
9788 user may try to do so, so we'll parse them and give an
9789 appropriate diagnostic here. */
9791 /* Consume the `='. */
9792 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9793 cp_lexer_consume_token (parser->lexer);
9795 /* Find the name of the parameter pack. */
9796 id_declarator = parameter_declarator->declarator;
9797 while (id_declarator && id_declarator->kind != cdk_id)
9798 id_declarator = id_declarator->declarator;
9800 if (id_declarator && id_declarator->kind == cdk_id)
9801 error ("%Htemplate parameter pack %qD cannot have a default argument",
9802 &start_token->location, id_declarator->u.id.unqualified_name);
9804 error ("%Htemplate parameter pack cannot have a default argument",
9805 &start_token->location);
9807 /* Parse the default argument, but throw away the result. */
9808 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9811 parm = grokdeclarator (parameter_declarator->declarator,
9812 ¶meter_declarator->decl_specifiers,
9813 PARM, /*initialized=*/0,
9815 if (parm == error_mark_node)
9816 return error_mark_node;
9818 return build_tree_list (parameter_declarator->default_argument, parm);
9821 /* Parse a type-parameter.
9824 class identifier [opt]
9825 class identifier [opt] = type-id
9826 typename identifier [opt]
9827 typename identifier [opt] = type-id
9828 template < template-parameter-list > class identifier [opt]
9829 template < template-parameter-list > class identifier [opt]
9832 GNU Extension (variadic templates):
9835 class ... identifier [opt]
9836 typename ... identifier [opt]
9838 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9839 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9840 the declaration of the parameter.
9842 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9845 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9850 /* Look for a keyword to tell us what kind of parameter this is. */
9851 token = cp_parser_require (parser, CPP_KEYWORD,
9852 "%<class%>, %<typename%>, or %<template%>");
9854 return error_mark_node;
9856 switch (token->keyword)
9862 tree default_argument;
9864 /* If the next token is an ellipsis, we have a template
9866 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9868 /* Consume the `...' token. */
9869 cp_lexer_consume_token (parser->lexer);
9870 maybe_warn_variadic_templates ();
9872 *is_parameter_pack = true;
9875 /* If the next token is an identifier, then it names the
9877 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9878 identifier = cp_parser_identifier (parser);
9880 identifier = NULL_TREE;
9882 /* Create the parameter. */
9883 parameter = finish_template_type_parm (class_type_node, identifier);
9885 /* If the next token is an `=', we have a default argument. */
9886 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9888 /* Consume the `=' token. */
9889 cp_lexer_consume_token (parser->lexer);
9890 /* Parse the default-argument. */
9891 push_deferring_access_checks (dk_no_deferred);
9892 default_argument = cp_parser_type_id (parser);
9894 /* Template parameter packs cannot have default
9896 if (*is_parameter_pack)
9899 error ("%Htemplate parameter pack %qD cannot have a "
9900 "default argument", &token->location, identifier);
9902 error ("%Htemplate parameter packs cannot have "
9903 "default arguments", &token->location);
9904 default_argument = NULL_TREE;
9906 pop_deferring_access_checks ();
9909 default_argument = NULL_TREE;
9911 /* Create the combined representation of the parameter and the
9912 default argument. */
9913 parameter = build_tree_list (default_argument, parameter);
9919 tree parameter_list;
9921 tree default_argument;
9923 /* Look for the `<'. */
9924 cp_parser_require (parser, CPP_LESS, "%<<%>");
9925 /* Parse the template-parameter-list. */
9926 parameter_list = cp_parser_template_parameter_list (parser);
9927 /* Look for the `>'. */
9928 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9929 /* Look for the `class' keyword. */
9930 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9931 /* If the next token is an ellipsis, we have a template
9933 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9935 /* Consume the `...' token. */
9936 cp_lexer_consume_token (parser->lexer);
9937 maybe_warn_variadic_templates ();
9939 *is_parameter_pack = true;
9941 /* If the next token is an `=', then there is a
9942 default-argument. If the next token is a `>', we are at
9943 the end of the parameter-list. If the next token is a `,',
9944 then we are at the end of this parameter. */
9945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9946 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9947 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9949 identifier = cp_parser_identifier (parser);
9950 /* Treat invalid names as if the parameter were nameless. */
9951 if (identifier == error_mark_node)
9952 identifier = NULL_TREE;
9955 identifier = NULL_TREE;
9957 /* Create the template parameter. */
9958 parameter = finish_template_template_parm (class_type_node,
9961 /* If the next token is an `=', then there is a
9962 default-argument. */
9963 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9967 /* Consume the `='. */
9968 cp_lexer_consume_token (parser->lexer);
9969 /* Parse the id-expression. */
9970 push_deferring_access_checks (dk_no_deferred);
9971 /* save token before parsing the id-expression, for error
9973 token = cp_lexer_peek_token (parser->lexer);
9975 = cp_parser_id_expression (parser,
9976 /*template_keyword_p=*/false,
9977 /*check_dependency_p=*/true,
9978 /*template_p=*/&is_template,
9979 /*declarator_p=*/false,
9980 /*optional_p=*/false);
9981 if (TREE_CODE (default_argument) == TYPE_DECL)
9982 /* If the id-expression was a template-id that refers to
9983 a template-class, we already have the declaration here,
9984 so no further lookup is needed. */
9987 /* Look up the name. */
9989 = cp_parser_lookup_name (parser, default_argument,
9991 /*is_template=*/is_template,
9992 /*is_namespace=*/false,
9993 /*check_dependency=*/true,
9994 /*ambiguous_decls=*/NULL,
9996 /* See if the default argument is valid. */
9998 = check_template_template_default_arg (default_argument);
10000 /* Template parameter packs cannot have default
10002 if (*is_parameter_pack)
10005 error ("%Htemplate parameter pack %qD cannot "
10006 "have a default argument",
10007 &token->location, identifier);
10009 error ("%Htemplate parameter packs cannot "
10010 "have default arguments",
10012 default_argument = NULL_TREE;
10014 pop_deferring_access_checks ();
10017 default_argument = NULL_TREE;
10019 /* Create the combined representation of the parameter and the
10020 default argument. */
10021 parameter = build_tree_list (default_argument, parameter);
10026 gcc_unreachable ();
10033 /* Parse a template-id.
10036 template-name < template-argument-list [opt] >
10038 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10039 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10040 returned. Otherwise, if the template-name names a function, or set
10041 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10042 names a class, returns a TYPE_DECL for the specialization.
10044 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10045 uninstantiated templates. */
10048 cp_parser_template_id (cp_parser *parser,
10049 bool template_keyword_p,
10050 bool check_dependency_p,
10051 bool is_declaration)
10057 cp_token_position start_of_id = 0;
10058 deferred_access_check *chk;
10059 VEC (deferred_access_check,gc) *access_check;
10060 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10061 bool is_identifier;
10063 /* If the next token corresponds to a template-id, there is no need
10065 next_token = cp_lexer_peek_token (parser->lexer);
10066 if (next_token->type == CPP_TEMPLATE_ID)
10068 struct tree_check *check_value;
10070 /* Get the stored value. */
10071 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10072 /* Perform any access checks that were deferred. */
10073 access_check = check_value->checks;
10077 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10080 perform_or_defer_access_check (chk->binfo,
10085 /* Return the stored value. */
10086 return check_value->value;
10089 /* Avoid performing name lookup if there is no possibility of
10090 finding a template-id. */
10091 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10092 || (next_token->type == CPP_NAME
10093 && !cp_parser_nth_token_starts_template_argument_list_p
10096 cp_parser_error (parser, "expected template-id");
10097 return error_mark_node;
10100 /* Remember where the template-id starts. */
10101 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10102 start_of_id = cp_lexer_token_position (parser->lexer, false);
10104 push_deferring_access_checks (dk_deferred);
10106 /* Parse the template-name. */
10107 is_identifier = false;
10108 token = cp_lexer_peek_token (parser->lexer);
10109 templ = cp_parser_template_name (parser, template_keyword_p,
10110 check_dependency_p,
10113 if (templ == error_mark_node || is_identifier)
10115 pop_deferring_access_checks ();
10119 /* If we find the sequence `[:' after a template-name, it's probably
10120 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10121 parse correctly the argument list. */
10122 next_token = cp_lexer_peek_token (parser->lexer);
10123 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10124 if (next_token->type == CPP_OPEN_SQUARE
10125 && next_token->flags & DIGRAPH
10126 && next_token_2->type == CPP_COLON
10127 && !(next_token_2->flags & PREV_WHITE))
10129 cp_parser_parse_tentatively (parser);
10130 /* Change `:' into `::'. */
10131 next_token_2->type = CPP_SCOPE;
10132 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10134 cp_lexer_consume_token (parser->lexer);
10136 /* Parse the arguments. */
10137 arguments = cp_parser_enclosed_template_argument_list (parser);
10138 if (!cp_parser_parse_definitely (parser))
10140 /* If we couldn't parse an argument list, then we revert our changes
10141 and return simply an error. Maybe this is not a template-id
10143 next_token_2->type = CPP_COLON;
10144 cp_parser_error (parser, "expected %<<%>");
10145 pop_deferring_access_checks ();
10146 return error_mark_node;
10148 /* Otherwise, emit an error about the invalid digraph, but continue
10149 parsing because we got our argument list. */
10150 if (permerror (next_token->location,
10151 "%<<::%> cannot begin a template-argument list"))
10153 static bool hint = false;
10154 inform (next_token->location,
10155 "%<<:%> is an alternate spelling for %<[%>."
10156 " Insert whitespace between %<<%> and %<::%>");
10157 if (!hint && !flag_permissive)
10159 inform (next_token->location, "(if you use %<-fpermissive%>"
10160 " G++ will accept your code)");
10167 /* Look for the `<' that starts the template-argument-list. */
10168 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10170 pop_deferring_access_checks ();
10171 return error_mark_node;
10173 /* Parse the arguments. */
10174 arguments = cp_parser_enclosed_template_argument_list (parser);
10177 /* Build a representation of the specialization. */
10178 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10179 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10180 else if (DECL_CLASS_TEMPLATE_P (templ)
10181 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10183 bool entering_scope;
10184 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10185 template (rather than some instantiation thereof) only if
10186 is not nested within some other construct. For example, in
10187 "template <typename T> void f(T) { A<T>::", A<T> is just an
10188 instantiation of A. */
10189 entering_scope = (template_parm_scope_p ()
10190 && cp_lexer_next_token_is (parser->lexer,
10193 = finish_template_type (templ, arguments, entering_scope);
10197 /* If it's not a class-template or a template-template, it should be
10198 a function-template. */
10199 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10200 || TREE_CODE (templ) == OVERLOAD
10201 || BASELINK_P (templ)));
10203 template_id = lookup_template_function (templ, arguments);
10206 /* If parsing tentatively, replace the sequence of tokens that makes
10207 up the template-id with a CPP_TEMPLATE_ID token. That way,
10208 should we re-parse the token stream, we will not have to repeat
10209 the effort required to do the parse, nor will we issue duplicate
10210 error messages about problems during instantiation of the
10214 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10216 /* Reset the contents of the START_OF_ID token. */
10217 token->type = CPP_TEMPLATE_ID;
10218 /* Retrieve any deferred checks. Do not pop this access checks yet
10219 so the memory will not be reclaimed during token replacing below. */
10220 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10221 token->u.tree_check_value->value = template_id;
10222 token->u.tree_check_value->checks = get_deferred_access_checks ();
10223 token->keyword = RID_MAX;
10225 /* Purge all subsequent tokens. */
10226 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10228 /* ??? Can we actually assume that, if template_id ==
10229 error_mark_node, we will have issued a diagnostic to the
10230 user, as opposed to simply marking the tentative parse as
10232 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10233 error ("%Hparse error in template argument list",
10237 pop_deferring_access_checks ();
10238 return template_id;
10241 /* Parse a template-name.
10246 The standard should actually say:
10250 operator-function-id
10252 A defect report has been filed about this issue.
10254 A conversion-function-id cannot be a template name because they cannot
10255 be part of a template-id. In fact, looking at this code:
10257 a.operator K<int>()
10259 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10260 It is impossible to call a templated conversion-function-id with an
10261 explicit argument list, since the only allowed template parameter is
10262 the type to which it is converting.
10264 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10265 `template' keyword, in a construction like:
10269 In that case `f' is taken to be a template-name, even though there
10270 is no way of knowing for sure.
10272 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10273 name refers to a set of overloaded functions, at least one of which
10274 is a template, or an IDENTIFIER_NODE with the name of the template,
10275 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10276 names are looked up inside uninstantiated templates. */
10279 cp_parser_template_name (cp_parser* parser,
10280 bool template_keyword_p,
10281 bool check_dependency_p,
10282 bool is_declaration,
10283 bool *is_identifier)
10288 cp_token *token = cp_lexer_peek_token (parser->lexer);
10290 /* If the next token is `operator', then we have either an
10291 operator-function-id or a conversion-function-id. */
10292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10294 /* We don't know whether we're looking at an
10295 operator-function-id or a conversion-function-id. */
10296 cp_parser_parse_tentatively (parser);
10297 /* Try an operator-function-id. */
10298 identifier = cp_parser_operator_function_id (parser);
10299 /* If that didn't work, try a conversion-function-id. */
10300 if (!cp_parser_parse_definitely (parser))
10302 cp_parser_error (parser, "expected template-name");
10303 return error_mark_node;
10306 /* Look for the identifier. */
10308 identifier = cp_parser_identifier (parser);
10310 /* If we didn't find an identifier, we don't have a template-id. */
10311 if (identifier == error_mark_node)
10312 return error_mark_node;
10314 /* If the name immediately followed the `template' keyword, then it
10315 is a template-name. However, if the next token is not `<', then
10316 we do not treat it as a template-name, since it is not being used
10317 as part of a template-id. This enables us to handle constructs
10320 template <typename T> struct S { S(); };
10321 template <typename T> S<T>::S();
10323 correctly. We would treat `S' as a template -- if it were `S<T>'
10324 -- but we do not if there is no `<'. */
10326 if (processing_template_decl
10327 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10329 /* In a declaration, in a dependent context, we pretend that the
10330 "template" keyword was present in order to improve error
10331 recovery. For example, given:
10333 template <typename T> void f(T::X<int>);
10335 we want to treat "X<int>" as a template-id. */
10337 && !template_keyword_p
10338 && parser->scope && TYPE_P (parser->scope)
10339 && check_dependency_p
10340 && dependent_scope_p (parser->scope)
10341 /* Do not do this for dtors (or ctors), since they never
10342 need the template keyword before their name. */
10343 && !constructor_name_p (identifier, parser->scope))
10345 cp_token_position start = 0;
10347 /* Explain what went wrong. */
10348 error ("%Hnon-template %qD used as template",
10349 &token->location, identifier);
10350 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10351 parser->scope, identifier);
10352 /* If parsing tentatively, find the location of the "<" token. */
10353 if (cp_parser_simulate_error (parser))
10354 start = cp_lexer_token_position (parser->lexer, true);
10355 /* Parse the template arguments so that we can issue error
10356 messages about them. */
10357 cp_lexer_consume_token (parser->lexer);
10358 cp_parser_enclosed_template_argument_list (parser);
10359 /* Skip tokens until we find a good place from which to
10360 continue parsing. */
10361 cp_parser_skip_to_closing_parenthesis (parser,
10362 /*recovering=*/true,
10364 /*consume_paren=*/false);
10365 /* If parsing tentatively, permanently remove the
10366 template argument list. That will prevent duplicate
10367 error messages from being issued about the missing
10368 "template" keyword. */
10370 cp_lexer_purge_tokens_after (parser->lexer, start);
10372 *is_identifier = true;
10376 /* If the "template" keyword is present, then there is generally
10377 no point in doing name-lookup, so we just return IDENTIFIER.
10378 But, if the qualifying scope is non-dependent then we can
10379 (and must) do name-lookup normally. */
10380 if (template_keyword_p
10382 || (TYPE_P (parser->scope)
10383 && dependent_type_p (parser->scope))))
10387 /* Look up the name. */
10388 decl = cp_parser_lookup_name (parser, identifier,
10390 /*is_template=*/false,
10391 /*is_namespace=*/false,
10392 check_dependency_p,
10393 /*ambiguous_decls=*/NULL,
10395 decl = maybe_get_template_decl_from_type_decl (decl);
10397 /* If DECL is a template, then the name was a template-name. */
10398 if (TREE_CODE (decl) == TEMPLATE_DECL)
10402 tree fn = NULL_TREE;
10404 /* The standard does not explicitly indicate whether a name that
10405 names a set of overloaded declarations, some of which are
10406 templates, is a template-name. However, such a name should
10407 be a template-name; otherwise, there is no way to form a
10408 template-id for the overloaded templates. */
10409 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10410 if (TREE_CODE (fns) == OVERLOAD)
10411 for (fn = fns; fn; fn = OVL_NEXT (fn))
10412 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10417 /* The name does not name a template. */
10418 cp_parser_error (parser, "expected template-name");
10419 return error_mark_node;
10423 /* If DECL is dependent, and refers to a function, then just return
10424 its name; we will look it up again during template instantiation. */
10425 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10427 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10428 if (TYPE_P (scope) && dependent_type_p (scope))
10435 /* Parse a template-argument-list.
10437 template-argument-list:
10438 template-argument ... [opt]
10439 template-argument-list , template-argument ... [opt]
10441 Returns a TREE_VEC containing the arguments. */
10444 cp_parser_template_argument_list (cp_parser* parser)
10446 tree fixed_args[10];
10447 unsigned n_args = 0;
10448 unsigned alloced = 10;
10449 tree *arg_ary = fixed_args;
10451 bool saved_in_template_argument_list_p;
10453 bool saved_non_ice_p;
10455 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10456 parser->in_template_argument_list_p = true;
10457 /* Even if the template-id appears in an integral
10458 constant-expression, the contents of the argument list do
10460 saved_ice_p = parser->integral_constant_expression_p;
10461 parser->integral_constant_expression_p = false;
10462 saved_non_ice_p = parser->non_integral_constant_expression_p;
10463 parser->non_integral_constant_expression_p = false;
10464 /* Parse the arguments. */
10470 /* Consume the comma. */
10471 cp_lexer_consume_token (parser->lexer);
10473 /* Parse the template-argument. */
10474 argument = cp_parser_template_argument (parser);
10476 /* If the next token is an ellipsis, we're expanding a template
10478 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10480 /* Consume the `...' token. */
10481 cp_lexer_consume_token (parser->lexer);
10483 /* Make the argument into a TYPE_PACK_EXPANSION or
10484 EXPR_PACK_EXPANSION. */
10485 argument = make_pack_expansion (argument);
10488 if (n_args == alloced)
10492 if (arg_ary == fixed_args)
10494 arg_ary = XNEWVEC (tree, alloced);
10495 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10498 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10500 arg_ary[n_args++] = argument;
10502 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10504 vec = make_tree_vec (n_args);
10507 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10509 if (arg_ary != fixed_args)
10511 parser->non_integral_constant_expression_p = saved_non_ice_p;
10512 parser->integral_constant_expression_p = saved_ice_p;
10513 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10517 /* Parse a template-argument.
10520 assignment-expression
10524 The representation is that of an assignment-expression, type-id, or
10525 id-expression -- except that the qualified id-expression is
10526 evaluated, so that the value returned is either a DECL or an
10529 Although the standard says "assignment-expression", it forbids
10530 throw-expressions or assignments in the template argument.
10531 Therefore, we use "conditional-expression" instead. */
10534 cp_parser_template_argument (cp_parser* parser)
10539 bool maybe_type_id = false;
10540 cp_token *token = NULL, *argument_start_token = NULL;
10543 /* There's really no way to know what we're looking at, so we just
10544 try each alternative in order.
10548 In a template-argument, an ambiguity between a type-id and an
10549 expression is resolved to a type-id, regardless of the form of
10550 the corresponding template-parameter.
10552 Therefore, we try a type-id first. */
10553 cp_parser_parse_tentatively (parser);
10554 argument = cp_parser_template_type_arg (parser);
10555 /* If there was no error parsing the type-id but the next token is a
10556 '>>', our behavior depends on which dialect of C++ we're
10557 parsing. In C++98, we probably found a typo for '> >'. But there
10558 are type-id which are also valid expressions. For instance:
10560 struct X { int operator >> (int); };
10561 template <int V> struct Foo {};
10564 Here 'X()' is a valid type-id of a function type, but the user just
10565 wanted to write the expression "X() >> 5". Thus, we remember that we
10566 found a valid type-id, but we still try to parse the argument as an
10567 expression to see what happens.
10569 In C++0x, the '>>' will be considered two separate '>'
10571 if (!cp_parser_error_occurred (parser)
10572 && cxx_dialect == cxx98
10573 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10575 maybe_type_id = true;
10576 cp_parser_abort_tentative_parse (parser);
10580 /* If the next token isn't a `,' or a `>', then this argument wasn't
10581 really finished. This means that the argument is not a valid
10583 if (!cp_parser_next_token_ends_template_argument_p (parser))
10584 cp_parser_error (parser, "expected template-argument");
10585 /* If that worked, we're done. */
10586 if (cp_parser_parse_definitely (parser))
10589 /* We're still not sure what the argument will be. */
10590 cp_parser_parse_tentatively (parser);
10591 /* Try a template. */
10592 argument_start_token = cp_lexer_peek_token (parser->lexer);
10593 argument = cp_parser_id_expression (parser,
10594 /*template_keyword_p=*/false,
10595 /*check_dependency_p=*/true,
10597 /*declarator_p=*/false,
10598 /*optional_p=*/false);
10599 /* If the next token isn't a `,' or a `>', then this argument wasn't
10600 really finished. */
10601 if (!cp_parser_next_token_ends_template_argument_p (parser))
10602 cp_parser_error (parser, "expected template-argument");
10603 if (!cp_parser_error_occurred (parser))
10605 /* Figure out what is being referred to. If the id-expression
10606 was for a class template specialization, then we will have a
10607 TYPE_DECL at this point. There is no need to do name lookup
10608 at this point in that case. */
10609 if (TREE_CODE (argument) != TYPE_DECL)
10610 argument = cp_parser_lookup_name (parser, argument,
10612 /*is_template=*/template_p,
10613 /*is_namespace=*/false,
10614 /*check_dependency=*/true,
10615 /*ambiguous_decls=*/NULL,
10616 argument_start_token->location);
10617 if (TREE_CODE (argument) != TEMPLATE_DECL
10618 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10619 cp_parser_error (parser, "expected template-name");
10621 if (cp_parser_parse_definitely (parser))
10623 /* It must be a non-type argument. There permitted cases are given
10624 in [temp.arg.nontype]:
10626 -- an integral constant-expression of integral or enumeration
10629 -- the name of a non-type template-parameter; or
10631 -- the name of an object or function with external linkage...
10633 -- the address of an object or function with external linkage...
10635 -- a pointer to member... */
10636 /* Look for a non-type template parameter. */
10637 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10639 cp_parser_parse_tentatively (parser);
10640 argument = cp_parser_primary_expression (parser,
10641 /*address_p=*/false,
10643 /*template_arg_p=*/true,
10645 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10646 || !cp_parser_next_token_ends_template_argument_p (parser))
10647 cp_parser_simulate_error (parser);
10648 if (cp_parser_parse_definitely (parser))
10652 /* If the next token is "&", the argument must be the address of an
10653 object or function with external linkage. */
10654 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10656 cp_lexer_consume_token (parser->lexer);
10657 /* See if we might have an id-expression. */
10658 token = cp_lexer_peek_token (parser->lexer);
10659 if (token->type == CPP_NAME
10660 || token->keyword == RID_OPERATOR
10661 || token->type == CPP_SCOPE
10662 || token->type == CPP_TEMPLATE_ID
10663 || token->type == CPP_NESTED_NAME_SPECIFIER)
10665 cp_parser_parse_tentatively (parser);
10666 argument = cp_parser_primary_expression (parser,
10669 /*template_arg_p=*/true,
10671 if (cp_parser_error_occurred (parser)
10672 || !cp_parser_next_token_ends_template_argument_p (parser))
10673 cp_parser_abort_tentative_parse (parser);
10676 if (TREE_CODE (argument) == INDIRECT_REF)
10678 gcc_assert (REFERENCE_REF_P (argument));
10679 argument = TREE_OPERAND (argument, 0);
10682 if (TREE_CODE (argument) == VAR_DECL)
10684 /* A variable without external linkage might still be a
10685 valid constant-expression, so no error is issued here
10686 if the external-linkage check fails. */
10687 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10688 cp_parser_simulate_error (parser);
10690 else if (is_overloaded_fn (argument))
10691 /* All overloaded functions are allowed; if the external
10692 linkage test does not pass, an error will be issued
10696 && (TREE_CODE (argument) == OFFSET_REF
10697 || TREE_CODE (argument) == SCOPE_REF))
10698 /* A pointer-to-member. */
10700 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10703 cp_parser_simulate_error (parser);
10705 if (cp_parser_parse_definitely (parser))
10708 argument = build_x_unary_op (ADDR_EXPR, argument,
10709 tf_warning_or_error);
10714 /* If the argument started with "&", there are no other valid
10715 alternatives at this point. */
10718 cp_parser_error (parser, "invalid non-type template argument");
10719 return error_mark_node;
10722 /* If the argument wasn't successfully parsed as a type-id followed
10723 by '>>', the argument can only be a constant expression now.
10724 Otherwise, we try parsing the constant-expression tentatively,
10725 because the argument could really be a type-id. */
10727 cp_parser_parse_tentatively (parser);
10728 argument = cp_parser_constant_expression (parser,
10729 /*allow_non_constant_p=*/false,
10730 /*non_constant_p=*/NULL);
10731 argument = fold_non_dependent_expr (argument);
10732 if (!maybe_type_id)
10734 if (!cp_parser_next_token_ends_template_argument_p (parser))
10735 cp_parser_error (parser, "expected template-argument");
10736 if (cp_parser_parse_definitely (parser))
10738 /* We did our best to parse the argument as a non type-id, but that
10739 was the only alternative that matched (albeit with a '>' after
10740 it). We can assume it's just a typo from the user, and a
10741 diagnostic will then be issued. */
10742 return cp_parser_template_type_arg (parser);
10745 /* Parse an explicit-instantiation.
10747 explicit-instantiation:
10748 template declaration
10750 Although the standard says `declaration', what it really means is:
10752 explicit-instantiation:
10753 template decl-specifier-seq [opt] declarator [opt] ;
10755 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10756 supposed to be allowed. A defect report has been filed about this
10761 explicit-instantiation:
10762 storage-class-specifier template
10763 decl-specifier-seq [opt] declarator [opt] ;
10764 function-specifier template
10765 decl-specifier-seq [opt] declarator [opt] ; */
10768 cp_parser_explicit_instantiation (cp_parser* parser)
10770 int declares_class_or_enum;
10771 cp_decl_specifier_seq decl_specifiers;
10772 tree extension_specifier = NULL_TREE;
10775 /* Look for an (optional) storage-class-specifier or
10776 function-specifier. */
10777 if (cp_parser_allow_gnu_extensions_p (parser))
10779 extension_specifier
10780 = cp_parser_storage_class_specifier_opt (parser);
10781 if (!extension_specifier)
10782 extension_specifier
10783 = cp_parser_function_specifier_opt (parser,
10784 /*decl_specs=*/NULL);
10787 /* Look for the `template' keyword. */
10788 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10789 /* Let the front end know that we are processing an explicit
10791 begin_explicit_instantiation ();
10792 /* [temp.explicit] says that we are supposed to ignore access
10793 control while processing explicit instantiation directives. */
10794 push_deferring_access_checks (dk_no_check);
10795 /* Parse a decl-specifier-seq. */
10796 token = cp_lexer_peek_token (parser->lexer);
10797 cp_parser_decl_specifier_seq (parser,
10798 CP_PARSER_FLAGS_OPTIONAL,
10800 &declares_class_or_enum);
10801 /* If there was exactly one decl-specifier, and it declared a class,
10802 and there's no declarator, then we have an explicit type
10804 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10808 type = check_tag_decl (&decl_specifiers);
10809 /* Turn access control back on for names used during
10810 template instantiation. */
10811 pop_deferring_access_checks ();
10813 do_type_instantiation (type, extension_specifier,
10814 /*complain=*/tf_error);
10818 cp_declarator *declarator;
10821 /* Parse the declarator. */
10823 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10824 /*ctor_dtor_or_conv_p=*/NULL,
10825 /*parenthesized_p=*/NULL,
10826 /*member_p=*/false);
10827 if (declares_class_or_enum & 2)
10828 cp_parser_check_for_definition_in_return_type (declarator,
10829 decl_specifiers.type,
10830 decl_specifiers.type_location);
10831 if (declarator != cp_error_declarator)
10833 decl = grokdeclarator (declarator, &decl_specifiers,
10834 NORMAL, 0, &decl_specifiers.attributes);
10835 /* Turn access control back on for names used during
10836 template instantiation. */
10837 pop_deferring_access_checks ();
10838 /* Do the explicit instantiation. */
10839 do_decl_instantiation (decl, extension_specifier);
10843 pop_deferring_access_checks ();
10844 /* Skip the body of the explicit instantiation. */
10845 cp_parser_skip_to_end_of_statement (parser);
10848 /* We're done with the instantiation. */
10849 end_explicit_instantiation ();
10851 cp_parser_consume_semicolon_at_end_of_statement (parser);
10854 /* Parse an explicit-specialization.
10856 explicit-specialization:
10857 template < > declaration
10859 Although the standard says `declaration', what it really means is:
10861 explicit-specialization:
10862 template <> decl-specifier [opt] init-declarator [opt] ;
10863 template <> function-definition
10864 template <> explicit-specialization
10865 template <> template-declaration */
10868 cp_parser_explicit_specialization (cp_parser* parser)
10870 bool need_lang_pop;
10871 cp_token *token = cp_lexer_peek_token (parser->lexer);
10873 /* Look for the `template' keyword. */
10874 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10875 /* Look for the `<'. */
10876 cp_parser_require (parser, CPP_LESS, "%<<%>");
10877 /* Look for the `>'. */
10878 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10879 /* We have processed another parameter list. */
10880 ++parser->num_template_parameter_lists;
10883 A template ... explicit specialization ... shall not have C
10885 if (current_lang_name == lang_name_c)
10887 error ("%Htemplate specialization with C linkage", &token->location);
10888 /* Give it C++ linkage to avoid confusing other parts of the
10890 push_lang_context (lang_name_cplusplus);
10891 need_lang_pop = true;
10894 need_lang_pop = false;
10895 /* Let the front end know that we are beginning a specialization. */
10896 if (!begin_specialization ())
10898 end_specialization ();
10902 /* If the next keyword is `template', we need to figure out whether
10903 or not we're looking a template-declaration. */
10904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10906 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10907 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10908 cp_parser_template_declaration_after_export (parser,
10909 /*member_p=*/false);
10911 cp_parser_explicit_specialization (parser);
10914 /* Parse the dependent declaration. */
10915 cp_parser_single_declaration (parser,
10917 /*member_p=*/false,
10918 /*explicit_specialization_p=*/true,
10919 /*friend_p=*/NULL);
10920 /* We're done with the specialization. */
10921 end_specialization ();
10922 /* For the erroneous case of a template with C linkage, we pushed an
10923 implicit C++ linkage scope; exit that scope now. */
10925 pop_lang_context ();
10926 /* We're done with this parameter list. */
10927 --parser->num_template_parameter_lists;
10930 /* Parse a type-specifier.
10933 simple-type-specifier
10936 elaborated-type-specifier
10944 Returns a representation of the type-specifier. For a
10945 class-specifier, enum-specifier, or elaborated-type-specifier, a
10946 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10948 The parser flags FLAGS is used to control type-specifier parsing.
10950 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10951 in a decl-specifier-seq.
10953 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10954 class-specifier, enum-specifier, or elaborated-type-specifier, then
10955 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10956 if a type is declared; 2 if it is defined. Otherwise, it is set to
10959 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10960 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10961 is set to FALSE. */
10964 cp_parser_type_specifier (cp_parser* parser,
10965 cp_parser_flags flags,
10966 cp_decl_specifier_seq *decl_specs,
10967 bool is_declaration,
10968 int* declares_class_or_enum,
10969 bool* is_cv_qualifier)
10971 tree type_spec = NULL_TREE;
10974 cp_decl_spec ds = ds_last;
10976 /* Assume this type-specifier does not declare a new type. */
10977 if (declares_class_or_enum)
10978 *declares_class_or_enum = 0;
10979 /* And that it does not specify a cv-qualifier. */
10980 if (is_cv_qualifier)
10981 *is_cv_qualifier = false;
10982 /* Peek at the next token. */
10983 token = cp_lexer_peek_token (parser->lexer);
10985 /* If we're looking at a keyword, we can use that to guide the
10986 production we choose. */
10987 keyword = token->keyword;
10991 /* Look for the enum-specifier. */
10992 type_spec = cp_parser_enum_specifier (parser);
10993 /* If that worked, we're done. */
10996 if (declares_class_or_enum)
10997 *declares_class_or_enum = 2;
10999 cp_parser_set_decl_spec_type (decl_specs,
11002 /*user_defined_p=*/true);
11006 goto elaborated_type_specifier;
11008 /* Any of these indicate either a class-specifier, or an
11009 elaborated-type-specifier. */
11013 /* Parse tentatively so that we can back up if we don't find a
11014 class-specifier. */
11015 cp_parser_parse_tentatively (parser);
11016 /* Look for the class-specifier. */
11017 type_spec = cp_parser_class_specifier (parser);
11018 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11019 /* If that worked, we're done. */
11020 if (cp_parser_parse_definitely (parser))
11022 if (declares_class_or_enum)
11023 *declares_class_or_enum = 2;
11025 cp_parser_set_decl_spec_type (decl_specs,
11028 /*user_defined_p=*/true);
11032 /* Fall through. */
11033 elaborated_type_specifier:
11034 /* We're declaring (not defining) a class or enum. */
11035 if (declares_class_or_enum)
11036 *declares_class_or_enum = 1;
11038 /* Fall through. */
11040 /* Look for an elaborated-type-specifier. */
11042 = (cp_parser_elaborated_type_specifier
11044 decl_specs && decl_specs->specs[(int) ds_friend],
11047 cp_parser_set_decl_spec_type (decl_specs,
11050 /*user_defined_p=*/true);
11055 if (is_cv_qualifier)
11056 *is_cv_qualifier = true;
11061 if (is_cv_qualifier)
11062 *is_cv_qualifier = true;
11067 if (is_cv_qualifier)
11068 *is_cv_qualifier = true;
11072 /* The `__complex__' keyword is a GNU extension. */
11080 /* Handle simple keywords. */
11085 ++decl_specs->specs[(int)ds];
11086 decl_specs->any_specifiers_p = true;
11088 return cp_lexer_consume_token (parser->lexer)->u.value;
11091 /* If we do not already have a type-specifier, assume we are looking
11092 at a simple-type-specifier. */
11093 type_spec = cp_parser_simple_type_specifier (parser,
11097 /* If we didn't find a type-specifier, and a type-specifier was not
11098 optional in this context, issue an error message. */
11099 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11101 cp_parser_error (parser, "expected type specifier");
11102 return error_mark_node;
11108 /* Parse a simple-type-specifier.
11110 simple-type-specifier:
11111 :: [opt] nested-name-specifier [opt] type-name
11112 :: [opt] nested-name-specifier template template-id
11127 simple-type-specifier:
11129 decltype ( expression )
11135 simple-type-specifier:
11136 __typeof__ unary-expression
11137 __typeof__ ( type-id )
11139 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11140 appropriately updated. */
11143 cp_parser_simple_type_specifier (cp_parser* parser,
11144 cp_decl_specifier_seq *decl_specs,
11145 cp_parser_flags flags)
11147 tree type = NULL_TREE;
11150 /* Peek at the next token. */
11151 token = cp_lexer_peek_token (parser->lexer);
11153 /* If we're looking at a keyword, things are easy. */
11154 switch (token->keyword)
11158 decl_specs->explicit_char_p = true;
11159 type = char_type_node;
11162 type = char16_type_node;
11165 type = char32_type_node;
11168 type = wchar_type_node;
11171 type = boolean_type_node;
11175 ++decl_specs->specs[(int) ds_short];
11176 type = short_integer_type_node;
11180 decl_specs->explicit_int_p = true;
11181 type = integer_type_node;
11185 ++decl_specs->specs[(int) ds_long];
11186 type = long_integer_type_node;
11190 ++decl_specs->specs[(int) ds_signed];
11191 type = integer_type_node;
11195 ++decl_specs->specs[(int) ds_unsigned];
11196 type = unsigned_type_node;
11199 type = float_type_node;
11202 type = double_type_node;
11205 type = void_type_node;
11209 maybe_warn_cpp0x ("C++0x auto");
11210 type = make_auto ();
11214 /* Parse the `decltype' type. */
11215 type = cp_parser_decltype (parser);
11218 cp_parser_set_decl_spec_type (decl_specs, type,
11220 /*user_defined_p=*/true);
11225 /* Consume the `typeof' token. */
11226 cp_lexer_consume_token (parser->lexer);
11227 /* Parse the operand to `typeof'. */
11228 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11229 /* If it is not already a TYPE, take its type. */
11230 if (!TYPE_P (type))
11231 type = finish_typeof (type);
11234 cp_parser_set_decl_spec_type (decl_specs, type,
11236 /*user_defined_p=*/true);
11244 /* If the type-specifier was for a built-in type, we're done. */
11249 /* Record the type. */
11251 && (token->keyword != RID_SIGNED
11252 && token->keyword != RID_UNSIGNED
11253 && token->keyword != RID_SHORT
11254 && token->keyword != RID_LONG))
11255 cp_parser_set_decl_spec_type (decl_specs,
11258 /*user_defined=*/false);
11260 decl_specs->any_specifiers_p = true;
11262 /* Consume the token. */
11263 id = cp_lexer_consume_token (parser->lexer)->u.value;
11265 /* There is no valid C++ program where a non-template type is
11266 followed by a "<". That usually indicates that the user thought
11267 that the type was a template. */
11268 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11270 return TYPE_NAME (type);
11273 /* The type-specifier must be a user-defined type. */
11274 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11279 /* Don't gobble tokens or issue error messages if this is an
11280 optional type-specifier. */
11281 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11282 cp_parser_parse_tentatively (parser);
11284 /* Look for the optional `::' operator. */
11286 = (cp_parser_global_scope_opt (parser,
11287 /*current_scope_valid_p=*/false)
11289 /* Look for the nested-name specifier. */
11291 = (cp_parser_nested_name_specifier_opt (parser,
11292 /*typename_keyword_p=*/false,
11293 /*check_dependency_p=*/true,
11295 /*is_declaration=*/false)
11297 token = cp_lexer_peek_token (parser->lexer);
11298 /* If we have seen a nested-name-specifier, and the next token
11299 is `template', then we are using the template-id production. */
11301 && cp_parser_optional_template_keyword (parser))
11303 /* Look for the template-id. */
11304 type = cp_parser_template_id (parser,
11305 /*template_keyword_p=*/true,
11306 /*check_dependency_p=*/true,
11307 /*is_declaration=*/false);
11308 /* If the template-id did not name a type, we are out of
11310 if (TREE_CODE (type) != TYPE_DECL)
11312 cp_parser_error (parser, "expected template-id for type");
11316 /* Otherwise, look for a type-name. */
11318 type = cp_parser_type_name (parser);
11319 /* Keep track of all name-lookups performed in class scopes. */
11323 && TREE_CODE (type) == TYPE_DECL
11324 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11325 maybe_note_name_used_in_class (DECL_NAME (type), type);
11326 /* If it didn't work out, we don't have a TYPE. */
11327 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11328 && !cp_parser_parse_definitely (parser))
11330 if (type && decl_specs)
11331 cp_parser_set_decl_spec_type (decl_specs, type,
11333 /*user_defined=*/true);
11336 /* If we didn't get a type-name, issue an error message. */
11337 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11339 cp_parser_error (parser, "expected type-name");
11340 return error_mark_node;
11343 /* There is no valid C++ program where a non-template type is
11344 followed by a "<". That usually indicates that the user thought
11345 that the type was a template. */
11346 if (type && type != error_mark_node)
11348 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11349 If it is, then the '<'...'>' enclose protocol names rather than
11350 template arguments, and so everything is fine. */
11351 if (c_dialect_objc ()
11352 && (objc_is_id (type) || objc_is_class_name (type)))
11354 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11355 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11357 /* Clobber the "unqualified" type previously entered into
11358 DECL_SPECS with the new, improved protocol-qualified version. */
11360 decl_specs->type = qual_type;
11365 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11372 /* Parse a type-name.
11385 Returns a TYPE_DECL for the type. */
11388 cp_parser_type_name (cp_parser* parser)
11392 /* We can't know yet whether it is a class-name or not. */
11393 cp_parser_parse_tentatively (parser);
11394 /* Try a class-name. */
11395 type_decl = cp_parser_class_name (parser,
11396 /*typename_keyword_p=*/false,
11397 /*template_keyword_p=*/false,
11399 /*check_dependency_p=*/true,
11400 /*class_head_p=*/false,
11401 /*is_declaration=*/false);
11402 /* If it's not a class-name, keep looking. */
11403 if (!cp_parser_parse_definitely (parser))
11405 /* It must be a typedef-name or an enum-name. */
11406 return cp_parser_nonclass_name (parser);
11412 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11420 Returns a TYPE_DECL for the type. */
11423 cp_parser_nonclass_name (cp_parser* parser)
11428 cp_token *token = cp_lexer_peek_token (parser->lexer);
11429 identifier = cp_parser_identifier (parser);
11430 if (identifier == error_mark_node)
11431 return error_mark_node;
11433 /* Look up the type-name. */
11434 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11436 if (TREE_CODE (type_decl) != TYPE_DECL
11437 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11439 /* See if this is an Objective-C type. */
11440 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11441 tree type = objc_get_protocol_qualified_type (identifier, protos);
11443 type_decl = TYPE_NAME (type);
11446 /* Issue an error if we did not find a type-name. */
11447 if (TREE_CODE (type_decl) != TYPE_DECL)
11449 if (!cp_parser_simulate_error (parser))
11450 cp_parser_name_lookup_error (parser, identifier, type_decl,
11451 "is not a type", token->location);
11452 return error_mark_node;
11454 /* Remember that the name was used in the definition of the
11455 current class so that we can check later to see if the
11456 meaning would have been different after the class was
11457 entirely defined. */
11458 else if (type_decl != error_mark_node
11460 maybe_note_name_used_in_class (identifier, type_decl);
11465 /* Parse an elaborated-type-specifier. Note that the grammar given
11466 here incorporates the resolution to DR68.
11468 elaborated-type-specifier:
11469 class-key :: [opt] nested-name-specifier [opt] identifier
11470 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11471 enum-key :: [opt] nested-name-specifier [opt] identifier
11472 typename :: [opt] nested-name-specifier identifier
11473 typename :: [opt] nested-name-specifier template [opt]
11478 elaborated-type-specifier:
11479 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11480 class-key attributes :: [opt] nested-name-specifier [opt]
11481 template [opt] template-id
11482 enum attributes :: [opt] nested-name-specifier [opt] identifier
11484 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11485 declared `friend'. If IS_DECLARATION is TRUE, then this
11486 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11487 something is being declared.
11489 Returns the TYPE specified. */
11492 cp_parser_elaborated_type_specifier (cp_parser* parser,
11494 bool is_declaration)
11496 enum tag_types tag_type;
11498 tree type = NULL_TREE;
11499 tree attributes = NULL_TREE;
11500 cp_token *token = NULL;
11502 /* See if we're looking at the `enum' keyword. */
11503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11505 /* Consume the `enum' token. */
11506 cp_lexer_consume_token (parser->lexer);
11507 /* Remember that it's an enumeration type. */
11508 tag_type = enum_type;
11509 /* Parse the optional `struct' or `class' key (for C++0x scoped
11511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11512 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11514 if (cxx_dialect == cxx98)
11515 maybe_warn_cpp0x ("scoped enums");
11517 /* Consume the `struct' or `class'. */
11518 cp_lexer_consume_token (parser->lexer);
11520 /* Parse the attributes. */
11521 attributes = cp_parser_attributes_opt (parser);
11523 /* Or, it might be `typename'. */
11524 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11527 /* Consume the `typename' token. */
11528 cp_lexer_consume_token (parser->lexer);
11529 /* Remember that it's a `typename' type. */
11530 tag_type = typename_type;
11531 /* The `typename' keyword is only allowed in templates. */
11532 if (!processing_template_decl)
11533 permerror (input_location, "using %<typename%> outside of template");
11535 /* Otherwise it must be a class-key. */
11538 tag_type = cp_parser_class_key (parser);
11539 if (tag_type == none_type)
11540 return error_mark_node;
11541 /* Parse the attributes. */
11542 attributes = cp_parser_attributes_opt (parser);
11545 /* Look for the `::' operator. */
11546 cp_parser_global_scope_opt (parser,
11547 /*current_scope_valid_p=*/false);
11548 /* Look for the nested-name-specifier. */
11549 if (tag_type == typename_type)
11551 if (!cp_parser_nested_name_specifier (parser,
11552 /*typename_keyword_p=*/true,
11553 /*check_dependency_p=*/true,
11556 return error_mark_node;
11559 /* Even though `typename' is not present, the proposed resolution
11560 to Core Issue 180 says that in `class A<T>::B', `B' should be
11561 considered a type-name, even if `A<T>' is dependent. */
11562 cp_parser_nested_name_specifier_opt (parser,
11563 /*typename_keyword_p=*/true,
11564 /*check_dependency_p=*/true,
11567 /* For everything but enumeration types, consider a template-id.
11568 For an enumeration type, consider only a plain identifier. */
11569 if (tag_type != enum_type)
11571 bool template_p = false;
11574 /* Allow the `template' keyword. */
11575 template_p = cp_parser_optional_template_keyword (parser);
11576 /* If we didn't see `template', we don't know if there's a
11577 template-id or not. */
11579 cp_parser_parse_tentatively (parser);
11580 /* Parse the template-id. */
11581 token = cp_lexer_peek_token (parser->lexer);
11582 decl = cp_parser_template_id (parser, template_p,
11583 /*check_dependency_p=*/true,
11585 /* If we didn't find a template-id, look for an ordinary
11587 if (!template_p && !cp_parser_parse_definitely (parser))
11589 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11590 in effect, then we must assume that, upon instantiation, the
11591 template will correspond to a class. */
11592 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11593 && tag_type == typename_type)
11594 type = make_typename_type (parser->scope, decl,
11596 /*complain=*/tf_error);
11597 /* If the `typename' keyword is in effect and DECL is not a type
11598 decl. Then type is non existant. */
11599 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11602 type = TREE_TYPE (decl);
11607 token = cp_lexer_peek_token (parser->lexer);
11608 identifier = cp_parser_identifier (parser);
11610 if (identifier == error_mark_node)
11612 parser->scope = NULL_TREE;
11613 return error_mark_node;
11616 /* For a `typename', we needn't call xref_tag. */
11617 if (tag_type == typename_type
11618 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11619 return cp_parser_make_typename_type (parser, parser->scope,
11622 /* Look up a qualified name in the usual way. */
11626 tree ambiguous_decls;
11628 decl = cp_parser_lookup_name (parser, identifier,
11630 /*is_template=*/false,
11631 /*is_namespace=*/false,
11632 /*check_dependency=*/true,
11636 /* If the lookup was ambiguous, an error will already have been
11638 if (ambiguous_decls)
11639 return error_mark_node;
11641 /* If we are parsing friend declaration, DECL may be a
11642 TEMPLATE_DECL tree node here. However, we need to check
11643 whether this TEMPLATE_DECL results in valid code. Consider
11644 the following example:
11647 template <class T> class C {};
11650 template <class T> friend class N::C; // #1, valid code
11652 template <class T> class Y {
11653 friend class N::C; // #2, invalid code
11656 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11657 name lookup of `N::C'. We see that friend declaration must
11658 be template for the code to be valid. Note that
11659 processing_template_decl does not work here since it is
11660 always 1 for the above two cases. */
11662 decl = (cp_parser_maybe_treat_template_as_class
11663 (decl, /*tag_name_p=*/is_friend
11664 && parser->num_template_parameter_lists));
11666 if (TREE_CODE (decl) != TYPE_DECL)
11668 cp_parser_diagnose_invalid_type_name (parser,
11672 return error_mark_node;
11675 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11677 bool allow_template = (parser->num_template_parameter_lists
11678 || DECL_SELF_REFERENCE_P (decl));
11679 type = check_elaborated_type_specifier (tag_type, decl,
11682 if (type == error_mark_node)
11683 return error_mark_node;
11686 /* Forward declarations of nested types, such as
11691 are invalid unless all components preceding the final '::'
11692 are complete. If all enclosing types are complete, these
11693 declarations become merely pointless.
11695 Invalid forward declarations of nested types are errors
11696 caught elsewhere in parsing. Those that are pointless arrive
11699 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11700 && !is_friend && !processing_explicit_instantiation)
11701 warning (0, "declaration %qD does not declare anything", decl);
11703 type = TREE_TYPE (decl);
11707 /* An elaborated-type-specifier sometimes introduces a new type and
11708 sometimes names an existing type. Normally, the rule is that it
11709 introduces a new type only if there is not an existing type of
11710 the same name already in scope. For example, given:
11713 void f() { struct S s; }
11715 the `struct S' in the body of `f' is the same `struct S' as in
11716 the global scope; the existing definition is used. However, if
11717 there were no global declaration, this would introduce a new
11718 local class named `S'.
11720 An exception to this rule applies to the following code:
11722 namespace N { struct S; }
11724 Here, the elaborated-type-specifier names a new type
11725 unconditionally; even if there is already an `S' in the
11726 containing scope this declaration names a new type.
11727 This exception only applies if the elaborated-type-specifier
11728 forms the complete declaration:
11732 A declaration consisting solely of `class-key identifier ;' is
11733 either a redeclaration of the name in the current scope or a
11734 forward declaration of the identifier as a class name. It
11735 introduces the name into the current scope.
11737 We are in this situation precisely when the next token is a `;'.
11739 An exception to the exception is that a `friend' declaration does
11740 *not* name a new type; i.e., given:
11742 struct S { friend struct T; };
11744 `T' is not a new type in the scope of `S'.
11746 Also, `new struct S' or `sizeof (struct S)' never results in the
11747 definition of a new type; a new type can only be declared in a
11748 declaration context. */
11754 /* Friends have special name lookup rules. */
11755 ts = ts_within_enclosing_non_class;
11756 else if (is_declaration
11757 && cp_lexer_next_token_is (parser->lexer,
11759 /* This is a `class-key identifier ;' */
11765 (parser->num_template_parameter_lists
11766 && (cp_parser_next_token_starts_class_definition_p (parser)
11767 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11768 /* An unqualified name was used to reference this type, so
11769 there were no qualifying templates. */
11770 if (!cp_parser_check_template_parameters (parser,
11771 /*num_templates=*/0,
11773 /*declarator=*/NULL))
11774 return error_mark_node;
11775 type = xref_tag (tag_type, identifier, ts, template_p);
11779 if (type == error_mark_node)
11780 return error_mark_node;
11782 /* Allow attributes on forward declarations of classes. */
11785 if (TREE_CODE (type) == TYPENAME_TYPE)
11786 warning (OPT_Wattributes,
11787 "attributes ignored on uninstantiated type");
11788 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11789 && ! processing_explicit_instantiation)
11790 warning (OPT_Wattributes,
11791 "attributes ignored on template instantiation");
11792 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11793 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11795 warning (OPT_Wattributes,
11796 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11799 if (tag_type != enum_type)
11800 cp_parser_check_class_key (tag_type, type);
11802 /* A "<" cannot follow an elaborated type specifier. If that
11803 happens, the user was probably trying to form a template-id. */
11804 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11809 /* Parse an enum-specifier.
11812 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11817 enum struct [C++0x]
11820 : type-specifier-seq
11823 enum-key attributes[opt] identifier [opt] enum-base [opt]
11824 { enumerator-list [opt] }attributes[opt]
11826 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11827 if the token stream isn't an enum-specifier after all. */
11830 cp_parser_enum_specifier (cp_parser* parser)
11835 bool scoped_enum_p = false;
11836 bool has_underlying_type = false;
11837 tree underlying_type = NULL_TREE;
11839 /* Parse tentatively so that we can back up if we don't find a
11841 cp_parser_parse_tentatively (parser);
11843 /* Caller guarantees that the current token is 'enum', an identifier
11844 possibly follows, and the token after that is an opening brace.
11845 If we don't have an identifier, fabricate an anonymous name for
11846 the enumeration being defined. */
11847 cp_lexer_consume_token (parser->lexer);
11849 /* Parse the "class" or "struct", which indicates a scoped
11850 enumeration type in C++0x. */
11851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11852 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11854 if (cxx_dialect == cxx98)
11855 maybe_warn_cpp0x ("scoped enums");
11857 /* Consume the `struct' or `class' token. */
11858 cp_lexer_consume_token (parser->lexer);
11860 scoped_enum_p = true;
11863 attributes = cp_parser_attributes_opt (parser);
11865 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11866 identifier = cp_parser_identifier (parser);
11868 identifier = make_anon_name ();
11870 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11871 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11873 cp_decl_specifier_seq type_specifiers;
11875 /* At this point this is surely not elaborated type specifier. */
11876 if (!cp_parser_parse_definitely (parser))
11879 if (cxx_dialect == cxx98)
11880 maybe_warn_cpp0x ("scoped enums");
11882 /* Consume the `:'. */
11883 cp_lexer_consume_token (parser->lexer);
11885 has_underlying_type = true;
11887 /* Parse the type-specifier-seq. */
11888 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11891 /* If that didn't work, stop. */
11892 if (type_specifiers.type != error_mark_node)
11894 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11895 /*initialized=*/0, NULL);
11896 if (underlying_type == error_mark_node)
11897 underlying_type = NULL_TREE;
11901 /* Look for the `{' but don't consume it yet. */
11902 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11904 cp_parser_error (parser, "expected %<{%>");
11905 if (has_underlying_type)
11909 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11912 /* Issue an error message if type-definitions are forbidden here. */
11913 if (!cp_parser_check_type_definition (parser))
11914 type = error_mark_node;
11916 /* Create the new type. We do this before consuming the opening
11917 brace so the enum will be recorded as being on the line of its
11918 tag (or the 'enum' keyword, if there is no tag). */
11919 type = start_enum (identifier, underlying_type, scoped_enum_p);
11921 /* Consume the opening brace. */
11922 cp_lexer_consume_token (parser->lexer);
11924 if (type == error_mark_node)
11926 cp_parser_skip_to_end_of_block_or_statement (parser);
11927 return error_mark_node;
11930 /* If the next token is not '}', then there are some enumerators. */
11931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11932 cp_parser_enumerator_list (parser, type);
11934 /* Consume the final '}'. */
11935 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11937 /* Look for trailing attributes to apply to this enumeration, and
11938 apply them if appropriate. */
11939 if (cp_parser_allow_gnu_extensions_p (parser))
11941 tree trailing_attr = cp_parser_attributes_opt (parser);
11942 trailing_attr = chainon (trailing_attr, attributes);
11943 cplus_decl_attributes (&type,
11945 (int) ATTR_FLAG_TYPE_IN_PLACE);
11948 /* Finish up the enumeration. */
11949 finish_enum (type);
11954 /* Parse an enumerator-list. The enumerators all have the indicated
11958 enumerator-definition
11959 enumerator-list , enumerator-definition */
11962 cp_parser_enumerator_list (cp_parser* parser, tree type)
11966 /* Parse an enumerator-definition. */
11967 cp_parser_enumerator_definition (parser, type);
11969 /* If the next token is not a ',', we've reached the end of
11971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11973 /* Otherwise, consume the `,' and keep going. */
11974 cp_lexer_consume_token (parser->lexer);
11975 /* If the next token is a `}', there is a trailing comma. */
11976 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11978 if (!in_system_header)
11979 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11985 /* Parse an enumerator-definition. The enumerator has the indicated
11988 enumerator-definition:
11990 enumerator = constant-expression
11996 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12001 /* Look for the identifier. */
12002 identifier = cp_parser_identifier (parser);
12003 if (identifier == error_mark_node)
12006 /* If the next token is an '=', then there is an explicit value. */
12007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12009 /* Consume the `=' token. */
12010 cp_lexer_consume_token (parser->lexer);
12011 /* Parse the value. */
12012 value = cp_parser_constant_expression (parser,
12013 /*allow_non_constant_p=*/false,
12019 /* If we are processing a template, make sure the initializer of the
12020 enumerator doesn't contain any bare template parameter pack. */
12021 if (check_for_bare_parameter_packs (value))
12022 value = error_mark_node;
12024 /* Create the enumerator. */
12025 build_enumerator (identifier, value, type);
12028 /* Parse a namespace-name.
12031 original-namespace-name
12034 Returns the NAMESPACE_DECL for the namespace. */
12037 cp_parser_namespace_name (cp_parser* parser)
12040 tree namespace_decl;
12042 cp_token *token = cp_lexer_peek_token (parser->lexer);
12044 /* Get the name of the namespace. */
12045 identifier = cp_parser_identifier (parser);
12046 if (identifier == error_mark_node)
12047 return error_mark_node;
12049 /* Look up the identifier in the currently active scope. Look only
12050 for namespaces, due to:
12052 [basic.lookup.udir]
12054 When looking up a namespace-name in a using-directive or alias
12055 definition, only namespace names are considered.
12059 [basic.lookup.qual]
12061 During the lookup of a name preceding the :: scope resolution
12062 operator, object, function, and enumerator names are ignored.
12064 (Note that cp_parser_qualifying_entity only calls this
12065 function if the token after the name is the scope resolution
12067 namespace_decl = cp_parser_lookup_name (parser, identifier,
12069 /*is_template=*/false,
12070 /*is_namespace=*/true,
12071 /*check_dependency=*/true,
12072 /*ambiguous_decls=*/NULL,
12074 /* If it's not a namespace, issue an error. */
12075 if (namespace_decl == error_mark_node
12076 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12078 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12079 error ("%H%qD is not a namespace-name", &token->location, identifier);
12080 cp_parser_error (parser, "expected namespace-name");
12081 namespace_decl = error_mark_node;
12084 return namespace_decl;
12087 /* Parse a namespace-definition.
12089 namespace-definition:
12090 named-namespace-definition
12091 unnamed-namespace-definition
12093 named-namespace-definition:
12094 original-namespace-definition
12095 extension-namespace-definition
12097 original-namespace-definition:
12098 namespace identifier { namespace-body }
12100 extension-namespace-definition:
12101 namespace original-namespace-name { namespace-body }
12103 unnamed-namespace-definition:
12104 namespace { namespace-body } */
12107 cp_parser_namespace_definition (cp_parser* parser)
12109 tree identifier, attribs;
12110 bool has_visibility;
12113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12116 cp_lexer_consume_token (parser->lexer);
12121 /* Look for the `namespace' keyword. */
12122 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12124 /* Get the name of the namespace. We do not attempt to distinguish
12125 between an original-namespace-definition and an
12126 extension-namespace-definition at this point. The semantic
12127 analysis routines are responsible for that. */
12128 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12129 identifier = cp_parser_identifier (parser);
12131 identifier = NULL_TREE;
12133 /* Parse any specified attributes. */
12134 attribs = cp_parser_attributes_opt (parser);
12136 /* Look for the `{' to start the namespace. */
12137 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12138 /* Start the namespace. */
12139 push_namespace (identifier);
12141 /* "inline namespace" is equivalent to a stub namespace definition
12142 followed by a strong using directive. */
12145 tree name_space = current_namespace;
12146 /* Set up namespace association. */
12147 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12148 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12149 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12150 /* Import the contents of the inline namespace. */
12152 do_using_directive (name_space);
12153 push_namespace (identifier);
12156 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12158 /* Parse the body of the namespace. */
12159 cp_parser_namespace_body (parser);
12161 #ifdef HANDLE_PRAGMA_VISIBILITY
12162 if (has_visibility)
12166 /* Finish the namespace. */
12168 /* Look for the final `}'. */
12169 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12172 /* Parse a namespace-body.
12175 declaration-seq [opt] */
12178 cp_parser_namespace_body (cp_parser* parser)
12180 cp_parser_declaration_seq_opt (parser);
12183 /* Parse a namespace-alias-definition.
12185 namespace-alias-definition:
12186 namespace identifier = qualified-namespace-specifier ; */
12189 cp_parser_namespace_alias_definition (cp_parser* parser)
12192 tree namespace_specifier;
12194 cp_token *token = cp_lexer_peek_token (parser->lexer);
12196 /* Look for the `namespace' keyword. */
12197 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12198 /* Look for the identifier. */
12199 identifier = cp_parser_identifier (parser);
12200 if (identifier == error_mark_node)
12202 /* Look for the `=' token. */
12203 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12204 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12206 error ("%H%<namespace%> definition is not allowed here", &token->location);
12207 /* Skip the definition. */
12208 cp_lexer_consume_token (parser->lexer);
12209 if (cp_parser_skip_to_closing_brace (parser))
12210 cp_lexer_consume_token (parser->lexer);
12213 cp_parser_require (parser, CPP_EQ, "%<=%>");
12214 /* Look for the qualified-namespace-specifier. */
12215 namespace_specifier
12216 = cp_parser_qualified_namespace_specifier (parser);
12217 /* Look for the `;' token. */
12218 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12220 /* Register the alias in the symbol table. */
12221 do_namespace_alias (identifier, namespace_specifier);
12224 /* Parse a qualified-namespace-specifier.
12226 qualified-namespace-specifier:
12227 :: [opt] nested-name-specifier [opt] namespace-name
12229 Returns a NAMESPACE_DECL corresponding to the specified
12233 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12235 /* Look for the optional `::'. */
12236 cp_parser_global_scope_opt (parser,
12237 /*current_scope_valid_p=*/false);
12239 /* Look for the optional nested-name-specifier. */
12240 cp_parser_nested_name_specifier_opt (parser,
12241 /*typename_keyword_p=*/false,
12242 /*check_dependency_p=*/true,
12244 /*is_declaration=*/true);
12246 return cp_parser_namespace_name (parser);
12249 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12250 access declaration.
12253 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12254 using :: unqualified-id ;
12256 access-declaration:
12262 cp_parser_using_declaration (cp_parser* parser,
12263 bool access_declaration_p)
12266 bool typename_p = false;
12267 bool global_scope_p;
12272 if (access_declaration_p)
12273 cp_parser_parse_tentatively (parser);
12276 /* Look for the `using' keyword. */
12277 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12279 /* Peek at the next token. */
12280 token = cp_lexer_peek_token (parser->lexer);
12281 /* See if it's `typename'. */
12282 if (token->keyword == RID_TYPENAME)
12284 /* Remember that we've seen it. */
12286 /* Consume the `typename' token. */
12287 cp_lexer_consume_token (parser->lexer);
12291 /* Look for the optional global scope qualification. */
12293 = (cp_parser_global_scope_opt (parser,
12294 /*current_scope_valid_p=*/false)
12297 /* If we saw `typename', or didn't see `::', then there must be a
12298 nested-name-specifier present. */
12299 if (typename_p || !global_scope_p)
12300 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12301 /*check_dependency_p=*/true,
12303 /*is_declaration=*/true);
12304 /* Otherwise, we could be in either of the two productions. In that
12305 case, treat the nested-name-specifier as optional. */
12307 qscope = cp_parser_nested_name_specifier_opt (parser,
12308 /*typename_keyword_p=*/false,
12309 /*check_dependency_p=*/true,
12311 /*is_declaration=*/true);
12313 qscope = global_namespace;
12315 if (access_declaration_p && cp_parser_error_occurred (parser))
12316 /* Something has already gone wrong; there's no need to parse
12317 further. Since an error has occurred, the return value of
12318 cp_parser_parse_definitely will be false, as required. */
12319 return cp_parser_parse_definitely (parser);
12321 token = cp_lexer_peek_token (parser->lexer);
12322 /* Parse the unqualified-id. */
12323 identifier = cp_parser_unqualified_id (parser,
12324 /*template_keyword_p=*/false,
12325 /*check_dependency_p=*/true,
12326 /*declarator_p=*/true,
12327 /*optional_p=*/false);
12329 if (access_declaration_p)
12331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12332 cp_parser_simulate_error (parser);
12333 if (!cp_parser_parse_definitely (parser))
12337 /* The function we call to handle a using-declaration is different
12338 depending on what scope we are in. */
12339 if (qscope == error_mark_node || identifier == error_mark_node)
12341 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12342 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12343 /* [namespace.udecl]
12345 A using declaration shall not name a template-id. */
12346 error ("%Ha template-id may not appear in a using-declaration",
12350 if (at_class_scope_p ())
12352 /* Create the USING_DECL. */
12353 decl = do_class_using_decl (parser->scope, identifier);
12355 if (check_for_bare_parameter_packs (decl))
12358 /* Add it to the list of members in this class. */
12359 finish_member_declaration (decl);
12363 decl = cp_parser_lookup_name_simple (parser,
12366 if (decl == error_mark_node)
12367 cp_parser_name_lookup_error (parser, identifier,
12370 else if (check_for_bare_parameter_packs (decl))
12372 else if (!at_namespace_scope_p ())
12373 do_local_using_decl (decl, qscope, identifier);
12375 do_toplevel_using_decl (decl, qscope, identifier);
12379 /* Look for the final `;'. */
12380 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12385 /* Parse a using-directive.
12388 using namespace :: [opt] nested-name-specifier [opt]
12389 namespace-name ; */
12392 cp_parser_using_directive (cp_parser* parser)
12394 tree namespace_decl;
12397 /* Look for the `using' keyword. */
12398 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12399 /* And the `namespace' keyword. */
12400 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12401 /* Look for the optional `::' operator. */
12402 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12403 /* And the optional nested-name-specifier. */
12404 cp_parser_nested_name_specifier_opt (parser,
12405 /*typename_keyword_p=*/false,
12406 /*check_dependency_p=*/true,
12408 /*is_declaration=*/true);
12409 /* Get the namespace being used. */
12410 namespace_decl = cp_parser_namespace_name (parser);
12411 /* And any specified attributes. */
12412 attribs = cp_parser_attributes_opt (parser);
12413 /* Update the symbol table. */
12414 parse_using_directive (namespace_decl, attribs);
12415 /* Look for the final `;'. */
12416 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12419 /* Parse an asm-definition.
12422 asm ( string-literal ) ;
12427 asm volatile [opt] ( string-literal ) ;
12428 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12429 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12430 : asm-operand-list [opt] ) ;
12431 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12432 : asm-operand-list [opt]
12433 : asm-operand-list [opt] ) ; */
12436 cp_parser_asm_definition (cp_parser* parser)
12439 tree outputs = NULL_TREE;
12440 tree inputs = NULL_TREE;
12441 tree clobbers = NULL_TREE;
12443 bool volatile_p = false;
12444 bool extended_p = false;
12445 bool invalid_inputs_p = false;
12446 bool invalid_outputs_p = false;
12448 /* Look for the `asm' keyword. */
12449 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12450 /* See if the next token is `volatile'. */
12451 if (cp_parser_allow_gnu_extensions_p (parser)
12452 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12454 /* Remember that we saw the `volatile' keyword. */
12456 /* Consume the token. */
12457 cp_lexer_consume_token (parser->lexer);
12459 /* Look for the opening `('. */
12460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12462 /* Look for the string. */
12463 string = cp_parser_string_literal (parser, false, false);
12464 if (string == error_mark_node)
12466 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12467 /*consume_paren=*/true);
12471 /* If we're allowing GNU extensions, check for the extended assembly
12472 syntax. Unfortunately, the `:' tokens need not be separated by
12473 a space in C, and so, for compatibility, we tolerate that here
12474 too. Doing that means that we have to treat the `::' operator as
12476 if (cp_parser_allow_gnu_extensions_p (parser)
12477 && parser->in_function_body
12478 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12479 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12481 bool inputs_p = false;
12482 bool clobbers_p = false;
12484 /* The extended syntax was used. */
12487 /* Look for outputs. */
12488 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12490 /* Consume the `:'. */
12491 cp_lexer_consume_token (parser->lexer);
12492 /* Parse the output-operands. */
12493 if (cp_lexer_next_token_is_not (parser->lexer,
12495 && cp_lexer_next_token_is_not (parser->lexer,
12497 && cp_lexer_next_token_is_not (parser->lexer,
12499 outputs = cp_parser_asm_operand_list (parser);
12501 if (outputs == error_mark_node)
12502 invalid_outputs_p = true;
12504 /* If the next token is `::', there are no outputs, and the
12505 next token is the beginning of the inputs. */
12506 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12507 /* The inputs are coming next. */
12510 /* Look for inputs. */
12512 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12514 /* Consume the `:' or `::'. */
12515 cp_lexer_consume_token (parser->lexer);
12516 /* Parse the output-operands. */
12517 if (cp_lexer_next_token_is_not (parser->lexer,
12519 && cp_lexer_next_token_is_not (parser->lexer,
12521 inputs = cp_parser_asm_operand_list (parser);
12523 if (inputs == error_mark_node)
12524 invalid_inputs_p = true;
12526 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12527 /* The clobbers are coming next. */
12530 /* Look for clobbers. */
12532 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12534 /* Consume the `:' or `::'. */
12535 cp_lexer_consume_token (parser->lexer);
12536 /* Parse the clobbers. */
12537 if (cp_lexer_next_token_is_not (parser->lexer,
12539 clobbers = cp_parser_asm_clobber_list (parser);
12542 /* Look for the closing `)'. */
12543 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12544 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12545 /*consume_paren=*/true);
12546 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12548 if (!invalid_inputs_p && !invalid_outputs_p)
12550 /* Create the ASM_EXPR. */
12551 if (parser->in_function_body)
12553 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12555 /* If the extended syntax was not used, mark the ASM_EXPR. */
12558 tree temp = asm_stmt;
12559 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12560 temp = TREE_OPERAND (temp, 0);
12562 ASM_INPUT_P (temp) = 1;
12566 cgraph_add_asm_node (string);
12570 /* Declarators [gram.dcl.decl] */
12572 /* Parse an init-declarator.
12575 declarator initializer [opt]
12580 declarator asm-specification [opt] attributes [opt] initializer [opt]
12582 function-definition:
12583 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12585 decl-specifier-seq [opt] declarator function-try-block
12589 function-definition:
12590 __extension__ function-definition
12592 The DECL_SPECIFIERS apply to this declarator. Returns a
12593 representation of the entity declared. If MEMBER_P is TRUE, then
12594 this declarator appears in a class scope. The new DECL created by
12595 this declarator is returned.
12597 The CHECKS are access checks that should be performed once we know
12598 what entity is being declared (and, therefore, what classes have
12601 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12602 for a function-definition here as well. If the declarator is a
12603 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12604 be TRUE upon return. By that point, the function-definition will
12605 have been completely parsed.
12607 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12611 cp_parser_init_declarator (cp_parser* parser,
12612 cp_decl_specifier_seq *decl_specifiers,
12613 VEC (deferred_access_check,gc)* checks,
12614 bool function_definition_allowed_p,
12616 int declares_class_or_enum,
12617 bool* function_definition_p)
12619 cp_token *token = NULL, *asm_spec_start_token = NULL,
12620 *attributes_start_token = NULL;
12621 cp_declarator *declarator;
12622 tree prefix_attributes;
12624 tree asm_specification;
12626 tree decl = NULL_TREE;
12628 int is_initialized;
12629 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12630 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12632 enum cpp_ttype initialization_kind;
12633 bool is_direct_init = false;
12634 bool is_non_constant_init;
12635 int ctor_dtor_or_conv_p;
12637 tree pushed_scope = NULL;
12639 /* Gather the attributes that were provided with the
12640 decl-specifiers. */
12641 prefix_attributes = decl_specifiers->attributes;
12643 /* Assume that this is not the declarator for a function
12645 if (function_definition_p)
12646 *function_definition_p = false;
12648 /* Defer access checks while parsing the declarator; we cannot know
12649 what names are accessible until we know what is being
12651 resume_deferring_access_checks ();
12653 /* Parse the declarator. */
12654 token = cp_lexer_peek_token (parser->lexer);
12656 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12657 &ctor_dtor_or_conv_p,
12658 /*parenthesized_p=*/NULL,
12659 /*member_p=*/false);
12660 /* Gather up the deferred checks. */
12661 stop_deferring_access_checks ();
12663 /* If the DECLARATOR was erroneous, there's no need to go
12665 if (declarator == cp_error_declarator)
12666 return error_mark_node;
12668 /* Check that the number of template-parameter-lists is OK. */
12669 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12671 return error_mark_node;
12673 if (declares_class_or_enum & 2)
12674 cp_parser_check_for_definition_in_return_type (declarator,
12675 decl_specifiers->type,
12676 decl_specifiers->type_location);
12678 /* Figure out what scope the entity declared by the DECLARATOR is
12679 located in. `grokdeclarator' sometimes changes the scope, so
12680 we compute it now. */
12681 scope = get_scope_of_declarator (declarator);
12683 /* If we're allowing GNU extensions, look for an asm-specification
12685 if (cp_parser_allow_gnu_extensions_p (parser))
12687 /* Look for an asm-specification. */
12688 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12689 asm_specification = cp_parser_asm_specification_opt (parser);
12690 /* And attributes. */
12691 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12692 attributes = cp_parser_attributes_opt (parser);
12696 asm_specification = NULL_TREE;
12697 attributes = NULL_TREE;
12700 /* Peek at the next token. */
12701 token = cp_lexer_peek_token (parser->lexer);
12702 /* Check to see if the token indicates the start of a
12703 function-definition. */
12704 if (function_declarator_p (declarator)
12705 && cp_parser_token_starts_function_definition_p (token))
12707 if (!function_definition_allowed_p)
12709 /* If a function-definition should not appear here, issue an
12711 cp_parser_error (parser,
12712 "a function-definition is not allowed here");
12713 return error_mark_node;
12717 location_t func_brace_location
12718 = cp_lexer_peek_token (parser->lexer)->location;
12720 /* Neither attributes nor an asm-specification are allowed
12721 on a function-definition. */
12722 if (asm_specification)
12723 error ("%Han asm-specification is not allowed "
12724 "on a function-definition",
12725 &asm_spec_start_token->location);
12727 error ("%Hattributes are not allowed on a function-definition",
12728 &attributes_start_token->location);
12729 /* This is a function-definition. */
12730 *function_definition_p = true;
12732 /* Parse the function definition. */
12734 decl = cp_parser_save_member_function_body (parser,
12737 prefix_attributes);
12740 = (cp_parser_function_definition_from_specifiers_and_declarator
12741 (parser, decl_specifiers, prefix_attributes, declarator));
12743 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12745 /* This is where the prologue starts... */
12746 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12747 = func_brace_location;
12756 Only in function declarations for constructors, destructors, and
12757 type conversions can the decl-specifier-seq be omitted.
12759 We explicitly postpone this check past the point where we handle
12760 function-definitions because we tolerate function-definitions
12761 that are missing their return types in some modes. */
12762 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12764 cp_parser_error (parser,
12765 "expected constructor, destructor, or type conversion");
12766 return error_mark_node;
12769 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12770 if (token->type == CPP_EQ
12771 || token->type == CPP_OPEN_PAREN
12772 || token->type == CPP_OPEN_BRACE)
12774 is_initialized = SD_INITIALIZED;
12775 initialization_kind = token->type;
12777 if (token->type == CPP_EQ
12778 && function_declarator_p (declarator))
12780 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12781 if (t2->keyword == RID_DEFAULT)
12782 is_initialized = SD_DEFAULTED;
12783 else if (t2->keyword == RID_DELETE)
12784 is_initialized = SD_DELETED;
12789 /* If the init-declarator isn't initialized and isn't followed by a
12790 `,' or `;', it's not a valid init-declarator. */
12791 if (token->type != CPP_COMMA
12792 && token->type != CPP_SEMICOLON)
12794 cp_parser_error (parser, "expected initializer");
12795 return error_mark_node;
12797 is_initialized = SD_UNINITIALIZED;
12798 initialization_kind = CPP_EOF;
12801 /* Because start_decl has side-effects, we should only call it if we
12802 know we're going ahead. By this point, we know that we cannot
12803 possibly be looking at any other construct. */
12804 cp_parser_commit_to_tentative_parse (parser);
12806 /* If the decl specifiers were bad, issue an error now that we're
12807 sure this was intended to be a declarator. Then continue
12808 declaring the variable(s), as int, to try to cut down on further
12810 if (decl_specifiers->any_specifiers_p
12811 && decl_specifiers->type == error_mark_node)
12813 cp_parser_error (parser, "invalid type in declaration");
12814 decl_specifiers->type = integer_type_node;
12817 /* Check to see whether or not this declaration is a friend. */
12818 friend_p = cp_parser_friend_p (decl_specifiers);
12820 /* Enter the newly declared entry in the symbol table. If we're
12821 processing a declaration in a class-specifier, we wait until
12822 after processing the initializer. */
12825 if (parser->in_unbraced_linkage_specification_p)
12826 decl_specifiers->storage_class = sc_extern;
12827 decl = start_decl (declarator, decl_specifiers,
12828 is_initialized, attributes, prefix_attributes,
12832 /* Enter the SCOPE. That way unqualified names appearing in the
12833 initializer will be looked up in SCOPE. */
12834 pushed_scope = push_scope (scope);
12836 /* Perform deferred access control checks, now that we know in which
12837 SCOPE the declared entity resides. */
12838 if (!member_p && decl)
12840 tree saved_current_function_decl = NULL_TREE;
12842 /* If the entity being declared is a function, pretend that we
12843 are in its scope. If it is a `friend', it may have access to
12844 things that would not otherwise be accessible. */
12845 if (TREE_CODE (decl) == FUNCTION_DECL)
12847 saved_current_function_decl = current_function_decl;
12848 current_function_decl = decl;
12851 /* Perform access checks for template parameters. */
12852 cp_parser_perform_template_parameter_access_checks (checks);
12854 /* Perform the access control checks for the declarator and the
12855 decl-specifiers. */
12856 perform_deferred_access_checks ();
12858 /* Restore the saved value. */
12859 if (TREE_CODE (decl) == FUNCTION_DECL)
12860 current_function_decl = saved_current_function_decl;
12863 /* Parse the initializer. */
12864 initializer = NULL_TREE;
12865 is_direct_init = false;
12866 is_non_constant_init = true;
12867 if (is_initialized)
12869 if (function_declarator_p (declarator))
12871 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12872 if (initialization_kind == CPP_EQ)
12873 initializer = cp_parser_pure_specifier (parser);
12876 /* If the declaration was erroneous, we don't really
12877 know what the user intended, so just silently
12878 consume the initializer. */
12879 if (decl != error_mark_node)
12880 error ("%Hinitializer provided for function",
12881 &initializer_start_token->location);
12882 cp_parser_skip_to_closing_parenthesis (parser,
12883 /*recovering=*/true,
12884 /*or_comma=*/false,
12885 /*consume_paren=*/true);
12889 initializer = cp_parser_initializer (parser,
12891 &is_non_constant_init);
12894 /* The old parser allows attributes to appear after a parenthesized
12895 initializer. Mark Mitchell proposed removing this functionality
12896 on the GCC mailing lists on 2002-08-13. This parser accepts the
12897 attributes -- but ignores them. */
12898 if (cp_parser_allow_gnu_extensions_p (parser)
12899 && initialization_kind == CPP_OPEN_PAREN)
12900 if (cp_parser_attributes_opt (parser))
12901 warning (OPT_Wattributes,
12902 "attributes after parenthesized initializer ignored");
12904 /* For an in-class declaration, use `grokfield' to create the
12910 pop_scope (pushed_scope);
12911 pushed_scope = false;
12913 decl = grokfield (declarator, decl_specifiers,
12914 initializer, !is_non_constant_init,
12915 /*asmspec=*/NULL_TREE,
12916 prefix_attributes);
12917 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12918 cp_parser_save_default_args (parser, decl);
12921 /* Finish processing the declaration. But, skip friend
12923 if (!friend_p && decl && decl != error_mark_node)
12925 cp_finish_decl (decl,
12926 initializer, !is_non_constant_init,
12928 /* If the initializer is in parentheses, then this is
12929 a direct-initialization, which means that an
12930 `explicit' constructor is OK. Otherwise, an
12931 `explicit' constructor cannot be used. */
12932 ((is_direct_init || !is_initialized)
12933 ? 0 : LOOKUP_ONLYCONVERTING));
12935 else if ((cxx_dialect != cxx98) && friend_p
12936 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12937 /* Core issue #226 (C++0x only): A default template-argument
12938 shall not be specified in a friend class template
12940 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12941 /*is_partial=*/0, /*is_friend_decl=*/1);
12943 if (!friend_p && pushed_scope)
12944 pop_scope (pushed_scope);
12949 /* Parse a declarator.
12953 ptr-operator declarator
12955 abstract-declarator:
12956 ptr-operator abstract-declarator [opt]
12957 direct-abstract-declarator
12962 attributes [opt] direct-declarator
12963 attributes [opt] ptr-operator declarator
12965 abstract-declarator:
12966 attributes [opt] ptr-operator abstract-declarator [opt]
12967 attributes [opt] direct-abstract-declarator
12969 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12970 detect constructor, destructor or conversion operators. It is set
12971 to -1 if the declarator is a name, and +1 if it is a
12972 function. Otherwise it is set to zero. Usually you just want to
12973 test for >0, but internally the negative value is used.
12975 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12976 a decl-specifier-seq unless it declares a constructor, destructor,
12977 or conversion. It might seem that we could check this condition in
12978 semantic analysis, rather than parsing, but that makes it difficult
12979 to handle something like `f()'. We want to notice that there are
12980 no decl-specifiers, and therefore realize that this is an
12981 expression, not a declaration.)
12983 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12984 the declarator is a direct-declarator of the form "(...)".
12986 MEMBER_P is true iff this declarator is a member-declarator. */
12988 static cp_declarator *
12989 cp_parser_declarator (cp_parser* parser,
12990 cp_parser_declarator_kind dcl_kind,
12991 int* ctor_dtor_or_conv_p,
12992 bool* parenthesized_p,
12996 cp_declarator *declarator;
12997 enum tree_code code;
12998 cp_cv_quals cv_quals;
13000 tree attributes = NULL_TREE;
13002 /* Assume this is not a constructor, destructor, or type-conversion
13004 if (ctor_dtor_or_conv_p)
13005 *ctor_dtor_or_conv_p = 0;
13007 if (cp_parser_allow_gnu_extensions_p (parser))
13008 attributes = cp_parser_attributes_opt (parser);
13010 /* Peek at the next token. */
13011 token = cp_lexer_peek_token (parser->lexer);
13013 /* Check for the ptr-operator production. */
13014 cp_parser_parse_tentatively (parser);
13015 /* Parse the ptr-operator. */
13016 code = cp_parser_ptr_operator (parser,
13019 /* If that worked, then we have a ptr-operator. */
13020 if (cp_parser_parse_definitely (parser))
13022 /* If a ptr-operator was found, then this declarator was not
13024 if (parenthesized_p)
13025 *parenthesized_p = true;
13026 /* The dependent declarator is optional if we are parsing an
13027 abstract-declarator. */
13028 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13029 cp_parser_parse_tentatively (parser);
13031 /* Parse the dependent declarator. */
13032 declarator = cp_parser_declarator (parser, dcl_kind,
13033 /*ctor_dtor_or_conv_p=*/NULL,
13034 /*parenthesized_p=*/NULL,
13035 /*member_p=*/false);
13037 /* If we are parsing an abstract-declarator, we must handle the
13038 case where the dependent declarator is absent. */
13039 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13040 && !cp_parser_parse_definitely (parser))
13043 declarator = cp_parser_make_indirect_declarator
13044 (code, class_type, cv_quals, declarator);
13046 /* Everything else is a direct-declarator. */
13049 if (parenthesized_p)
13050 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13052 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13053 ctor_dtor_or_conv_p,
13057 if (attributes && declarator && declarator != cp_error_declarator)
13058 declarator->attributes = attributes;
13063 /* Parse a direct-declarator or direct-abstract-declarator.
13067 direct-declarator ( parameter-declaration-clause )
13068 cv-qualifier-seq [opt]
13069 exception-specification [opt]
13070 direct-declarator [ constant-expression [opt] ]
13073 direct-abstract-declarator:
13074 direct-abstract-declarator [opt]
13075 ( parameter-declaration-clause )
13076 cv-qualifier-seq [opt]
13077 exception-specification [opt]
13078 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13079 ( abstract-declarator )
13081 Returns a representation of the declarator. DCL_KIND is
13082 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13083 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13084 we are parsing a direct-declarator. It is
13085 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13086 of ambiguity we prefer an abstract declarator, as per
13087 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13088 cp_parser_declarator. */
13090 static cp_declarator *
13091 cp_parser_direct_declarator (cp_parser* parser,
13092 cp_parser_declarator_kind dcl_kind,
13093 int* ctor_dtor_or_conv_p,
13097 cp_declarator *declarator = NULL;
13098 tree scope = NULL_TREE;
13099 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13100 bool saved_in_declarator_p = parser->in_declarator_p;
13102 tree pushed_scope = NULL_TREE;
13106 /* Peek at the next token. */
13107 token = cp_lexer_peek_token (parser->lexer);
13108 if (token->type == CPP_OPEN_PAREN)
13110 /* This is either a parameter-declaration-clause, or a
13111 parenthesized declarator. When we know we are parsing a
13112 named declarator, it must be a parenthesized declarator
13113 if FIRST is true. For instance, `(int)' is a
13114 parameter-declaration-clause, with an omitted
13115 direct-abstract-declarator. But `((*))', is a
13116 parenthesized abstract declarator. Finally, when T is a
13117 template parameter `(T)' is a
13118 parameter-declaration-clause, and not a parenthesized
13121 We first try and parse a parameter-declaration-clause,
13122 and then try a nested declarator (if FIRST is true).
13124 It is not an error for it not to be a
13125 parameter-declaration-clause, even when FIRST is
13131 The first is the declaration of a function while the
13132 second is the definition of a variable, including its
13135 Having seen only the parenthesis, we cannot know which of
13136 these two alternatives should be selected. Even more
13137 complex are examples like:
13142 The former is a function-declaration; the latter is a
13143 variable initialization.
13145 Thus again, we try a parameter-declaration-clause, and if
13146 that fails, we back out and return. */
13148 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13151 unsigned saved_num_template_parameter_lists;
13152 bool is_declarator = false;
13155 /* In a member-declarator, the only valid interpretation
13156 of a parenthesis is the start of a
13157 parameter-declaration-clause. (It is invalid to
13158 initialize a static data member with a parenthesized
13159 initializer; only the "=" form of initialization is
13162 cp_parser_parse_tentatively (parser);
13164 /* Consume the `('. */
13165 cp_lexer_consume_token (parser->lexer);
13168 /* If this is going to be an abstract declarator, we're
13169 in a declarator and we can't have default args. */
13170 parser->default_arg_ok_p = false;
13171 parser->in_declarator_p = true;
13174 /* Inside the function parameter list, surrounding
13175 template-parameter-lists do not apply. */
13176 saved_num_template_parameter_lists
13177 = parser->num_template_parameter_lists;
13178 parser->num_template_parameter_lists = 0;
13180 begin_scope (sk_function_parms, NULL_TREE);
13182 /* Parse the parameter-declaration-clause. */
13183 params = cp_parser_parameter_declaration_clause (parser);
13185 parser->num_template_parameter_lists
13186 = saved_num_template_parameter_lists;
13188 /* If all went well, parse the cv-qualifier-seq and the
13189 exception-specification. */
13190 if (member_p || cp_parser_parse_definitely (parser))
13192 cp_cv_quals cv_quals;
13193 tree exception_specification;
13196 is_declarator = true;
13198 if (ctor_dtor_or_conv_p)
13199 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13201 /* Consume the `)'. */
13202 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13204 /* Parse the cv-qualifier-seq. */
13205 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13206 /* And the exception-specification. */
13207 exception_specification
13208 = cp_parser_exception_specification_opt (parser);
13211 = cp_parser_late_return_type_opt (parser);
13213 /* Create the function-declarator. */
13214 declarator = make_call_declarator (declarator,
13217 exception_specification,
13219 /* Any subsequent parameter lists are to do with
13220 return type, so are not those of the declared
13222 parser->default_arg_ok_p = false;
13225 /* Remove the function parms from scope. */
13226 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13227 pop_binding (DECL_NAME (t), t);
13231 /* Repeat the main loop. */
13235 /* If this is the first, we can try a parenthesized
13239 bool saved_in_type_id_in_expr_p;
13241 parser->default_arg_ok_p = saved_default_arg_ok_p;
13242 parser->in_declarator_p = saved_in_declarator_p;
13244 /* Consume the `('. */
13245 cp_lexer_consume_token (parser->lexer);
13246 /* Parse the nested declarator. */
13247 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13248 parser->in_type_id_in_expr_p = true;
13250 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13251 /*parenthesized_p=*/NULL,
13253 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13255 /* Expect a `)'. */
13256 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13257 declarator = cp_error_declarator;
13258 if (declarator == cp_error_declarator)
13261 goto handle_declarator;
13263 /* Otherwise, we must be done. */
13267 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13268 && token->type == CPP_OPEN_SQUARE)
13270 /* Parse an array-declarator. */
13273 if (ctor_dtor_or_conv_p)
13274 *ctor_dtor_or_conv_p = 0;
13277 parser->default_arg_ok_p = false;
13278 parser->in_declarator_p = true;
13279 /* Consume the `['. */
13280 cp_lexer_consume_token (parser->lexer);
13281 /* Peek at the next token. */
13282 token = cp_lexer_peek_token (parser->lexer);
13283 /* If the next token is `]', then there is no
13284 constant-expression. */
13285 if (token->type != CPP_CLOSE_SQUARE)
13287 bool non_constant_p;
13290 = cp_parser_constant_expression (parser,
13291 /*allow_non_constant=*/true,
13293 if (!non_constant_p)
13294 bounds = fold_non_dependent_expr (bounds);
13295 else if (processing_template_decl)
13297 /* Remember this wasn't a constant-expression. */
13298 bounds = build_nop (TREE_TYPE (bounds), bounds);
13299 TREE_SIDE_EFFECTS (bounds) = 1;
13302 /* Normally, the array bound must be an integral constant
13303 expression. However, as an extension, we allow VLAs
13304 in function scopes. */
13305 else if (!parser->in_function_body)
13307 error ("%Harray bound is not an integer constant",
13309 bounds = error_mark_node;
13313 bounds = NULL_TREE;
13314 /* Look for the closing `]'. */
13315 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13317 declarator = cp_error_declarator;
13321 declarator = make_array_declarator (declarator, bounds);
13323 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13325 tree qualifying_scope;
13326 tree unqualified_name;
13327 special_function_kind sfk;
13329 bool pack_expansion_p = false;
13330 cp_token *declarator_id_start_token;
13332 /* Parse a declarator-id */
13333 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13336 cp_parser_parse_tentatively (parser);
13338 /* If we see an ellipsis, we should be looking at a
13340 if (token->type == CPP_ELLIPSIS)
13342 /* Consume the `...' */
13343 cp_lexer_consume_token (parser->lexer);
13345 pack_expansion_p = true;
13349 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13351 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13352 qualifying_scope = parser->scope;
13357 if (!unqualified_name && pack_expansion_p)
13359 /* Check whether an error occurred. */
13360 okay = !cp_parser_error_occurred (parser);
13362 /* We already consumed the ellipsis to mark a
13363 parameter pack, but we have no way to report it,
13364 so abort the tentative parse. We will be exiting
13365 immediately anyway. */
13366 cp_parser_abort_tentative_parse (parser);
13369 okay = cp_parser_parse_definitely (parser);
13372 unqualified_name = error_mark_node;
13373 else if (unqualified_name
13374 && (qualifying_scope
13375 || (TREE_CODE (unqualified_name)
13376 != IDENTIFIER_NODE)))
13378 cp_parser_error (parser, "expected unqualified-id");
13379 unqualified_name = error_mark_node;
13383 if (!unqualified_name)
13385 if (unqualified_name == error_mark_node)
13387 declarator = cp_error_declarator;
13388 pack_expansion_p = false;
13389 declarator->parameter_pack_p = false;
13393 if (qualifying_scope && at_namespace_scope_p ()
13394 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13396 /* In the declaration of a member of a template class
13397 outside of the class itself, the SCOPE will sometimes
13398 be a TYPENAME_TYPE. For example, given:
13400 template <typename T>
13401 int S<T>::R::i = 3;
13403 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13404 this context, we must resolve S<T>::R to an ordinary
13405 type, rather than a typename type.
13407 The reason we normally avoid resolving TYPENAME_TYPEs
13408 is that a specialization of `S' might render
13409 `S<T>::R' not a type. However, if `S' is
13410 specialized, then this `i' will not be used, so there
13411 is no harm in resolving the types here. */
13414 /* Resolve the TYPENAME_TYPE. */
13415 type = resolve_typename_type (qualifying_scope,
13416 /*only_current_p=*/false);
13417 /* If that failed, the declarator is invalid. */
13418 if (TREE_CODE (type) == TYPENAME_TYPE)
13419 error ("%H%<%T::%E%> is not a type",
13420 &declarator_id_start_token->location,
13421 TYPE_CONTEXT (qualifying_scope),
13422 TYPE_IDENTIFIER (qualifying_scope));
13423 qualifying_scope = type;
13428 if (unqualified_name)
13432 if (qualifying_scope
13433 && CLASS_TYPE_P (qualifying_scope))
13434 class_type = qualifying_scope;
13436 class_type = current_class_type;
13438 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13440 tree name_type = TREE_TYPE (unqualified_name);
13441 if (class_type && same_type_p (name_type, class_type))
13443 if (qualifying_scope
13444 && CLASSTYPE_USE_TEMPLATE (name_type))
13446 error ("%Hinvalid use of constructor as a template",
13447 &declarator_id_start_token->location);
13448 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13449 "name the constructor in a qualified name",
13451 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13452 class_type, name_type);
13453 declarator = cp_error_declarator;
13457 unqualified_name = constructor_name (class_type);
13461 /* We do not attempt to print the declarator
13462 here because we do not have enough
13463 information about its original syntactic
13465 cp_parser_error (parser, "invalid declarator");
13466 declarator = cp_error_declarator;
13473 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13474 sfk = sfk_destructor;
13475 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13476 sfk = sfk_conversion;
13477 else if (/* There's no way to declare a constructor
13478 for an anonymous type, even if the type
13479 got a name for linkage purposes. */
13480 !TYPE_WAS_ANONYMOUS (class_type)
13481 && constructor_name_p (unqualified_name,
13484 unqualified_name = constructor_name (class_type);
13485 sfk = sfk_constructor;
13488 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13489 *ctor_dtor_or_conv_p = -1;
13492 declarator = make_id_declarator (qualifying_scope,
13495 declarator->id_loc = token->location;
13496 declarator->parameter_pack_p = pack_expansion_p;
13498 if (pack_expansion_p)
13499 maybe_warn_variadic_templates ();
13501 handle_declarator:;
13502 scope = get_scope_of_declarator (declarator);
13504 /* Any names that appear after the declarator-id for a
13505 member are looked up in the containing scope. */
13506 pushed_scope = push_scope (scope);
13507 parser->in_declarator_p = true;
13508 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13509 || (declarator && declarator->kind == cdk_id))
13510 /* Default args are only allowed on function
13512 parser->default_arg_ok_p = saved_default_arg_ok_p;
13514 parser->default_arg_ok_p = false;
13523 /* For an abstract declarator, we might wind up with nothing at this
13524 point. That's an error; the declarator is not optional. */
13526 cp_parser_error (parser, "expected declarator");
13528 /* If we entered a scope, we must exit it now. */
13530 pop_scope (pushed_scope);
13532 parser->default_arg_ok_p = saved_default_arg_ok_p;
13533 parser->in_declarator_p = saved_in_declarator_p;
13538 /* Parse a ptr-operator.
13541 * cv-qualifier-seq [opt]
13543 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13548 & cv-qualifier-seq [opt]
13550 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13551 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13552 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13553 filled in with the TYPE containing the member. *CV_QUALS is
13554 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13555 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13556 Note that the tree codes returned by this function have nothing
13557 to do with the types of trees that will be eventually be created
13558 to represent the pointer or reference type being parsed. They are
13559 just constants with suggestive names. */
13560 static enum tree_code
13561 cp_parser_ptr_operator (cp_parser* parser,
13563 cp_cv_quals *cv_quals)
13565 enum tree_code code = ERROR_MARK;
13568 /* Assume that it's not a pointer-to-member. */
13570 /* And that there are no cv-qualifiers. */
13571 *cv_quals = TYPE_UNQUALIFIED;
13573 /* Peek at the next token. */
13574 token = cp_lexer_peek_token (parser->lexer);
13576 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13577 if (token->type == CPP_MULT)
13578 code = INDIRECT_REF;
13579 else if (token->type == CPP_AND)
13581 else if ((cxx_dialect != cxx98) &&
13582 token->type == CPP_AND_AND) /* C++0x only */
13583 code = NON_LVALUE_EXPR;
13585 if (code != ERROR_MARK)
13587 /* Consume the `*', `&' or `&&'. */
13588 cp_lexer_consume_token (parser->lexer);
13590 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13591 `&', if we are allowing GNU extensions. (The only qualifier
13592 that can legally appear after `&' is `restrict', but that is
13593 enforced during semantic analysis. */
13594 if (code == INDIRECT_REF
13595 || cp_parser_allow_gnu_extensions_p (parser))
13596 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13600 /* Try the pointer-to-member case. */
13601 cp_parser_parse_tentatively (parser);
13602 /* Look for the optional `::' operator. */
13603 cp_parser_global_scope_opt (parser,
13604 /*current_scope_valid_p=*/false);
13605 /* Look for the nested-name specifier. */
13606 token = cp_lexer_peek_token (parser->lexer);
13607 cp_parser_nested_name_specifier (parser,
13608 /*typename_keyword_p=*/false,
13609 /*check_dependency_p=*/true,
13611 /*is_declaration=*/false);
13612 /* If we found it, and the next token is a `*', then we are
13613 indeed looking at a pointer-to-member operator. */
13614 if (!cp_parser_error_occurred (parser)
13615 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13617 /* Indicate that the `*' operator was used. */
13618 code = INDIRECT_REF;
13620 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13621 error ("%H%qD is a namespace", &token->location, parser->scope);
13624 /* The type of which the member is a member is given by the
13626 *type = parser->scope;
13627 /* The next name will not be qualified. */
13628 parser->scope = NULL_TREE;
13629 parser->qualifying_scope = NULL_TREE;
13630 parser->object_scope = NULL_TREE;
13631 /* Look for the optional cv-qualifier-seq. */
13632 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13635 /* If that didn't work we don't have a ptr-operator. */
13636 if (!cp_parser_parse_definitely (parser))
13637 cp_parser_error (parser, "expected ptr-operator");
13643 /* Parse an (optional) cv-qualifier-seq.
13646 cv-qualifier cv-qualifier-seq [opt]
13657 Returns a bitmask representing the cv-qualifiers. */
13660 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13662 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13667 cp_cv_quals cv_qualifier;
13669 /* Peek at the next token. */
13670 token = cp_lexer_peek_token (parser->lexer);
13671 /* See if it's a cv-qualifier. */
13672 switch (token->keyword)
13675 cv_qualifier = TYPE_QUAL_CONST;
13679 cv_qualifier = TYPE_QUAL_VOLATILE;
13683 cv_qualifier = TYPE_QUAL_RESTRICT;
13687 cv_qualifier = TYPE_UNQUALIFIED;
13694 if (cv_quals & cv_qualifier)
13696 error ("%Hduplicate cv-qualifier", &token->location);
13697 cp_lexer_purge_token (parser->lexer);
13701 cp_lexer_consume_token (parser->lexer);
13702 cv_quals |= cv_qualifier;
13709 /* Parse a late-specified return type, if any. This is not a separate
13710 non-terminal, but part of a function declarator, which looks like
13714 Returns the type indicated by the type-id. */
13717 cp_parser_late_return_type_opt (cp_parser* parser)
13721 /* Peek at the next token. */
13722 token = cp_lexer_peek_token (parser->lexer);
13723 /* A late-specified return type is indicated by an initial '->'. */
13724 if (token->type != CPP_DEREF)
13727 /* Consume the ->. */
13728 cp_lexer_consume_token (parser->lexer);
13730 return cp_parser_type_id (parser);
13733 /* Parse a declarator-id.
13737 :: [opt] nested-name-specifier [opt] type-name
13739 In the `id-expression' case, the value returned is as for
13740 cp_parser_id_expression if the id-expression was an unqualified-id.
13741 If the id-expression was a qualified-id, then a SCOPE_REF is
13742 returned. The first operand is the scope (either a NAMESPACE_DECL
13743 or TREE_TYPE), but the second is still just a representation of an
13747 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13750 /* The expression must be an id-expression. Assume that qualified
13751 names are the names of types so that:
13754 int S<T>::R::i = 3;
13756 will work; we must treat `S<T>::R' as the name of a type.
13757 Similarly, assume that qualified names are templates, where
13761 int S<T>::R<T>::i = 3;
13764 id = cp_parser_id_expression (parser,
13765 /*template_keyword_p=*/false,
13766 /*check_dependency_p=*/false,
13767 /*template_p=*/NULL,
13768 /*declarator_p=*/true,
13770 if (id && BASELINK_P (id))
13771 id = BASELINK_FUNCTIONS (id);
13775 /* Parse a type-id.
13778 type-specifier-seq abstract-declarator [opt]
13780 Returns the TYPE specified. */
13783 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13785 cp_decl_specifier_seq type_specifier_seq;
13786 cp_declarator *abstract_declarator;
13788 /* Parse the type-specifier-seq. */
13789 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13790 &type_specifier_seq);
13791 if (type_specifier_seq.type == error_mark_node)
13792 return error_mark_node;
13794 /* There might or might not be an abstract declarator. */
13795 cp_parser_parse_tentatively (parser);
13796 /* Look for the declarator. */
13797 abstract_declarator
13798 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13799 /*parenthesized_p=*/NULL,
13800 /*member_p=*/false);
13801 /* Check to see if there really was a declarator. */
13802 if (!cp_parser_parse_definitely (parser))
13803 abstract_declarator = NULL;
13805 if (type_specifier_seq.type
13806 && type_uses_auto (type_specifier_seq.type))
13808 error ("invalid use of %<auto%>");
13809 return error_mark_node;
13812 return groktypename (&type_specifier_seq, abstract_declarator,
13816 static tree cp_parser_type_id (cp_parser *parser)
13818 return cp_parser_type_id_1 (parser, false);
13821 static tree cp_parser_template_type_arg (cp_parser *parser)
13823 return cp_parser_type_id_1 (parser, true);
13826 /* Parse a type-specifier-seq.
13828 type-specifier-seq:
13829 type-specifier type-specifier-seq [opt]
13833 type-specifier-seq:
13834 attributes type-specifier-seq [opt]
13836 If IS_CONDITION is true, we are at the start of a "condition",
13837 e.g., we've just seen "if (".
13839 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13842 cp_parser_type_specifier_seq (cp_parser* parser,
13844 cp_decl_specifier_seq *type_specifier_seq)
13846 bool seen_type_specifier = false;
13847 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13848 cp_token *start_token = NULL;
13850 /* Clear the TYPE_SPECIFIER_SEQ. */
13851 clear_decl_specs (type_specifier_seq);
13853 /* Parse the type-specifiers and attributes. */
13856 tree type_specifier;
13857 bool is_cv_qualifier;
13859 /* Check for attributes first. */
13860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13862 type_specifier_seq->attributes =
13863 chainon (type_specifier_seq->attributes,
13864 cp_parser_attributes_opt (parser));
13868 /* record the token of the beginning of the type specifier seq,
13869 for error reporting purposes*/
13871 start_token = cp_lexer_peek_token (parser->lexer);
13873 /* Look for the type-specifier. */
13874 type_specifier = cp_parser_type_specifier (parser,
13876 type_specifier_seq,
13877 /*is_declaration=*/false,
13880 if (!type_specifier)
13882 /* If the first type-specifier could not be found, this is not a
13883 type-specifier-seq at all. */
13884 if (!seen_type_specifier)
13886 cp_parser_error (parser, "expected type-specifier");
13887 type_specifier_seq->type = error_mark_node;
13890 /* If subsequent type-specifiers could not be found, the
13891 type-specifier-seq is complete. */
13895 seen_type_specifier = true;
13896 /* The standard says that a condition can be:
13898 type-specifier-seq declarator = assignment-expression
13905 we should treat the "S" as a declarator, not as a
13906 type-specifier. The standard doesn't say that explicitly for
13907 type-specifier-seq, but it does say that for
13908 decl-specifier-seq in an ordinary declaration. Perhaps it
13909 would be clearer just to allow a decl-specifier-seq here, and
13910 then add a semantic restriction that if any decl-specifiers
13911 that are not type-specifiers appear, the program is invalid. */
13912 if (is_condition && !is_cv_qualifier)
13913 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13916 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13919 /* Parse a parameter-declaration-clause.
13921 parameter-declaration-clause:
13922 parameter-declaration-list [opt] ... [opt]
13923 parameter-declaration-list , ...
13925 Returns a representation for the parameter declarations. A return
13926 value of NULL indicates a parameter-declaration-clause consisting
13927 only of an ellipsis. */
13930 cp_parser_parameter_declaration_clause (cp_parser* parser)
13937 /* Peek at the next token. */
13938 token = cp_lexer_peek_token (parser->lexer);
13939 /* Check for trivial parameter-declaration-clauses. */
13940 if (token->type == CPP_ELLIPSIS)
13942 /* Consume the `...' token. */
13943 cp_lexer_consume_token (parser->lexer);
13946 else if (token->type == CPP_CLOSE_PAREN)
13947 /* There are no parameters. */
13949 #ifndef NO_IMPLICIT_EXTERN_C
13950 if (in_system_header && current_class_type == NULL
13951 && current_lang_name == lang_name_c)
13955 return void_list_node;
13957 /* Check for `(void)', too, which is a special case. */
13958 else if (token->keyword == RID_VOID
13959 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13960 == CPP_CLOSE_PAREN))
13962 /* Consume the `void' token. */
13963 cp_lexer_consume_token (parser->lexer);
13964 /* There are no parameters. */
13965 return void_list_node;
13968 /* Parse the parameter-declaration-list. */
13969 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13970 /* If a parse error occurred while parsing the
13971 parameter-declaration-list, then the entire
13972 parameter-declaration-clause is erroneous. */
13976 /* Peek at the next token. */
13977 token = cp_lexer_peek_token (parser->lexer);
13978 /* If it's a `,', the clause should terminate with an ellipsis. */
13979 if (token->type == CPP_COMMA)
13981 /* Consume the `,'. */
13982 cp_lexer_consume_token (parser->lexer);
13983 /* Expect an ellipsis. */
13985 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13987 /* It might also be `...' if the optional trailing `,' was
13989 else if (token->type == CPP_ELLIPSIS)
13991 /* Consume the `...' token. */
13992 cp_lexer_consume_token (parser->lexer);
13993 /* And remember that we saw it. */
13997 ellipsis_p = false;
13999 /* Finish the parameter list. */
14001 parameters = chainon (parameters, void_list_node);
14006 /* Parse a parameter-declaration-list.
14008 parameter-declaration-list:
14009 parameter-declaration
14010 parameter-declaration-list , parameter-declaration
14012 Returns a representation of the parameter-declaration-list, as for
14013 cp_parser_parameter_declaration_clause. However, the
14014 `void_list_node' is never appended to the list. Upon return,
14015 *IS_ERROR will be true iff an error occurred. */
14018 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14020 tree parameters = NULL_TREE;
14021 tree *tail = ¶meters;
14022 bool saved_in_unbraced_linkage_specification_p;
14024 /* Assume all will go well. */
14026 /* The special considerations that apply to a function within an
14027 unbraced linkage specifications do not apply to the parameters
14028 to the function. */
14029 saved_in_unbraced_linkage_specification_p
14030 = parser->in_unbraced_linkage_specification_p;
14031 parser->in_unbraced_linkage_specification_p = false;
14033 /* Look for more parameters. */
14036 cp_parameter_declarator *parameter;
14037 tree decl = error_mark_node;
14038 bool parenthesized_p;
14039 /* Parse the parameter. */
14041 = cp_parser_parameter_declaration (parser,
14042 /*template_parm_p=*/false,
14045 /* We don't know yet if the enclosing context is deprecated, so wait
14046 and warn in grokparms if appropriate. */
14047 deprecated_state = DEPRECATED_SUPPRESS;
14050 decl = grokdeclarator (parameter->declarator,
14051 ¶meter->decl_specifiers,
14053 parameter->default_argument != NULL_TREE,
14054 ¶meter->decl_specifiers.attributes);
14056 deprecated_state = DEPRECATED_NORMAL;
14058 /* If a parse error occurred parsing the parameter declaration,
14059 then the entire parameter-declaration-list is erroneous. */
14060 if (decl == error_mark_node)
14063 parameters = error_mark_node;
14067 if (parameter->decl_specifiers.attributes)
14068 cplus_decl_attributes (&decl,
14069 parameter->decl_specifiers.attributes,
14071 if (DECL_NAME (decl))
14072 decl = pushdecl (decl);
14074 /* Add the new parameter to the list. */
14075 *tail = build_tree_list (parameter->default_argument, decl);
14076 tail = &TREE_CHAIN (*tail);
14078 /* Peek at the next token. */
14079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14080 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14081 /* These are for Objective-C++ */
14082 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14083 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14084 /* The parameter-declaration-list is complete. */
14086 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14090 /* Peek at the next token. */
14091 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14092 /* If it's an ellipsis, then the list is complete. */
14093 if (token->type == CPP_ELLIPSIS)
14095 /* Otherwise, there must be more parameters. Consume the
14097 cp_lexer_consume_token (parser->lexer);
14098 /* When parsing something like:
14100 int i(float f, double d)
14102 we can tell after seeing the declaration for "f" that we
14103 are not looking at an initialization of a variable "i",
14104 but rather at the declaration of a function "i".
14106 Due to the fact that the parsing of template arguments
14107 (as specified to a template-id) requires backtracking we
14108 cannot use this technique when inside a template argument
14110 if (!parser->in_template_argument_list_p
14111 && !parser->in_type_id_in_expr_p
14112 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14113 /* However, a parameter-declaration of the form
14114 "foat(f)" (which is a valid declaration of a
14115 parameter "f") can also be interpreted as an
14116 expression (the conversion of "f" to "float"). */
14117 && !parenthesized_p)
14118 cp_parser_commit_to_tentative_parse (parser);
14122 cp_parser_error (parser, "expected %<,%> or %<...%>");
14123 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14124 cp_parser_skip_to_closing_parenthesis (parser,
14125 /*recovering=*/true,
14126 /*or_comma=*/false,
14127 /*consume_paren=*/false);
14132 parser->in_unbraced_linkage_specification_p
14133 = saved_in_unbraced_linkage_specification_p;
14138 /* Parse a parameter declaration.
14140 parameter-declaration:
14141 decl-specifier-seq ... [opt] declarator
14142 decl-specifier-seq declarator = assignment-expression
14143 decl-specifier-seq ... [opt] abstract-declarator [opt]
14144 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14146 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14147 declares a template parameter. (In that case, a non-nested `>'
14148 token encountered during the parsing of the assignment-expression
14149 is not interpreted as a greater-than operator.)
14151 Returns a representation of the parameter, or NULL if an error
14152 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14153 true iff the declarator is of the form "(p)". */
14155 static cp_parameter_declarator *
14156 cp_parser_parameter_declaration (cp_parser *parser,
14157 bool template_parm_p,
14158 bool *parenthesized_p)
14160 int declares_class_or_enum;
14161 bool greater_than_is_operator_p;
14162 cp_decl_specifier_seq decl_specifiers;
14163 cp_declarator *declarator;
14164 tree default_argument;
14165 cp_token *token = NULL, *declarator_token_start = NULL;
14166 const char *saved_message;
14168 /* In a template parameter, `>' is not an operator.
14172 When parsing a default template-argument for a non-type
14173 template-parameter, the first non-nested `>' is taken as the end
14174 of the template parameter-list rather than a greater-than
14176 greater_than_is_operator_p = !template_parm_p;
14178 /* Type definitions may not appear in parameter types. */
14179 saved_message = parser->type_definition_forbidden_message;
14180 parser->type_definition_forbidden_message
14181 = "types may not be defined in parameter types";
14183 /* Parse the declaration-specifiers. */
14184 cp_parser_decl_specifier_seq (parser,
14185 CP_PARSER_FLAGS_NONE,
14187 &declares_class_or_enum);
14188 /* If an error occurred, there's no reason to attempt to parse the
14189 rest of the declaration. */
14190 if (cp_parser_error_occurred (parser))
14192 parser->type_definition_forbidden_message = saved_message;
14196 /* Peek at the next token. */
14197 token = cp_lexer_peek_token (parser->lexer);
14199 /* If the next token is a `)', `,', `=', `>', or `...', then there
14200 is no declarator. However, when variadic templates are enabled,
14201 there may be a declarator following `...'. */
14202 if (token->type == CPP_CLOSE_PAREN
14203 || token->type == CPP_COMMA
14204 || token->type == CPP_EQ
14205 || token->type == CPP_GREATER)
14208 if (parenthesized_p)
14209 *parenthesized_p = false;
14211 /* Otherwise, there should be a declarator. */
14214 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14215 parser->default_arg_ok_p = false;
14217 /* After seeing a decl-specifier-seq, if the next token is not a
14218 "(", there is no possibility that the code is a valid
14219 expression. Therefore, if parsing tentatively, we commit at
14221 if (!parser->in_template_argument_list_p
14222 /* In an expression context, having seen:
14226 we cannot be sure whether we are looking at a
14227 function-type (taking a "char" as a parameter) or a cast
14228 of some object of type "char" to "int". */
14229 && !parser->in_type_id_in_expr_p
14230 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14231 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14232 cp_parser_commit_to_tentative_parse (parser);
14233 /* Parse the declarator. */
14234 declarator_token_start = token;
14235 declarator = cp_parser_declarator (parser,
14236 CP_PARSER_DECLARATOR_EITHER,
14237 /*ctor_dtor_or_conv_p=*/NULL,
14239 /*member_p=*/false);
14240 parser->default_arg_ok_p = saved_default_arg_ok_p;
14241 /* After the declarator, allow more attributes. */
14242 decl_specifiers.attributes
14243 = chainon (decl_specifiers.attributes,
14244 cp_parser_attributes_opt (parser));
14247 /* If the next token is an ellipsis, and we have not seen a
14248 declarator name, and the type of the declarator contains parameter
14249 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14250 a parameter pack expansion expression. Otherwise, leave the
14251 ellipsis for a C-style variadic function. */
14252 token = cp_lexer_peek_token (parser->lexer);
14253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14255 tree type = decl_specifiers.type;
14257 if (type && DECL_P (type))
14258 type = TREE_TYPE (type);
14261 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14262 && declarator_can_be_parameter_pack (declarator)
14263 && (!declarator || !declarator->parameter_pack_p)
14264 && uses_parameter_packs (type))
14266 /* Consume the `...'. */
14267 cp_lexer_consume_token (parser->lexer);
14268 maybe_warn_variadic_templates ();
14270 /* Build a pack expansion type */
14272 declarator->parameter_pack_p = true;
14274 decl_specifiers.type = make_pack_expansion (type);
14278 /* The restriction on defining new types applies only to the type
14279 of the parameter, not to the default argument. */
14280 parser->type_definition_forbidden_message = saved_message;
14282 /* If the next token is `=', then process a default argument. */
14283 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14285 /* Consume the `='. */
14286 cp_lexer_consume_token (parser->lexer);
14288 /* If we are defining a class, then the tokens that make up the
14289 default argument must be saved and processed later. */
14290 if (!template_parm_p && at_class_scope_p ()
14291 && TYPE_BEING_DEFINED (current_class_type))
14293 unsigned depth = 0;
14294 int maybe_template_id = 0;
14295 cp_token *first_token;
14298 /* Add tokens until we have processed the entire default
14299 argument. We add the range [first_token, token). */
14300 first_token = cp_lexer_peek_token (parser->lexer);
14305 /* Peek at the next token. */
14306 token = cp_lexer_peek_token (parser->lexer);
14307 /* What we do depends on what token we have. */
14308 switch (token->type)
14310 /* In valid code, a default argument must be
14311 immediately followed by a `,' `)', or `...'. */
14313 if (depth == 0 && maybe_template_id)
14315 /* If we've seen a '<', we might be in a
14316 template-argument-list. Until Core issue 325 is
14317 resolved, we don't know how this situation ought
14318 to be handled, so try to DTRT. We check whether
14319 what comes after the comma is a valid parameter
14320 declaration list. If it is, then the comma ends
14321 the default argument; otherwise the default
14322 argument continues. */
14323 bool error = false;
14325 /* Set ITALP so cp_parser_parameter_declaration_list
14326 doesn't decide to commit to this parse. */
14327 bool saved_italp = parser->in_template_argument_list_p;
14328 parser->in_template_argument_list_p = true;
14330 cp_parser_parse_tentatively (parser);
14331 cp_lexer_consume_token (parser->lexer);
14332 cp_parser_parameter_declaration_list (parser, &error);
14333 if (!cp_parser_error_occurred (parser) && !error)
14335 cp_parser_abort_tentative_parse (parser);
14337 parser->in_template_argument_list_p = saved_italp;
14340 case CPP_CLOSE_PAREN:
14342 /* If we run into a non-nested `;', `}', or `]',
14343 then the code is invalid -- but the default
14344 argument is certainly over. */
14345 case CPP_SEMICOLON:
14346 case CPP_CLOSE_BRACE:
14347 case CPP_CLOSE_SQUARE:
14350 /* Update DEPTH, if necessary. */
14351 else if (token->type == CPP_CLOSE_PAREN
14352 || token->type == CPP_CLOSE_BRACE
14353 || token->type == CPP_CLOSE_SQUARE)
14357 case CPP_OPEN_PAREN:
14358 case CPP_OPEN_SQUARE:
14359 case CPP_OPEN_BRACE:
14365 /* This might be the comparison operator, or it might
14366 start a template argument list. */
14367 ++maybe_template_id;
14371 if (cxx_dialect == cxx98)
14373 /* Fall through for C++0x, which treats the `>>'
14374 operator like two `>' tokens in certain
14380 /* This might be an operator, or it might close a
14381 template argument list. But if a previous '<'
14382 started a template argument list, this will have
14383 closed it, so we can't be in one anymore. */
14384 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14385 if (maybe_template_id < 0)
14386 maybe_template_id = 0;
14390 /* If we run out of tokens, issue an error message. */
14392 case CPP_PRAGMA_EOL:
14393 error ("%Hfile ends in default argument", &token->location);
14399 /* In these cases, we should look for template-ids.
14400 For example, if the default argument is
14401 `X<int, double>()', we need to do name lookup to
14402 figure out whether or not `X' is a template; if
14403 so, the `,' does not end the default argument.
14405 That is not yet done. */
14412 /* If we've reached the end, stop. */
14416 /* Add the token to the token block. */
14417 token = cp_lexer_consume_token (parser->lexer);
14420 /* Create a DEFAULT_ARG to represent the unparsed default
14422 default_argument = make_node (DEFAULT_ARG);
14423 DEFARG_TOKENS (default_argument)
14424 = cp_token_cache_new (first_token, token);
14425 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14427 /* Outside of a class definition, we can just parse the
14428 assignment-expression. */
14431 token = cp_lexer_peek_token (parser->lexer);
14433 = cp_parser_default_argument (parser, template_parm_p);
14436 if (!parser->default_arg_ok_p)
14438 if (flag_permissive)
14439 warning (0, "deprecated use of default argument for parameter of non-function");
14442 error ("%Hdefault arguments are only "
14443 "permitted for function parameters",
14445 default_argument = NULL_TREE;
14448 else if ((declarator && declarator->parameter_pack_p)
14449 || (decl_specifiers.type
14450 && PACK_EXPANSION_P (decl_specifiers.type)))
14452 const char* kind = template_parm_p? "template " : "";
14454 /* Find the name of the parameter pack. */
14455 cp_declarator *id_declarator = declarator;
14456 while (id_declarator && id_declarator->kind != cdk_id)
14457 id_declarator = id_declarator->declarator;
14459 if (id_declarator && id_declarator->kind == cdk_id)
14460 error ("%H%sparameter pack %qD cannot have a default argument",
14461 &declarator_token_start->location,
14462 kind, id_declarator->u.id.unqualified_name);
14464 error ("%H%sparameter pack cannot have a default argument",
14465 &declarator_token_start->location, kind);
14467 default_argument = NULL_TREE;
14471 default_argument = NULL_TREE;
14473 return make_parameter_declarator (&decl_specifiers,
14478 /* Parse a default argument and return it.
14480 TEMPLATE_PARM_P is true if this is a default argument for a
14481 non-type template parameter. */
14483 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14485 tree default_argument = NULL_TREE;
14486 bool saved_greater_than_is_operator_p;
14487 bool saved_local_variables_forbidden_p;
14489 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14491 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14492 parser->greater_than_is_operator_p = !template_parm_p;
14493 /* Local variable names (and the `this' keyword) may not
14494 appear in a default argument. */
14495 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14496 parser->local_variables_forbidden_p = true;
14497 /* The default argument expression may cause implicitly
14498 defined member functions to be synthesized, which will
14499 result in garbage collection. We must treat this
14500 situation as if we were within the body of function so as
14501 to avoid collecting live data on the stack. */
14503 /* Parse the assignment-expression. */
14504 if (template_parm_p)
14505 push_deferring_access_checks (dk_no_deferred);
14507 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14508 if (template_parm_p)
14509 pop_deferring_access_checks ();
14510 /* Restore saved state. */
14512 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14513 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14515 return default_argument;
14518 /* Parse a function-body.
14521 compound_statement */
14524 cp_parser_function_body (cp_parser *parser)
14526 cp_parser_compound_statement (parser, NULL, false);
14529 /* Parse a ctor-initializer-opt followed by a function-body. Return
14530 true if a ctor-initializer was present. */
14533 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14536 bool ctor_initializer_p;
14538 /* Begin the function body. */
14539 body = begin_function_body ();
14540 /* Parse the optional ctor-initializer. */
14541 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14542 /* Parse the function-body. */
14543 cp_parser_function_body (parser);
14544 /* Finish the function body. */
14545 finish_function_body (body);
14547 return ctor_initializer_p;
14550 /* Parse an initializer.
14553 = initializer-clause
14554 ( expression-list )
14556 Returns an expression representing the initializer. If no
14557 initializer is present, NULL_TREE is returned.
14559 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14560 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14561 set to TRUE if there is no initializer present. If there is an
14562 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14563 is set to true; otherwise it is set to false. */
14566 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14567 bool* non_constant_p)
14572 /* Peek at the next token. */
14573 token = cp_lexer_peek_token (parser->lexer);
14575 /* Let our caller know whether or not this initializer was
14577 *is_direct_init = (token->type != CPP_EQ);
14578 /* Assume that the initializer is constant. */
14579 *non_constant_p = false;
14581 if (token->type == CPP_EQ)
14583 /* Consume the `='. */
14584 cp_lexer_consume_token (parser->lexer);
14585 /* Parse the initializer-clause. */
14586 init = cp_parser_initializer_clause (parser, non_constant_p);
14588 else if (token->type == CPP_OPEN_PAREN)
14589 init = cp_parser_parenthesized_expression_list (parser, false,
14591 /*allow_expansion_p=*/true,
14593 else if (token->type == CPP_OPEN_BRACE)
14595 maybe_warn_cpp0x ("extended initializer lists");
14596 init = cp_parser_braced_list (parser, non_constant_p);
14597 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14601 /* Anything else is an error. */
14602 cp_parser_error (parser, "expected initializer");
14603 init = error_mark_node;
14609 /* Parse an initializer-clause.
14611 initializer-clause:
14612 assignment-expression
14615 Returns an expression representing the initializer.
14617 If the `assignment-expression' production is used the value
14618 returned is simply a representation for the expression.
14620 Otherwise, calls cp_parser_braced_list. */
14623 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14627 /* Assume the expression is constant. */
14628 *non_constant_p = false;
14630 /* If it is not a `{', then we are looking at an
14631 assignment-expression. */
14632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14635 = cp_parser_constant_expression (parser,
14636 /*allow_non_constant_p=*/true,
14638 if (!*non_constant_p)
14639 initializer = fold_non_dependent_expr (initializer);
14642 initializer = cp_parser_braced_list (parser, non_constant_p);
14644 return initializer;
14647 /* Parse a brace-enclosed initializer list.
14650 { initializer-list , [opt] }
14653 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14654 the elements of the initializer-list (or NULL, if the last
14655 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14656 NULL_TREE. There is no way to detect whether or not the optional
14657 trailing `,' was provided. NON_CONSTANT_P is as for
14658 cp_parser_initializer. */
14661 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14665 /* Consume the `{' token. */
14666 cp_lexer_consume_token (parser->lexer);
14667 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14668 initializer = make_node (CONSTRUCTOR);
14669 /* If it's not a `}', then there is a non-trivial initializer. */
14670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14672 /* Parse the initializer list. */
14673 CONSTRUCTOR_ELTS (initializer)
14674 = cp_parser_initializer_list (parser, non_constant_p);
14675 /* A trailing `,' token is allowed. */
14676 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14677 cp_lexer_consume_token (parser->lexer);
14679 /* Now, there should be a trailing `}'. */
14680 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14681 TREE_TYPE (initializer) = init_list_type_node;
14682 return initializer;
14685 /* Parse an initializer-list.
14688 initializer-clause ... [opt]
14689 initializer-list , initializer-clause ... [opt]
14694 identifier : initializer-clause
14695 initializer-list, identifier : initializer-clause
14697 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14698 for the initializer. If the INDEX of the elt is non-NULL, it is the
14699 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14700 as for cp_parser_initializer. */
14702 static VEC(constructor_elt,gc) *
14703 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14705 VEC(constructor_elt,gc) *v = NULL;
14707 /* Assume all of the expressions are constant. */
14708 *non_constant_p = false;
14710 /* Parse the rest of the list. */
14716 bool clause_non_constant_p;
14718 /* If the next token is an identifier and the following one is a
14719 colon, we are looking at the GNU designated-initializer
14721 if (cp_parser_allow_gnu_extensions_p (parser)
14722 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14723 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14725 /* Warn the user that they are using an extension. */
14726 pedwarn (input_location, OPT_pedantic,
14727 "ISO C++ does not allow designated initializers");
14728 /* Consume the identifier. */
14729 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14730 /* Consume the `:'. */
14731 cp_lexer_consume_token (parser->lexer);
14734 identifier = NULL_TREE;
14736 /* Parse the initializer. */
14737 initializer = cp_parser_initializer_clause (parser,
14738 &clause_non_constant_p);
14739 /* If any clause is non-constant, so is the entire initializer. */
14740 if (clause_non_constant_p)
14741 *non_constant_p = true;
14743 /* If we have an ellipsis, this is an initializer pack
14745 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14747 /* Consume the `...'. */
14748 cp_lexer_consume_token (parser->lexer);
14750 /* Turn the initializer into an initializer expansion. */
14751 initializer = make_pack_expansion (initializer);
14754 /* Add it to the vector. */
14755 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14757 /* If the next token is not a comma, we have reached the end of
14759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14762 /* Peek at the next token. */
14763 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14764 /* If the next token is a `}', then we're still done. An
14765 initializer-clause can have a trailing `,' after the
14766 initializer-list and before the closing `}'. */
14767 if (token->type == CPP_CLOSE_BRACE)
14770 /* Consume the `,' token. */
14771 cp_lexer_consume_token (parser->lexer);
14777 /* Classes [gram.class] */
14779 /* Parse a class-name.
14785 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14786 to indicate that names looked up in dependent types should be
14787 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14788 keyword has been used to indicate that the name that appears next
14789 is a template. TAG_TYPE indicates the explicit tag given before
14790 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14791 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14792 is the class being defined in a class-head.
14794 Returns the TYPE_DECL representing the class. */
14797 cp_parser_class_name (cp_parser *parser,
14798 bool typename_keyword_p,
14799 bool template_keyword_p,
14800 enum tag_types tag_type,
14801 bool check_dependency_p,
14803 bool is_declaration)
14809 tree identifier = NULL_TREE;
14811 /* All class-names start with an identifier. */
14812 token = cp_lexer_peek_token (parser->lexer);
14813 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14815 cp_parser_error (parser, "expected class-name");
14816 return error_mark_node;
14819 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14820 to a template-id, so we save it here. */
14821 scope = parser->scope;
14822 if (scope == error_mark_node)
14823 return error_mark_node;
14825 /* Any name names a type if we're following the `typename' keyword
14826 in a qualified name where the enclosing scope is type-dependent. */
14827 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14828 && dependent_type_p (scope));
14829 /* Handle the common case (an identifier, but not a template-id)
14831 if (token->type == CPP_NAME
14832 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14834 cp_token *identifier_token;
14837 /* Look for the identifier. */
14838 identifier_token = cp_lexer_peek_token (parser->lexer);
14839 ambiguous_p = identifier_token->ambiguous_p;
14840 identifier = cp_parser_identifier (parser);
14841 /* If the next token isn't an identifier, we are certainly not
14842 looking at a class-name. */
14843 if (identifier == error_mark_node)
14844 decl = error_mark_node;
14845 /* If we know this is a type-name, there's no need to look it
14847 else if (typename_p)
14851 tree ambiguous_decls;
14852 /* If we already know that this lookup is ambiguous, then
14853 we've already issued an error message; there's no reason
14857 cp_parser_simulate_error (parser);
14858 return error_mark_node;
14860 /* If the next token is a `::', then the name must be a type
14863 [basic.lookup.qual]
14865 During the lookup for a name preceding the :: scope
14866 resolution operator, object, function, and enumerator
14867 names are ignored. */
14868 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14869 tag_type = typename_type;
14870 /* Look up the name. */
14871 decl = cp_parser_lookup_name (parser, identifier,
14873 /*is_template=*/false,
14874 /*is_namespace=*/false,
14875 check_dependency_p,
14877 identifier_token->location);
14878 if (ambiguous_decls)
14880 error ("%Hreference to %qD is ambiguous",
14881 &identifier_token->location, identifier);
14882 print_candidates (ambiguous_decls);
14883 if (cp_parser_parsing_tentatively (parser))
14885 identifier_token->ambiguous_p = true;
14886 cp_parser_simulate_error (parser);
14888 return error_mark_node;
14894 /* Try a template-id. */
14895 decl = cp_parser_template_id (parser, template_keyword_p,
14896 check_dependency_p,
14898 if (decl == error_mark_node)
14899 return error_mark_node;
14902 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14904 /* If this is a typename, create a TYPENAME_TYPE. */
14905 if (typename_p && decl != error_mark_node)
14907 decl = make_typename_type (scope, decl, typename_type,
14908 /*complain=*/tf_error);
14909 if (decl != error_mark_node)
14910 decl = TYPE_NAME (decl);
14913 /* Check to see that it is really the name of a class. */
14914 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14915 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14916 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14917 /* Situations like this:
14919 template <typename T> struct A {
14920 typename T::template X<int>::I i;
14923 are problematic. Is `T::template X<int>' a class-name? The
14924 standard does not seem to be definitive, but there is no other
14925 valid interpretation of the following `::'. Therefore, those
14926 names are considered class-names. */
14928 decl = make_typename_type (scope, decl, tag_type, tf_error);
14929 if (decl != error_mark_node)
14930 decl = TYPE_NAME (decl);
14932 else if (TREE_CODE (decl) != TYPE_DECL
14933 || TREE_TYPE (decl) == error_mark_node
14934 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14935 decl = error_mark_node;
14937 if (decl == error_mark_node)
14938 cp_parser_error (parser, "expected class-name");
14939 else if (identifier && !parser->scope)
14940 maybe_note_name_used_in_class (identifier, decl);
14945 /* Parse a class-specifier.
14948 class-head { member-specification [opt] }
14950 Returns the TREE_TYPE representing the class. */
14953 cp_parser_class_specifier (cp_parser* parser)
14956 tree attributes = NULL_TREE;
14957 bool nested_name_specifier_p;
14958 unsigned saved_num_template_parameter_lists;
14959 bool saved_in_function_body;
14960 bool saved_in_unbraced_linkage_specification_p;
14961 tree old_scope = NULL_TREE;
14962 tree scope = NULL_TREE;
14965 push_deferring_access_checks (dk_no_deferred);
14967 /* Parse the class-head. */
14968 type = cp_parser_class_head (parser,
14969 &nested_name_specifier_p,
14972 /* If the class-head was a semantic disaster, skip the entire body
14976 cp_parser_skip_to_end_of_block_or_statement (parser);
14977 pop_deferring_access_checks ();
14978 return error_mark_node;
14981 /* Look for the `{'. */
14982 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14984 pop_deferring_access_checks ();
14985 return error_mark_node;
14988 /* Process the base classes. If they're invalid, skip the
14989 entire class body. */
14990 if (!xref_basetypes (type, bases))
14992 /* Consuming the closing brace yields better error messages
14994 if (cp_parser_skip_to_closing_brace (parser))
14995 cp_lexer_consume_token (parser->lexer);
14996 pop_deferring_access_checks ();
14997 return error_mark_node;
15000 /* Issue an error message if type-definitions are forbidden here. */
15001 cp_parser_check_type_definition (parser);
15002 /* Remember that we are defining one more class. */
15003 ++parser->num_classes_being_defined;
15004 /* Inside the class, surrounding template-parameter-lists do not
15006 saved_num_template_parameter_lists
15007 = parser->num_template_parameter_lists;
15008 parser->num_template_parameter_lists = 0;
15009 /* We are not in a function body. */
15010 saved_in_function_body = parser->in_function_body;
15011 parser->in_function_body = false;
15012 /* We are not immediately inside an extern "lang" block. */
15013 saved_in_unbraced_linkage_specification_p
15014 = parser->in_unbraced_linkage_specification_p;
15015 parser->in_unbraced_linkage_specification_p = false;
15017 /* Start the class. */
15018 if (nested_name_specifier_p)
15020 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15021 old_scope = push_inner_scope (scope);
15023 type = begin_class_definition (type, attributes);
15025 if (type == error_mark_node)
15026 /* If the type is erroneous, skip the entire body of the class. */
15027 cp_parser_skip_to_closing_brace (parser);
15029 /* Parse the member-specification. */
15030 cp_parser_member_specification_opt (parser);
15032 /* Look for the trailing `}'. */
15033 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15034 /* Look for trailing attributes to apply to this class. */
15035 if (cp_parser_allow_gnu_extensions_p (parser))
15036 attributes = cp_parser_attributes_opt (parser);
15037 if (type != error_mark_node)
15038 type = finish_struct (type, attributes);
15039 if (nested_name_specifier_p)
15040 pop_inner_scope (old_scope, scope);
15041 /* If this class is not itself within the scope of another class,
15042 then we need to parse the bodies of all of the queued function
15043 definitions. Note that the queued functions defined in a class
15044 are not always processed immediately following the
15045 class-specifier for that class. Consider:
15048 struct B { void f() { sizeof (A); } };
15051 If `f' were processed before the processing of `A' were
15052 completed, there would be no way to compute the size of `A'.
15053 Note that the nesting we are interested in here is lexical --
15054 not the semantic nesting given by TYPE_CONTEXT. In particular,
15057 struct A { struct B; };
15058 struct A::B { void f() { } };
15060 there is no need to delay the parsing of `A::B::f'. */
15061 if (--parser->num_classes_being_defined == 0)
15065 tree class_type = NULL_TREE;
15066 tree pushed_scope = NULL_TREE;
15068 /* In a first pass, parse default arguments to the functions.
15069 Then, in a second pass, parse the bodies of the functions.
15070 This two-phased approach handles cases like:
15078 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15079 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15080 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15081 TREE_PURPOSE (parser->unparsed_functions_queues)
15082 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15084 fn = TREE_VALUE (queue_entry);
15085 /* If there are default arguments that have not yet been processed,
15086 take care of them now. */
15087 if (class_type != TREE_PURPOSE (queue_entry))
15090 pop_scope (pushed_scope);
15091 class_type = TREE_PURPOSE (queue_entry);
15092 pushed_scope = push_scope (class_type);
15094 /* Make sure that any template parameters are in scope. */
15095 maybe_begin_member_template_processing (fn);
15096 /* Parse the default argument expressions. */
15097 cp_parser_late_parsing_default_args (parser, fn);
15098 /* Remove any template parameters from the symbol table. */
15099 maybe_end_member_template_processing ();
15102 pop_scope (pushed_scope);
15103 /* Now parse the body of the functions. */
15104 for (TREE_VALUE (parser->unparsed_functions_queues)
15105 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15106 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15107 TREE_VALUE (parser->unparsed_functions_queues)
15108 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15110 /* Figure out which function we need to process. */
15111 fn = TREE_VALUE (queue_entry);
15112 /* Parse the function. */
15113 cp_parser_late_parsing_for_member (parser, fn);
15117 /* Put back any saved access checks. */
15118 pop_deferring_access_checks ();
15120 /* Restore saved state. */
15121 parser->in_function_body = saved_in_function_body;
15122 parser->num_template_parameter_lists
15123 = saved_num_template_parameter_lists;
15124 parser->in_unbraced_linkage_specification_p
15125 = saved_in_unbraced_linkage_specification_p;
15130 /* Parse a class-head.
15133 class-key identifier [opt] base-clause [opt]
15134 class-key nested-name-specifier identifier base-clause [opt]
15135 class-key nested-name-specifier [opt] template-id
15139 class-key attributes identifier [opt] base-clause [opt]
15140 class-key attributes nested-name-specifier identifier base-clause [opt]
15141 class-key attributes nested-name-specifier [opt] template-id
15144 Upon return BASES is initialized to the list of base classes (or
15145 NULL, if there are none) in the same form returned by
15146 cp_parser_base_clause.
15148 Returns the TYPE of the indicated class. Sets
15149 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15150 involving a nested-name-specifier was used, and FALSE otherwise.
15152 Returns error_mark_node if this is not a class-head.
15154 Returns NULL_TREE if the class-head is syntactically valid, but
15155 semantically invalid in a way that means we should skip the entire
15156 body of the class. */
15159 cp_parser_class_head (cp_parser* parser,
15160 bool* nested_name_specifier_p,
15161 tree *attributes_p,
15164 tree nested_name_specifier;
15165 enum tag_types class_key;
15166 tree id = NULL_TREE;
15167 tree type = NULL_TREE;
15169 bool template_id_p = false;
15170 bool qualified_p = false;
15171 bool invalid_nested_name_p = false;
15172 bool invalid_explicit_specialization_p = false;
15173 tree pushed_scope = NULL_TREE;
15174 unsigned num_templates;
15175 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15176 /* Assume no nested-name-specifier will be present. */
15177 *nested_name_specifier_p = false;
15178 /* Assume no template parameter lists will be used in defining the
15182 *bases = NULL_TREE;
15184 /* Look for the class-key. */
15185 class_key = cp_parser_class_key (parser);
15186 if (class_key == none_type)
15187 return error_mark_node;
15189 /* Parse the attributes. */
15190 attributes = cp_parser_attributes_opt (parser);
15192 /* If the next token is `::', that is invalid -- but sometimes
15193 people do try to write:
15197 Handle this gracefully by accepting the extra qualifier, and then
15198 issuing an error about it later if this really is a
15199 class-head. If it turns out just to be an elaborated type
15200 specifier, remain silent. */
15201 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15202 qualified_p = true;
15204 push_deferring_access_checks (dk_no_check);
15206 /* Determine the name of the class. Begin by looking for an
15207 optional nested-name-specifier. */
15208 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15209 nested_name_specifier
15210 = cp_parser_nested_name_specifier_opt (parser,
15211 /*typename_keyword_p=*/false,
15212 /*check_dependency_p=*/false,
15214 /*is_declaration=*/false);
15215 /* If there was a nested-name-specifier, then there *must* be an
15217 if (nested_name_specifier)
15219 type_start_token = cp_lexer_peek_token (parser->lexer);
15220 /* Although the grammar says `identifier', it really means
15221 `class-name' or `template-name'. You are only allowed to
15222 define a class that has already been declared with this
15225 The proposed resolution for Core Issue 180 says that wherever
15226 you see `class T::X' you should treat `X' as a type-name.
15228 It is OK to define an inaccessible class; for example:
15230 class A { class B; };
15233 We do not know if we will see a class-name, or a
15234 template-name. We look for a class-name first, in case the
15235 class-name is a template-id; if we looked for the
15236 template-name first we would stop after the template-name. */
15237 cp_parser_parse_tentatively (parser);
15238 type = cp_parser_class_name (parser,
15239 /*typename_keyword_p=*/false,
15240 /*template_keyword_p=*/false,
15242 /*check_dependency_p=*/false,
15243 /*class_head_p=*/true,
15244 /*is_declaration=*/false);
15245 /* If that didn't work, ignore the nested-name-specifier. */
15246 if (!cp_parser_parse_definitely (parser))
15248 invalid_nested_name_p = true;
15249 type_start_token = cp_lexer_peek_token (parser->lexer);
15250 id = cp_parser_identifier (parser);
15251 if (id == error_mark_node)
15254 /* If we could not find a corresponding TYPE, treat this
15255 declaration like an unqualified declaration. */
15256 if (type == error_mark_node)
15257 nested_name_specifier = NULL_TREE;
15258 /* Otherwise, count the number of templates used in TYPE and its
15259 containing scopes. */
15264 for (scope = TREE_TYPE (type);
15265 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15266 scope = (TYPE_P (scope)
15267 ? TYPE_CONTEXT (scope)
15268 : DECL_CONTEXT (scope)))
15270 && CLASS_TYPE_P (scope)
15271 && CLASSTYPE_TEMPLATE_INFO (scope)
15272 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15273 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15277 /* Otherwise, the identifier is optional. */
15280 /* We don't know whether what comes next is a template-id,
15281 an identifier, or nothing at all. */
15282 cp_parser_parse_tentatively (parser);
15283 /* Check for a template-id. */
15284 type_start_token = cp_lexer_peek_token (parser->lexer);
15285 id = cp_parser_template_id (parser,
15286 /*template_keyword_p=*/false,
15287 /*check_dependency_p=*/true,
15288 /*is_declaration=*/true);
15289 /* If that didn't work, it could still be an identifier. */
15290 if (!cp_parser_parse_definitely (parser))
15292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15294 type_start_token = cp_lexer_peek_token (parser->lexer);
15295 id = cp_parser_identifier (parser);
15302 template_id_p = true;
15307 pop_deferring_access_checks ();
15310 cp_parser_check_for_invalid_template_id (parser, id,
15311 type_start_token->location);
15313 /* If it's not a `:' or a `{' then we can't really be looking at a
15314 class-head, since a class-head only appears as part of a
15315 class-specifier. We have to detect this situation before calling
15316 xref_tag, since that has irreversible side-effects. */
15317 if (!cp_parser_next_token_starts_class_definition_p (parser))
15319 cp_parser_error (parser, "expected %<{%> or %<:%>");
15320 return error_mark_node;
15323 /* At this point, we're going ahead with the class-specifier, even
15324 if some other problem occurs. */
15325 cp_parser_commit_to_tentative_parse (parser);
15326 /* Issue the error about the overly-qualified name now. */
15329 cp_parser_error (parser,
15330 "global qualification of class name is invalid");
15331 return error_mark_node;
15333 else if (invalid_nested_name_p)
15335 cp_parser_error (parser,
15336 "qualified name does not name a class");
15337 return error_mark_node;
15339 else if (nested_name_specifier)
15343 /* Reject typedef-names in class heads. */
15344 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15346 error ("%Hinvalid class name in declaration of %qD",
15347 &type_start_token->location, type);
15352 /* Figure out in what scope the declaration is being placed. */
15353 scope = current_scope ();
15354 /* If that scope does not contain the scope in which the
15355 class was originally declared, the program is invalid. */
15356 if (scope && !is_ancestor (scope, nested_name_specifier))
15358 if (at_namespace_scope_p ())
15359 error ("%Hdeclaration of %qD in namespace %qD which does not "
15361 &type_start_token->location,
15362 type, scope, nested_name_specifier);
15364 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15365 &type_start_token->location,
15366 type, scope, nested_name_specifier);
15372 A declarator-id shall not be qualified except for the
15373 definition of a ... nested class outside of its class
15374 ... [or] the definition or explicit instantiation of a
15375 class member of a namespace outside of its namespace. */
15376 if (scope == nested_name_specifier)
15378 permerror (input_location, "%Hextra qualification not allowed",
15379 &nested_name_specifier_token_start->location);
15380 nested_name_specifier = NULL_TREE;
15384 /* An explicit-specialization must be preceded by "template <>". If
15385 it is not, try to recover gracefully. */
15386 if (at_namespace_scope_p ()
15387 && parser->num_template_parameter_lists == 0
15390 error ("%Han explicit specialization must be preceded by %<template <>%>",
15391 &type_start_token->location);
15392 invalid_explicit_specialization_p = true;
15393 /* Take the same action that would have been taken by
15394 cp_parser_explicit_specialization. */
15395 ++parser->num_template_parameter_lists;
15396 begin_specialization ();
15398 /* There must be no "return" statements between this point and the
15399 end of this function; set "type "to the correct return value and
15400 use "goto done;" to return. */
15401 /* Make sure that the right number of template parameters were
15403 if (!cp_parser_check_template_parameters (parser, num_templates,
15404 type_start_token->location,
15405 /*declarator=*/NULL))
15407 /* If something went wrong, there is no point in even trying to
15408 process the class-definition. */
15413 /* Look up the type. */
15416 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15417 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15418 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15420 error ("%Hfunction template %qD redeclared as a class template",
15421 &type_start_token->location, id);
15422 type = error_mark_node;
15426 type = TREE_TYPE (id);
15427 type = maybe_process_partial_specialization (type);
15429 if (nested_name_specifier)
15430 pushed_scope = push_scope (nested_name_specifier);
15432 else if (nested_name_specifier)
15438 template <typename T> struct S { struct T };
15439 template <typename T> struct S<T>::T { };
15441 we will get a TYPENAME_TYPE when processing the definition of
15442 `S::T'. We need to resolve it to the actual type before we
15443 try to define it. */
15444 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15446 class_type = resolve_typename_type (TREE_TYPE (type),
15447 /*only_current_p=*/false);
15448 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15449 type = TYPE_NAME (class_type);
15452 cp_parser_error (parser, "could not resolve typename type");
15453 type = error_mark_node;
15457 if (maybe_process_partial_specialization (TREE_TYPE (type))
15458 == error_mark_node)
15464 class_type = current_class_type;
15465 /* Enter the scope indicated by the nested-name-specifier. */
15466 pushed_scope = push_scope (nested_name_specifier);
15467 /* Get the canonical version of this type. */
15468 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15469 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15470 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15472 type = push_template_decl (type);
15473 if (type == error_mark_node)
15480 type = TREE_TYPE (type);
15481 *nested_name_specifier_p = true;
15483 else /* The name is not a nested name. */
15485 /* If the class was unnamed, create a dummy name. */
15487 id = make_anon_name ();
15488 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15489 parser->num_template_parameter_lists);
15492 /* Indicate whether this class was declared as a `class' or as a
15494 if (TREE_CODE (type) == RECORD_TYPE)
15495 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15496 cp_parser_check_class_key (class_key, type);
15498 /* If this type was already complete, and we see another definition,
15499 that's an error. */
15500 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15502 error ("%Hredefinition of %q#T",
15503 &type_start_token->location, type);
15504 error ("%Hprevious definition of %q+#T",
15505 &type_start_token->location, type);
15509 else if (type == error_mark_node)
15512 /* We will have entered the scope containing the class; the names of
15513 base classes should be looked up in that context. For example:
15515 struct A { struct B {}; struct C; };
15516 struct A::C : B {};
15520 /* Get the list of base-classes, if there is one. */
15521 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15522 *bases = cp_parser_base_clause (parser);
15525 /* Leave the scope given by the nested-name-specifier. We will
15526 enter the class scope itself while processing the members. */
15528 pop_scope (pushed_scope);
15530 if (invalid_explicit_specialization_p)
15532 end_specialization ();
15533 --parser->num_template_parameter_lists;
15535 *attributes_p = attributes;
15539 /* Parse a class-key.
15546 Returns the kind of class-key specified, or none_type to indicate
15549 static enum tag_types
15550 cp_parser_class_key (cp_parser* parser)
15553 enum tag_types tag_type;
15555 /* Look for the class-key. */
15556 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15560 /* Check to see if the TOKEN is a class-key. */
15561 tag_type = cp_parser_token_is_class_key (token);
15563 cp_parser_error (parser, "expected class-key");
15567 /* Parse an (optional) member-specification.
15569 member-specification:
15570 member-declaration member-specification [opt]
15571 access-specifier : member-specification [opt] */
15574 cp_parser_member_specification_opt (cp_parser* parser)
15581 /* Peek at the next token. */
15582 token = cp_lexer_peek_token (parser->lexer);
15583 /* If it's a `}', or EOF then we've seen all the members. */
15584 if (token->type == CPP_CLOSE_BRACE
15585 || token->type == CPP_EOF
15586 || token->type == CPP_PRAGMA_EOL)
15589 /* See if this token is a keyword. */
15590 keyword = token->keyword;
15594 case RID_PROTECTED:
15596 /* Consume the access-specifier. */
15597 cp_lexer_consume_token (parser->lexer);
15598 /* Remember which access-specifier is active. */
15599 current_access_specifier = token->u.value;
15600 /* Look for the `:'. */
15601 cp_parser_require (parser, CPP_COLON, "%<:%>");
15605 /* Accept #pragmas at class scope. */
15606 if (token->type == CPP_PRAGMA)
15608 cp_parser_pragma (parser, pragma_external);
15612 /* Otherwise, the next construction must be a
15613 member-declaration. */
15614 cp_parser_member_declaration (parser);
15619 /* Parse a member-declaration.
15621 member-declaration:
15622 decl-specifier-seq [opt] member-declarator-list [opt] ;
15623 function-definition ; [opt]
15624 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15626 template-declaration
15628 member-declarator-list:
15630 member-declarator-list , member-declarator
15633 declarator pure-specifier [opt]
15634 declarator constant-initializer [opt]
15635 identifier [opt] : constant-expression
15639 member-declaration:
15640 __extension__ member-declaration
15643 declarator attributes [opt] pure-specifier [opt]
15644 declarator attributes [opt] constant-initializer [opt]
15645 identifier [opt] attributes [opt] : constant-expression
15649 member-declaration:
15650 static_assert-declaration */
15653 cp_parser_member_declaration (cp_parser* parser)
15655 cp_decl_specifier_seq decl_specifiers;
15656 tree prefix_attributes;
15658 int declares_class_or_enum;
15660 cp_token *token = NULL;
15661 cp_token *decl_spec_token_start = NULL;
15662 cp_token *initializer_token_start = NULL;
15663 int saved_pedantic;
15665 /* Check for the `__extension__' keyword. */
15666 if (cp_parser_extension_opt (parser, &saved_pedantic))
15669 cp_parser_member_declaration (parser);
15670 /* Restore the old value of the PEDANTIC flag. */
15671 pedantic = saved_pedantic;
15676 /* Check for a template-declaration. */
15677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15679 /* An explicit specialization here is an error condition, and we
15680 expect the specialization handler to detect and report this. */
15681 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15682 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15683 cp_parser_explicit_specialization (parser);
15685 cp_parser_template_declaration (parser, /*member_p=*/true);
15690 /* Check for a using-declaration. */
15691 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15693 /* Parse the using-declaration. */
15694 cp_parser_using_declaration (parser,
15695 /*access_declaration_p=*/false);
15699 /* Check for @defs. */
15700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15703 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15704 ivar = ivar_chains;
15708 ivar = TREE_CHAIN (member);
15709 TREE_CHAIN (member) = NULL_TREE;
15710 finish_member_declaration (member);
15715 /* If the next token is `static_assert' we have a static assertion. */
15716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15718 cp_parser_static_assert (parser, /*member_p=*/true);
15722 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15725 /* Parse the decl-specifier-seq. */
15726 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15727 cp_parser_decl_specifier_seq (parser,
15728 CP_PARSER_FLAGS_OPTIONAL,
15730 &declares_class_or_enum);
15731 prefix_attributes = decl_specifiers.attributes;
15732 decl_specifiers.attributes = NULL_TREE;
15733 /* Check for an invalid type-name. */
15734 if (!decl_specifiers.type
15735 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15737 /* If there is no declarator, then the decl-specifier-seq should
15739 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15741 /* If there was no decl-specifier-seq, and the next token is a
15742 `;', then we have something like:
15748 Each member-declaration shall declare at least one member
15749 name of the class. */
15750 if (!decl_specifiers.any_specifiers_p)
15752 cp_token *token = cp_lexer_peek_token (parser->lexer);
15753 if (!in_system_header_at (token->location))
15754 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15760 /* See if this declaration is a friend. */
15761 friend_p = cp_parser_friend_p (&decl_specifiers);
15762 /* If there were decl-specifiers, check to see if there was
15763 a class-declaration. */
15764 type = check_tag_decl (&decl_specifiers);
15765 /* Nested classes have already been added to the class, but
15766 a `friend' needs to be explicitly registered. */
15769 /* If the `friend' keyword was present, the friend must
15770 be introduced with a class-key. */
15771 if (!declares_class_or_enum)
15772 error ("%Ha class-key must be used when declaring a friend",
15773 &decl_spec_token_start->location);
15776 template <typename T> struct A {
15777 friend struct A<T>::B;
15780 A<T>::B will be represented by a TYPENAME_TYPE, and
15781 therefore not recognized by check_tag_decl. */
15783 && decl_specifiers.type
15784 && TYPE_P (decl_specifiers.type))
15785 type = decl_specifiers.type;
15786 if (!type || !TYPE_P (type))
15787 error ("%Hfriend declaration does not name a class or "
15788 "function", &decl_spec_token_start->location);
15790 make_friend_class (current_class_type, type,
15791 /*complain=*/true);
15793 /* If there is no TYPE, an error message will already have
15795 else if (!type || type == error_mark_node)
15797 /* An anonymous aggregate has to be handled specially; such
15798 a declaration really declares a data member (with a
15799 particular type), as opposed to a nested class. */
15800 else if (ANON_AGGR_TYPE_P (type))
15802 /* Remove constructors and such from TYPE, now that we
15803 know it is an anonymous aggregate. */
15804 fixup_anonymous_aggr (type);
15805 /* And make the corresponding data member. */
15806 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15807 /* Add it to the class. */
15808 finish_member_declaration (decl);
15811 cp_parser_check_access_in_redeclaration
15813 decl_spec_token_start->location);
15818 /* See if these declarations will be friends. */
15819 friend_p = cp_parser_friend_p (&decl_specifiers);
15821 /* Keep going until we hit the `;' at the end of the
15823 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15825 tree attributes = NULL_TREE;
15826 tree first_attribute;
15828 /* Peek at the next token. */
15829 token = cp_lexer_peek_token (parser->lexer);
15831 /* Check for a bitfield declaration. */
15832 if (token->type == CPP_COLON
15833 || (token->type == CPP_NAME
15834 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15840 /* Get the name of the bitfield. Note that we cannot just
15841 check TOKEN here because it may have been invalidated by
15842 the call to cp_lexer_peek_nth_token above. */
15843 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15844 identifier = cp_parser_identifier (parser);
15846 identifier = NULL_TREE;
15848 /* Consume the `:' token. */
15849 cp_lexer_consume_token (parser->lexer);
15850 /* Get the width of the bitfield. */
15852 = cp_parser_constant_expression (parser,
15853 /*allow_non_constant=*/false,
15856 /* Look for attributes that apply to the bitfield. */
15857 attributes = cp_parser_attributes_opt (parser);
15858 /* Remember which attributes are prefix attributes and
15860 first_attribute = attributes;
15861 /* Combine the attributes. */
15862 attributes = chainon (prefix_attributes, attributes);
15864 /* Create the bitfield declaration. */
15865 decl = grokbitfield (identifier
15866 ? make_id_declarator (NULL_TREE,
15876 cp_declarator *declarator;
15878 tree asm_specification;
15879 int ctor_dtor_or_conv_p;
15881 /* Parse the declarator. */
15883 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15884 &ctor_dtor_or_conv_p,
15885 /*parenthesized_p=*/NULL,
15886 /*member_p=*/true);
15888 /* If something went wrong parsing the declarator, make sure
15889 that we at least consume some tokens. */
15890 if (declarator == cp_error_declarator)
15892 /* Skip to the end of the statement. */
15893 cp_parser_skip_to_end_of_statement (parser);
15894 /* If the next token is not a semicolon, that is
15895 probably because we just skipped over the body of
15896 a function. So, we consume a semicolon if
15897 present, but do not issue an error message if it
15899 if (cp_lexer_next_token_is (parser->lexer,
15901 cp_lexer_consume_token (parser->lexer);
15905 if (declares_class_or_enum & 2)
15906 cp_parser_check_for_definition_in_return_type
15907 (declarator, decl_specifiers.type,
15908 decl_specifiers.type_location);
15910 /* Look for an asm-specification. */
15911 asm_specification = cp_parser_asm_specification_opt (parser);
15912 /* Look for attributes that apply to the declaration. */
15913 attributes = cp_parser_attributes_opt (parser);
15914 /* Remember which attributes are prefix attributes and
15916 first_attribute = attributes;
15917 /* Combine the attributes. */
15918 attributes = chainon (prefix_attributes, attributes);
15920 /* If it's an `=', then we have a constant-initializer or a
15921 pure-specifier. It is not correct to parse the
15922 initializer before registering the member declaration
15923 since the member declaration should be in scope while
15924 its initializer is processed. However, the rest of the
15925 front end does not yet provide an interface that allows
15926 us to handle this correctly. */
15927 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15931 A pure-specifier shall be used only in the declaration of
15932 a virtual function.
15934 A member-declarator can contain a constant-initializer
15935 only if it declares a static member of integral or
15938 Therefore, if the DECLARATOR is for a function, we look
15939 for a pure-specifier; otherwise, we look for a
15940 constant-initializer. When we call `grokfield', it will
15941 perform more stringent semantics checks. */
15942 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15943 if (function_declarator_p (declarator))
15944 initializer = cp_parser_pure_specifier (parser);
15946 /* Parse the initializer. */
15947 initializer = cp_parser_constant_initializer (parser);
15949 /* Otherwise, there is no initializer. */
15951 initializer = NULL_TREE;
15953 /* See if we are probably looking at a function
15954 definition. We are certainly not looking at a
15955 member-declarator. Calling `grokfield' has
15956 side-effects, so we must not do it unless we are sure
15957 that we are looking at a member-declarator. */
15958 if (cp_parser_token_starts_function_definition_p
15959 (cp_lexer_peek_token (parser->lexer)))
15961 /* The grammar does not allow a pure-specifier to be
15962 used when a member function is defined. (It is
15963 possible that this fact is an oversight in the
15964 standard, since a pure function may be defined
15965 outside of the class-specifier. */
15967 error ("%Hpure-specifier on function-definition",
15968 &initializer_token_start->location);
15969 decl = cp_parser_save_member_function_body (parser,
15973 /* If the member was not a friend, declare it here. */
15975 finish_member_declaration (decl);
15976 /* Peek at the next token. */
15977 token = cp_lexer_peek_token (parser->lexer);
15978 /* If the next token is a semicolon, consume it. */
15979 if (token->type == CPP_SEMICOLON)
15980 cp_lexer_consume_token (parser->lexer);
15984 if (declarator->kind == cdk_function)
15985 declarator->id_loc = token->location;
15986 /* Create the declaration. */
15987 decl = grokfield (declarator, &decl_specifiers,
15988 initializer, /*init_const_expr_p=*/true,
15993 /* Reset PREFIX_ATTRIBUTES. */
15994 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15995 attributes = TREE_CHAIN (attributes);
15997 TREE_CHAIN (attributes) = NULL_TREE;
15999 /* If there is any qualification still in effect, clear it
16000 now; we will be starting fresh with the next declarator. */
16001 parser->scope = NULL_TREE;
16002 parser->qualifying_scope = NULL_TREE;
16003 parser->object_scope = NULL_TREE;
16004 /* If it's a `,', then there are more declarators. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16006 cp_lexer_consume_token (parser->lexer);
16007 /* If the next token isn't a `;', then we have a parse error. */
16008 else if (cp_lexer_next_token_is_not (parser->lexer,
16011 cp_parser_error (parser, "expected %<;%>");
16012 /* Skip tokens until we find a `;'. */
16013 cp_parser_skip_to_end_of_statement (parser);
16020 /* Add DECL to the list of members. */
16022 finish_member_declaration (decl);
16024 if (TREE_CODE (decl) == FUNCTION_DECL)
16025 cp_parser_save_default_args (parser, decl);
16030 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16033 /* Parse a pure-specifier.
16038 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16039 Otherwise, ERROR_MARK_NODE is returned. */
16042 cp_parser_pure_specifier (cp_parser* parser)
16046 /* Look for the `=' token. */
16047 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16048 return error_mark_node;
16049 /* Look for the `0' token. */
16050 token = cp_lexer_peek_token (parser->lexer);
16052 if (token->type == CPP_EOF
16053 || token->type == CPP_PRAGMA_EOL)
16054 return error_mark_node;
16056 cp_lexer_consume_token (parser->lexer);
16058 /* Accept = default or = delete in c++0x mode. */
16059 if (token->keyword == RID_DEFAULT
16060 || token->keyword == RID_DELETE)
16062 maybe_warn_cpp0x ("defaulted and deleted functions");
16063 return token->u.value;
16066 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16067 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16069 cp_parser_error (parser,
16070 "invalid pure specifier (only %<= 0%> is allowed)");
16071 cp_parser_skip_to_end_of_statement (parser);
16072 return error_mark_node;
16074 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16076 error ("%Htemplates may not be %<virtual%>", &token->location);
16077 return error_mark_node;
16080 return integer_zero_node;
16083 /* Parse a constant-initializer.
16085 constant-initializer:
16086 = constant-expression
16088 Returns a representation of the constant-expression. */
16091 cp_parser_constant_initializer (cp_parser* parser)
16093 /* Look for the `=' token. */
16094 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16095 return error_mark_node;
16097 /* It is invalid to write:
16099 struct S { static const int i = { 7 }; };
16102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16104 cp_parser_error (parser,
16105 "a brace-enclosed initializer is not allowed here");
16106 /* Consume the opening brace. */
16107 cp_lexer_consume_token (parser->lexer);
16108 /* Skip the initializer. */
16109 cp_parser_skip_to_closing_brace (parser);
16110 /* Look for the trailing `}'. */
16111 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16113 return error_mark_node;
16116 return cp_parser_constant_expression (parser,
16117 /*allow_non_constant=*/false,
16121 /* Derived classes [gram.class.derived] */
16123 /* Parse a base-clause.
16126 : base-specifier-list
16128 base-specifier-list:
16129 base-specifier ... [opt]
16130 base-specifier-list , base-specifier ... [opt]
16132 Returns a TREE_LIST representing the base-classes, in the order in
16133 which they were declared. The representation of each node is as
16134 described by cp_parser_base_specifier.
16136 In the case that no bases are specified, this function will return
16137 NULL_TREE, not ERROR_MARK_NODE. */
16140 cp_parser_base_clause (cp_parser* parser)
16142 tree bases = NULL_TREE;
16144 /* Look for the `:' that begins the list. */
16145 cp_parser_require (parser, CPP_COLON, "%<:%>");
16147 /* Scan the base-specifier-list. */
16152 bool pack_expansion_p = false;
16154 /* Look for the base-specifier. */
16155 base = cp_parser_base_specifier (parser);
16156 /* Look for the (optional) ellipsis. */
16157 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16159 /* Consume the `...'. */
16160 cp_lexer_consume_token (parser->lexer);
16162 pack_expansion_p = true;
16165 /* Add BASE to the front of the list. */
16166 if (base != error_mark_node)
16168 if (pack_expansion_p)
16169 /* Make this a pack expansion type. */
16170 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16173 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16175 TREE_CHAIN (base) = bases;
16179 /* Peek at the next token. */
16180 token = cp_lexer_peek_token (parser->lexer);
16181 /* If it's not a comma, then the list is complete. */
16182 if (token->type != CPP_COMMA)
16184 /* Consume the `,'. */
16185 cp_lexer_consume_token (parser->lexer);
16188 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16189 base class had a qualified name. However, the next name that
16190 appears is certainly not qualified. */
16191 parser->scope = NULL_TREE;
16192 parser->qualifying_scope = NULL_TREE;
16193 parser->object_scope = NULL_TREE;
16195 return nreverse (bases);
16198 /* Parse a base-specifier.
16201 :: [opt] nested-name-specifier [opt] class-name
16202 virtual access-specifier [opt] :: [opt] nested-name-specifier
16204 access-specifier virtual [opt] :: [opt] nested-name-specifier
16207 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16208 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16209 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16210 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16213 cp_parser_base_specifier (cp_parser* parser)
16217 bool virtual_p = false;
16218 bool duplicate_virtual_error_issued_p = false;
16219 bool duplicate_access_error_issued_p = false;
16220 bool class_scope_p, template_p;
16221 tree access = access_default_node;
16224 /* Process the optional `virtual' and `access-specifier'. */
16227 /* Peek at the next token. */
16228 token = cp_lexer_peek_token (parser->lexer);
16229 /* Process `virtual'. */
16230 switch (token->keyword)
16233 /* If `virtual' appears more than once, issue an error. */
16234 if (virtual_p && !duplicate_virtual_error_issued_p)
16236 cp_parser_error (parser,
16237 "%<virtual%> specified more than once in base-specified");
16238 duplicate_virtual_error_issued_p = true;
16243 /* Consume the `virtual' token. */
16244 cp_lexer_consume_token (parser->lexer);
16249 case RID_PROTECTED:
16251 /* If more than one access specifier appears, issue an
16253 if (access != access_default_node
16254 && !duplicate_access_error_issued_p)
16256 cp_parser_error (parser,
16257 "more than one access specifier in base-specified");
16258 duplicate_access_error_issued_p = true;
16261 access = ridpointers[(int) token->keyword];
16263 /* Consume the access-specifier. */
16264 cp_lexer_consume_token (parser->lexer);
16273 /* It is not uncommon to see programs mechanically, erroneously, use
16274 the 'typename' keyword to denote (dependent) qualified types
16275 as base classes. */
16276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16278 token = cp_lexer_peek_token (parser->lexer);
16279 if (!processing_template_decl)
16280 error ("%Hkeyword %<typename%> not allowed outside of templates",
16283 error ("%Hkeyword %<typename%> not allowed in this context "
16284 "(the base class is implicitly a type)",
16286 cp_lexer_consume_token (parser->lexer);
16289 /* Look for the optional `::' operator. */
16290 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16291 /* Look for the nested-name-specifier. The simplest way to
16296 The keyword `typename' is not permitted in a base-specifier or
16297 mem-initializer; in these contexts a qualified name that
16298 depends on a template-parameter is implicitly assumed to be a
16301 is to pretend that we have seen the `typename' keyword at this
16303 cp_parser_nested_name_specifier_opt (parser,
16304 /*typename_keyword_p=*/true,
16305 /*check_dependency_p=*/true,
16307 /*is_declaration=*/true);
16308 /* If the base class is given by a qualified name, assume that names
16309 we see are type names or templates, as appropriate. */
16310 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16311 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16313 /* Finally, look for the class-name. */
16314 type = cp_parser_class_name (parser,
16318 /*check_dependency_p=*/true,
16319 /*class_head_p=*/false,
16320 /*is_declaration=*/true);
16322 if (type == error_mark_node)
16323 return error_mark_node;
16325 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16328 /* Exception handling [gram.exception] */
16330 /* Parse an (optional) exception-specification.
16332 exception-specification:
16333 throw ( type-id-list [opt] )
16335 Returns a TREE_LIST representing the exception-specification. The
16336 TREE_VALUE of each node is a type. */
16339 cp_parser_exception_specification_opt (cp_parser* parser)
16344 /* Peek at the next token. */
16345 token = cp_lexer_peek_token (parser->lexer);
16346 /* If it's not `throw', then there's no exception-specification. */
16347 if (!cp_parser_is_keyword (token, RID_THROW))
16350 /* Consume the `throw'. */
16351 cp_lexer_consume_token (parser->lexer);
16353 /* Look for the `('. */
16354 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16356 /* Peek at the next token. */
16357 token = cp_lexer_peek_token (parser->lexer);
16358 /* If it's not a `)', then there is a type-id-list. */
16359 if (token->type != CPP_CLOSE_PAREN)
16361 const char *saved_message;
16363 /* Types may not be defined in an exception-specification. */
16364 saved_message = parser->type_definition_forbidden_message;
16365 parser->type_definition_forbidden_message
16366 = "types may not be defined in an exception-specification";
16367 /* Parse the type-id-list. */
16368 type_id_list = cp_parser_type_id_list (parser);
16369 /* Restore the saved message. */
16370 parser->type_definition_forbidden_message = saved_message;
16373 type_id_list = empty_except_spec;
16375 /* Look for the `)'. */
16376 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16378 return type_id_list;
16381 /* Parse an (optional) type-id-list.
16385 type-id-list , type-id ... [opt]
16387 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16388 in the order that the types were presented. */
16391 cp_parser_type_id_list (cp_parser* parser)
16393 tree types = NULL_TREE;
16400 /* Get the next type-id. */
16401 type = cp_parser_type_id (parser);
16402 /* Parse the optional ellipsis. */
16403 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16405 /* Consume the `...'. */
16406 cp_lexer_consume_token (parser->lexer);
16408 /* Turn the type into a pack expansion expression. */
16409 type = make_pack_expansion (type);
16411 /* Add it to the list. */
16412 types = add_exception_specifier (types, type, /*complain=*/1);
16413 /* Peek at the next token. */
16414 token = cp_lexer_peek_token (parser->lexer);
16415 /* If it is not a `,', we are done. */
16416 if (token->type != CPP_COMMA)
16418 /* Consume the `,'. */
16419 cp_lexer_consume_token (parser->lexer);
16422 return nreverse (types);
16425 /* Parse a try-block.
16428 try compound-statement handler-seq */
16431 cp_parser_try_block (cp_parser* parser)
16435 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16436 try_block = begin_try_block ();
16437 cp_parser_compound_statement (parser, NULL, true);
16438 finish_try_block (try_block);
16439 cp_parser_handler_seq (parser);
16440 finish_handler_sequence (try_block);
16445 /* Parse a function-try-block.
16447 function-try-block:
16448 try ctor-initializer [opt] function-body handler-seq */
16451 cp_parser_function_try_block (cp_parser* parser)
16453 tree compound_stmt;
16455 bool ctor_initializer_p;
16457 /* Look for the `try' keyword. */
16458 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16460 /* Let the rest of the front end know where we are. */
16461 try_block = begin_function_try_block (&compound_stmt);
16462 /* Parse the function-body. */
16464 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16465 /* We're done with the `try' part. */
16466 finish_function_try_block (try_block);
16467 /* Parse the handlers. */
16468 cp_parser_handler_seq (parser);
16469 /* We're done with the handlers. */
16470 finish_function_handler_sequence (try_block, compound_stmt);
16472 return ctor_initializer_p;
16475 /* Parse a handler-seq.
16478 handler handler-seq [opt] */
16481 cp_parser_handler_seq (cp_parser* parser)
16487 /* Parse the handler. */
16488 cp_parser_handler (parser);
16489 /* Peek at the next token. */
16490 token = cp_lexer_peek_token (parser->lexer);
16491 /* If it's not `catch' then there are no more handlers. */
16492 if (!cp_parser_is_keyword (token, RID_CATCH))
16497 /* Parse a handler.
16500 catch ( exception-declaration ) compound-statement */
16503 cp_parser_handler (cp_parser* parser)
16508 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16509 handler = begin_handler ();
16510 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16511 declaration = cp_parser_exception_declaration (parser);
16512 finish_handler_parms (declaration, handler);
16513 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16514 cp_parser_compound_statement (parser, NULL, false);
16515 finish_handler (handler);
16518 /* Parse an exception-declaration.
16520 exception-declaration:
16521 type-specifier-seq declarator
16522 type-specifier-seq abstract-declarator
16526 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16527 ellipsis variant is used. */
16530 cp_parser_exception_declaration (cp_parser* parser)
16532 cp_decl_specifier_seq type_specifiers;
16533 cp_declarator *declarator;
16534 const char *saved_message;
16536 /* If it's an ellipsis, it's easy to handle. */
16537 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16539 /* Consume the `...' token. */
16540 cp_lexer_consume_token (parser->lexer);
16544 /* Types may not be defined in exception-declarations. */
16545 saved_message = parser->type_definition_forbidden_message;
16546 parser->type_definition_forbidden_message
16547 = "types may not be defined in exception-declarations";
16549 /* Parse the type-specifier-seq. */
16550 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16552 /* If it's a `)', then there is no declarator. */
16553 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16556 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16557 /*ctor_dtor_or_conv_p=*/NULL,
16558 /*parenthesized_p=*/NULL,
16559 /*member_p=*/false);
16561 /* Restore the saved message. */
16562 parser->type_definition_forbidden_message = saved_message;
16564 if (!type_specifiers.any_specifiers_p)
16565 return error_mark_node;
16567 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16570 /* Parse a throw-expression.
16573 throw assignment-expression [opt]
16575 Returns a THROW_EXPR representing the throw-expression. */
16578 cp_parser_throw_expression (cp_parser* parser)
16583 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16584 token = cp_lexer_peek_token (parser->lexer);
16585 /* Figure out whether or not there is an assignment-expression
16586 following the "throw" keyword. */
16587 if (token->type == CPP_COMMA
16588 || token->type == CPP_SEMICOLON
16589 || token->type == CPP_CLOSE_PAREN
16590 || token->type == CPP_CLOSE_SQUARE
16591 || token->type == CPP_CLOSE_BRACE
16592 || token->type == CPP_COLON)
16593 expression = NULL_TREE;
16595 expression = cp_parser_assignment_expression (parser,
16596 /*cast_p=*/false, NULL);
16598 return build_throw (expression);
16601 /* GNU Extensions */
16603 /* Parse an (optional) asm-specification.
16606 asm ( string-literal )
16608 If the asm-specification is present, returns a STRING_CST
16609 corresponding to the string-literal. Otherwise, returns
16613 cp_parser_asm_specification_opt (cp_parser* parser)
16616 tree asm_specification;
16618 /* Peek at the next token. */
16619 token = cp_lexer_peek_token (parser->lexer);
16620 /* If the next token isn't the `asm' keyword, then there's no
16621 asm-specification. */
16622 if (!cp_parser_is_keyword (token, RID_ASM))
16625 /* Consume the `asm' token. */
16626 cp_lexer_consume_token (parser->lexer);
16627 /* Look for the `('. */
16628 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16630 /* Look for the string-literal. */
16631 asm_specification = cp_parser_string_literal (parser, false, false);
16633 /* Look for the `)'. */
16634 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16636 return asm_specification;
16639 /* Parse an asm-operand-list.
16643 asm-operand-list , asm-operand
16646 string-literal ( expression )
16647 [ string-literal ] string-literal ( expression )
16649 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16650 each node is the expression. The TREE_PURPOSE is itself a
16651 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16652 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16653 is a STRING_CST for the string literal before the parenthesis. Returns
16654 ERROR_MARK_NODE if any of the operands are invalid. */
16657 cp_parser_asm_operand_list (cp_parser* parser)
16659 tree asm_operands = NULL_TREE;
16660 bool invalid_operands = false;
16664 tree string_literal;
16668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16670 /* Consume the `[' token. */
16671 cp_lexer_consume_token (parser->lexer);
16672 /* Read the operand name. */
16673 name = cp_parser_identifier (parser);
16674 if (name != error_mark_node)
16675 name = build_string (IDENTIFIER_LENGTH (name),
16676 IDENTIFIER_POINTER (name));
16677 /* Look for the closing `]'. */
16678 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16682 /* Look for the string-literal. */
16683 string_literal = cp_parser_string_literal (parser, false, false);
16685 /* Look for the `('. */
16686 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16687 /* Parse the expression. */
16688 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16689 /* Look for the `)'. */
16690 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16692 if (name == error_mark_node
16693 || string_literal == error_mark_node
16694 || expression == error_mark_node)
16695 invalid_operands = true;
16697 /* Add this operand to the list. */
16698 asm_operands = tree_cons (build_tree_list (name, string_literal),
16701 /* If the next token is not a `,', there are no more
16703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16705 /* Consume the `,'. */
16706 cp_lexer_consume_token (parser->lexer);
16709 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16712 /* Parse an asm-clobber-list.
16716 asm-clobber-list , string-literal
16718 Returns a TREE_LIST, indicating the clobbers in the order that they
16719 appeared. The TREE_VALUE of each node is a STRING_CST. */
16722 cp_parser_asm_clobber_list (cp_parser* parser)
16724 tree clobbers = NULL_TREE;
16728 tree string_literal;
16730 /* Look for the string literal. */
16731 string_literal = cp_parser_string_literal (parser, false, false);
16732 /* Add it to the list. */
16733 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16734 /* If the next token is not a `,', then the list is
16736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16738 /* Consume the `,' token. */
16739 cp_lexer_consume_token (parser->lexer);
16745 /* Parse an (optional) series of attributes.
16748 attributes attribute
16751 __attribute__ (( attribute-list [opt] ))
16753 The return value is as for cp_parser_attribute_list. */
16756 cp_parser_attributes_opt (cp_parser* parser)
16758 tree attributes = NULL_TREE;
16763 tree attribute_list;
16765 /* Peek at the next token. */
16766 token = cp_lexer_peek_token (parser->lexer);
16767 /* If it's not `__attribute__', then we're done. */
16768 if (token->keyword != RID_ATTRIBUTE)
16771 /* Consume the `__attribute__' keyword. */
16772 cp_lexer_consume_token (parser->lexer);
16773 /* Look for the two `(' tokens. */
16774 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16775 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16777 /* Peek at the next token. */
16778 token = cp_lexer_peek_token (parser->lexer);
16779 if (token->type != CPP_CLOSE_PAREN)
16780 /* Parse the attribute-list. */
16781 attribute_list = cp_parser_attribute_list (parser);
16783 /* If the next token is a `)', then there is no attribute
16785 attribute_list = NULL;
16787 /* Look for the two `)' tokens. */
16788 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16789 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16791 /* Add these new attributes to the list. */
16792 attributes = chainon (attributes, attribute_list);
16798 /* Parse an attribute-list.
16802 attribute-list , attribute
16806 identifier ( identifier )
16807 identifier ( identifier , expression-list )
16808 identifier ( expression-list )
16810 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16811 to an attribute. The TREE_PURPOSE of each node is the identifier
16812 indicating which attribute is in use. The TREE_VALUE represents
16813 the arguments, if any. */
16816 cp_parser_attribute_list (cp_parser* parser)
16818 tree attribute_list = NULL_TREE;
16819 bool save_translate_strings_p = parser->translate_strings_p;
16821 parser->translate_strings_p = false;
16828 /* Look for the identifier. We also allow keywords here; for
16829 example `__attribute__ ((const))' is legal. */
16830 token = cp_lexer_peek_token (parser->lexer);
16831 if (token->type == CPP_NAME
16832 || token->type == CPP_KEYWORD)
16834 tree arguments = NULL_TREE;
16836 /* Consume the token. */
16837 token = cp_lexer_consume_token (parser->lexer);
16839 /* Save away the identifier that indicates which attribute
16841 identifier = token->u.value;
16842 attribute = build_tree_list (identifier, NULL_TREE);
16844 /* Peek at the next token. */
16845 token = cp_lexer_peek_token (parser->lexer);
16846 /* If it's an `(', then parse the attribute arguments. */
16847 if (token->type == CPP_OPEN_PAREN)
16849 arguments = cp_parser_parenthesized_expression_list
16850 (parser, true, /*cast_p=*/false,
16851 /*allow_expansion_p=*/false,
16852 /*non_constant_p=*/NULL);
16853 /* Save the arguments away. */
16854 TREE_VALUE (attribute) = arguments;
16857 if (arguments != error_mark_node)
16859 /* Add this attribute to the list. */
16860 TREE_CHAIN (attribute) = attribute_list;
16861 attribute_list = attribute;
16864 token = cp_lexer_peek_token (parser->lexer);
16866 /* Now, look for more attributes. If the next token isn't a
16867 `,', we're done. */
16868 if (token->type != CPP_COMMA)
16871 /* Consume the comma and keep going. */
16872 cp_lexer_consume_token (parser->lexer);
16874 parser->translate_strings_p = save_translate_strings_p;
16876 /* We built up the list in reverse order. */
16877 return nreverse (attribute_list);
16880 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16881 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16882 current value of the PEDANTIC flag, regardless of whether or not
16883 the `__extension__' keyword is present. The caller is responsible
16884 for restoring the value of the PEDANTIC flag. */
16887 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16889 /* Save the old value of the PEDANTIC flag. */
16890 *saved_pedantic = pedantic;
16892 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16894 /* Consume the `__extension__' token. */
16895 cp_lexer_consume_token (parser->lexer);
16896 /* We're not being pedantic while the `__extension__' keyword is
16906 /* Parse a label declaration.
16909 __label__ label-declarator-seq ;
16911 label-declarator-seq:
16912 identifier , label-declarator-seq
16916 cp_parser_label_declaration (cp_parser* parser)
16918 /* Look for the `__label__' keyword. */
16919 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16925 /* Look for an identifier. */
16926 identifier = cp_parser_identifier (parser);
16927 /* If we failed, stop. */
16928 if (identifier == error_mark_node)
16930 /* Declare it as a label. */
16931 finish_label_decl (identifier);
16932 /* If the next token is a `;', stop. */
16933 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16935 /* Look for the `,' separating the label declarations. */
16936 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16939 /* Look for the final `;'. */
16940 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16943 /* Support Functions */
16945 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16946 NAME should have one of the representations used for an
16947 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16948 is returned. If PARSER->SCOPE is a dependent type, then a
16949 SCOPE_REF is returned.
16951 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16952 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16953 was formed. Abstractly, such entities should not be passed to this
16954 function, because they do not need to be looked up, but it is
16955 simpler to check for this special case here, rather than at the
16958 In cases not explicitly covered above, this function returns a
16959 DECL, OVERLOAD, or baselink representing the result of the lookup.
16960 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16963 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16964 (e.g., "struct") that was used. In that case bindings that do not
16965 refer to types are ignored.
16967 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16970 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16973 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16976 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16977 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16978 NULL_TREE otherwise. */
16981 cp_parser_lookup_name (cp_parser *parser, tree name,
16982 enum tag_types tag_type,
16985 bool check_dependency,
16986 tree *ambiguous_decls,
16987 location_t name_location)
16991 tree object_type = parser->context->object_type;
16993 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16994 flags |= LOOKUP_COMPLAIN;
16996 /* Assume that the lookup will be unambiguous. */
16997 if (ambiguous_decls)
16998 *ambiguous_decls = NULL_TREE;
17000 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17001 no longer valid. Note that if we are parsing tentatively, and
17002 the parse fails, OBJECT_TYPE will be automatically restored. */
17003 parser->context->object_type = NULL_TREE;
17005 if (name == error_mark_node)
17006 return error_mark_node;
17008 /* A template-id has already been resolved; there is no lookup to
17010 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17012 if (BASELINK_P (name))
17014 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17015 == TEMPLATE_ID_EXPR);
17019 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17020 it should already have been checked to make sure that the name
17021 used matches the type being destroyed. */
17022 if (TREE_CODE (name) == BIT_NOT_EXPR)
17026 /* Figure out to which type this destructor applies. */
17028 type = parser->scope;
17029 else if (object_type)
17030 type = object_type;
17032 type = current_class_type;
17033 /* If that's not a class type, there is no destructor. */
17034 if (!type || !CLASS_TYPE_P (type))
17035 return error_mark_node;
17036 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17037 lazily_declare_fn (sfk_destructor, type);
17038 if (!CLASSTYPE_DESTRUCTORS (type))
17039 return error_mark_node;
17040 /* If it was a class type, return the destructor. */
17041 return CLASSTYPE_DESTRUCTORS (type);
17044 /* By this point, the NAME should be an ordinary identifier. If
17045 the id-expression was a qualified name, the qualifying scope is
17046 stored in PARSER->SCOPE at this point. */
17047 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17049 /* Perform the lookup. */
17054 if (parser->scope == error_mark_node)
17055 return error_mark_node;
17057 /* If the SCOPE is dependent, the lookup must be deferred until
17058 the template is instantiated -- unless we are explicitly
17059 looking up names in uninstantiated templates. Even then, we
17060 cannot look up the name if the scope is not a class type; it
17061 might, for example, be a template type parameter. */
17062 dependent_p = (TYPE_P (parser->scope)
17063 && dependent_scope_p (parser->scope));
17064 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17066 /* Defer lookup. */
17067 decl = error_mark_node;
17070 tree pushed_scope = NULL_TREE;
17072 /* If PARSER->SCOPE is a dependent type, then it must be a
17073 class type, and we must not be checking dependencies;
17074 otherwise, we would have processed this lookup above. So
17075 that PARSER->SCOPE is not considered a dependent base by
17076 lookup_member, we must enter the scope here. */
17078 pushed_scope = push_scope (parser->scope);
17079 /* If the PARSER->SCOPE is a template specialization, it
17080 may be instantiated during name lookup. In that case,
17081 errors may be issued. Even if we rollback the current
17082 tentative parse, those errors are valid. */
17083 decl = lookup_qualified_name (parser->scope, name,
17084 tag_type != none_type,
17085 /*complain=*/true);
17087 /* If we have a single function from a using decl, pull it out. */
17088 if (TREE_CODE (decl) == OVERLOAD
17089 && !really_overloaded_fn (decl))
17090 decl = OVL_FUNCTION (decl);
17093 pop_scope (pushed_scope);
17096 /* If the scope is a dependent type and either we deferred lookup or
17097 we did lookup but didn't find the name, rememeber the name. */
17098 if (decl == error_mark_node && TYPE_P (parser->scope)
17099 && dependent_type_p (parser->scope))
17105 /* The resolution to Core Issue 180 says that `struct
17106 A::B' should be considered a type-name, even if `A'
17108 type = make_typename_type (parser->scope, name, tag_type,
17109 /*complain=*/tf_error);
17110 decl = TYPE_NAME (type);
17112 else if (is_template
17113 && (cp_parser_next_token_ends_template_argument_p (parser)
17114 || cp_lexer_next_token_is (parser->lexer,
17116 decl = make_unbound_class_template (parser->scope,
17118 /*complain=*/tf_error);
17120 decl = build_qualified_name (/*type=*/NULL_TREE,
17121 parser->scope, name,
17124 parser->qualifying_scope = parser->scope;
17125 parser->object_scope = NULL_TREE;
17127 else if (object_type)
17129 tree object_decl = NULL_TREE;
17130 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17131 OBJECT_TYPE is not a class. */
17132 if (CLASS_TYPE_P (object_type))
17133 /* If the OBJECT_TYPE is a template specialization, it may
17134 be instantiated during name lookup. In that case, errors
17135 may be issued. Even if we rollback the current tentative
17136 parse, those errors are valid. */
17137 object_decl = lookup_member (object_type,
17140 tag_type != none_type);
17141 /* Look it up in the enclosing context, too. */
17142 decl = lookup_name_real (name, tag_type != none_type,
17144 /*block_p=*/true, is_namespace, flags);
17145 parser->object_scope = object_type;
17146 parser->qualifying_scope = NULL_TREE;
17148 decl = object_decl;
17152 decl = lookup_name_real (name, tag_type != none_type,
17154 /*block_p=*/true, is_namespace, flags);
17155 parser->qualifying_scope = NULL_TREE;
17156 parser->object_scope = NULL_TREE;
17159 /* If the lookup failed, let our caller know. */
17160 if (!decl || decl == error_mark_node)
17161 return error_mark_node;
17163 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17164 if (TREE_CODE (decl) == TREE_LIST)
17166 if (ambiguous_decls)
17167 *ambiguous_decls = decl;
17168 /* The error message we have to print is too complicated for
17169 cp_parser_error, so we incorporate its actions directly. */
17170 if (!cp_parser_simulate_error (parser))
17172 error ("%Hreference to %qD is ambiguous",
17173 &name_location, name);
17174 print_candidates (decl);
17176 return error_mark_node;
17179 gcc_assert (DECL_P (decl)
17180 || TREE_CODE (decl) == OVERLOAD
17181 || TREE_CODE (decl) == SCOPE_REF
17182 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17183 || BASELINK_P (decl));
17185 /* If we have resolved the name of a member declaration, check to
17186 see if the declaration is accessible. When the name resolves to
17187 set of overloaded functions, accessibility is checked when
17188 overload resolution is done.
17190 During an explicit instantiation, access is not checked at all,
17191 as per [temp.explicit]. */
17193 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17198 /* Like cp_parser_lookup_name, but for use in the typical case where
17199 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17200 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17203 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17205 return cp_parser_lookup_name (parser, name,
17207 /*is_template=*/false,
17208 /*is_namespace=*/false,
17209 /*check_dependency=*/true,
17210 /*ambiguous_decls=*/NULL,
17214 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17215 the current context, return the TYPE_DECL. If TAG_NAME_P is
17216 true, the DECL indicates the class being defined in a class-head,
17217 or declared in an elaborated-type-specifier.
17219 Otherwise, return DECL. */
17222 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17224 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17225 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17228 template <typename T> struct B;
17231 template <typename T> struct A::B {};
17233 Similarly, in an elaborated-type-specifier:
17235 namespace N { struct X{}; }
17238 template <typename T> friend struct N::X;
17241 However, if the DECL refers to a class type, and we are in
17242 the scope of the class, then the name lookup automatically
17243 finds the TYPE_DECL created by build_self_reference rather
17244 than a TEMPLATE_DECL. For example, in:
17246 template <class T> struct S {
17250 there is no need to handle such case. */
17252 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17253 return DECL_TEMPLATE_RESULT (decl);
17258 /* If too many, or too few, template-parameter lists apply to the
17259 declarator, issue an error message. Returns TRUE if all went well,
17260 and FALSE otherwise. */
17263 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17264 cp_declarator *declarator,
17265 location_t declarator_location)
17267 unsigned num_templates;
17269 /* We haven't seen any classes that involve template parameters yet. */
17272 switch (declarator->kind)
17275 if (declarator->u.id.qualifying_scope)
17280 scope = declarator->u.id.qualifying_scope;
17281 member = declarator->u.id.unqualified_name;
17283 while (scope && CLASS_TYPE_P (scope))
17285 /* You're supposed to have one `template <...>'
17286 for every template class, but you don't need one
17287 for a full specialization. For example:
17289 template <class T> struct S{};
17290 template <> struct S<int> { void f(); };
17291 void S<int>::f () {}
17293 is correct; there shouldn't be a `template <>' for
17294 the definition of `S<int>::f'. */
17295 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17296 /* If SCOPE does not have template information of any
17297 kind, then it is not a template, nor is it nested
17298 within a template. */
17300 if (explicit_class_specialization_p (scope))
17302 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17305 scope = TYPE_CONTEXT (scope);
17308 else if (TREE_CODE (declarator->u.id.unqualified_name)
17309 == TEMPLATE_ID_EXPR)
17310 /* If the DECLARATOR has the form `X<y>' then it uses one
17311 additional level of template parameters. */
17314 return cp_parser_check_template_parameters
17315 (parser, num_templates, declarator_location, declarator);
17321 case cdk_reference:
17323 return (cp_parser_check_declarator_template_parameters
17324 (parser, declarator->declarator, declarator_location));
17330 gcc_unreachable ();
17335 /* NUM_TEMPLATES were used in the current declaration. If that is
17336 invalid, return FALSE and issue an error messages. Otherwise,
17337 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17338 declarator and we can print more accurate diagnostics. */
17341 cp_parser_check_template_parameters (cp_parser* parser,
17342 unsigned num_templates,
17343 location_t location,
17344 cp_declarator *declarator)
17346 /* If there are the same number of template classes and parameter
17347 lists, that's OK. */
17348 if (parser->num_template_parameter_lists == num_templates)
17350 /* If there are more, but only one more, then we are referring to a
17351 member template. That's OK too. */
17352 if (parser->num_template_parameter_lists == num_templates + 1)
17354 /* If there are more template classes than parameter lists, we have
17357 template <class T> void S<T>::R<T>::f (); */
17358 if (parser->num_template_parameter_lists < num_templates)
17361 error_at (location, "specializing member %<%T::%E%> "
17362 "requires %<template<>%> syntax",
17363 declarator->u.id.qualifying_scope,
17364 declarator->u.id.unqualified_name);
17366 error_at (location, "too few template-parameter-lists");
17369 /* Otherwise, there are too many template parameter lists. We have
17372 template <class T> template <class U> void S::f(); */
17373 error ("%Htoo many template-parameter-lists", &location);
17377 /* Parse an optional `::' token indicating that the following name is
17378 from the global namespace. If so, PARSER->SCOPE is set to the
17379 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17380 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17381 Returns the new value of PARSER->SCOPE, if the `::' token is
17382 present, and NULL_TREE otherwise. */
17385 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17389 /* Peek at the next token. */
17390 token = cp_lexer_peek_token (parser->lexer);
17391 /* If we're looking at a `::' token then we're starting from the
17392 global namespace, not our current location. */
17393 if (token->type == CPP_SCOPE)
17395 /* Consume the `::' token. */
17396 cp_lexer_consume_token (parser->lexer);
17397 /* Set the SCOPE so that we know where to start the lookup. */
17398 parser->scope = global_namespace;
17399 parser->qualifying_scope = global_namespace;
17400 parser->object_scope = NULL_TREE;
17402 return parser->scope;
17404 else if (!current_scope_valid_p)
17406 parser->scope = NULL_TREE;
17407 parser->qualifying_scope = NULL_TREE;
17408 parser->object_scope = NULL_TREE;
17414 /* Returns TRUE if the upcoming token sequence is the start of a
17415 constructor declarator. If FRIEND_P is true, the declarator is
17416 preceded by the `friend' specifier. */
17419 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17421 bool constructor_p;
17422 tree type_decl = NULL_TREE;
17423 bool nested_name_p;
17424 cp_token *next_token;
17426 /* The common case is that this is not a constructor declarator, so
17427 try to avoid doing lots of work if at all possible. It's not
17428 valid declare a constructor at function scope. */
17429 if (parser->in_function_body)
17431 /* And only certain tokens can begin a constructor declarator. */
17432 next_token = cp_lexer_peek_token (parser->lexer);
17433 if (next_token->type != CPP_NAME
17434 && next_token->type != CPP_SCOPE
17435 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17436 && next_token->type != CPP_TEMPLATE_ID)
17439 /* Parse tentatively; we are going to roll back all of the tokens
17441 cp_parser_parse_tentatively (parser);
17442 /* Assume that we are looking at a constructor declarator. */
17443 constructor_p = true;
17445 /* Look for the optional `::' operator. */
17446 cp_parser_global_scope_opt (parser,
17447 /*current_scope_valid_p=*/false);
17448 /* Look for the nested-name-specifier. */
17450 = (cp_parser_nested_name_specifier_opt (parser,
17451 /*typename_keyword_p=*/false,
17452 /*check_dependency_p=*/false,
17454 /*is_declaration=*/false)
17456 /* Outside of a class-specifier, there must be a
17457 nested-name-specifier. */
17458 if (!nested_name_p &&
17459 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17461 constructor_p = false;
17462 /* If we still think that this might be a constructor-declarator,
17463 look for a class-name. */
17468 template <typename T> struct S { S(); };
17469 template <typename T> S<T>::S ();
17471 we must recognize that the nested `S' names a class.
17474 template <typename T> S<T>::S<T> ();
17476 we must recognize that the nested `S' names a template. */
17477 type_decl = cp_parser_class_name (parser,
17478 /*typename_keyword_p=*/false,
17479 /*template_keyword_p=*/false,
17481 /*check_dependency_p=*/false,
17482 /*class_head_p=*/false,
17483 /*is_declaration=*/false);
17484 /* If there was no class-name, then this is not a constructor. */
17485 constructor_p = !cp_parser_error_occurred (parser);
17488 /* If we're still considering a constructor, we have to see a `(',
17489 to begin the parameter-declaration-clause, followed by either a
17490 `)', an `...', or a decl-specifier. We need to check for a
17491 type-specifier to avoid being fooled into thinking that:
17495 is a constructor. (It is actually a function named `f' that
17496 takes one parameter (of type `int') and returns a value of type
17499 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17502 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17503 /* A parameter declaration begins with a decl-specifier,
17504 which is either the "attribute" keyword, a storage class
17505 specifier, or (usually) a type-specifier. */
17506 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17509 tree pushed_scope = NULL_TREE;
17510 unsigned saved_num_template_parameter_lists;
17512 /* Names appearing in the type-specifier should be looked up
17513 in the scope of the class. */
17514 if (current_class_type)
17518 type = TREE_TYPE (type_decl);
17519 if (TREE_CODE (type) == TYPENAME_TYPE)
17521 type = resolve_typename_type (type,
17522 /*only_current_p=*/false);
17523 if (TREE_CODE (type) == TYPENAME_TYPE)
17525 cp_parser_abort_tentative_parse (parser);
17529 pushed_scope = push_scope (type);
17532 /* Inside the constructor parameter list, surrounding
17533 template-parameter-lists do not apply. */
17534 saved_num_template_parameter_lists
17535 = parser->num_template_parameter_lists;
17536 parser->num_template_parameter_lists = 0;
17538 /* Look for the type-specifier. */
17539 cp_parser_type_specifier (parser,
17540 CP_PARSER_FLAGS_NONE,
17541 /*decl_specs=*/NULL,
17542 /*is_declarator=*/true,
17543 /*declares_class_or_enum=*/NULL,
17544 /*is_cv_qualifier=*/NULL);
17546 parser->num_template_parameter_lists
17547 = saved_num_template_parameter_lists;
17549 /* Leave the scope of the class. */
17551 pop_scope (pushed_scope);
17553 constructor_p = !cp_parser_error_occurred (parser);
17557 constructor_p = false;
17558 /* We did not really want to consume any tokens. */
17559 cp_parser_abort_tentative_parse (parser);
17561 return constructor_p;
17564 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17565 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17566 they must be performed once we are in the scope of the function.
17568 Returns the function defined. */
17571 cp_parser_function_definition_from_specifiers_and_declarator
17572 (cp_parser* parser,
17573 cp_decl_specifier_seq *decl_specifiers,
17575 const cp_declarator *declarator)
17580 /* Begin the function-definition. */
17581 success_p = start_function (decl_specifiers, declarator, attributes);
17583 /* The things we're about to see are not directly qualified by any
17584 template headers we've seen thus far. */
17585 reset_specialization ();
17587 /* If there were names looked up in the decl-specifier-seq that we
17588 did not check, check them now. We must wait until we are in the
17589 scope of the function to perform the checks, since the function
17590 might be a friend. */
17591 perform_deferred_access_checks ();
17595 /* Skip the entire function. */
17596 cp_parser_skip_to_end_of_block_or_statement (parser);
17597 fn = error_mark_node;
17599 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17601 /* Seen already, skip it. An error message has already been output. */
17602 cp_parser_skip_to_end_of_block_or_statement (parser);
17603 fn = current_function_decl;
17604 current_function_decl = NULL_TREE;
17605 /* If this is a function from a class, pop the nested class. */
17606 if (current_class_name)
17607 pop_nested_class ();
17610 fn = cp_parser_function_definition_after_declarator (parser,
17611 /*inline_p=*/false);
17616 /* Parse the part of a function-definition that follows the
17617 declarator. INLINE_P is TRUE iff this function is an inline
17618 function defined with a class-specifier.
17620 Returns the function defined. */
17623 cp_parser_function_definition_after_declarator (cp_parser* parser,
17627 bool ctor_initializer_p = false;
17628 bool saved_in_unbraced_linkage_specification_p;
17629 bool saved_in_function_body;
17630 unsigned saved_num_template_parameter_lists;
17633 saved_in_function_body = parser->in_function_body;
17634 parser->in_function_body = true;
17635 /* If the next token is `return', then the code may be trying to
17636 make use of the "named return value" extension that G++ used to
17638 token = cp_lexer_peek_token (parser->lexer);
17639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17641 /* Consume the `return' keyword. */
17642 cp_lexer_consume_token (parser->lexer);
17643 /* Look for the identifier that indicates what value is to be
17645 cp_parser_identifier (parser);
17646 /* Issue an error message. */
17647 error ("%Hnamed return values are no longer supported",
17649 /* Skip tokens until we reach the start of the function body. */
17652 cp_token *token = cp_lexer_peek_token (parser->lexer);
17653 if (token->type == CPP_OPEN_BRACE
17654 || token->type == CPP_EOF
17655 || token->type == CPP_PRAGMA_EOL)
17657 cp_lexer_consume_token (parser->lexer);
17660 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17661 anything declared inside `f'. */
17662 saved_in_unbraced_linkage_specification_p
17663 = parser->in_unbraced_linkage_specification_p;
17664 parser->in_unbraced_linkage_specification_p = false;
17665 /* Inside the function, surrounding template-parameter-lists do not
17667 saved_num_template_parameter_lists
17668 = parser->num_template_parameter_lists;
17669 parser->num_template_parameter_lists = 0;
17670 /* If the next token is `try', then we are looking at a
17671 function-try-block. */
17672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17673 ctor_initializer_p = cp_parser_function_try_block (parser);
17674 /* A function-try-block includes the function-body, so we only do
17675 this next part if we're not processing a function-try-block. */
17678 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17680 /* Finish the function. */
17681 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17682 (inline_p ? 2 : 0));
17683 /* Generate code for it, if necessary. */
17684 expand_or_defer_fn (fn);
17685 /* Restore the saved values. */
17686 parser->in_unbraced_linkage_specification_p
17687 = saved_in_unbraced_linkage_specification_p;
17688 parser->num_template_parameter_lists
17689 = saved_num_template_parameter_lists;
17690 parser->in_function_body = saved_in_function_body;
17695 /* Parse a template-declaration, assuming that the `export' (and
17696 `extern') keywords, if present, has already been scanned. MEMBER_P
17697 is as for cp_parser_template_declaration. */
17700 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17702 tree decl = NULL_TREE;
17703 VEC (deferred_access_check,gc) *checks;
17704 tree parameter_list;
17705 bool friend_p = false;
17706 bool need_lang_pop;
17709 /* Look for the `template' keyword. */
17710 token = cp_lexer_peek_token (parser->lexer);
17711 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17715 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17717 if (at_class_scope_p () && current_function_decl)
17719 /* 14.5.2.2 [temp.mem]
17721 A local class shall not have member templates. */
17722 error ("%Hinvalid declaration of member template in local class",
17724 cp_parser_skip_to_end_of_block_or_statement (parser);
17729 A template ... shall not have C linkage. */
17730 if (current_lang_name == lang_name_c)
17732 error ("%Htemplate with C linkage", &token->location);
17733 /* Give it C++ linkage to avoid confusing other parts of the
17735 push_lang_context (lang_name_cplusplus);
17736 need_lang_pop = true;
17739 need_lang_pop = false;
17741 /* We cannot perform access checks on the template parameter
17742 declarations until we know what is being declared, just as we
17743 cannot check the decl-specifier list. */
17744 push_deferring_access_checks (dk_deferred);
17746 /* If the next token is `>', then we have an invalid
17747 specialization. Rather than complain about an invalid template
17748 parameter, issue an error message here. */
17749 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17751 cp_parser_error (parser, "invalid explicit specialization");
17752 begin_specialization ();
17753 parameter_list = NULL_TREE;
17756 /* Parse the template parameters. */
17757 parameter_list = cp_parser_template_parameter_list (parser);
17759 /* Get the deferred access checks from the parameter list. These
17760 will be checked once we know what is being declared, as for a
17761 member template the checks must be performed in the scope of the
17762 class containing the member. */
17763 checks = get_deferred_access_checks ();
17765 /* Look for the `>'. */
17766 cp_parser_skip_to_end_of_template_parameter_list (parser);
17767 /* We just processed one more parameter list. */
17768 ++parser->num_template_parameter_lists;
17769 /* If the next token is `template', there are more template
17771 if (cp_lexer_next_token_is_keyword (parser->lexer,
17773 cp_parser_template_declaration_after_export (parser, member_p);
17776 /* There are no access checks when parsing a template, as we do not
17777 know if a specialization will be a friend. */
17778 push_deferring_access_checks (dk_no_check);
17779 token = cp_lexer_peek_token (parser->lexer);
17780 decl = cp_parser_single_declaration (parser,
17783 /*explicit_specialization_p=*/false,
17785 pop_deferring_access_checks ();
17787 /* If this is a member template declaration, let the front
17789 if (member_p && !friend_p && decl)
17791 if (TREE_CODE (decl) == TYPE_DECL)
17792 cp_parser_check_access_in_redeclaration (decl, token->location);
17794 decl = finish_member_template_decl (decl);
17796 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17797 make_friend_class (current_class_type, TREE_TYPE (decl),
17798 /*complain=*/true);
17800 /* We are done with the current parameter list. */
17801 --parser->num_template_parameter_lists;
17803 pop_deferring_access_checks ();
17806 finish_template_decl (parameter_list);
17808 /* Register member declarations. */
17809 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17810 finish_member_declaration (decl);
17811 /* For the erroneous case of a template with C linkage, we pushed an
17812 implicit C++ linkage scope; exit that scope now. */
17814 pop_lang_context ();
17815 /* If DECL is a function template, we must return to parse it later.
17816 (Even though there is no definition, there might be default
17817 arguments that need handling.) */
17818 if (member_p && decl
17819 && (TREE_CODE (decl) == FUNCTION_DECL
17820 || DECL_FUNCTION_TEMPLATE_P (decl)))
17821 TREE_VALUE (parser->unparsed_functions_queues)
17822 = tree_cons (NULL_TREE, decl,
17823 TREE_VALUE (parser->unparsed_functions_queues));
17826 /* Perform the deferred access checks from a template-parameter-list.
17827 CHECKS is a TREE_LIST of access checks, as returned by
17828 get_deferred_access_checks. */
17831 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17833 ++processing_template_parmlist;
17834 perform_access_checks (checks);
17835 --processing_template_parmlist;
17838 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17839 `function-definition' sequence. MEMBER_P is true, this declaration
17840 appears in a class scope.
17842 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17843 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17846 cp_parser_single_declaration (cp_parser* parser,
17847 VEC (deferred_access_check,gc)* checks,
17849 bool explicit_specialization_p,
17852 int declares_class_or_enum;
17853 tree decl = NULL_TREE;
17854 cp_decl_specifier_seq decl_specifiers;
17855 bool function_definition_p = false;
17856 cp_token *decl_spec_token_start;
17858 /* This function is only used when processing a template
17860 gcc_assert (innermost_scope_kind () == sk_template_parms
17861 || innermost_scope_kind () == sk_template_spec);
17863 /* Defer access checks until we know what is being declared. */
17864 push_deferring_access_checks (dk_deferred);
17866 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17868 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17869 cp_parser_decl_specifier_seq (parser,
17870 CP_PARSER_FLAGS_OPTIONAL,
17872 &declares_class_or_enum);
17874 *friend_p = cp_parser_friend_p (&decl_specifiers);
17876 /* There are no template typedefs. */
17877 if (decl_specifiers.specs[(int) ds_typedef])
17879 error ("%Htemplate declaration of %qs",
17880 &decl_spec_token_start->location, "typedef");
17881 decl = error_mark_node;
17884 /* Gather up the access checks that occurred the
17885 decl-specifier-seq. */
17886 stop_deferring_access_checks ();
17888 /* Check for the declaration of a template class. */
17889 if (declares_class_or_enum)
17891 if (cp_parser_declares_only_class_p (parser))
17893 decl = shadow_tag (&decl_specifiers);
17898 friend template <typename T> struct A<T>::B;
17901 A<T>::B will be represented by a TYPENAME_TYPE, and
17902 therefore not recognized by shadow_tag. */
17903 if (friend_p && *friend_p
17905 && decl_specifiers.type
17906 && TYPE_P (decl_specifiers.type))
17907 decl = decl_specifiers.type;
17909 if (decl && decl != error_mark_node)
17910 decl = TYPE_NAME (decl);
17912 decl = error_mark_node;
17914 /* Perform access checks for template parameters. */
17915 cp_parser_perform_template_parameter_access_checks (checks);
17918 /* If it's not a template class, try for a template function. If
17919 the next token is a `;', then this declaration does not declare
17920 anything. But, if there were errors in the decl-specifiers, then
17921 the error might well have come from an attempted class-specifier.
17922 In that case, there's no need to warn about a missing declarator. */
17924 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17925 || decl_specifiers.type != error_mark_node))
17927 decl = cp_parser_init_declarator (parser,
17930 /*function_definition_allowed_p=*/true,
17932 declares_class_or_enum,
17933 &function_definition_p);
17935 /* 7.1.1-1 [dcl.stc]
17937 A storage-class-specifier shall not be specified in an explicit
17938 specialization... */
17940 && explicit_specialization_p
17941 && decl_specifiers.storage_class != sc_none)
17943 error ("%Hexplicit template specialization cannot have a storage class",
17944 &decl_spec_token_start->location);
17945 decl = error_mark_node;
17949 pop_deferring_access_checks ();
17951 /* Clear any current qualification; whatever comes next is the start
17952 of something new. */
17953 parser->scope = NULL_TREE;
17954 parser->qualifying_scope = NULL_TREE;
17955 parser->object_scope = NULL_TREE;
17956 /* Look for a trailing `;' after the declaration. */
17957 if (!function_definition_p
17958 && (decl == error_mark_node
17959 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17960 cp_parser_skip_to_end_of_block_or_statement (parser);
17965 /* Parse a cast-expression that is not the operand of a unary "&". */
17968 cp_parser_simple_cast_expression (cp_parser *parser)
17970 return cp_parser_cast_expression (parser, /*address_p=*/false,
17971 /*cast_p=*/false, NULL);
17974 /* Parse a functional cast to TYPE. Returns an expression
17975 representing the cast. */
17978 cp_parser_functional_cast (cp_parser* parser, tree type)
17980 tree expression_list;
17984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17986 maybe_warn_cpp0x ("extended initializer lists");
17987 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17988 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17989 if (TREE_CODE (type) == TYPE_DECL)
17990 type = TREE_TYPE (type);
17991 return finish_compound_literal (type, expression_list);
17995 = cp_parser_parenthesized_expression_list (parser, false,
17997 /*allow_expansion_p=*/true,
17998 /*non_constant_p=*/NULL);
18000 cast = build_functional_cast (type, expression_list,
18001 tf_warning_or_error);
18002 /* [expr.const]/1: In an integral constant expression "only type
18003 conversions to integral or enumeration type can be used". */
18004 if (TREE_CODE (type) == TYPE_DECL)
18005 type = TREE_TYPE (type);
18006 if (cast != error_mark_node
18007 && !cast_valid_in_integral_constant_expression_p (type)
18008 && (cp_parser_non_integral_constant_expression
18009 (parser, "a call to a constructor")))
18010 return error_mark_node;
18014 /* Save the tokens that make up the body of a member function defined
18015 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18016 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18017 specifiers applied to the declaration. Returns the FUNCTION_DECL
18018 for the member function. */
18021 cp_parser_save_member_function_body (cp_parser* parser,
18022 cp_decl_specifier_seq *decl_specifiers,
18023 cp_declarator *declarator,
18030 /* Create the function-declaration. */
18031 fn = start_method (decl_specifiers, declarator, attributes);
18032 /* If something went badly wrong, bail out now. */
18033 if (fn == error_mark_node)
18035 /* If there's a function-body, skip it. */
18036 if (cp_parser_token_starts_function_definition_p
18037 (cp_lexer_peek_token (parser->lexer)))
18038 cp_parser_skip_to_end_of_block_or_statement (parser);
18039 return error_mark_node;
18042 /* Remember it, if there default args to post process. */
18043 cp_parser_save_default_args (parser, fn);
18045 /* Save away the tokens that make up the body of the
18047 first = parser->lexer->next_token;
18048 /* We can have braced-init-list mem-initializers before the fn body. */
18049 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18051 cp_lexer_consume_token (parser->lexer);
18052 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18053 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18055 /* cache_group will stop after an un-nested { } pair, too. */
18056 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18059 /* variadic mem-inits have ... after the ')'. */
18060 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18061 cp_lexer_consume_token (parser->lexer);
18064 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18065 /* Handle function try blocks. */
18066 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18067 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18068 last = parser->lexer->next_token;
18070 /* Save away the inline definition; we will process it when the
18071 class is complete. */
18072 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18073 DECL_PENDING_INLINE_P (fn) = 1;
18075 /* We need to know that this was defined in the class, so that
18076 friend templates are handled correctly. */
18077 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18079 /* We're done with the inline definition. */
18080 finish_method (fn);
18082 /* Add FN to the queue of functions to be parsed later. */
18083 TREE_VALUE (parser->unparsed_functions_queues)
18084 = tree_cons (NULL_TREE, fn,
18085 TREE_VALUE (parser->unparsed_functions_queues));
18090 /* Parse a template-argument-list, as well as the trailing ">" (but
18091 not the opening ">"). See cp_parser_template_argument_list for the
18095 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18099 tree saved_qualifying_scope;
18100 tree saved_object_scope;
18101 bool saved_greater_than_is_operator_p;
18102 bool saved_skip_evaluation;
18106 When parsing a template-id, the first non-nested `>' is taken as
18107 the end of the template-argument-list rather than a greater-than
18109 saved_greater_than_is_operator_p
18110 = parser->greater_than_is_operator_p;
18111 parser->greater_than_is_operator_p = false;
18112 /* Parsing the argument list may modify SCOPE, so we save it
18114 saved_scope = parser->scope;
18115 saved_qualifying_scope = parser->qualifying_scope;
18116 saved_object_scope = parser->object_scope;
18117 /* We need to evaluate the template arguments, even though this
18118 template-id may be nested within a "sizeof". */
18119 saved_skip_evaluation = skip_evaluation;
18120 skip_evaluation = false;
18121 /* Parse the template-argument-list itself. */
18122 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18123 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18124 arguments = NULL_TREE;
18126 arguments = cp_parser_template_argument_list (parser);
18127 /* Look for the `>' that ends the template-argument-list. If we find
18128 a '>>' instead, it's probably just a typo. */
18129 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18131 if (cxx_dialect != cxx98)
18133 /* In C++0x, a `>>' in a template argument list or cast
18134 expression is considered to be two separate `>'
18135 tokens. So, change the current token to a `>', but don't
18136 consume it: it will be consumed later when the outer
18137 template argument list (or cast expression) is parsed.
18138 Note that this replacement of `>' for `>>' is necessary
18139 even if we are parsing tentatively: in the tentative
18140 case, after calling
18141 cp_parser_enclosed_template_argument_list we will always
18142 throw away all of the template arguments and the first
18143 closing `>', either because the template argument list
18144 was erroneous or because we are replacing those tokens
18145 with a CPP_TEMPLATE_ID token. The second `>' (which will
18146 not have been thrown away) is needed either to close an
18147 outer template argument list or to complete a new-style
18149 cp_token *token = cp_lexer_peek_token (parser->lexer);
18150 token->type = CPP_GREATER;
18152 else if (!saved_greater_than_is_operator_p)
18154 /* If we're in a nested template argument list, the '>>' has
18155 to be a typo for '> >'. We emit the error message, but we
18156 continue parsing and we push a '>' as next token, so that
18157 the argument list will be parsed correctly. Note that the
18158 global source location is still on the token before the
18159 '>>', so we need to say explicitly where we want it. */
18160 cp_token *token = cp_lexer_peek_token (parser->lexer);
18161 error ("%H%<>>%> should be %<> >%> "
18162 "within a nested template argument list",
18165 token->type = CPP_GREATER;
18169 /* If this is not a nested template argument list, the '>>'
18170 is a typo for '>'. Emit an error message and continue.
18171 Same deal about the token location, but here we can get it
18172 right by consuming the '>>' before issuing the diagnostic. */
18173 cp_token *token = cp_lexer_consume_token (parser->lexer);
18174 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18175 "a template argument list", &token->location);
18179 cp_parser_skip_to_end_of_template_parameter_list (parser);
18180 /* The `>' token might be a greater-than operator again now. */
18181 parser->greater_than_is_operator_p
18182 = saved_greater_than_is_operator_p;
18183 /* Restore the SAVED_SCOPE. */
18184 parser->scope = saved_scope;
18185 parser->qualifying_scope = saved_qualifying_scope;
18186 parser->object_scope = saved_object_scope;
18187 skip_evaluation = saved_skip_evaluation;
18192 /* MEMBER_FUNCTION is a member function, or a friend. If default
18193 arguments, or the body of the function have not yet been parsed,
18197 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18199 /* If this member is a template, get the underlying
18201 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18202 member_function = DECL_TEMPLATE_RESULT (member_function);
18204 /* There should not be any class definitions in progress at this
18205 point; the bodies of members are only parsed outside of all class
18207 gcc_assert (parser->num_classes_being_defined == 0);
18208 /* While we're parsing the member functions we might encounter more
18209 classes. We want to handle them right away, but we don't want
18210 them getting mixed up with functions that are currently in the
18212 parser->unparsed_functions_queues
18213 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18215 /* Make sure that any template parameters are in scope. */
18216 maybe_begin_member_template_processing (member_function);
18218 /* If the body of the function has not yet been parsed, parse it
18220 if (DECL_PENDING_INLINE_P (member_function))
18222 tree function_scope;
18223 cp_token_cache *tokens;
18225 /* The function is no longer pending; we are processing it. */
18226 tokens = DECL_PENDING_INLINE_INFO (member_function);
18227 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18228 DECL_PENDING_INLINE_P (member_function) = 0;
18230 /* If this is a local class, enter the scope of the containing
18232 function_scope = current_function_decl;
18233 if (function_scope)
18234 push_function_context ();
18236 /* Push the body of the function onto the lexer stack. */
18237 cp_parser_push_lexer_for_tokens (parser, tokens);
18239 /* Let the front end know that we going to be defining this
18241 start_preparsed_function (member_function, NULL_TREE,
18242 SF_PRE_PARSED | SF_INCLASS_INLINE);
18244 /* Don't do access checking if it is a templated function. */
18245 if (processing_template_decl)
18246 push_deferring_access_checks (dk_no_check);
18248 /* Now, parse the body of the function. */
18249 cp_parser_function_definition_after_declarator (parser,
18250 /*inline_p=*/true);
18252 if (processing_template_decl)
18253 pop_deferring_access_checks ();
18255 /* Leave the scope of the containing function. */
18256 if (function_scope)
18257 pop_function_context ();
18258 cp_parser_pop_lexer (parser);
18261 /* Remove any template parameters from the symbol table. */
18262 maybe_end_member_template_processing ();
18264 /* Restore the queue. */
18265 parser->unparsed_functions_queues
18266 = TREE_CHAIN (parser->unparsed_functions_queues);
18269 /* If DECL contains any default args, remember it on the unparsed
18270 functions queue. */
18273 cp_parser_save_default_args (cp_parser* parser, tree decl)
18277 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18279 probe = TREE_CHAIN (probe))
18280 if (TREE_PURPOSE (probe))
18282 TREE_PURPOSE (parser->unparsed_functions_queues)
18283 = tree_cons (current_class_type, decl,
18284 TREE_PURPOSE (parser->unparsed_functions_queues));
18289 /* FN is a FUNCTION_DECL which may contains a parameter with an
18290 unparsed DEFAULT_ARG. Parse the default args now. This function
18291 assumes that the current scope is the scope in which the default
18292 argument should be processed. */
18295 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18297 bool saved_local_variables_forbidden_p;
18300 /* While we're parsing the default args, we might (due to the
18301 statement expression extension) encounter more classes. We want
18302 to handle them right away, but we don't want them getting mixed
18303 up with default args that are currently in the queue. */
18304 parser->unparsed_functions_queues
18305 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18307 /* Local variable names (and the `this' keyword) may not appear
18308 in a default argument. */
18309 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18310 parser->local_variables_forbidden_p = true;
18312 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18314 parm = TREE_CHAIN (parm))
18316 cp_token_cache *tokens;
18317 tree default_arg = TREE_PURPOSE (parm);
18319 VEC(tree,gc) *insts;
18326 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18327 /* This can happen for a friend declaration for a function
18328 already declared with default arguments. */
18331 /* Push the saved tokens for the default argument onto the parser's
18333 tokens = DEFARG_TOKENS (default_arg);
18334 cp_parser_push_lexer_for_tokens (parser, tokens);
18336 /* Parse the assignment-expression. */
18337 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18338 if (parsed_arg == error_mark_node)
18340 cp_parser_pop_lexer (parser);
18344 if (!processing_template_decl)
18345 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18347 TREE_PURPOSE (parm) = parsed_arg;
18349 /* Update any instantiations we've already created. */
18350 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18351 VEC_iterate (tree, insts, ix, copy); ix++)
18352 TREE_PURPOSE (copy) = parsed_arg;
18354 /* If the token stream has not been completely used up, then
18355 there was extra junk after the end of the default
18357 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18358 cp_parser_error (parser, "expected %<,%>");
18360 /* Revert to the main lexer. */
18361 cp_parser_pop_lexer (parser);
18364 /* Make sure no default arg is missing. */
18365 check_default_args (fn);
18367 /* Restore the state of local_variables_forbidden_p. */
18368 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18370 /* Restore the queue. */
18371 parser->unparsed_functions_queues
18372 = TREE_CHAIN (parser->unparsed_functions_queues);
18375 /* Parse the operand of `sizeof' (or a similar operator). Returns
18376 either a TYPE or an expression, depending on the form of the
18377 input. The KEYWORD indicates which kind of expression we have
18381 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18383 tree expr = NULL_TREE;
18384 const char *saved_message;
18386 bool saved_integral_constant_expression_p;
18387 bool saved_non_integral_constant_expression_p;
18388 bool pack_expansion_p = false;
18390 /* Types cannot be defined in a `sizeof' expression. Save away the
18392 saved_message = parser->type_definition_forbidden_message;
18393 /* And create the new one. */
18394 tmp = concat ("types may not be defined in %<",
18395 IDENTIFIER_POINTER (ridpointers[keyword]),
18396 "%> expressions", NULL);
18397 parser->type_definition_forbidden_message = tmp;
18399 /* The restrictions on constant-expressions do not apply inside
18400 sizeof expressions. */
18401 saved_integral_constant_expression_p
18402 = parser->integral_constant_expression_p;
18403 saved_non_integral_constant_expression_p
18404 = parser->non_integral_constant_expression_p;
18405 parser->integral_constant_expression_p = false;
18407 /* If it's a `...', then we are computing the length of a parameter
18409 if (keyword == RID_SIZEOF
18410 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18412 /* Consume the `...'. */
18413 cp_lexer_consume_token (parser->lexer);
18414 maybe_warn_variadic_templates ();
18416 /* Note that this is an expansion. */
18417 pack_expansion_p = true;
18420 /* Do not actually evaluate the expression. */
18422 /* If it's a `(', then we might be looking at the type-id
18424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18427 bool saved_in_type_id_in_expr_p;
18429 /* We can't be sure yet whether we're looking at a type-id or an
18431 cp_parser_parse_tentatively (parser);
18432 /* Consume the `('. */
18433 cp_lexer_consume_token (parser->lexer);
18434 /* Parse the type-id. */
18435 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18436 parser->in_type_id_in_expr_p = true;
18437 type = cp_parser_type_id (parser);
18438 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18439 /* Now, look for the trailing `)'. */
18440 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18441 /* If all went well, then we're done. */
18442 if (cp_parser_parse_definitely (parser))
18444 cp_decl_specifier_seq decl_specs;
18446 /* Build a trivial decl-specifier-seq. */
18447 clear_decl_specs (&decl_specs);
18448 decl_specs.type = type;
18450 /* Call grokdeclarator to figure out what type this is. */
18451 expr = grokdeclarator (NULL,
18455 /*attrlist=*/NULL);
18459 /* If the type-id production did not work out, then we must be
18460 looking at the unary-expression production. */
18462 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18463 /*cast_p=*/false, NULL);
18465 if (pack_expansion_p)
18466 /* Build a pack expansion. */
18467 expr = make_pack_expansion (expr);
18469 /* Go back to evaluating expressions. */
18472 /* Free the message we created. */
18474 /* And restore the old one. */
18475 parser->type_definition_forbidden_message = saved_message;
18476 parser->integral_constant_expression_p
18477 = saved_integral_constant_expression_p;
18478 parser->non_integral_constant_expression_p
18479 = saved_non_integral_constant_expression_p;
18484 /* If the current declaration has no declarator, return true. */
18487 cp_parser_declares_only_class_p (cp_parser *parser)
18489 /* If the next token is a `;' or a `,' then there is no
18491 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18492 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18495 /* Update the DECL_SPECS to reflect the storage class indicated by
18499 cp_parser_set_storage_class (cp_parser *parser,
18500 cp_decl_specifier_seq *decl_specs,
18502 location_t location)
18504 cp_storage_class storage_class;
18506 if (parser->in_unbraced_linkage_specification_p)
18508 error ("%Hinvalid use of %qD in linkage specification",
18509 &location, ridpointers[keyword]);
18512 else if (decl_specs->storage_class != sc_none)
18514 decl_specs->conflicting_specifiers_p = true;
18518 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18519 && decl_specs->specs[(int) ds_thread])
18521 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18522 decl_specs->specs[(int) ds_thread] = 0;
18528 storage_class = sc_auto;
18531 storage_class = sc_register;
18534 storage_class = sc_static;
18537 storage_class = sc_extern;
18540 storage_class = sc_mutable;
18543 gcc_unreachable ();
18545 decl_specs->storage_class = storage_class;
18547 /* A storage class specifier cannot be applied alongside a typedef
18548 specifier. If there is a typedef specifier present then set
18549 conflicting_specifiers_p which will trigger an error later
18550 on in grokdeclarator. */
18551 if (decl_specs->specs[(int)ds_typedef])
18552 decl_specs->conflicting_specifiers_p = true;
18555 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18556 is true, the type is a user-defined type; otherwise it is a
18557 built-in type specified by a keyword. */
18560 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18562 location_t location,
18563 bool user_defined_p)
18565 decl_specs->any_specifiers_p = true;
18567 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18568 (with, for example, in "typedef int wchar_t;") we remember that
18569 this is what happened. In system headers, we ignore these
18570 declarations so that G++ can work with system headers that are not
18572 if (decl_specs->specs[(int) ds_typedef]
18574 && (type_spec == boolean_type_node
18575 || type_spec == char16_type_node
18576 || type_spec == char32_type_node
18577 || type_spec == wchar_type_node)
18578 && (decl_specs->type
18579 || decl_specs->specs[(int) ds_long]
18580 || decl_specs->specs[(int) ds_short]
18581 || decl_specs->specs[(int) ds_unsigned]
18582 || decl_specs->specs[(int) ds_signed]))
18584 decl_specs->redefined_builtin_type = type_spec;
18585 if (!decl_specs->type)
18587 decl_specs->type = type_spec;
18588 decl_specs->user_defined_type_p = false;
18589 decl_specs->type_location = location;
18592 else if (decl_specs->type)
18593 decl_specs->multiple_types_p = true;
18596 decl_specs->type = type_spec;
18597 decl_specs->user_defined_type_p = user_defined_p;
18598 decl_specs->redefined_builtin_type = NULL_TREE;
18599 decl_specs->type_location = location;
18603 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18604 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18607 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18609 return decl_specifiers->specs[(int) ds_friend] != 0;
18612 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18613 issue an error message indicating that TOKEN_DESC was expected.
18615 Returns the token consumed, if the token had the appropriate type.
18616 Otherwise, returns NULL. */
18619 cp_parser_require (cp_parser* parser,
18620 enum cpp_ttype type,
18621 const char* token_desc)
18623 if (cp_lexer_next_token_is (parser->lexer, type))
18624 return cp_lexer_consume_token (parser->lexer);
18627 /* Output the MESSAGE -- unless we're parsing tentatively. */
18628 if (!cp_parser_simulate_error (parser))
18630 char *message = concat ("expected ", token_desc, NULL);
18631 cp_parser_error (parser, message);
18638 /* An error message is produced if the next token is not '>'.
18639 All further tokens are skipped until the desired token is
18640 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18643 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18645 /* Current level of '< ... >'. */
18646 unsigned level = 0;
18647 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18648 unsigned nesting_depth = 0;
18650 /* Are we ready, yet? If not, issue error message. */
18651 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18654 /* Skip tokens until the desired token is found. */
18657 /* Peek at the next token. */
18658 switch (cp_lexer_peek_token (parser->lexer)->type)
18661 if (!nesting_depth)
18666 if (cxx_dialect == cxx98)
18667 /* C++0x views the `>>' operator as two `>' tokens, but
18670 else if (!nesting_depth && level-- == 0)
18672 /* We've hit a `>>' where the first `>' closes the
18673 template argument list, and the second `>' is
18674 spurious. Just consume the `>>' and stop; we've
18675 already produced at least one error. */
18676 cp_lexer_consume_token (parser->lexer);
18679 /* Fall through for C++0x, so we handle the second `>' in
18683 if (!nesting_depth && level-- == 0)
18685 /* We've reached the token we want, consume it and stop. */
18686 cp_lexer_consume_token (parser->lexer);
18691 case CPP_OPEN_PAREN:
18692 case CPP_OPEN_SQUARE:
18696 case CPP_CLOSE_PAREN:
18697 case CPP_CLOSE_SQUARE:
18698 if (nesting_depth-- == 0)
18703 case CPP_PRAGMA_EOL:
18704 case CPP_SEMICOLON:
18705 case CPP_OPEN_BRACE:
18706 case CPP_CLOSE_BRACE:
18707 /* The '>' was probably forgotten, don't look further. */
18714 /* Consume this token. */
18715 cp_lexer_consume_token (parser->lexer);
18719 /* If the next token is the indicated keyword, consume it. Otherwise,
18720 issue an error message indicating that TOKEN_DESC was expected.
18722 Returns the token consumed, if the token had the appropriate type.
18723 Otherwise, returns NULL. */
18726 cp_parser_require_keyword (cp_parser* parser,
18728 const char* token_desc)
18730 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18732 if (token && token->keyword != keyword)
18734 dyn_string_t error_msg;
18736 /* Format the error message. */
18737 error_msg = dyn_string_new (0);
18738 dyn_string_append_cstr (error_msg, "expected ");
18739 dyn_string_append_cstr (error_msg, token_desc);
18740 cp_parser_error (parser, error_msg->s);
18741 dyn_string_delete (error_msg);
18748 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18749 function-definition. */
18752 cp_parser_token_starts_function_definition_p (cp_token* token)
18754 return (/* An ordinary function-body begins with an `{'. */
18755 token->type == CPP_OPEN_BRACE
18756 /* A ctor-initializer begins with a `:'. */
18757 || token->type == CPP_COLON
18758 /* A function-try-block begins with `try'. */
18759 || token->keyword == RID_TRY
18760 /* The named return value extension begins with `return'. */
18761 || token->keyword == RID_RETURN);
18764 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18768 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18772 token = cp_lexer_peek_token (parser->lexer);
18773 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18776 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18777 C++0x) ending a template-argument. */
18780 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18784 token = cp_lexer_peek_token (parser->lexer);
18785 return (token->type == CPP_COMMA
18786 || token->type == CPP_GREATER
18787 || token->type == CPP_ELLIPSIS
18788 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18791 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18792 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18795 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18800 token = cp_lexer_peek_nth_token (parser->lexer, n);
18801 if (token->type == CPP_LESS)
18803 /* Check for the sequence `<::' in the original code. It would be lexed as
18804 `[:', where `[' is a digraph, and there is no whitespace before
18806 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18809 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18810 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18816 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18817 or none_type otherwise. */
18819 static enum tag_types
18820 cp_parser_token_is_class_key (cp_token* token)
18822 switch (token->keyword)
18827 return record_type;
18836 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18839 cp_parser_check_class_key (enum tag_types class_key, tree type)
18841 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18842 permerror (input_location, "%qs tag used in naming %q#T",
18843 class_key == union_type ? "union"
18844 : class_key == record_type ? "struct" : "class",
18848 /* Issue an error message if DECL is redeclared with different
18849 access than its original declaration [class.access.spec/3].
18850 This applies to nested classes and nested class templates.
18854 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18856 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18859 if ((TREE_PRIVATE (decl)
18860 != (current_access_specifier == access_private_node))
18861 || (TREE_PROTECTED (decl)
18862 != (current_access_specifier == access_protected_node)))
18863 error ("%H%qD redeclared with different access", &location, decl);
18866 /* Look for the `template' keyword, as a syntactic disambiguator.
18867 Return TRUE iff it is present, in which case it will be
18871 cp_parser_optional_template_keyword (cp_parser *parser)
18873 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18875 /* The `template' keyword can only be used within templates;
18876 outside templates the parser can always figure out what is a
18877 template and what is not. */
18878 if (!processing_template_decl)
18880 cp_token *token = cp_lexer_peek_token (parser->lexer);
18881 error ("%H%<template%> (as a disambiguator) is only allowed "
18882 "within templates", &token->location);
18883 /* If this part of the token stream is rescanned, the same
18884 error message would be generated. So, we purge the token
18885 from the stream. */
18886 cp_lexer_purge_token (parser->lexer);
18891 /* Consume the `template' keyword. */
18892 cp_lexer_consume_token (parser->lexer);
18900 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18901 set PARSER->SCOPE, and perform other related actions. */
18904 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18907 struct tree_check *check_value;
18908 deferred_access_check *chk;
18909 VEC (deferred_access_check,gc) *checks;
18911 /* Get the stored value. */
18912 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18913 /* Perform any access checks that were deferred. */
18914 checks = check_value->checks;
18918 VEC_iterate (deferred_access_check, checks, i, chk) ;
18921 perform_or_defer_access_check (chk->binfo,
18926 /* Set the scope from the stored value. */
18927 parser->scope = check_value->value;
18928 parser->qualifying_scope = check_value->qualifying_scope;
18929 parser->object_scope = NULL_TREE;
18932 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18933 encounter the end of a block before what we were looking for. */
18936 cp_parser_cache_group (cp_parser *parser,
18937 enum cpp_ttype end,
18942 cp_token *token = cp_lexer_peek_token (parser->lexer);
18944 /* Abort a parenthesized expression if we encounter a semicolon. */
18945 if ((end == CPP_CLOSE_PAREN || depth == 0)
18946 && token->type == CPP_SEMICOLON)
18948 /* If we've reached the end of the file, stop. */
18949 if (token->type == CPP_EOF
18950 || (end != CPP_PRAGMA_EOL
18951 && token->type == CPP_PRAGMA_EOL))
18953 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18954 /* We've hit the end of an enclosing block, so there's been some
18955 kind of syntax error. */
18958 /* Consume the token. */
18959 cp_lexer_consume_token (parser->lexer);
18960 /* See if it starts a new group. */
18961 if (token->type == CPP_OPEN_BRACE)
18963 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18964 /* In theory this should probably check end == '}', but
18965 cp_parser_save_member_function_body needs it to exit
18966 after either '}' or ')' when called with ')'. */
18970 else if (token->type == CPP_OPEN_PAREN)
18972 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18973 if (depth == 0 && end == CPP_CLOSE_PAREN)
18976 else if (token->type == CPP_PRAGMA)
18977 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18978 else if (token->type == end)
18983 /* Begin parsing tentatively. We always save tokens while parsing
18984 tentatively so that if the tentative parsing fails we can restore the
18988 cp_parser_parse_tentatively (cp_parser* parser)
18990 /* Enter a new parsing context. */
18991 parser->context = cp_parser_context_new (parser->context);
18992 /* Begin saving tokens. */
18993 cp_lexer_save_tokens (parser->lexer);
18994 /* In order to avoid repetitive access control error messages,
18995 access checks are queued up until we are no longer parsing
18997 push_deferring_access_checks (dk_deferred);
19000 /* Commit to the currently active tentative parse. */
19003 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19005 cp_parser_context *context;
19008 /* Mark all of the levels as committed. */
19009 lexer = parser->lexer;
19010 for (context = parser->context; context->next; context = context->next)
19012 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19014 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19015 while (!cp_lexer_saving_tokens (lexer))
19016 lexer = lexer->next;
19017 cp_lexer_commit_tokens (lexer);
19021 /* Abort the currently active tentative parse. All consumed tokens
19022 will be rolled back, and no diagnostics will be issued. */
19025 cp_parser_abort_tentative_parse (cp_parser* parser)
19027 cp_parser_simulate_error (parser);
19028 /* Now, pretend that we want to see if the construct was
19029 successfully parsed. */
19030 cp_parser_parse_definitely (parser);
19033 /* Stop parsing tentatively. If a parse error has occurred, restore the
19034 token stream. Otherwise, commit to the tokens we have consumed.
19035 Returns true if no error occurred; false otherwise. */
19038 cp_parser_parse_definitely (cp_parser* parser)
19040 bool error_occurred;
19041 cp_parser_context *context;
19043 /* Remember whether or not an error occurred, since we are about to
19044 destroy that information. */
19045 error_occurred = cp_parser_error_occurred (parser);
19046 /* Remove the topmost context from the stack. */
19047 context = parser->context;
19048 parser->context = context->next;
19049 /* If no parse errors occurred, commit to the tentative parse. */
19050 if (!error_occurred)
19052 /* Commit to the tokens read tentatively, unless that was
19054 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19055 cp_lexer_commit_tokens (parser->lexer);
19057 pop_to_parent_deferring_access_checks ();
19059 /* Otherwise, if errors occurred, roll back our state so that things
19060 are just as they were before we began the tentative parse. */
19063 cp_lexer_rollback_tokens (parser->lexer);
19064 pop_deferring_access_checks ();
19066 /* Add the context to the front of the free list. */
19067 context->next = cp_parser_context_free_list;
19068 cp_parser_context_free_list = context;
19070 return !error_occurred;
19073 /* Returns true if we are parsing tentatively and are not committed to
19074 this tentative parse. */
19077 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19079 return (cp_parser_parsing_tentatively (parser)
19080 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19083 /* Returns nonzero iff an error has occurred during the most recent
19084 tentative parse. */
19087 cp_parser_error_occurred (cp_parser* parser)
19089 return (cp_parser_parsing_tentatively (parser)
19090 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19093 /* Returns nonzero if GNU extensions are allowed. */
19096 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19098 return parser->allow_gnu_extensions_p;
19101 /* Objective-C++ Productions */
19104 /* Parse an Objective-C expression, which feeds into a primary-expression
19108 objc-message-expression
19109 objc-string-literal
19110 objc-encode-expression
19111 objc-protocol-expression
19112 objc-selector-expression
19114 Returns a tree representation of the expression. */
19117 cp_parser_objc_expression (cp_parser* parser)
19119 /* Try to figure out what kind of declaration is present. */
19120 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19124 case CPP_OPEN_SQUARE:
19125 return cp_parser_objc_message_expression (parser);
19127 case CPP_OBJC_STRING:
19128 kwd = cp_lexer_consume_token (parser->lexer);
19129 return objc_build_string_object (kwd->u.value);
19132 switch (kwd->keyword)
19134 case RID_AT_ENCODE:
19135 return cp_parser_objc_encode_expression (parser);
19137 case RID_AT_PROTOCOL:
19138 return cp_parser_objc_protocol_expression (parser);
19140 case RID_AT_SELECTOR:
19141 return cp_parser_objc_selector_expression (parser);
19147 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19148 &kwd->location, kwd->u.value);
19149 cp_parser_skip_to_end_of_block_or_statement (parser);
19152 return error_mark_node;
19155 /* Parse an Objective-C message expression.
19157 objc-message-expression:
19158 [ objc-message-receiver objc-message-args ]
19160 Returns a representation of an Objective-C message. */
19163 cp_parser_objc_message_expression (cp_parser* parser)
19165 tree receiver, messageargs;
19167 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19168 receiver = cp_parser_objc_message_receiver (parser);
19169 messageargs = cp_parser_objc_message_args (parser);
19170 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19172 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19175 /* Parse an objc-message-receiver.
19177 objc-message-receiver:
19179 simple-type-specifier
19181 Returns a representation of the type or expression. */
19184 cp_parser_objc_message_receiver (cp_parser* parser)
19188 /* An Objective-C message receiver may be either (1) a type
19189 or (2) an expression. */
19190 cp_parser_parse_tentatively (parser);
19191 rcv = cp_parser_expression (parser, false, NULL);
19193 if (cp_parser_parse_definitely (parser))
19196 rcv = cp_parser_simple_type_specifier (parser,
19197 /*decl_specs=*/NULL,
19198 CP_PARSER_FLAGS_NONE);
19200 return objc_get_class_reference (rcv);
19203 /* Parse the arguments and selectors comprising an Objective-C message.
19208 objc-selector-args , objc-comma-args
19210 objc-selector-args:
19211 objc-selector [opt] : assignment-expression
19212 objc-selector-args objc-selector [opt] : assignment-expression
19215 assignment-expression
19216 objc-comma-args , assignment-expression
19218 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19219 selector arguments and TREE_VALUE containing a list of comma
19223 cp_parser_objc_message_args (cp_parser* parser)
19225 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19226 bool maybe_unary_selector_p = true;
19227 cp_token *token = cp_lexer_peek_token (parser->lexer);
19229 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19231 tree selector = NULL_TREE, arg;
19233 if (token->type != CPP_COLON)
19234 selector = cp_parser_objc_selector (parser);
19236 /* Detect if we have a unary selector. */
19237 if (maybe_unary_selector_p
19238 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19239 return build_tree_list (selector, NULL_TREE);
19241 maybe_unary_selector_p = false;
19242 cp_parser_require (parser, CPP_COLON, "%<:%>");
19243 arg = cp_parser_assignment_expression (parser, false, NULL);
19246 = chainon (sel_args,
19247 build_tree_list (selector, arg));
19249 token = cp_lexer_peek_token (parser->lexer);
19252 /* Handle non-selector arguments, if any. */
19253 while (token->type == CPP_COMMA)
19257 cp_lexer_consume_token (parser->lexer);
19258 arg = cp_parser_assignment_expression (parser, false, NULL);
19261 = chainon (addl_args,
19262 build_tree_list (NULL_TREE, arg));
19264 token = cp_lexer_peek_token (parser->lexer);
19267 return build_tree_list (sel_args, addl_args);
19270 /* Parse an Objective-C encode expression.
19272 objc-encode-expression:
19273 @encode objc-typename
19275 Returns an encoded representation of the type argument. */
19278 cp_parser_objc_encode_expression (cp_parser* parser)
19283 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19284 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19285 token = cp_lexer_peek_token (parser->lexer);
19286 type = complete_type (cp_parser_type_id (parser));
19287 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19291 error ("%H%<@encode%> must specify a type as an argument",
19293 return error_mark_node;
19296 return objc_build_encode_expr (type);
19299 /* Parse an Objective-C @defs expression. */
19302 cp_parser_objc_defs_expression (cp_parser *parser)
19306 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19307 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19308 name = cp_parser_identifier (parser);
19309 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19311 return objc_get_class_ivars (name);
19314 /* Parse an Objective-C protocol expression.
19316 objc-protocol-expression:
19317 @protocol ( identifier )
19319 Returns a representation of the protocol expression. */
19322 cp_parser_objc_protocol_expression (cp_parser* parser)
19326 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19327 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19328 proto = cp_parser_identifier (parser);
19329 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19331 return objc_build_protocol_expr (proto);
19334 /* Parse an Objective-C selector expression.
19336 objc-selector-expression:
19337 @selector ( objc-method-signature )
19339 objc-method-signature:
19345 objc-selector-seq objc-selector :
19347 Returns a representation of the method selector. */
19350 cp_parser_objc_selector_expression (cp_parser* parser)
19352 tree sel_seq = NULL_TREE;
19353 bool maybe_unary_selector_p = true;
19356 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19357 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19358 token = cp_lexer_peek_token (parser->lexer);
19360 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19361 || token->type == CPP_SCOPE)
19363 tree selector = NULL_TREE;
19365 if (token->type != CPP_COLON
19366 || token->type == CPP_SCOPE)
19367 selector = cp_parser_objc_selector (parser);
19369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19370 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19372 /* Detect if we have a unary selector. */
19373 if (maybe_unary_selector_p)
19375 sel_seq = selector;
19376 goto finish_selector;
19380 cp_parser_error (parser, "expected %<:%>");
19383 maybe_unary_selector_p = false;
19384 token = cp_lexer_consume_token (parser->lexer);
19386 if (token->type == CPP_SCOPE)
19389 = chainon (sel_seq,
19390 build_tree_list (selector, NULL_TREE));
19392 = chainon (sel_seq,
19393 build_tree_list (NULL_TREE, NULL_TREE));
19397 = chainon (sel_seq,
19398 build_tree_list (selector, NULL_TREE));
19400 token = cp_lexer_peek_token (parser->lexer);
19404 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19406 return objc_build_selector_expr (sel_seq);
19409 /* Parse a list of identifiers.
19411 objc-identifier-list:
19413 objc-identifier-list , identifier
19415 Returns a TREE_LIST of identifier nodes. */
19418 cp_parser_objc_identifier_list (cp_parser* parser)
19420 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19421 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19423 while (sep->type == CPP_COMMA)
19425 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19426 list = chainon (list,
19427 build_tree_list (NULL_TREE,
19428 cp_parser_identifier (parser)));
19429 sep = cp_lexer_peek_token (parser->lexer);
19435 /* Parse an Objective-C alias declaration.
19437 objc-alias-declaration:
19438 @compatibility_alias identifier identifier ;
19440 This function registers the alias mapping with the Objective-C front end.
19441 It returns nothing. */
19444 cp_parser_objc_alias_declaration (cp_parser* parser)
19448 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19449 alias = cp_parser_identifier (parser);
19450 orig = cp_parser_identifier (parser);
19451 objc_declare_alias (alias, orig);
19452 cp_parser_consume_semicolon_at_end_of_statement (parser);
19455 /* Parse an Objective-C class forward-declaration.
19457 objc-class-declaration:
19458 @class objc-identifier-list ;
19460 The function registers the forward declarations with the Objective-C
19461 front end. It returns nothing. */
19464 cp_parser_objc_class_declaration (cp_parser* parser)
19466 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19467 objc_declare_class (cp_parser_objc_identifier_list (parser));
19468 cp_parser_consume_semicolon_at_end_of_statement (parser);
19471 /* Parse a list of Objective-C protocol references.
19473 objc-protocol-refs-opt:
19474 objc-protocol-refs [opt]
19476 objc-protocol-refs:
19477 < objc-identifier-list >
19479 Returns a TREE_LIST of identifiers, if any. */
19482 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19484 tree protorefs = NULL_TREE;
19486 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19488 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19489 protorefs = cp_parser_objc_identifier_list (parser);
19490 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19496 /* Parse a Objective-C visibility specification. */
19499 cp_parser_objc_visibility_spec (cp_parser* parser)
19501 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19503 switch (vis->keyword)
19505 case RID_AT_PRIVATE:
19506 objc_set_visibility (2);
19508 case RID_AT_PROTECTED:
19509 objc_set_visibility (0);
19511 case RID_AT_PUBLIC:
19512 objc_set_visibility (1);
19518 /* Eat '@private'/'@protected'/'@public'. */
19519 cp_lexer_consume_token (parser->lexer);
19522 /* Parse an Objective-C method type. */
19525 cp_parser_objc_method_type (cp_parser* parser)
19527 objc_set_method_type
19528 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19533 /* Parse an Objective-C protocol qualifier. */
19536 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19538 tree quals = NULL_TREE, node;
19539 cp_token *token = cp_lexer_peek_token (parser->lexer);
19541 node = token->u.value;
19543 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19544 && (node == ridpointers [(int) RID_IN]
19545 || node == ridpointers [(int) RID_OUT]
19546 || node == ridpointers [(int) RID_INOUT]
19547 || node == ridpointers [(int) RID_BYCOPY]
19548 || node == ridpointers [(int) RID_BYREF]
19549 || node == ridpointers [(int) RID_ONEWAY]))
19551 quals = tree_cons (NULL_TREE, node, quals);
19552 cp_lexer_consume_token (parser->lexer);
19553 token = cp_lexer_peek_token (parser->lexer);
19554 node = token->u.value;
19560 /* Parse an Objective-C typename. */
19563 cp_parser_objc_typename (cp_parser* parser)
19565 tree type_name = NULL_TREE;
19567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19569 tree proto_quals, cp_type = NULL_TREE;
19571 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19572 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19574 /* An ObjC type name may consist of just protocol qualifiers, in which
19575 case the type shall default to 'id'. */
19576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19577 cp_type = cp_parser_type_id (parser);
19579 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19580 type_name = build_tree_list (proto_quals, cp_type);
19586 /* Check to see if TYPE refers to an Objective-C selector name. */
19589 cp_parser_objc_selector_p (enum cpp_ttype type)
19591 return (type == CPP_NAME || type == CPP_KEYWORD
19592 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19593 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19594 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19595 || type == CPP_XOR || type == CPP_XOR_EQ);
19598 /* Parse an Objective-C selector. */
19601 cp_parser_objc_selector (cp_parser* parser)
19603 cp_token *token = cp_lexer_consume_token (parser->lexer);
19605 if (!cp_parser_objc_selector_p (token->type))
19607 error ("%Hinvalid Objective-C++ selector name", &token->location);
19608 return error_mark_node;
19611 /* C++ operator names are allowed to appear in ObjC selectors. */
19612 switch (token->type)
19614 case CPP_AND_AND: return get_identifier ("and");
19615 case CPP_AND_EQ: return get_identifier ("and_eq");
19616 case CPP_AND: return get_identifier ("bitand");
19617 case CPP_OR: return get_identifier ("bitor");
19618 case CPP_COMPL: return get_identifier ("compl");
19619 case CPP_NOT: return get_identifier ("not");
19620 case CPP_NOT_EQ: return get_identifier ("not_eq");
19621 case CPP_OR_OR: return get_identifier ("or");
19622 case CPP_OR_EQ: return get_identifier ("or_eq");
19623 case CPP_XOR: return get_identifier ("xor");
19624 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19625 default: return token->u.value;
19629 /* Parse an Objective-C params list. */
19632 cp_parser_objc_method_keyword_params (cp_parser* parser)
19634 tree params = NULL_TREE;
19635 bool maybe_unary_selector_p = true;
19636 cp_token *token = cp_lexer_peek_token (parser->lexer);
19638 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19640 tree selector = NULL_TREE, type_name, identifier;
19642 if (token->type != CPP_COLON)
19643 selector = cp_parser_objc_selector (parser);
19645 /* Detect if we have a unary selector. */
19646 if (maybe_unary_selector_p
19647 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19650 maybe_unary_selector_p = false;
19651 cp_parser_require (parser, CPP_COLON, "%<:%>");
19652 type_name = cp_parser_objc_typename (parser);
19653 identifier = cp_parser_identifier (parser);
19657 objc_build_keyword_decl (selector,
19661 token = cp_lexer_peek_token (parser->lexer);
19667 /* Parse the non-keyword Objective-C params. */
19670 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19672 tree params = make_node (TREE_LIST);
19673 cp_token *token = cp_lexer_peek_token (parser->lexer);
19674 *ellipsisp = false; /* Initially, assume no ellipsis. */
19676 while (token->type == CPP_COMMA)
19678 cp_parameter_declarator *parmdecl;
19681 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19682 token = cp_lexer_peek_token (parser->lexer);
19684 if (token->type == CPP_ELLIPSIS)
19686 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19691 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19692 parm = grokdeclarator (parmdecl->declarator,
19693 &parmdecl->decl_specifiers,
19694 PARM, /*initialized=*/0,
19695 /*attrlist=*/NULL);
19697 chainon (params, build_tree_list (NULL_TREE, parm));
19698 token = cp_lexer_peek_token (parser->lexer);
19704 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19707 cp_parser_objc_interstitial_code (cp_parser* parser)
19709 cp_token *token = cp_lexer_peek_token (parser->lexer);
19711 /* If the next token is `extern' and the following token is a string
19712 literal, then we have a linkage specification. */
19713 if (token->keyword == RID_EXTERN
19714 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19715 cp_parser_linkage_specification (parser);
19716 /* Handle #pragma, if any. */
19717 else if (token->type == CPP_PRAGMA)
19718 cp_parser_pragma (parser, pragma_external);
19719 /* Allow stray semicolons. */
19720 else if (token->type == CPP_SEMICOLON)
19721 cp_lexer_consume_token (parser->lexer);
19722 /* Finally, try to parse a block-declaration, or a function-definition. */
19724 cp_parser_block_declaration (parser, /*statement_p=*/false);
19727 /* Parse a method signature. */
19730 cp_parser_objc_method_signature (cp_parser* parser)
19732 tree rettype, kwdparms, optparms;
19733 bool ellipsis = false;
19735 cp_parser_objc_method_type (parser);
19736 rettype = cp_parser_objc_typename (parser);
19737 kwdparms = cp_parser_objc_method_keyword_params (parser);
19738 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19740 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19743 /* Pars an Objective-C method prototype list. */
19746 cp_parser_objc_method_prototype_list (cp_parser* parser)
19748 cp_token *token = cp_lexer_peek_token (parser->lexer);
19750 while (token->keyword != RID_AT_END)
19752 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19754 objc_add_method_declaration
19755 (cp_parser_objc_method_signature (parser));
19756 cp_parser_consume_semicolon_at_end_of_statement (parser);
19759 /* Allow for interspersed non-ObjC++ code. */
19760 cp_parser_objc_interstitial_code (parser);
19762 token = cp_lexer_peek_token (parser->lexer);
19765 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19766 objc_finish_interface ();
19769 /* Parse an Objective-C method definition list. */
19772 cp_parser_objc_method_definition_list (cp_parser* parser)
19774 cp_token *token = cp_lexer_peek_token (parser->lexer);
19776 while (token->keyword != RID_AT_END)
19780 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19782 push_deferring_access_checks (dk_deferred);
19783 objc_start_method_definition
19784 (cp_parser_objc_method_signature (parser));
19786 /* For historical reasons, we accept an optional semicolon. */
19787 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19788 cp_lexer_consume_token (parser->lexer);
19790 perform_deferred_access_checks ();
19791 stop_deferring_access_checks ();
19792 meth = cp_parser_function_definition_after_declarator (parser,
19794 pop_deferring_access_checks ();
19795 objc_finish_method_definition (meth);
19798 /* Allow for interspersed non-ObjC++ code. */
19799 cp_parser_objc_interstitial_code (parser);
19801 token = cp_lexer_peek_token (parser->lexer);
19804 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19805 objc_finish_implementation ();
19808 /* Parse Objective-C ivars. */
19811 cp_parser_objc_class_ivars (cp_parser* parser)
19813 cp_token *token = cp_lexer_peek_token (parser->lexer);
19815 if (token->type != CPP_OPEN_BRACE)
19816 return; /* No ivars specified. */
19818 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19819 token = cp_lexer_peek_token (parser->lexer);
19821 while (token->type != CPP_CLOSE_BRACE)
19823 cp_decl_specifier_seq declspecs;
19824 int decl_class_or_enum_p;
19825 tree prefix_attributes;
19827 cp_parser_objc_visibility_spec (parser);
19829 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19832 cp_parser_decl_specifier_seq (parser,
19833 CP_PARSER_FLAGS_OPTIONAL,
19835 &decl_class_or_enum_p);
19836 prefix_attributes = declspecs.attributes;
19837 declspecs.attributes = NULL_TREE;
19839 /* Keep going until we hit the `;' at the end of the
19841 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19843 tree width = NULL_TREE, attributes, first_attribute, decl;
19844 cp_declarator *declarator = NULL;
19845 int ctor_dtor_or_conv_p;
19847 /* Check for a (possibly unnamed) bitfield declaration. */
19848 token = cp_lexer_peek_token (parser->lexer);
19849 if (token->type == CPP_COLON)
19852 if (token->type == CPP_NAME
19853 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19856 /* Get the name of the bitfield. */
19857 declarator = make_id_declarator (NULL_TREE,
19858 cp_parser_identifier (parser),
19862 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19863 /* Get the width of the bitfield. */
19865 = cp_parser_constant_expression (parser,
19866 /*allow_non_constant=*/false,
19871 /* Parse the declarator. */
19873 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19874 &ctor_dtor_or_conv_p,
19875 /*parenthesized_p=*/NULL,
19876 /*member_p=*/false);
19879 /* Look for attributes that apply to the ivar. */
19880 attributes = cp_parser_attributes_opt (parser);
19881 /* Remember which attributes are prefix attributes and
19883 first_attribute = attributes;
19884 /* Combine the attributes. */
19885 attributes = chainon (prefix_attributes, attributes);
19888 /* Create the bitfield declaration. */
19889 decl = grokbitfield (declarator, &declspecs,
19893 decl = grokfield (declarator, &declspecs,
19894 NULL_TREE, /*init_const_expr_p=*/false,
19895 NULL_TREE, attributes);
19897 /* Add the instance variable. */
19898 objc_add_instance_variable (decl);
19900 /* Reset PREFIX_ATTRIBUTES. */
19901 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19902 attributes = TREE_CHAIN (attributes);
19904 TREE_CHAIN (attributes) = NULL_TREE;
19906 token = cp_lexer_peek_token (parser->lexer);
19908 if (token->type == CPP_COMMA)
19910 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19916 cp_parser_consume_semicolon_at_end_of_statement (parser);
19917 token = cp_lexer_peek_token (parser->lexer);
19920 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19921 /* For historical reasons, we accept an optional semicolon. */
19922 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19923 cp_lexer_consume_token (parser->lexer);
19926 /* Parse an Objective-C protocol declaration. */
19929 cp_parser_objc_protocol_declaration (cp_parser* parser)
19931 tree proto, protorefs;
19934 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19937 tok = cp_lexer_peek_token (parser->lexer);
19938 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19942 /* See if we have a forward declaration or a definition. */
19943 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19945 /* Try a forward declaration first. */
19946 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19948 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19950 cp_parser_consume_semicolon_at_end_of_statement (parser);
19953 /* Ok, we got a full-fledged definition (or at least should). */
19956 proto = cp_parser_identifier (parser);
19957 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19958 objc_start_protocol (proto, protorefs);
19959 cp_parser_objc_method_prototype_list (parser);
19963 /* Parse an Objective-C superclass or category. */
19966 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19969 cp_token *next = cp_lexer_peek_token (parser->lexer);
19971 *super = *categ = NULL_TREE;
19972 if (next->type == CPP_COLON)
19974 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19975 *super = cp_parser_identifier (parser);
19977 else if (next->type == CPP_OPEN_PAREN)
19979 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19980 *categ = cp_parser_identifier (parser);
19981 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19985 /* Parse an Objective-C class interface. */
19988 cp_parser_objc_class_interface (cp_parser* parser)
19990 tree name, super, categ, protos;
19992 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19993 name = cp_parser_identifier (parser);
19994 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19995 protos = cp_parser_objc_protocol_refs_opt (parser);
19997 /* We have either a class or a category on our hands. */
19999 objc_start_category_interface (name, categ, protos);
20002 objc_start_class_interface (name, super, protos);
20003 /* Handle instance variable declarations, if any. */
20004 cp_parser_objc_class_ivars (parser);
20005 objc_continue_interface ();
20008 cp_parser_objc_method_prototype_list (parser);
20011 /* Parse an Objective-C class implementation. */
20014 cp_parser_objc_class_implementation (cp_parser* parser)
20016 tree name, super, categ;
20018 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20019 name = cp_parser_identifier (parser);
20020 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20022 /* We have either a class or a category on our hands. */
20024 objc_start_category_implementation (name, categ);
20027 objc_start_class_implementation (name, super);
20028 /* Handle instance variable declarations, if any. */
20029 cp_parser_objc_class_ivars (parser);
20030 objc_continue_implementation ();
20033 cp_parser_objc_method_definition_list (parser);
20036 /* Consume the @end token and finish off the implementation. */
20039 cp_parser_objc_end_implementation (cp_parser* parser)
20041 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20042 objc_finish_implementation ();
20045 /* Parse an Objective-C declaration. */
20048 cp_parser_objc_declaration (cp_parser* parser)
20050 /* Try to figure out what kind of declaration is present. */
20051 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20053 switch (kwd->keyword)
20056 cp_parser_objc_alias_declaration (parser);
20059 cp_parser_objc_class_declaration (parser);
20061 case RID_AT_PROTOCOL:
20062 cp_parser_objc_protocol_declaration (parser);
20064 case RID_AT_INTERFACE:
20065 cp_parser_objc_class_interface (parser);
20067 case RID_AT_IMPLEMENTATION:
20068 cp_parser_objc_class_implementation (parser);
20071 cp_parser_objc_end_implementation (parser);
20074 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20075 &kwd->location, kwd->u.value);
20076 cp_parser_skip_to_end_of_block_or_statement (parser);
20080 /* Parse an Objective-C try-catch-finally statement.
20082 objc-try-catch-finally-stmt:
20083 @try compound-statement objc-catch-clause-seq [opt]
20084 objc-finally-clause [opt]
20086 objc-catch-clause-seq:
20087 objc-catch-clause objc-catch-clause-seq [opt]
20090 @catch ( exception-declaration ) compound-statement
20092 objc-finally-clause
20093 @finally compound-statement
20095 Returns NULL_TREE. */
20098 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20099 location_t location;
20102 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20103 location = cp_lexer_peek_token (parser->lexer)->location;
20104 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20105 node, lest it get absorbed into the surrounding block. */
20106 stmt = push_stmt_list ();
20107 cp_parser_compound_statement (parser, NULL, false);
20108 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20110 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20112 cp_parameter_declarator *parmdecl;
20115 cp_lexer_consume_token (parser->lexer);
20116 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20117 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20118 parm = grokdeclarator (parmdecl->declarator,
20119 &parmdecl->decl_specifiers,
20120 PARM, /*initialized=*/0,
20121 /*attrlist=*/NULL);
20122 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20123 objc_begin_catch_clause (parm);
20124 cp_parser_compound_statement (parser, NULL, false);
20125 objc_finish_catch_clause ();
20128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20130 cp_lexer_consume_token (parser->lexer);
20131 location = cp_lexer_peek_token (parser->lexer)->location;
20132 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20133 node, lest it get absorbed into the surrounding block. */
20134 stmt = push_stmt_list ();
20135 cp_parser_compound_statement (parser, NULL, false);
20136 objc_build_finally_clause (location, pop_stmt_list (stmt));
20139 return objc_finish_try_stmt ();
20142 /* Parse an Objective-C synchronized statement.
20144 objc-synchronized-stmt:
20145 @synchronized ( expression ) compound-statement
20147 Returns NULL_TREE. */
20150 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20151 location_t location;
20154 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20156 location = cp_lexer_peek_token (parser->lexer)->location;
20157 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20158 lock = cp_parser_expression (parser, false, NULL);
20159 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20161 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20162 node, lest it get absorbed into the surrounding block. */
20163 stmt = push_stmt_list ();
20164 cp_parser_compound_statement (parser, NULL, false);
20166 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20169 /* Parse an Objective-C throw statement.
20172 @throw assignment-expression [opt] ;
20174 Returns a constructed '@throw' statement. */
20177 cp_parser_objc_throw_statement (cp_parser *parser) {
20178 tree expr = NULL_TREE;
20180 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20183 expr = cp_parser_assignment_expression (parser, false, NULL);
20185 cp_parser_consume_semicolon_at_end_of_statement (parser);
20187 return objc_build_throw_stmt (expr);
20190 /* Parse an Objective-C statement. */
20193 cp_parser_objc_statement (cp_parser * parser) {
20194 /* Try to figure out what kind of declaration is present. */
20195 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20197 switch (kwd->keyword)
20200 return cp_parser_objc_try_catch_finally_statement (parser);
20201 case RID_AT_SYNCHRONIZED:
20202 return cp_parser_objc_synchronized_statement (parser);
20204 return cp_parser_objc_throw_statement (parser);
20206 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20207 &kwd->location, kwd->u.value);
20208 cp_parser_skip_to_end_of_block_or_statement (parser);
20211 return error_mark_node;
20214 /* OpenMP 2.5 parsing routines. */
20216 /* Returns name of the next clause.
20217 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20218 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20219 returned and the token is consumed. */
20221 static pragma_omp_clause
20222 cp_parser_omp_clause_name (cp_parser *parser)
20224 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20227 result = PRAGMA_OMP_CLAUSE_IF;
20228 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20229 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20230 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20231 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20232 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20234 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20235 const char *p = IDENTIFIER_POINTER (id);
20240 if (!strcmp ("collapse", p))
20241 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20242 else if (!strcmp ("copyin", p))
20243 result = PRAGMA_OMP_CLAUSE_COPYIN;
20244 else if (!strcmp ("copyprivate", p))
20245 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20248 if (!strcmp ("firstprivate", p))
20249 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20252 if (!strcmp ("lastprivate", p))
20253 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20256 if (!strcmp ("nowait", p))
20257 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20258 else if (!strcmp ("num_threads", p))
20259 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20262 if (!strcmp ("ordered", p))
20263 result = PRAGMA_OMP_CLAUSE_ORDERED;
20266 if (!strcmp ("reduction", p))
20267 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20270 if (!strcmp ("schedule", p))
20271 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20272 else if (!strcmp ("shared", p))
20273 result = PRAGMA_OMP_CLAUSE_SHARED;
20276 if (!strcmp ("untied", p))
20277 result = PRAGMA_OMP_CLAUSE_UNTIED;
20282 if (result != PRAGMA_OMP_CLAUSE_NONE)
20283 cp_lexer_consume_token (parser->lexer);
20288 /* Validate that a clause of the given type does not already exist. */
20291 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20292 const char *name, location_t location)
20296 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20297 if (OMP_CLAUSE_CODE (c) == code)
20299 error ("%Htoo many %qs clauses", &location, name);
20307 variable-list , identifier
20309 In addition, we match a closing parenthesis. An opening parenthesis
20310 will have been consumed by the caller.
20312 If KIND is nonzero, create the appropriate node and install the decl
20313 in OMP_CLAUSE_DECL and add the node to the head of the list.
20315 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20316 return the list created. */
20319 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20327 token = cp_lexer_peek_token (parser->lexer);
20328 name = cp_parser_id_expression (parser, /*template_p=*/false,
20329 /*check_dependency_p=*/true,
20330 /*template_p=*/NULL,
20331 /*declarator_p=*/false,
20332 /*optional_p=*/false);
20333 if (name == error_mark_node)
20336 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20337 if (decl == error_mark_node)
20338 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20339 else if (kind != 0)
20341 tree u = build_omp_clause (kind);
20342 OMP_CLAUSE_DECL (u) = decl;
20343 OMP_CLAUSE_CHAIN (u) = list;
20347 list = tree_cons (decl, NULL_TREE, list);
20350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20352 cp_lexer_consume_token (parser->lexer);
20355 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20359 /* Try to resync to an unnested comma. Copied from
20360 cp_parser_parenthesized_expression_list. */
20362 ending = cp_parser_skip_to_closing_parenthesis (parser,
20363 /*recovering=*/true,
20365 /*consume_paren=*/true);
20373 /* Similarly, but expect leading and trailing parenthesis. This is a very
20374 common case for omp clauses. */
20377 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20379 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20380 return cp_parser_omp_var_list_no_open (parser, kind, list);
20385 collapse ( constant-expression ) */
20388 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20394 loc = cp_lexer_peek_token (parser->lexer)->location;
20395 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20398 num = cp_parser_constant_expression (parser, false, NULL);
20400 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20402 /*or_comma=*/false,
20403 /*consume_paren=*/true);
20405 if (num == error_mark_node)
20407 num = fold_non_dependent_expr (num);
20408 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20409 || !host_integerp (num, 0)
20410 || (n = tree_low_cst (num, 0)) <= 0
20413 error ("%Hcollapse argument needs positive constant integer expression",
20418 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20419 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20420 OMP_CLAUSE_CHAIN (c) = list;
20421 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20427 default ( shared | none ) */
20430 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20432 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20435 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20437 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20439 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20440 const char *p = IDENTIFIER_POINTER (id);
20445 if (strcmp ("none", p) != 0)
20447 kind = OMP_CLAUSE_DEFAULT_NONE;
20451 if (strcmp ("shared", p) != 0)
20453 kind = OMP_CLAUSE_DEFAULT_SHARED;
20460 cp_lexer_consume_token (parser->lexer);
20465 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20470 /*or_comma=*/false,
20471 /*consume_paren=*/true);
20473 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20476 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20477 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20478 OMP_CLAUSE_CHAIN (c) = list;
20479 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20485 if ( expression ) */
20488 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20495 t = cp_parser_condition (parser);
20497 if (t == error_mark_node
20498 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20500 /*or_comma=*/false,
20501 /*consume_paren=*/true);
20503 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20505 c = build_omp_clause (OMP_CLAUSE_IF);
20506 OMP_CLAUSE_IF_EXPR (c) = t;
20507 OMP_CLAUSE_CHAIN (c) = list;
20516 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20517 tree list, location_t location)
20521 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20523 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20524 OMP_CLAUSE_CHAIN (c) = list;
20529 num_threads ( expression ) */
20532 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20533 location_t location)
20537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20540 t = cp_parser_expression (parser, false, NULL);
20542 if (t == error_mark_node
20543 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20545 /*or_comma=*/false,
20546 /*consume_paren=*/true);
20548 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20549 "num_threads", location);
20551 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20552 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20553 OMP_CLAUSE_CHAIN (c) = list;
20562 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20563 tree list, location_t location)
20567 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20568 "ordered", location);
20570 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20571 OMP_CLAUSE_CHAIN (c) = list;
20576 reduction ( reduction-operator : variable-list )
20578 reduction-operator:
20579 One of: + * - & ^ | && || */
20582 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20584 enum tree_code code;
20587 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20590 switch (cp_lexer_peek_token (parser->lexer)->type)
20602 code = BIT_AND_EXPR;
20605 code = BIT_XOR_EXPR;
20608 code = BIT_IOR_EXPR;
20611 code = TRUTH_ANDIF_EXPR;
20614 code = TRUTH_ORIF_EXPR;
20617 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20618 "%<|%>, %<&&%>, or %<||%>");
20620 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20621 /*or_comma=*/false,
20622 /*consume_paren=*/true);
20625 cp_lexer_consume_token (parser->lexer);
20627 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20630 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20631 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20632 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20638 schedule ( schedule-kind )
20639 schedule ( schedule-kind , expression )
20642 static | dynamic | guided | runtime | auto */
20645 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20649 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20652 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20657 const char *p = IDENTIFIER_POINTER (id);
20662 if (strcmp ("dynamic", p) != 0)
20664 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20668 if (strcmp ("guided", p) != 0)
20670 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20674 if (strcmp ("runtime", p) != 0)
20676 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20683 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20684 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20685 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20686 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20689 cp_lexer_consume_token (parser->lexer);
20691 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20694 cp_lexer_consume_token (parser->lexer);
20696 token = cp_lexer_peek_token (parser->lexer);
20697 t = cp_parser_assignment_expression (parser, false, NULL);
20699 if (t == error_mark_node)
20701 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20702 error ("%Hschedule %<runtime%> does not take "
20703 "a %<chunk_size%> parameter", &token->location);
20704 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20705 error ("%Hschedule %<auto%> does not take "
20706 "a %<chunk_size%> parameter", &token->location);
20708 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20710 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20713 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20716 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20717 OMP_CLAUSE_CHAIN (c) = list;
20721 cp_parser_error (parser, "invalid schedule kind");
20723 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20724 /*or_comma=*/false,
20725 /*consume_paren=*/true);
20733 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20734 tree list, location_t location)
20738 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20740 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20741 OMP_CLAUSE_CHAIN (c) = list;
20745 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20746 is a bitmask in MASK. Return the list of clauses found; the result
20747 of clause default goes in *pdefault. */
20750 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20751 const char *where, cp_token *pragma_tok)
20753 tree clauses = NULL;
20755 cp_token *token = NULL;
20757 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20759 pragma_omp_clause c_kind;
20760 const char *c_name;
20761 tree prev = clauses;
20763 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20764 cp_lexer_consume_token (parser->lexer);
20766 token = cp_lexer_peek_token (parser->lexer);
20767 c_kind = cp_parser_omp_clause_name (parser);
20772 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20773 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20775 c_name = "collapse";
20777 case PRAGMA_OMP_CLAUSE_COPYIN:
20778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20781 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20782 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20784 c_name = "copyprivate";
20786 case PRAGMA_OMP_CLAUSE_DEFAULT:
20787 clauses = cp_parser_omp_clause_default (parser, clauses,
20789 c_name = "default";
20791 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20794 c_name = "firstprivate";
20796 case PRAGMA_OMP_CLAUSE_IF:
20797 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20800 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20801 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20803 c_name = "lastprivate";
20805 case PRAGMA_OMP_CLAUSE_NOWAIT:
20806 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20809 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20810 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20812 c_name = "num_threads";
20814 case PRAGMA_OMP_CLAUSE_ORDERED:
20815 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20817 c_name = "ordered";
20819 case PRAGMA_OMP_CLAUSE_PRIVATE:
20820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20822 c_name = "private";
20824 case PRAGMA_OMP_CLAUSE_REDUCTION:
20825 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20826 c_name = "reduction";
20828 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20829 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20831 c_name = "schedule";
20833 case PRAGMA_OMP_CLAUSE_SHARED:
20834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20838 case PRAGMA_OMP_CLAUSE_UNTIED:
20839 clauses = cp_parser_omp_clause_untied (parser, clauses,
20844 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20848 if (((mask >> c_kind) & 1) == 0)
20850 /* Remove the invalid clause(s) from the list to avoid
20851 confusing the rest of the compiler. */
20853 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20857 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20858 return finish_omp_clauses (clauses);
20865 In practice, we're also interested in adding the statement to an
20866 outer node. So it is convenient if we work around the fact that
20867 cp_parser_statement calls add_stmt. */
20870 cp_parser_begin_omp_structured_block (cp_parser *parser)
20872 unsigned save = parser->in_statement;
20874 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20875 This preserves the "not within loop or switch" style error messages
20876 for nonsense cases like
20882 if (parser->in_statement)
20883 parser->in_statement = IN_OMP_BLOCK;
20889 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20891 parser->in_statement = save;
20895 cp_parser_omp_structured_block (cp_parser *parser)
20897 tree stmt = begin_omp_structured_block ();
20898 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20900 cp_parser_statement (parser, NULL_TREE, false, NULL);
20902 cp_parser_end_omp_structured_block (parser, save);
20903 return finish_omp_structured_block (stmt);
20907 # pragma omp atomic new-line
20911 x binop= expr | x++ | ++x | x-- | --x
20913 +, *, -, /, &, ^, |, <<, >>
20915 where x is an lvalue expression with scalar type. */
20918 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20921 enum tree_code code;
20923 cp_parser_require_pragma_eol (parser, pragma_tok);
20925 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20926 /*cast_p=*/false, NULL);
20927 switch (TREE_CODE (lhs))
20932 case PREINCREMENT_EXPR:
20933 case POSTINCREMENT_EXPR:
20934 lhs = TREE_OPERAND (lhs, 0);
20936 rhs = integer_one_node;
20939 case PREDECREMENT_EXPR:
20940 case POSTDECREMENT_EXPR:
20941 lhs = TREE_OPERAND (lhs, 0);
20943 rhs = integer_one_node;
20947 switch (cp_lexer_peek_token (parser->lexer)->type)
20953 code = TRUNC_DIV_EXPR;
20961 case CPP_LSHIFT_EQ:
20962 code = LSHIFT_EXPR;
20964 case CPP_RSHIFT_EQ:
20965 code = RSHIFT_EXPR;
20968 code = BIT_AND_EXPR;
20971 code = BIT_IOR_EXPR;
20974 code = BIT_XOR_EXPR;
20977 cp_parser_error (parser,
20978 "invalid operator for %<#pragma omp atomic%>");
20981 cp_lexer_consume_token (parser->lexer);
20983 rhs = cp_parser_expression (parser, false, NULL);
20984 if (rhs == error_mark_node)
20988 finish_omp_atomic (code, lhs, rhs);
20989 cp_parser_consume_semicolon_at_end_of_statement (parser);
20993 cp_parser_skip_to_end_of_block_or_statement (parser);
20998 # pragma omp barrier new-line */
21001 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21003 cp_parser_require_pragma_eol (parser, pragma_tok);
21004 finish_omp_barrier ();
21008 # pragma omp critical [(name)] new-line
21009 structured-block */
21012 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21014 tree stmt, name = NULL;
21016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21018 cp_lexer_consume_token (parser->lexer);
21020 name = cp_parser_identifier (parser);
21022 if (name == error_mark_node
21023 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21025 /*or_comma=*/false,
21026 /*consume_paren=*/true);
21027 if (name == error_mark_node)
21030 cp_parser_require_pragma_eol (parser, pragma_tok);
21032 stmt = cp_parser_omp_structured_block (parser);
21033 return c_finish_omp_critical (stmt, name);
21037 # pragma omp flush flush-vars[opt] new-line
21040 ( variable-list ) */
21043 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21046 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21047 cp_parser_require_pragma_eol (parser, pragma_tok);
21049 finish_omp_flush ();
21052 /* Helper function, to parse omp for increment expression. */
21055 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21057 tree cond = cp_parser_binary_expression (parser, false, true,
21058 PREC_NOT_OPERATOR, NULL);
21061 if (cond == error_mark_node
21062 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21064 cp_parser_skip_to_end_of_statement (parser);
21065 return error_mark_node;
21068 switch (TREE_CODE (cond))
21076 return error_mark_node;
21079 /* If decl is an iterator, preserve LHS and RHS of the relational
21080 expr until finish_omp_for. */
21082 && (type_dependent_expression_p (decl)
21083 || CLASS_TYPE_P (TREE_TYPE (decl))))
21086 return build_x_binary_op (TREE_CODE (cond),
21087 TREE_OPERAND (cond, 0), ERROR_MARK,
21088 TREE_OPERAND (cond, 1), ERROR_MARK,
21089 &overloaded_p, tf_warning_or_error);
21092 /* Helper function, to parse omp for increment expression. */
21095 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21097 cp_token *token = cp_lexer_peek_token (parser->lexer);
21103 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21105 op = (token->type == CPP_PLUS_PLUS
21106 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21107 cp_lexer_consume_token (parser->lexer);
21108 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21110 return error_mark_node;
21111 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21114 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21116 return error_mark_node;
21118 token = cp_lexer_peek_token (parser->lexer);
21119 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21121 op = (token->type == CPP_PLUS_PLUS
21122 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21123 cp_lexer_consume_token (parser->lexer);
21124 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21127 op = cp_parser_assignment_operator_opt (parser);
21128 if (op == ERROR_MARK)
21129 return error_mark_node;
21131 if (op != NOP_EXPR)
21133 rhs = cp_parser_assignment_expression (parser, false, NULL);
21134 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21135 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21138 lhs = cp_parser_binary_expression (parser, false, false,
21139 PREC_ADDITIVE_EXPRESSION, NULL);
21140 token = cp_lexer_peek_token (parser->lexer);
21141 decl_first = lhs == decl;
21144 if (token->type != CPP_PLUS
21145 && token->type != CPP_MINUS)
21146 return error_mark_node;
21150 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21151 cp_lexer_consume_token (parser->lexer);
21152 rhs = cp_parser_binary_expression (parser, false, false,
21153 PREC_ADDITIVE_EXPRESSION, NULL);
21154 token = cp_lexer_peek_token (parser->lexer);
21155 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21157 if (lhs == NULL_TREE)
21159 if (op == PLUS_EXPR)
21162 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21165 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21166 NULL, tf_warning_or_error);
21169 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21173 if (rhs != decl || op == MINUS_EXPR)
21174 return error_mark_node;
21175 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21178 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21180 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21183 /* Parse the restricted form of the for statement allowed by OpenMP. */
21186 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21188 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21189 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21190 tree this_pre_body, cl;
21191 location_t loc_first;
21192 bool collapse_err = false;
21193 int i, collapse = 1, nbraces = 0;
21195 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21196 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21197 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21199 gcc_assert (collapse >= 1);
21201 declv = make_tree_vec (collapse);
21202 initv = make_tree_vec (collapse);
21203 condv = make_tree_vec (collapse);
21204 incrv = make_tree_vec (collapse);
21206 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21208 for (i = 0; i < collapse; i++)
21210 int bracecount = 0;
21211 bool add_private_clause = false;
21214 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21216 cp_parser_error (parser, "for statement expected");
21219 loc = cp_lexer_consume_token (parser->lexer)->location;
21221 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21224 init = decl = real_decl = NULL;
21225 this_pre_body = push_stmt_list ();
21226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21228 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21232 integer-type var = lb
21233 random-access-iterator-type var = lb
21234 pointer-type var = lb
21236 cp_decl_specifier_seq type_specifiers;
21238 /* First, try to parse as an initialized declaration. See
21239 cp_parser_condition, from whence the bulk of this is copied. */
21241 cp_parser_parse_tentatively (parser);
21242 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21244 if (cp_parser_parse_definitely (parser))
21246 /* If parsing a type specifier seq succeeded, then this
21247 MUST be a initialized declaration. */
21248 tree asm_specification, attributes;
21249 cp_declarator *declarator;
21251 declarator = cp_parser_declarator (parser,
21252 CP_PARSER_DECLARATOR_NAMED,
21253 /*ctor_dtor_or_conv_p=*/NULL,
21254 /*parenthesized_p=*/NULL,
21255 /*member_p=*/false);
21256 attributes = cp_parser_attributes_opt (parser);
21257 asm_specification = cp_parser_asm_specification_opt (parser);
21259 if (declarator == cp_error_declarator)
21260 cp_parser_skip_to_end_of_statement (parser);
21264 tree pushed_scope, auto_node;
21266 decl = start_decl (declarator, &type_specifiers,
21267 SD_INITIALIZED, attributes,
21268 /*prefix_attributes=*/NULL_TREE,
21271 auto_node = type_uses_auto (TREE_TYPE (decl));
21272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21274 if (cp_lexer_next_token_is (parser->lexer,
21276 error ("parenthesized initialization is not allowed in "
21277 "OpenMP %<for%> loop");
21279 /* Trigger an error. */
21280 cp_parser_require (parser, CPP_EQ, "%<=%>");
21282 init = error_mark_node;
21283 cp_parser_skip_to_end_of_statement (parser);
21285 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21286 || type_dependent_expression_p (decl)
21289 bool is_direct_init, is_non_constant_init;
21291 init = cp_parser_initializer (parser,
21293 &is_non_constant_init);
21295 if (auto_node && describable_type (init))
21298 = do_auto_deduction (TREE_TYPE (decl), init,
21301 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21302 && !type_dependent_expression_p (decl))
21306 cp_finish_decl (decl, init, !is_non_constant_init,
21308 LOOKUP_ONLYCONVERTING);
21309 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21312 = tree_cons (NULL, this_pre_body, for_block);
21316 init = pop_stmt_list (this_pre_body);
21317 this_pre_body = NULL_TREE;
21322 cp_lexer_consume_token (parser->lexer);
21323 init = cp_parser_assignment_expression (parser, false, NULL);
21326 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21327 init = error_mark_node;
21329 cp_finish_decl (decl, NULL_TREE,
21330 /*init_const_expr_p=*/false,
21332 LOOKUP_ONLYCONVERTING);
21336 pop_scope (pushed_scope);
21342 /* If parsing a type specifier sequence failed, then
21343 this MUST be a simple expression. */
21344 cp_parser_parse_tentatively (parser);
21345 decl = cp_parser_primary_expression (parser, false, false,
21347 if (!cp_parser_error_occurred (parser)
21350 && CLASS_TYPE_P (TREE_TYPE (decl)))
21354 cp_parser_parse_definitely (parser);
21355 cp_parser_require (parser, CPP_EQ, "%<=%>");
21356 rhs = cp_parser_assignment_expression (parser, false, NULL);
21357 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21359 tf_warning_or_error));
21360 add_private_clause = true;
21365 cp_parser_abort_tentative_parse (parser);
21366 init = cp_parser_expression (parser, false, NULL);
21369 if (TREE_CODE (init) == MODIFY_EXPR
21370 || TREE_CODE (init) == MODOP_EXPR)
21371 real_decl = TREE_OPERAND (init, 0);
21376 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21379 this_pre_body = pop_stmt_list (this_pre_body);
21383 pre_body = push_stmt_list ();
21385 add_stmt (this_pre_body);
21386 pre_body = pop_stmt_list (pre_body);
21389 pre_body = this_pre_body;
21394 if (par_clauses != NULL && real_decl != NULL_TREE)
21397 for (c = par_clauses; *c ; )
21398 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21399 && OMP_CLAUSE_DECL (*c) == real_decl)
21401 error ("%Hiteration variable %qD should not be firstprivate",
21403 *c = OMP_CLAUSE_CHAIN (*c);
21405 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21406 && OMP_CLAUSE_DECL (*c) == real_decl)
21408 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21409 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21410 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21411 OMP_CLAUSE_DECL (l) = real_decl;
21412 OMP_CLAUSE_CHAIN (l) = clauses;
21413 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21415 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21416 CP_OMP_CLAUSE_INFO (*c) = NULL;
21417 add_private_clause = false;
21421 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21422 && OMP_CLAUSE_DECL (*c) == real_decl)
21423 add_private_clause = false;
21424 c = &OMP_CLAUSE_CHAIN (*c);
21428 if (add_private_clause)
21431 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21433 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21434 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21435 && OMP_CLAUSE_DECL (c) == decl)
21437 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21438 && OMP_CLAUSE_DECL (c) == decl)
21439 error ("%Hiteration variable %qD should not be firstprivate",
21441 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21442 && OMP_CLAUSE_DECL (c) == decl)
21443 error ("%Hiteration variable %qD should not be reduction",
21448 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21449 OMP_CLAUSE_DECL (c) = decl;
21450 c = finish_omp_clauses (c);
21453 OMP_CLAUSE_CHAIN (c) = clauses;
21460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21461 cond = cp_parser_omp_for_cond (parser, decl);
21462 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21467 /* If decl is an iterator, preserve the operator on decl
21468 until finish_omp_for. */
21470 && (type_dependent_expression_p (decl)
21471 || CLASS_TYPE_P (TREE_TYPE (decl))))
21472 incr = cp_parser_omp_for_incr (parser, decl);
21474 incr = cp_parser_expression (parser, false, NULL);
21477 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21478 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21479 /*or_comma=*/false,
21480 /*consume_paren=*/true);
21482 TREE_VEC_ELT (declv, i) = decl;
21483 TREE_VEC_ELT (initv, i) = init;
21484 TREE_VEC_ELT (condv, i) = cond;
21485 TREE_VEC_ELT (incrv, i) = incr;
21487 if (i == collapse - 1)
21490 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21491 in between the collapsed for loops to be still considered perfectly
21492 nested. Hopefully the final version clarifies this.
21493 For now handle (multiple) {'s and empty statements. */
21494 cp_parser_parse_tentatively (parser);
21497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21499 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21501 cp_lexer_consume_token (parser->lexer);
21504 else if (bracecount
21505 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21506 cp_lexer_consume_token (parser->lexer);
21509 loc = cp_lexer_peek_token (parser->lexer)->location;
21510 error ("%Hnot enough collapsed for loops", &loc);
21511 collapse_err = true;
21512 cp_parser_abort_tentative_parse (parser);
21521 cp_parser_parse_definitely (parser);
21522 nbraces += bracecount;
21526 /* Note that we saved the original contents of this flag when we entered
21527 the structured block, and so we don't need to re-save it here. */
21528 parser->in_statement = IN_OMP_FOR;
21530 /* Note that the grammar doesn't call for a structured block here,
21531 though the loop as a whole is a structured block. */
21532 body = push_stmt_list ();
21533 cp_parser_statement (parser, NULL_TREE, false, NULL);
21534 body = pop_stmt_list (body);
21536 if (declv == NULL_TREE)
21539 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21540 pre_body, clauses);
21544 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21546 cp_lexer_consume_token (parser->lexer);
21549 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21550 cp_lexer_consume_token (parser->lexer);
21555 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21556 error ("%Hcollapsed loops not perfectly nested", &loc);
21558 collapse_err = true;
21559 cp_parser_statement_seq_opt (parser, NULL);
21560 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21566 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21567 for_block = TREE_CHAIN (for_block);
21574 #pragma omp for for-clause[optseq] new-line
21577 #define OMP_FOR_CLAUSE_MASK \
21578 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21579 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21580 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21581 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21582 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21583 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21584 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21585 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21588 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21590 tree clauses, sb, ret;
21593 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21594 "#pragma omp for", pragma_tok);
21596 sb = begin_omp_structured_block ();
21597 save = cp_parser_begin_omp_structured_block (parser);
21599 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21601 cp_parser_end_omp_structured_block (parser, save);
21602 add_stmt (finish_omp_structured_block (sb));
21608 # pragma omp master new-line
21609 structured-block */
21612 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21614 cp_parser_require_pragma_eol (parser, pragma_tok);
21615 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21619 # pragma omp ordered new-line
21620 structured-block */
21623 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21625 cp_parser_require_pragma_eol (parser, pragma_tok);
21626 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21632 { section-sequence }
21635 section-directive[opt] structured-block
21636 section-sequence section-directive structured-block */
21639 cp_parser_omp_sections_scope (cp_parser *parser)
21641 tree stmt, substmt;
21642 bool error_suppress = false;
21645 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21648 stmt = push_stmt_list ();
21650 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21654 substmt = begin_omp_structured_block ();
21655 save = cp_parser_begin_omp_structured_block (parser);
21659 cp_parser_statement (parser, NULL_TREE, false, NULL);
21661 tok = cp_lexer_peek_token (parser->lexer);
21662 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21664 if (tok->type == CPP_CLOSE_BRACE)
21666 if (tok->type == CPP_EOF)
21670 cp_parser_end_omp_structured_block (parser, save);
21671 substmt = finish_omp_structured_block (substmt);
21672 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21673 add_stmt (substmt);
21678 tok = cp_lexer_peek_token (parser->lexer);
21679 if (tok->type == CPP_CLOSE_BRACE)
21681 if (tok->type == CPP_EOF)
21684 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21686 cp_lexer_consume_token (parser->lexer);
21687 cp_parser_require_pragma_eol (parser, tok);
21688 error_suppress = false;
21690 else if (!error_suppress)
21692 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21693 error_suppress = true;
21696 substmt = cp_parser_omp_structured_block (parser);
21697 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21698 add_stmt (substmt);
21700 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21702 substmt = pop_stmt_list (stmt);
21704 stmt = make_node (OMP_SECTIONS);
21705 TREE_TYPE (stmt) = void_type_node;
21706 OMP_SECTIONS_BODY (stmt) = substmt;
21713 # pragma omp sections sections-clause[optseq] newline
21716 #define OMP_SECTIONS_CLAUSE_MASK \
21717 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21718 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21719 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21720 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21721 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21724 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21728 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21729 "#pragma omp sections", pragma_tok);
21731 ret = cp_parser_omp_sections_scope (parser);
21733 OMP_SECTIONS_CLAUSES (ret) = clauses;
21739 # pragma parallel parallel-clause new-line
21740 # pragma parallel for parallel-for-clause new-line
21741 # pragma parallel sections parallel-sections-clause new-line */
21743 #define OMP_PARALLEL_CLAUSE_MASK \
21744 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21745 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21746 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21747 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21748 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21749 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21750 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21751 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21754 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21756 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21757 const char *p_name = "#pragma omp parallel";
21758 tree stmt, clauses, par_clause, ws_clause, block;
21759 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21764 cp_lexer_consume_token (parser->lexer);
21765 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21766 p_name = "#pragma omp parallel for";
21767 mask |= OMP_FOR_CLAUSE_MASK;
21768 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21770 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21772 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21773 const char *p = IDENTIFIER_POINTER (id);
21774 if (strcmp (p, "sections") == 0)
21776 cp_lexer_consume_token (parser->lexer);
21777 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21778 p_name = "#pragma omp parallel sections";
21779 mask |= OMP_SECTIONS_CLAUSE_MASK;
21780 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21784 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21785 block = begin_omp_parallel ();
21786 save = cp_parser_begin_omp_structured_block (parser);
21790 case PRAGMA_OMP_PARALLEL:
21791 cp_parser_statement (parser, NULL_TREE, false, NULL);
21792 par_clause = clauses;
21795 case PRAGMA_OMP_PARALLEL_FOR:
21796 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21797 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21800 case PRAGMA_OMP_PARALLEL_SECTIONS:
21801 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21802 stmt = cp_parser_omp_sections_scope (parser);
21804 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21808 gcc_unreachable ();
21811 cp_parser_end_omp_structured_block (parser, save);
21812 stmt = finish_omp_parallel (par_clause, block);
21813 if (p_kind != PRAGMA_OMP_PARALLEL)
21814 OMP_PARALLEL_COMBINED (stmt) = 1;
21819 # pragma omp single single-clause[optseq] new-line
21820 structured-block */
21822 #define OMP_SINGLE_CLAUSE_MASK \
21823 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21824 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21825 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21826 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21829 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21831 tree stmt = make_node (OMP_SINGLE);
21832 TREE_TYPE (stmt) = void_type_node;
21834 OMP_SINGLE_CLAUSES (stmt)
21835 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21836 "#pragma omp single", pragma_tok);
21837 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21839 return add_stmt (stmt);
21843 # pragma omp task task-clause[optseq] new-line
21844 structured-block */
21846 #define OMP_TASK_CLAUSE_MASK \
21847 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21848 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21849 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21850 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21851 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21852 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21855 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21857 tree clauses, block;
21860 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21861 "#pragma omp task", pragma_tok);
21862 block = begin_omp_task ();
21863 save = cp_parser_begin_omp_structured_block (parser);
21864 cp_parser_statement (parser, NULL_TREE, false, NULL);
21865 cp_parser_end_omp_structured_block (parser, save);
21866 return finish_omp_task (clauses, block);
21870 # pragma omp taskwait new-line */
21873 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21875 cp_parser_require_pragma_eol (parser, pragma_tok);
21876 finish_omp_taskwait ();
21880 # pragma omp threadprivate (variable-list) */
21883 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21887 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21888 cp_parser_require_pragma_eol (parser, pragma_tok);
21890 finish_omp_threadprivate (vars);
21893 /* Main entry point to OpenMP statement pragmas. */
21896 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21900 switch (pragma_tok->pragma_kind)
21902 case PRAGMA_OMP_ATOMIC:
21903 cp_parser_omp_atomic (parser, pragma_tok);
21905 case PRAGMA_OMP_CRITICAL:
21906 stmt = cp_parser_omp_critical (parser, pragma_tok);
21908 case PRAGMA_OMP_FOR:
21909 stmt = cp_parser_omp_for (parser, pragma_tok);
21911 case PRAGMA_OMP_MASTER:
21912 stmt = cp_parser_omp_master (parser, pragma_tok);
21914 case PRAGMA_OMP_ORDERED:
21915 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21917 case PRAGMA_OMP_PARALLEL:
21918 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21920 case PRAGMA_OMP_SECTIONS:
21921 stmt = cp_parser_omp_sections (parser, pragma_tok);
21923 case PRAGMA_OMP_SINGLE:
21924 stmt = cp_parser_omp_single (parser, pragma_tok);
21926 case PRAGMA_OMP_TASK:
21927 stmt = cp_parser_omp_task (parser, pragma_tok);
21930 gcc_unreachable ();
21934 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21939 static GTY (()) cp_parser *the_parser;
21942 /* Special handling for the first token or line in the file. The first
21943 thing in the file might be #pragma GCC pch_preprocess, which loads a
21944 PCH file, which is a GC collection point. So we need to handle this
21945 first pragma without benefit of an existing lexer structure.
21947 Always returns one token to the caller in *FIRST_TOKEN. This is
21948 either the true first token of the file, or the first token after
21949 the initial pragma. */
21952 cp_parser_initial_pragma (cp_token *first_token)
21956 cp_lexer_get_preprocessor_token (NULL, first_token);
21957 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21960 cp_lexer_get_preprocessor_token (NULL, first_token);
21961 if (first_token->type == CPP_STRING)
21963 name = first_token->u.value;
21965 cp_lexer_get_preprocessor_token (NULL, first_token);
21966 if (first_token->type != CPP_PRAGMA_EOL)
21967 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21968 &first_token->location);
21971 error ("%Hexpected string literal", &first_token->location);
21973 /* Skip to the end of the pragma. */
21974 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21975 cp_lexer_get_preprocessor_token (NULL, first_token);
21977 /* Now actually load the PCH file. */
21979 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21981 /* Read one more token to return to our caller. We have to do this
21982 after reading the PCH file in, since its pointers have to be
21984 cp_lexer_get_preprocessor_token (NULL, first_token);
21987 /* Normal parsing of a pragma token. Here we can (and must) use the
21991 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21993 cp_token *pragma_tok;
21996 pragma_tok = cp_lexer_consume_token (parser->lexer);
21997 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21998 parser->lexer->in_pragma = true;
22000 id = pragma_tok->pragma_kind;
22003 case PRAGMA_GCC_PCH_PREPROCESS:
22004 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22005 &pragma_tok->location);
22008 case PRAGMA_OMP_BARRIER:
22011 case pragma_compound:
22012 cp_parser_omp_barrier (parser, pragma_tok);
22015 error ("%H%<#pragma omp barrier%> may only be "
22016 "used in compound statements", &pragma_tok->location);
22023 case PRAGMA_OMP_FLUSH:
22026 case pragma_compound:
22027 cp_parser_omp_flush (parser, pragma_tok);
22030 error ("%H%<#pragma omp flush%> may only be "
22031 "used in compound statements", &pragma_tok->location);
22038 case PRAGMA_OMP_TASKWAIT:
22041 case pragma_compound:
22042 cp_parser_omp_taskwait (parser, pragma_tok);
22045 error ("%H%<#pragma omp taskwait%> may only be "
22046 "used in compound statements",
22047 &pragma_tok->location);
22054 case PRAGMA_OMP_THREADPRIVATE:
22055 cp_parser_omp_threadprivate (parser, pragma_tok);
22058 case PRAGMA_OMP_ATOMIC:
22059 case PRAGMA_OMP_CRITICAL:
22060 case PRAGMA_OMP_FOR:
22061 case PRAGMA_OMP_MASTER:
22062 case PRAGMA_OMP_ORDERED:
22063 case PRAGMA_OMP_PARALLEL:
22064 case PRAGMA_OMP_SECTIONS:
22065 case PRAGMA_OMP_SINGLE:
22066 case PRAGMA_OMP_TASK:
22067 if (context == pragma_external)
22069 cp_parser_omp_construct (parser, pragma_tok);
22072 case PRAGMA_OMP_SECTION:
22073 error ("%H%<#pragma omp section%> may only be used in "
22074 "%<#pragma omp sections%> construct", &pragma_tok->location);
22078 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22079 c_invoke_pragma_handler (id);
22083 cp_parser_error (parser, "expected declaration specifiers");
22087 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22091 /* The interface the pragma parsers have to the lexer. */
22094 pragma_lex (tree *value)
22097 enum cpp_ttype ret;
22099 tok = cp_lexer_peek_token (the_parser->lexer);
22102 *value = tok->u.value;
22104 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22106 else if (ret == CPP_STRING)
22107 *value = cp_parser_string_literal (the_parser, false, false);
22110 cp_lexer_consume_token (the_parser->lexer);
22111 if (ret == CPP_KEYWORD)
22119 /* External interface. */
22121 /* Parse one entire translation unit. */
22124 c_parse_file (void)
22126 bool error_occurred;
22127 static bool already_called = false;
22129 if (already_called)
22131 sorry ("inter-module optimizations not implemented for C++");
22134 already_called = true;
22136 the_parser = cp_parser_new ();
22137 push_deferring_access_checks (flag_access_control
22138 ? dk_no_deferred : dk_no_check);
22139 error_occurred = cp_parser_translation_unit (the_parser);
22143 #include "gt-cp-parser.h"