2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 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"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 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. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
610 if (token == lexer->last_token)
616 if (token->type != CPP_PURGED)
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
683 if (tok == lexer->last_token)
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
956 declarator->parameter_pack_p = false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
979 declarator->parameter_pack_p = false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1004 declarator->parameter_pack_p = false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator *target,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification,
1019 tree late_return_type)
1021 cp_declarator *declarator;
1023 declarator = make_declarator (cdk_function);
1024 declarator->declarator = target;
1025 declarator->u.function.parameters = parms;
1026 declarator->u.function.qualifiers = cv_qualifiers;
1027 declarator->u.function.exception_specification = exception_specification;
1028 declarator->u.function.late_return_type = late_return_type;
1031 declarator->parameter_pack_p = target->parameter_pack_p;
1032 target->parameter_pack_p = false;
1035 declarator->parameter_pack_p = false;
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1044 make_array_declarator (cp_declarator *element, tree bounds)
1046 cp_declarator *declarator;
1048 declarator = make_declarator (cdk_array);
1049 declarator->declarator = element;
1050 declarator->u.array.bounds = bounds;
1053 declarator->parameter_pack_p = element->parameter_pack_p;
1054 element->parameter_pack_p = false;
1057 declarator->parameter_pack_p = false;
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1072 while (declarator && !found)
1074 switch ((int)declarator->kind)
1085 declarator = declarator->declarator;
1093 cp_parameter_declarator *no_parameters;
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100 cp_declarator *declarator,
1101 tree default_argument)
1103 cp_parameter_declarator *parameter;
1105 parameter = ((cp_parameter_declarator *)
1106 alloc_declarator (sizeof (cp_parameter_declarator)));
1107 parameter->next = NULL;
1108 if (decl_specifiers)
1109 parameter->decl_specifiers = *decl_specifiers;
1111 clear_decl_specs (¶meter->decl_specifiers);
1112 parameter->declarator = declarator;
1113 parameter->default_argument = default_argument;
1114 parameter->ellipsis_p = false;
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1122 function_declarator_p (const cp_declarator *declarator)
1126 if (declarator->kind == cdk_function
1127 && declarator->declarator->kind == cdk_id)
1129 if (declarator->kind == cdk_id
1130 || declarator->kind == cdk_error)
1132 declarator = declarator->declarator;
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1195 typedef enum cp_parser_flags
1198 CP_PARSER_FLAGS_NONE = 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1206 /* The different kinds of declarators we want to parse. */
1208 typedef enum cp_parser_declarator_kind
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1225 PREC_LOGICAL_OR_EXPRESSION,
1226 PREC_LOGICAL_AND_EXPRESSION,
1227 PREC_INCLUSIVE_OR_EXPRESSION,
1228 PREC_EXCLUSIVE_OR_EXPRESSION,
1229 PREC_AND_EXPRESSION,
1230 PREC_EQUALITY_EXPRESSION,
1231 PREC_RELATIONAL_EXPRESSION,
1232 PREC_SHIFT_EXPRESSION,
1233 PREC_ADDITIVE_EXPRESSION,
1234 PREC_MULTIPLICATIVE_EXPRESSION,
1236 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1242 typedef struct cp_parser_binary_operations_map_node
1244 /* The token type. */
1245 enum cpp_ttype token_type;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1252 /* The status of a tentative parse. */
1254 typedef enum cp_parser_status_kind
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR,
1260 /* We are committed to this tentative parse, whether or not an error
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1265 typedef struct cp_parser_expression_stack_entry
1267 /* Left hand side of the binary operation we are currently
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type;
1275 /* Precedence of the binary operation we are parsing. */
1277 } cp_parser_expression_stack_entry;
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack[NUM_PREC_VALUES];
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context GTY (())
1288 /* If this is a tentative parsing context, the status of the
1290 enum cp_parser_status_kind status;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context *next;
1299 } cp_parser_context;
1303 /* Constructors and destructors. */
1305 static cp_parser_context *cp_parser_context_new
1306 (cp_parser_context *);
1308 /* Class variables. */
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1320 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1327 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1330 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1338 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1340 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1342 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1344 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1346 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1354 /* Constructors and destructors. */
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1362 cp_parser_context *context;
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list != NULL)
1367 /* Pull the first entry from the free list. */
1368 context = cp_parser_context_free_list;
1369 cp_parser_context_free_list = context->next;
1370 memset (context, 0, sizeof (*context));
1373 context = GGC_CNEW (cp_parser_context);
1375 /* No errors have occurred yet in this context. */
1376 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context->object_type = next->object_type;
1384 /* Thread the stack. */
1385 context->next = next;
1391 /* The cp_parser structure represents the C++ parser. */
1393 typedef struct cp_parser GTY(())
1395 /* The lexer from which we are obtaining tokens. */
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1419 tree qualifying_scope;
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context *context;
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p;
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p;
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p;
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p;
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p;
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p;
1462 /* TRUE if local variable names and `this' are forbidden in the
1464 bool local_variables_forbidden_p;
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1469 bool in_unbraced_linkage_specification_p;
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p;
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p;
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement;
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p;
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1499 bool in_type_id_in_expr_p;
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c;
1505 /* TRUE if strings in expressions should be translated to the execution
1507 bool translate_strings_p;
1509 /* TRUE if we are presently parsing the body of a function, but not
1511 bool in_function_body;
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message;
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues;
1534 /* The number of classes whose definitions are currently in
1536 unsigned num_classes_being_defined;
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists;
1545 /* Constructors and destructors. */
1547 static cp_parser *cp_parser_new
1550 /* Routines to parse various constructs.
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1562 /* Lexical conventions [gram.lex] */
1564 static tree cp_parser_identifier
1566 static tree cp_parser_string_literal
1567 (cp_parser *, bool, bool);
1569 /* Basic concepts [gram.basic] */
1571 static bool cp_parser_translation_unit
1574 /* Expressions [gram.expr] */
1576 static tree cp_parser_primary_expression
1577 (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579 (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser *, bool, bool, bool);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599 (cp_parser *, bool, bool);
1600 static enum tree_code cp_parser_unary_operator
1602 static tree cp_parser_new_expression
1604 static tree cp_parser_new_placement
1606 static tree cp_parser_new_type_id
1607 (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1610 static cp_declarator *cp_parser_direct_new_declarator
1612 static tree cp_parser_new_initializer
1614 static tree cp_parser_delete_expression
1616 static tree cp_parser_cast_expression
1617 (cp_parser *, bool, bool);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1626 static tree cp_parser_expression
1627 (cp_parser *, bool);
1628 static tree cp_parser_constant_expression
1629 (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1633 /* Statements [gram.stmt.stmt] */
1635 static void cp_parser_statement
1636 (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1639 static tree cp_parser_expression_statement
1640 (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642 (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646 (cp_parser *, bool *);
1647 static tree cp_parser_condition
1649 static tree cp_parser_iteration_statement
1651 static void cp_parser_for_init_statement
1653 static tree cp_parser_jump_statement
1655 static void cp_parser_declaration_statement
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1663 /* Declarations [gram.dcl.dcl] */
1665 static void cp_parser_declaration_seq_opt
1667 static void cp_parser_declaration
1669 static void cp_parser_block_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1686 static tree cp_parser_nonclass_name
1687 (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1692 static void cp_parser_enumerator_list
1693 (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1698 static void cp_parser_namespace_definition
1700 static void cp_parser_namespace_body
1702 static tree cp_parser_qualified_namespace_specifier
1704 static void cp_parser_namespace_alias_definition
1706 static bool cp_parser_using_declaration
1707 (cp_parser *, bool);
1708 static void cp_parser_using_directive
1710 static void cp_parser_asm_definition
1712 static void cp_parser_linkage_specification
1714 static void cp_parser_static_assert
1715 (cp_parser *, bool);
1716 static tree cp_parser_decltype
1719 /* Declarators [gram.dcl.decl] */
1721 static tree cp_parser_init_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1731 static tree cp_parser_late_return_type_opt
1733 static tree cp_parser_declarator_id
1734 (cp_parser *, bool);
1735 static tree cp_parser_type_id
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744 (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser *, bool);
1747 static void cp_parser_function_body
1749 static tree cp_parser_initializer
1750 (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756 (cp_parser *, bool *);
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1761 /* Classes [gram.class] */
1763 static tree cp_parser_class_name
1764 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1767 static tree cp_parser_class_head
1768 (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1771 static void cp_parser_member_specification_opt
1773 static void cp_parser_member_declaration
1775 static tree cp_parser_pure_specifier
1777 static tree cp_parser_constant_initializer
1780 /* Derived classes [gram.class.derived] */
1782 static tree cp_parser_base_clause
1784 static tree cp_parser_base_specifier
1787 /* Special member functions [gram.special] */
1789 static tree cp_parser_conversion_function_id
1791 static tree cp_parser_conversion_type_id
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1795 static bool cp_parser_ctor_initializer_opt
1797 static void cp_parser_mem_initializer_list
1799 static tree cp_parser_mem_initializer
1801 static tree cp_parser_mem_initializer_id
1804 /* Overloading [gram.over] */
1806 static tree cp_parser_operator_function_id
1808 static tree cp_parser_operator
1811 /* Templates [gram.temp] */
1813 static void cp_parser_template_declaration
1814 (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1817 static tree cp_parser_template_parameter
1818 (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1827 static tree cp_parser_template_argument
1829 static void cp_parser_explicit_instantiation
1831 static void cp_parser_explicit_specialization
1834 /* Exception handling [gram.exception] */
1836 static tree cp_parser_try_block
1838 static bool cp_parser_function_try_block
1840 static void cp_parser_handler_seq
1842 static void cp_parser_handler
1844 static tree cp_parser_exception_declaration
1846 static tree cp_parser_throw_expression
1848 static tree cp_parser_exception_specification_opt
1850 static tree cp_parser_type_id_list
1853 /* GNU Extensions */
1855 static tree cp_parser_asm_specification_opt
1857 static tree cp_parser_asm_operand_list
1859 static tree cp_parser_asm_clobber_list
1861 static tree cp_parser_attributes_opt
1863 static tree cp_parser_attribute_list
1865 static bool cp_parser_extension_opt
1866 (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872 (cp_parser *, enum pragma_context);
1874 /* Objective-C++ Productions */
1876 static tree cp_parser_objc_message_receiver
1878 static tree cp_parser_objc_message_args
1880 static tree cp_parser_objc_message_expression
1882 static tree cp_parser_objc_encode_expression
1884 static tree cp_parser_objc_defs_expression
1886 static tree cp_parser_objc_protocol_expression
1888 static tree cp_parser_objc_selector_expression
1890 static tree cp_parser_objc_expression
1892 static bool cp_parser_objc_selector_p
1894 static tree cp_parser_objc_selector
1896 static tree cp_parser_objc_protocol_refs_opt
1898 static void cp_parser_objc_declaration
1900 static tree cp_parser_objc_statement
1903 /* Utility Routines */
1905 static tree cp_parser_lookup_name
1906 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1937 static void cp_parser_save_default_args
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946 (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1949 static void cp_parser_set_storage_class
1950 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956 (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958 (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1961 static bool cp_parser_next_token_starts_class_definition_p
1963 static bool cp_parser_next_token_ends_template_argument_p
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1969 static void cp_parser_check_class_key
1970 (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1975 static void cp_parser_pre_parsed_nested_name_specifier
1977 static bool cp_parser_cache_group
1978 (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1981 static void cp_parser_commit_to_tentative_parse
1983 static void cp_parser_abort_tentative_parse
1985 static bool cp_parser_parse_definitely
1987 static inline bool cp_parser_parsing_tentatively
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1991 static void cp_parser_error
1992 (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1997 static bool cp_parser_check_type_definition
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2015 static void cp_parser_skip_to_end_of_block_or_statement
2017 static bool cp_parser_skip_to_closing_brace
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2025 static bool cp_parser_allow_gnu_extensions_p
2027 static bool cp_parser_is_string_literal
2029 static bool cp_parser_is_keyword
2030 (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2036 /* Returns nonzero if we are parsing tentatively. */
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2041 return parser->context->next != NULL;
2044 /* Returns nonzero if TOKEN is a string literal. */
2047 cp_parser_is_string_literal (cp_token* token)
2049 return (token->type == CPP_STRING ||
2050 token->type == CPP_STRING16 ||
2051 token->type == CPP_STRING32 ||
2052 token->type == CPP_WSTRING);
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2060 return token->keyword == keyword;
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2070 cp_parser_error (cp_parser* parser, const char* message)
2072 if (!cp_parser_simulate_error (parser))
2074 cp_token *token = cp_lexer_peek_token (parser->lexer);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token);
2079 if (token->type == CPP_PRAGMA)
2081 error ("%H%<#pragma%> is not allowed here", &token->location);
2082 cp_parser_skip_to_pragma_eol (parser, token);
2086 c_parse_error (message,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2090 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2101 cp_parser_name_lookup_error (cp_parser* parser,
2104 const char* desired,
2105 location_t location)
2107 /* If name lookup completely failed, tell the user that NAME was not
2109 if (decl == error_mark_node)
2111 if (parser->scope && parser->scope != global_namespace)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location, parser->scope, name);
2114 else if (parser->scope == global_namespace)
2115 error ("%H%<::%E%> has not been declared", &location, name);
2116 else if (parser->object_scope
2117 && !CLASS_TYPE_P (parser->object_scope))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location, name, parser->object_scope);
2120 else if (parser->object_scope)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location, parser->object_scope, name);
2124 error ("%H%qE has not been declared", &location, name);
2126 else if (parser->scope && parser->scope != global_namespace)
2127 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128 else if (parser->scope == global_namespace)
2129 error ("%H%<::%E%> %s", &location, name, desired);
2131 error ("%H%qE %s", &location, name, desired);
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2139 cp_parser_simulate_error (cp_parser* parser)
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2143 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2149 /* Check for repeated decl-specifiers. */
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153 location_t location)
2157 for (ds = ds_first; ds != ds_last; ++ds)
2159 unsigned count = decl_specs->specs[(int)ds];
2162 /* The "long" specifier is a special case because of "long long". */
2166 error ("%H%<long long long%> is too long for GCC", &location);
2167 else if (pedantic && !in_system_header && warn_long_long
2168 && cxx_dialect == cxx98)
2169 pedwarn (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2174 static const char *const decl_spec_names[] = {
2190 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2200 cp_parser_check_type_definition (cp_parser* parser)
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2232 && declarator->kind == cdk_function)
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2251 cp_token_position start = 0;
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2287 if (!parser->allow_non_integral_constant_expression_p)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2330 else if (!parser->scope)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2389 cp_parser_commit_to_tentative_parse (parser);
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2423 cp_parser_abort_tentative_parse (parser);
2426 if (!cp_parser_parse_definitely (parser))
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2460 switch (token->type)
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2468 /* This matches the processing in skip_to_end_of_statement. */
2473 case CPP_OPEN_BRACE:
2476 case CPP_CLOSE_BRACE:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2486 case CPP_OPEN_PAREN:
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2495 cp_lexer_consume_token (parser->lexer);
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2516 unsigned nesting_depth = 0;
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2522 switch (token->type)
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2530 /* If the next token is a `;', we have reached the end of the
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2557 if (--nesting_depth == 0)
2559 cp_lexer_consume_token (parser->lexer);
2563 case CPP_OPEN_BRACE:
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2586 /* If there is additional (erroneous) input, skip to the end of
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2601 int nesting_depth = 0;
2603 while (nesting_depth >= 0)
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2607 switch (token->type)
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2615 /* Stop if this is an unnested ';'. */
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2628 case CPP_OPEN_BRACE:
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2649 unsigned nesting_depth = 0;
2653 cp_token *token = cp_lexer_peek_token (parser->lexer);
2655 switch (token->type)
2658 case CPP_PRAGMA_EOL:
2659 /* If we've run out of tokens, stop. */
2662 case CPP_CLOSE_BRACE:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth-- == 0)
2669 case CPP_OPEN_BRACE:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser->lexer);
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2693 parser->lexer->in_pragma = false;
2696 token = cp_lexer_consume_token (parser->lexer);
2697 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2709 parser->lexer->in_pragma = false;
2710 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720 tree id, location_t id_location)
2723 if (TREE_CODE (id) == IDENTIFIER_NODE)
2725 result = make_typename_type (scope, id, typename_type,
2726 /*complain=*/tf_none);
2727 if (result == error_mark_node)
2728 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2731 return make_typename_type (scope, id, typename_type, tf_error);
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741 cp_cv_quals cv_qualifiers,
2742 cp_declarator *target)
2744 if (code == ERROR_MARK)
2745 return cp_error_declarator;
2747 if (code == INDIRECT_REF)
2748 if (class_type == NULL_TREE)
2749 return make_pointer_declarator (cv_qualifiers, target);
2751 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, false);
2754 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, true);
2759 /* Create a new C++ parser. */
2762 cp_parser_new (void)
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer = cp_lexer_new_main ();
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775 binops_by_token[binops[i].token_type] = binops[i];
2777 parser = GGC_CNEW (cp_parser);
2778 parser->lexer = lexer;
2779 parser->context = cp_parser_context_new (NULL);
2781 /* For now, we always accept GNU extensions. */
2782 parser->allow_gnu_extensions_p = 1;
2784 /* The `>' token is a greater-than operator, not the end of a
2786 parser->greater_than_is_operator_p = true;
2788 parser->default_arg_ok_p = true;
2790 /* We are not parsing a constant-expression. */
2791 parser->integral_constant_expression_p = false;
2792 parser->allow_non_integral_constant_expression_p = false;
2793 parser->non_integral_constant_expression_p = false;
2795 /* Local variable names are not forbidden. */
2796 parser->local_variables_forbidden_p = false;
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser->in_unbraced_linkage_specification_p = false;
2801 /* We are not processing a declarator. */
2802 parser->in_declarator_p = false;
2804 /* We are not processing a template-argument-list. */
2805 parser->in_template_argument_list_p = false;
2807 /* We are not in an iteration statement. */
2808 parser->in_statement = 0;
2810 /* We are not in a switch statement. */
2811 parser->in_switch_statement_p = false;
2813 /* We are not parsing a type-id inside an expression. */
2814 parser->in_type_id_in_expr_p = false;
2816 /* Declarations aren't implicitly extern "C". */
2817 parser->implicit_extern_c = false;
2819 /* String literals should be translated to the execution character set. */
2820 parser->translate_strings_p = true;
2822 /* We are not parsing a function body. */
2823 parser->in_function_body = false;
2825 /* The unparsed function queue is empty. */
2826 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2828 /* There are no classes being defined. */
2829 parser->num_classes_being_defined = 0;
2831 /* No template parameters apply. */
2832 parser->num_template_parameter_lists = 0;
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2844 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845 lexer->next = parser->lexer;
2846 parser->lexer = lexer;
2848 /* Move the current source position to that of the first token in the
2850 cp_lexer_set_source_position_from_token (lexer->next_token);
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2856 cp_parser_pop_lexer (cp_parser *parser)
2858 cp_lexer *lexer = parser->lexer;
2859 parser->lexer = lexer->next;
2860 cp_lexer_destroy (lexer);
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2867 /* Lexical conventions [gram.lex] */
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2873 cp_parser_identifier (cp_parser* parser)
2877 /* Look for the identifier. */
2878 token = cp_parser_require (parser, CPP_NAME, "identifier");
2879 /* Return the value. */
2880 return token ? token->u.value : error_mark_node;
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2894 This code is largely lifted from lex_string() in c-lex.c.
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2902 struct obstack str_ob;
2903 cpp_string str, istr, *strs;
2905 enum cpp_ttype type;
2907 tok = cp_lexer_peek_token (parser->lexer);
2908 if (!cp_parser_is_string_literal (tok))
2910 cp_parser_error (parser, "expected string-literal");
2911 return error_mark_node;
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2921 cp_lexer_consume_token (parser->lexer);
2923 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924 str.len = TREE_STRING_LENGTH (tok->u.value);
2931 gcc_obstack_init (&str_ob);
2936 cp_lexer_consume_token (parser->lexer);
2938 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939 str.len = TREE_STRING_LENGTH (tok->u.value);
2941 if (type != tok->type)
2943 if (type == CPP_STRING)
2945 else if (tok->type != CPP_STRING)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok->location);
2950 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2952 tok = cp_lexer_peek_token (parser->lexer);
2954 while (cp_parser_is_string_literal (tok));
2956 strs = (cpp_string *) obstack_finish (&str_ob);
2959 if (type != CPP_STRING && !wide_ok)
2961 cp_parser_error (parser, "a wide string is invalid in this context");
2965 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966 (parse_in, strs, count, &istr, type))
2968 value = build_string (istr.len, (const char *)istr.text);
2969 free (CONST_CAST (unsigned char *, istr.text));
2975 TREE_TYPE (value) = char_array_type_node;
2978 TREE_TYPE (value) = char16_array_type_node;
2981 TREE_TYPE (value) = char32_array_type_node;
2984 TREE_TYPE (value) = wchar_array_type_node;
2988 value = fix_string_type (value);
2991 /* cpp_interpret_string has issued an error. */
2992 value = error_mark_node;
2995 obstack_free (&str_ob, 0);
3001 /* Basic concepts [gram.basic] */
3003 /* Parse a translation-unit.
3006 declaration-seq [opt]
3008 Returns TRUE if all went well. */
3011 cp_parser_translation_unit (cp_parser* parser)
3013 /* The address of the first non-permanent object on the declarator
3015 static void *declarator_obstack_base;
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator)
3022 gcc_obstack_init (&declarator_obstack);
3023 /* Create the error declarator. */
3024 cp_error_declarator = make_declarator (cdk_error);
3025 /* Create the empty parameter list. */
3026 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3031 cp_parser_declaration_seq_opt (parser);
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser->lexer);
3038 parser->lexer = NULL;
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser->implicit_extern_c)
3044 pop_lang_context ();
3045 parser->implicit_extern_c = false;
3049 finish_translation_unit ();
3055 cp_parser_error (parser, "expected declaration");
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack)
3061 == declarator_obstack_base);
3063 /* All went well. */
3067 /* Expressions [gram.expr] */
3069 /* Parse a primary-expression.
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3103 Objective-C++ Extension:
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3120 cp_parser_primary_expression (cp_parser *parser,
3123 bool template_arg_p,
3126 cp_token *token = NULL;
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk = CP_ID_KIND_NONE;
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133 switch (token->type)
3146 token = cp_lexer_consume_token (parser->lexer);
3147 /* Floating-point literals are only allowed in an integral
3148 constant expression if they are cast to an integral or
3149 enumeration type. */
3150 if (TREE_CODE (token->u.value) == REAL_CST
3151 && parser->integral_constant_expression_p
3154 /* CAST_P will be set even in invalid code like "int(2.7 +
3155 ...)". Therefore, we have to check that the next token
3156 is sure to end the cast. */
3159 cp_token *next_token;
3161 next_token = cp_lexer_peek_token (parser->lexer);
3162 if (/* The comma at the end of an
3163 enumerator-definition. */
3164 next_token->type != CPP_COMMA
3165 /* The curly brace at the end of an enum-specifier. */
3166 && next_token->type != CPP_CLOSE_BRACE
3167 /* The end of a statement. */
3168 && next_token->type != CPP_SEMICOLON
3169 /* The end of the cast-expression. */
3170 && next_token->type != CPP_CLOSE_PAREN
3171 /* The end of an array bound. */
3172 && next_token->type != CPP_CLOSE_SQUARE
3173 /* The closing ">" in a template-argument-list. */
3174 && (next_token->type != CPP_GREATER
3175 || parser->greater_than_is_operator_p)
3176 /* C++0x only: A ">>" treated like two ">" tokens,
3177 in a template-argument-list. */
3178 && (next_token->type != CPP_RSHIFT
3179 || (cxx_dialect == cxx98)
3180 || parser->greater_than_is_operator_p))
3184 /* If we are within a cast, then the constraint that the
3185 cast is to an integral or enumeration type will be
3186 checked at that point. If we are not within a cast, then
3187 this code is invalid. */
3189 cp_parser_non_integral_constant_expression
3190 (parser, "floating-point literal");
3192 return token->u.value;
3198 /* ??? Should wide strings be allowed when parser->translate_strings_p
3199 is false (i.e. in attributes)? If not, we can kill the third
3200 argument to cp_parser_string_literal. */
3201 return cp_parser_string_literal (parser,
3202 parser->translate_strings_p,
3205 case CPP_OPEN_PAREN:
3208 bool saved_greater_than_is_operator_p;
3210 /* Consume the `('. */
3211 cp_lexer_consume_token (parser->lexer);
3212 /* Within a parenthesized expression, a `>' token is always
3213 the greater-than operator. */
3214 saved_greater_than_is_operator_p
3215 = parser->greater_than_is_operator_p;
3216 parser->greater_than_is_operator_p = true;
3217 /* If we see `( { ' then we are looking at the beginning of
3218 a GNU statement-expression. */
3219 if (cp_parser_allow_gnu_extensions_p (parser)
3220 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3222 /* Statement-expressions are not allowed by the standard. */
3223 pedwarn (token->location, OPT_pedantic,
3224 "ISO C++ forbids braced-groups within expressions");
3226 /* And they're not allowed outside of a function-body; you
3227 cannot, for example, write:
3229 int i = ({ int j = 3; j + 1; });
3231 at class or namespace scope. */
3232 if (!parser->in_function_body
3233 || parser->in_template_argument_list_p)
3235 error ("%Hstatement-expressions are not allowed outside "
3236 "functions nor in template-argument lists",
3238 cp_parser_skip_to_end_of_block_or_statement (parser);
3239 expr = error_mark_node;
3243 /* Start the statement-expression. */
3244 expr = begin_stmt_expr ();
3245 /* Parse the compound-statement. */
3246 cp_parser_compound_statement (parser, expr, false);
3248 expr = finish_stmt_expr (expr, false);
3253 /* Parse the parenthesized expression. */
3254 expr = cp_parser_expression (parser, cast_p);
3255 /* Let the front end know that this expression was
3256 enclosed in parentheses. This matters in case, for
3257 example, the expression is of the form `A::B', since
3258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3260 finish_parenthesized_expr (expr);
3262 /* The `>' token might be the end of a template-id or
3263 template-parameter-list now. */
3264 parser->greater_than_is_operator_p
3265 = saved_greater_than_is_operator_p;
3266 /* Consume the `)'. */
3267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268 cp_parser_skip_to_end_of_statement (parser);
3274 switch (token->keyword)
3276 /* These two are the boolean literals. */
3278 cp_lexer_consume_token (parser->lexer);
3279 return boolean_true_node;
3281 cp_lexer_consume_token (parser->lexer);
3282 return boolean_false_node;
3284 /* The `__null' literal. */
3286 cp_lexer_consume_token (parser->lexer);
3289 /* Recognize the `this' keyword. */
3291 cp_lexer_consume_token (parser->lexer);
3292 if (parser->local_variables_forbidden_p)
3294 error ("%H%<this%> may not be used in this context",
3296 return error_mark_node;
3298 /* Pointers cannot appear in constant-expressions. */
3299 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300 return error_mark_node;
3301 return finish_this_expr ();
3303 /* The `operator' keyword can be the beginning of an
3308 case RID_FUNCTION_NAME:
3309 case RID_PRETTY_FUNCTION_NAME:
3310 case RID_C99_FUNCTION_NAME:
3311 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312 __func__ are the names of variables -- but they are
3313 treated specially. Therefore, they are handled here,
3314 rather than relying on the generic id-expression logic
3315 below. Grammatically, these names are id-expressions.
3317 Consume the token. */
3318 token = cp_lexer_consume_token (parser->lexer);
3319 /* Look up the name. */
3320 return finish_fname (token->u.value);
3327 /* The `__builtin_va_arg' construct is used to handle
3328 `va_arg'. Consume the `__builtin_va_arg' token. */
3329 cp_lexer_consume_token (parser->lexer);
3330 /* Look for the opening `('. */
3331 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332 /* Now, parse the assignment-expression. */
3333 expression = cp_parser_assignment_expression (parser,
3335 /* Look for the `,'. */
3336 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337 /* Parse the type-id. */
3338 type = cp_parser_type_id (parser);
3339 /* Look for the closing `)'. */
3340 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341 /* Using `va_arg' in a constant-expression is not
3343 if (cp_parser_non_integral_constant_expression (parser,
3345 return error_mark_node;
3346 return build_x_va_arg (expression, type);
3350 return cp_parser_builtin_offsetof (parser);
3352 case RID_HAS_NOTHROW_ASSIGN:
3353 case RID_HAS_NOTHROW_CONSTRUCTOR:
3354 case RID_HAS_NOTHROW_COPY:
3355 case RID_HAS_TRIVIAL_ASSIGN:
3356 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357 case RID_HAS_TRIVIAL_COPY:
3358 case RID_HAS_TRIVIAL_DESTRUCTOR:
3359 case RID_HAS_VIRTUAL_DESTRUCTOR:
3360 case RID_IS_ABSTRACT:
3361 case RID_IS_BASE_OF:
3363 case RID_IS_CONVERTIBLE_TO:
3367 case RID_IS_POLYMORPHIC:
3369 return cp_parser_trait_expr (parser, token->keyword);
3371 /* Objective-C++ expressions. */
3373 case RID_AT_PROTOCOL:
3374 case RID_AT_SELECTOR:
3375 return cp_parser_objc_expression (parser);
3378 cp_parser_error (parser, "expected primary-expression");
3379 return error_mark_node;
3382 /* An id-expression can start with either an identifier, a
3383 `::' as the beginning of a qualified-id, or the "operator"
3387 case CPP_TEMPLATE_ID:
3388 case CPP_NESTED_NAME_SPECIFIER:
3392 const char *error_msg;
3395 cp_token *id_expr_token;
3398 /* Parse the id-expression. */
3400 = cp_parser_id_expression (parser,
3401 /*template_keyword_p=*/false,
3402 /*check_dependency_p=*/true,
3404 /*declarator_p=*/false,
3405 /*optional_p=*/false);
3406 if (id_expression == error_mark_node)
3407 return error_mark_node;
3408 id_expr_token = token;
3409 token = cp_lexer_peek_token (parser->lexer);
3410 done = (token->type != CPP_OPEN_SQUARE
3411 && token->type != CPP_OPEN_PAREN
3412 && token->type != CPP_DOT
3413 && token->type != CPP_DEREF
3414 && token->type != CPP_PLUS_PLUS
3415 && token->type != CPP_MINUS_MINUS);
3416 /* If we have a template-id, then no further lookup is
3417 required. If the template-id was for a template-class, we
3418 will sometimes have a TYPE_DECL at this point. */
3419 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420 || TREE_CODE (id_expression) == TYPE_DECL)
3421 decl = id_expression;
3422 /* Look up the name. */
3425 tree ambiguous_decls;
3427 decl = cp_parser_lookup_name (parser, id_expression,
3430 /*is_namespace=*/false,
3431 /*check_dependency=*/true,
3433 id_expr_token->location);
3434 /* If the lookup was ambiguous, an error will already have
3436 if (ambiguous_decls)
3437 return error_mark_node;
3439 /* In Objective-C++, an instance variable (ivar) may be preferred
3440 to whatever cp_parser_lookup_name() found. */
3441 decl = objc_lookup_ivar (decl, id_expression);
3443 /* If name lookup gives us a SCOPE_REF, then the
3444 qualifying scope was dependent. */
3445 if (TREE_CODE (decl) == SCOPE_REF)
3447 /* At this point, we do not know if DECL is a valid
3448 integral constant expression. We assume that it is
3449 in fact such an expression, so that code like:
3451 template <int N> struct A {
3455 is accepted. At template-instantiation time, we
3456 will check that B<N>::i is actually a constant. */
3459 /* Check to see if DECL is a local variable in a context
3460 where that is forbidden. */
3461 if (parser->local_variables_forbidden_p
3462 && local_variable_p (decl))
3464 /* It might be that we only found DECL because we are
3465 trying to be generous with pre-ISO scoping rules.
3466 For example, consider:
3470 for (int i = 0; i < 10; ++i) {}
3471 extern void f(int j = i);
3474 Here, name look up will originally find the out
3475 of scope `i'. We need to issue a warning message,
3476 but then use the global `i'. */
3477 decl = check_for_out_of_scope_variable (decl);
3478 if (local_variable_p (decl))
3480 error ("%Hlocal variable %qD may not appear in this context",
3481 &id_expr_token->location, decl);
3482 return error_mark_node;
3487 decl = (finish_id_expression
3488 (id_expression, decl, parser->scope,
3490 parser->integral_constant_expression_p,
3491 parser->allow_non_integral_constant_expression_p,
3492 &parser->non_integral_constant_expression_p,
3493 template_p, done, address_p,
3496 id_expr_token->location));
3498 cp_parser_error (parser, error_msg);
3502 /* Anything else is an error. */
3504 /* ...unless we have an Objective-C++ message or string literal,
3506 if (c_dialect_objc ()
3507 && (token->type == CPP_OPEN_SQUARE
3508 || token->type == CPP_OBJC_STRING))
3509 return cp_parser_objc_expression (parser);
3511 cp_parser_error (parser, "expected primary-expression");
3512 return error_mark_node;
3516 /* Parse an id-expression.
3523 :: [opt] nested-name-specifier template [opt] unqualified-id
3525 :: operator-function-id
3528 Return a representation of the unqualified portion of the
3529 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3530 a `::' or nested-name-specifier.
3532 Often, if the id-expression was a qualified-id, the caller will
3533 want to make a SCOPE_REF to represent the qualified-id. This
3534 function does not do this in order to avoid wastefully creating
3535 SCOPE_REFs when they are not required.
3537 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3540 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541 uninstantiated templates.
3543 If *TEMPLATE_P is non-NULL, it is set to true iff the
3544 `template' keyword is used to explicitly indicate that the entity
3545 named is a template.
3547 If DECLARATOR_P is true, the id-expression is appearing as part of
3548 a declarator, rather than as part of an expression. */
3551 cp_parser_id_expression (cp_parser *parser,
3552 bool template_keyword_p,
3553 bool check_dependency_p,
3558 bool global_scope_p;
3559 bool nested_name_specifier_p;
3561 /* Assume the `template' keyword was not used. */
3563 *template_p = template_keyword_p;
3565 /* Look for the optional `::' operator. */
3567 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3569 /* Look for the optional nested-name-specifier. */
3570 nested_name_specifier_p
3571 = (cp_parser_nested_name_specifier_opt (parser,
3572 /*typename_keyword_p=*/false,
3577 /* If there is a nested-name-specifier, then we are looking at
3578 the first qualified-id production. */
3579 if (nested_name_specifier_p)
3582 tree saved_object_scope;
3583 tree saved_qualifying_scope;
3584 tree unqualified_id;
3587 /* See if the next token is the `template' keyword. */
3589 template_p = &is_template;
3590 *template_p = cp_parser_optional_template_keyword (parser);
3591 /* Name lookup we do during the processing of the
3592 unqualified-id might obliterate SCOPE. */
3593 saved_scope = parser->scope;
3594 saved_object_scope = parser->object_scope;
3595 saved_qualifying_scope = parser->qualifying_scope;
3596 /* Process the final unqualified-id. */
3597 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3600 /*optional_p=*/false);
3601 /* Restore the SAVED_SCOPE for our caller. */
3602 parser->scope = saved_scope;
3603 parser->object_scope = saved_object_scope;
3604 parser->qualifying_scope = saved_qualifying_scope;
3606 return unqualified_id;
3608 /* Otherwise, if we are in global scope, then we are looking at one
3609 of the other qualified-id productions. */
3610 else if (global_scope_p)
3615 /* Peek at the next token. */
3616 token = cp_lexer_peek_token (parser->lexer);
3618 /* If it's an identifier, and the next token is not a "<", then
3619 we can avoid the template-id case. This is an optimization
3620 for this common case. */
3621 if (token->type == CPP_NAME
3622 && !cp_parser_nth_token_starts_template_argument_list_p
3624 return cp_parser_identifier (parser);
3626 cp_parser_parse_tentatively (parser);
3627 /* Try a template-id. */
3628 id = cp_parser_template_id (parser,
3629 /*template_keyword_p=*/false,
3630 /*check_dependency_p=*/true,
3632 /* If that worked, we're done. */
3633 if (cp_parser_parse_definitely (parser))
3636 /* Peek at the next token. (Changes in the token buffer may
3637 have invalidated the pointer obtained above.) */
3638 token = cp_lexer_peek_token (parser->lexer);
3640 switch (token->type)
3643 return cp_parser_identifier (parser);
3646 if (token->keyword == RID_OPERATOR)
3647 return cp_parser_operator_function_id (parser);
3651 cp_parser_error (parser, "expected id-expression");
3652 return error_mark_node;
3656 return cp_parser_unqualified_id (parser, template_keyword_p,
3657 /*check_dependency_p=*/true,
3662 /* Parse an unqualified-id.
3666 operator-function-id
3667 conversion-function-id
3671 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672 keyword, in a construct like `A::template ...'.
3674 Returns a representation of unqualified-id. For the `identifier'
3675 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3676 production a BIT_NOT_EXPR is returned; the operand of the
3677 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3678 other productions, see the documentation accompanying the
3679 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3680 names are looked up in uninstantiated templates. If DECLARATOR_P
3681 is true, the unqualified-id is appearing as part of a declarator,
3682 rather than as part of an expression. */
3685 cp_parser_unqualified_id (cp_parser* parser,
3686 bool template_keyword_p,
3687 bool check_dependency_p,
3693 /* Peek at the next token. */
3694 token = cp_lexer_peek_token (parser->lexer);
3696 switch (token->type)
3702 /* We don't know yet whether or not this will be a
3704 cp_parser_parse_tentatively (parser);
3705 /* Try a template-id. */
3706 id = cp_parser_template_id (parser, template_keyword_p,
3709 /* If it worked, we're done. */
3710 if (cp_parser_parse_definitely (parser))
3712 /* Otherwise, it's an ordinary identifier. */
3713 return cp_parser_identifier (parser);
3716 case CPP_TEMPLATE_ID:
3717 return cp_parser_template_id (parser, template_keyword_p,
3724 tree qualifying_scope;
3729 /* Consume the `~' token. */
3730 cp_lexer_consume_token (parser->lexer);
3731 /* Parse the class-name. The standard, as written, seems to
3734 template <typename T> struct S { ~S (); };
3735 template <typename T> S<T>::~S() {}
3737 is invalid, since `~' must be followed by a class-name, but
3738 `S<T>' is dependent, and so not known to be a class.
3739 That's not right; we need to look in uninstantiated
3740 templates. A further complication arises from:
3742 template <typename T> void f(T t) {
3746 Here, it is not possible to look up `T' in the scope of `T'
3747 itself. We must look in both the current scope, and the
3748 scope of the containing complete expression.
3750 Yet another issue is:
3759 The standard does not seem to say that the `S' in `~S'
3760 should refer to the type `S' and not the data member
3763 /* DR 244 says that we look up the name after the "~" in the
3764 same scope as we looked up the qualifying name. That idea
3765 isn't fully worked out; it's more complicated than that. */
3766 scope = parser->scope;
3767 object_scope = parser->object_scope;
3768 qualifying_scope = parser->qualifying_scope;
3770 /* Check for invalid scopes. */
3771 if (scope == error_mark_node)
3773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774 cp_lexer_consume_token (parser->lexer);
3775 return error_mark_node;
3777 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3779 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780 error ("%Hscope %qT before %<~%> is not a class-name",
3781 &token->location, scope);
3782 cp_parser_simulate_error (parser);
3783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784 cp_lexer_consume_token (parser->lexer);
3785 return error_mark_node;
3787 gcc_assert (!scope || TYPE_P (scope));
3789 /* If the name is of the form "X::~X" it's OK. */
3790 token = cp_lexer_peek_token (parser->lexer);
3792 && token->type == CPP_NAME
3793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3795 && constructor_name_p (token->u.value, scope))
3797 cp_lexer_consume_token (parser->lexer);
3798 return build_nt (BIT_NOT_EXPR, scope);
3801 /* If there was an explicit qualification (S::~T), first look
3802 in the scope given by the qualification (i.e., S). */
3804 type_decl = NULL_TREE;
3807 cp_parser_parse_tentatively (parser);
3808 type_decl = cp_parser_class_name (parser,
3809 /*typename_keyword_p=*/false,
3810 /*template_keyword_p=*/false,
3812 /*check_dependency=*/false,
3813 /*class_head_p=*/false,
3815 if (cp_parser_parse_definitely (parser))
3818 /* In "N::S::~S", look in "N" as well. */
3819 if (!done && scope && qualifying_scope)
3821 cp_parser_parse_tentatively (parser);
3822 parser->scope = qualifying_scope;
3823 parser->object_scope = NULL_TREE;
3824 parser->qualifying_scope = NULL_TREE;
3826 = cp_parser_class_name (parser,
3827 /*typename_keyword_p=*/false,
3828 /*template_keyword_p=*/false,
3830 /*check_dependency=*/false,
3831 /*class_head_p=*/false,
3833 if (cp_parser_parse_definitely (parser))
3836 /* In "p->S::~T", look in the scope given by "*p" as well. */
3837 else if (!done && object_scope)
3839 cp_parser_parse_tentatively (parser);
3840 parser->scope = object_scope;
3841 parser->object_scope = NULL_TREE;
3842 parser->qualifying_scope = NULL_TREE;
3844 = cp_parser_class_name (parser,
3845 /*typename_keyword_p=*/false,
3846 /*template_keyword_p=*/false,
3848 /*check_dependency=*/false,
3849 /*class_head_p=*/false,
3851 if (cp_parser_parse_definitely (parser))
3854 /* Look in the surrounding context. */
3857 parser->scope = NULL_TREE;
3858 parser->object_scope = NULL_TREE;
3859 parser->qualifying_scope = NULL_TREE;
3861 = cp_parser_class_name (parser,
3862 /*typename_keyword_p=*/false,
3863 /*template_keyword_p=*/false,
3865 /*check_dependency=*/false,
3866 /*class_head_p=*/false,
3869 /* If an error occurred, assume that the name of the
3870 destructor is the same as the name of the qualifying
3871 class. That allows us to keep parsing after running
3872 into ill-formed destructor names. */
3873 if (type_decl == error_mark_node && scope)
3874 return build_nt (BIT_NOT_EXPR, scope);
3875 else if (type_decl == error_mark_node)
3876 return error_mark_node;
3878 /* Check that destructor name and scope match. */
3879 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3881 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882 error ("%Hdeclaration of %<~%T%> as member of %qT",
3883 &token->location, type_decl, scope);
3884 cp_parser_simulate_error (parser);
3885 return error_mark_node;
3890 A typedef-name that names a class shall not be used as the
3891 identifier in the declarator for a destructor declaration. */
3893 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894 && !DECL_SELF_REFERENCE_P (type_decl)
3895 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896 error ("%Htypedef-name %qD used as destructor declarator",
3897 &token->location, type_decl);
3899 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3903 if (token->keyword == RID_OPERATOR)
3907 /* This could be a template-id, so we try that first. */
3908 cp_parser_parse_tentatively (parser);
3909 /* Try a template-id. */
3910 id = cp_parser_template_id (parser, template_keyword_p,
3911 /*check_dependency_p=*/true,
3913 /* If that worked, we're done. */
3914 if (cp_parser_parse_definitely (parser))
3916 /* We still don't know whether we're looking at an
3917 operator-function-id or a conversion-function-id. */
3918 cp_parser_parse_tentatively (parser);
3919 /* Try an operator-function-id. */
3920 id = cp_parser_operator_function_id (parser);
3921 /* If that didn't work, try a conversion-function-id. */
3922 if (!cp_parser_parse_definitely (parser))
3923 id = cp_parser_conversion_function_id (parser);
3932 cp_parser_error (parser, "expected unqualified-id");
3933 return error_mark_node;
3937 /* Parse an (optional) nested-name-specifier.
3939 nested-name-specifier: [C++98]
3940 class-or-namespace-name :: nested-name-specifier [opt]
3941 class-or-namespace-name :: template nested-name-specifier [opt]
3943 nested-name-specifier: [C++0x]
3946 nested-name-specifier identifier ::
3947 nested-name-specifier template [opt] simple-template-id ::
3949 PARSER->SCOPE should be set appropriately before this function is
3950 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3951 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3954 Sets PARSER->SCOPE to the class (TYPE) or namespace
3955 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3956 it unchanged if there is no nested-name-specifier. Returns the new
3957 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3959 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3960 part of a declaration and/or decl-specifier. */
3963 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3964 bool typename_keyword_p,
3965 bool check_dependency_p,
3967 bool is_declaration)
3969 bool success = false;
3970 cp_token_position start = 0;
3973 /* Remember where the nested-name-specifier starts. */
3974 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3976 start = cp_lexer_token_position (parser->lexer, false);
3977 push_deferring_access_checks (dk_deferred);
3984 tree saved_qualifying_scope;
3985 bool template_keyword_p;
3987 /* Spot cases that cannot be the beginning of a
3988 nested-name-specifier. */
3989 token = cp_lexer_peek_token (parser->lexer);
3991 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3992 the already parsed nested-name-specifier. */
3993 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3995 /* Grab the nested-name-specifier and continue the loop. */
3996 cp_parser_pre_parsed_nested_name_specifier (parser);
3997 /* If we originally encountered this nested-name-specifier
3998 with IS_DECLARATION set to false, we will not have
3999 resolved TYPENAME_TYPEs, so we must do so here. */
4001 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4003 new_scope = resolve_typename_type (parser->scope,
4004 /*only_current_p=*/false);
4005 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4006 parser->scope = new_scope;
4012 /* Spot cases that cannot be the beginning of a
4013 nested-name-specifier. On the second and subsequent times
4014 through the loop, we look for the `template' keyword. */
4015 if (success && token->keyword == RID_TEMPLATE)
4017 /* A template-id can start a nested-name-specifier. */
4018 else if (token->type == CPP_TEMPLATE_ID)
4022 /* If the next token is not an identifier, then it is
4023 definitely not a type-name or namespace-name. */
4024 if (token->type != CPP_NAME)
4026 /* If the following token is neither a `<' (to begin a
4027 template-id), nor a `::', then we are not looking at a
4028 nested-name-specifier. */
4029 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4030 if (token->type != CPP_SCOPE
4031 && !cp_parser_nth_token_starts_template_argument_list_p
4036 /* The nested-name-specifier is optional, so we parse
4038 cp_parser_parse_tentatively (parser);
4040 /* Look for the optional `template' keyword, if this isn't the
4041 first time through the loop. */
4043 template_keyword_p = cp_parser_optional_template_keyword (parser);
4045 template_keyword_p = false;
4047 /* Save the old scope since the name lookup we are about to do
4048 might destroy it. */
4049 old_scope = parser->scope;
4050 saved_qualifying_scope = parser->qualifying_scope;
4051 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4052 look up names in "X<T>::I" in order to determine that "Y" is
4053 a template. So, if we have a typename at this point, we make
4054 an effort to look through it. */
4056 && !typename_keyword_p
4058 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4059 parser->scope = resolve_typename_type (parser->scope,
4060 /*only_current_p=*/false);
4061 /* Parse the qualifying entity. */
4063 = cp_parser_qualifying_entity (parser,
4069 /* Look for the `::' token. */
4070 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4072 /* If we found what we wanted, we keep going; otherwise, we're
4074 if (!cp_parser_parse_definitely (parser))
4076 bool error_p = false;
4078 /* Restore the OLD_SCOPE since it was valid before the
4079 failed attempt at finding the last
4080 class-or-namespace-name. */
4081 parser->scope = old_scope;
4082 parser->qualifying_scope = saved_qualifying_scope;
4083 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4085 /* If the next token is an identifier, and the one after
4086 that is a `::', then any valid interpretation would have
4087 found a class-or-namespace-name. */
4088 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4089 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4091 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4094 token = cp_lexer_consume_token (parser->lexer);
4097 if (!token->ambiguous_p)
4100 tree ambiguous_decls;
4102 decl = cp_parser_lookup_name (parser, token->u.value,
4104 /*is_template=*/false,
4105 /*is_namespace=*/false,
4106 /*check_dependency=*/true,
4109 if (TREE_CODE (decl) == TEMPLATE_DECL)
4110 error ("%H%qD used without template parameters",
4111 &token->location, decl);
4112 else if (ambiguous_decls)
4114 error ("%Hreference to %qD is ambiguous",
4115 &token->location, token->u.value);
4116 print_candidates (ambiguous_decls);
4117 decl = error_mark_node;
4121 const char* msg = "is not a class or namespace";
4122 if (cxx_dialect != cxx98)
4123 msg = "is not a class, namespace, or enumeration";
4124 cp_parser_name_lookup_error
4125 (parser, token->u.value, decl, msg,
4129 parser->scope = error_mark_node;
4131 /* Treat this as a successful nested-name-specifier
4136 If the name found is not a class-name (clause
4137 _class_) or namespace-name (_namespace.def_), the
4138 program is ill-formed. */
4141 cp_lexer_consume_token (parser->lexer);
4145 /* We've found one valid nested-name-specifier. */
4147 /* Name lookup always gives us a DECL. */
4148 if (TREE_CODE (new_scope) == TYPE_DECL)
4149 new_scope = TREE_TYPE (new_scope);
4150 /* Uses of "template" must be followed by actual templates. */
4151 if (template_keyword_p
4152 && !(CLASS_TYPE_P (new_scope)
4153 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4154 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4155 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4156 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4157 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4158 == TEMPLATE_ID_EXPR)))
4159 permerror (input_location, TYPE_P (new_scope)
4160 ? "%qT is not a template"
4161 : "%qD is not a template",
4163 /* If it is a class scope, try to complete it; we are about to
4164 be looking up names inside the class. */
4165 if (TYPE_P (new_scope)
4166 /* Since checking types for dependency can be expensive,
4167 avoid doing it if the type is already complete. */
4168 && !COMPLETE_TYPE_P (new_scope)
4169 /* Do not try to complete dependent types. */
4170 && !dependent_type_p (new_scope))
4172 new_scope = complete_type (new_scope);
4173 /* If it is a typedef to current class, use the current
4174 class instead, as the typedef won't have any names inside
4176 if (!COMPLETE_TYPE_P (new_scope)
4177 && currently_open_class (new_scope))
4178 new_scope = TYPE_MAIN_VARIANT (new_scope);
4180 /* Make sure we look in the right scope the next time through
4182 parser->scope = new_scope;
4185 /* If parsing tentatively, replace the sequence of tokens that makes
4186 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4187 token. That way, should we re-parse the token stream, we will
4188 not have to repeat the effort required to do the parse, nor will
4189 we issue duplicate error messages. */
4190 if (success && start)
4194 token = cp_lexer_token_at (parser->lexer, start);
4195 /* Reset the contents of the START token. */
4196 token->type = CPP_NESTED_NAME_SPECIFIER;
4197 /* Retrieve any deferred checks. Do not pop this access checks yet
4198 so the memory will not be reclaimed during token replacing below. */
4199 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4200 token->u.tree_check_value->value = parser->scope;
4201 token->u.tree_check_value->checks = get_deferred_access_checks ();
4202 token->u.tree_check_value->qualifying_scope =
4203 parser->qualifying_scope;
4204 token->keyword = RID_MAX;
4206 /* Purge all subsequent tokens. */
4207 cp_lexer_purge_tokens_after (parser->lexer, start);
4211 pop_to_parent_deferring_access_checks ();
4213 return success ? parser->scope : NULL_TREE;
4216 /* Parse a nested-name-specifier. See
4217 cp_parser_nested_name_specifier_opt for details. This function
4218 behaves identically, except that it will an issue an error if no
4219 nested-name-specifier is present. */
4222 cp_parser_nested_name_specifier (cp_parser *parser,
4223 bool typename_keyword_p,
4224 bool check_dependency_p,
4226 bool is_declaration)
4230 /* Look for the nested-name-specifier. */
4231 scope = cp_parser_nested_name_specifier_opt (parser,
4236 /* If it was not present, issue an error message. */
4239 cp_parser_error (parser, "expected nested-name-specifier");
4240 parser->scope = NULL_TREE;
4246 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4247 this is either a class-name or a namespace-name (which corresponds
4248 to the class-or-namespace-name production in the grammar). For
4249 C++0x, it can also be a type-name that refers to an enumeration
4252 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4253 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4254 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4255 TYPE_P is TRUE iff the next name should be taken as a class-name,
4256 even the same name is declared to be another entity in the same
4259 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4260 specified by the class-or-namespace-name. If neither is found the
4261 ERROR_MARK_NODE is returned. */
4264 cp_parser_qualifying_entity (cp_parser *parser,
4265 bool typename_keyword_p,
4266 bool template_keyword_p,
4267 bool check_dependency_p,
4269 bool is_declaration)
4272 tree saved_qualifying_scope;
4273 tree saved_object_scope;
4276 bool successful_parse_p;
4278 /* Before we try to parse the class-name, we must save away the
4279 current PARSER->SCOPE since cp_parser_class_name will destroy
4281 saved_scope = parser->scope;
4282 saved_qualifying_scope = parser->qualifying_scope;
4283 saved_object_scope = parser->object_scope;
4284 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4285 there is no need to look for a namespace-name. */
4286 only_class_p = template_keyword_p
4287 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4289 cp_parser_parse_tentatively (parser);
4290 scope = cp_parser_class_name (parser,
4293 type_p ? class_type : none_type,
4295 /*class_head_p=*/false,
4297 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4298 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4300 && cxx_dialect != cxx98
4301 && !successful_parse_p)
4303 /* Restore the saved scope. */
4304 parser->scope = saved_scope;
4305 parser->qualifying_scope = saved_qualifying_scope;
4306 parser->object_scope = saved_object_scope;
4308 /* Parse tentatively. */
4309 cp_parser_parse_tentatively (parser);
4311 /* Parse a typedef-name or enum-name. */
4312 scope = cp_parser_nonclass_name (parser);
4313 successful_parse_p = cp_parser_parse_definitely (parser);
4315 /* If that didn't work, try for a namespace-name. */
4316 if (!only_class_p && !successful_parse_p)
4318 /* Restore the saved scope. */
4319 parser->scope = saved_scope;
4320 parser->qualifying_scope = saved_qualifying_scope;
4321 parser->object_scope = saved_object_scope;
4322 /* If we are not looking at an identifier followed by the scope
4323 resolution operator, then this is not part of a
4324 nested-name-specifier. (Note that this function is only used
4325 to parse the components of a nested-name-specifier.) */
4326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4327 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4328 return error_mark_node;
4329 scope = cp_parser_namespace_name (parser);
4335 /* Parse a postfix-expression.
4339 postfix-expression [ expression ]
4340 postfix-expression ( expression-list [opt] )
4341 simple-type-specifier ( expression-list [opt] )
4342 typename :: [opt] nested-name-specifier identifier
4343 ( expression-list [opt] )
4344 typename :: [opt] nested-name-specifier template [opt] template-id
4345 ( expression-list [opt] )
4346 postfix-expression . template [opt] id-expression
4347 postfix-expression -> template [opt] id-expression
4348 postfix-expression . pseudo-destructor-name
4349 postfix-expression -> pseudo-destructor-name
4350 postfix-expression ++
4351 postfix-expression --
4352 dynamic_cast < type-id > ( expression )
4353 static_cast < type-id > ( expression )
4354 reinterpret_cast < type-id > ( expression )
4355 const_cast < type-id > ( expression )
4356 typeid ( expression )
4362 ( type-id ) { initializer-list , [opt] }
4364 This extension is a GNU version of the C99 compound-literal
4365 construct. (The C99 grammar uses `type-name' instead of `type-id',
4366 but they are essentially the same concept.)
4368 If ADDRESS_P is true, the postfix expression is the operand of the
4369 `&' operator. CAST_P is true if this expression is the target of a
4372 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4373 class member access expressions [expr.ref].
4375 Returns a representation of the expression. */
4378 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4379 bool member_access_only_p)
4383 cp_id_kind idk = CP_ID_KIND_NONE;
4384 tree postfix_expression = NULL_TREE;
4385 bool is_member_access = false;
4387 /* Peek at the next token. */
4388 token = cp_lexer_peek_token (parser->lexer);
4389 /* Some of the productions are determined by keywords. */
4390 keyword = token->keyword;
4400 const char *saved_message;
4402 /* All of these can be handled in the same way from the point
4403 of view of parsing. Begin by consuming the token
4404 identifying the cast. */
4405 cp_lexer_consume_token (parser->lexer);
4407 /* New types cannot be defined in the cast. */
4408 saved_message = parser->type_definition_forbidden_message;
4409 parser->type_definition_forbidden_message
4410 = "types may not be defined in casts";
4412 /* Look for the opening `<'. */
4413 cp_parser_require (parser, CPP_LESS, "%<<%>");
4414 /* Parse the type to which we are casting. */
4415 type = cp_parser_type_id (parser);
4416 /* Look for the closing `>'. */
4417 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4418 /* Restore the old message. */
4419 parser->type_definition_forbidden_message = saved_message;
4421 /* And the expression which is being cast. */
4422 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4423 expression = cp_parser_expression (parser, /*cast_p=*/true);
4424 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4426 /* Only type conversions to integral or enumeration types
4427 can be used in constant-expressions. */
4428 if (!cast_valid_in_integral_constant_expression_p (type)
4429 && (cp_parser_non_integral_constant_expression
4431 "a cast to a type other than an integral or "
4432 "enumeration type")))
4433 return error_mark_node;
4439 = build_dynamic_cast (type, expression, tf_warning_or_error);
4443 = build_static_cast (type, expression, tf_warning_or_error);
4447 = build_reinterpret_cast (type, expression,
4448 tf_warning_or_error);
4452 = build_const_cast (type, expression, tf_warning_or_error);
4463 const char *saved_message;
4464 bool saved_in_type_id_in_expr_p;
4466 /* Consume the `typeid' token. */
4467 cp_lexer_consume_token (parser->lexer);
4468 /* Look for the `(' token. */
4469 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470 /* Types cannot be defined in a `typeid' expression. */
4471 saved_message = parser->type_definition_forbidden_message;
4472 parser->type_definition_forbidden_message
4473 = "types may not be defined in a %<typeid%> expression";
4474 /* We can't be sure yet whether we're looking at a type-id or an
4476 cp_parser_parse_tentatively (parser);
4477 /* Try a type-id first. */
4478 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4479 parser->in_type_id_in_expr_p = true;
4480 type = cp_parser_type_id (parser);
4481 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4482 /* Look for the `)' token. Otherwise, we can't be sure that
4483 we're not looking at an expression: consider `typeid (int
4484 (3))', for example. */
4485 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4486 /* If all went well, simply lookup the type-id. */
4487 if (cp_parser_parse_definitely (parser))
4488 postfix_expression = get_typeid (type);
4489 /* Otherwise, fall back to the expression variant. */
4494 /* Look for an expression. */
4495 expression = cp_parser_expression (parser, /*cast_p=*/false);
4496 /* Compute its typeid. */
4497 postfix_expression = build_typeid (expression);
4498 /* Look for the `)' token. */
4499 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4501 /* Restore the saved message. */
4502 parser->type_definition_forbidden_message = saved_message;
4503 /* `typeid' may not appear in an integral constant expression. */
4504 if (cp_parser_non_integral_constant_expression(parser,
4505 "%<typeid%> operator"))
4506 return error_mark_node;
4513 /* The syntax permitted here is the same permitted for an
4514 elaborated-type-specifier. */
4515 type = cp_parser_elaborated_type_specifier (parser,
4516 /*is_friend=*/false,
4517 /*is_declaration=*/false);
4518 postfix_expression = cp_parser_functional_cast (parser, type);
4526 /* If the next thing is a simple-type-specifier, we may be
4527 looking at a functional cast. We could also be looking at
4528 an id-expression. So, we try the functional cast, and if
4529 that doesn't work we fall back to the primary-expression. */
4530 cp_parser_parse_tentatively (parser);
4531 /* Look for the simple-type-specifier. */
4532 type = cp_parser_simple_type_specifier (parser,
4533 /*decl_specs=*/NULL,
4534 CP_PARSER_FLAGS_NONE);
4535 /* Parse the cast itself. */
4536 if (!cp_parser_error_occurred (parser))
4538 = cp_parser_functional_cast (parser, type);
4539 /* If that worked, we're done. */
4540 if (cp_parser_parse_definitely (parser))
4543 /* If the functional-cast didn't work out, try a
4544 compound-literal. */
4545 if (cp_parser_allow_gnu_extensions_p (parser)
4546 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4548 VEC(constructor_elt,gc) *initializer_list = NULL;
4549 bool saved_in_type_id_in_expr_p;
4551 cp_parser_parse_tentatively (parser);
4552 /* Consume the `('. */
4553 cp_lexer_consume_token (parser->lexer);
4554 /* Parse the type. */
4555 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4556 parser->in_type_id_in_expr_p = true;
4557 type = cp_parser_type_id (parser);
4558 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4559 /* Look for the `)'. */
4560 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4561 /* Look for the `{'. */
4562 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4563 /* If things aren't going well, there's no need to
4565 if (!cp_parser_error_occurred (parser))
4567 bool non_constant_p;
4568 /* Parse the initializer-list. */
4570 = cp_parser_initializer_list (parser, &non_constant_p);
4571 /* Allow a trailing `,'. */
4572 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4573 cp_lexer_consume_token (parser->lexer);
4574 /* Look for the final `}'. */
4575 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4577 /* If that worked, we're definitely looking at a
4578 compound-literal expression. */
4579 if (cp_parser_parse_definitely (parser))
4581 /* Warn the user that a compound literal is not
4582 allowed in standard C++. */
4583 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4584 /* For simplicity, we disallow compound literals in
4585 constant-expressions. We could
4586 allow compound literals of integer type, whose
4587 initializer was a constant, in constant
4588 expressions. Permitting that usage, as a further
4589 extension, would not change the meaning of any
4590 currently accepted programs. (Of course, as
4591 compound literals are not part of ISO C++, the
4592 standard has nothing to say.) */
4593 if (cp_parser_non_integral_constant_expression
4594 (parser, "non-constant compound literals"))
4596 postfix_expression = error_mark_node;
4599 /* Form the representation of the compound-literal. */
4601 = (finish_compound_literal
4602 (type, build_constructor (init_list_type_node,
4603 initializer_list)));
4608 /* It must be a primary-expression. */
4610 = cp_parser_primary_expression (parser, address_p, cast_p,
4611 /*template_arg_p=*/false,
4617 /* Keep looping until the postfix-expression is complete. */
4620 if (idk == CP_ID_KIND_UNQUALIFIED
4621 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4623 /* It is not a Koenig lookup function call. */
4625 = unqualified_name_lookup_error (postfix_expression);
4627 /* Peek at the next token. */
4628 token = cp_lexer_peek_token (parser->lexer);
4630 switch (token->type)
4632 case CPP_OPEN_SQUARE:
4634 = cp_parser_postfix_open_square_expression (parser,
4637 idk = CP_ID_KIND_NONE;
4638 is_member_access = false;
4641 case CPP_OPEN_PAREN:
4642 /* postfix-expression ( expression-list [opt] ) */
4645 bool is_builtin_constant_p;
4646 bool saved_integral_constant_expression_p = false;
4647 bool saved_non_integral_constant_expression_p = false;
4650 is_member_access = false;
4652 is_builtin_constant_p
4653 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4654 if (is_builtin_constant_p)
4656 /* The whole point of __builtin_constant_p is to allow
4657 non-constant expressions to appear as arguments. */
4658 saved_integral_constant_expression_p
4659 = parser->integral_constant_expression_p;
4660 saved_non_integral_constant_expression_p
4661 = parser->non_integral_constant_expression_p;
4662 parser->integral_constant_expression_p = false;
4664 args = (cp_parser_parenthesized_expression_list
4665 (parser, /*is_attribute_list=*/false,
4666 /*cast_p=*/false, /*allow_expansion_p=*/true,
4667 /*non_constant_p=*/NULL));
4668 if (is_builtin_constant_p)
4670 parser->integral_constant_expression_p
4671 = saved_integral_constant_expression_p;
4672 parser->non_integral_constant_expression_p
4673 = saved_non_integral_constant_expression_p;
4676 if (args == error_mark_node)
4678 postfix_expression = error_mark_node;
4682 /* Function calls are not permitted in
4683 constant-expressions. */
4684 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4685 && cp_parser_non_integral_constant_expression (parser,
4688 postfix_expression = error_mark_node;
4693 if (idk == CP_ID_KIND_UNQUALIFIED)
4695 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4701 = perform_koenig_lookup (postfix_expression, args);
4705 = unqualified_fn_lookup_error (postfix_expression);
4707 /* We do not perform argument-dependent lookup if
4708 normal lookup finds a non-function, in accordance
4709 with the expected resolution of DR 218. */
4710 else if (args && is_overloaded_fn (postfix_expression))
4712 tree fn = get_first_fn (postfix_expression);
4714 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4715 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4717 /* Only do argument dependent lookup if regular
4718 lookup does not find a set of member functions.
4719 [basic.lookup.koenig]/2a */
4720 if (!DECL_FUNCTION_MEMBER_P (fn))
4724 = perform_koenig_lookup (postfix_expression, args);
4729 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4731 tree instance = TREE_OPERAND (postfix_expression, 0);
4732 tree fn = TREE_OPERAND (postfix_expression, 1);
4734 if (processing_template_decl
4735 && (type_dependent_expression_p (instance)
4736 || (!BASELINK_P (fn)
4737 && TREE_CODE (fn) != FIELD_DECL)
4738 || type_dependent_expression_p (fn)
4739 || any_type_dependent_arguments_p (args)))
4742 = build_nt_call_list (postfix_expression, args);
4746 if (BASELINK_P (fn))
4748 = (build_new_method_call
4749 (instance, fn, args, NULL_TREE,
4750 (idk == CP_ID_KIND_QUALIFIED
4751 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4753 tf_warning_or_error));
4756 = finish_call_expr (postfix_expression, args,
4757 /*disallow_virtual=*/false,
4759 tf_warning_or_error);
4761 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4762 || TREE_CODE (postfix_expression) == MEMBER_REF
4763 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4764 postfix_expression = (build_offset_ref_call_from_tree
4765 (postfix_expression, args));
4766 else if (idk == CP_ID_KIND_QUALIFIED)
4767 /* A call to a static class member, or a namespace-scope
4770 = finish_call_expr (postfix_expression, args,
4771 /*disallow_virtual=*/true,
4773 tf_warning_or_error);
4775 /* All other function calls. */
4777 = finish_call_expr (postfix_expression, args,
4778 /*disallow_virtual=*/false,
4780 tf_warning_or_error);
4782 if (warn_disallowed_functions)
4783 warn_if_disallowed_function_p (postfix_expression);
4785 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4786 idk = CP_ID_KIND_NONE;
4792 /* postfix-expression . template [opt] id-expression
4793 postfix-expression . pseudo-destructor-name
4794 postfix-expression -> template [opt] id-expression
4795 postfix-expression -> pseudo-destructor-name */
4797 /* Consume the `.' or `->' operator. */
4798 cp_lexer_consume_token (parser->lexer);
4801 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4806 is_member_access = true;
4810 /* postfix-expression ++ */
4811 /* Consume the `++' token. */
4812 cp_lexer_consume_token (parser->lexer);
4813 /* Generate a representation for the complete expression. */
4815 = finish_increment_expr (postfix_expression,
4816 POSTINCREMENT_EXPR);
4817 /* Increments may not appear in constant-expressions. */
4818 if (cp_parser_non_integral_constant_expression (parser,
4820 postfix_expression = error_mark_node;
4821 idk = CP_ID_KIND_NONE;
4822 is_member_access = false;
4825 case CPP_MINUS_MINUS:
4826 /* postfix-expression -- */
4827 /* Consume the `--' token. */
4828 cp_lexer_consume_token (parser->lexer);
4829 /* Generate a representation for the complete expression. */
4831 = finish_increment_expr (postfix_expression,
4832 POSTDECREMENT_EXPR);
4833 /* Decrements may not appear in constant-expressions. */
4834 if (cp_parser_non_integral_constant_expression (parser,
4836 postfix_expression = error_mark_node;
4837 idk = CP_ID_KIND_NONE;
4838 is_member_access = false;
4842 if (member_access_only_p)
4843 return is_member_access? postfix_expression : error_mark_node;
4845 return postfix_expression;
4849 /* We should never get here. */
4851 return error_mark_node;
4854 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4855 by cp_parser_builtin_offsetof. We're looking for
4857 postfix-expression [ expression ]
4859 FOR_OFFSETOF is set if we're being called in that context, which
4860 changes how we deal with integer constant expressions. */
4863 cp_parser_postfix_open_square_expression (cp_parser *parser,
4864 tree postfix_expression,
4869 /* Consume the `[' token. */
4870 cp_lexer_consume_token (parser->lexer);
4872 /* Parse the index expression. */
4873 /* ??? For offsetof, there is a question of what to allow here. If
4874 offsetof is not being used in an integral constant expression context,
4875 then we *could* get the right answer by computing the value at runtime.
4876 If we are in an integral constant expression context, then we might
4877 could accept any constant expression; hard to say without analysis.
4878 Rather than open the barn door too wide right away, allow only integer
4879 constant expressions here. */
4881 index = cp_parser_constant_expression (parser, false, NULL);
4883 index = cp_parser_expression (parser, /*cast_p=*/false);
4885 /* Look for the closing `]'. */
4886 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4888 /* Build the ARRAY_REF. */
4889 postfix_expression = grok_array_decl (postfix_expression, index);
4891 /* When not doing offsetof, array references are not permitted in
4892 constant-expressions. */
4894 && (cp_parser_non_integral_constant_expression
4895 (parser, "an array reference")))
4896 postfix_expression = error_mark_node;
4898 return postfix_expression;
4901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4902 by cp_parser_builtin_offsetof. We're looking for
4904 postfix-expression . template [opt] id-expression
4905 postfix-expression . pseudo-destructor-name
4906 postfix-expression -> template [opt] id-expression
4907 postfix-expression -> pseudo-destructor-name
4909 FOR_OFFSETOF is set if we're being called in that context. That sorta
4910 limits what of the above we'll actually accept, but nevermind.
4911 TOKEN_TYPE is the "." or "->" token, which will already have been
4912 removed from the stream. */
4915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4916 enum cpp_ttype token_type,
4917 tree postfix_expression,
4918 bool for_offsetof, cp_id_kind *idk,
4919 location_t location)
4923 bool pseudo_destructor_p;
4924 tree scope = NULL_TREE;
4926 /* If this is a `->' operator, dereference the pointer. */
4927 if (token_type == CPP_DEREF)
4928 postfix_expression = build_x_arrow (postfix_expression);
4929 /* Check to see whether or not the expression is type-dependent. */
4930 dependent_p = type_dependent_expression_p (postfix_expression);
4931 /* The identifier following the `->' or `.' is not qualified. */
4932 parser->scope = NULL_TREE;
4933 parser->qualifying_scope = NULL_TREE;
4934 parser->object_scope = NULL_TREE;
4935 *idk = CP_ID_KIND_NONE;
4936 /* Enter the scope corresponding to the type of the object
4937 given by the POSTFIX_EXPRESSION. */
4938 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4940 scope = TREE_TYPE (postfix_expression);
4941 /* According to the standard, no expression should ever have
4942 reference type. Unfortunately, we do not currently match
4943 the standard in this respect in that our internal representation
4944 of an expression may have reference type even when the standard
4945 says it does not. Therefore, we have to manually obtain the
4946 underlying type here. */
4947 scope = non_reference (scope);
4948 /* The type of the POSTFIX_EXPRESSION must be complete. */
4949 if (scope == unknown_type_node)
4951 error ("%H%qE does not have class type", &location, postfix_expression);
4955 scope = complete_type_or_else (scope, NULL_TREE);
4956 /* Let the name lookup machinery know that we are processing a
4957 class member access expression. */
4958 parser->context->object_type = scope;
4959 /* If something went wrong, we want to be able to discern that case,
4960 as opposed to the case where there was no SCOPE due to the type
4961 of expression being dependent. */
4963 scope = error_mark_node;
4964 /* If the SCOPE was erroneous, make the various semantic analysis
4965 functions exit quickly -- and without issuing additional error
4967 if (scope == error_mark_node)
4968 postfix_expression = error_mark_node;
4971 /* Assume this expression is not a pseudo-destructor access. */
4972 pseudo_destructor_p = false;
4974 /* If the SCOPE is a scalar type, then, if this is a valid program,
4975 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4976 is type dependent, it can be pseudo-destructor-name or something else.
4977 Try to parse it as pseudo-destructor-name first. */
4978 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4983 cp_parser_parse_tentatively (parser);
4984 /* Parse the pseudo-destructor-name. */
4986 cp_parser_pseudo_destructor_name (parser, &s, &type);
4988 && (cp_parser_error_occurred (parser)
4989 || TREE_CODE (type) != TYPE_DECL
4990 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4991 cp_parser_abort_tentative_parse (parser);
4992 else if (cp_parser_parse_definitely (parser))
4994 pseudo_destructor_p = true;
4996 = finish_pseudo_destructor_expr (postfix_expression,
4997 s, TREE_TYPE (type));
5001 if (!pseudo_destructor_p)
5003 /* If the SCOPE is not a scalar type, we are looking at an
5004 ordinary class member access expression, rather than a
5005 pseudo-destructor-name. */
5007 cp_token *token = cp_lexer_peek_token (parser->lexer);
5008 /* Parse the id-expression. */
5009 name = (cp_parser_id_expression
5011 cp_parser_optional_template_keyword (parser),
5012 /*check_dependency_p=*/true,
5014 /*declarator_p=*/false,
5015 /*optional_p=*/false));
5016 /* In general, build a SCOPE_REF if the member name is qualified.
5017 However, if the name was not dependent and has already been
5018 resolved; there is no need to build the SCOPE_REF. For example;
5020 struct X { void f(); };
5021 template <typename T> void f(T* t) { t->X::f(); }
5023 Even though "t" is dependent, "X::f" is not and has been resolved
5024 to a BASELINK; there is no need to include scope information. */
5026 /* But we do need to remember that there was an explicit scope for
5027 virtual function calls. */
5029 *idk = CP_ID_KIND_QUALIFIED;
5031 /* If the name is a template-id that names a type, we will get a
5032 TYPE_DECL here. That is invalid code. */
5033 if (TREE_CODE (name) == TYPE_DECL)
5035 error ("%Hinvalid use of %qD", &token->location, name);
5036 postfix_expression = error_mark_node;
5040 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5042 name = build_qualified_name (/*type=*/NULL_TREE,
5046 parser->scope = NULL_TREE;
5047 parser->qualifying_scope = NULL_TREE;
5048 parser->object_scope = NULL_TREE;
5050 if (scope && name && BASELINK_P (name))
5051 adjust_result_of_qualified_name_lookup
5052 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5054 = finish_class_member_access_expr (postfix_expression, name,
5056 tf_warning_or_error);
5060 /* We no longer need to look up names in the scope of the object on
5061 the left-hand side of the `.' or `->' operator. */
5062 parser->context->object_type = NULL_TREE;
5064 /* Outside of offsetof, these operators may not appear in
5065 constant-expressions. */
5067 && (cp_parser_non_integral_constant_expression
5068 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5069 postfix_expression = error_mark_node;
5071 return postfix_expression;
5074 /* Parse a parenthesized expression-list.
5077 assignment-expression
5078 expression-list, assignment-expression
5083 identifier, expression-list
5085 CAST_P is true if this expression is the target of a cast.
5087 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5090 Returns a TREE_LIST. The TREE_VALUE of each node is a
5091 representation of an assignment-expression. Note that a TREE_LIST
5092 is returned even if there is only a single expression in the list.
5093 error_mark_node is returned if the ( and or ) are
5094 missing. NULL_TREE is returned on no expressions. The parentheses
5095 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5096 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5097 indicates whether or not all of the expressions in the list were
5101 cp_parser_parenthesized_expression_list (cp_parser* parser,
5102 bool is_attribute_list,
5104 bool allow_expansion_p,
5105 bool *non_constant_p)
5107 tree expression_list = NULL_TREE;
5108 bool fold_expr_p = is_attribute_list;
5109 tree identifier = NULL_TREE;
5110 bool saved_greater_than_is_operator_p;
5112 /* Assume all the expressions will be constant. */
5114 *non_constant_p = false;
5116 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5117 return error_mark_node;
5119 /* Within a parenthesized expression, a `>' token is always
5120 the greater-than operator. */
5121 saved_greater_than_is_operator_p
5122 = parser->greater_than_is_operator_p;
5123 parser->greater_than_is_operator_p = true;
5125 /* Consume expressions until there are no more. */
5126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5131 /* At the beginning of attribute lists, check to see if the
5132 next token is an identifier. */
5133 if (is_attribute_list
5134 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5138 /* Consume the identifier. */
5139 token = cp_lexer_consume_token (parser->lexer);
5140 /* Save the identifier. */
5141 identifier = token->u.value;
5145 bool expr_non_constant_p;
5147 /* Parse the next assignment-expression. */
5148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5150 /* A braced-init-list. */
5151 maybe_warn_cpp0x ("extended initializer lists");
5152 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5153 if (non_constant_p && expr_non_constant_p)
5154 *non_constant_p = true;
5156 else if (non_constant_p)
5158 expr = (cp_parser_constant_expression
5159 (parser, /*allow_non_constant_p=*/true,
5160 &expr_non_constant_p));
5161 if (expr_non_constant_p)
5162 *non_constant_p = true;
5165 expr = cp_parser_assignment_expression (parser, cast_p);
5168 expr = fold_non_dependent_expr (expr);
5170 /* If we have an ellipsis, then this is an expression
5172 if (allow_expansion_p
5173 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5175 /* Consume the `...'. */
5176 cp_lexer_consume_token (parser->lexer);
5178 /* Build the argument pack. */
5179 expr = make_pack_expansion (expr);
5182 /* Add it to the list. We add error_mark_node
5183 expressions to the list, so that we can still tell if
5184 the correct form for a parenthesized expression-list
5185 is found. That gives better errors. */
5186 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5188 if (expr == error_mark_node)
5192 /* After the first item, attribute lists look the same as
5193 expression lists. */
5194 is_attribute_list = false;
5197 /* If the next token isn't a `,', then we are done. */
5198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5201 /* Otherwise, consume the `,' and keep going. */
5202 cp_lexer_consume_token (parser->lexer);
5205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5210 /* We try and resync to an unnested comma, as that will give the
5211 user better diagnostics. */
5212 ending = cp_parser_skip_to_closing_parenthesis (parser,
5213 /*recovering=*/true,
5215 /*consume_paren=*/true);
5220 parser->greater_than_is_operator_p
5221 = saved_greater_than_is_operator_p;
5222 return error_mark_node;
5226 parser->greater_than_is_operator_p
5227 = saved_greater_than_is_operator_p;
5229 /* We built up the list in reverse order so we must reverse it now. */
5230 expression_list = nreverse (expression_list);
5232 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5234 return expression_list;
5237 /* Parse a pseudo-destructor-name.
5239 pseudo-destructor-name:
5240 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5241 :: [opt] nested-name-specifier template template-id :: ~ type-name
5242 :: [opt] nested-name-specifier [opt] ~ type-name
5244 If either of the first two productions is used, sets *SCOPE to the
5245 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5246 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5247 or ERROR_MARK_NODE if the parse fails. */
5250 cp_parser_pseudo_destructor_name (cp_parser* parser,
5254 bool nested_name_specifier_p;
5256 /* Assume that things will not work out. */
5257 *type = error_mark_node;
5259 /* Look for the optional `::' operator. */
5260 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5261 /* Look for the optional nested-name-specifier. */
5262 nested_name_specifier_p
5263 = (cp_parser_nested_name_specifier_opt (parser,
5264 /*typename_keyword_p=*/false,
5265 /*check_dependency_p=*/true,
5267 /*is_declaration=*/true)
5269 /* Now, if we saw a nested-name-specifier, we might be doing the
5270 second production. */
5271 if (nested_name_specifier_p
5272 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5274 /* Consume the `template' keyword. */
5275 cp_lexer_consume_token (parser->lexer);
5276 /* Parse the template-id. */
5277 cp_parser_template_id (parser,
5278 /*template_keyword_p=*/true,
5279 /*check_dependency_p=*/false,
5280 /*is_declaration=*/true);
5281 /* Look for the `::' token. */
5282 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5284 /* If the next token is not a `~', then there might be some
5285 additional qualification. */
5286 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5288 /* At this point, we're looking for "type-name :: ~". The type-name
5289 must not be a class-name, since this is a pseudo-destructor. So,
5290 it must be either an enum-name, or a typedef-name -- both of which
5291 are just identifiers. So, we peek ahead to check that the "::"
5292 and "~" tokens are present; if they are not, then we can avoid
5293 calling type_name. */
5294 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5295 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5296 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5298 cp_parser_error (parser, "non-scalar type");
5302 /* Look for the type-name. */
5303 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5304 if (*scope == error_mark_node)
5307 /* Look for the `::' token. */
5308 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5313 /* Look for the `~'. */
5314 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5315 /* Look for the type-name again. We are not responsible for
5316 checking that it matches the first type-name. */
5317 *type = cp_parser_nonclass_name (parser);
5320 /* Parse a unary-expression.
5326 unary-operator cast-expression
5327 sizeof unary-expression
5335 __extension__ cast-expression
5336 __alignof__ unary-expression
5337 __alignof__ ( type-id )
5338 __real__ cast-expression
5339 __imag__ cast-expression
5342 ADDRESS_P is true iff the unary-expression is appearing as the
5343 operand of the `&' operator. CAST_P is true if this expression is
5344 the target of a cast.
5346 Returns a representation of the expression. */
5349 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5352 enum tree_code unary_operator;
5354 /* Peek at the next token. */
5355 token = cp_lexer_peek_token (parser->lexer);
5356 /* Some keywords give away the kind of expression. */
5357 if (token->type == CPP_KEYWORD)
5359 enum rid keyword = token->keyword;
5369 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5370 /* Consume the token. */
5371 cp_lexer_consume_token (parser->lexer);
5372 /* Parse the operand. */
5373 operand = cp_parser_sizeof_operand (parser, keyword);
5375 if (TYPE_P (operand))
5376 return cxx_sizeof_or_alignof_type (operand, op, true);
5378 return cxx_sizeof_or_alignof_expr (operand, op, true);
5382 return cp_parser_new_expression (parser);
5385 return cp_parser_delete_expression (parser);
5389 /* The saved value of the PEDANTIC flag. */
5393 /* Save away the PEDANTIC flag. */
5394 cp_parser_extension_opt (parser, &saved_pedantic);
5395 /* Parse the cast-expression. */
5396 expr = cp_parser_simple_cast_expression (parser);
5397 /* Restore the PEDANTIC flag. */
5398 pedantic = saved_pedantic;
5408 /* Consume the `__real__' or `__imag__' token. */
5409 cp_lexer_consume_token (parser->lexer);
5410 /* Parse the cast-expression. */
5411 expression = cp_parser_simple_cast_expression (parser);
5412 /* Create the complete representation. */
5413 return build_x_unary_op ((keyword == RID_REALPART
5414 ? REALPART_EXPR : IMAGPART_EXPR),
5416 tf_warning_or_error);
5425 /* Look for the `:: new' and `:: delete', which also signal the
5426 beginning of a new-expression, or delete-expression,
5427 respectively. If the next token is `::', then it might be one of
5429 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5433 /* See if the token after the `::' is one of the keywords in
5434 which we're interested. */
5435 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5436 /* If it's `new', we have a new-expression. */
5437 if (keyword == RID_NEW)
5438 return cp_parser_new_expression (parser);
5439 /* Similarly, for `delete'. */
5440 else if (keyword == RID_DELETE)
5441 return cp_parser_delete_expression (parser);
5444 /* Look for a unary operator. */
5445 unary_operator = cp_parser_unary_operator (token);
5446 /* The `++' and `--' operators can be handled similarly, even though
5447 they are not technically unary-operators in the grammar. */
5448 if (unary_operator == ERROR_MARK)
5450 if (token->type == CPP_PLUS_PLUS)
5451 unary_operator = PREINCREMENT_EXPR;
5452 else if (token->type == CPP_MINUS_MINUS)
5453 unary_operator = PREDECREMENT_EXPR;
5454 /* Handle the GNU address-of-label extension. */
5455 else if (cp_parser_allow_gnu_extensions_p (parser)
5456 && token->type == CPP_AND_AND)
5460 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5462 /* Consume the '&&' token. */
5463 cp_lexer_consume_token (parser->lexer);
5464 /* Look for the identifier. */
5465 identifier = cp_parser_identifier (parser);
5466 /* Create an expression representing the address. */
5467 expression = finish_label_address_expr (identifier, loc);
5468 if (cp_parser_non_integral_constant_expression (parser,
5469 "the address of a label"))
5470 expression = error_mark_node;
5474 if (unary_operator != ERROR_MARK)
5476 tree cast_expression;
5477 tree expression = error_mark_node;
5478 const char *non_constant_p = NULL;
5480 /* Consume the operator token. */
5481 token = cp_lexer_consume_token (parser->lexer);
5482 /* Parse the cast-expression. */
5484 = cp_parser_cast_expression (parser,
5485 unary_operator == ADDR_EXPR,
5487 /* Now, build an appropriate representation. */
5488 switch (unary_operator)
5491 non_constant_p = "%<*%>";
5492 expression = build_x_indirect_ref (cast_expression, "unary *",
5493 tf_warning_or_error);
5497 non_constant_p = "%<&%>";
5500 expression = build_x_unary_op (unary_operator, cast_expression,
5501 tf_warning_or_error);
5504 case PREINCREMENT_EXPR:
5505 case PREDECREMENT_EXPR:
5506 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5507 ? "%<++%>" : "%<--%>");
5509 case UNARY_PLUS_EXPR:
5511 case TRUTH_NOT_EXPR:
5512 expression = finish_unary_op_expr (unary_operator, cast_expression);
5520 && cp_parser_non_integral_constant_expression (parser,
5522 expression = error_mark_node;
5527 return cp_parser_postfix_expression (parser, address_p, cast_p,
5528 /*member_access_only_p=*/false);
5531 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5532 unary-operator, the corresponding tree code is returned. */
5534 static enum tree_code
5535 cp_parser_unary_operator (cp_token* token)
5537 switch (token->type)
5540 return INDIRECT_REF;
5546 return UNARY_PLUS_EXPR;
5552 return TRUTH_NOT_EXPR;
5555 return BIT_NOT_EXPR;
5562 /* Parse a new-expression.
5565 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5566 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5568 Returns a representation of the expression. */
5571 cp_parser_new_expression (cp_parser* parser)
5573 bool global_scope_p;
5579 /* Look for the optional `::' operator. */
5581 = (cp_parser_global_scope_opt (parser,
5582 /*current_scope_valid_p=*/false)
5584 /* Look for the `new' operator. */
5585 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5586 /* There's no easy way to tell a new-placement from the
5587 `( type-id )' construct. */
5588 cp_parser_parse_tentatively (parser);
5589 /* Look for a new-placement. */
5590 placement = cp_parser_new_placement (parser);
5591 /* If that didn't work out, there's no new-placement. */
5592 if (!cp_parser_parse_definitely (parser))
5593 placement = NULL_TREE;
5595 /* If the next token is a `(', then we have a parenthesized
5597 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5600 /* Consume the `('. */
5601 cp_lexer_consume_token (parser->lexer);
5602 /* Parse the type-id. */
5603 type = cp_parser_type_id (parser);
5604 /* Look for the closing `)'. */
5605 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5606 token = cp_lexer_peek_token (parser->lexer);
5607 /* There should not be a direct-new-declarator in this production,
5608 but GCC used to allowed this, so we check and emit a sensible error
5609 message for this case. */
5610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5612 error ("%Harray bound forbidden after parenthesized type-id",
5614 inform (token->location,
5615 "try removing the parentheses around the type-id");
5616 cp_parser_direct_new_declarator (parser);
5620 /* Otherwise, there must be a new-type-id. */
5622 type = cp_parser_new_type_id (parser, &nelts);
5624 /* If the next token is a `(' or '{', then we have a new-initializer. */
5625 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5626 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5627 initializer = cp_parser_new_initializer (parser);
5629 initializer = NULL_TREE;
5631 /* A new-expression may not appear in an integral constant
5633 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5634 return error_mark_node;
5636 /* Create a representation of the new-expression. */
5637 return build_new (placement, type, nelts, initializer, global_scope_p,
5638 tf_warning_or_error);
5641 /* Parse a new-placement.
5646 Returns the same representation as for an expression-list. */
5649 cp_parser_new_placement (cp_parser* parser)
5651 tree expression_list;
5653 /* Parse the expression-list. */
5654 expression_list = (cp_parser_parenthesized_expression_list
5655 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5656 /*non_constant_p=*/NULL));
5658 return expression_list;
5661 /* Parse a new-type-id.
5664 type-specifier-seq new-declarator [opt]
5666 Returns the TYPE allocated. If the new-type-id indicates an array
5667 type, *NELTS is set to the number of elements in the last array
5668 bound; the TYPE will not include the last array bound. */
5671 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5673 cp_decl_specifier_seq type_specifier_seq;
5674 cp_declarator *new_declarator;
5675 cp_declarator *declarator;
5676 cp_declarator *outer_declarator;
5677 const char *saved_message;
5680 /* The type-specifier sequence must not contain type definitions.
5681 (It cannot contain declarations of new types either, but if they
5682 are not definitions we will catch that because they are not
5684 saved_message = parser->type_definition_forbidden_message;
5685 parser->type_definition_forbidden_message
5686 = "types may not be defined in a new-type-id";
5687 /* Parse the type-specifier-seq. */
5688 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5689 &type_specifier_seq);
5690 /* Restore the old message. */
5691 parser->type_definition_forbidden_message = saved_message;
5692 /* Parse the new-declarator. */
5693 new_declarator = cp_parser_new_declarator_opt (parser);
5695 /* Determine the number of elements in the last array dimension, if
5698 /* Skip down to the last array dimension. */
5699 declarator = new_declarator;
5700 outer_declarator = NULL;
5701 while (declarator && (declarator->kind == cdk_pointer
5702 || declarator->kind == cdk_ptrmem))
5704 outer_declarator = declarator;
5705 declarator = declarator->declarator;
5708 && declarator->kind == cdk_array
5709 && declarator->declarator
5710 && declarator->declarator->kind == cdk_array)
5712 outer_declarator = declarator;
5713 declarator = declarator->declarator;
5716 if (declarator && declarator->kind == cdk_array)
5718 *nelts = declarator->u.array.bounds;
5719 if (*nelts == error_mark_node)
5720 *nelts = integer_one_node;
5722 if (outer_declarator)
5723 outer_declarator->declarator = declarator->declarator;
5725 new_declarator = NULL;
5728 type = groktypename (&type_specifier_seq, new_declarator);
5732 /* Parse an (optional) new-declarator.
5735 ptr-operator new-declarator [opt]
5736 direct-new-declarator
5738 Returns the declarator. */
5740 static cp_declarator *
5741 cp_parser_new_declarator_opt (cp_parser* parser)
5743 enum tree_code code;
5745 cp_cv_quals cv_quals;
5747 /* We don't know if there's a ptr-operator next, or not. */
5748 cp_parser_parse_tentatively (parser);
5749 /* Look for a ptr-operator. */
5750 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5751 /* If that worked, look for more new-declarators. */
5752 if (cp_parser_parse_definitely (parser))
5754 cp_declarator *declarator;
5756 /* Parse another optional declarator. */
5757 declarator = cp_parser_new_declarator_opt (parser);
5759 return cp_parser_make_indirect_declarator
5760 (code, type, cv_quals, declarator);
5763 /* If the next token is a `[', there is a direct-new-declarator. */
5764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5765 return cp_parser_direct_new_declarator (parser);
5770 /* Parse a direct-new-declarator.
5772 direct-new-declarator:
5774 direct-new-declarator [constant-expression]
5778 static cp_declarator *
5779 cp_parser_direct_new_declarator (cp_parser* parser)
5781 cp_declarator *declarator = NULL;
5787 /* Look for the opening `['. */
5788 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5789 /* The first expression is not required to be constant. */
5792 cp_token *token = cp_lexer_peek_token (parser->lexer);
5793 expression = cp_parser_expression (parser, /*cast_p=*/false);
5794 /* The standard requires that the expression have integral
5795 type. DR 74 adds enumeration types. We believe that the
5796 real intent is that these expressions be handled like the
5797 expression in a `switch' condition, which also allows
5798 classes with a single conversion to integral or
5799 enumeration type. */
5800 if (!processing_template_decl)
5803 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5808 error ("%Hexpression in new-declarator must have integral "
5809 "or enumeration type", &token->location);
5810 expression = error_mark_node;
5814 /* But all the other expressions must be. */
5817 = cp_parser_constant_expression (parser,
5818 /*allow_non_constant=*/false,
5820 /* Look for the closing `]'. */
5821 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5823 /* Add this bound to the declarator. */
5824 declarator = make_array_declarator (declarator, expression);
5826 /* If the next token is not a `[', then there are no more
5828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5835 /* Parse a new-initializer.
5838 ( expression-list [opt] )
5841 Returns a representation of the expression-list. If there is no
5842 expression-list, VOID_ZERO_NODE is returned. */
5845 cp_parser_new_initializer (cp_parser* parser)
5847 tree expression_list;
5849 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5851 bool expr_non_constant_p;
5852 maybe_warn_cpp0x ("extended initializer lists");
5853 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5854 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5855 expression_list = build_tree_list (NULL_TREE, expression_list);
5858 expression_list = (cp_parser_parenthesized_expression_list
5859 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5860 /*non_constant_p=*/NULL));
5861 if (!expression_list)
5862 expression_list = void_zero_node;
5864 return expression_list;
5867 /* Parse a delete-expression.
5870 :: [opt] delete cast-expression
5871 :: [opt] delete [ ] cast-expression
5873 Returns a representation of the expression. */
5876 cp_parser_delete_expression (cp_parser* parser)
5878 bool global_scope_p;
5882 /* Look for the optional `::' operator. */
5884 = (cp_parser_global_scope_opt (parser,
5885 /*current_scope_valid_p=*/false)
5887 /* Look for the `delete' keyword. */
5888 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5889 /* See if the array syntax is in use. */
5890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5892 /* Consume the `[' token. */
5893 cp_lexer_consume_token (parser->lexer);
5894 /* Look for the `]' token. */
5895 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5896 /* Remember that this is the `[]' construct. */
5902 /* Parse the cast-expression. */
5903 expression = cp_parser_simple_cast_expression (parser);
5905 /* A delete-expression may not appear in an integral constant
5907 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5908 return error_mark_node;
5910 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5913 /* Returns true if TOKEN may start a cast-expression and false
5917 cp_parser_token_starts_cast_expression (cp_token *token)
5919 switch (token->type)
5925 case CPP_CLOSE_SQUARE:
5926 case CPP_CLOSE_PAREN:
5927 case CPP_CLOSE_BRACE:
5931 case CPP_DEREF_STAR:
5939 case CPP_GREATER_EQ:
5958 /* '[' may start a primary-expression in obj-c++. */
5959 case CPP_OPEN_SQUARE:
5960 return c_dialect_objc ();
5967 /* Parse a cast-expression.
5971 ( type-id ) cast-expression
5973 ADDRESS_P is true iff the unary-expression is appearing as the
5974 operand of the `&' operator. CAST_P is true if this expression is
5975 the target of a cast.
5977 Returns a representation of the expression. */
5980 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5982 /* If it's a `(', then we might be looking at a cast. */
5983 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5985 tree type = NULL_TREE;
5986 tree expr = NULL_TREE;
5987 bool compound_literal_p;
5988 const char *saved_message;
5990 /* There's no way to know yet whether or not this is a cast.
5991 For example, `(int (3))' is a unary-expression, while `(int)
5992 3' is a cast. So, we resort to parsing tentatively. */
5993 cp_parser_parse_tentatively (parser);
5994 /* Types may not be defined in a cast. */
5995 saved_message = parser->type_definition_forbidden_message;
5996 parser->type_definition_forbidden_message
5997 = "types may not be defined in casts";
5998 /* Consume the `('. */
5999 cp_lexer_consume_token (parser->lexer);
6000 /* A very tricky bit is that `(struct S) { 3 }' is a
6001 compound-literal (which we permit in C++ as an extension).
6002 But, that construct is not a cast-expression -- it is a
6003 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6004 is legal; if the compound-literal were a cast-expression,
6005 you'd need an extra set of parentheses.) But, if we parse
6006 the type-id, and it happens to be a class-specifier, then we
6007 will commit to the parse at that point, because we cannot
6008 undo the action that is done when creating a new class. So,
6009 then we cannot back up and do a postfix-expression.
6011 Therefore, we scan ahead to the closing `)', and check to see
6012 if the token after the `)' is a `{'. If so, we are not
6013 looking at a cast-expression.
6015 Save tokens so that we can put them back. */
6016 cp_lexer_save_tokens (parser->lexer);
6017 /* Skip tokens until the next token is a closing parenthesis.
6018 If we find the closing `)', and the next token is a `{', then
6019 we are looking at a compound-literal. */
6021 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6022 /*consume_paren=*/true)
6023 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6024 /* Roll back the tokens we skipped. */
6025 cp_lexer_rollback_tokens (parser->lexer);
6026 /* If we were looking at a compound-literal, simulate an error
6027 so that the call to cp_parser_parse_definitely below will
6029 if (compound_literal_p)
6030 cp_parser_simulate_error (parser);
6033 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6034 parser->in_type_id_in_expr_p = true;
6035 /* Look for the type-id. */
6036 type = cp_parser_type_id (parser);
6037 /* Look for the closing `)'. */
6038 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6039 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6042 /* Restore the saved message. */
6043 parser->type_definition_forbidden_message = saved_message;
6045 /* At this point this can only be either a cast or a
6046 parenthesized ctor such as `(T ())' that looks like a cast to
6047 function returning T. */
6048 if (!cp_parser_error_occurred (parser)
6049 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6052 cp_parser_parse_definitely (parser);
6053 expr = cp_parser_cast_expression (parser,
6054 /*address_p=*/false,
6057 /* Warn about old-style casts, if so requested. */
6058 if (warn_old_style_cast
6059 && !in_system_header
6060 && !VOID_TYPE_P (type)
6061 && current_lang_name != lang_name_c)
6062 warning (OPT_Wold_style_cast, "use of old-style cast");
6064 /* Only type conversions to integral or enumeration types
6065 can be used in constant-expressions. */
6066 if (!cast_valid_in_integral_constant_expression_p (type)
6067 && (cp_parser_non_integral_constant_expression
6069 "a cast to a type other than an integral or "
6070 "enumeration type")))
6071 return error_mark_node;
6073 /* Perform the cast. */
6074 expr = build_c_cast (type, expr);
6078 cp_parser_abort_tentative_parse (parser);
6081 /* If we get here, then it's not a cast, so it must be a
6082 unary-expression. */
6083 return cp_parser_unary_expression (parser, address_p, cast_p);
6086 /* Parse a binary expression of the general form:
6090 pm-expression .* cast-expression
6091 pm-expression ->* cast-expression
6093 multiplicative-expression:
6095 multiplicative-expression * pm-expression
6096 multiplicative-expression / pm-expression
6097 multiplicative-expression % pm-expression
6099 additive-expression:
6100 multiplicative-expression
6101 additive-expression + multiplicative-expression
6102 additive-expression - multiplicative-expression
6106 shift-expression << additive-expression
6107 shift-expression >> additive-expression
6109 relational-expression:
6111 relational-expression < shift-expression
6112 relational-expression > shift-expression
6113 relational-expression <= shift-expression
6114 relational-expression >= shift-expression
6118 relational-expression:
6119 relational-expression <? shift-expression
6120 relational-expression >? shift-expression
6122 equality-expression:
6123 relational-expression
6124 equality-expression == relational-expression
6125 equality-expression != relational-expression
6129 and-expression & equality-expression
6131 exclusive-or-expression:
6133 exclusive-or-expression ^ and-expression
6135 inclusive-or-expression:
6136 exclusive-or-expression
6137 inclusive-or-expression | exclusive-or-expression
6139 logical-and-expression:
6140 inclusive-or-expression
6141 logical-and-expression && inclusive-or-expression
6143 logical-or-expression:
6144 logical-and-expression
6145 logical-or-expression || logical-and-expression
6147 All these are implemented with a single function like:
6150 simple-cast-expression
6151 binary-expression <token> binary-expression
6153 CAST_P is true if this expression is the target of a cast.
6155 The binops_by_token map is used to get the tree codes for each <token> type.
6156 binary-expressions are associated according to a precedence table. */
6158 #define TOKEN_PRECEDENCE(token) \
6159 (((token->type == CPP_GREATER \
6160 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6161 && !parser->greater_than_is_operator_p) \
6162 ? PREC_NOT_OPERATOR \
6163 : binops_by_token[token->type].prec)
6166 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6167 enum cp_parser_prec prec)
6169 cp_parser_expression_stack stack;
6170 cp_parser_expression_stack_entry *sp = &stack[0];
6173 enum tree_code tree_type, lhs_type, rhs_type;
6174 enum cp_parser_prec new_prec, lookahead_prec;
6177 /* Parse the first expression. */
6178 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6179 lhs_type = ERROR_MARK;
6183 /* Get an operator token. */
6184 token = cp_lexer_peek_token (parser->lexer);
6186 if (warn_cxx0x_compat
6187 && token->type == CPP_RSHIFT
6188 && !parser->greater_than_is_operator_p)
6190 warning (OPT_Wc__0x_compat,
6191 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6193 warning (OPT_Wc__0x_compat,
6194 "suggest parentheses around %<>>%> expression");
6197 new_prec = TOKEN_PRECEDENCE (token);
6199 /* Popping an entry off the stack means we completed a subexpression:
6200 - either we found a token which is not an operator (`>' where it is not
6201 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6202 will happen repeatedly;
6203 - or, we found an operator which has lower priority. This is the case
6204 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6206 if (new_prec <= prec)
6215 tree_type = binops_by_token[token->type].tree_type;
6217 /* We used the operator token. */
6218 cp_lexer_consume_token (parser->lexer);
6220 /* Extract another operand. It may be the RHS of this expression
6221 or the LHS of a new, higher priority expression. */
6222 rhs = cp_parser_simple_cast_expression (parser);
6223 rhs_type = ERROR_MARK;
6225 /* Get another operator token. Look up its precedence to avoid
6226 building a useless (immediately popped) stack entry for common
6227 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6228 token = cp_lexer_peek_token (parser->lexer);
6229 lookahead_prec = TOKEN_PRECEDENCE (token);
6230 if (lookahead_prec > new_prec)
6232 /* ... and prepare to parse the RHS of the new, higher priority
6233 expression. Since precedence levels on the stack are
6234 monotonically increasing, we do not have to care about
6237 sp->tree_type = tree_type;
6239 sp->lhs_type = lhs_type;
6242 lhs_type = rhs_type;
6244 new_prec = lookahead_prec;
6248 /* If the stack is not empty, we have parsed into LHS the right side
6249 (`4' in the example above) of an expression we had suspended.
6250 We can use the information on the stack to recover the LHS (`3')
6251 from the stack together with the tree code (`MULT_EXPR'), and
6252 the precedence of the higher level subexpression
6253 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6254 which will be used to actually build the additive expression. */
6257 tree_type = sp->tree_type;
6259 rhs_type = lhs_type;
6261 lhs_type = sp->lhs_type;
6264 overloaded_p = false;
6265 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6266 ERROR_MARK for everything that is not a binary expression.
6267 This makes warn_about_parentheses miss some warnings that
6268 involve unary operators. For unary expressions we should
6269 pass the correct tree_code unless the unary expression was
6270 surrounded by parentheses.
6272 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6273 &overloaded_p, tf_warning_or_error);
6274 lhs_type = tree_type;
6276 /* If the binary operator required the use of an overloaded operator,
6277 then this expression cannot be an integral constant-expression.
6278 An overloaded operator can be used even if both operands are
6279 otherwise permissible in an integral constant-expression if at
6280 least one of the operands is of enumeration type. */
6283 && (cp_parser_non_integral_constant_expression
6284 (parser, "calls to overloaded operators")))
6285 return error_mark_node;
6292 /* Parse the `? expression : assignment-expression' part of a
6293 conditional-expression. The LOGICAL_OR_EXPR is the
6294 logical-or-expression that started the conditional-expression.
6295 Returns a representation of the entire conditional-expression.
6297 This routine is used by cp_parser_assignment_expression.
6299 ? expression : assignment-expression
6303 ? : assignment-expression */
6306 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6309 tree assignment_expr;
6311 /* Consume the `?' token. */
6312 cp_lexer_consume_token (parser->lexer);
6313 if (cp_parser_allow_gnu_extensions_p (parser)
6314 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6315 /* Implicit true clause. */
6318 /* Parse the expression. */
6319 expr = cp_parser_expression (parser, /*cast_p=*/false);
6321 /* The next token should be a `:'. */
6322 cp_parser_require (parser, CPP_COLON, "%<:%>");
6323 /* Parse the assignment-expression. */
6324 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6326 /* Build the conditional-expression. */
6327 return build_x_conditional_expr (logical_or_expr,
6330 tf_warning_or_error);
6333 /* Parse an assignment-expression.
6335 assignment-expression:
6336 conditional-expression
6337 logical-or-expression assignment-operator assignment_expression
6340 CAST_P is true if this expression is the target of a cast.
6342 Returns a representation for the expression. */
6345 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6349 /* If the next token is the `throw' keyword, then we're looking at
6350 a throw-expression. */
6351 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6352 expr = cp_parser_throw_expression (parser);
6353 /* Otherwise, it must be that we are looking at a
6354 logical-or-expression. */
6357 /* Parse the binary expressions (logical-or-expression). */
6358 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6359 /* If the next token is a `?' then we're actually looking at a
6360 conditional-expression. */
6361 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6362 return cp_parser_question_colon_clause (parser, expr);
6365 enum tree_code assignment_operator;
6367 /* If it's an assignment-operator, we're using the second
6370 = cp_parser_assignment_operator_opt (parser);
6371 if (assignment_operator != ERROR_MARK)
6373 bool non_constant_p;
6375 /* Parse the right-hand side of the assignment. */
6376 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6378 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6379 maybe_warn_cpp0x ("extended initializer lists");
6381 /* An assignment may not appear in a
6382 constant-expression. */
6383 if (cp_parser_non_integral_constant_expression (parser,
6385 return error_mark_node;
6386 /* Build the assignment expression. */
6387 expr = build_x_modify_expr (expr,
6388 assignment_operator,
6390 tf_warning_or_error);
6398 /* Parse an (optional) assignment-operator.
6400 assignment-operator: one of
6401 = *= /= %= += -= >>= <<= &= ^= |=
6405 assignment-operator: one of
6408 If the next token is an assignment operator, the corresponding tree
6409 code is returned, and the token is consumed. For example, for
6410 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6411 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6412 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6413 operator, ERROR_MARK is returned. */
6415 static enum tree_code
6416 cp_parser_assignment_operator_opt (cp_parser* parser)
6421 /* Peek at the next token. */
6422 token = cp_lexer_peek_token (parser->lexer);
6424 switch (token->type)
6435 op = TRUNC_DIV_EXPR;
6439 op = TRUNC_MOD_EXPR;
6471 /* Nothing else is an assignment operator. */
6475 /* If it was an assignment operator, consume it. */
6476 if (op != ERROR_MARK)
6477 cp_lexer_consume_token (parser->lexer);
6482 /* Parse an expression.
6485 assignment-expression
6486 expression , assignment-expression
6488 CAST_P is true if this expression is the target of a cast.
6490 Returns a representation of the expression. */
6493 cp_parser_expression (cp_parser* parser, bool cast_p)
6495 tree expression = NULL_TREE;
6499 tree assignment_expression;
6501 /* Parse the next assignment-expression. */
6502 assignment_expression
6503 = cp_parser_assignment_expression (parser, cast_p);
6504 /* If this is the first assignment-expression, we can just
6507 expression = assignment_expression;
6509 expression = build_x_compound_expr (expression,
6510 assignment_expression,
6511 tf_warning_or_error);
6512 /* If the next token is not a comma, then we are done with the
6514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6516 /* Consume the `,'. */
6517 cp_lexer_consume_token (parser->lexer);
6518 /* A comma operator cannot appear in a constant-expression. */
6519 if (cp_parser_non_integral_constant_expression (parser,
6520 "a comma operator"))
6521 expression = error_mark_node;
6527 /* Parse a constant-expression.
6529 constant-expression:
6530 conditional-expression
6532 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6533 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6534 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6535 is false, NON_CONSTANT_P should be NULL. */
6538 cp_parser_constant_expression (cp_parser* parser,
6539 bool allow_non_constant_p,
6540 bool *non_constant_p)
6542 bool saved_integral_constant_expression_p;
6543 bool saved_allow_non_integral_constant_expression_p;
6544 bool saved_non_integral_constant_expression_p;
6547 /* It might seem that we could simply parse the
6548 conditional-expression, and then check to see if it were
6549 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6550 one that the compiler can figure out is constant, possibly after
6551 doing some simplifications or optimizations. The standard has a
6552 precise definition of constant-expression, and we must honor
6553 that, even though it is somewhat more restrictive.
6559 is not a legal declaration, because `(2, 3)' is not a
6560 constant-expression. The `,' operator is forbidden in a
6561 constant-expression. However, GCC's constant-folding machinery
6562 will fold this operation to an INTEGER_CST for `3'. */
6564 /* Save the old settings. */
6565 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6566 saved_allow_non_integral_constant_expression_p
6567 = parser->allow_non_integral_constant_expression_p;
6568 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6569 /* We are now parsing a constant-expression. */
6570 parser->integral_constant_expression_p = true;
6571 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6572 parser->non_integral_constant_expression_p = false;
6573 /* Although the grammar says "conditional-expression", we parse an
6574 "assignment-expression", which also permits "throw-expression"
6575 and the use of assignment operators. In the case that
6576 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6577 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6578 actually essential that we look for an assignment-expression.
6579 For example, cp_parser_initializer_clauses uses this function to
6580 determine whether a particular assignment-expression is in fact
6582 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6583 /* Restore the old settings. */
6584 parser->integral_constant_expression_p
6585 = saved_integral_constant_expression_p;
6586 parser->allow_non_integral_constant_expression_p
6587 = saved_allow_non_integral_constant_expression_p;
6588 if (allow_non_constant_p)
6589 *non_constant_p = parser->non_integral_constant_expression_p;
6590 else if (parser->non_integral_constant_expression_p)
6591 expression = error_mark_node;
6592 parser->non_integral_constant_expression_p
6593 = saved_non_integral_constant_expression_p;
6598 /* Parse __builtin_offsetof.
6600 offsetof-expression:
6601 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6603 offsetof-member-designator:
6605 | offsetof-member-designator "." id-expression
6606 | offsetof-member-designator "[" expression "]" */
6609 cp_parser_builtin_offsetof (cp_parser *parser)
6611 int save_ice_p, save_non_ice_p;
6616 /* We're about to accept non-integral-constant things, but will
6617 definitely yield an integral constant expression. Save and
6618 restore these values around our local parsing. */
6619 save_ice_p = parser->integral_constant_expression_p;
6620 save_non_ice_p = parser->non_integral_constant_expression_p;
6622 /* Consume the "__builtin_offsetof" token. */
6623 cp_lexer_consume_token (parser->lexer);
6624 /* Consume the opening `('. */
6625 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6626 /* Parse the type-id. */
6627 type = cp_parser_type_id (parser);
6628 /* Look for the `,'. */
6629 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6630 token = cp_lexer_peek_token (parser->lexer);
6632 /* Build the (type *)null that begins the traditional offsetof macro. */
6633 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6634 tf_warning_or_error);
6636 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6637 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6638 true, &dummy, token->location);
6641 token = cp_lexer_peek_token (parser->lexer);
6642 switch (token->type)
6644 case CPP_OPEN_SQUARE:
6645 /* offsetof-member-designator "[" expression "]" */
6646 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6650 /* offsetof-member-designator "." identifier */
6651 cp_lexer_consume_token (parser->lexer);
6652 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6657 case CPP_CLOSE_PAREN:
6658 /* Consume the ")" token. */
6659 cp_lexer_consume_token (parser->lexer);
6663 /* Error. We know the following require will fail, but
6664 that gives the proper error message. */
6665 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6666 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6667 expr = error_mark_node;
6673 /* If we're processing a template, we can't finish the semantics yet.
6674 Otherwise we can fold the entire expression now. */
6675 if (processing_template_decl)
6676 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6678 expr = finish_offsetof (expr);
6681 parser->integral_constant_expression_p = save_ice_p;
6682 parser->non_integral_constant_expression_p = save_non_ice_p;
6687 /* Parse a trait expression. */
6690 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6693 tree type1, type2 = NULL_TREE;
6694 bool binary = false;
6695 cp_decl_specifier_seq decl_specs;
6699 case RID_HAS_NOTHROW_ASSIGN:
6700 kind = CPTK_HAS_NOTHROW_ASSIGN;
6702 case RID_HAS_NOTHROW_CONSTRUCTOR:
6703 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6705 case RID_HAS_NOTHROW_COPY:
6706 kind = CPTK_HAS_NOTHROW_COPY;
6708 case RID_HAS_TRIVIAL_ASSIGN:
6709 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6711 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6712 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6714 case RID_HAS_TRIVIAL_COPY:
6715 kind = CPTK_HAS_TRIVIAL_COPY;
6717 case RID_HAS_TRIVIAL_DESTRUCTOR:
6718 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6720 case RID_HAS_VIRTUAL_DESTRUCTOR:
6721 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6723 case RID_IS_ABSTRACT:
6724 kind = CPTK_IS_ABSTRACT;
6726 case RID_IS_BASE_OF:
6727 kind = CPTK_IS_BASE_OF;
6731 kind = CPTK_IS_CLASS;
6733 case RID_IS_CONVERTIBLE_TO:
6734 kind = CPTK_IS_CONVERTIBLE_TO;
6738 kind = CPTK_IS_EMPTY;
6741 kind = CPTK_IS_ENUM;
6746 case RID_IS_POLYMORPHIC:
6747 kind = CPTK_IS_POLYMORPHIC;
6750 kind = CPTK_IS_UNION;
6756 /* Consume the token. */
6757 cp_lexer_consume_token (parser->lexer);
6759 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6761 type1 = cp_parser_type_id (parser);
6763 if (type1 == error_mark_node)
6764 return error_mark_node;
6766 /* Build a trivial decl-specifier-seq. */
6767 clear_decl_specs (&decl_specs);
6768 decl_specs.type = type1;
6770 /* Call grokdeclarator to figure out what type this is. */
6771 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6772 /*initialized=*/0, /*attrlist=*/NULL);
6776 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6778 type2 = cp_parser_type_id (parser);
6780 if (type2 == error_mark_node)
6781 return error_mark_node;
6783 /* Build a trivial decl-specifier-seq. */
6784 clear_decl_specs (&decl_specs);
6785 decl_specs.type = type2;
6787 /* Call grokdeclarator to figure out what type this is. */
6788 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6789 /*initialized=*/0, /*attrlist=*/NULL);
6792 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6794 /* Complete the trait expression, which may mean either processing
6795 the trait expr now or saving it for template instantiation. */
6796 return finish_trait_expr (kind, type1, type2);
6799 /* Statements [gram.stmt.stmt] */
6801 /* Parse a statement.
6805 expression-statement
6810 declaration-statement
6813 IN_COMPOUND is true when the statement is nested inside a
6814 cp_parser_compound_statement; this matters for certain pragmas.
6816 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6817 is a (possibly labeled) if statement which is not enclosed in braces
6818 and has an else clause. This is used to implement -Wparentheses. */
6821 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6822 bool in_compound, bool *if_p)
6826 location_t statement_location;
6831 /* There is no statement yet. */
6832 statement = NULL_TREE;
6833 /* Peek at the next token. */
6834 token = cp_lexer_peek_token (parser->lexer);
6835 /* Remember the location of the first token in the statement. */
6836 statement_location = token->location;
6837 /* If this is a keyword, then that will often determine what kind of
6838 statement we have. */
6839 if (token->type == CPP_KEYWORD)
6841 enum rid keyword = token->keyword;
6847 /* Looks like a labeled-statement with a case label.
6848 Parse the label, and then use tail recursion to parse
6850 cp_parser_label_for_labeled_statement (parser);
6855 statement = cp_parser_selection_statement (parser, if_p);
6861 statement = cp_parser_iteration_statement (parser);
6868 statement = cp_parser_jump_statement (parser);
6871 /* Objective-C++ exception-handling constructs. */
6874 case RID_AT_FINALLY:
6875 case RID_AT_SYNCHRONIZED:
6877 statement = cp_parser_objc_statement (parser);
6881 statement = cp_parser_try_block (parser);
6885 /* This must be a namespace alias definition. */
6886 cp_parser_declaration_statement (parser);
6890 /* It might be a keyword like `int' that can start a
6891 declaration-statement. */
6895 else if (token->type == CPP_NAME)
6897 /* If the next token is a `:', then we are looking at a
6898 labeled-statement. */
6899 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6900 if (token->type == CPP_COLON)
6902 /* Looks like a labeled-statement with an ordinary label.
6903 Parse the label, and then use tail recursion to parse
6905 cp_parser_label_for_labeled_statement (parser);
6909 /* Anything that starts with a `{' must be a compound-statement. */
6910 else if (token->type == CPP_OPEN_BRACE)
6911 statement = cp_parser_compound_statement (parser, NULL, false);
6912 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6913 a statement all its own. */
6914 else if (token->type == CPP_PRAGMA)
6916 /* Only certain OpenMP pragmas are attached to statements, and thus
6917 are considered statements themselves. All others are not. In
6918 the context of a compound, accept the pragma as a "statement" and
6919 return so that we can check for a close brace. Otherwise we
6920 require a real statement and must go back and read one. */
6922 cp_parser_pragma (parser, pragma_compound);
6923 else if (!cp_parser_pragma (parser, pragma_stmt))
6927 else if (token->type == CPP_EOF)
6929 cp_parser_error (parser, "expected statement");
6933 /* Everything else must be a declaration-statement or an
6934 expression-statement. Try for the declaration-statement
6935 first, unless we are looking at a `;', in which case we know that
6936 we have an expression-statement. */
6939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6941 cp_parser_parse_tentatively (parser);
6942 /* Try to parse the declaration-statement. */
6943 cp_parser_declaration_statement (parser);
6944 /* If that worked, we're done. */
6945 if (cp_parser_parse_definitely (parser))
6948 /* Look for an expression-statement instead. */
6949 statement = cp_parser_expression_statement (parser, in_statement_expr);
6952 /* Set the line number for the statement. */
6953 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6954 SET_EXPR_LOCATION (statement, statement_location);
6957 /* Parse the label for a labeled-statement, i.e.
6960 case constant-expression :
6964 case constant-expression ... constant-expression : statement
6966 When a label is parsed without errors, the label is added to the
6967 parse tree by the finish_* functions, so this function doesn't
6968 have to return the label. */
6971 cp_parser_label_for_labeled_statement (cp_parser* parser)
6975 /* The next token should be an identifier. */
6976 token = cp_lexer_peek_token (parser->lexer);
6977 if (token->type != CPP_NAME
6978 && token->type != CPP_KEYWORD)
6980 cp_parser_error (parser, "expected labeled-statement");
6984 switch (token->keyword)
6991 /* Consume the `case' token. */
6992 cp_lexer_consume_token (parser->lexer);
6993 /* Parse the constant-expression. */
6994 expr = cp_parser_constant_expression (parser,
6995 /*allow_non_constant_p=*/false,
6998 ellipsis = cp_lexer_peek_token (parser->lexer);
6999 if (ellipsis->type == CPP_ELLIPSIS)
7001 /* Consume the `...' token. */
7002 cp_lexer_consume_token (parser->lexer);
7004 cp_parser_constant_expression (parser,
7005 /*allow_non_constant_p=*/false,
7007 /* We don't need to emit warnings here, as the common code
7008 will do this for us. */
7011 expr_hi = NULL_TREE;
7013 if (parser->in_switch_statement_p)
7014 finish_case_label (expr, expr_hi);
7016 error ("%Hcase label %qE not within a switch statement",
7017 &token->location, expr);
7022 /* Consume the `default' token. */
7023 cp_lexer_consume_token (parser->lexer);
7025 if (parser->in_switch_statement_p)
7026 finish_case_label (NULL_TREE, NULL_TREE);
7028 error ("%Hcase label not within a switch statement", &token->location);
7032 /* Anything else must be an ordinary label. */
7033 finish_label_stmt (cp_parser_identifier (parser));
7037 /* Require the `:' token. */
7038 cp_parser_require (parser, CPP_COLON, "%<:%>");
7041 /* Parse an expression-statement.
7043 expression-statement:
7046 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7047 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7048 indicates whether this expression-statement is part of an
7049 expression statement. */
7052 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7054 tree statement = NULL_TREE;
7056 /* If the next token is a ';', then there is no expression
7058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7059 statement = cp_parser_expression (parser, /*cast_p=*/false);
7061 /* Consume the final `;'. */
7062 cp_parser_consume_semicolon_at_end_of_statement (parser);
7064 if (in_statement_expr
7065 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7066 /* This is the final expression statement of a statement
7068 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7070 statement = finish_expr_stmt (statement);
7077 /* Parse a compound-statement.
7080 { statement-seq [opt] }
7085 { label-declaration-seq [opt] statement-seq [opt] }
7087 label-declaration-seq:
7089 label-declaration-seq label-declaration
7091 Returns a tree representing the statement. */
7094 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7099 /* Consume the `{'. */
7100 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7101 return error_mark_node;
7102 /* Begin the compound-statement. */
7103 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7104 /* If the next keyword is `__label__' we have a label declaration. */
7105 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7106 cp_parser_label_declaration (parser);
7107 /* Parse an (optional) statement-seq. */
7108 cp_parser_statement_seq_opt (parser, in_statement_expr);
7109 /* Finish the compound-statement. */
7110 finish_compound_stmt (compound_stmt);
7111 /* Consume the `}'. */
7112 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7114 return compound_stmt;
7117 /* Parse an (optional) statement-seq.
7121 statement-seq [opt] statement */
7124 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7126 /* Scan statements until there aren't any more. */
7129 cp_token *token = cp_lexer_peek_token (parser->lexer);
7131 /* If we're looking at a `}', then we've run out of statements. */
7132 if (token->type == CPP_CLOSE_BRACE
7133 || token->type == CPP_EOF
7134 || token->type == CPP_PRAGMA_EOL)
7137 /* If we are in a compound statement and find 'else' then
7138 something went wrong. */
7139 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7141 if (parser->in_statement & IN_IF_STMT)
7145 token = cp_lexer_consume_token (parser->lexer);
7146 error ("%H%<else%> without a previous %<if%>", &token->location);
7150 /* Parse the statement. */
7151 cp_parser_statement (parser, in_statement_expr, true, NULL);
7155 /* Parse a selection-statement.
7157 selection-statement:
7158 if ( condition ) statement
7159 if ( condition ) statement else statement
7160 switch ( condition ) statement
7162 Returns the new IF_STMT or SWITCH_STMT.
7164 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7165 is a (possibly labeled) if statement which is not enclosed in
7166 braces and has an else clause. This is used to implement
7170 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7178 /* Peek at the next token. */
7179 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7181 /* See what kind of keyword it is. */
7182 keyword = token->keyword;
7191 /* Look for the `('. */
7192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7194 cp_parser_skip_to_end_of_statement (parser);
7195 return error_mark_node;
7198 /* Begin the selection-statement. */
7199 if (keyword == RID_IF)
7200 statement = begin_if_stmt ();
7202 statement = begin_switch_stmt ();
7204 /* Parse the condition. */
7205 condition = cp_parser_condition (parser);
7206 /* Look for the `)'. */
7207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7208 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7209 /*consume_paren=*/true);
7211 if (keyword == RID_IF)
7214 unsigned char in_statement;
7216 /* Add the condition. */
7217 finish_if_stmt_cond (condition, statement);
7219 /* Parse the then-clause. */
7220 in_statement = parser->in_statement;
7221 parser->in_statement |= IN_IF_STMT;
7222 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7225 add_stmt (build_empty_stmt ());
7226 cp_lexer_consume_token (parser->lexer);
7227 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7228 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7229 "empty body in an %<if%> statement");
7233 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7234 parser->in_statement = in_statement;
7236 finish_then_clause (statement);
7238 /* If the next token is `else', parse the else-clause. */
7239 if (cp_lexer_next_token_is_keyword (parser->lexer,
7242 /* Consume the `else' keyword. */
7243 cp_lexer_consume_token (parser->lexer);
7244 begin_else_clause (statement);
7245 /* Parse the else-clause. */
7246 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7248 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7249 OPT_Wempty_body, "suggest braces around "
7250 "empty body in an %<else%> statement");
7251 add_stmt (build_empty_stmt ());
7252 cp_lexer_consume_token (parser->lexer);
7255 cp_parser_implicitly_scoped_statement (parser, NULL);
7257 finish_else_clause (statement);
7259 /* If we are currently parsing a then-clause, then
7260 IF_P will not be NULL. We set it to true to
7261 indicate that this if statement has an else clause.
7262 This may trigger the Wparentheses warning below
7263 when we get back up to the parent if statement. */
7269 /* This if statement does not have an else clause. If
7270 NESTED_IF is true, then the then-clause is an if
7271 statement which does have an else clause. We warn
7272 about the potential ambiguity. */
7274 warning (OPT_Wparentheses,
7275 ("%Hsuggest explicit braces "
7276 "to avoid ambiguous %<else%>"),
7277 EXPR_LOCUS (statement));
7280 /* Now we're all done with the if-statement. */
7281 finish_if_stmt (statement);
7285 bool in_switch_statement_p;
7286 unsigned char in_statement;
7288 /* Add the condition. */
7289 finish_switch_cond (condition, statement);
7291 /* Parse the body of the switch-statement. */
7292 in_switch_statement_p = parser->in_switch_statement_p;
7293 in_statement = parser->in_statement;
7294 parser->in_switch_statement_p = true;
7295 parser->in_statement |= IN_SWITCH_STMT;
7296 cp_parser_implicitly_scoped_statement (parser, NULL);
7297 parser->in_switch_statement_p = in_switch_statement_p;
7298 parser->in_statement = in_statement;
7300 /* Now we're all done with the switch-statement. */
7301 finish_switch_stmt (statement);
7309 cp_parser_error (parser, "expected selection-statement");
7310 return error_mark_node;
7314 /* Parse a condition.
7318 type-specifier-seq declarator = initializer-clause
7319 type-specifier-seq declarator braced-init-list
7324 type-specifier-seq declarator asm-specification [opt]
7325 attributes [opt] = assignment-expression
7327 Returns the expression that should be tested. */
7330 cp_parser_condition (cp_parser* parser)
7332 cp_decl_specifier_seq type_specifiers;
7333 const char *saved_message;
7335 /* Try the declaration first. */
7336 cp_parser_parse_tentatively (parser);
7337 /* New types are not allowed in the type-specifier-seq for a
7339 saved_message = parser->type_definition_forbidden_message;
7340 parser->type_definition_forbidden_message
7341 = "types may not be defined in conditions";
7342 /* Parse the type-specifier-seq. */
7343 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7345 /* Restore the saved message. */
7346 parser->type_definition_forbidden_message = saved_message;
7347 /* If all is well, we might be looking at a declaration. */
7348 if (!cp_parser_error_occurred (parser))
7351 tree asm_specification;
7353 cp_declarator *declarator;
7354 tree initializer = NULL_TREE;
7356 /* Parse the declarator. */
7357 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7358 /*ctor_dtor_or_conv_p=*/NULL,
7359 /*parenthesized_p=*/NULL,
7360 /*member_p=*/false);
7361 /* Parse the attributes. */
7362 attributes = cp_parser_attributes_opt (parser);
7363 /* Parse the asm-specification. */
7364 asm_specification = cp_parser_asm_specification_opt (parser);
7365 /* If the next token is not an `=' or '{', then we might still be
7366 looking at an expression. For example:
7370 looks like a decl-specifier-seq and a declarator -- but then
7371 there is no `=', so this is an expression. */
7372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7373 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7374 cp_parser_simulate_error (parser);
7376 /* If we did see an `=' or '{', then we are looking at a declaration
7378 if (cp_parser_parse_definitely (parser))
7381 bool non_constant_p;
7382 bool flags = LOOKUP_ONLYCONVERTING;
7384 /* Create the declaration. */
7385 decl = start_decl (declarator, &type_specifiers,
7386 /*initialized_p=*/true,
7387 attributes, /*prefix_attributes=*/NULL_TREE,
7390 /* Parse the initializer. */
7391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7393 initializer = cp_parser_braced_list (parser, &non_constant_p);
7394 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7399 /* Consume the `='. */
7400 cp_lexer_consume_token (parser->lexer);
7401 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7403 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7404 maybe_warn_cpp0x ("extended initializer lists");
7406 if (!non_constant_p)
7407 initializer = fold_non_dependent_expr (initializer);
7409 /* Process the initializer. */
7410 cp_finish_decl (decl,
7411 initializer, !non_constant_p,
7416 pop_scope (pushed_scope);
7418 return convert_from_reference (decl);
7421 /* If we didn't even get past the declarator successfully, we are
7422 definitely not looking at a declaration. */
7424 cp_parser_abort_tentative_parse (parser);
7426 /* Otherwise, we are looking at an expression. */
7427 return cp_parser_expression (parser, /*cast_p=*/false);
7430 /* Parse an iteration-statement.
7432 iteration-statement:
7433 while ( condition ) statement
7434 do statement while ( expression ) ;
7435 for ( for-init-statement condition [opt] ; expression [opt] )
7438 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7441 cp_parser_iteration_statement (cp_parser* parser)
7446 unsigned char in_statement;
7448 /* Peek at the next token. */
7449 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7451 return error_mark_node;
7453 /* Remember whether or not we are already within an iteration
7455 in_statement = parser->in_statement;
7457 /* See what kind of keyword it is. */
7458 keyword = token->keyword;
7465 /* Begin the while-statement. */
7466 statement = begin_while_stmt ();
7467 /* Look for the `('. */
7468 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7469 /* Parse the condition. */
7470 condition = cp_parser_condition (parser);
7471 finish_while_stmt_cond (condition, statement);
7472 /* Look for the `)'. */
7473 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7474 /* Parse the dependent statement. */
7475 parser->in_statement = IN_ITERATION_STMT;
7476 cp_parser_already_scoped_statement (parser);
7477 parser->in_statement = in_statement;
7478 /* We're done with the while-statement. */
7479 finish_while_stmt (statement);
7487 /* Begin the do-statement. */
7488 statement = begin_do_stmt ();
7489 /* Parse the body of the do-statement. */
7490 parser->in_statement = IN_ITERATION_STMT;
7491 cp_parser_implicitly_scoped_statement (parser, NULL);
7492 parser->in_statement = in_statement;
7493 finish_do_body (statement);
7494 /* Look for the `while' keyword. */
7495 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7496 /* Look for the `('. */
7497 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7498 /* Parse the expression. */
7499 expression = cp_parser_expression (parser, /*cast_p=*/false);
7500 /* We're done with the do-statement. */
7501 finish_do_stmt (expression, statement);
7502 /* Look for the `)'. */
7503 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7504 /* Look for the `;'. */
7505 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7511 tree condition = NULL_TREE;
7512 tree expression = NULL_TREE;
7514 /* Begin the for-statement. */
7515 statement = begin_for_stmt ();
7516 /* Look for the `('. */
7517 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7518 /* Parse the initialization. */
7519 cp_parser_for_init_statement (parser);
7520 finish_for_init_stmt (statement);
7522 /* If there's a condition, process it. */
7523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7524 condition = cp_parser_condition (parser);
7525 finish_for_cond (condition, statement);
7526 /* Look for the `;'. */
7527 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7529 /* If there's an expression, process it. */
7530 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7531 expression = cp_parser_expression (parser, /*cast_p=*/false);
7532 finish_for_expr (expression, statement);
7533 /* Look for the `)'. */
7534 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7536 /* Parse the body of the for-statement. */
7537 parser->in_statement = IN_ITERATION_STMT;
7538 cp_parser_already_scoped_statement (parser);
7539 parser->in_statement = in_statement;
7541 /* We're done with the for-statement. */
7542 finish_for_stmt (statement);
7547 cp_parser_error (parser, "expected iteration-statement");
7548 statement = error_mark_node;
7555 /* Parse a for-init-statement.
7558 expression-statement
7559 simple-declaration */
7562 cp_parser_for_init_statement (cp_parser* parser)
7564 /* If the next token is a `;', then we have an empty
7565 expression-statement. Grammatically, this is also a
7566 simple-declaration, but an invalid one, because it does not
7567 declare anything. Therefore, if we did not handle this case
7568 specially, we would issue an error message about an invalid
7570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7572 /* We're going to speculatively look for a declaration, falling back
7573 to an expression, if necessary. */
7574 cp_parser_parse_tentatively (parser);
7575 /* Parse the declaration. */
7576 cp_parser_simple_declaration (parser,
7577 /*function_definition_allowed_p=*/false);
7578 /* If the tentative parse failed, then we shall need to look for an
7579 expression-statement. */
7580 if (cp_parser_parse_definitely (parser))
7584 cp_parser_expression_statement (parser, false);
7587 /* Parse a jump-statement.
7592 return expression [opt] ;
7593 return braced-init-list ;
7601 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7604 cp_parser_jump_statement (cp_parser* parser)
7606 tree statement = error_mark_node;
7609 unsigned char in_statement;
7611 /* Peek at the next token. */
7612 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7614 return error_mark_node;
7616 /* See what kind of keyword it is. */
7617 keyword = token->keyword;
7621 in_statement = parser->in_statement & ~IN_IF_STMT;
7622 switch (in_statement)
7625 error ("%Hbreak statement not within loop or switch", &token->location);
7628 gcc_assert ((in_statement & IN_SWITCH_STMT)
7629 || in_statement == IN_ITERATION_STMT);
7630 statement = finish_break_stmt ();
7633 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7636 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7639 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7643 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7646 error ("%Hcontinue statement not within a loop", &token->location);
7648 case IN_ITERATION_STMT:
7650 statement = finish_continue_stmt ();
7653 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7658 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7664 bool expr_non_constant_p;
7666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7668 maybe_warn_cpp0x ("extended initializer lists");
7669 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7671 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7672 expr = cp_parser_expression (parser, /*cast_p=*/false);
7674 /* If the next token is a `;', then there is no
7677 /* Build the return-statement. */
7678 statement = finish_return_stmt (expr);
7679 /* Look for the final `;'. */
7680 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7685 /* Create the goto-statement. */
7686 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7688 /* Issue a warning about this use of a GNU extension. */
7689 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7690 /* Consume the '*' token. */
7691 cp_lexer_consume_token (parser->lexer);
7692 /* Parse the dependent expression. */
7693 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7696 finish_goto_stmt (cp_parser_identifier (parser));
7697 /* Look for the final `;'. */
7698 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7702 cp_parser_error (parser, "expected jump-statement");
7709 /* Parse a declaration-statement.
7711 declaration-statement:
7712 block-declaration */
7715 cp_parser_declaration_statement (cp_parser* parser)
7719 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7720 p = obstack_alloc (&declarator_obstack, 0);
7722 /* Parse the block-declaration. */
7723 cp_parser_block_declaration (parser, /*statement_p=*/true);
7725 /* Free any declarators allocated. */
7726 obstack_free (&declarator_obstack, p);
7728 /* Finish off the statement. */
7732 /* Some dependent statements (like `if (cond) statement'), are
7733 implicitly in their own scope. In other words, if the statement is
7734 a single statement (as opposed to a compound-statement), it is
7735 none-the-less treated as if it were enclosed in braces. Any
7736 declarations appearing in the dependent statement are out of scope
7737 after control passes that point. This function parses a statement,
7738 but ensures that is in its own scope, even if it is not a
7741 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7742 is a (possibly labeled) if statement which is not enclosed in
7743 braces and has an else clause. This is used to implement
7746 Returns the new statement. */
7749 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7756 /* Mark if () ; with a special NOP_EXPR. */
7757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7759 cp_lexer_consume_token (parser->lexer);
7760 statement = add_stmt (build_empty_stmt ());
7762 /* if a compound is opened, we simply parse the statement directly. */
7763 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7764 statement = cp_parser_compound_statement (parser, NULL, false);
7765 /* If the token is not a `{', then we must take special action. */
7768 /* Create a compound-statement. */
7769 statement = begin_compound_stmt (0);
7770 /* Parse the dependent-statement. */
7771 cp_parser_statement (parser, NULL_TREE, false, if_p);
7772 /* Finish the dummy compound-statement. */
7773 finish_compound_stmt (statement);
7776 /* Return the statement. */
7780 /* For some dependent statements (like `while (cond) statement'), we
7781 have already created a scope. Therefore, even if the dependent
7782 statement is a compound-statement, we do not want to create another
7786 cp_parser_already_scoped_statement (cp_parser* parser)
7788 /* If the token is a `{', then we must take special action. */
7789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7790 cp_parser_statement (parser, NULL_TREE, false, NULL);
7793 /* Avoid calling cp_parser_compound_statement, so that we
7794 don't create a new scope. Do everything else by hand. */
7795 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7796 cp_parser_statement_seq_opt (parser, NULL_TREE);
7797 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7801 /* Declarations [gram.dcl.dcl] */
7803 /* Parse an optional declaration-sequence.
7807 declaration-seq declaration */
7810 cp_parser_declaration_seq_opt (cp_parser* parser)
7816 token = cp_lexer_peek_token (parser->lexer);
7818 if (token->type == CPP_CLOSE_BRACE
7819 || token->type == CPP_EOF
7820 || token->type == CPP_PRAGMA_EOL)
7823 if (token->type == CPP_SEMICOLON)
7825 /* A declaration consisting of a single semicolon is
7826 invalid. Allow it unless we're being pedantic. */
7827 cp_lexer_consume_token (parser->lexer);
7828 if (!in_system_header)
7829 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7833 /* If we're entering or exiting a region that's implicitly
7834 extern "C", modify the lang context appropriately. */
7835 if (!parser->implicit_extern_c && token->implicit_extern_c)
7837 push_lang_context (lang_name_c);
7838 parser->implicit_extern_c = true;
7840 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7842 pop_lang_context ();
7843 parser->implicit_extern_c = false;
7846 if (token->type == CPP_PRAGMA)
7848 /* A top-level declaration can consist solely of a #pragma.
7849 A nested declaration cannot, so this is done here and not
7850 in cp_parser_declaration. (A #pragma at block scope is
7851 handled in cp_parser_statement.) */
7852 cp_parser_pragma (parser, pragma_external);
7856 /* Parse the declaration itself. */
7857 cp_parser_declaration (parser);
7861 /* Parse a declaration.
7866 template-declaration
7867 explicit-instantiation
7868 explicit-specialization
7869 linkage-specification
7870 namespace-definition
7875 __extension__ declaration */
7878 cp_parser_declaration (cp_parser* parser)
7885 /* Check for the `__extension__' keyword. */
7886 if (cp_parser_extension_opt (parser, &saved_pedantic))
7888 /* Parse the qualified declaration. */
7889 cp_parser_declaration (parser);
7890 /* Restore the PEDANTIC flag. */
7891 pedantic = saved_pedantic;
7896 /* Try to figure out what kind of declaration is present. */
7897 token1 = *cp_lexer_peek_token (parser->lexer);
7899 if (token1.type != CPP_EOF)
7900 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7903 token2.type = CPP_EOF;
7904 token2.keyword = RID_MAX;
7907 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7908 p = obstack_alloc (&declarator_obstack, 0);
7910 /* If the next token is `extern' and the following token is a string
7911 literal, then we have a linkage specification. */
7912 if (token1.keyword == RID_EXTERN
7913 && cp_parser_is_string_literal (&token2))
7914 cp_parser_linkage_specification (parser);
7915 /* If the next token is `template', then we have either a template
7916 declaration, an explicit instantiation, or an explicit
7918 else if (token1.keyword == RID_TEMPLATE)
7920 /* `template <>' indicates a template specialization. */
7921 if (token2.type == CPP_LESS
7922 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7923 cp_parser_explicit_specialization (parser);
7924 /* `template <' indicates a template declaration. */
7925 else if (token2.type == CPP_LESS)
7926 cp_parser_template_declaration (parser, /*member_p=*/false);
7927 /* Anything else must be an explicit instantiation. */
7929 cp_parser_explicit_instantiation (parser);
7931 /* If the next token is `export', then we have a template
7933 else if (token1.keyword == RID_EXPORT)
7934 cp_parser_template_declaration (parser, /*member_p=*/false);
7935 /* If the next token is `extern', 'static' or 'inline' and the one
7936 after that is `template', we have a GNU extended explicit
7937 instantiation directive. */
7938 else if (cp_parser_allow_gnu_extensions_p (parser)
7939 && (token1.keyword == RID_EXTERN
7940 || token1.keyword == RID_STATIC
7941 || token1.keyword == RID_INLINE)
7942 && token2.keyword == RID_TEMPLATE)
7943 cp_parser_explicit_instantiation (parser);
7944 /* If the next token is `namespace', check for a named or unnamed
7945 namespace definition. */
7946 else if (token1.keyword == RID_NAMESPACE
7947 && (/* A named namespace definition. */
7948 (token2.type == CPP_NAME
7949 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7951 /* An unnamed namespace definition. */
7952 || token2.type == CPP_OPEN_BRACE
7953 || token2.keyword == RID_ATTRIBUTE))
7954 cp_parser_namespace_definition (parser);
7955 /* An inline (associated) namespace definition. */
7956 else if (token1.keyword == RID_INLINE
7957 && token2.keyword == RID_NAMESPACE)
7958 cp_parser_namespace_definition (parser);
7959 /* Objective-C++ declaration/definition. */
7960 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7961 cp_parser_objc_declaration (parser);
7962 /* We must have either a block declaration or a function
7965 /* Try to parse a block-declaration, or a function-definition. */
7966 cp_parser_block_declaration (parser, /*statement_p=*/false);
7968 /* Free any declarators allocated. */
7969 obstack_free (&declarator_obstack, p);
7972 /* Parse a block-declaration.
7977 namespace-alias-definition
7984 __extension__ block-declaration
7989 static_assert-declaration
7991 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7992 part of a declaration-statement. */
7995 cp_parser_block_declaration (cp_parser *parser,
8001 /* Check for the `__extension__' keyword. */
8002 if (cp_parser_extension_opt (parser, &saved_pedantic))
8004 /* Parse the qualified declaration. */
8005 cp_parser_block_declaration (parser, statement_p);
8006 /* Restore the PEDANTIC flag. */
8007 pedantic = saved_pedantic;
8012 /* Peek at the next token to figure out which kind of declaration is
8014 token1 = cp_lexer_peek_token (parser->lexer);
8016 /* If the next keyword is `asm', we have an asm-definition. */
8017 if (token1->keyword == RID_ASM)
8020 cp_parser_commit_to_tentative_parse (parser);
8021 cp_parser_asm_definition (parser);
8023 /* If the next keyword is `namespace', we have a
8024 namespace-alias-definition. */
8025 else if (token1->keyword == RID_NAMESPACE)
8026 cp_parser_namespace_alias_definition (parser);
8027 /* If the next keyword is `using', we have either a
8028 using-declaration or a using-directive. */
8029 else if (token1->keyword == RID_USING)
8034 cp_parser_commit_to_tentative_parse (parser);
8035 /* If the token after `using' is `namespace', then we have a
8037 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8038 if (token2->keyword == RID_NAMESPACE)
8039 cp_parser_using_directive (parser);
8040 /* Otherwise, it's a using-declaration. */
8042 cp_parser_using_declaration (parser,
8043 /*access_declaration_p=*/false);
8045 /* If the next keyword is `__label__' we have a misplaced label
8047 else if (token1->keyword == RID_LABEL)
8049 cp_lexer_consume_token (parser->lexer);
8050 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8051 cp_parser_skip_to_end_of_statement (parser);
8052 /* If the next token is now a `;', consume it. */
8053 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8054 cp_lexer_consume_token (parser->lexer);
8056 /* If the next token is `static_assert' we have a static assertion. */
8057 else if (token1->keyword == RID_STATIC_ASSERT)
8058 cp_parser_static_assert (parser, /*member_p=*/false);
8059 /* Anything else must be a simple-declaration. */
8061 cp_parser_simple_declaration (parser, !statement_p);
8064 /* Parse a simple-declaration.
8067 decl-specifier-seq [opt] init-declarator-list [opt] ;
8069 init-declarator-list:
8071 init-declarator-list , init-declarator
8073 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8074 function-definition as a simple-declaration. */
8077 cp_parser_simple_declaration (cp_parser* parser,
8078 bool function_definition_allowed_p)
8080 cp_decl_specifier_seq decl_specifiers;
8081 int declares_class_or_enum;
8082 bool saw_declarator;
8084 /* Defer access checks until we know what is being declared; the
8085 checks for names appearing in the decl-specifier-seq should be
8086 done as if we were in the scope of the thing being declared. */
8087 push_deferring_access_checks (dk_deferred);
8089 /* Parse the decl-specifier-seq. We have to keep track of whether
8090 or not the decl-specifier-seq declares a named class or
8091 enumeration type, since that is the only case in which the
8092 init-declarator-list is allowed to be empty.
8096 In a simple-declaration, the optional init-declarator-list can be
8097 omitted only when declaring a class or enumeration, that is when
8098 the decl-specifier-seq contains either a class-specifier, an
8099 elaborated-type-specifier, or an enum-specifier. */
8100 cp_parser_decl_specifier_seq (parser,
8101 CP_PARSER_FLAGS_OPTIONAL,
8103 &declares_class_or_enum);
8104 /* We no longer need to defer access checks. */
8105 stop_deferring_access_checks ();
8107 /* In a block scope, a valid declaration must always have a
8108 decl-specifier-seq. By not trying to parse declarators, we can
8109 resolve the declaration/expression ambiguity more quickly. */
8110 if (!function_definition_allowed_p
8111 && !decl_specifiers.any_specifiers_p)
8113 cp_parser_error (parser, "expected declaration");
8117 /* If the next two tokens are both identifiers, the code is
8118 erroneous. The usual cause of this situation is code like:
8122 where "T" should name a type -- but does not. */
8123 if (!decl_specifiers.type
8124 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8126 /* If parsing tentatively, we should commit; we really are
8127 looking at a declaration. */
8128 cp_parser_commit_to_tentative_parse (parser);
8133 /* If we have seen at least one decl-specifier, and the next token
8134 is not a parenthesis, then we must be looking at a declaration.
8135 (After "int (" we might be looking at a functional cast.) */
8136 if (decl_specifiers.any_specifiers_p
8137 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8138 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8139 && !cp_parser_error_occurred (parser))
8140 cp_parser_commit_to_tentative_parse (parser);
8142 /* Keep going until we hit the `;' at the end of the simple
8144 saw_declarator = false;
8145 while (cp_lexer_next_token_is_not (parser->lexer,
8149 bool function_definition_p;
8154 /* If we are processing next declarator, coma is expected */
8155 token = cp_lexer_peek_token (parser->lexer);
8156 gcc_assert (token->type == CPP_COMMA);
8157 cp_lexer_consume_token (parser->lexer);
8160 saw_declarator = true;
8162 /* Parse the init-declarator. */
8163 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8165 function_definition_allowed_p,
8167 declares_class_or_enum,
8168 &function_definition_p);
8169 /* If an error occurred while parsing tentatively, exit quickly.
8170 (That usually happens when in the body of a function; each
8171 statement is treated as a declaration-statement until proven
8173 if (cp_parser_error_occurred (parser))
8175 /* Handle function definitions specially. */
8176 if (function_definition_p)
8178 /* If the next token is a `,', then we are probably
8179 processing something like:
8183 which is erroneous. */
8184 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8186 cp_token *token = cp_lexer_peek_token (parser->lexer);
8187 error ("%Hmixing declarations and function-definitions is forbidden",
8190 /* Otherwise, we're done with the list of declarators. */
8193 pop_deferring_access_checks ();
8197 /* The next token should be either a `,' or a `;'. */
8198 token = cp_lexer_peek_token (parser->lexer);
8199 /* If it's a `,', there are more declarators to come. */
8200 if (token->type == CPP_COMMA)
8201 /* will be consumed next time around */;
8202 /* If it's a `;', we are done. */
8203 else if (token->type == CPP_SEMICOLON)
8205 /* Anything else is an error. */
8208 /* If we have already issued an error message we don't need
8209 to issue another one. */
8210 if (decl != error_mark_node
8211 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8212 cp_parser_error (parser, "expected %<,%> or %<;%>");
8213 /* Skip tokens until we reach the end of the statement. */
8214 cp_parser_skip_to_end_of_statement (parser);
8215 /* If the next token is now a `;', consume it. */
8216 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8217 cp_lexer_consume_token (parser->lexer);
8220 /* After the first time around, a function-definition is not
8221 allowed -- even if it was OK at first. For example:
8226 function_definition_allowed_p = false;
8229 /* Issue an error message if no declarators are present, and the
8230 decl-specifier-seq does not itself declare a class or
8232 if (!saw_declarator)
8234 if (cp_parser_declares_only_class_p (parser))
8235 shadow_tag (&decl_specifiers);
8236 /* Perform any deferred access checks. */
8237 perform_deferred_access_checks ();
8240 /* Consume the `;'. */
8241 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8244 pop_deferring_access_checks ();
8247 /* Parse a decl-specifier-seq.
8250 decl-specifier-seq [opt] decl-specifier
8253 storage-class-specifier
8264 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8266 The parser flags FLAGS is used to control type-specifier parsing.
8268 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8271 1: one of the decl-specifiers is an elaborated-type-specifier
8272 (i.e., a type declaration)
8273 2: one of the decl-specifiers is an enum-specifier or a
8274 class-specifier (i.e., a type definition)
8279 cp_parser_decl_specifier_seq (cp_parser* parser,
8280 cp_parser_flags flags,
8281 cp_decl_specifier_seq *decl_specs,
8282 int* declares_class_or_enum)
8284 bool constructor_possible_p = !parser->in_declarator_p;
8285 cp_token *start_token = NULL;
8287 /* Clear DECL_SPECS. */
8288 clear_decl_specs (decl_specs);
8290 /* Assume no class or enumeration type is declared. */
8291 *declares_class_or_enum = 0;
8293 /* Keep reading specifiers until there are no more to read. */
8297 bool found_decl_spec;
8300 /* Peek at the next token. */
8301 token = cp_lexer_peek_token (parser->lexer);
8303 /* Save the first token of the decl spec list for error
8306 start_token = token;
8307 /* Handle attributes. */
8308 if (token->keyword == RID_ATTRIBUTE)
8310 /* Parse the attributes. */
8311 decl_specs->attributes
8312 = chainon (decl_specs->attributes,
8313 cp_parser_attributes_opt (parser));
8316 /* Assume we will find a decl-specifier keyword. */
8317 found_decl_spec = true;
8318 /* If the next token is an appropriate keyword, we can simply
8319 add it to the list. */
8320 switch (token->keyword)
8325 if (!at_class_scope_p ())
8327 error ("%H%<friend%> used outside of class", &token->location);
8328 cp_lexer_purge_token (parser->lexer);
8332 ++decl_specs->specs[(int) ds_friend];
8333 /* Consume the token. */
8334 cp_lexer_consume_token (parser->lexer);
8338 /* function-specifier:
8345 cp_parser_function_specifier_opt (parser, decl_specs);
8351 ++decl_specs->specs[(int) ds_typedef];
8352 /* Consume the token. */
8353 cp_lexer_consume_token (parser->lexer);
8354 /* A constructor declarator cannot appear in a typedef. */
8355 constructor_possible_p = false;
8356 /* The "typedef" keyword can only occur in a declaration; we
8357 may as well commit at this point. */
8358 cp_parser_commit_to_tentative_parse (parser);
8360 if (decl_specs->storage_class != sc_none)
8361 decl_specs->conflicting_specifiers_p = true;
8364 /* storage-class-specifier:
8374 if (cxx_dialect == cxx98)
8376 /* Consume the token. */
8377 cp_lexer_consume_token (parser->lexer);
8379 /* Complain about `auto' as a storage specifier, if
8380 we're complaining about C++0x compatibility. */
8383 "%H%<auto%> will change meaning in C++0x; please remove it",
8386 /* Set the storage class anyway. */
8387 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8391 /* C++0x auto type-specifier. */
8392 found_decl_spec = false;
8399 /* Consume the token. */
8400 cp_lexer_consume_token (parser->lexer);
8401 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8405 /* Consume the token. */
8406 cp_lexer_consume_token (parser->lexer);
8407 ++decl_specs->specs[(int) ds_thread];
8411 /* We did not yet find a decl-specifier yet. */
8412 found_decl_spec = false;
8416 /* Constructors are a special case. The `S' in `S()' is not a
8417 decl-specifier; it is the beginning of the declarator. */
8420 && constructor_possible_p
8421 && (cp_parser_constructor_declarator_p
8422 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8424 /* If we don't have a DECL_SPEC yet, then we must be looking at
8425 a type-specifier. */
8426 if (!found_decl_spec && !constructor_p)
8428 int decl_spec_declares_class_or_enum;
8429 bool is_cv_qualifier;
8433 = cp_parser_type_specifier (parser, flags,
8435 /*is_declaration=*/true,
8436 &decl_spec_declares_class_or_enum,
8438 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8440 /* If this type-specifier referenced a user-defined type
8441 (a typedef, class-name, etc.), then we can't allow any
8442 more such type-specifiers henceforth.
8446 The longest sequence of decl-specifiers that could
8447 possibly be a type name is taken as the
8448 decl-specifier-seq of a declaration. The sequence shall
8449 be self-consistent as described below.
8453 As a general rule, at most one type-specifier is allowed
8454 in the complete decl-specifier-seq of a declaration. The
8455 only exceptions are the following:
8457 -- const or volatile can be combined with any other
8460 -- signed or unsigned can be combined with char, long,
8468 void g (const int Pc);
8470 Here, Pc is *not* part of the decl-specifier seq; it's
8471 the declarator. Therefore, once we see a type-specifier
8472 (other than a cv-qualifier), we forbid any additional
8473 user-defined types. We *do* still allow things like `int
8474 int' to be considered a decl-specifier-seq, and issue the
8475 error message later. */
8476 if (type_spec && !is_cv_qualifier)
8477 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8478 /* A constructor declarator cannot follow a type-specifier. */
8481 constructor_possible_p = false;
8482 found_decl_spec = true;
8486 /* If we still do not have a DECL_SPEC, then there are no more
8488 if (!found_decl_spec)
8491 decl_specs->any_specifiers_p = true;
8492 /* After we see one decl-specifier, further decl-specifiers are
8494 flags |= CP_PARSER_FLAGS_OPTIONAL;
8497 cp_parser_check_decl_spec (decl_specs, start_token->location);
8499 /* Don't allow a friend specifier with a class definition. */
8500 if (decl_specs->specs[(int) ds_friend] != 0
8501 && (*declares_class_or_enum & 2))
8502 error ("%Hclass definition may not be declared a friend",
8503 &start_token->location);
8506 /* Parse an (optional) storage-class-specifier.
8508 storage-class-specifier:
8517 storage-class-specifier:
8520 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8523 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8525 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8528 if (cxx_dialect != cxx98)
8530 /* Fall through for C++98. */
8537 /* Consume the token. */
8538 return cp_lexer_consume_token (parser->lexer)->u.value;
8545 /* Parse an (optional) function-specifier.
8552 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8553 Updates DECL_SPECS, if it is non-NULL. */
8556 cp_parser_function_specifier_opt (cp_parser* parser,
8557 cp_decl_specifier_seq *decl_specs)
8559 cp_token *token = cp_lexer_peek_token (parser->lexer);
8560 switch (token->keyword)
8564 ++decl_specs->specs[(int) ds_inline];
8568 /* 14.5.2.3 [temp.mem]
8570 A member function template shall not be virtual. */
8571 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8572 error ("%Htemplates may not be %<virtual%>", &token->location);
8573 else if (decl_specs)
8574 ++decl_specs->specs[(int) ds_virtual];
8579 ++decl_specs->specs[(int) ds_explicit];
8586 /* Consume the token. */
8587 return cp_lexer_consume_token (parser->lexer)->u.value;
8590 /* Parse a linkage-specification.
8592 linkage-specification:
8593 extern string-literal { declaration-seq [opt] }
8594 extern string-literal declaration */
8597 cp_parser_linkage_specification (cp_parser* parser)
8601 /* Look for the `extern' keyword. */
8602 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8604 /* Look for the string-literal. */
8605 linkage = cp_parser_string_literal (parser, false, false);
8607 /* Transform the literal into an identifier. If the literal is a
8608 wide-character string, or contains embedded NULs, then we can't
8609 handle it as the user wants. */
8610 if (strlen (TREE_STRING_POINTER (linkage))
8611 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8613 cp_parser_error (parser, "invalid linkage-specification");
8614 /* Assume C++ linkage. */
8615 linkage = lang_name_cplusplus;
8618 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8620 /* We're now using the new linkage. */
8621 push_lang_context (linkage);
8623 /* If the next token is a `{', then we're using the first
8625 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8627 /* Consume the `{' token. */
8628 cp_lexer_consume_token (parser->lexer);
8629 /* Parse the declarations. */
8630 cp_parser_declaration_seq_opt (parser);
8631 /* Look for the closing `}'. */
8632 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8634 /* Otherwise, there's just one declaration. */
8637 bool saved_in_unbraced_linkage_specification_p;
8639 saved_in_unbraced_linkage_specification_p
8640 = parser->in_unbraced_linkage_specification_p;
8641 parser->in_unbraced_linkage_specification_p = true;
8642 cp_parser_declaration (parser);
8643 parser->in_unbraced_linkage_specification_p
8644 = saved_in_unbraced_linkage_specification_p;
8647 /* We're done with the linkage-specification. */
8648 pop_lang_context ();
8651 /* Parse a static_assert-declaration.
8653 static_assert-declaration:
8654 static_assert ( constant-expression , string-literal ) ;
8656 If MEMBER_P, this static_assert is a class member. */
8659 cp_parser_static_assert(cp_parser *parser, bool member_p)
8664 location_t saved_loc;
8666 /* Peek at the `static_assert' token so we can keep track of exactly
8667 where the static assertion started. */
8668 token = cp_lexer_peek_token (parser->lexer);
8669 saved_loc = token->location;
8671 /* Look for the `static_assert' keyword. */
8672 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8673 "%<static_assert%>"))
8676 /* We know we are in a static assertion; commit to any tentative
8678 if (cp_parser_parsing_tentatively (parser))
8679 cp_parser_commit_to_tentative_parse (parser);
8681 /* Parse the `(' starting the static assertion condition. */
8682 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8684 /* Parse the constant-expression. */
8686 cp_parser_constant_expression (parser,
8687 /*allow_non_constant_p=*/false,
8688 /*non_constant_p=*/NULL);
8690 /* Parse the separating `,'. */
8691 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8693 /* Parse the string-literal message. */
8694 message = cp_parser_string_literal (parser,
8695 /*translate=*/false,
8698 /* A `)' completes the static assertion. */
8699 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8700 cp_parser_skip_to_closing_parenthesis (parser,
8701 /*recovering=*/true,
8703 /*consume_paren=*/true);
8705 /* A semicolon terminates the declaration. */
8706 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8708 /* Complete the static assertion, which may mean either processing
8709 the static assert now or saving it for template instantiation. */
8710 finish_static_assert (condition, message, saved_loc, member_p);
8713 /* Parse a `decltype' type. Returns the type.
8715 simple-type-specifier:
8716 decltype ( expression ) */
8719 cp_parser_decltype (cp_parser *parser)
8722 bool id_expression_or_member_access_p = false;
8723 const char *saved_message;
8724 bool saved_integral_constant_expression_p;
8725 bool saved_non_integral_constant_expression_p;
8726 cp_token *id_expr_start_token;
8728 /* Look for the `decltype' token. */
8729 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8730 return error_mark_node;
8732 /* Types cannot be defined in a `decltype' expression. Save away the
8734 saved_message = parser->type_definition_forbidden_message;
8736 /* And create the new one. */
8737 parser->type_definition_forbidden_message
8738 = "types may not be defined in %<decltype%> expressions";
8740 /* The restrictions on constant-expressions do not apply inside
8741 decltype expressions. */
8742 saved_integral_constant_expression_p
8743 = parser->integral_constant_expression_p;
8744 saved_non_integral_constant_expression_p
8745 = parser->non_integral_constant_expression_p;
8746 parser->integral_constant_expression_p = false;
8748 /* Do not actually evaluate the expression. */
8751 /* Parse the opening `('. */
8752 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8753 return error_mark_node;
8755 /* First, try parsing an id-expression. */
8756 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8757 cp_parser_parse_tentatively (parser);
8758 expr = cp_parser_id_expression (parser,
8759 /*template_keyword_p=*/false,
8760 /*check_dependency_p=*/true,
8761 /*template_p=*/NULL,
8762 /*declarator_p=*/false,
8763 /*optional_p=*/false);
8765 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8767 bool non_integral_constant_expression_p = false;
8768 tree id_expression = expr;
8770 const char *error_msg;
8772 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8773 /* Lookup the name we got back from the id-expression. */
8774 expr = cp_parser_lookup_name (parser, expr,
8776 /*is_template=*/false,
8777 /*is_namespace=*/false,
8778 /*check_dependency=*/true,
8779 /*ambiguous_decls=*/NULL,
8780 id_expr_start_token->location);
8783 && expr != error_mark_node
8784 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8785 && TREE_CODE (expr) != TYPE_DECL
8786 && (TREE_CODE (expr) != BIT_NOT_EXPR
8787 || !TYPE_P (TREE_OPERAND (expr, 0)))
8788 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8790 /* Complete lookup of the id-expression. */
8791 expr = (finish_id_expression
8792 (id_expression, expr, parser->scope, &idk,
8793 /*integral_constant_expression_p=*/false,
8794 /*allow_non_integral_constant_expression_p=*/true,
8795 &non_integral_constant_expression_p,
8796 /*template_p=*/false,
8798 /*address_p=*/false,
8799 /*template_arg_p=*/false,
8801 id_expr_start_token->location));
8803 if (expr == error_mark_node)
8804 /* We found an id-expression, but it was something that we
8805 should not have found. This is an error, not something
8806 we can recover from, so note that we found an
8807 id-expression and we'll recover as gracefully as
8809 id_expression_or_member_access_p = true;
8813 && expr != error_mark_node
8814 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8815 /* We have an id-expression. */
8816 id_expression_or_member_access_p = true;
8819 if (!id_expression_or_member_access_p)
8821 /* Abort the id-expression parse. */
8822 cp_parser_abort_tentative_parse (parser);
8824 /* Parsing tentatively, again. */
8825 cp_parser_parse_tentatively (parser);
8827 /* Parse a class member access. */
8828 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8830 /*member_access_only_p=*/true);
8833 && expr != error_mark_node
8834 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8835 /* We have an id-expression. */
8836 id_expression_or_member_access_p = true;
8839 if (id_expression_or_member_access_p)
8840 /* We have parsed the complete id-expression or member access. */
8841 cp_parser_parse_definitely (parser);
8844 /* Abort our attempt to parse an id-expression or member access
8846 cp_parser_abort_tentative_parse (parser);
8848 /* Parse a full expression. */
8849 expr = cp_parser_expression (parser, /*cast_p=*/false);
8852 /* Go back to evaluating expressions. */
8855 /* Restore the old message and the integral constant expression
8857 parser->type_definition_forbidden_message = saved_message;
8858 parser->integral_constant_expression_p
8859 = saved_integral_constant_expression_p;
8860 parser->non_integral_constant_expression_p
8861 = saved_non_integral_constant_expression_p;
8863 if (expr == error_mark_node)
8865 /* Skip everything up to the closing `)'. */
8866 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8867 /*consume_paren=*/true);
8868 return error_mark_node;
8871 /* Parse to the closing `)'. */
8872 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8874 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8875 /*consume_paren=*/true);
8876 return error_mark_node;
8879 return finish_decltype_type (expr, id_expression_or_member_access_p);
8882 /* Special member functions [gram.special] */
8884 /* Parse a conversion-function-id.
8886 conversion-function-id:
8887 operator conversion-type-id
8889 Returns an IDENTIFIER_NODE representing the operator. */
8892 cp_parser_conversion_function_id (cp_parser* parser)
8896 tree saved_qualifying_scope;
8897 tree saved_object_scope;
8898 tree pushed_scope = NULL_TREE;
8900 /* Look for the `operator' token. */
8901 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8902 return error_mark_node;
8903 /* When we parse the conversion-type-id, the current scope will be
8904 reset. However, we need that information in able to look up the
8905 conversion function later, so we save it here. */
8906 saved_scope = parser->scope;
8907 saved_qualifying_scope = parser->qualifying_scope;
8908 saved_object_scope = parser->object_scope;
8909 /* We must enter the scope of the class so that the names of
8910 entities declared within the class are available in the
8911 conversion-type-id. For example, consider:
8918 S::operator I() { ... }
8920 In order to see that `I' is a type-name in the definition, we
8921 must be in the scope of `S'. */
8923 pushed_scope = push_scope (saved_scope);
8924 /* Parse the conversion-type-id. */
8925 type = cp_parser_conversion_type_id (parser);
8926 /* Leave the scope of the class, if any. */
8928 pop_scope (pushed_scope);
8929 /* Restore the saved scope. */
8930 parser->scope = saved_scope;
8931 parser->qualifying_scope = saved_qualifying_scope;
8932 parser->object_scope = saved_object_scope;
8933 /* If the TYPE is invalid, indicate failure. */
8934 if (type == error_mark_node)
8935 return error_mark_node;
8936 return mangle_conv_op_name_for_type (type);
8939 /* Parse a conversion-type-id:
8942 type-specifier-seq conversion-declarator [opt]
8944 Returns the TYPE specified. */
8947 cp_parser_conversion_type_id (cp_parser* parser)
8950 cp_decl_specifier_seq type_specifiers;
8951 cp_declarator *declarator;
8952 tree type_specified;
8954 /* Parse the attributes. */
8955 attributes = cp_parser_attributes_opt (parser);
8956 /* Parse the type-specifiers. */
8957 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8959 /* If that didn't work, stop. */
8960 if (type_specifiers.type == error_mark_node)
8961 return error_mark_node;
8962 /* Parse the conversion-declarator. */
8963 declarator = cp_parser_conversion_declarator_opt (parser);
8965 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8966 /*initialized=*/0, &attributes);
8968 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8969 return type_specified;
8972 /* Parse an (optional) conversion-declarator.
8974 conversion-declarator:
8975 ptr-operator conversion-declarator [opt]
8979 static cp_declarator *
8980 cp_parser_conversion_declarator_opt (cp_parser* parser)
8982 enum tree_code code;
8984 cp_cv_quals cv_quals;
8986 /* We don't know if there's a ptr-operator next, or not. */
8987 cp_parser_parse_tentatively (parser);
8988 /* Try the ptr-operator. */
8989 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8990 /* If it worked, look for more conversion-declarators. */
8991 if (cp_parser_parse_definitely (parser))
8993 cp_declarator *declarator;
8995 /* Parse another optional declarator. */
8996 declarator = cp_parser_conversion_declarator_opt (parser);
8998 return cp_parser_make_indirect_declarator
8999 (code, class_type, cv_quals, declarator);
9005 /* Parse an (optional) ctor-initializer.
9008 : mem-initializer-list
9010 Returns TRUE iff the ctor-initializer was actually present. */
9013 cp_parser_ctor_initializer_opt (cp_parser* parser)
9015 /* If the next token is not a `:', then there is no
9016 ctor-initializer. */
9017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9019 /* Do default initialization of any bases and members. */
9020 if (DECL_CONSTRUCTOR_P (current_function_decl))
9021 finish_mem_initializers (NULL_TREE);
9026 /* Consume the `:' token. */
9027 cp_lexer_consume_token (parser->lexer);
9028 /* And the mem-initializer-list. */
9029 cp_parser_mem_initializer_list (parser);
9034 /* Parse a mem-initializer-list.
9036 mem-initializer-list:
9037 mem-initializer ... [opt]
9038 mem-initializer ... [opt] , mem-initializer-list */
9041 cp_parser_mem_initializer_list (cp_parser* parser)
9043 tree mem_initializer_list = NULL_TREE;
9044 cp_token *token = cp_lexer_peek_token (parser->lexer);
9046 /* Let the semantic analysis code know that we are starting the
9047 mem-initializer-list. */
9048 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9049 error ("%Honly constructors take base initializers",
9052 /* Loop through the list. */
9055 tree mem_initializer;
9057 token = cp_lexer_peek_token (parser->lexer);
9058 /* Parse the mem-initializer. */
9059 mem_initializer = cp_parser_mem_initializer (parser);
9060 /* If the next token is a `...', we're expanding member initializers. */
9061 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9063 /* Consume the `...'. */
9064 cp_lexer_consume_token (parser->lexer);
9066 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9067 can be expanded but members cannot. */
9068 if (mem_initializer != error_mark_node
9069 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9071 error ("%Hcannot expand initializer for member %<%D%>",
9072 &token->location, TREE_PURPOSE (mem_initializer));
9073 mem_initializer = error_mark_node;
9076 /* Construct the pack expansion type. */
9077 if (mem_initializer != error_mark_node)
9078 mem_initializer = make_pack_expansion (mem_initializer);
9080 /* Add it to the list, unless it was erroneous. */
9081 if (mem_initializer != error_mark_node)
9083 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9084 mem_initializer_list = mem_initializer;
9086 /* If the next token is not a `,', we're done. */
9087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9089 /* Consume the `,' token. */
9090 cp_lexer_consume_token (parser->lexer);
9093 /* Perform semantic analysis. */
9094 if (DECL_CONSTRUCTOR_P (current_function_decl))
9095 finish_mem_initializers (mem_initializer_list);
9098 /* Parse a mem-initializer.
9101 mem-initializer-id ( expression-list [opt] )
9102 mem-initializer-id braced-init-list
9107 ( expression-list [opt] )
9109 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9110 class) or FIELD_DECL (for a non-static data member) to initialize;
9111 the TREE_VALUE is the expression-list. An empty initialization
9112 list is represented by void_list_node. */
9115 cp_parser_mem_initializer (cp_parser* parser)
9117 tree mem_initializer_id;
9118 tree expression_list;
9120 cp_token *token = cp_lexer_peek_token (parser->lexer);
9122 /* Find out what is being initialized. */
9123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9125 permerror (token->location,
9126 "anachronistic old-style base class initializer");
9127 mem_initializer_id = NULL_TREE;
9130 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9131 member = expand_member_init (mem_initializer_id);
9132 if (member && !DECL_P (member))
9133 in_base_initializer = 1;
9135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9137 bool expr_non_constant_p;
9138 maybe_warn_cpp0x ("extended initializer lists");
9139 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9140 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9141 expression_list = build_tree_list (NULL_TREE, expression_list);
9145 = cp_parser_parenthesized_expression_list (parser, false,
9147 /*allow_expansion_p=*/true,
9148 /*non_constant_p=*/NULL);
9149 if (expression_list == error_mark_node)
9150 return error_mark_node;
9151 if (!expression_list)
9152 expression_list = void_type_node;
9154 in_base_initializer = 0;
9156 return member ? build_tree_list (member, expression_list) : error_mark_node;
9159 /* Parse a mem-initializer-id.
9162 :: [opt] nested-name-specifier [opt] class-name
9165 Returns a TYPE indicating the class to be initializer for the first
9166 production. Returns an IDENTIFIER_NODE indicating the data member
9167 to be initialized for the second production. */
9170 cp_parser_mem_initializer_id (cp_parser* parser)
9172 bool global_scope_p;
9173 bool nested_name_specifier_p;
9174 bool template_p = false;
9177 cp_token *token = cp_lexer_peek_token (parser->lexer);
9179 /* `typename' is not allowed in this context ([temp.res]). */
9180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9182 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9183 "member initializer is implicitly a type)",
9185 cp_lexer_consume_token (parser->lexer);
9187 /* Look for the optional `::' operator. */
9189 = (cp_parser_global_scope_opt (parser,
9190 /*current_scope_valid_p=*/false)
9192 /* Look for the optional nested-name-specifier. The simplest way to
9197 The keyword `typename' is not permitted in a base-specifier or
9198 mem-initializer; in these contexts a qualified name that
9199 depends on a template-parameter is implicitly assumed to be a
9202 is to assume that we have seen the `typename' keyword at this
9204 nested_name_specifier_p
9205 = (cp_parser_nested_name_specifier_opt (parser,
9206 /*typename_keyword_p=*/true,
9207 /*check_dependency_p=*/true,
9209 /*is_declaration=*/true)
9211 if (nested_name_specifier_p)
9212 template_p = cp_parser_optional_template_keyword (parser);
9213 /* If there is a `::' operator or a nested-name-specifier, then we
9214 are definitely looking for a class-name. */
9215 if (global_scope_p || nested_name_specifier_p)
9216 return cp_parser_class_name (parser,
9217 /*typename_keyword_p=*/true,
9218 /*template_keyword_p=*/template_p,
9220 /*check_dependency_p=*/true,
9221 /*class_head_p=*/false,
9222 /*is_declaration=*/true);
9223 /* Otherwise, we could also be looking for an ordinary identifier. */
9224 cp_parser_parse_tentatively (parser);
9225 /* Try a class-name. */
9226 id = cp_parser_class_name (parser,
9227 /*typename_keyword_p=*/true,
9228 /*template_keyword_p=*/false,
9230 /*check_dependency_p=*/true,
9231 /*class_head_p=*/false,
9232 /*is_declaration=*/true);
9233 /* If we found one, we're done. */
9234 if (cp_parser_parse_definitely (parser))
9236 /* Otherwise, look for an ordinary identifier. */
9237 return cp_parser_identifier (parser);
9240 /* Overloading [gram.over] */
9242 /* Parse an operator-function-id.
9244 operator-function-id:
9247 Returns an IDENTIFIER_NODE for the operator which is a
9248 human-readable spelling of the identifier, e.g., `operator +'. */
9251 cp_parser_operator_function_id (cp_parser* parser)
9253 /* Look for the `operator' keyword. */
9254 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9255 return error_mark_node;
9256 /* And then the name of the operator itself. */
9257 return cp_parser_operator (parser);
9260 /* Parse an operator.
9263 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9264 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9265 || ++ -- , ->* -> () []
9272 Returns an IDENTIFIER_NODE for the operator which is a
9273 human-readable spelling of the identifier, e.g., `operator +'. */
9276 cp_parser_operator (cp_parser* parser)
9278 tree id = NULL_TREE;
9281 /* Peek at the next token. */
9282 token = cp_lexer_peek_token (parser->lexer);
9283 /* Figure out which operator we have. */
9284 switch (token->type)
9290 /* The keyword should be either `new' or `delete'. */
9291 if (token->keyword == RID_NEW)
9293 else if (token->keyword == RID_DELETE)
9298 /* Consume the `new' or `delete' token. */
9299 cp_lexer_consume_token (parser->lexer);
9301 /* Peek at the next token. */
9302 token = cp_lexer_peek_token (parser->lexer);
9303 /* If it's a `[' token then this is the array variant of the
9305 if (token->type == CPP_OPEN_SQUARE)
9307 /* Consume the `[' token. */
9308 cp_lexer_consume_token (parser->lexer);
9309 /* Look for the `]' token. */
9310 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9311 id = ansi_opname (op == NEW_EXPR
9312 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9314 /* Otherwise, we have the non-array variant. */
9316 id = ansi_opname (op);
9322 id = ansi_opname (PLUS_EXPR);
9326 id = ansi_opname (MINUS_EXPR);
9330 id = ansi_opname (MULT_EXPR);
9334 id = ansi_opname (TRUNC_DIV_EXPR);
9338 id = ansi_opname (TRUNC_MOD_EXPR);
9342 id = ansi_opname (BIT_XOR_EXPR);
9346 id = ansi_opname (BIT_AND_EXPR);
9350 id = ansi_opname (BIT_IOR_EXPR);
9354 id = ansi_opname (BIT_NOT_EXPR);
9358 id = ansi_opname (TRUTH_NOT_EXPR);
9362 id = ansi_assopname (NOP_EXPR);
9366 id = ansi_opname (LT_EXPR);
9370 id = ansi_opname (GT_EXPR);
9374 id = ansi_assopname (PLUS_EXPR);
9378 id = ansi_assopname (MINUS_EXPR);
9382 id = ansi_assopname (MULT_EXPR);
9386 id = ansi_assopname (TRUNC_DIV_EXPR);
9390 id = ansi_assopname (TRUNC_MOD_EXPR);
9394 id = ansi_assopname (BIT_XOR_EXPR);
9398 id = ansi_assopname (BIT_AND_EXPR);
9402 id = ansi_assopname (BIT_IOR_EXPR);
9406 id = ansi_opname (LSHIFT_EXPR);
9410 id = ansi_opname (RSHIFT_EXPR);
9414 id = ansi_assopname (LSHIFT_EXPR);
9418 id = ansi_assopname (RSHIFT_EXPR);
9422 id = ansi_opname (EQ_EXPR);
9426 id = ansi_opname (NE_EXPR);
9430 id = ansi_opname (LE_EXPR);
9433 case CPP_GREATER_EQ:
9434 id = ansi_opname (GE_EXPR);
9438 id = ansi_opname (TRUTH_ANDIF_EXPR);
9442 id = ansi_opname (TRUTH_ORIF_EXPR);
9446 id = ansi_opname (POSTINCREMENT_EXPR);
9449 case CPP_MINUS_MINUS:
9450 id = ansi_opname (PREDECREMENT_EXPR);
9454 id = ansi_opname (COMPOUND_EXPR);
9457 case CPP_DEREF_STAR:
9458 id = ansi_opname (MEMBER_REF);
9462 id = ansi_opname (COMPONENT_REF);
9465 case CPP_OPEN_PAREN:
9466 /* Consume the `('. */
9467 cp_lexer_consume_token (parser->lexer);
9468 /* Look for the matching `)'. */
9469 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9470 return ansi_opname (CALL_EXPR);
9472 case CPP_OPEN_SQUARE:
9473 /* Consume the `['. */
9474 cp_lexer_consume_token (parser->lexer);
9475 /* Look for the matching `]'. */
9476 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9477 return ansi_opname (ARRAY_REF);
9480 /* Anything else is an error. */
9484 /* If we have selected an identifier, we need to consume the
9487 cp_lexer_consume_token (parser->lexer);
9488 /* Otherwise, no valid operator name was present. */
9491 cp_parser_error (parser, "expected operator");
9492 id = error_mark_node;
9498 /* Parse a template-declaration.
9500 template-declaration:
9501 export [opt] template < template-parameter-list > declaration
9503 If MEMBER_P is TRUE, this template-declaration occurs within a
9506 The grammar rule given by the standard isn't correct. What
9509 template-declaration:
9510 export [opt] template-parameter-list-seq
9511 decl-specifier-seq [opt] init-declarator [opt] ;
9512 export [opt] template-parameter-list-seq
9515 template-parameter-list-seq:
9516 template-parameter-list-seq [opt]
9517 template < template-parameter-list > */
9520 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9522 /* Check for `export'. */
9523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9525 /* Consume the `export' token. */
9526 cp_lexer_consume_token (parser->lexer);
9527 /* Warn that we do not support `export'. */
9528 warning (0, "keyword %<export%> not implemented, and will be ignored");
9531 cp_parser_template_declaration_after_export (parser, member_p);
9534 /* Parse a template-parameter-list.
9536 template-parameter-list:
9538 template-parameter-list , template-parameter
9540 Returns a TREE_LIST. Each node represents a template parameter.
9541 The nodes are connected via their TREE_CHAINs. */
9544 cp_parser_template_parameter_list (cp_parser* parser)
9546 tree parameter_list = NULL_TREE;
9548 begin_template_parm_list ();
9553 bool is_parameter_pack;
9555 /* Parse the template-parameter. */
9556 parameter = cp_parser_template_parameter (parser,
9558 &is_parameter_pack);
9559 /* Add it to the list. */
9560 if (parameter != error_mark_node)
9561 parameter_list = process_template_parm (parameter_list,
9567 tree err_parm = build_tree_list (parameter, parameter);
9568 TREE_VALUE (err_parm) = error_mark_node;
9569 parameter_list = chainon (parameter_list, err_parm);
9572 /* If the next token is not a `,', we're done. */
9573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9575 /* Otherwise, consume the `,' token. */
9576 cp_lexer_consume_token (parser->lexer);
9579 return end_template_parm_list (parameter_list);
9582 /* Parse a template-parameter.
9586 parameter-declaration
9588 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9589 the parameter. The TREE_PURPOSE is the default value, if any.
9590 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9591 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9592 set to true iff this parameter is a parameter pack. */
9595 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9596 bool *is_parameter_pack)
9599 cp_parameter_declarator *parameter_declarator;
9600 cp_declarator *id_declarator;
9603 /* Assume it is a type parameter or a template parameter. */
9604 *is_non_type = false;
9605 /* Assume it not a parameter pack. */
9606 *is_parameter_pack = false;
9607 /* Peek at the next token. */
9608 token = cp_lexer_peek_token (parser->lexer);
9609 /* If it is `class' or `template', we have a type-parameter. */
9610 if (token->keyword == RID_TEMPLATE)
9611 return cp_parser_type_parameter (parser, is_parameter_pack);
9612 /* If it is `class' or `typename' we do not know yet whether it is a
9613 type parameter or a non-type parameter. Consider:
9615 template <typename T, typename T::X X> ...
9619 template <class C, class D*> ...
9621 Here, the first parameter is a type parameter, and the second is
9622 a non-type parameter. We can tell by looking at the token after
9623 the identifier -- if it is a `,', `=', or `>' then we have a type
9625 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9627 /* Peek at the token after `class' or `typename'. */
9628 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9629 /* If it's an ellipsis, we have a template type parameter
9631 if (token->type == CPP_ELLIPSIS)
9632 return cp_parser_type_parameter (parser, is_parameter_pack);
9633 /* If it's an identifier, skip it. */
9634 if (token->type == CPP_NAME)
9635 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9636 /* Now, see if the token looks like the end of a template
9638 if (token->type == CPP_COMMA
9639 || token->type == CPP_EQ
9640 || token->type == CPP_GREATER)
9641 return cp_parser_type_parameter (parser, is_parameter_pack);
9644 /* Otherwise, it is a non-type parameter.
9648 When parsing a default template-argument for a non-type
9649 template-parameter, the first non-nested `>' is taken as the end
9650 of the template parameter-list rather than a greater-than
9652 *is_non_type = true;
9653 parameter_declarator
9654 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9655 /*parenthesized_p=*/NULL);
9657 /* If the parameter declaration is marked as a parameter pack, set
9658 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9659 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9661 if (parameter_declarator
9662 && parameter_declarator->declarator
9663 && parameter_declarator->declarator->parameter_pack_p)
9665 *is_parameter_pack = true;
9666 parameter_declarator->declarator->parameter_pack_p = false;
9669 /* If the next token is an ellipsis, and we don't already have it
9670 marked as a parameter pack, then we have a parameter pack (that
9671 has no declarator). */
9672 if (!*is_parameter_pack
9673 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9674 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9676 /* Consume the `...'. */
9677 cp_lexer_consume_token (parser->lexer);
9678 maybe_warn_variadic_templates ();
9680 *is_parameter_pack = true;
9682 /* We might end up with a pack expansion as the type of the non-type
9683 template parameter, in which case this is a non-type template
9685 else if (parameter_declarator
9686 && parameter_declarator->decl_specifiers.type
9687 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9689 *is_parameter_pack = true;
9690 parameter_declarator->decl_specifiers.type =
9691 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9694 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9696 /* Parameter packs cannot have default arguments. However, a
9697 user may try to do so, so we'll parse them and give an
9698 appropriate diagnostic here. */
9700 /* Consume the `='. */
9701 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9702 cp_lexer_consume_token (parser->lexer);
9704 /* Find the name of the parameter pack. */
9705 id_declarator = parameter_declarator->declarator;
9706 while (id_declarator && id_declarator->kind != cdk_id)
9707 id_declarator = id_declarator->declarator;
9709 if (id_declarator && id_declarator->kind == cdk_id)
9710 error ("%Htemplate parameter pack %qD cannot have a default argument",
9711 &start_token->location, id_declarator->u.id.unqualified_name);
9713 error ("%Htemplate parameter pack cannot have a default argument",
9714 &start_token->location);
9716 /* Parse the default argument, but throw away the result. */
9717 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9720 parm = grokdeclarator (parameter_declarator->declarator,
9721 ¶meter_declarator->decl_specifiers,
9722 PARM, /*initialized=*/0,
9724 if (parm == error_mark_node)
9725 return error_mark_node;
9727 return build_tree_list (parameter_declarator->default_argument, parm);
9730 /* Parse a type-parameter.
9733 class identifier [opt]
9734 class identifier [opt] = type-id
9735 typename identifier [opt]
9736 typename identifier [opt] = type-id
9737 template < template-parameter-list > class identifier [opt]
9738 template < template-parameter-list > class identifier [opt]
9741 GNU Extension (variadic templates):
9744 class ... identifier [opt]
9745 typename ... identifier [opt]
9747 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9748 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9749 the declaration of the parameter.
9751 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9754 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9759 /* Look for a keyword to tell us what kind of parameter this is. */
9760 token = cp_parser_require (parser, CPP_KEYWORD,
9761 "%<class%>, %<typename%>, or %<template%>");
9763 return error_mark_node;
9765 switch (token->keyword)
9771 tree default_argument;
9773 /* If the next token is an ellipsis, we have a template
9775 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9777 /* Consume the `...' token. */
9778 cp_lexer_consume_token (parser->lexer);
9779 maybe_warn_variadic_templates ();
9781 *is_parameter_pack = true;
9784 /* If the next token is an identifier, then it names the
9786 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9787 identifier = cp_parser_identifier (parser);
9789 identifier = NULL_TREE;
9791 /* Create the parameter. */
9792 parameter = finish_template_type_parm (class_type_node, identifier);
9794 /* If the next token is an `=', we have a default argument. */
9795 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9797 /* Consume the `=' token. */
9798 cp_lexer_consume_token (parser->lexer);
9799 /* Parse the default-argument. */
9800 push_deferring_access_checks (dk_no_deferred);
9801 default_argument = cp_parser_type_id (parser);
9803 /* Template parameter packs cannot have default
9805 if (*is_parameter_pack)
9808 error ("%Htemplate parameter pack %qD cannot have a "
9809 "default argument", &token->location, identifier);
9811 error ("%Htemplate parameter packs cannot have "
9812 "default arguments", &token->location);
9813 default_argument = NULL_TREE;
9815 pop_deferring_access_checks ();
9818 default_argument = NULL_TREE;
9820 /* Create the combined representation of the parameter and the
9821 default argument. */
9822 parameter = build_tree_list (default_argument, parameter);
9828 tree parameter_list;
9830 tree default_argument;
9832 /* Look for the `<'. */
9833 cp_parser_require (parser, CPP_LESS, "%<<%>");
9834 /* Parse the template-parameter-list. */
9835 parameter_list = cp_parser_template_parameter_list (parser);
9836 /* Look for the `>'. */
9837 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9838 /* Look for the `class' keyword. */
9839 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9840 /* If the next token is an ellipsis, we have a template
9842 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9844 /* Consume the `...' token. */
9845 cp_lexer_consume_token (parser->lexer);
9846 maybe_warn_variadic_templates ();
9848 *is_parameter_pack = true;
9850 /* If the next token is an `=', then there is a
9851 default-argument. If the next token is a `>', we are at
9852 the end of the parameter-list. If the next token is a `,',
9853 then we are at the end of this parameter. */
9854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9855 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9856 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9858 identifier = cp_parser_identifier (parser);
9859 /* Treat invalid names as if the parameter were nameless. */
9860 if (identifier == error_mark_node)
9861 identifier = NULL_TREE;
9864 identifier = NULL_TREE;
9866 /* Create the template parameter. */
9867 parameter = finish_template_template_parm (class_type_node,
9870 /* If the next token is an `=', then there is a
9871 default-argument. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9876 /* Consume the `='. */
9877 cp_lexer_consume_token (parser->lexer);
9878 /* Parse the id-expression. */
9879 push_deferring_access_checks (dk_no_deferred);
9880 /* save token before parsing the id-expression, for error
9882 token = cp_lexer_peek_token (parser->lexer);
9884 = cp_parser_id_expression (parser,
9885 /*template_keyword_p=*/false,
9886 /*check_dependency_p=*/true,
9887 /*template_p=*/&is_template,
9888 /*declarator_p=*/false,
9889 /*optional_p=*/false);
9890 if (TREE_CODE (default_argument) == TYPE_DECL)
9891 /* If the id-expression was a template-id that refers to
9892 a template-class, we already have the declaration here,
9893 so no further lookup is needed. */
9896 /* Look up the name. */
9898 = cp_parser_lookup_name (parser, default_argument,
9900 /*is_template=*/is_template,
9901 /*is_namespace=*/false,
9902 /*check_dependency=*/true,
9903 /*ambiguous_decls=*/NULL,
9905 /* See if the default argument is valid. */
9907 = check_template_template_default_arg (default_argument);
9909 /* Template parameter packs cannot have default
9911 if (*is_parameter_pack)
9914 error ("%Htemplate parameter pack %qD cannot "
9915 "have a default argument",
9916 &token->location, identifier);
9918 error ("%Htemplate parameter packs cannot "
9919 "have default arguments",
9921 default_argument = NULL_TREE;
9923 pop_deferring_access_checks ();
9926 default_argument = NULL_TREE;
9928 /* Create the combined representation of the parameter and the
9929 default argument. */
9930 parameter = build_tree_list (default_argument, parameter);
9942 /* Parse a template-id.
9945 template-name < template-argument-list [opt] >
9947 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9948 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9949 returned. Otherwise, if the template-name names a function, or set
9950 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9951 names a class, returns a TYPE_DECL for the specialization.
9953 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9954 uninstantiated templates. */
9957 cp_parser_template_id (cp_parser *parser,
9958 bool template_keyword_p,
9959 bool check_dependency_p,
9960 bool is_declaration)
9966 cp_token_position start_of_id = 0;
9967 deferred_access_check *chk;
9968 VEC (deferred_access_check,gc) *access_check;
9969 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9972 /* If the next token corresponds to a template-id, there is no need
9974 next_token = cp_lexer_peek_token (parser->lexer);
9975 if (next_token->type == CPP_TEMPLATE_ID)
9977 struct tree_check *check_value;
9979 /* Get the stored value. */
9980 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9981 /* Perform any access checks that were deferred. */
9982 access_check = check_value->checks;
9986 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9989 perform_or_defer_access_check (chk->binfo,
9994 /* Return the stored value. */
9995 return check_value->value;
9998 /* Avoid performing name lookup if there is no possibility of
9999 finding a template-id. */
10000 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10001 || (next_token->type == CPP_NAME
10002 && !cp_parser_nth_token_starts_template_argument_list_p
10005 cp_parser_error (parser, "expected template-id");
10006 return error_mark_node;
10009 /* Remember where the template-id starts. */
10010 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10011 start_of_id = cp_lexer_token_position (parser->lexer, false);
10013 push_deferring_access_checks (dk_deferred);
10015 /* Parse the template-name. */
10016 is_identifier = false;
10017 token = cp_lexer_peek_token (parser->lexer);
10018 templ = cp_parser_template_name (parser, template_keyword_p,
10019 check_dependency_p,
10022 if (templ == error_mark_node || is_identifier)
10024 pop_deferring_access_checks ();
10028 /* If we find the sequence `[:' after a template-name, it's probably
10029 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10030 parse correctly the argument list. */
10031 next_token = cp_lexer_peek_token (parser->lexer);
10032 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10033 if (next_token->type == CPP_OPEN_SQUARE
10034 && next_token->flags & DIGRAPH
10035 && next_token_2->type == CPP_COLON
10036 && !(next_token_2->flags & PREV_WHITE))
10038 cp_parser_parse_tentatively (parser);
10039 /* Change `:' into `::'. */
10040 next_token_2->type = CPP_SCOPE;
10041 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10043 cp_lexer_consume_token (parser->lexer);
10045 /* Parse the arguments. */
10046 arguments = cp_parser_enclosed_template_argument_list (parser);
10047 if (!cp_parser_parse_definitely (parser))
10049 /* If we couldn't parse an argument list, then we revert our changes
10050 and return simply an error. Maybe this is not a template-id
10052 next_token_2->type = CPP_COLON;
10053 cp_parser_error (parser, "expected %<<%>");
10054 pop_deferring_access_checks ();
10055 return error_mark_node;
10057 /* Otherwise, emit an error about the invalid digraph, but continue
10058 parsing because we got our argument list. */
10059 if (permerror (next_token->location,
10060 "%<<::%> cannot begin a template-argument list"))
10062 static bool hint = false;
10063 inform (next_token->location,
10064 "%<<:%> is an alternate spelling for %<[%>."
10065 " Insert whitespace between %<<%> and %<::%>");
10066 if (!hint && !flag_permissive)
10068 inform (next_token->location, "(if you use %<-fpermissive%>"
10069 " G++ will accept your code)");
10076 /* Look for the `<' that starts the template-argument-list. */
10077 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10079 pop_deferring_access_checks ();
10080 return error_mark_node;
10082 /* Parse the arguments. */
10083 arguments = cp_parser_enclosed_template_argument_list (parser);
10086 /* Build a representation of the specialization. */
10087 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10088 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10089 else if (DECL_CLASS_TEMPLATE_P (templ)
10090 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10092 bool entering_scope;
10093 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10094 template (rather than some instantiation thereof) only if
10095 is not nested within some other construct. For example, in
10096 "template <typename T> void f(T) { A<T>::", A<T> is just an
10097 instantiation of A. */
10098 entering_scope = (template_parm_scope_p ()
10099 && cp_lexer_next_token_is (parser->lexer,
10102 = finish_template_type (templ, arguments, entering_scope);
10106 /* If it's not a class-template or a template-template, it should be
10107 a function-template. */
10108 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10109 || TREE_CODE (templ) == OVERLOAD
10110 || BASELINK_P (templ)));
10112 template_id = lookup_template_function (templ, arguments);
10115 /* If parsing tentatively, replace the sequence of tokens that makes
10116 up the template-id with a CPP_TEMPLATE_ID token. That way,
10117 should we re-parse the token stream, we will not have to repeat
10118 the effort required to do the parse, nor will we issue duplicate
10119 error messages about problems during instantiation of the
10123 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10125 /* Reset the contents of the START_OF_ID token. */
10126 token->type = CPP_TEMPLATE_ID;
10127 /* Retrieve any deferred checks. Do not pop this access checks yet
10128 so the memory will not be reclaimed during token replacing below. */
10129 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10130 token->u.tree_check_value->value = template_id;
10131 token->u.tree_check_value->checks = get_deferred_access_checks ();
10132 token->keyword = RID_MAX;
10134 /* Purge all subsequent tokens. */
10135 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10137 /* ??? Can we actually assume that, if template_id ==
10138 error_mark_node, we will have issued a diagnostic to the
10139 user, as opposed to simply marking the tentative parse as
10141 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10142 error ("%Hparse error in template argument list",
10146 pop_deferring_access_checks ();
10147 return template_id;
10150 /* Parse a template-name.
10155 The standard should actually say:
10159 operator-function-id
10161 A defect report has been filed about this issue.
10163 A conversion-function-id cannot be a template name because they cannot
10164 be part of a template-id. In fact, looking at this code:
10166 a.operator K<int>()
10168 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10169 It is impossible to call a templated conversion-function-id with an
10170 explicit argument list, since the only allowed template parameter is
10171 the type to which it is converting.
10173 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10174 `template' keyword, in a construction like:
10178 In that case `f' is taken to be a template-name, even though there
10179 is no way of knowing for sure.
10181 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10182 name refers to a set of overloaded functions, at least one of which
10183 is a template, or an IDENTIFIER_NODE with the name of the template,
10184 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10185 names are looked up inside uninstantiated templates. */
10188 cp_parser_template_name (cp_parser* parser,
10189 bool template_keyword_p,
10190 bool check_dependency_p,
10191 bool is_declaration,
10192 bool *is_identifier)
10197 cp_token *token = cp_lexer_peek_token (parser->lexer);
10199 /* If the next token is `operator', then we have either an
10200 operator-function-id or a conversion-function-id. */
10201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10203 /* We don't know whether we're looking at an
10204 operator-function-id or a conversion-function-id. */
10205 cp_parser_parse_tentatively (parser);
10206 /* Try an operator-function-id. */
10207 identifier = cp_parser_operator_function_id (parser);
10208 /* If that didn't work, try a conversion-function-id. */
10209 if (!cp_parser_parse_definitely (parser))
10211 cp_parser_error (parser, "expected template-name");
10212 return error_mark_node;
10215 /* Look for the identifier. */
10217 identifier = cp_parser_identifier (parser);
10219 /* If we didn't find an identifier, we don't have a template-id. */
10220 if (identifier == error_mark_node)
10221 return error_mark_node;
10223 /* If the name immediately followed the `template' keyword, then it
10224 is a template-name. However, if the next token is not `<', then
10225 we do not treat it as a template-name, since it is not being used
10226 as part of a template-id. This enables us to handle constructs
10229 template <typename T> struct S { S(); };
10230 template <typename T> S<T>::S();
10232 correctly. We would treat `S' as a template -- if it were `S<T>'
10233 -- but we do not if there is no `<'. */
10235 if (processing_template_decl
10236 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10238 /* In a declaration, in a dependent context, we pretend that the
10239 "template" keyword was present in order to improve error
10240 recovery. For example, given:
10242 template <typename T> void f(T::X<int>);
10244 we want to treat "X<int>" as a template-id. */
10246 && !template_keyword_p
10247 && parser->scope && TYPE_P (parser->scope)
10248 && check_dependency_p
10249 && dependent_type_p (parser->scope)
10250 /* Do not do this for dtors (or ctors), since they never
10251 need the template keyword before their name. */
10252 && !constructor_name_p (identifier, parser->scope))
10254 cp_token_position start = 0;
10256 /* Explain what went wrong. */
10257 error ("%Hnon-template %qD used as template",
10258 &token->location, identifier);
10259 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10260 parser->scope, identifier);
10261 /* If parsing tentatively, find the location of the "<" token. */
10262 if (cp_parser_simulate_error (parser))
10263 start = cp_lexer_token_position (parser->lexer, true);
10264 /* Parse the template arguments so that we can issue error
10265 messages about them. */
10266 cp_lexer_consume_token (parser->lexer);
10267 cp_parser_enclosed_template_argument_list (parser);
10268 /* Skip tokens until we find a good place from which to
10269 continue parsing. */
10270 cp_parser_skip_to_closing_parenthesis (parser,
10271 /*recovering=*/true,
10273 /*consume_paren=*/false);
10274 /* If parsing tentatively, permanently remove the
10275 template argument list. That will prevent duplicate
10276 error messages from being issued about the missing
10277 "template" keyword. */
10279 cp_lexer_purge_tokens_after (parser->lexer, start);
10281 *is_identifier = true;
10285 /* If the "template" keyword is present, then there is generally
10286 no point in doing name-lookup, so we just return IDENTIFIER.
10287 But, if the qualifying scope is non-dependent then we can
10288 (and must) do name-lookup normally. */
10289 if (template_keyword_p
10291 || (TYPE_P (parser->scope)
10292 && dependent_type_p (parser->scope))))
10296 /* Look up the name. */
10297 decl = cp_parser_lookup_name (parser, identifier,
10299 /*is_template=*/false,
10300 /*is_namespace=*/false,
10301 check_dependency_p,
10302 /*ambiguous_decls=*/NULL,
10304 decl = maybe_get_template_decl_from_type_decl (decl);
10306 /* If DECL is a template, then the name was a template-name. */
10307 if (TREE_CODE (decl) == TEMPLATE_DECL)
10311 tree fn = NULL_TREE;
10313 /* The standard does not explicitly indicate whether a name that
10314 names a set of overloaded declarations, some of which are
10315 templates, is a template-name. However, such a name should
10316 be a template-name; otherwise, there is no way to form a
10317 template-id for the overloaded templates. */
10318 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10319 if (TREE_CODE (fns) == OVERLOAD)
10320 for (fn = fns; fn; fn = OVL_NEXT (fn))
10321 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10326 /* The name does not name a template. */
10327 cp_parser_error (parser, "expected template-name");
10328 return error_mark_node;
10332 /* If DECL is dependent, and refers to a function, then just return
10333 its name; we will look it up again during template instantiation. */
10334 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10336 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10337 if (TYPE_P (scope) && dependent_type_p (scope))
10344 /* Parse a template-argument-list.
10346 template-argument-list:
10347 template-argument ... [opt]
10348 template-argument-list , template-argument ... [opt]
10350 Returns a TREE_VEC containing the arguments. */
10353 cp_parser_template_argument_list (cp_parser* parser)
10355 tree fixed_args[10];
10356 unsigned n_args = 0;
10357 unsigned alloced = 10;
10358 tree *arg_ary = fixed_args;
10360 bool saved_in_template_argument_list_p;
10362 bool saved_non_ice_p;
10364 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10365 parser->in_template_argument_list_p = true;
10366 /* Even if the template-id appears in an integral
10367 constant-expression, the contents of the argument list do
10369 saved_ice_p = parser->integral_constant_expression_p;
10370 parser->integral_constant_expression_p = false;
10371 saved_non_ice_p = parser->non_integral_constant_expression_p;
10372 parser->non_integral_constant_expression_p = false;
10373 /* Parse the arguments. */
10379 /* Consume the comma. */
10380 cp_lexer_consume_token (parser->lexer);
10382 /* Parse the template-argument. */
10383 argument = cp_parser_template_argument (parser);
10385 /* If the next token is an ellipsis, we're expanding a template
10387 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10389 /* Consume the `...' token. */
10390 cp_lexer_consume_token (parser->lexer);
10392 /* Make the argument into a TYPE_PACK_EXPANSION or
10393 EXPR_PACK_EXPANSION. */
10394 argument = make_pack_expansion (argument);
10397 if (n_args == alloced)
10401 if (arg_ary == fixed_args)
10403 arg_ary = XNEWVEC (tree, alloced);
10404 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10407 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10409 arg_ary[n_args++] = argument;
10411 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10413 vec = make_tree_vec (n_args);
10416 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10418 if (arg_ary != fixed_args)
10420 parser->non_integral_constant_expression_p = saved_non_ice_p;
10421 parser->integral_constant_expression_p = saved_ice_p;
10422 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10426 /* Parse a template-argument.
10429 assignment-expression
10433 The representation is that of an assignment-expression, type-id, or
10434 id-expression -- except that the qualified id-expression is
10435 evaluated, so that the value returned is either a DECL or an
10438 Although the standard says "assignment-expression", it forbids
10439 throw-expressions or assignments in the template argument.
10440 Therefore, we use "conditional-expression" instead. */
10443 cp_parser_template_argument (cp_parser* parser)
10448 bool maybe_type_id = false;
10449 cp_token *token = NULL, *argument_start_token = NULL;
10452 /* There's really no way to know what we're looking at, so we just
10453 try each alternative in order.
10457 In a template-argument, an ambiguity between a type-id and an
10458 expression is resolved to a type-id, regardless of the form of
10459 the corresponding template-parameter.
10461 Therefore, we try a type-id first. */
10462 cp_parser_parse_tentatively (parser);
10463 argument = cp_parser_type_id (parser);
10464 /* If there was no error parsing the type-id but the next token is a
10465 '>>', our behavior depends on which dialect of C++ we're
10466 parsing. In C++98, we probably found a typo for '> >'. But there
10467 are type-id which are also valid expressions. For instance:
10469 struct X { int operator >> (int); };
10470 template <int V> struct Foo {};
10473 Here 'X()' is a valid type-id of a function type, but the user just
10474 wanted to write the expression "X() >> 5". Thus, we remember that we
10475 found a valid type-id, but we still try to parse the argument as an
10476 expression to see what happens.
10478 In C++0x, the '>>' will be considered two separate '>'
10480 if (!cp_parser_error_occurred (parser)
10481 && cxx_dialect == cxx98
10482 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10484 maybe_type_id = true;
10485 cp_parser_abort_tentative_parse (parser);
10489 /* If the next token isn't a `,' or a `>', then this argument wasn't
10490 really finished. This means that the argument is not a valid
10492 if (!cp_parser_next_token_ends_template_argument_p (parser))
10493 cp_parser_error (parser, "expected template-argument");
10494 /* If that worked, we're done. */
10495 if (cp_parser_parse_definitely (parser))
10498 /* We're still not sure what the argument will be. */
10499 cp_parser_parse_tentatively (parser);
10500 /* Try a template. */
10501 argument_start_token = cp_lexer_peek_token (parser->lexer);
10502 argument = cp_parser_id_expression (parser,
10503 /*template_keyword_p=*/false,
10504 /*check_dependency_p=*/true,
10506 /*declarator_p=*/false,
10507 /*optional_p=*/false);
10508 /* If the next token isn't a `,' or a `>', then this argument wasn't
10509 really finished. */
10510 if (!cp_parser_next_token_ends_template_argument_p (parser))
10511 cp_parser_error (parser, "expected template-argument");
10512 if (!cp_parser_error_occurred (parser))
10514 /* Figure out what is being referred to. If the id-expression
10515 was for a class template specialization, then we will have a
10516 TYPE_DECL at this point. There is no need to do name lookup
10517 at this point in that case. */
10518 if (TREE_CODE (argument) != TYPE_DECL)
10519 argument = cp_parser_lookup_name (parser, argument,
10521 /*is_template=*/template_p,
10522 /*is_namespace=*/false,
10523 /*check_dependency=*/true,
10524 /*ambiguous_decls=*/NULL,
10525 argument_start_token->location);
10526 if (TREE_CODE (argument) != TEMPLATE_DECL
10527 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10528 cp_parser_error (parser, "expected template-name");
10530 if (cp_parser_parse_definitely (parser))
10532 /* It must be a non-type argument. There permitted cases are given
10533 in [temp.arg.nontype]:
10535 -- an integral constant-expression of integral or enumeration
10538 -- the name of a non-type template-parameter; or
10540 -- the name of an object or function with external linkage...
10542 -- the address of an object or function with external linkage...
10544 -- a pointer to member... */
10545 /* Look for a non-type template parameter. */
10546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10548 cp_parser_parse_tentatively (parser);
10549 argument = cp_parser_primary_expression (parser,
10550 /*address_p=*/false,
10552 /*template_arg_p=*/true,
10554 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10555 || !cp_parser_next_token_ends_template_argument_p (parser))
10556 cp_parser_simulate_error (parser);
10557 if (cp_parser_parse_definitely (parser))
10561 /* If the next token is "&", the argument must be the address of an
10562 object or function with external linkage. */
10563 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10565 cp_lexer_consume_token (parser->lexer);
10566 /* See if we might have an id-expression. */
10567 token = cp_lexer_peek_token (parser->lexer);
10568 if (token->type == CPP_NAME
10569 || token->keyword == RID_OPERATOR
10570 || token->type == CPP_SCOPE
10571 || token->type == CPP_TEMPLATE_ID
10572 || token->type == CPP_NESTED_NAME_SPECIFIER)
10574 cp_parser_parse_tentatively (parser);
10575 argument = cp_parser_primary_expression (parser,
10578 /*template_arg_p=*/true,
10580 if (cp_parser_error_occurred (parser)
10581 || !cp_parser_next_token_ends_template_argument_p (parser))
10582 cp_parser_abort_tentative_parse (parser);
10585 if (TREE_CODE (argument) == INDIRECT_REF)
10587 gcc_assert (REFERENCE_REF_P (argument));
10588 argument = TREE_OPERAND (argument, 0);
10591 if (TREE_CODE (argument) == VAR_DECL)
10593 /* A variable without external linkage might still be a
10594 valid constant-expression, so no error is issued here
10595 if the external-linkage check fails. */
10596 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10597 cp_parser_simulate_error (parser);
10599 else if (is_overloaded_fn (argument))
10600 /* All overloaded functions are allowed; if the external
10601 linkage test does not pass, an error will be issued
10605 && (TREE_CODE (argument) == OFFSET_REF
10606 || TREE_CODE (argument) == SCOPE_REF))
10607 /* A pointer-to-member. */
10609 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10612 cp_parser_simulate_error (parser);
10614 if (cp_parser_parse_definitely (parser))
10617 argument = build_x_unary_op (ADDR_EXPR, argument,
10618 tf_warning_or_error);
10623 /* If the argument started with "&", there are no other valid
10624 alternatives at this point. */
10627 cp_parser_error (parser, "invalid non-type template argument");
10628 return error_mark_node;
10631 /* If the argument wasn't successfully parsed as a type-id followed
10632 by '>>', the argument can only be a constant expression now.
10633 Otherwise, we try parsing the constant-expression tentatively,
10634 because the argument could really be a type-id. */
10636 cp_parser_parse_tentatively (parser);
10637 argument = cp_parser_constant_expression (parser,
10638 /*allow_non_constant_p=*/false,
10639 /*non_constant_p=*/NULL);
10640 argument = fold_non_dependent_expr (argument);
10641 if (!maybe_type_id)
10643 if (!cp_parser_next_token_ends_template_argument_p (parser))
10644 cp_parser_error (parser, "expected template-argument");
10645 if (cp_parser_parse_definitely (parser))
10647 /* We did our best to parse the argument as a non type-id, but that
10648 was the only alternative that matched (albeit with a '>' after
10649 it). We can assume it's just a typo from the user, and a
10650 diagnostic will then be issued. */
10651 return cp_parser_type_id (parser);
10654 /* Parse an explicit-instantiation.
10656 explicit-instantiation:
10657 template declaration
10659 Although the standard says `declaration', what it really means is:
10661 explicit-instantiation:
10662 template decl-specifier-seq [opt] declarator [opt] ;
10664 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10665 supposed to be allowed. A defect report has been filed about this
10670 explicit-instantiation:
10671 storage-class-specifier template
10672 decl-specifier-seq [opt] declarator [opt] ;
10673 function-specifier template
10674 decl-specifier-seq [opt] declarator [opt] ; */
10677 cp_parser_explicit_instantiation (cp_parser* parser)
10679 int declares_class_or_enum;
10680 cp_decl_specifier_seq decl_specifiers;
10681 tree extension_specifier = NULL_TREE;
10684 /* Look for an (optional) storage-class-specifier or
10685 function-specifier. */
10686 if (cp_parser_allow_gnu_extensions_p (parser))
10688 extension_specifier
10689 = cp_parser_storage_class_specifier_opt (parser);
10690 if (!extension_specifier)
10691 extension_specifier
10692 = cp_parser_function_specifier_opt (parser,
10693 /*decl_specs=*/NULL);
10696 /* Look for the `template' keyword. */
10697 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10698 /* Let the front end know that we are processing an explicit
10700 begin_explicit_instantiation ();
10701 /* [temp.explicit] says that we are supposed to ignore access
10702 control while processing explicit instantiation directives. */
10703 push_deferring_access_checks (dk_no_check);
10704 /* Parse a decl-specifier-seq. */
10705 token = cp_lexer_peek_token (parser->lexer);
10706 cp_parser_decl_specifier_seq (parser,
10707 CP_PARSER_FLAGS_OPTIONAL,
10709 &declares_class_or_enum);
10710 /* If there was exactly one decl-specifier, and it declared a class,
10711 and there's no declarator, then we have an explicit type
10713 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10717 type = check_tag_decl (&decl_specifiers);
10718 /* Turn access control back on for names used during
10719 template instantiation. */
10720 pop_deferring_access_checks ();
10722 do_type_instantiation (type, extension_specifier,
10723 /*complain=*/tf_error);
10727 cp_declarator *declarator;
10730 /* Parse the declarator. */
10732 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10733 /*ctor_dtor_or_conv_p=*/NULL,
10734 /*parenthesized_p=*/NULL,
10735 /*member_p=*/false);
10736 if (declares_class_or_enum & 2)
10737 cp_parser_check_for_definition_in_return_type (declarator,
10738 decl_specifiers.type,
10739 decl_specifiers.type_location);
10740 if (declarator != cp_error_declarator)
10742 decl = grokdeclarator (declarator, &decl_specifiers,
10743 NORMAL, 0, &decl_specifiers.attributes);
10744 /* Turn access control back on for names used during
10745 template instantiation. */
10746 pop_deferring_access_checks ();
10747 /* Do the explicit instantiation. */
10748 do_decl_instantiation (decl, extension_specifier);
10752 pop_deferring_access_checks ();
10753 /* Skip the body of the explicit instantiation. */
10754 cp_parser_skip_to_end_of_statement (parser);
10757 /* We're done with the instantiation. */
10758 end_explicit_instantiation ();
10760 cp_parser_consume_semicolon_at_end_of_statement (parser);
10763 /* Parse an explicit-specialization.
10765 explicit-specialization:
10766 template < > declaration
10768 Although the standard says `declaration', what it really means is:
10770 explicit-specialization:
10771 template <> decl-specifier [opt] init-declarator [opt] ;
10772 template <> function-definition
10773 template <> explicit-specialization
10774 template <> template-declaration */
10777 cp_parser_explicit_specialization (cp_parser* parser)
10779 bool need_lang_pop;
10780 cp_token *token = cp_lexer_peek_token (parser->lexer);
10782 /* Look for the `template' keyword. */
10783 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10784 /* Look for the `<'. */
10785 cp_parser_require (parser, CPP_LESS, "%<<%>");
10786 /* Look for the `>'. */
10787 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10788 /* We have processed another parameter list. */
10789 ++parser->num_template_parameter_lists;
10792 A template ... explicit specialization ... shall not have C
10794 if (current_lang_name == lang_name_c)
10796 error ("%Htemplate specialization with C linkage", &token->location);
10797 /* Give it C++ linkage to avoid confusing other parts of the
10799 push_lang_context (lang_name_cplusplus);
10800 need_lang_pop = true;
10803 need_lang_pop = false;
10804 /* Let the front end know that we are beginning a specialization. */
10805 if (!begin_specialization ())
10807 end_specialization ();
10808 cp_parser_skip_to_end_of_block_or_statement (parser);
10812 /* If the next keyword is `template', we need to figure out whether
10813 or not we're looking a template-declaration. */
10814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10816 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10817 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10818 cp_parser_template_declaration_after_export (parser,
10819 /*member_p=*/false);
10821 cp_parser_explicit_specialization (parser);
10824 /* Parse the dependent declaration. */
10825 cp_parser_single_declaration (parser,
10827 /*member_p=*/false,
10828 /*explicit_specialization_p=*/true,
10829 /*friend_p=*/NULL);
10830 /* We're done with the specialization. */
10831 end_specialization ();
10832 /* For the erroneous case of a template with C linkage, we pushed an
10833 implicit C++ linkage scope; exit that scope now. */
10835 pop_lang_context ();
10836 /* We're done with this parameter list. */
10837 --parser->num_template_parameter_lists;
10840 /* Parse a type-specifier.
10843 simple-type-specifier
10846 elaborated-type-specifier
10854 Returns a representation of the type-specifier. For a
10855 class-specifier, enum-specifier, or elaborated-type-specifier, a
10856 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10858 The parser flags FLAGS is used to control type-specifier parsing.
10860 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10861 in a decl-specifier-seq.
10863 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10864 class-specifier, enum-specifier, or elaborated-type-specifier, then
10865 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10866 if a type is declared; 2 if it is defined. Otherwise, it is set to
10869 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10870 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10871 is set to FALSE. */
10874 cp_parser_type_specifier (cp_parser* parser,
10875 cp_parser_flags flags,
10876 cp_decl_specifier_seq *decl_specs,
10877 bool is_declaration,
10878 int* declares_class_or_enum,
10879 bool* is_cv_qualifier)
10881 tree type_spec = NULL_TREE;
10884 cp_decl_spec ds = ds_last;
10886 /* Assume this type-specifier does not declare a new type. */
10887 if (declares_class_or_enum)
10888 *declares_class_or_enum = 0;
10889 /* And that it does not specify a cv-qualifier. */
10890 if (is_cv_qualifier)
10891 *is_cv_qualifier = false;
10892 /* Peek at the next token. */
10893 token = cp_lexer_peek_token (parser->lexer);
10895 /* If we're looking at a keyword, we can use that to guide the
10896 production we choose. */
10897 keyword = token->keyword;
10901 /* Look for the enum-specifier. */
10902 type_spec = cp_parser_enum_specifier (parser);
10903 /* If that worked, we're done. */
10906 if (declares_class_or_enum)
10907 *declares_class_or_enum = 2;
10909 cp_parser_set_decl_spec_type (decl_specs,
10912 /*user_defined_p=*/true);
10916 goto elaborated_type_specifier;
10918 /* Any of these indicate either a class-specifier, or an
10919 elaborated-type-specifier. */
10923 /* Parse tentatively so that we can back up if we don't find a
10924 class-specifier. */
10925 cp_parser_parse_tentatively (parser);
10926 /* Look for the class-specifier. */
10927 type_spec = cp_parser_class_specifier (parser);
10928 /* If that worked, we're done. */
10929 if (cp_parser_parse_definitely (parser))
10931 if (declares_class_or_enum)
10932 *declares_class_or_enum = 2;
10934 cp_parser_set_decl_spec_type (decl_specs,
10937 /*user_defined_p=*/true);
10941 /* Fall through. */
10942 elaborated_type_specifier:
10943 /* We're declaring (not defining) a class or enum. */
10944 if (declares_class_or_enum)
10945 *declares_class_or_enum = 1;
10947 /* Fall through. */
10949 /* Look for an elaborated-type-specifier. */
10951 = (cp_parser_elaborated_type_specifier
10953 decl_specs && decl_specs->specs[(int) ds_friend],
10956 cp_parser_set_decl_spec_type (decl_specs,
10959 /*user_defined_p=*/true);
10964 if (is_cv_qualifier)
10965 *is_cv_qualifier = true;
10970 if (is_cv_qualifier)
10971 *is_cv_qualifier = true;
10976 if (is_cv_qualifier)
10977 *is_cv_qualifier = true;
10981 /* The `__complex__' keyword is a GNU extension. */
10989 /* Handle simple keywords. */
10994 ++decl_specs->specs[(int)ds];
10995 decl_specs->any_specifiers_p = true;
10997 return cp_lexer_consume_token (parser->lexer)->u.value;
11000 /* If we do not already have a type-specifier, assume we are looking
11001 at a simple-type-specifier. */
11002 type_spec = cp_parser_simple_type_specifier (parser,
11006 /* If we didn't find a type-specifier, and a type-specifier was not
11007 optional in this context, issue an error message. */
11008 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11010 cp_parser_error (parser, "expected type specifier");
11011 return error_mark_node;
11017 /* Parse a simple-type-specifier.
11019 simple-type-specifier:
11020 :: [opt] nested-name-specifier [opt] type-name
11021 :: [opt] nested-name-specifier template template-id
11036 simple-type-specifier:
11038 decltype ( expression )
11044 simple-type-specifier:
11045 __typeof__ unary-expression
11046 __typeof__ ( type-id )
11048 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11049 appropriately updated. */
11052 cp_parser_simple_type_specifier (cp_parser* parser,
11053 cp_decl_specifier_seq *decl_specs,
11054 cp_parser_flags flags)
11056 tree type = NULL_TREE;
11059 /* Peek at the next token. */
11060 token = cp_lexer_peek_token (parser->lexer);
11062 /* If we're looking at a keyword, things are easy. */
11063 switch (token->keyword)
11067 decl_specs->explicit_char_p = true;
11068 type = char_type_node;
11071 type = char16_type_node;
11074 type = char32_type_node;
11077 type = wchar_type_node;
11080 type = boolean_type_node;
11084 ++decl_specs->specs[(int) ds_short];
11085 type = short_integer_type_node;
11089 decl_specs->explicit_int_p = true;
11090 type = integer_type_node;
11094 ++decl_specs->specs[(int) ds_long];
11095 type = long_integer_type_node;
11099 ++decl_specs->specs[(int) ds_signed];
11100 type = integer_type_node;
11104 ++decl_specs->specs[(int) ds_unsigned];
11105 type = unsigned_type_node;
11108 type = float_type_node;
11111 type = double_type_node;
11114 type = void_type_node;
11118 maybe_warn_cpp0x ("C++0x auto");
11119 type = make_auto ();
11123 /* Parse the `decltype' type. */
11124 type = cp_parser_decltype (parser);
11127 cp_parser_set_decl_spec_type (decl_specs, type,
11129 /*user_defined_p=*/true);
11134 /* Consume the `typeof' token. */
11135 cp_lexer_consume_token (parser->lexer);
11136 /* Parse the operand to `typeof'. */
11137 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11138 /* If it is not already a TYPE, take its type. */
11139 if (!TYPE_P (type))
11140 type = finish_typeof (type);
11143 cp_parser_set_decl_spec_type (decl_specs, type,
11145 /*user_defined_p=*/true);
11153 /* If the type-specifier was for a built-in type, we're done. */
11158 /* Record the type. */
11160 && (token->keyword != RID_SIGNED
11161 && token->keyword != RID_UNSIGNED
11162 && token->keyword != RID_SHORT
11163 && token->keyword != RID_LONG))
11164 cp_parser_set_decl_spec_type (decl_specs,
11167 /*user_defined=*/false);
11169 decl_specs->any_specifiers_p = true;
11171 /* Consume the token. */
11172 id = cp_lexer_consume_token (parser->lexer)->u.value;
11174 /* There is no valid C++ program where a non-template type is
11175 followed by a "<". That usually indicates that the user thought
11176 that the type was a template. */
11177 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11179 return TYPE_NAME (type);
11182 /* The type-specifier must be a user-defined type. */
11183 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11188 /* Don't gobble tokens or issue error messages if this is an
11189 optional type-specifier. */
11190 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11191 cp_parser_parse_tentatively (parser);
11193 /* Look for the optional `::' operator. */
11195 = (cp_parser_global_scope_opt (parser,
11196 /*current_scope_valid_p=*/false)
11198 /* Look for the nested-name specifier. */
11200 = (cp_parser_nested_name_specifier_opt (parser,
11201 /*typename_keyword_p=*/false,
11202 /*check_dependency_p=*/true,
11204 /*is_declaration=*/false)
11206 token = cp_lexer_peek_token (parser->lexer);
11207 /* If we have seen a nested-name-specifier, and the next token
11208 is `template', then we are using the template-id production. */
11210 && cp_parser_optional_template_keyword (parser))
11212 /* Look for the template-id. */
11213 type = cp_parser_template_id (parser,
11214 /*template_keyword_p=*/true,
11215 /*check_dependency_p=*/true,
11216 /*is_declaration=*/false);
11217 /* If the template-id did not name a type, we are out of
11219 if (TREE_CODE (type) != TYPE_DECL)
11221 cp_parser_error (parser, "expected template-id for type");
11225 /* Otherwise, look for a type-name. */
11227 type = cp_parser_type_name (parser);
11228 /* Keep track of all name-lookups performed in class scopes. */
11232 && TREE_CODE (type) == TYPE_DECL
11233 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11234 maybe_note_name_used_in_class (DECL_NAME (type), type);
11235 /* If it didn't work out, we don't have a TYPE. */
11236 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11237 && !cp_parser_parse_definitely (parser))
11239 if (type && decl_specs)
11240 cp_parser_set_decl_spec_type (decl_specs, type,
11242 /*user_defined=*/true);
11245 /* If we didn't get a type-name, issue an error message. */
11246 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11248 cp_parser_error (parser, "expected type-name");
11249 return error_mark_node;
11252 /* There is no valid C++ program where a non-template type is
11253 followed by a "<". That usually indicates that the user thought
11254 that the type was a template. */
11255 if (type && type != error_mark_node)
11257 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11258 If it is, then the '<'...'>' enclose protocol names rather than
11259 template arguments, and so everything is fine. */
11260 if (c_dialect_objc ()
11261 && (objc_is_id (type) || objc_is_class_name (type)))
11263 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11264 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11266 /* Clobber the "unqualified" type previously entered into
11267 DECL_SPECS with the new, improved protocol-qualified version. */
11269 decl_specs->type = qual_type;
11274 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11281 /* Parse a type-name.
11294 Returns a TYPE_DECL for the type. */
11297 cp_parser_type_name (cp_parser* parser)
11301 /* We can't know yet whether it is a class-name or not. */
11302 cp_parser_parse_tentatively (parser);
11303 /* Try a class-name. */
11304 type_decl = cp_parser_class_name (parser,
11305 /*typename_keyword_p=*/false,
11306 /*template_keyword_p=*/false,
11308 /*check_dependency_p=*/true,
11309 /*class_head_p=*/false,
11310 /*is_declaration=*/false);
11311 /* If it's not a class-name, keep looking. */
11312 if (!cp_parser_parse_definitely (parser))
11314 /* It must be a typedef-name or an enum-name. */
11315 return cp_parser_nonclass_name (parser);
11321 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11329 Returns a TYPE_DECL for the type. */
11332 cp_parser_nonclass_name (cp_parser* parser)
11337 cp_token *token = cp_lexer_peek_token (parser->lexer);
11338 identifier = cp_parser_identifier (parser);
11339 if (identifier == error_mark_node)
11340 return error_mark_node;
11342 /* Look up the type-name. */
11343 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11345 if (TREE_CODE (type_decl) != TYPE_DECL
11346 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11348 /* See if this is an Objective-C type. */
11349 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11350 tree type = objc_get_protocol_qualified_type (identifier, protos);
11352 type_decl = TYPE_NAME (type);
11355 /* Issue an error if we did not find a type-name. */
11356 if (TREE_CODE (type_decl) != TYPE_DECL)
11358 if (!cp_parser_simulate_error (parser))
11359 cp_parser_name_lookup_error (parser, identifier, type_decl,
11360 "is not a type", token->location);
11361 return error_mark_node;
11363 /* Remember that the name was used in the definition of the
11364 current class so that we can check later to see if the
11365 meaning would have been different after the class was
11366 entirely defined. */
11367 else if (type_decl != error_mark_node
11369 maybe_note_name_used_in_class (identifier, type_decl);
11374 /* Parse an elaborated-type-specifier. Note that the grammar given
11375 here incorporates the resolution to DR68.
11377 elaborated-type-specifier:
11378 class-key :: [opt] nested-name-specifier [opt] identifier
11379 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11380 enum-key :: [opt] nested-name-specifier [opt] identifier
11381 typename :: [opt] nested-name-specifier identifier
11382 typename :: [opt] nested-name-specifier template [opt]
11387 elaborated-type-specifier:
11388 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11389 class-key attributes :: [opt] nested-name-specifier [opt]
11390 template [opt] template-id
11391 enum attributes :: [opt] nested-name-specifier [opt] identifier
11393 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11394 declared `friend'. If IS_DECLARATION is TRUE, then this
11395 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11396 something is being declared.
11398 Returns the TYPE specified. */
11401 cp_parser_elaborated_type_specifier (cp_parser* parser,
11403 bool is_declaration)
11405 enum tag_types tag_type;
11407 tree type = NULL_TREE;
11408 tree attributes = NULL_TREE;
11409 cp_token *token = NULL;
11411 /* See if we're looking at the `enum' keyword. */
11412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11414 /* Consume the `enum' token. */
11415 cp_lexer_consume_token (parser->lexer);
11416 /* Remember that it's an enumeration type. */
11417 tag_type = enum_type;
11418 /* Parse the optional `struct' or `class' key (for C++0x scoped
11420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11421 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11423 if (cxx_dialect == cxx98)
11424 maybe_warn_cpp0x ("scoped enums");
11426 /* Consume the `struct' or `class'. */
11427 cp_lexer_consume_token (parser->lexer);
11429 /* Parse the attributes. */
11430 attributes = cp_parser_attributes_opt (parser);
11432 /* Or, it might be `typename'. */
11433 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11436 /* Consume the `typename' token. */
11437 cp_lexer_consume_token (parser->lexer);
11438 /* Remember that it's a `typename' type. */
11439 tag_type = typename_type;
11440 /* The `typename' keyword is only allowed in templates. */
11441 if (!processing_template_decl)
11442 permerror (input_location, "using %<typename%> outside of template");
11444 /* Otherwise it must be a class-key. */
11447 tag_type = cp_parser_class_key (parser);
11448 if (tag_type == none_type)
11449 return error_mark_node;
11450 /* Parse the attributes. */
11451 attributes = cp_parser_attributes_opt (parser);
11454 /* Look for the `::' operator. */
11455 cp_parser_global_scope_opt (parser,
11456 /*current_scope_valid_p=*/false);
11457 /* Look for the nested-name-specifier. */
11458 if (tag_type == typename_type)
11460 if (!cp_parser_nested_name_specifier (parser,
11461 /*typename_keyword_p=*/true,
11462 /*check_dependency_p=*/true,
11465 return error_mark_node;
11468 /* Even though `typename' is not present, the proposed resolution
11469 to Core Issue 180 says that in `class A<T>::B', `B' should be
11470 considered a type-name, even if `A<T>' is dependent. */
11471 cp_parser_nested_name_specifier_opt (parser,
11472 /*typename_keyword_p=*/true,
11473 /*check_dependency_p=*/true,
11476 /* For everything but enumeration types, consider a template-id.
11477 For an enumeration type, consider only a plain identifier. */
11478 if (tag_type != enum_type)
11480 bool template_p = false;
11483 /* Allow the `template' keyword. */
11484 template_p = cp_parser_optional_template_keyword (parser);
11485 /* If we didn't see `template', we don't know if there's a
11486 template-id or not. */
11488 cp_parser_parse_tentatively (parser);
11489 /* Parse the template-id. */
11490 token = cp_lexer_peek_token (parser->lexer);
11491 decl = cp_parser_template_id (parser, template_p,
11492 /*check_dependency_p=*/true,
11494 /* If we didn't find a template-id, look for an ordinary
11496 if (!template_p && !cp_parser_parse_definitely (parser))
11498 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11499 in effect, then we must assume that, upon instantiation, the
11500 template will correspond to a class. */
11501 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11502 && tag_type == typename_type)
11503 type = make_typename_type (parser->scope, decl,
11505 /*complain=*/tf_error);
11507 type = TREE_TYPE (decl);
11512 token = cp_lexer_peek_token (parser->lexer);
11513 identifier = cp_parser_identifier (parser);
11515 if (identifier == error_mark_node)
11517 parser->scope = NULL_TREE;
11518 return error_mark_node;
11521 /* For a `typename', we needn't call xref_tag. */
11522 if (tag_type == typename_type
11523 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11524 return cp_parser_make_typename_type (parser, parser->scope,
11527 /* Look up a qualified name in the usual way. */
11531 tree ambiguous_decls;
11533 decl = cp_parser_lookup_name (parser, identifier,
11535 /*is_template=*/false,
11536 /*is_namespace=*/false,
11537 /*check_dependency=*/true,
11541 /* If the lookup was ambiguous, an error will already have been
11543 if (ambiguous_decls)
11544 return error_mark_node;
11546 /* If we are parsing friend declaration, DECL may be a
11547 TEMPLATE_DECL tree node here. However, we need to check
11548 whether this TEMPLATE_DECL results in valid code. Consider
11549 the following example:
11552 template <class T> class C {};
11555 template <class T> friend class N::C; // #1, valid code
11557 template <class T> class Y {
11558 friend class N::C; // #2, invalid code
11561 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11562 name lookup of `N::C'. We see that friend declaration must
11563 be template for the code to be valid. Note that
11564 processing_template_decl does not work here since it is
11565 always 1 for the above two cases. */
11567 decl = (cp_parser_maybe_treat_template_as_class
11568 (decl, /*tag_name_p=*/is_friend
11569 && parser->num_template_parameter_lists));
11571 if (TREE_CODE (decl) != TYPE_DECL)
11573 cp_parser_diagnose_invalid_type_name (parser,
11577 return error_mark_node;
11580 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11582 bool allow_template = (parser->num_template_parameter_lists
11583 || DECL_SELF_REFERENCE_P (decl));
11584 type = check_elaborated_type_specifier (tag_type, decl,
11587 if (type == error_mark_node)
11588 return error_mark_node;
11591 /* Forward declarations of nested types, such as
11596 are invalid unless all components preceding the final '::'
11597 are complete. If all enclosing types are complete, these
11598 declarations become merely pointless.
11600 Invalid forward declarations of nested types are errors
11601 caught elsewhere in parsing. Those that are pointless arrive
11604 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11605 && !is_friend && !processing_explicit_instantiation)
11606 warning (0, "declaration %qD does not declare anything", decl);
11608 type = TREE_TYPE (decl);
11612 /* An elaborated-type-specifier sometimes introduces a new type and
11613 sometimes names an existing type. Normally, the rule is that it
11614 introduces a new type only if there is not an existing type of
11615 the same name already in scope. For example, given:
11618 void f() { struct S s; }
11620 the `struct S' in the body of `f' is the same `struct S' as in
11621 the global scope; the existing definition is used. However, if
11622 there were no global declaration, this would introduce a new
11623 local class named `S'.
11625 An exception to this rule applies to the following code:
11627 namespace N { struct S; }
11629 Here, the elaborated-type-specifier names a new type
11630 unconditionally; even if there is already an `S' in the
11631 containing scope this declaration names a new type.
11632 This exception only applies if the elaborated-type-specifier
11633 forms the complete declaration:
11637 A declaration consisting solely of `class-key identifier ;' is
11638 either a redeclaration of the name in the current scope or a
11639 forward declaration of the identifier as a class name. It
11640 introduces the name into the current scope.
11642 We are in this situation precisely when the next token is a `;'.
11644 An exception to the exception is that a `friend' declaration does
11645 *not* name a new type; i.e., given:
11647 struct S { friend struct T; };
11649 `T' is not a new type in the scope of `S'.
11651 Also, `new struct S' or `sizeof (struct S)' never results in the
11652 definition of a new type; a new type can only be declared in a
11653 declaration context. */
11659 /* Friends have special name lookup rules. */
11660 ts = ts_within_enclosing_non_class;
11661 else if (is_declaration
11662 && cp_lexer_next_token_is (parser->lexer,
11664 /* This is a `class-key identifier ;' */
11670 (parser->num_template_parameter_lists
11671 && (cp_parser_next_token_starts_class_definition_p (parser)
11672 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11673 /* An unqualified name was used to reference this type, so
11674 there were no qualifying templates. */
11675 if (!cp_parser_check_template_parameters (parser,
11676 /*num_templates=*/0,
11678 return error_mark_node;
11679 type = xref_tag (tag_type, identifier, ts, template_p);
11683 if (type == error_mark_node)
11684 return error_mark_node;
11686 /* Allow attributes on forward declarations of classes. */
11689 if (TREE_CODE (type) == TYPENAME_TYPE)
11690 warning (OPT_Wattributes,
11691 "attributes ignored on uninstantiated type");
11692 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11693 && ! processing_explicit_instantiation)
11694 warning (OPT_Wattributes,
11695 "attributes ignored on template instantiation");
11696 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11697 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11699 warning (OPT_Wattributes,
11700 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11703 if (tag_type != enum_type)
11704 cp_parser_check_class_key (tag_type, type);
11706 /* A "<" cannot follow an elaborated type specifier. If that
11707 happens, the user was probably trying to form a template-id. */
11708 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11713 /* Parse an enum-specifier.
11716 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11721 enum struct [C++0x]
11724 : type-specifier-seq
11727 enum-key attributes[opt] identifier [opt] enum-base [opt]
11728 { enumerator-list [opt] }attributes[opt]
11730 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11731 if the token stream isn't an enum-specifier after all. */
11734 cp_parser_enum_specifier (cp_parser* parser)
11739 bool scoped_enum_p = false;
11740 bool has_underlying_type = false;
11741 tree underlying_type = NULL_TREE;
11743 /* Parse tentatively so that we can back up if we don't find a
11745 cp_parser_parse_tentatively (parser);
11747 /* Caller guarantees that the current token is 'enum', an identifier
11748 possibly follows, and the token after that is an opening brace.
11749 If we don't have an identifier, fabricate an anonymous name for
11750 the enumeration being defined. */
11751 cp_lexer_consume_token (parser->lexer);
11753 /* Parse the "class" or "struct", which indicates a scoped
11754 enumeration type in C++0x. */
11755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11756 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11758 if (cxx_dialect == cxx98)
11759 maybe_warn_cpp0x ("scoped enums");
11761 /* Consume the `struct' or `class' token. */
11762 cp_lexer_consume_token (parser->lexer);
11764 scoped_enum_p = true;
11767 attributes = cp_parser_attributes_opt (parser);
11769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11770 identifier = cp_parser_identifier (parser);
11772 identifier = make_anon_name ();
11774 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11775 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11777 cp_decl_specifier_seq type_specifiers;
11779 /* At this point this is surely not elaborated type specifier. */
11780 if (!cp_parser_parse_definitely (parser))
11783 if (cxx_dialect == cxx98)
11784 maybe_warn_cpp0x ("scoped enums");
11786 /* Consume the `:'. */
11787 cp_lexer_consume_token (parser->lexer);
11789 has_underlying_type = true;
11791 /* Parse the type-specifier-seq. */
11792 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11795 /* If that didn't work, stop. */
11796 if (type_specifiers.type != error_mark_node)
11798 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11799 /*initialized=*/0, NULL);
11800 if (underlying_type == error_mark_node)
11801 underlying_type = NULL_TREE;
11805 /* Look for the `{' but don't consume it yet. */
11806 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11808 cp_parser_error (parser, "expected %<{%>");
11809 if (has_underlying_type)
11813 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11816 /* Issue an error message if type-definitions are forbidden here. */
11817 if (!cp_parser_check_type_definition (parser))
11818 type = error_mark_node;
11820 /* Create the new type. We do this before consuming the opening
11821 brace so the enum will be recorded as being on the line of its
11822 tag (or the 'enum' keyword, if there is no tag). */
11823 type = start_enum (identifier, underlying_type, scoped_enum_p);
11825 /* Consume the opening brace. */
11826 cp_lexer_consume_token (parser->lexer);
11828 if (type == error_mark_node)
11830 cp_parser_skip_to_end_of_block_or_statement (parser);
11831 return error_mark_node;
11834 /* If the next token is not '}', then there are some enumerators. */
11835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11836 cp_parser_enumerator_list (parser, type);
11838 /* Consume the final '}'. */
11839 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11841 /* Look for trailing attributes to apply to this enumeration, and
11842 apply them if appropriate. */
11843 if (cp_parser_allow_gnu_extensions_p (parser))
11845 tree trailing_attr = cp_parser_attributes_opt (parser);
11846 cplus_decl_attributes (&type,
11848 (int) ATTR_FLAG_TYPE_IN_PLACE);
11851 /* Finish up the enumeration. */
11852 finish_enum (type);
11857 /* Parse an enumerator-list. The enumerators all have the indicated
11861 enumerator-definition
11862 enumerator-list , enumerator-definition */
11865 cp_parser_enumerator_list (cp_parser* parser, tree type)
11869 /* Parse an enumerator-definition. */
11870 cp_parser_enumerator_definition (parser, type);
11872 /* If the next token is not a ',', we've reached the end of
11874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11876 /* Otherwise, consume the `,' and keep going. */
11877 cp_lexer_consume_token (parser->lexer);
11878 /* If the next token is a `}', there is a trailing comma. */
11879 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11881 if (!in_system_header)
11882 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11888 /* Parse an enumerator-definition. The enumerator has the indicated
11891 enumerator-definition:
11893 enumerator = constant-expression
11899 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11904 /* Look for the identifier. */
11905 identifier = cp_parser_identifier (parser);
11906 if (identifier == error_mark_node)
11909 /* If the next token is an '=', then there is an explicit value. */
11910 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11912 /* Consume the `=' token. */
11913 cp_lexer_consume_token (parser->lexer);
11914 /* Parse the value. */
11915 value = cp_parser_constant_expression (parser,
11916 /*allow_non_constant_p=*/false,
11922 /* Create the enumerator. */
11923 build_enumerator (identifier, value, type);
11926 /* Parse a namespace-name.
11929 original-namespace-name
11932 Returns the NAMESPACE_DECL for the namespace. */
11935 cp_parser_namespace_name (cp_parser* parser)
11938 tree namespace_decl;
11940 cp_token *token = cp_lexer_peek_token (parser->lexer);
11942 /* Get the name of the namespace. */
11943 identifier = cp_parser_identifier (parser);
11944 if (identifier == error_mark_node)
11945 return error_mark_node;
11947 /* Look up the identifier in the currently active scope. Look only
11948 for namespaces, due to:
11950 [basic.lookup.udir]
11952 When looking up a namespace-name in a using-directive or alias
11953 definition, only namespace names are considered.
11957 [basic.lookup.qual]
11959 During the lookup of a name preceding the :: scope resolution
11960 operator, object, function, and enumerator names are ignored.
11962 (Note that cp_parser_qualifying_entity only calls this
11963 function if the token after the name is the scope resolution
11965 namespace_decl = cp_parser_lookup_name (parser, identifier,
11967 /*is_template=*/false,
11968 /*is_namespace=*/true,
11969 /*check_dependency=*/true,
11970 /*ambiguous_decls=*/NULL,
11972 /* If it's not a namespace, issue an error. */
11973 if (namespace_decl == error_mark_node
11974 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11976 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11977 error ("%H%qD is not a namespace-name", &token->location, identifier);
11978 cp_parser_error (parser, "expected namespace-name");
11979 namespace_decl = error_mark_node;
11982 return namespace_decl;
11985 /* Parse a namespace-definition.
11987 namespace-definition:
11988 named-namespace-definition
11989 unnamed-namespace-definition
11991 named-namespace-definition:
11992 original-namespace-definition
11993 extension-namespace-definition
11995 original-namespace-definition:
11996 namespace identifier { namespace-body }
11998 extension-namespace-definition:
11999 namespace original-namespace-name { namespace-body }
12001 unnamed-namespace-definition:
12002 namespace { namespace-body } */
12005 cp_parser_namespace_definition (cp_parser* parser)
12007 tree identifier, attribs;
12008 bool has_visibility;
12011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12014 cp_lexer_consume_token (parser->lexer);
12019 /* Look for the `namespace' keyword. */
12020 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12022 /* Get the name of the namespace. We do not attempt to distinguish
12023 between an original-namespace-definition and an
12024 extension-namespace-definition at this point. The semantic
12025 analysis routines are responsible for that. */
12026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12027 identifier = cp_parser_identifier (parser);
12029 identifier = NULL_TREE;
12031 /* Parse any specified attributes. */
12032 attribs = cp_parser_attributes_opt (parser);
12034 /* Look for the `{' to start the namespace. */
12035 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12036 /* Start the namespace. */
12037 push_namespace (identifier);
12039 /* "inline namespace" is equivalent to a stub namespace definition
12040 followed by a strong using directive. */
12043 tree name_space = current_namespace;
12044 /* Set up namespace association. */
12045 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12046 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12047 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12048 /* Import the contents of the inline namespace. */
12050 do_using_directive (name_space);
12051 push_namespace (identifier);
12054 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12056 /* Parse the body of the namespace. */
12057 cp_parser_namespace_body (parser);
12059 #ifdef HANDLE_PRAGMA_VISIBILITY
12060 if (has_visibility)
12064 /* Finish the namespace. */
12066 /* Look for the final `}'. */
12067 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12070 /* Parse a namespace-body.
12073 declaration-seq [opt] */
12076 cp_parser_namespace_body (cp_parser* parser)
12078 cp_parser_declaration_seq_opt (parser);
12081 /* Parse a namespace-alias-definition.
12083 namespace-alias-definition:
12084 namespace identifier = qualified-namespace-specifier ; */
12087 cp_parser_namespace_alias_definition (cp_parser* parser)
12090 tree namespace_specifier;
12092 cp_token *token = cp_lexer_peek_token (parser->lexer);
12094 /* Look for the `namespace' keyword. */
12095 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12096 /* Look for the identifier. */
12097 identifier = cp_parser_identifier (parser);
12098 if (identifier == error_mark_node)
12100 /* Look for the `=' token. */
12101 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12102 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12104 error ("%H%<namespace%> definition is not allowed here", &token->location);
12105 /* Skip the definition. */
12106 cp_lexer_consume_token (parser->lexer);
12107 if (cp_parser_skip_to_closing_brace (parser))
12108 cp_lexer_consume_token (parser->lexer);
12111 cp_parser_require (parser, CPP_EQ, "%<=%>");
12112 /* Look for the qualified-namespace-specifier. */
12113 namespace_specifier
12114 = cp_parser_qualified_namespace_specifier (parser);
12115 /* Look for the `;' token. */
12116 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12118 /* Register the alias in the symbol table. */
12119 do_namespace_alias (identifier, namespace_specifier);
12122 /* Parse a qualified-namespace-specifier.
12124 qualified-namespace-specifier:
12125 :: [opt] nested-name-specifier [opt] namespace-name
12127 Returns a NAMESPACE_DECL corresponding to the specified
12131 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12133 /* Look for the optional `::'. */
12134 cp_parser_global_scope_opt (parser,
12135 /*current_scope_valid_p=*/false);
12137 /* Look for the optional nested-name-specifier. */
12138 cp_parser_nested_name_specifier_opt (parser,
12139 /*typename_keyword_p=*/false,
12140 /*check_dependency_p=*/true,
12142 /*is_declaration=*/true);
12144 return cp_parser_namespace_name (parser);
12147 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12148 access declaration.
12151 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12152 using :: unqualified-id ;
12154 access-declaration:
12160 cp_parser_using_declaration (cp_parser* parser,
12161 bool access_declaration_p)
12164 bool typename_p = false;
12165 bool global_scope_p;
12170 if (access_declaration_p)
12171 cp_parser_parse_tentatively (parser);
12174 /* Look for the `using' keyword. */
12175 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12177 /* Peek at the next token. */
12178 token = cp_lexer_peek_token (parser->lexer);
12179 /* See if it's `typename'. */
12180 if (token->keyword == RID_TYPENAME)
12182 /* Remember that we've seen it. */
12184 /* Consume the `typename' token. */
12185 cp_lexer_consume_token (parser->lexer);
12189 /* Look for the optional global scope qualification. */
12191 = (cp_parser_global_scope_opt (parser,
12192 /*current_scope_valid_p=*/false)
12195 /* If we saw `typename', or didn't see `::', then there must be a
12196 nested-name-specifier present. */
12197 if (typename_p || !global_scope_p)
12198 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12199 /*check_dependency_p=*/true,
12201 /*is_declaration=*/true);
12202 /* Otherwise, we could be in either of the two productions. In that
12203 case, treat the nested-name-specifier as optional. */
12205 qscope = cp_parser_nested_name_specifier_opt (parser,
12206 /*typename_keyword_p=*/false,
12207 /*check_dependency_p=*/true,
12209 /*is_declaration=*/true);
12211 qscope = global_namespace;
12213 if (access_declaration_p && cp_parser_error_occurred (parser))
12214 /* Something has already gone wrong; there's no need to parse
12215 further. Since an error has occurred, the return value of
12216 cp_parser_parse_definitely will be false, as required. */
12217 return cp_parser_parse_definitely (parser);
12219 token = cp_lexer_peek_token (parser->lexer);
12220 /* Parse the unqualified-id. */
12221 identifier = cp_parser_unqualified_id (parser,
12222 /*template_keyword_p=*/false,
12223 /*check_dependency_p=*/true,
12224 /*declarator_p=*/true,
12225 /*optional_p=*/false);
12227 if (access_declaration_p)
12229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12230 cp_parser_simulate_error (parser);
12231 if (!cp_parser_parse_definitely (parser))
12235 /* The function we call to handle a using-declaration is different
12236 depending on what scope we are in. */
12237 if (qscope == error_mark_node || identifier == error_mark_node)
12239 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12240 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12241 /* [namespace.udecl]
12243 A using declaration shall not name a template-id. */
12244 error ("%Ha template-id may not appear in a using-declaration",
12248 if (at_class_scope_p ())
12250 /* Create the USING_DECL. */
12251 decl = do_class_using_decl (parser->scope, identifier);
12253 if (check_for_bare_parameter_packs (decl))
12256 /* Add it to the list of members in this class. */
12257 finish_member_declaration (decl);
12261 decl = cp_parser_lookup_name_simple (parser,
12264 if (decl == error_mark_node)
12265 cp_parser_name_lookup_error (parser, identifier,
12268 else if (check_for_bare_parameter_packs (decl))
12270 else if (!at_namespace_scope_p ())
12271 do_local_using_decl (decl, qscope, identifier);
12273 do_toplevel_using_decl (decl, qscope, identifier);
12277 /* Look for the final `;'. */
12278 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12283 /* Parse a using-directive.
12286 using namespace :: [opt] nested-name-specifier [opt]
12287 namespace-name ; */
12290 cp_parser_using_directive (cp_parser* parser)
12292 tree namespace_decl;
12295 /* Look for the `using' keyword. */
12296 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12297 /* And the `namespace' keyword. */
12298 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12299 /* Look for the optional `::' operator. */
12300 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12301 /* And the optional nested-name-specifier. */
12302 cp_parser_nested_name_specifier_opt (parser,
12303 /*typename_keyword_p=*/false,
12304 /*check_dependency_p=*/true,
12306 /*is_declaration=*/true);
12307 /* Get the namespace being used. */
12308 namespace_decl = cp_parser_namespace_name (parser);
12309 /* And any specified attributes. */
12310 attribs = cp_parser_attributes_opt (parser);
12311 /* Update the symbol table. */
12312 parse_using_directive (namespace_decl, attribs);
12313 /* Look for the final `;'. */
12314 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12317 /* Parse an asm-definition.
12320 asm ( string-literal ) ;
12325 asm volatile [opt] ( string-literal ) ;
12326 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12327 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12328 : asm-operand-list [opt] ) ;
12329 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12330 : asm-operand-list [opt]
12331 : asm-operand-list [opt] ) ; */
12334 cp_parser_asm_definition (cp_parser* parser)
12337 tree outputs = NULL_TREE;
12338 tree inputs = NULL_TREE;
12339 tree clobbers = NULL_TREE;
12341 bool volatile_p = false;
12342 bool extended_p = false;
12343 bool invalid_inputs_p = false;
12344 bool invalid_outputs_p = false;
12346 /* Look for the `asm' keyword. */
12347 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12348 /* See if the next token is `volatile'. */
12349 if (cp_parser_allow_gnu_extensions_p (parser)
12350 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12352 /* Remember that we saw the `volatile' keyword. */
12354 /* Consume the token. */
12355 cp_lexer_consume_token (parser->lexer);
12357 /* Look for the opening `('. */
12358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12360 /* Look for the string. */
12361 string = cp_parser_string_literal (parser, false, false);
12362 if (string == error_mark_node)
12364 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12365 /*consume_paren=*/true);
12369 /* If we're allowing GNU extensions, check for the extended assembly
12370 syntax. Unfortunately, the `:' tokens need not be separated by
12371 a space in C, and so, for compatibility, we tolerate that here
12372 too. Doing that means that we have to treat the `::' operator as
12374 if (cp_parser_allow_gnu_extensions_p (parser)
12375 && parser->in_function_body
12376 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12377 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12379 bool inputs_p = false;
12380 bool clobbers_p = false;
12382 /* The extended syntax was used. */
12385 /* Look for outputs. */
12386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12388 /* Consume the `:'. */
12389 cp_lexer_consume_token (parser->lexer);
12390 /* Parse the output-operands. */
12391 if (cp_lexer_next_token_is_not (parser->lexer,
12393 && cp_lexer_next_token_is_not (parser->lexer,
12395 && cp_lexer_next_token_is_not (parser->lexer,
12397 outputs = cp_parser_asm_operand_list (parser);
12399 if (outputs == error_mark_node)
12400 invalid_outputs_p = true;
12402 /* If the next token is `::', there are no outputs, and the
12403 next token is the beginning of the inputs. */
12404 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12405 /* The inputs are coming next. */
12408 /* Look for inputs. */
12410 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12412 /* Consume the `:' or `::'. */
12413 cp_lexer_consume_token (parser->lexer);
12414 /* Parse the output-operands. */
12415 if (cp_lexer_next_token_is_not (parser->lexer,
12417 && cp_lexer_next_token_is_not (parser->lexer,
12419 inputs = cp_parser_asm_operand_list (parser);
12421 if (inputs == error_mark_node)
12422 invalid_inputs_p = true;
12424 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12425 /* The clobbers are coming next. */
12428 /* Look for clobbers. */
12430 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12432 /* Consume the `:' or `::'. */
12433 cp_lexer_consume_token (parser->lexer);
12434 /* Parse the clobbers. */
12435 if (cp_lexer_next_token_is_not (parser->lexer,
12437 clobbers = cp_parser_asm_clobber_list (parser);
12440 /* Look for the closing `)'. */
12441 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12442 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12443 /*consume_paren=*/true);
12444 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12446 if (!invalid_inputs_p && !invalid_outputs_p)
12448 /* Create the ASM_EXPR. */
12449 if (parser->in_function_body)
12451 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12453 /* If the extended syntax was not used, mark the ASM_EXPR. */
12456 tree temp = asm_stmt;
12457 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12458 temp = TREE_OPERAND (temp, 0);
12460 ASM_INPUT_P (temp) = 1;
12464 cgraph_add_asm_node (string);
12468 /* Declarators [gram.dcl.decl] */
12470 /* Parse an init-declarator.
12473 declarator initializer [opt]
12478 declarator asm-specification [opt] attributes [opt] initializer [opt]
12480 function-definition:
12481 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12483 decl-specifier-seq [opt] declarator function-try-block
12487 function-definition:
12488 __extension__ function-definition
12490 The DECL_SPECIFIERS apply to this declarator. Returns a
12491 representation of the entity declared. If MEMBER_P is TRUE, then
12492 this declarator appears in a class scope. The new DECL created by
12493 this declarator is returned.
12495 The CHECKS are access checks that should be performed once we know
12496 what entity is being declared (and, therefore, what classes have
12499 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12500 for a function-definition here as well. If the declarator is a
12501 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12502 be TRUE upon return. By that point, the function-definition will
12503 have been completely parsed.
12505 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12509 cp_parser_init_declarator (cp_parser* parser,
12510 cp_decl_specifier_seq *decl_specifiers,
12511 VEC (deferred_access_check,gc)* checks,
12512 bool function_definition_allowed_p,
12514 int declares_class_or_enum,
12515 bool* function_definition_p)
12517 cp_token *token = NULL, *asm_spec_start_token = NULL,
12518 *attributes_start_token = NULL;
12519 cp_declarator *declarator;
12520 tree prefix_attributes;
12522 tree asm_specification;
12524 tree decl = NULL_TREE;
12526 int is_initialized;
12527 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12528 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12530 enum cpp_ttype initialization_kind;
12531 bool is_direct_init = false;
12532 bool is_non_constant_init;
12533 int ctor_dtor_or_conv_p;
12535 tree pushed_scope = NULL;
12537 /* Gather the attributes that were provided with the
12538 decl-specifiers. */
12539 prefix_attributes = decl_specifiers->attributes;
12541 /* Assume that this is not the declarator for a function
12543 if (function_definition_p)
12544 *function_definition_p = false;
12546 /* Defer access checks while parsing the declarator; we cannot know
12547 what names are accessible until we know what is being
12549 resume_deferring_access_checks ();
12551 /* Parse the declarator. */
12552 token = cp_lexer_peek_token (parser->lexer);
12554 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12555 &ctor_dtor_or_conv_p,
12556 /*parenthesized_p=*/NULL,
12557 /*member_p=*/false);
12558 /* Gather up the deferred checks. */
12559 stop_deferring_access_checks ();
12561 /* If the DECLARATOR was erroneous, there's no need to go
12563 if (declarator == cp_error_declarator)
12564 return error_mark_node;
12566 /* Check that the number of template-parameter-lists is OK. */
12567 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12569 return error_mark_node;
12571 if (declares_class_or_enum & 2)
12572 cp_parser_check_for_definition_in_return_type (declarator,
12573 decl_specifiers->type,
12574 decl_specifiers->type_location);
12576 /* Figure out what scope the entity declared by the DECLARATOR is
12577 located in. `grokdeclarator' sometimes changes the scope, so
12578 we compute it now. */
12579 scope = get_scope_of_declarator (declarator);
12581 /* If we're allowing GNU extensions, look for an asm-specification
12583 if (cp_parser_allow_gnu_extensions_p (parser))
12585 /* Look for an asm-specification. */
12586 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12587 asm_specification = cp_parser_asm_specification_opt (parser);
12588 /* And attributes. */
12589 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12590 attributes = cp_parser_attributes_opt (parser);
12594 asm_specification = NULL_TREE;
12595 attributes = NULL_TREE;
12598 /* Peek at the next token. */
12599 token = cp_lexer_peek_token (parser->lexer);
12600 /* Check to see if the token indicates the start of a
12601 function-definition. */
12602 if (function_declarator_p (declarator)
12603 && cp_parser_token_starts_function_definition_p (token))
12605 if (!function_definition_allowed_p)
12607 /* If a function-definition should not appear here, issue an
12609 cp_parser_error (parser,
12610 "a function-definition is not allowed here");
12611 return error_mark_node;
12615 location_t func_brace_location
12616 = cp_lexer_peek_token (parser->lexer)->location;
12618 /* Neither attributes nor an asm-specification are allowed
12619 on a function-definition. */
12620 if (asm_specification)
12621 error ("%Han asm-specification is not allowed "
12622 "on a function-definition",
12623 &asm_spec_start_token->location);
12625 error ("%Hattributes are not allowed on a function-definition",
12626 &attributes_start_token->location);
12627 /* This is a function-definition. */
12628 *function_definition_p = true;
12630 /* Parse the function definition. */
12632 decl = cp_parser_save_member_function_body (parser,
12635 prefix_attributes);
12638 = (cp_parser_function_definition_from_specifiers_and_declarator
12639 (parser, decl_specifiers, prefix_attributes, declarator));
12641 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12643 /* This is where the prologue starts... */
12644 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12645 = func_brace_location;
12654 Only in function declarations for constructors, destructors, and
12655 type conversions can the decl-specifier-seq be omitted.
12657 We explicitly postpone this check past the point where we handle
12658 function-definitions because we tolerate function-definitions
12659 that are missing their return types in some modes. */
12660 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12662 cp_parser_error (parser,
12663 "expected constructor, destructor, or type conversion");
12664 return error_mark_node;
12667 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12668 if (token->type == CPP_EQ
12669 || token->type == CPP_OPEN_PAREN
12670 || token->type == CPP_OPEN_BRACE)
12672 is_initialized = SD_INITIALIZED;
12673 initialization_kind = token->type;
12675 if (token->type == CPP_EQ
12676 && function_declarator_p (declarator))
12678 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12679 if (t2->keyword == RID_DEFAULT)
12680 is_initialized = SD_DEFAULTED;
12681 else if (t2->keyword == RID_DELETE)
12682 is_initialized = SD_DELETED;
12687 /* If the init-declarator isn't initialized and isn't followed by a
12688 `,' or `;', it's not a valid init-declarator. */
12689 if (token->type != CPP_COMMA
12690 && token->type != CPP_SEMICOLON)
12692 cp_parser_error (parser, "expected initializer");
12693 return error_mark_node;
12695 is_initialized = SD_UNINITIALIZED;
12696 initialization_kind = CPP_EOF;
12699 /* Because start_decl has side-effects, we should only call it if we
12700 know we're going ahead. By this point, we know that we cannot
12701 possibly be looking at any other construct. */
12702 cp_parser_commit_to_tentative_parse (parser);
12704 /* If the decl specifiers were bad, issue an error now that we're
12705 sure this was intended to be a declarator. Then continue
12706 declaring the variable(s), as int, to try to cut down on further
12708 if (decl_specifiers->any_specifiers_p
12709 && decl_specifiers->type == error_mark_node)
12711 cp_parser_error (parser, "invalid type in declaration");
12712 decl_specifiers->type = integer_type_node;
12715 /* Check to see whether or not this declaration is a friend. */
12716 friend_p = cp_parser_friend_p (decl_specifiers);
12718 /* Enter the newly declared entry in the symbol table. If we're
12719 processing a declaration in a class-specifier, we wait until
12720 after processing the initializer. */
12723 if (parser->in_unbraced_linkage_specification_p)
12724 decl_specifiers->storage_class = sc_extern;
12725 decl = start_decl (declarator, decl_specifiers,
12726 is_initialized, attributes, prefix_attributes,
12730 /* Enter the SCOPE. That way unqualified names appearing in the
12731 initializer will be looked up in SCOPE. */
12732 pushed_scope = push_scope (scope);
12734 /* Perform deferred access control checks, now that we know in which
12735 SCOPE the declared entity resides. */
12736 if (!member_p && decl)
12738 tree saved_current_function_decl = NULL_TREE;
12740 /* If the entity being declared is a function, pretend that we
12741 are in its scope. If it is a `friend', it may have access to
12742 things that would not otherwise be accessible. */
12743 if (TREE_CODE (decl) == FUNCTION_DECL)
12745 saved_current_function_decl = current_function_decl;
12746 current_function_decl = decl;
12749 /* Perform access checks for template parameters. */
12750 cp_parser_perform_template_parameter_access_checks (checks);
12752 /* Perform the access control checks for the declarator and the
12753 decl-specifiers. */
12754 perform_deferred_access_checks ();
12756 /* Restore the saved value. */
12757 if (TREE_CODE (decl) == FUNCTION_DECL)
12758 current_function_decl = saved_current_function_decl;
12761 /* Parse the initializer. */
12762 initializer = NULL_TREE;
12763 is_direct_init = false;
12764 is_non_constant_init = true;
12765 if (is_initialized)
12767 if (function_declarator_p (declarator))
12769 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12770 if (initialization_kind == CPP_EQ)
12771 initializer = cp_parser_pure_specifier (parser);
12774 /* If the declaration was erroneous, we don't really
12775 know what the user intended, so just silently
12776 consume the initializer. */
12777 if (decl != error_mark_node)
12778 error ("%Hinitializer provided for function",
12779 &initializer_start_token->location);
12780 cp_parser_skip_to_closing_parenthesis (parser,
12781 /*recovering=*/true,
12782 /*or_comma=*/false,
12783 /*consume_paren=*/true);
12787 initializer = cp_parser_initializer (parser,
12789 &is_non_constant_init);
12792 /* The old parser allows attributes to appear after a parenthesized
12793 initializer. Mark Mitchell proposed removing this functionality
12794 on the GCC mailing lists on 2002-08-13. This parser accepts the
12795 attributes -- but ignores them. */
12796 if (cp_parser_allow_gnu_extensions_p (parser)
12797 && initialization_kind == CPP_OPEN_PAREN)
12798 if (cp_parser_attributes_opt (parser))
12799 warning (OPT_Wattributes,
12800 "attributes after parenthesized initializer ignored");
12802 /* For an in-class declaration, use `grokfield' to create the
12808 pop_scope (pushed_scope);
12809 pushed_scope = false;
12811 decl = grokfield (declarator, decl_specifiers,
12812 initializer, !is_non_constant_init,
12813 /*asmspec=*/NULL_TREE,
12814 prefix_attributes);
12815 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12816 cp_parser_save_default_args (parser, decl);
12819 /* Finish processing the declaration. But, skip friend
12821 if (!friend_p && decl && decl != error_mark_node)
12823 cp_finish_decl (decl,
12824 initializer, !is_non_constant_init,
12826 /* If the initializer is in parentheses, then this is
12827 a direct-initialization, which means that an
12828 `explicit' constructor is OK. Otherwise, an
12829 `explicit' constructor cannot be used. */
12830 ((is_direct_init || !is_initialized)
12831 ? 0 : LOOKUP_ONLYCONVERTING));
12833 else if ((cxx_dialect != cxx98) && friend_p
12834 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12835 /* Core issue #226 (C++0x only): A default template-argument
12836 shall not be specified in a friend class template
12838 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12839 /*is_partial=*/0, /*is_friend_decl=*/1);
12841 if (!friend_p && pushed_scope)
12842 pop_scope (pushed_scope);
12847 /* Parse a declarator.
12851 ptr-operator declarator
12853 abstract-declarator:
12854 ptr-operator abstract-declarator [opt]
12855 direct-abstract-declarator
12860 attributes [opt] direct-declarator
12861 attributes [opt] ptr-operator declarator
12863 abstract-declarator:
12864 attributes [opt] ptr-operator abstract-declarator [opt]
12865 attributes [opt] direct-abstract-declarator
12867 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12868 detect constructor, destructor or conversion operators. It is set
12869 to -1 if the declarator is a name, and +1 if it is a
12870 function. Otherwise it is set to zero. Usually you just want to
12871 test for >0, but internally the negative value is used.
12873 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12874 a decl-specifier-seq unless it declares a constructor, destructor,
12875 or conversion. It might seem that we could check this condition in
12876 semantic analysis, rather than parsing, but that makes it difficult
12877 to handle something like `f()'. We want to notice that there are
12878 no decl-specifiers, and therefore realize that this is an
12879 expression, not a declaration.)
12881 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12882 the declarator is a direct-declarator of the form "(...)".
12884 MEMBER_P is true iff this declarator is a member-declarator. */
12886 static cp_declarator *
12887 cp_parser_declarator (cp_parser* parser,
12888 cp_parser_declarator_kind dcl_kind,
12889 int* ctor_dtor_or_conv_p,
12890 bool* parenthesized_p,
12894 cp_declarator *declarator;
12895 enum tree_code code;
12896 cp_cv_quals cv_quals;
12898 tree attributes = NULL_TREE;
12900 /* Assume this is not a constructor, destructor, or type-conversion
12902 if (ctor_dtor_or_conv_p)
12903 *ctor_dtor_or_conv_p = 0;
12905 if (cp_parser_allow_gnu_extensions_p (parser))
12906 attributes = cp_parser_attributes_opt (parser);
12908 /* Peek at the next token. */
12909 token = cp_lexer_peek_token (parser->lexer);
12911 /* Check for the ptr-operator production. */
12912 cp_parser_parse_tentatively (parser);
12913 /* Parse the ptr-operator. */
12914 code = cp_parser_ptr_operator (parser,
12917 /* If that worked, then we have a ptr-operator. */
12918 if (cp_parser_parse_definitely (parser))
12920 /* If a ptr-operator was found, then this declarator was not
12922 if (parenthesized_p)
12923 *parenthesized_p = true;
12924 /* The dependent declarator is optional if we are parsing an
12925 abstract-declarator. */
12926 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12927 cp_parser_parse_tentatively (parser);
12929 /* Parse the dependent declarator. */
12930 declarator = cp_parser_declarator (parser, dcl_kind,
12931 /*ctor_dtor_or_conv_p=*/NULL,
12932 /*parenthesized_p=*/NULL,
12933 /*member_p=*/false);
12935 /* If we are parsing an abstract-declarator, we must handle the
12936 case where the dependent declarator is absent. */
12937 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12938 && !cp_parser_parse_definitely (parser))
12941 declarator = cp_parser_make_indirect_declarator
12942 (code, class_type, cv_quals, declarator);
12944 /* Everything else is a direct-declarator. */
12947 if (parenthesized_p)
12948 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12950 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12951 ctor_dtor_or_conv_p,
12955 if (attributes && declarator && declarator != cp_error_declarator)
12956 declarator->attributes = attributes;
12961 /* Parse a direct-declarator or direct-abstract-declarator.
12965 direct-declarator ( parameter-declaration-clause )
12966 cv-qualifier-seq [opt]
12967 exception-specification [opt]
12968 direct-declarator [ constant-expression [opt] ]
12971 direct-abstract-declarator:
12972 direct-abstract-declarator [opt]
12973 ( parameter-declaration-clause )
12974 cv-qualifier-seq [opt]
12975 exception-specification [opt]
12976 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12977 ( abstract-declarator )
12979 Returns a representation of the declarator. DCL_KIND is
12980 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12981 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12982 we are parsing a direct-declarator. It is
12983 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12984 of ambiguity we prefer an abstract declarator, as per
12985 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12986 cp_parser_declarator. */
12988 static cp_declarator *
12989 cp_parser_direct_declarator (cp_parser* parser,
12990 cp_parser_declarator_kind dcl_kind,
12991 int* ctor_dtor_or_conv_p,
12995 cp_declarator *declarator = NULL;
12996 tree scope = NULL_TREE;
12997 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12998 bool saved_in_declarator_p = parser->in_declarator_p;
13000 tree pushed_scope = NULL_TREE;
13004 /* Peek at the next token. */
13005 token = cp_lexer_peek_token (parser->lexer);
13006 if (token->type == CPP_OPEN_PAREN)
13008 /* This is either a parameter-declaration-clause, or a
13009 parenthesized declarator. When we know we are parsing a
13010 named declarator, it must be a parenthesized declarator
13011 if FIRST is true. For instance, `(int)' is a
13012 parameter-declaration-clause, with an omitted
13013 direct-abstract-declarator. But `((*))', is a
13014 parenthesized abstract declarator. Finally, when T is a
13015 template parameter `(T)' is a
13016 parameter-declaration-clause, and not a parenthesized
13019 We first try and parse a parameter-declaration-clause,
13020 and then try a nested declarator (if FIRST is true).
13022 It is not an error for it not to be a
13023 parameter-declaration-clause, even when FIRST is
13029 The first is the declaration of a function while the
13030 second is the definition of a variable, including its
13033 Having seen only the parenthesis, we cannot know which of
13034 these two alternatives should be selected. Even more
13035 complex are examples like:
13040 The former is a function-declaration; the latter is a
13041 variable initialization.
13043 Thus again, we try a parameter-declaration-clause, and if
13044 that fails, we back out and return. */
13046 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13049 unsigned saved_num_template_parameter_lists;
13050 bool is_declarator = false;
13053 /* In a member-declarator, the only valid interpretation
13054 of a parenthesis is the start of a
13055 parameter-declaration-clause. (It is invalid to
13056 initialize a static data member with a parenthesized
13057 initializer; only the "=" form of initialization is
13060 cp_parser_parse_tentatively (parser);
13062 /* Consume the `('. */
13063 cp_lexer_consume_token (parser->lexer);
13066 /* If this is going to be an abstract declarator, we're
13067 in a declarator and we can't have default args. */
13068 parser->default_arg_ok_p = false;
13069 parser->in_declarator_p = true;
13072 /* Inside the function parameter list, surrounding
13073 template-parameter-lists do not apply. */
13074 saved_num_template_parameter_lists
13075 = parser->num_template_parameter_lists;
13076 parser->num_template_parameter_lists = 0;
13078 begin_scope (sk_function_parms, NULL_TREE);
13080 /* Parse the parameter-declaration-clause. */
13081 params = cp_parser_parameter_declaration_clause (parser);
13083 parser->num_template_parameter_lists
13084 = saved_num_template_parameter_lists;
13086 /* If all went well, parse the cv-qualifier-seq and the
13087 exception-specification. */
13088 if (member_p || cp_parser_parse_definitely (parser))
13090 cp_cv_quals cv_quals;
13091 tree exception_specification;
13094 is_declarator = true;
13096 if (ctor_dtor_or_conv_p)
13097 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13099 /* Consume the `)'. */
13100 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13102 /* Parse the cv-qualifier-seq. */
13103 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13104 /* And the exception-specification. */
13105 exception_specification
13106 = cp_parser_exception_specification_opt (parser);
13109 = cp_parser_late_return_type_opt (parser);
13111 /* Create the function-declarator. */
13112 declarator = make_call_declarator (declarator,
13115 exception_specification,
13117 /* Any subsequent parameter lists are to do with
13118 return type, so are not those of the declared
13120 parser->default_arg_ok_p = false;
13123 /* Remove the function parms from scope. */
13124 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13125 pop_binding (DECL_NAME (t), t);
13129 /* Repeat the main loop. */
13133 /* If this is the first, we can try a parenthesized
13137 bool saved_in_type_id_in_expr_p;
13139 parser->default_arg_ok_p = saved_default_arg_ok_p;
13140 parser->in_declarator_p = saved_in_declarator_p;
13142 /* Consume the `('. */
13143 cp_lexer_consume_token (parser->lexer);
13144 /* Parse the nested declarator. */
13145 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13146 parser->in_type_id_in_expr_p = true;
13148 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13149 /*parenthesized_p=*/NULL,
13151 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13153 /* Expect a `)'. */
13154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13155 declarator = cp_error_declarator;
13156 if (declarator == cp_error_declarator)
13159 goto handle_declarator;
13161 /* Otherwise, we must be done. */
13165 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13166 && token->type == CPP_OPEN_SQUARE)
13168 /* Parse an array-declarator. */
13171 if (ctor_dtor_or_conv_p)
13172 *ctor_dtor_or_conv_p = 0;
13175 parser->default_arg_ok_p = false;
13176 parser->in_declarator_p = true;
13177 /* Consume the `['. */
13178 cp_lexer_consume_token (parser->lexer);
13179 /* Peek at the next token. */
13180 token = cp_lexer_peek_token (parser->lexer);
13181 /* If the next token is `]', then there is no
13182 constant-expression. */
13183 if (token->type != CPP_CLOSE_SQUARE)
13185 bool non_constant_p;
13188 = cp_parser_constant_expression (parser,
13189 /*allow_non_constant=*/true,
13191 if (!non_constant_p)
13192 bounds = fold_non_dependent_expr (bounds);
13193 /* Normally, the array bound must be an integral constant
13194 expression. However, as an extension, we allow VLAs
13195 in function scopes. */
13196 else if (!parser->in_function_body)
13198 error ("%Harray bound is not an integer constant",
13200 bounds = error_mark_node;
13204 bounds = NULL_TREE;
13205 /* Look for the closing `]'. */
13206 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13208 declarator = cp_error_declarator;
13212 declarator = make_array_declarator (declarator, bounds);
13214 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13216 tree qualifying_scope;
13217 tree unqualified_name;
13218 special_function_kind sfk;
13220 bool pack_expansion_p = false;
13221 cp_token *declarator_id_start_token;
13223 /* Parse a declarator-id */
13224 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13227 cp_parser_parse_tentatively (parser);
13229 /* If we see an ellipsis, we should be looking at a
13231 if (token->type == CPP_ELLIPSIS)
13233 /* Consume the `...' */
13234 cp_lexer_consume_token (parser->lexer);
13236 pack_expansion_p = true;
13240 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13242 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13243 qualifying_scope = parser->scope;
13248 if (!unqualified_name && pack_expansion_p)
13250 /* Check whether an error occurred. */
13251 okay = !cp_parser_error_occurred (parser);
13253 /* We already consumed the ellipsis to mark a
13254 parameter pack, but we have no way to report it,
13255 so abort the tentative parse. We will be exiting
13256 immediately anyway. */
13257 cp_parser_abort_tentative_parse (parser);
13260 okay = cp_parser_parse_definitely (parser);
13263 unqualified_name = error_mark_node;
13264 else if (unqualified_name
13265 && (qualifying_scope
13266 || (TREE_CODE (unqualified_name)
13267 != IDENTIFIER_NODE)))
13269 cp_parser_error (parser, "expected unqualified-id");
13270 unqualified_name = error_mark_node;
13274 if (!unqualified_name)
13276 if (unqualified_name == error_mark_node)
13278 declarator = cp_error_declarator;
13279 pack_expansion_p = false;
13280 declarator->parameter_pack_p = false;
13284 if (qualifying_scope && at_namespace_scope_p ()
13285 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13287 /* In the declaration of a member of a template class
13288 outside of the class itself, the SCOPE will sometimes
13289 be a TYPENAME_TYPE. For example, given:
13291 template <typename T>
13292 int S<T>::R::i = 3;
13294 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13295 this context, we must resolve S<T>::R to an ordinary
13296 type, rather than a typename type.
13298 The reason we normally avoid resolving TYPENAME_TYPEs
13299 is that a specialization of `S' might render
13300 `S<T>::R' not a type. However, if `S' is
13301 specialized, then this `i' will not be used, so there
13302 is no harm in resolving the types here. */
13305 /* Resolve the TYPENAME_TYPE. */
13306 type = resolve_typename_type (qualifying_scope,
13307 /*only_current_p=*/false);
13308 /* If that failed, the declarator is invalid. */
13309 if (TREE_CODE (type) == TYPENAME_TYPE)
13310 error ("%H%<%T::%E%> is not a type",
13311 &declarator_id_start_token->location,
13312 TYPE_CONTEXT (qualifying_scope),
13313 TYPE_IDENTIFIER (qualifying_scope));
13314 qualifying_scope = type;
13319 if (unqualified_name)
13323 if (qualifying_scope
13324 && CLASS_TYPE_P (qualifying_scope))
13325 class_type = qualifying_scope;
13327 class_type = current_class_type;
13329 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13331 tree name_type = TREE_TYPE (unqualified_name);
13332 if (class_type && same_type_p (name_type, class_type))
13334 if (qualifying_scope
13335 && CLASSTYPE_USE_TEMPLATE (name_type))
13337 error ("%Hinvalid use of constructor as a template",
13338 &declarator_id_start_token->location);
13339 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13340 "name the constructor in a qualified name",
13342 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13343 class_type, name_type);
13344 declarator = cp_error_declarator;
13348 unqualified_name = constructor_name (class_type);
13352 /* We do not attempt to print the declarator
13353 here because we do not have enough
13354 information about its original syntactic
13356 cp_parser_error (parser, "invalid declarator");
13357 declarator = cp_error_declarator;
13364 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13365 sfk = sfk_destructor;
13366 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13367 sfk = sfk_conversion;
13368 else if (/* There's no way to declare a constructor
13369 for an anonymous type, even if the type
13370 got a name for linkage purposes. */
13371 !TYPE_WAS_ANONYMOUS (class_type)
13372 && constructor_name_p (unqualified_name,
13375 unqualified_name = constructor_name (class_type);
13376 sfk = sfk_constructor;
13379 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13380 *ctor_dtor_or_conv_p = -1;
13383 declarator = make_id_declarator (qualifying_scope,
13386 declarator->id_loc = token->location;
13387 declarator->parameter_pack_p = pack_expansion_p;
13389 if (pack_expansion_p)
13390 maybe_warn_variadic_templates ();
13392 handle_declarator:;
13393 scope = get_scope_of_declarator (declarator);
13395 /* Any names that appear after the declarator-id for a
13396 member are looked up in the containing scope. */
13397 pushed_scope = push_scope (scope);
13398 parser->in_declarator_p = true;
13399 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13400 || (declarator && declarator->kind == cdk_id))
13401 /* Default args are only allowed on function
13403 parser->default_arg_ok_p = saved_default_arg_ok_p;
13405 parser->default_arg_ok_p = false;
13414 /* For an abstract declarator, we might wind up with nothing at this
13415 point. That's an error; the declarator is not optional. */
13417 cp_parser_error (parser, "expected declarator");
13419 /* If we entered a scope, we must exit it now. */
13421 pop_scope (pushed_scope);
13423 parser->default_arg_ok_p = saved_default_arg_ok_p;
13424 parser->in_declarator_p = saved_in_declarator_p;
13429 /* Parse a ptr-operator.
13432 * cv-qualifier-seq [opt]
13434 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13439 & cv-qualifier-seq [opt]
13441 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13442 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13443 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13444 filled in with the TYPE containing the member. *CV_QUALS is
13445 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13446 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13447 Note that the tree codes returned by this function have nothing
13448 to do with the types of trees that will be eventually be created
13449 to represent the pointer or reference type being parsed. They are
13450 just constants with suggestive names. */
13451 static enum tree_code
13452 cp_parser_ptr_operator (cp_parser* parser,
13454 cp_cv_quals *cv_quals)
13456 enum tree_code code = ERROR_MARK;
13459 /* Assume that it's not a pointer-to-member. */
13461 /* And that there are no cv-qualifiers. */
13462 *cv_quals = TYPE_UNQUALIFIED;
13464 /* Peek at the next token. */
13465 token = cp_lexer_peek_token (parser->lexer);
13467 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13468 if (token->type == CPP_MULT)
13469 code = INDIRECT_REF;
13470 else if (token->type == CPP_AND)
13472 else if ((cxx_dialect != cxx98) &&
13473 token->type == CPP_AND_AND) /* C++0x only */
13474 code = NON_LVALUE_EXPR;
13476 if (code != ERROR_MARK)
13478 /* Consume the `*', `&' or `&&'. */
13479 cp_lexer_consume_token (parser->lexer);
13481 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13482 `&', if we are allowing GNU extensions. (The only qualifier
13483 that can legally appear after `&' is `restrict', but that is
13484 enforced during semantic analysis. */
13485 if (code == INDIRECT_REF
13486 || cp_parser_allow_gnu_extensions_p (parser))
13487 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13491 /* Try the pointer-to-member case. */
13492 cp_parser_parse_tentatively (parser);
13493 /* Look for the optional `::' operator. */
13494 cp_parser_global_scope_opt (parser,
13495 /*current_scope_valid_p=*/false);
13496 /* Look for the nested-name specifier. */
13497 token = cp_lexer_peek_token (parser->lexer);
13498 cp_parser_nested_name_specifier (parser,
13499 /*typename_keyword_p=*/false,
13500 /*check_dependency_p=*/true,
13502 /*is_declaration=*/false);
13503 /* If we found it, and the next token is a `*', then we are
13504 indeed looking at a pointer-to-member operator. */
13505 if (!cp_parser_error_occurred (parser)
13506 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13508 /* Indicate that the `*' operator was used. */
13509 code = INDIRECT_REF;
13511 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13512 error ("%H%qD is a namespace", &token->location, parser->scope);
13515 /* The type of which the member is a member is given by the
13517 *type = parser->scope;
13518 /* The next name will not be qualified. */
13519 parser->scope = NULL_TREE;
13520 parser->qualifying_scope = NULL_TREE;
13521 parser->object_scope = NULL_TREE;
13522 /* Look for the optional cv-qualifier-seq. */
13523 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13526 /* If that didn't work we don't have a ptr-operator. */
13527 if (!cp_parser_parse_definitely (parser))
13528 cp_parser_error (parser, "expected ptr-operator");
13534 /* Parse an (optional) cv-qualifier-seq.
13537 cv-qualifier cv-qualifier-seq [opt]
13548 Returns a bitmask representing the cv-qualifiers. */
13551 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13553 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13558 cp_cv_quals cv_qualifier;
13560 /* Peek at the next token. */
13561 token = cp_lexer_peek_token (parser->lexer);
13562 /* See if it's a cv-qualifier. */
13563 switch (token->keyword)
13566 cv_qualifier = TYPE_QUAL_CONST;
13570 cv_qualifier = TYPE_QUAL_VOLATILE;
13574 cv_qualifier = TYPE_QUAL_RESTRICT;
13578 cv_qualifier = TYPE_UNQUALIFIED;
13585 if (cv_quals & cv_qualifier)
13587 error ("%Hduplicate cv-qualifier", &token->location);
13588 cp_lexer_purge_token (parser->lexer);
13592 cp_lexer_consume_token (parser->lexer);
13593 cv_quals |= cv_qualifier;
13600 /* Parse a late-specified return type, if any. This is not a separate
13601 non-terminal, but part of a function declarator, which looks like
13605 Returns the type indicated by the type-id. */
13608 cp_parser_late_return_type_opt (cp_parser* parser)
13612 /* Peek at the next token. */
13613 token = cp_lexer_peek_token (parser->lexer);
13614 /* A late-specified return type is indicated by an initial '->'. */
13615 if (token->type != CPP_DEREF)
13618 /* Consume the ->. */
13619 cp_lexer_consume_token (parser->lexer);
13621 return cp_parser_type_id (parser);
13624 /* Parse a declarator-id.
13628 :: [opt] nested-name-specifier [opt] type-name
13630 In the `id-expression' case, the value returned is as for
13631 cp_parser_id_expression if the id-expression was an unqualified-id.
13632 If the id-expression was a qualified-id, then a SCOPE_REF is
13633 returned. The first operand is the scope (either a NAMESPACE_DECL
13634 or TREE_TYPE), but the second is still just a representation of an
13638 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13641 /* The expression must be an id-expression. Assume that qualified
13642 names are the names of types so that:
13645 int S<T>::R::i = 3;
13647 will work; we must treat `S<T>::R' as the name of a type.
13648 Similarly, assume that qualified names are templates, where
13652 int S<T>::R<T>::i = 3;
13655 id = cp_parser_id_expression (parser,
13656 /*template_keyword_p=*/false,
13657 /*check_dependency_p=*/false,
13658 /*template_p=*/NULL,
13659 /*declarator_p=*/true,
13661 if (id && BASELINK_P (id))
13662 id = BASELINK_FUNCTIONS (id);
13666 /* Parse a type-id.
13669 type-specifier-seq abstract-declarator [opt]
13671 Returns the TYPE specified. */
13674 cp_parser_type_id (cp_parser* parser)
13676 cp_decl_specifier_seq type_specifier_seq;
13677 cp_declarator *abstract_declarator;
13679 /* Parse the type-specifier-seq. */
13680 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13681 &type_specifier_seq);
13682 if (type_specifier_seq.type == error_mark_node)
13683 return error_mark_node;
13685 /* There might or might not be an abstract declarator. */
13686 cp_parser_parse_tentatively (parser);
13687 /* Look for the declarator. */
13688 abstract_declarator
13689 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13690 /*parenthesized_p=*/NULL,
13691 /*member_p=*/false);
13692 /* Check to see if there really was a declarator. */
13693 if (!cp_parser_parse_definitely (parser))
13694 abstract_declarator = NULL;
13696 return groktypename (&type_specifier_seq, abstract_declarator);
13699 /* Parse a type-specifier-seq.
13701 type-specifier-seq:
13702 type-specifier type-specifier-seq [opt]
13706 type-specifier-seq:
13707 attributes type-specifier-seq [opt]
13709 If IS_CONDITION is true, we are at the start of a "condition",
13710 e.g., we've just seen "if (".
13712 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13715 cp_parser_type_specifier_seq (cp_parser* parser,
13717 cp_decl_specifier_seq *type_specifier_seq)
13719 bool seen_type_specifier = false;
13720 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13721 cp_token *start_token = NULL;
13723 /* Clear the TYPE_SPECIFIER_SEQ. */
13724 clear_decl_specs (type_specifier_seq);
13726 /* Parse the type-specifiers and attributes. */
13729 tree type_specifier;
13730 bool is_cv_qualifier;
13732 /* Check for attributes first. */
13733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13735 type_specifier_seq->attributes =
13736 chainon (type_specifier_seq->attributes,
13737 cp_parser_attributes_opt (parser));
13741 /* record the token of the beginning of the type specifier seq,
13742 for error reporting purposes*/
13744 start_token = cp_lexer_peek_token (parser->lexer);
13746 /* Look for the type-specifier. */
13747 type_specifier = cp_parser_type_specifier (parser,
13749 type_specifier_seq,
13750 /*is_declaration=*/false,
13753 if (!type_specifier)
13755 /* If the first type-specifier could not be found, this is not a
13756 type-specifier-seq at all. */
13757 if (!seen_type_specifier)
13759 cp_parser_error (parser, "expected type-specifier");
13760 type_specifier_seq->type = error_mark_node;
13763 /* If subsequent type-specifiers could not be found, the
13764 type-specifier-seq is complete. */
13768 seen_type_specifier = true;
13769 /* The standard says that a condition can be:
13771 type-specifier-seq declarator = assignment-expression
13778 we should treat the "S" as a declarator, not as a
13779 type-specifier. The standard doesn't say that explicitly for
13780 type-specifier-seq, but it does say that for
13781 decl-specifier-seq in an ordinary declaration. Perhaps it
13782 would be clearer just to allow a decl-specifier-seq here, and
13783 then add a semantic restriction that if any decl-specifiers
13784 that are not type-specifiers appear, the program is invalid. */
13785 if (is_condition && !is_cv_qualifier)
13786 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13789 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13792 /* Parse a parameter-declaration-clause.
13794 parameter-declaration-clause:
13795 parameter-declaration-list [opt] ... [opt]
13796 parameter-declaration-list , ...
13798 Returns a representation for the parameter declarations. A return
13799 value of NULL indicates a parameter-declaration-clause consisting
13800 only of an ellipsis. */
13803 cp_parser_parameter_declaration_clause (cp_parser* parser)
13810 /* Peek at the next token. */
13811 token = cp_lexer_peek_token (parser->lexer);
13812 /* Check for trivial parameter-declaration-clauses. */
13813 if (token->type == CPP_ELLIPSIS)
13815 /* Consume the `...' token. */
13816 cp_lexer_consume_token (parser->lexer);
13819 else if (token->type == CPP_CLOSE_PAREN)
13820 /* There are no parameters. */
13822 #ifndef NO_IMPLICIT_EXTERN_C
13823 if (in_system_header && current_class_type == NULL
13824 && current_lang_name == lang_name_c)
13828 return void_list_node;
13830 /* Check for `(void)', too, which is a special case. */
13831 else if (token->keyword == RID_VOID
13832 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13833 == CPP_CLOSE_PAREN))
13835 /* Consume the `void' token. */
13836 cp_lexer_consume_token (parser->lexer);
13837 /* There are no parameters. */
13838 return void_list_node;
13841 /* Parse the parameter-declaration-list. */
13842 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13843 /* If a parse error occurred while parsing the
13844 parameter-declaration-list, then the entire
13845 parameter-declaration-clause is erroneous. */
13849 /* Peek at the next token. */
13850 token = cp_lexer_peek_token (parser->lexer);
13851 /* If it's a `,', the clause should terminate with an ellipsis. */
13852 if (token->type == CPP_COMMA)
13854 /* Consume the `,'. */
13855 cp_lexer_consume_token (parser->lexer);
13856 /* Expect an ellipsis. */
13858 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13860 /* It might also be `...' if the optional trailing `,' was
13862 else if (token->type == CPP_ELLIPSIS)
13864 /* Consume the `...' token. */
13865 cp_lexer_consume_token (parser->lexer);
13866 /* And remember that we saw it. */
13870 ellipsis_p = false;
13872 /* Finish the parameter list. */
13874 parameters = chainon (parameters, void_list_node);
13879 /* Parse a parameter-declaration-list.
13881 parameter-declaration-list:
13882 parameter-declaration
13883 parameter-declaration-list , parameter-declaration
13885 Returns a representation of the parameter-declaration-list, as for
13886 cp_parser_parameter_declaration_clause. However, the
13887 `void_list_node' is never appended to the list. Upon return,
13888 *IS_ERROR will be true iff an error occurred. */
13891 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13893 tree parameters = NULL_TREE;
13894 tree *tail = ¶meters;
13895 bool saved_in_unbraced_linkage_specification_p;
13897 /* Assume all will go well. */
13899 /* The special considerations that apply to a function within an
13900 unbraced linkage specifications do not apply to the parameters
13901 to the function. */
13902 saved_in_unbraced_linkage_specification_p
13903 = parser->in_unbraced_linkage_specification_p;
13904 parser->in_unbraced_linkage_specification_p = false;
13906 /* Look for more parameters. */
13909 cp_parameter_declarator *parameter;
13910 tree decl = error_mark_node;
13911 bool parenthesized_p;
13912 /* Parse the parameter. */
13914 = cp_parser_parameter_declaration (parser,
13915 /*template_parm_p=*/false,
13918 /* We don't know yet if the enclosing context is deprecated, so wait
13919 and warn in grokparms if appropriate. */
13920 deprecated_state = DEPRECATED_SUPPRESS;
13923 decl = grokdeclarator (parameter->declarator,
13924 ¶meter->decl_specifiers,
13926 parameter->default_argument != NULL_TREE,
13927 ¶meter->decl_specifiers.attributes);
13929 deprecated_state = DEPRECATED_NORMAL;
13931 /* If a parse error occurred parsing the parameter declaration,
13932 then the entire parameter-declaration-list is erroneous. */
13933 if (decl == error_mark_node)
13936 parameters = error_mark_node;
13940 if (parameter->decl_specifiers.attributes)
13941 cplus_decl_attributes (&decl,
13942 parameter->decl_specifiers.attributes,
13944 if (DECL_NAME (decl))
13945 decl = pushdecl (decl);
13947 /* Add the new parameter to the list. */
13948 *tail = build_tree_list (parameter->default_argument, decl);
13949 tail = &TREE_CHAIN (*tail);
13951 /* Peek at the next token. */
13952 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13953 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13954 /* These are for Objective-C++ */
13955 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13956 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13957 /* The parameter-declaration-list is complete. */
13959 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13963 /* Peek at the next token. */
13964 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13965 /* If it's an ellipsis, then the list is complete. */
13966 if (token->type == CPP_ELLIPSIS)
13968 /* Otherwise, there must be more parameters. Consume the
13970 cp_lexer_consume_token (parser->lexer);
13971 /* When parsing something like:
13973 int i(float f, double d)
13975 we can tell after seeing the declaration for "f" that we
13976 are not looking at an initialization of a variable "i",
13977 but rather at the declaration of a function "i".
13979 Due to the fact that the parsing of template arguments
13980 (as specified to a template-id) requires backtracking we
13981 cannot use this technique when inside a template argument
13983 if (!parser->in_template_argument_list_p
13984 && !parser->in_type_id_in_expr_p
13985 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13986 /* However, a parameter-declaration of the form
13987 "foat(f)" (which is a valid declaration of a
13988 parameter "f") can also be interpreted as an
13989 expression (the conversion of "f" to "float"). */
13990 && !parenthesized_p)
13991 cp_parser_commit_to_tentative_parse (parser);
13995 cp_parser_error (parser, "expected %<,%> or %<...%>");
13996 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13997 cp_parser_skip_to_closing_parenthesis (parser,
13998 /*recovering=*/true,
13999 /*or_comma=*/false,
14000 /*consume_paren=*/false);
14005 parser->in_unbraced_linkage_specification_p
14006 = saved_in_unbraced_linkage_specification_p;
14011 /* Parse a parameter declaration.
14013 parameter-declaration:
14014 decl-specifier-seq ... [opt] declarator
14015 decl-specifier-seq declarator = assignment-expression
14016 decl-specifier-seq ... [opt] abstract-declarator [opt]
14017 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14019 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14020 declares a template parameter. (In that case, a non-nested `>'
14021 token encountered during the parsing of the assignment-expression
14022 is not interpreted as a greater-than operator.)
14024 Returns a representation of the parameter, or NULL if an error
14025 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14026 true iff the declarator is of the form "(p)". */
14028 static cp_parameter_declarator *
14029 cp_parser_parameter_declaration (cp_parser *parser,
14030 bool template_parm_p,
14031 bool *parenthesized_p)
14033 int declares_class_or_enum;
14034 bool greater_than_is_operator_p;
14035 cp_decl_specifier_seq decl_specifiers;
14036 cp_declarator *declarator;
14037 tree default_argument;
14038 cp_token *token = NULL, *declarator_token_start = NULL;
14039 const char *saved_message;
14041 /* In a template parameter, `>' is not an operator.
14045 When parsing a default template-argument for a non-type
14046 template-parameter, the first non-nested `>' is taken as the end
14047 of the template parameter-list rather than a greater-than
14049 greater_than_is_operator_p = !template_parm_p;
14051 /* Type definitions may not appear in parameter types. */
14052 saved_message = parser->type_definition_forbidden_message;
14053 parser->type_definition_forbidden_message
14054 = "types may not be defined in parameter types";
14056 /* Parse the declaration-specifiers. */
14057 cp_parser_decl_specifier_seq (parser,
14058 CP_PARSER_FLAGS_NONE,
14060 &declares_class_or_enum);
14061 /* If an error occurred, there's no reason to attempt to parse the
14062 rest of the declaration. */
14063 if (cp_parser_error_occurred (parser))
14065 parser->type_definition_forbidden_message = saved_message;
14069 /* Peek at the next token. */
14070 token = cp_lexer_peek_token (parser->lexer);
14072 /* If the next token is a `)', `,', `=', `>', or `...', then there
14073 is no declarator. However, when variadic templates are enabled,
14074 there may be a declarator following `...'. */
14075 if (token->type == CPP_CLOSE_PAREN
14076 || token->type == CPP_COMMA
14077 || token->type == CPP_EQ
14078 || token->type == CPP_GREATER)
14081 if (parenthesized_p)
14082 *parenthesized_p = false;
14084 /* Otherwise, there should be a declarator. */
14087 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14088 parser->default_arg_ok_p = false;
14090 /* After seeing a decl-specifier-seq, if the next token is not a
14091 "(", there is no possibility that the code is a valid
14092 expression. Therefore, if parsing tentatively, we commit at
14094 if (!parser->in_template_argument_list_p
14095 /* In an expression context, having seen:
14099 we cannot be sure whether we are looking at a
14100 function-type (taking a "char" as a parameter) or a cast
14101 of some object of type "char" to "int". */
14102 && !parser->in_type_id_in_expr_p
14103 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14104 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14105 cp_parser_commit_to_tentative_parse (parser);
14106 /* Parse the declarator. */
14107 declarator_token_start = token;
14108 declarator = cp_parser_declarator (parser,
14109 CP_PARSER_DECLARATOR_EITHER,
14110 /*ctor_dtor_or_conv_p=*/NULL,
14112 /*member_p=*/false);
14113 parser->default_arg_ok_p = saved_default_arg_ok_p;
14114 /* After the declarator, allow more attributes. */
14115 decl_specifiers.attributes
14116 = chainon (decl_specifiers.attributes,
14117 cp_parser_attributes_opt (parser));
14120 /* If the next token is an ellipsis, and we have not seen a
14121 declarator name, and the type of the declarator contains parameter
14122 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14123 a parameter pack expansion expression. Otherwise, leave the
14124 ellipsis for a C-style variadic function. */
14125 token = cp_lexer_peek_token (parser->lexer);
14126 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14128 tree type = decl_specifiers.type;
14130 if (type && DECL_P (type))
14131 type = TREE_TYPE (type);
14134 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14135 && declarator_can_be_parameter_pack (declarator)
14136 && (!declarator || !declarator->parameter_pack_p)
14137 && uses_parameter_packs (type))
14139 /* Consume the `...'. */
14140 cp_lexer_consume_token (parser->lexer);
14141 maybe_warn_variadic_templates ();
14143 /* Build a pack expansion type */
14145 declarator->parameter_pack_p = true;
14147 decl_specifiers.type = make_pack_expansion (type);
14151 /* The restriction on defining new types applies only to the type
14152 of the parameter, not to the default argument. */
14153 parser->type_definition_forbidden_message = saved_message;
14155 /* If the next token is `=', then process a default argument. */
14156 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14158 /* Consume the `='. */
14159 cp_lexer_consume_token (parser->lexer);
14161 /* If we are defining a class, then the tokens that make up the
14162 default argument must be saved and processed later. */
14163 if (!template_parm_p && at_class_scope_p ()
14164 && TYPE_BEING_DEFINED (current_class_type))
14166 unsigned depth = 0;
14167 int maybe_template_id = 0;
14168 cp_token *first_token;
14171 /* Add tokens until we have processed the entire default
14172 argument. We add the range [first_token, token). */
14173 first_token = cp_lexer_peek_token (parser->lexer);
14178 /* Peek at the next token. */
14179 token = cp_lexer_peek_token (parser->lexer);
14180 /* What we do depends on what token we have. */
14181 switch (token->type)
14183 /* In valid code, a default argument must be
14184 immediately followed by a `,' `)', or `...'. */
14186 if (depth == 0 && maybe_template_id)
14188 /* If we've seen a '<', we might be in a
14189 template-argument-list. Until Core issue 325 is
14190 resolved, we don't know how this situation ought
14191 to be handled, so try to DTRT. We check whether
14192 what comes after the comma is a valid parameter
14193 declaration list. If it is, then the comma ends
14194 the default argument; otherwise the default
14195 argument continues. */
14196 bool error = false;
14198 /* Set ITALP so cp_parser_parameter_declaration_list
14199 doesn't decide to commit to this parse. */
14200 bool saved_italp = parser->in_template_argument_list_p;
14201 parser->in_template_argument_list_p = true;
14203 cp_parser_parse_tentatively (parser);
14204 cp_lexer_consume_token (parser->lexer);
14205 cp_parser_parameter_declaration_list (parser, &error);
14206 if (!cp_parser_error_occurred (parser) && !error)
14208 cp_parser_abort_tentative_parse (parser);
14210 parser->in_template_argument_list_p = saved_italp;
14213 case CPP_CLOSE_PAREN:
14215 /* If we run into a non-nested `;', `}', or `]',
14216 then the code is invalid -- but the default
14217 argument is certainly over. */
14218 case CPP_SEMICOLON:
14219 case CPP_CLOSE_BRACE:
14220 case CPP_CLOSE_SQUARE:
14223 /* Update DEPTH, if necessary. */
14224 else if (token->type == CPP_CLOSE_PAREN
14225 || token->type == CPP_CLOSE_BRACE
14226 || token->type == CPP_CLOSE_SQUARE)
14230 case CPP_OPEN_PAREN:
14231 case CPP_OPEN_SQUARE:
14232 case CPP_OPEN_BRACE:
14238 /* This might be the comparison operator, or it might
14239 start a template argument list. */
14240 ++maybe_template_id;
14244 if (cxx_dialect == cxx98)
14246 /* Fall through for C++0x, which treats the `>>'
14247 operator like two `>' tokens in certain
14253 /* This might be an operator, or it might close a
14254 template argument list. But if a previous '<'
14255 started a template argument list, this will have
14256 closed it, so we can't be in one anymore. */
14257 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14258 if (maybe_template_id < 0)
14259 maybe_template_id = 0;
14263 /* If we run out of tokens, issue an error message. */
14265 case CPP_PRAGMA_EOL:
14266 error ("%Hfile ends in default argument", &token->location);
14272 /* In these cases, we should look for template-ids.
14273 For example, if the default argument is
14274 `X<int, double>()', we need to do name lookup to
14275 figure out whether or not `X' is a template; if
14276 so, the `,' does not end the default argument.
14278 That is not yet done. */
14285 /* If we've reached the end, stop. */
14289 /* Add the token to the token block. */
14290 token = cp_lexer_consume_token (parser->lexer);
14293 /* Create a DEFAULT_ARG to represent the unparsed default
14295 default_argument = make_node (DEFAULT_ARG);
14296 DEFARG_TOKENS (default_argument)
14297 = cp_token_cache_new (first_token, token);
14298 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14300 /* Outside of a class definition, we can just parse the
14301 assignment-expression. */
14304 token = cp_lexer_peek_token (parser->lexer);
14306 = cp_parser_default_argument (parser, template_parm_p);
14309 if (!parser->default_arg_ok_p)
14311 if (flag_permissive)
14312 warning (0, "deprecated use of default argument for parameter of non-function");
14315 error ("%Hdefault arguments are only "
14316 "permitted for function parameters",
14318 default_argument = NULL_TREE;
14321 else if ((declarator && declarator->parameter_pack_p)
14322 || (decl_specifiers.type
14323 && PACK_EXPANSION_P (decl_specifiers.type)))
14325 const char* kind = template_parm_p? "template " : "";
14327 /* Find the name of the parameter pack. */
14328 cp_declarator *id_declarator = declarator;
14329 while (id_declarator && id_declarator->kind != cdk_id)
14330 id_declarator = id_declarator->declarator;
14332 if (id_declarator && id_declarator->kind == cdk_id)
14333 error ("%H%sparameter pack %qD cannot have a default argument",
14334 &declarator_token_start->location,
14335 kind, id_declarator->u.id.unqualified_name);
14337 error ("%H%sparameter pack cannot have a default argument",
14338 &declarator_token_start->location, kind);
14340 default_argument = NULL_TREE;
14344 default_argument = NULL_TREE;
14346 return make_parameter_declarator (&decl_specifiers,
14351 /* Parse a default argument and return it.
14353 TEMPLATE_PARM_P is true if this is a default argument for a
14354 non-type template parameter. */
14356 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14358 tree default_argument = NULL_TREE;
14359 bool saved_greater_than_is_operator_p;
14360 bool saved_local_variables_forbidden_p;
14362 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14364 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14365 parser->greater_than_is_operator_p = !template_parm_p;
14366 /* Local variable names (and the `this' keyword) may not
14367 appear in a default argument. */
14368 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14369 parser->local_variables_forbidden_p = true;
14370 /* The default argument expression may cause implicitly
14371 defined member functions to be synthesized, which will
14372 result in garbage collection. We must treat this
14373 situation as if we were within the body of function so as
14374 to avoid collecting live data on the stack. */
14376 /* Parse the assignment-expression. */
14377 if (template_parm_p)
14378 push_deferring_access_checks (dk_no_deferred);
14380 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14381 if (template_parm_p)
14382 pop_deferring_access_checks ();
14383 /* Restore saved state. */
14385 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14386 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14388 return default_argument;
14391 /* Parse a function-body.
14394 compound_statement */
14397 cp_parser_function_body (cp_parser *parser)
14399 cp_parser_compound_statement (parser, NULL, false);
14402 /* Parse a ctor-initializer-opt followed by a function-body. Return
14403 true if a ctor-initializer was present. */
14406 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14409 bool ctor_initializer_p;
14411 /* Begin the function body. */
14412 body = begin_function_body ();
14413 /* Parse the optional ctor-initializer. */
14414 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14415 /* Parse the function-body. */
14416 cp_parser_function_body (parser);
14417 /* Finish the function body. */
14418 finish_function_body (body);
14420 return ctor_initializer_p;
14423 /* Parse an initializer.
14426 = initializer-clause
14427 ( expression-list )
14429 Returns an expression representing the initializer. If no
14430 initializer is present, NULL_TREE is returned.
14432 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14433 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14434 set to TRUE if there is no initializer present. If there is an
14435 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14436 is set to true; otherwise it is set to false. */
14439 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14440 bool* non_constant_p)
14445 /* Peek at the next token. */
14446 token = cp_lexer_peek_token (parser->lexer);
14448 /* Let our caller know whether or not this initializer was
14450 *is_direct_init = (token->type != CPP_EQ);
14451 /* Assume that the initializer is constant. */
14452 *non_constant_p = false;
14454 if (token->type == CPP_EQ)
14456 /* Consume the `='. */
14457 cp_lexer_consume_token (parser->lexer);
14458 /* Parse the initializer-clause. */
14459 init = cp_parser_initializer_clause (parser, non_constant_p);
14461 else if (token->type == CPP_OPEN_PAREN)
14462 init = cp_parser_parenthesized_expression_list (parser, false,
14464 /*allow_expansion_p=*/true,
14466 else if (token->type == CPP_OPEN_BRACE)
14468 maybe_warn_cpp0x ("extended initializer lists");
14469 init = cp_parser_braced_list (parser, non_constant_p);
14470 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14474 /* Anything else is an error. */
14475 cp_parser_error (parser, "expected initializer");
14476 init = error_mark_node;
14482 /* Parse an initializer-clause.
14484 initializer-clause:
14485 assignment-expression
14488 Returns an expression representing the initializer.
14490 If the `assignment-expression' production is used the value
14491 returned is simply a representation for the expression.
14493 Otherwise, calls cp_parser_braced_list. */
14496 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14500 /* Assume the expression is constant. */
14501 *non_constant_p = false;
14503 /* If it is not a `{', then we are looking at an
14504 assignment-expression. */
14505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14508 = cp_parser_constant_expression (parser,
14509 /*allow_non_constant_p=*/true,
14511 if (!*non_constant_p)
14512 initializer = fold_non_dependent_expr (initializer);
14515 initializer = cp_parser_braced_list (parser, non_constant_p);
14517 return initializer;
14520 /* Parse a brace-enclosed initializer list.
14523 { initializer-list , [opt] }
14526 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14527 the elements of the initializer-list (or NULL, if the last
14528 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14529 NULL_TREE. There is no way to detect whether or not the optional
14530 trailing `,' was provided. NON_CONSTANT_P is as for
14531 cp_parser_initializer. */
14534 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14538 /* Consume the `{' token. */
14539 cp_lexer_consume_token (parser->lexer);
14540 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14541 initializer = make_node (CONSTRUCTOR);
14542 /* If it's not a `}', then there is a non-trivial initializer. */
14543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14545 /* Parse the initializer list. */
14546 CONSTRUCTOR_ELTS (initializer)
14547 = cp_parser_initializer_list (parser, non_constant_p);
14548 /* A trailing `,' token is allowed. */
14549 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14550 cp_lexer_consume_token (parser->lexer);
14552 /* Now, there should be a trailing `}'. */
14553 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14554 TREE_TYPE (initializer) = init_list_type_node;
14555 return initializer;
14558 /* Parse an initializer-list.
14561 initializer-clause ... [opt]
14562 initializer-list , initializer-clause ... [opt]
14567 identifier : initializer-clause
14568 initializer-list, identifier : initializer-clause
14570 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14571 for the initializer. If the INDEX of the elt is non-NULL, it is the
14572 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14573 as for cp_parser_initializer. */
14575 static VEC(constructor_elt,gc) *
14576 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14578 VEC(constructor_elt,gc) *v = NULL;
14580 /* Assume all of the expressions are constant. */
14581 *non_constant_p = false;
14583 /* Parse the rest of the list. */
14589 bool clause_non_constant_p;
14591 /* If the next token is an identifier and the following one is a
14592 colon, we are looking at the GNU designated-initializer
14594 if (cp_parser_allow_gnu_extensions_p (parser)
14595 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14596 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14598 /* Warn the user that they are using an extension. */
14599 pedwarn (input_location, OPT_pedantic,
14600 "ISO C++ does not allow designated initializers");
14601 /* Consume the identifier. */
14602 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14603 /* Consume the `:'. */
14604 cp_lexer_consume_token (parser->lexer);
14607 identifier = NULL_TREE;
14609 /* Parse the initializer. */
14610 initializer = cp_parser_initializer_clause (parser,
14611 &clause_non_constant_p);
14612 /* If any clause is non-constant, so is the entire initializer. */
14613 if (clause_non_constant_p)
14614 *non_constant_p = true;
14616 /* If we have an ellipsis, this is an initializer pack
14618 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14620 /* Consume the `...'. */
14621 cp_lexer_consume_token (parser->lexer);
14623 /* Turn the initializer into an initializer expansion. */
14624 initializer = make_pack_expansion (initializer);
14627 /* Add it to the vector. */
14628 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14630 /* If the next token is not a comma, we have reached the end of
14632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14635 /* Peek at the next token. */
14636 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14637 /* If the next token is a `}', then we're still done. An
14638 initializer-clause can have a trailing `,' after the
14639 initializer-list and before the closing `}'. */
14640 if (token->type == CPP_CLOSE_BRACE)
14643 /* Consume the `,' token. */
14644 cp_lexer_consume_token (parser->lexer);
14650 /* Classes [gram.class] */
14652 /* Parse a class-name.
14658 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14659 to indicate that names looked up in dependent types should be
14660 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14661 keyword has been used to indicate that the name that appears next
14662 is a template. TAG_TYPE indicates the explicit tag given before
14663 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14664 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14665 is the class being defined in a class-head.
14667 Returns the TYPE_DECL representing the class. */
14670 cp_parser_class_name (cp_parser *parser,
14671 bool typename_keyword_p,
14672 bool template_keyword_p,
14673 enum tag_types tag_type,
14674 bool check_dependency_p,
14676 bool is_declaration)
14683 /* All class-names start with an identifier. */
14684 token = cp_lexer_peek_token (parser->lexer);
14685 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14687 cp_parser_error (parser, "expected class-name");
14688 return error_mark_node;
14691 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14692 to a template-id, so we save it here. */
14693 scope = parser->scope;
14694 if (scope == error_mark_node)
14695 return error_mark_node;
14697 /* Any name names a type if we're following the `typename' keyword
14698 in a qualified name where the enclosing scope is type-dependent. */
14699 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14700 && dependent_type_p (scope));
14701 /* Handle the common case (an identifier, but not a template-id)
14703 if (token->type == CPP_NAME
14704 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14706 cp_token *identifier_token;
14710 /* Look for the identifier. */
14711 identifier_token = cp_lexer_peek_token (parser->lexer);
14712 ambiguous_p = identifier_token->ambiguous_p;
14713 identifier = cp_parser_identifier (parser);
14714 /* If the next token isn't an identifier, we are certainly not
14715 looking at a class-name. */
14716 if (identifier == error_mark_node)
14717 decl = error_mark_node;
14718 /* If we know this is a type-name, there's no need to look it
14720 else if (typename_p)
14724 tree ambiguous_decls;
14725 /* If we already know that this lookup is ambiguous, then
14726 we've already issued an error message; there's no reason
14730 cp_parser_simulate_error (parser);
14731 return error_mark_node;
14733 /* If the next token is a `::', then the name must be a type
14736 [basic.lookup.qual]
14738 During the lookup for a name preceding the :: scope
14739 resolution operator, object, function, and enumerator
14740 names are ignored. */
14741 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14742 tag_type = typename_type;
14743 /* Look up the name. */
14744 decl = cp_parser_lookup_name (parser, identifier,
14746 /*is_template=*/false,
14747 /*is_namespace=*/false,
14748 check_dependency_p,
14750 identifier_token->location);
14751 if (ambiguous_decls)
14753 error ("%Hreference to %qD is ambiguous",
14754 &identifier_token->location, identifier);
14755 print_candidates (ambiguous_decls);
14756 if (cp_parser_parsing_tentatively (parser))
14758 identifier_token->ambiguous_p = true;
14759 cp_parser_simulate_error (parser);
14761 return error_mark_node;
14767 /* Try a template-id. */
14768 decl = cp_parser_template_id (parser, template_keyword_p,
14769 check_dependency_p,
14771 if (decl == error_mark_node)
14772 return error_mark_node;
14775 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14777 /* If this is a typename, create a TYPENAME_TYPE. */
14778 if (typename_p && decl != error_mark_node)
14780 decl = make_typename_type (scope, decl, typename_type,
14781 /*complain=*/tf_error);
14782 if (decl != error_mark_node)
14783 decl = TYPE_NAME (decl);
14786 /* Check to see that it is really the name of a class. */
14787 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14788 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14789 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14790 /* Situations like this:
14792 template <typename T> struct A {
14793 typename T::template X<int>::I i;
14796 are problematic. Is `T::template X<int>' a class-name? The
14797 standard does not seem to be definitive, but there is no other
14798 valid interpretation of the following `::'. Therefore, those
14799 names are considered class-names. */
14801 decl = make_typename_type (scope, decl, tag_type, tf_error);
14802 if (decl != error_mark_node)
14803 decl = TYPE_NAME (decl);
14805 else if (TREE_CODE (decl) != TYPE_DECL
14806 || TREE_TYPE (decl) == error_mark_node
14807 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14808 decl = error_mark_node;
14810 if (decl == error_mark_node)
14811 cp_parser_error (parser, "expected class-name");
14816 /* Parse a class-specifier.
14819 class-head { member-specification [opt] }
14821 Returns the TREE_TYPE representing the class. */
14824 cp_parser_class_specifier (cp_parser* parser)
14828 tree attributes = NULL_TREE;
14829 int has_trailing_semicolon;
14830 bool nested_name_specifier_p;
14831 unsigned saved_num_template_parameter_lists;
14832 bool saved_in_function_body;
14833 tree old_scope = NULL_TREE;
14834 tree scope = NULL_TREE;
14837 push_deferring_access_checks (dk_no_deferred);
14839 /* Parse the class-head. */
14840 type = cp_parser_class_head (parser,
14841 &nested_name_specifier_p,
14844 /* If the class-head was a semantic disaster, skip the entire body
14848 cp_parser_skip_to_end_of_block_or_statement (parser);
14849 pop_deferring_access_checks ();
14850 return error_mark_node;
14853 /* Look for the `{'. */
14854 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14856 pop_deferring_access_checks ();
14857 return error_mark_node;
14860 /* Process the base classes. If they're invalid, skip the
14861 entire class body. */
14862 if (!xref_basetypes (type, bases))
14864 /* Consuming the closing brace yields better error messages
14866 if (cp_parser_skip_to_closing_brace (parser))
14867 cp_lexer_consume_token (parser->lexer);
14868 pop_deferring_access_checks ();
14869 return error_mark_node;
14872 /* Issue an error message if type-definitions are forbidden here. */
14873 cp_parser_check_type_definition (parser);
14874 /* Remember that we are defining one more class. */
14875 ++parser->num_classes_being_defined;
14876 /* Inside the class, surrounding template-parameter-lists do not
14878 saved_num_template_parameter_lists
14879 = parser->num_template_parameter_lists;
14880 parser->num_template_parameter_lists = 0;
14881 /* We are not in a function body. */
14882 saved_in_function_body = parser->in_function_body;
14883 parser->in_function_body = false;
14885 /* Start the class. */
14886 if (nested_name_specifier_p)
14888 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14889 old_scope = push_inner_scope (scope);
14891 type = begin_class_definition (type, attributes);
14893 if (type == error_mark_node)
14894 /* If the type is erroneous, skip the entire body of the class. */
14895 cp_parser_skip_to_closing_brace (parser);
14897 /* Parse the member-specification. */
14898 cp_parser_member_specification_opt (parser);
14900 /* Look for the trailing `}'. */
14901 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14902 /* We get better error messages by noticing a common problem: a
14903 missing trailing `;'. */
14904 token = cp_lexer_peek_token (parser->lexer);
14905 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14906 /* Look for trailing attributes to apply to this class. */
14907 if (cp_parser_allow_gnu_extensions_p (parser))
14908 attributes = cp_parser_attributes_opt (parser);
14909 if (type != error_mark_node)
14910 type = finish_struct (type, attributes);
14911 if (nested_name_specifier_p)
14912 pop_inner_scope (old_scope, scope);
14913 /* If this class is not itself within the scope of another class,
14914 then we need to parse the bodies of all of the queued function
14915 definitions. Note that the queued functions defined in a class
14916 are not always processed immediately following the
14917 class-specifier for that class. Consider:
14920 struct B { void f() { sizeof (A); } };
14923 If `f' were processed before the processing of `A' were
14924 completed, there would be no way to compute the size of `A'.
14925 Note that the nesting we are interested in here is lexical --
14926 not the semantic nesting given by TYPE_CONTEXT. In particular,
14929 struct A { struct B; };
14930 struct A::B { void f() { } };
14932 there is no need to delay the parsing of `A::B::f'. */
14933 if (--parser->num_classes_being_defined == 0)
14937 tree class_type = NULL_TREE;
14938 tree pushed_scope = NULL_TREE;
14940 /* In a first pass, parse default arguments to the functions.
14941 Then, in a second pass, parse the bodies of the functions.
14942 This two-phased approach handles cases like:
14950 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14951 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14952 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14953 TREE_PURPOSE (parser->unparsed_functions_queues)
14954 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14956 fn = TREE_VALUE (queue_entry);
14957 /* If there are default arguments that have not yet been processed,
14958 take care of them now. */
14959 if (class_type != TREE_PURPOSE (queue_entry))
14962 pop_scope (pushed_scope);
14963 class_type = TREE_PURPOSE (queue_entry);
14964 pushed_scope = push_scope (class_type);
14966 /* Make sure that any template parameters are in scope. */
14967 maybe_begin_member_template_processing (fn);
14968 /* Parse the default argument expressions. */
14969 cp_parser_late_parsing_default_args (parser, fn);
14970 /* Remove any template parameters from the symbol table. */
14971 maybe_end_member_template_processing ();
14974 pop_scope (pushed_scope);
14975 /* Now parse the body of the functions. */
14976 for (TREE_VALUE (parser->unparsed_functions_queues)
14977 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14978 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14979 TREE_VALUE (parser->unparsed_functions_queues)
14980 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14982 /* Figure out which function we need to process. */
14983 fn = TREE_VALUE (queue_entry);
14984 /* Parse the function. */
14985 cp_parser_late_parsing_for_member (parser, fn);
14989 /* Put back any saved access checks. */
14990 pop_deferring_access_checks ();
14992 /* Restore saved state. */
14993 parser->in_function_body = saved_in_function_body;
14994 parser->num_template_parameter_lists
14995 = saved_num_template_parameter_lists;
15000 /* Parse a class-head.
15003 class-key identifier [opt] base-clause [opt]
15004 class-key nested-name-specifier identifier base-clause [opt]
15005 class-key nested-name-specifier [opt] template-id
15009 class-key attributes identifier [opt] base-clause [opt]
15010 class-key attributes nested-name-specifier identifier base-clause [opt]
15011 class-key attributes nested-name-specifier [opt] template-id
15014 Upon return BASES is initialized to the list of base classes (or
15015 NULL, if there are none) in the same form returned by
15016 cp_parser_base_clause.
15018 Returns the TYPE of the indicated class. Sets
15019 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15020 involving a nested-name-specifier was used, and FALSE otherwise.
15022 Returns error_mark_node if this is not a class-head.
15024 Returns NULL_TREE if the class-head is syntactically valid, but
15025 semantically invalid in a way that means we should skip the entire
15026 body of the class. */
15029 cp_parser_class_head (cp_parser* parser,
15030 bool* nested_name_specifier_p,
15031 tree *attributes_p,
15034 tree nested_name_specifier;
15035 enum tag_types class_key;
15036 tree id = NULL_TREE;
15037 tree type = NULL_TREE;
15039 bool template_id_p = false;
15040 bool qualified_p = false;
15041 bool invalid_nested_name_p = false;
15042 bool invalid_explicit_specialization_p = false;
15043 tree pushed_scope = NULL_TREE;
15044 unsigned num_templates;
15045 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15046 /* Assume no nested-name-specifier will be present. */
15047 *nested_name_specifier_p = false;
15048 /* Assume no template parameter lists will be used in defining the
15052 *bases = NULL_TREE;
15054 /* Look for the class-key. */
15055 class_key = cp_parser_class_key (parser);
15056 if (class_key == none_type)
15057 return error_mark_node;
15059 /* Parse the attributes. */
15060 attributes = cp_parser_attributes_opt (parser);
15062 /* If the next token is `::', that is invalid -- but sometimes
15063 people do try to write:
15067 Handle this gracefully by accepting the extra qualifier, and then
15068 issuing an error about it later if this really is a
15069 class-head. If it turns out just to be an elaborated type
15070 specifier, remain silent. */
15071 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15072 qualified_p = true;
15074 push_deferring_access_checks (dk_no_check);
15076 /* Determine the name of the class. Begin by looking for an
15077 optional nested-name-specifier. */
15078 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15079 nested_name_specifier
15080 = cp_parser_nested_name_specifier_opt (parser,
15081 /*typename_keyword_p=*/false,
15082 /*check_dependency_p=*/false,
15084 /*is_declaration=*/false);
15085 /* If there was a nested-name-specifier, then there *must* be an
15087 if (nested_name_specifier)
15089 type_start_token = cp_lexer_peek_token (parser->lexer);
15090 /* Although the grammar says `identifier', it really means
15091 `class-name' or `template-name'. You are only allowed to
15092 define a class that has already been declared with this
15095 The proposed resolution for Core Issue 180 says that wherever
15096 you see `class T::X' you should treat `X' as a type-name.
15098 It is OK to define an inaccessible class; for example:
15100 class A { class B; };
15103 We do not know if we will see a class-name, or a
15104 template-name. We look for a class-name first, in case the
15105 class-name is a template-id; if we looked for the
15106 template-name first we would stop after the template-name. */
15107 cp_parser_parse_tentatively (parser);
15108 type = cp_parser_class_name (parser,
15109 /*typename_keyword_p=*/false,
15110 /*template_keyword_p=*/false,
15112 /*check_dependency_p=*/false,
15113 /*class_head_p=*/true,
15114 /*is_declaration=*/false);
15115 /* If that didn't work, ignore the nested-name-specifier. */
15116 if (!cp_parser_parse_definitely (parser))
15118 invalid_nested_name_p = true;
15119 type_start_token = cp_lexer_peek_token (parser->lexer);
15120 id = cp_parser_identifier (parser);
15121 if (id == error_mark_node)
15124 /* If we could not find a corresponding TYPE, treat this
15125 declaration like an unqualified declaration. */
15126 if (type == error_mark_node)
15127 nested_name_specifier = NULL_TREE;
15128 /* Otherwise, count the number of templates used in TYPE and its
15129 containing scopes. */
15134 for (scope = TREE_TYPE (type);
15135 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15136 scope = (TYPE_P (scope)
15137 ? TYPE_CONTEXT (scope)
15138 : DECL_CONTEXT (scope)))
15140 && CLASS_TYPE_P (scope)
15141 && CLASSTYPE_TEMPLATE_INFO (scope)
15142 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15143 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15147 /* Otherwise, the identifier is optional. */
15150 /* We don't know whether what comes next is a template-id,
15151 an identifier, or nothing at all. */
15152 cp_parser_parse_tentatively (parser);
15153 /* Check for a template-id. */
15154 type_start_token = cp_lexer_peek_token (parser->lexer);
15155 id = cp_parser_template_id (parser,
15156 /*template_keyword_p=*/false,
15157 /*check_dependency_p=*/true,
15158 /*is_declaration=*/true);
15159 /* If that didn't work, it could still be an identifier. */
15160 if (!cp_parser_parse_definitely (parser))
15162 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15164 type_start_token = cp_lexer_peek_token (parser->lexer);
15165 id = cp_parser_identifier (parser);
15172 template_id_p = true;
15177 pop_deferring_access_checks ();
15180 cp_parser_check_for_invalid_template_id (parser, id,
15181 type_start_token->location);
15183 /* If it's not a `:' or a `{' then we can't really be looking at a
15184 class-head, since a class-head only appears as part of a
15185 class-specifier. We have to detect this situation before calling
15186 xref_tag, since that has irreversible side-effects. */
15187 if (!cp_parser_next_token_starts_class_definition_p (parser))
15189 cp_parser_error (parser, "expected %<{%> or %<:%>");
15190 return error_mark_node;
15193 /* At this point, we're going ahead with the class-specifier, even
15194 if some other problem occurs. */
15195 cp_parser_commit_to_tentative_parse (parser);
15196 /* Issue the error about the overly-qualified name now. */
15199 cp_parser_error (parser,
15200 "global qualification of class name is invalid");
15201 return error_mark_node;
15203 else if (invalid_nested_name_p)
15205 cp_parser_error (parser,
15206 "qualified name does not name a class");
15207 return error_mark_node;
15209 else if (nested_name_specifier)
15213 /* Reject typedef-names in class heads. */
15214 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15216 error ("%Hinvalid class name in declaration of %qD",
15217 &type_start_token->location, type);
15222 /* Figure out in what scope the declaration is being placed. */
15223 scope = current_scope ();
15224 /* If that scope does not contain the scope in which the
15225 class was originally declared, the program is invalid. */
15226 if (scope && !is_ancestor (scope, nested_name_specifier))
15228 if (at_namespace_scope_p ())
15229 error ("%Hdeclaration of %qD in namespace %qD which does not "
15231 &type_start_token->location,
15232 type, scope, nested_name_specifier);
15234 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15235 &type_start_token->location,
15236 type, scope, nested_name_specifier);
15242 A declarator-id shall not be qualified except for the
15243 definition of a ... nested class outside of its class
15244 ... [or] the definition or explicit instantiation of a
15245 class member of a namespace outside of its namespace. */
15246 if (scope == nested_name_specifier)
15248 permerror (input_location, "%Hextra qualification not allowed",
15249 &nested_name_specifier_token_start->location);
15250 nested_name_specifier = NULL_TREE;
15254 /* An explicit-specialization must be preceded by "template <>". If
15255 it is not, try to recover gracefully. */
15256 if (at_namespace_scope_p ()
15257 && parser->num_template_parameter_lists == 0
15260 error ("%Han explicit specialization must be preceded by %<template <>%>",
15261 &type_start_token->location);
15262 invalid_explicit_specialization_p = true;
15263 /* Take the same action that would have been taken by
15264 cp_parser_explicit_specialization. */
15265 ++parser->num_template_parameter_lists;
15266 begin_specialization ();
15268 /* There must be no "return" statements between this point and the
15269 end of this function; set "type "to the correct return value and
15270 use "goto done;" to return. */
15271 /* Make sure that the right number of template parameters were
15273 if (!cp_parser_check_template_parameters (parser, num_templates,
15274 type_start_token->location))
15276 /* If something went wrong, there is no point in even trying to
15277 process the class-definition. */
15282 /* Look up the type. */
15285 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15286 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15287 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15289 error ("%Hfunction template %qD redeclared as a class template",
15290 &type_start_token->location, id);
15291 type = error_mark_node;
15295 type = TREE_TYPE (id);
15296 type = maybe_process_partial_specialization (type);
15298 if (nested_name_specifier)
15299 pushed_scope = push_scope (nested_name_specifier);
15301 else if (nested_name_specifier)
15307 template <typename T> struct S { struct T };
15308 template <typename T> struct S<T>::T { };
15310 we will get a TYPENAME_TYPE when processing the definition of
15311 `S::T'. We need to resolve it to the actual type before we
15312 try to define it. */
15313 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15315 class_type = resolve_typename_type (TREE_TYPE (type),
15316 /*only_current_p=*/false);
15317 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15318 type = TYPE_NAME (class_type);
15321 cp_parser_error (parser, "could not resolve typename type");
15322 type = error_mark_node;
15326 if (maybe_process_partial_specialization (TREE_TYPE (type))
15327 == error_mark_node)
15333 class_type = current_class_type;
15334 /* Enter the scope indicated by the nested-name-specifier. */
15335 pushed_scope = push_scope (nested_name_specifier);
15336 /* Get the canonical version of this type. */
15337 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15338 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15339 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15341 type = push_template_decl (type);
15342 if (type == error_mark_node)
15349 type = TREE_TYPE (type);
15350 *nested_name_specifier_p = true;
15352 else /* The name is not a nested name. */
15354 /* If the class was unnamed, create a dummy name. */
15356 id = make_anon_name ();
15357 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15358 parser->num_template_parameter_lists);
15361 /* Indicate whether this class was declared as a `class' or as a
15363 if (TREE_CODE (type) == RECORD_TYPE)
15364 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15365 cp_parser_check_class_key (class_key, type);
15367 /* If this type was already complete, and we see another definition,
15368 that's an error. */
15369 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15371 error ("%Hredefinition of %q#T",
15372 &type_start_token->location, type);
15373 error ("%Hprevious definition of %q+#T",
15374 &type_start_token->location, type);
15378 else if (type == error_mark_node)
15381 /* We will have entered the scope containing the class; the names of
15382 base classes should be looked up in that context. For example:
15384 struct A { struct B {}; struct C; };
15385 struct A::C : B {};
15389 /* Get the list of base-classes, if there is one. */
15390 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15391 *bases = cp_parser_base_clause (parser);
15394 /* Leave the scope given by the nested-name-specifier. We will
15395 enter the class scope itself while processing the members. */
15397 pop_scope (pushed_scope);
15399 if (invalid_explicit_specialization_p)
15401 end_specialization ();
15402 --parser->num_template_parameter_lists;
15404 *attributes_p = attributes;
15408 /* Parse a class-key.
15415 Returns the kind of class-key specified, or none_type to indicate
15418 static enum tag_types
15419 cp_parser_class_key (cp_parser* parser)
15422 enum tag_types tag_type;
15424 /* Look for the class-key. */
15425 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15429 /* Check to see if the TOKEN is a class-key. */
15430 tag_type = cp_parser_token_is_class_key (token);
15432 cp_parser_error (parser, "expected class-key");
15436 /* Parse an (optional) member-specification.
15438 member-specification:
15439 member-declaration member-specification [opt]
15440 access-specifier : member-specification [opt] */
15443 cp_parser_member_specification_opt (cp_parser* parser)
15450 /* Peek at the next token. */
15451 token = cp_lexer_peek_token (parser->lexer);
15452 /* If it's a `}', or EOF then we've seen all the members. */
15453 if (token->type == CPP_CLOSE_BRACE
15454 || token->type == CPP_EOF
15455 || token->type == CPP_PRAGMA_EOL)
15458 /* See if this token is a keyword. */
15459 keyword = token->keyword;
15463 case RID_PROTECTED:
15465 /* Consume the access-specifier. */
15466 cp_lexer_consume_token (parser->lexer);
15467 /* Remember which access-specifier is active. */
15468 current_access_specifier = token->u.value;
15469 /* Look for the `:'. */
15470 cp_parser_require (parser, CPP_COLON, "%<:%>");
15474 /* Accept #pragmas at class scope. */
15475 if (token->type == CPP_PRAGMA)
15477 cp_parser_pragma (parser, pragma_external);
15481 /* Otherwise, the next construction must be a
15482 member-declaration. */
15483 cp_parser_member_declaration (parser);
15488 /* Parse a member-declaration.
15490 member-declaration:
15491 decl-specifier-seq [opt] member-declarator-list [opt] ;
15492 function-definition ; [opt]
15493 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15495 template-declaration
15497 member-declarator-list:
15499 member-declarator-list , member-declarator
15502 declarator pure-specifier [opt]
15503 declarator constant-initializer [opt]
15504 identifier [opt] : constant-expression
15508 member-declaration:
15509 __extension__ member-declaration
15512 declarator attributes [opt] pure-specifier [opt]
15513 declarator attributes [opt] constant-initializer [opt]
15514 identifier [opt] attributes [opt] : constant-expression
15518 member-declaration:
15519 static_assert-declaration */
15522 cp_parser_member_declaration (cp_parser* parser)
15524 cp_decl_specifier_seq decl_specifiers;
15525 tree prefix_attributes;
15527 int declares_class_or_enum;
15529 cp_token *token = NULL;
15530 cp_token *decl_spec_token_start = NULL;
15531 cp_token *initializer_token_start = NULL;
15532 int saved_pedantic;
15534 /* Check for the `__extension__' keyword. */
15535 if (cp_parser_extension_opt (parser, &saved_pedantic))
15538 cp_parser_member_declaration (parser);
15539 /* Restore the old value of the PEDANTIC flag. */
15540 pedantic = saved_pedantic;
15545 /* Check for a template-declaration. */
15546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15548 /* An explicit specialization here is an error condition, and we
15549 expect the specialization handler to detect and report this. */
15550 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15551 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15552 cp_parser_explicit_specialization (parser);
15554 cp_parser_template_declaration (parser, /*member_p=*/true);
15559 /* Check for a using-declaration. */
15560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15562 /* Parse the using-declaration. */
15563 cp_parser_using_declaration (parser,
15564 /*access_declaration_p=*/false);
15568 /* Check for @defs. */
15569 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15572 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15573 ivar = ivar_chains;
15577 ivar = TREE_CHAIN (member);
15578 TREE_CHAIN (member) = NULL_TREE;
15579 finish_member_declaration (member);
15584 /* If the next token is `static_assert' we have a static assertion. */
15585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15587 cp_parser_static_assert (parser, /*member_p=*/true);
15591 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15594 /* Parse the decl-specifier-seq. */
15595 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15596 cp_parser_decl_specifier_seq (parser,
15597 CP_PARSER_FLAGS_OPTIONAL,
15599 &declares_class_or_enum);
15600 prefix_attributes = decl_specifiers.attributes;
15601 decl_specifiers.attributes = NULL_TREE;
15602 /* Check for an invalid type-name. */
15603 if (!decl_specifiers.type
15604 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15606 /* If there is no declarator, then the decl-specifier-seq should
15608 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15610 /* If there was no decl-specifier-seq, and the next token is a
15611 `;', then we have something like:
15617 Each member-declaration shall declare at least one member
15618 name of the class. */
15619 if (!decl_specifiers.any_specifiers_p)
15621 cp_token *token = cp_lexer_peek_token (parser->lexer);
15622 if (!in_system_header_at (token->location))
15623 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15629 /* See if this declaration is a friend. */
15630 friend_p = cp_parser_friend_p (&decl_specifiers);
15631 /* If there were decl-specifiers, check to see if there was
15632 a class-declaration. */
15633 type = check_tag_decl (&decl_specifiers);
15634 /* Nested classes have already been added to the class, but
15635 a `friend' needs to be explicitly registered. */
15638 /* If the `friend' keyword was present, the friend must
15639 be introduced with a class-key. */
15640 if (!declares_class_or_enum)
15641 error ("%Ha class-key must be used when declaring a friend",
15642 &decl_spec_token_start->location);
15645 template <typename T> struct A {
15646 friend struct A<T>::B;
15649 A<T>::B will be represented by a TYPENAME_TYPE, and
15650 therefore not recognized by check_tag_decl. */
15652 && decl_specifiers.type
15653 && TYPE_P (decl_specifiers.type))
15654 type = decl_specifiers.type;
15655 if (!type || !TYPE_P (type))
15656 error ("%Hfriend declaration does not name a class or "
15657 "function", &decl_spec_token_start->location);
15659 make_friend_class (current_class_type, type,
15660 /*complain=*/true);
15662 /* If there is no TYPE, an error message will already have
15664 else if (!type || type == error_mark_node)
15666 /* An anonymous aggregate has to be handled specially; such
15667 a declaration really declares a data member (with a
15668 particular type), as opposed to a nested class. */
15669 else if (ANON_AGGR_TYPE_P (type))
15671 /* Remove constructors and such from TYPE, now that we
15672 know it is an anonymous aggregate. */
15673 fixup_anonymous_aggr (type);
15674 /* And make the corresponding data member. */
15675 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15676 /* Add it to the class. */
15677 finish_member_declaration (decl);
15680 cp_parser_check_access_in_redeclaration
15682 decl_spec_token_start->location);
15687 /* See if these declarations will be friends. */
15688 friend_p = cp_parser_friend_p (&decl_specifiers);
15690 /* Keep going until we hit the `;' at the end of the
15692 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15694 tree attributes = NULL_TREE;
15695 tree first_attribute;
15697 /* Peek at the next token. */
15698 token = cp_lexer_peek_token (parser->lexer);
15700 /* Check for a bitfield declaration. */
15701 if (token->type == CPP_COLON
15702 || (token->type == CPP_NAME
15703 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15709 /* Get the name of the bitfield. Note that we cannot just
15710 check TOKEN here because it may have been invalidated by
15711 the call to cp_lexer_peek_nth_token above. */
15712 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15713 identifier = cp_parser_identifier (parser);
15715 identifier = NULL_TREE;
15717 /* Consume the `:' token. */
15718 cp_lexer_consume_token (parser->lexer);
15719 /* Get the width of the bitfield. */
15721 = cp_parser_constant_expression (parser,
15722 /*allow_non_constant=*/false,
15725 /* Look for attributes that apply to the bitfield. */
15726 attributes = cp_parser_attributes_opt (parser);
15727 /* Remember which attributes are prefix attributes and
15729 first_attribute = attributes;
15730 /* Combine the attributes. */
15731 attributes = chainon (prefix_attributes, attributes);
15733 /* Create the bitfield declaration. */
15734 decl = grokbitfield (identifier
15735 ? make_id_declarator (NULL_TREE,
15745 cp_declarator *declarator;
15747 tree asm_specification;
15748 int ctor_dtor_or_conv_p;
15750 /* Parse the declarator. */
15752 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15753 &ctor_dtor_or_conv_p,
15754 /*parenthesized_p=*/NULL,
15755 /*member_p=*/true);
15757 /* If something went wrong parsing the declarator, make sure
15758 that we at least consume some tokens. */
15759 if (declarator == cp_error_declarator)
15761 /* Skip to the end of the statement. */
15762 cp_parser_skip_to_end_of_statement (parser);
15763 /* If the next token is not a semicolon, that is
15764 probably because we just skipped over the body of
15765 a function. So, we consume a semicolon if
15766 present, but do not issue an error message if it
15768 if (cp_lexer_next_token_is (parser->lexer,
15770 cp_lexer_consume_token (parser->lexer);
15774 if (declares_class_or_enum & 2)
15775 cp_parser_check_for_definition_in_return_type
15776 (declarator, decl_specifiers.type,
15777 decl_specifiers.type_location);
15779 /* Look for an asm-specification. */
15780 asm_specification = cp_parser_asm_specification_opt (parser);
15781 /* Look for attributes that apply to the declaration. */
15782 attributes = cp_parser_attributes_opt (parser);
15783 /* Remember which attributes are prefix attributes and
15785 first_attribute = attributes;
15786 /* Combine the attributes. */
15787 attributes = chainon (prefix_attributes, attributes);
15789 /* If it's an `=', then we have a constant-initializer or a
15790 pure-specifier. It is not correct to parse the
15791 initializer before registering the member declaration
15792 since the member declaration should be in scope while
15793 its initializer is processed. However, the rest of the
15794 front end does not yet provide an interface that allows
15795 us to handle this correctly. */
15796 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15800 A pure-specifier shall be used only in the declaration of
15801 a virtual function.
15803 A member-declarator can contain a constant-initializer
15804 only if it declares a static member of integral or
15807 Therefore, if the DECLARATOR is for a function, we look
15808 for a pure-specifier; otherwise, we look for a
15809 constant-initializer. When we call `grokfield', it will
15810 perform more stringent semantics checks. */
15811 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15812 if (function_declarator_p (declarator))
15813 initializer = cp_parser_pure_specifier (parser);
15815 /* Parse the initializer. */
15816 initializer = cp_parser_constant_initializer (parser);
15818 /* Otherwise, there is no initializer. */
15820 initializer = NULL_TREE;
15822 /* See if we are probably looking at a function
15823 definition. We are certainly not looking at a
15824 member-declarator. Calling `grokfield' has
15825 side-effects, so we must not do it unless we are sure
15826 that we are looking at a member-declarator. */
15827 if (cp_parser_token_starts_function_definition_p
15828 (cp_lexer_peek_token (parser->lexer)))
15830 /* The grammar does not allow a pure-specifier to be
15831 used when a member function is defined. (It is
15832 possible that this fact is an oversight in the
15833 standard, since a pure function may be defined
15834 outside of the class-specifier. */
15836 error ("%Hpure-specifier on function-definition",
15837 &initializer_token_start->location);
15838 decl = cp_parser_save_member_function_body (parser,
15842 /* If the member was not a friend, declare it here. */
15844 finish_member_declaration (decl);
15845 /* Peek at the next token. */
15846 token = cp_lexer_peek_token (parser->lexer);
15847 /* If the next token is a semicolon, consume it. */
15848 if (token->type == CPP_SEMICOLON)
15849 cp_lexer_consume_token (parser->lexer);
15853 if (declarator->kind == cdk_function)
15854 declarator->id_loc = token->location;
15855 /* Create the declaration. */
15856 decl = grokfield (declarator, &decl_specifiers,
15857 initializer, /*init_const_expr_p=*/true,
15862 /* Reset PREFIX_ATTRIBUTES. */
15863 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15864 attributes = TREE_CHAIN (attributes);
15866 TREE_CHAIN (attributes) = NULL_TREE;
15868 /* If there is any qualification still in effect, clear it
15869 now; we will be starting fresh with the next declarator. */
15870 parser->scope = NULL_TREE;
15871 parser->qualifying_scope = NULL_TREE;
15872 parser->object_scope = NULL_TREE;
15873 /* If it's a `,', then there are more declarators. */
15874 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15875 cp_lexer_consume_token (parser->lexer);
15876 /* If the next token isn't a `;', then we have a parse error. */
15877 else if (cp_lexer_next_token_is_not (parser->lexer,
15880 cp_parser_error (parser, "expected %<;%>");
15881 /* Skip tokens until we find a `;'. */
15882 cp_parser_skip_to_end_of_statement (parser);
15889 /* Add DECL to the list of members. */
15891 finish_member_declaration (decl);
15893 if (TREE_CODE (decl) == FUNCTION_DECL)
15894 cp_parser_save_default_args (parser, decl);
15899 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15902 /* Parse a pure-specifier.
15907 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15908 Otherwise, ERROR_MARK_NODE is returned. */
15911 cp_parser_pure_specifier (cp_parser* parser)
15915 /* Look for the `=' token. */
15916 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15917 return error_mark_node;
15918 /* Look for the `0' token. */
15919 token = cp_lexer_consume_token (parser->lexer);
15921 /* Accept = default or = delete in c++0x mode. */
15922 if (token->keyword == RID_DEFAULT
15923 || token->keyword == RID_DELETE)
15925 maybe_warn_cpp0x ("defaulted and deleted functions");
15926 return token->u.value;
15929 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15930 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15932 cp_parser_error (parser,
15933 "invalid pure specifier (only %<= 0%> is allowed)");
15934 cp_parser_skip_to_end_of_statement (parser);
15935 return error_mark_node;
15937 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15939 error ("%Htemplates may not be %<virtual%>", &token->location);
15940 return error_mark_node;
15943 return integer_zero_node;
15946 /* Parse a constant-initializer.
15948 constant-initializer:
15949 = constant-expression
15951 Returns a representation of the constant-expression. */
15954 cp_parser_constant_initializer (cp_parser* parser)
15956 /* Look for the `=' token. */
15957 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15958 return error_mark_node;
15960 /* It is invalid to write:
15962 struct S { static const int i = { 7 }; };
15965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15967 cp_parser_error (parser,
15968 "a brace-enclosed initializer is not allowed here");
15969 /* Consume the opening brace. */
15970 cp_lexer_consume_token (parser->lexer);
15971 /* Skip the initializer. */
15972 cp_parser_skip_to_closing_brace (parser);
15973 /* Look for the trailing `}'. */
15974 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15976 return error_mark_node;
15979 return cp_parser_constant_expression (parser,
15980 /*allow_non_constant=*/false,
15984 /* Derived classes [gram.class.derived] */
15986 /* Parse a base-clause.
15989 : base-specifier-list
15991 base-specifier-list:
15992 base-specifier ... [opt]
15993 base-specifier-list , base-specifier ... [opt]
15995 Returns a TREE_LIST representing the base-classes, in the order in
15996 which they were declared. The representation of each node is as
15997 described by cp_parser_base_specifier.
15999 In the case that no bases are specified, this function will return
16000 NULL_TREE, not ERROR_MARK_NODE. */
16003 cp_parser_base_clause (cp_parser* parser)
16005 tree bases = NULL_TREE;
16007 /* Look for the `:' that begins the list. */
16008 cp_parser_require (parser, CPP_COLON, "%<:%>");
16010 /* Scan the base-specifier-list. */
16015 bool pack_expansion_p = false;
16017 /* Look for the base-specifier. */
16018 base = cp_parser_base_specifier (parser);
16019 /* Look for the (optional) ellipsis. */
16020 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16022 /* Consume the `...'. */
16023 cp_lexer_consume_token (parser->lexer);
16025 pack_expansion_p = true;
16028 /* Add BASE to the front of the list. */
16029 if (base != error_mark_node)
16031 if (pack_expansion_p)
16032 /* Make this a pack expansion type. */
16033 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16036 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16038 TREE_CHAIN (base) = bases;
16042 /* Peek at the next token. */
16043 token = cp_lexer_peek_token (parser->lexer);
16044 /* If it's not a comma, then the list is complete. */
16045 if (token->type != CPP_COMMA)
16047 /* Consume the `,'. */
16048 cp_lexer_consume_token (parser->lexer);
16051 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16052 base class had a qualified name. However, the next name that
16053 appears is certainly not qualified. */
16054 parser->scope = NULL_TREE;
16055 parser->qualifying_scope = NULL_TREE;
16056 parser->object_scope = NULL_TREE;
16058 return nreverse (bases);
16061 /* Parse a base-specifier.
16064 :: [opt] nested-name-specifier [opt] class-name
16065 virtual access-specifier [opt] :: [opt] nested-name-specifier
16067 access-specifier virtual [opt] :: [opt] nested-name-specifier
16070 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16071 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16072 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16073 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16076 cp_parser_base_specifier (cp_parser* parser)
16080 bool virtual_p = false;
16081 bool duplicate_virtual_error_issued_p = false;
16082 bool duplicate_access_error_issued_p = false;
16083 bool class_scope_p, template_p;
16084 tree access = access_default_node;
16087 /* Process the optional `virtual' and `access-specifier'. */
16090 /* Peek at the next token. */
16091 token = cp_lexer_peek_token (parser->lexer);
16092 /* Process `virtual'. */
16093 switch (token->keyword)
16096 /* If `virtual' appears more than once, issue an error. */
16097 if (virtual_p && !duplicate_virtual_error_issued_p)
16099 cp_parser_error (parser,
16100 "%<virtual%> specified more than once in base-specified");
16101 duplicate_virtual_error_issued_p = true;
16106 /* Consume the `virtual' token. */
16107 cp_lexer_consume_token (parser->lexer);
16112 case RID_PROTECTED:
16114 /* If more than one access specifier appears, issue an
16116 if (access != access_default_node
16117 && !duplicate_access_error_issued_p)
16119 cp_parser_error (parser,
16120 "more than one access specifier in base-specified");
16121 duplicate_access_error_issued_p = true;
16124 access = ridpointers[(int) token->keyword];
16126 /* Consume the access-specifier. */
16127 cp_lexer_consume_token (parser->lexer);
16136 /* It is not uncommon to see programs mechanically, erroneously, use
16137 the 'typename' keyword to denote (dependent) qualified types
16138 as base classes. */
16139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16141 token = cp_lexer_peek_token (parser->lexer);
16142 if (!processing_template_decl)
16143 error ("%Hkeyword %<typename%> not allowed outside of templates",
16146 error ("%Hkeyword %<typename%> not allowed in this context "
16147 "(the base class is implicitly a type)",
16149 cp_lexer_consume_token (parser->lexer);
16152 /* Look for the optional `::' operator. */
16153 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16154 /* Look for the nested-name-specifier. The simplest way to
16159 The keyword `typename' is not permitted in a base-specifier or
16160 mem-initializer; in these contexts a qualified name that
16161 depends on a template-parameter is implicitly assumed to be a
16164 is to pretend that we have seen the `typename' keyword at this
16166 cp_parser_nested_name_specifier_opt (parser,
16167 /*typename_keyword_p=*/true,
16168 /*check_dependency_p=*/true,
16170 /*is_declaration=*/true);
16171 /* If the base class is given by a qualified name, assume that names
16172 we see are type names or templates, as appropriate. */
16173 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16174 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16176 /* Finally, look for the class-name. */
16177 type = cp_parser_class_name (parser,
16181 /*check_dependency_p=*/true,
16182 /*class_head_p=*/false,
16183 /*is_declaration=*/true);
16185 if (type == error_mark_node)
16186 return error_mark_node;
16188 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16191 /* Exception handling [gram.exception] */
16193 /* Parse an (optional) exception-specification.
16195 exception-specification:
16196 throw ( type-id-list [opt] )
16198 Returns a TREE_LIST representing the exception-specification. The
16199 TREE_VALUE of each node is a type. */
16202 cp_parser_exception_specification_opt (cp_parser* parser)
16207 /* Peek at the next token. */
16208 token = cp_lexer_peek_token (parser->lexer);
16209 /* If it's not `throw', then there's no exception-specification. */
16210 if (!cp_parser_is_keyword (token, RID_THROW))
16213 /* Consume the `throw'. */
16214 cp_lexer_consume_token (parser->lexer);
16216 /* Look for the `('. */
16217 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16219 /* Peek at the next token. */
16220 token = cp_lexer_peek_token (parser->lexer);
16221 /* If it's not a `)', then there is a type-id-list. */
16222 if (token->type != CPP_CLOSE_PAREN)
16224 const char *saved_message;
16226 /* Types may not be defined in an exception-specification. */
16227 saved_message = parser->type_definition_forbidden_message;
16228 parser->type_definition_forbidden_message
16229 = "types may not be defined in an exception-specification";
16230 /* Parse the type-id-list. */
16231 type_id_list = cp_parser_type_id_list (parser);
16232 /* Restore the saved message. */
16233 parser->type_definition_forbidden_message = saved_message;
16236 type_id_list = empty_except_spec;
16238 /* Look for the `)'. */
16239 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16241 return type_id_list;
16244 /* Parse an (optional) type-id-list.
16248 type-id-list , type-id ... [opt]
16250 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16251 in the order that the types were presented. */
16254 cp_parser_type_id_list (cp_parser* parser)
16256 tree types = NULL_TREE;
16263 /* Get the next type-id. */
16264 type = cp_parser_type_id (parser);
16265 /* Parse the optional ellipsis. */
16266 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16268 /* Consume the `...'. */
16269 cp_lexer_consume_token (parser->lexer);
16271 /* Turn the type into a pack expansion expression. */
16272 type = make_pack_expansion (type);
16274 /* Add it to the list. */
16275 types = add_exception_specifier (types, type, /*complain=*/1);
16276 /* Peek at the next token. */
16277 token = cp_lexer_peek_token (parser->lexer);
16278 /* If it is not a `,', we are done. */
16279 if (token->type != CPP_COMMA)
16281 /* Consume the `,'. */
16282 cp_lexer_consume_token (parser->lexer);
16285 return nreverse (types);
16288 /* Parse a try-block.
16291 try compound-statement handler-seq */
16294 cp_parser_try_block (cp_parser* parser)
16298 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16299 try_block = begin_try_block ();
16300 cp_parser_compound_statement (parser, NULL, true);
16301 finish_try_block (try_block);
16302 cp_parser_handler_seq (parser);
16303 finish_handler_sequence (try_block);
16308 /* Parse a function-try-block.
16310 function-try-block:
16311 try ctor-initializer [opt] function-body handler-seq */
16314 cp_parser_function_try_block (cp_parser* parser)
16316 tree compound_stmt;
16318 bool ctor_initializer_p;
16320 /* Look for the `try' keyword. */
16321 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16323 /* Let the rest of the front end know where we are. */
16324 try_block = begin_function_try_block (&compound_stmt);
16325 /* Parse the function-body. */
16327 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16328 /* We're done with the `try' part. */
16329 finish_function_try_block (try_block);
16330 /* Parse the handlers. */
16331 cp_parser_handler_seq (parser);
16332 /* We're done with the handlers. */
16333 finish_function_handler_sequence (try_block, compound_stmt);
16335 return ctor_initializer_p;
16338 /* Parse a handler-seq.
16341 handler handler-seq [opt] */
16344 cp_parser_handler_seq (cp_parser* parser)
16350 /* Parse the handler. */
16351 cp_parser_handler (parser);
16352 /* Peek at the next token. */
16353 token = cp_lexer_peek_token (parser->lexer);
16354 /* If it's not `catch' then there are no more handlers. */
16355 if (!cp_parser_is_keyword (token, RID_CATCH))
16360 /* Parse a handler.
16363 catch ( exception-declaration ) compound-statement */
16366 cp_parser_handler (cp_parser* parser)
16371 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16372 handler = begin_handler ();
16373 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16374 declaration = cp_parser_exception_declaration (parser);
16375 finish_handler_parms (declaration, handler);
16376 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16377 cp_parser_compound_statement (parser, NULL, false);
16378 finish_handler (handler);
16381 /* Parse an exception-declaration.
16383 exception-declaration:
16384 type-specifier-seq declarator
16385 type-specifier-seq abstract-declarator
16389 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16390 ellipsis variant is used. */
16393 cp_parser_exception_declaration (cp_parser* parser)
16395 cp_decl_specifier_seq type_specifiers;
16396 cp_declarator *declarator;
16397 const char *saved_message;
16399 /* If it's an ellipsis, it's easy to handle. */
16400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16402 /* Consume the `...' token. */
16403 cp_lexer_consume_token (parser->lexer);
16407 /* Types may not be defined in exception-declarations. */
16408 saved_message = parser->type_definition_forbidden_message;
16409 parser->type_definition_forbidden_message
16410 = "types may not be defined in exception-declarations";
16412 /* Parse the type-specifier-seq. */
16413 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16415 /* If it's a `)', then there is no declarator. */
16416 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16419 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16420 /*ctor_dtor_or_conv_p=*/NULL,
16421 /*parenthesized_p=*/NULL,
16422 /*member_p=*/false);
16424 /* Restore the saved message. */
16425 parser->type_definition_forbidden_message = saved_message;
16427 if (!type_specifiers.any_specifiers_p)
16428 return error_mark_node;
16430 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16433 /* Parse a throw-expression.
16436 throw assignment-expression [opt]
16438 Returns a THROW_EXPR representing the throw-expression. */
16441 cp_parser_throw_expression (cp_parser* parser)
16446 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16447 token = cp_lexer_peek_token (parser->lexer);
16448 /* Figure out whether or not there is an assignment-expression
16449 following the "throw" keyword. */
16450 if (token->type == CPP_COMMA
16451 || token->type == CPP_SEMICOLON
16452 || token->type == CPP_CLOSE_PAREN
16453 || token->type == CPP_CLOSE_SQUARE
16454 || token->type == CPP_CLOSE_BRACE
16455 || token->type == CPP_COLON)
16456 expression = NULL_TREE;
16458 expression = cp_parser_assignment_expression (parser,
16461 return build_throw (expression);
16464 /* GNU Extensions */
16466 /* Parse an (optional) asm-specification.
16469 asm ( string-literal )
16471 If the asm-specification is present, returns a STRING_CST
16472 corresponding to the string-literal. Otherwise, returns
16476 cp_parser_asm_specification_opt (cp_parser* parser)
16479 tree asm_specification;
16481 /* Peek at the next token. */
16482 token = cp_lexer_peek_token (parser->lexer);
16483 /* If the next token isn't the `asm' keyword, then there's no
16484 asm-specification. */
16485 if (!cp_parser_is_keyword (token, RID_ASM))
16488 /* Consume the `asm' token. */
16489 cp_lexer_consume_token (parser->lexer);
16490 /* Look for the `('. */
16491 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16493 /* Look for the string-literal. */
16494 asm_specification = cp_parser_string_literal (parser, false, false);
16496 /* Look for the `)'. */
16497 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16499 return asm_specification;
16502 /* Parse an asm-operand-list.
16506 asm-operand-list , asm-operand
16509 string-literal ( expression )
16510 [ string-literal ] string-literal ( expression )
16512 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16513 each node is the expression. The TREE_PURPOSE is itself a
16514 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16515 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16516 is a STRING_CST for the string literal before the parenthesis. Returns
16517 ERROR_MARK_NODE if any of the operands are invalid. */
16520 cp_parser_asm_operand_list (cp_parser* parser)
16522 tree asm_operands = NULL_TREE;
16523 bool invalid_operands = false;
16527 tree string_literal;
16531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16533 /* Consume the `[' token. */
16534 cp_lexer_consume_token (parser->lexer);
16535 /* Read the operand name. */
16536 name = cp_parser_identifier (parser);
16537 if (name != error_mark_node)
16538 name = build_string (IDENTIFIER_LENGTH (name),
16539 IDENTIFIER_POINTER (name));
16540 /* Look for the closing `]'. */
16541 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16545 /* Look for the string-literal. */
16546 string_literal = cp_parser_string_literal (parser, false, false);
16548 /* Look for the `('. */
16549 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16550 /* Parse the expression. */
16551 expression = cp_parser_expression (parser, /*cast_p=*/false);
16552 /* Look for the `)'. */
16553 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16555 if (name == error_mark_node
16556 || string_literal == error_mark_node
16557 || expression == error_mark_node)
16558 invalid_operands = true;
16560 /* Add this operand to the list. */
16561 asm_operands = tree_cons (build_tree_list (name, string_literal),
16564 /* If the next token is not a `,', there are no more
16566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16568 /* Consume the `,'. */
16569 cp_lexer_consume_token (parser->lexer);
16572 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16575 /* Parse an asm-clobber-list.
16579 asm-clobber-list , string-literal
16581 Returns a TREE_LIST, indicating the clobbers in the order that they
16582 appeared. The TREE_VALUE of each node is a STRING_CST. */
16585 cp_parser_asm_clobber_list (cp_parser* parser)
16587 tree clobbers = NULL_TREE;
16591 tree string_literal;
16593 /* Look for the string literal. */
16594 string_literal = cp_parser_string_literal (parser, false, false);
16595 /* Add it to the list. */
16596 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16597 /* If the next token is not a `,', then the list is
16599 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16601 /* Consume the `,' token. */
16602 cp_lexer_consume_token (parser->lexer);
16608 /* Parse an (optional) series of attributes.
16611 attributes attribute
16614 __attribute__ (( attribute-list [opt] ))
16616 The return value is as for cp_parser_attribute_list. */
16619 cp_parser_attributes_opt (cp_parser* parser)
16621 tree attributes = NULL_TREE;
16626 tree attribute_list;
16628 /* Peek at the next token. */
16629 token = cp_lexer_peek_token (parser->lexer);
16630 /* If it's not `__attribute__', then we're done. */
16631 if (token->keyword != RID_ATTRIBUTE)
16634 /* Consume the `__attribute__' keyword. */
16635 cp_lexer_consume_token (parser->lexer);
16636 /* Look for the two `(' tokens. */
16637 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16638 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16640 /* Peek at the next token. */
16641 token = cp_lexer_peek_token (parser->lexer);
16642 if (token->type != CPP_CLOSE_PAREN)
16643 /* Parse the attribute-list. */
16644 attribute_list = cp_parser_attribute_list (parser);
16646 /* If the next token is a `)', then there is no attribute
16648 attribute_list = NULL;
16650 /* Look for the two `)' tokens. */
16651 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16652 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16654 /* Add these new attributes to the list. */
16655 attributes = chainon (attributes, attribute_list);
16661 /* Parse an attribute-list.
16665 attribute-list , attribute
16669 identifier ( identifier )
16670 identifier ( identifier , expression-list )
16671 identifier ( expression-list )
16673 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16674 to an attribute. The TREE_PURPOSE of each node is the identifier
16675 indicating which attribute is in use. The TREE_VALUE represents
16676 the arguments, if any. */
16679 cp_parser_attribute_list (cp_parser* parser)
16681 tree attribute_list = NULL_TREE;
16682 bool save_translate_strings_p = parser->translate_strings_p;
16684 parser->translate_strings_p = false;
16691 /* Look for the identifier. We also allow keywords here; for
16692 example `__attribute__ ((const))' is legal. */
16693 token = cp_lexer_peek_token (parser->lexer);
16694 if (token->type == CPP_NAME
16695 || token->type == CPP_KEYWORD)
16697 tree arguments = NULL_TREE;
16699 /* Consume the token. */
16700 token = cp_lexer_consume_token (parser->lexer);
16702 /* Save away the identifier that indicates which attribute
16704 identifier = token->u.value;
16705 attribute = build_tree_list (identifier, NULL_TREE);
16707 /* Peek at the next token. */
16708 token = cp_lexer_peek_token (parser->lexer);
16709 /* If it's an `(', then parse the attribute arguments. */
16710 if (token->type == CPP_OPEN_PAREN)
16712 arguments = cp_parser_parenthesized_expression_list
16713 (parser, true, /*cast_p=*/false,
16714 /*allow_expansion_p=*/false,
16715 /*non_constant_p=*/NULL);
16716 /* Save the arguments away. */
16717 TREE_VALUE (attribute) = arguments;
16720 if (arguments != error_mark_node)
16722 /* Add this attribute to the list. */
16723 TREE_CHAIN (attribute) = attribute_list;
16724 attribute_list = attribute;
16727 token = cp_lexer_peek_token (parser->lexer);
16729 /* Now, look for more attributes. If the next token isn't a
16730 `,', we're done. */
16731 if (token->type != CPP_COMMA)
16734 /* Consume the comma and keep going. */
16735 cp_lexer_consume_token (parser->lexer);
16737 parser->translate_strings_p = save_translate_strings_p;
16739 /* We built up the list in reverse order. */
16740 return nreverse (attribute_list);
16743 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16744 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16745 current value of the PEDANTIC flag, regardless of whether or not
16746 the `__extension__' keyword is present. The caller is responsible
16747 for restoring the value of the PEDANTIC flag. */
16750 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16752 /* Save the old value of the PEDANTIC flag. */
16753 *saved_pedantic = pedantic;
16755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16757 /* Consume the `__extension__' token. */
16758 cp_lexer_consume_token (parser->lexer);
16759 /* We're not being pedantic while the `__extension__' keyword is
16769 /* Parse a label declaration.
16772 __label__ label-declarator-seq ;
16774 label-declarator-seq:
16775 identifier , label-declarator-seq
16779 cp_parser_label_declaration (cp_parser* parser)
16781 /* Look for the `__label__' keyword. */
16782 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16788 /* Look for an identifier. */
16789 identifier = cp_parser_identifier (parser);
16790 /* If we failed, stop. */
16791 if (identifier == error_mark_node)
16793 /* Declare it as a label. */
16794 finish_label_decl (identifier);
16795 /* If the next token is a `;', stop. */
16796 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16798 /* Look for the `,' separating the label declarations. */
16799 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16802 /* Look for the final `;'. */
16803 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16806 /* Support Functions */
16808 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16809 NAME should have one of the representations used for an
16810 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16811 is returned. If PARSER->SCOPE is a dependent type, then a
16812 SCOPE_REF is returned.
16814 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16815 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16816 was formed. Abstractly, such entities should not be passed to this
16817 function, because they do not need to be looked up, but it is
16818 simpler to check for this special case here, rather than at the
16821 In cases not explicitly covered above, this function returns a
16822 DECL, OVERLOAD, or baselink representing the result of the lookup.
16823 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16826 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16827 (e.g., "struct") that was used. In that case bindings that do not
16828 refer to types are ignored.
16830 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16833 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16836 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16839 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16840 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16841 NULL_TREE otherwise. */
16844 cp_parser_lookup_name (cp_parser *parser, tree name,
16845 enum tag_types tag_type,
16848 bool check_dependency,
16849 tree *ambiguous_decls,
16850 location_t name_location)
16854 tree object_type = parser->context->object_type;
16856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16857 flags |= LOOKUP_COMPLAIN;
16859 /* Assume that the lookup will be unambiguous. */
16860 if (ambiguous_decls)
16861 *ambiguous_decls = NULL_TREE;
16863 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16864 no longer valid. Note that if we are parsing tentatively, and
16865 the parse fails, OBJECT_TYPE will be automatically restored. */
16866 parser->context->object_type = NULL_TREE;
16868 if (name == error_mark_node)
16869 return error_mark_node;
16871 /* A template-id has already been resolved; there is no lookup to
16873 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16875 if (BASELINK_P (name))
16877 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16878 == TEMPLATE_ID_EXPR);
16882 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16883 it should already have been checked to make sure that the name
16884 used matches the type being destroyed. */
16885 if (TREE_CODE (name) == BIT_NOT_EXPR)
16889 /* Figure out to which type this destructor applies. */
16891 type = parser->scope;
16892 else if (object_type)
16893 type = object_type;
16895 type = current_class_type;
16896 /* If that's not a class type, there is no destructor. */
16897 if (!type || !CLASS_TYPE_P (type))
16898 return error_mark_node;
16899 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16900 lazily_declare_fn (sfk_destructor, type);
16901 if (!CLASSTYPE_DESTRUCTORS (type))
16902 return error_mark_node;
16903 /* If it was a class type, return the destructor. */
16904 return CLASSTYPE_DESTRUCTORS (type);
16907 /* By this point, the NAME should be an ordinary identifier. If
16908 the id-expression was a qualified name, the qualifying scope is
16909 stored in PARSER->SCOPE at this point. */
16910 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16912 /* Perform the lookup. */
16917 if (parser->scope == error_mark_node)
16918 return error_mark_node;
16920 /* If the SCOPE is dependent, the lookup must be deferred until
16921 the template is instantiated -- unless we are explicitly
16922 looking up names in uninstantiated templates. Even then, we
16923 cannot look up the name if the scope is not a class type; it
16924 might, for example, be a template type parameter. */
16925 dependent_p = (TYPE_P (parser->scope)
16926 && !(parser->in_declarator_p
16927 && currently_open_class (parser->scope))
16928 && dependent_type_p (parser->scope));
16929 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16936 /* The resolution to Core Issue 180 says that `struct
16937 A::B' should be considered a type-name, even if `A'
16939 type = make_typename_type (parser->scope, name, tag_type,
16940 /*complain=*/tf_error);
16941 decl = TYPE_NAME (type);
16943 else if (is_template
16944 && (cp_parser_next_token_ends_template_argument_p (parser)
16945 || cp_lexer_next_token_is (parser->lexer,
16947 decl = make_unbound_class_template (parser->scope,
16949 /*complain=*/tf_error);
16951 decl = build_qualified_name (/*type=*/NULL_TREE,
16952 parser->scope, name,
16957 tree pushed_scope = NULL_TREE;
16959 /* If PARSER->SCOPE is a dependent type, then it must be a
16960 class type, and we must not be checking dependencies;
16961 otherwise, we would have processed this lookup above. So
16962 that PARSER->SCOPE is not considered a dependent base by
16963 lookup_member, we must enter the scope here. */
16965 pushed_scope = push_scope (parser->scope);
16966 /* If the PARSER->SCOPE is a template specialization, it
16967 may be instantiated during name lookup. In that case,
16968 errors may be issued. Even if we rollback the current
16969 tentative parse, those errors are valid. */
16970 decl = lookup_qualified_name (parser->scope, name,
16971 tag_type != none_type,
16972 /*complain=*/true);
16974 /* If we have a single function from a using decl, pull it out. */
16976 && TREE_CODE (decl) == OVERLOAD
16977 && !really_overloaded_fn (decl))
16978 decl = OVL_FUNCTION (decl);
16981 pop_scope (pushed_scope);
16983 parser->qualifying_scope = parser->scope;
16984 parser->object_scope = NULL_TREE;
16986 else if (object_type)
16988 tree object_decl = NULL_TREE;
16989 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16990 OBJECT_TYPE is not a class. */
16991 if (CLASS_TYPE_P (object_type))
16992 /* If the OBJECT_TYPE is a template specialization, it may
16993 be instantiated during name lookup. In that case, errors
16994 may be issued. Even if we rollback the current tentative
16995 parse, those errors are valid. */
16996 object_decl = lookup_member (object_type,
16999 tag_type != none_type);
17000 /* Look it up in the enclosing context, too. */
17001 decl = lookup_name_real (name, tag_type != none_type,
17003 /*block_p=*/true, is_namespace, flags);
17004 parser->object_scope = object_type;
17005 parser->qualifying_scope = NULL_TREE;
17007 decl = object_decl;
17011 decl = lookup_name_real (name, tag_type != none_type,
17013 /*block_p=*/true, is_namespace, flags);
17014 parser->qualifying_scope = NULL_TREE;
17015 parser->object_scope = NULL_TREE;
17018 /* If the lookup failed, let our caller know. */
17019 if (!decl || decl == error_mark_node)
17020 return error_mark_node;
17022 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17023 if (TREE_CODE (decl) == TREE_LIST)
17025 if (ambiguous_decls)
17026 *ambiguous_decls = decl;
17027 /* The error message we have to print is too complicated for
17028 cp_parser_error, so we incorporate its actions directly. */
17029 if (!cp_parser_simulate_error (parser))
17031 error ("%Hreference to %qD is ambiguous",
17032 &name_location, name);
17033 print_candidates (decl);
17035 return error_mark_node;
17038 gcc_assert (DECL_P (decl)
17039 || TREE_CODE (decl) == OVERLOAD
17040 || TREE_CODE (decl) == SCOPE_REF
17041 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17042 || BASELINK_P (decl));
17044 /* If we have resolved the name of a member declaration, check to
17045 see if the declaration is accessible. When the name resolves to
17046 set of overloaded functions, accessibility is checked when
17047 overload resolution is done.
17049 During an explicit instantiation, access is not checked at all,
17050 as per [temp.explicit]. */
17052 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17057 /* Like cp_parser_lookup_name, but for use in the typical case where
17058 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17059 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17062 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17064 return cp_parser_lookup_name (parser, name,
17066 /*is_template=*/false,
17067 /*is_namespace=*/false,
17068 /*check_dependency=*/true,
17069 /*ambiguous_decls=*/NULL,
17073 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17074 the current context, return the TYPE_DECL. If TAG_NAME_P is
17075 true, the DECL indicates the class being defined in a class-head,
17076 or declared in an elaborated-type-specifier.
17078 Otherwise, return DECL. */
17081 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17083 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17084 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17087 template <typename T> struct B;
17090 template <typename T> struct A::B {};
17092 Similarly, in an elaborated-type-specifier:
17094 namespace N { struct X{}; }
17097 template <typename T> friend struct N::X;
17100 However, if the DECL refers to a class type, and we are in
17101 the scope of the class, then the name lookup automatically
17102 finds the TYPE_DECL created by build_self_reference rather
17103 than a TEMPLATE_DECL. For example, in:
17105 template <class T> struct S {
17109 there is no need to handle such case. */
17111 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17112 return DECL_TEMPLATE_RESULT (decl);
17117 /* If too many, or too few, template-parameter lists apply to the
17118 declarator, issue an error message. Returns TRUE if all went well,
17119 and FALSE otherwise. */
17122 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17123 cp_declarator *declarator,
17124 location_t declarator_location)
17126 unsigned num_templates;
17128 /* We haven't seen any classes that involve template parameters yet. */
17131 switch (declarator->kind)
17134 if (declarator->u.id.qualifying_scope)
17139 scope = declarator->u.id.qualifying_scope;
17140 member = declarator->u.id.unqualified_name;
17142 while (scope && CLASS_TYPE_P (scope))
17144 /* You're supposed to have one `template <...>'
17145 for every template class, but you don't need one
17146 for a full specialization. For example:
17148 template <class T> struct S{};
17149 template <> struct S<int> { void f(); };
17150 void S<int>::f () {}
17152 is correct; there shouldn't be a `template <>' for
17153 the definition of `S<int>::f'. */
17154 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17155 /* If SCOPE does not have template information of any
17156 kind, then it is not a template, nor is it nested
17157 within a template. */
17159 if (explicit_class_specialization_p (scope))
17161 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17164 scope = TYPE_CONTEXT (scope);
17167 else if (TREE_CODE (declarator->u.id.unqualified_name)
17168 == TEMPLATE_ID_EXPR)
17169 /* If the DECLARATOR has the form `X<y>' then it uses one
17170 additional level of template parameters. */
17173 return cp_parser_check_template_parameters (parser,
17175 declarator_location);
17180 case cdk_reference:
17182 return (cp_parser_check_declarator_template_parameters
17183 (parser, declarator->declarator, declarator_location));
17189 gcc_unreachable ();
17194 /* NUM_TEMPLATES were used in the current declaration. If that is
17195 invalid, return FALSE and issue an error messages. Otherwise,
17199 cp_parser_check_template_parameters (cp_parser* parser,
17200 unsigned num_templates,
17201 location_t location)
17203 /* If there are more template classes than parameter lists, we have
17206 template <class T> void S<T>::R<T>::f (); */
17207 if (parser->num_template_parameter_lists < num_templates)
17209 error ("%Htoo few template-parameter-lists", &location);
17212 /* If there are the same number of template classes and parameter
17213 lists, that's OK. */
17214 if (parser->num_template_parameter_lists == num_templates)
17216 /* If there are more, but only one more, then we are referring to a
17217 member template. That's OK too. */
17218 if (parser->num_template_parameter_lists == num_templates + 1)
17220 /* Otherwise, there are too many template parameter lists. We have
17223 template <class T> template <class U> void S::f(); */
17224 error ("%Htoo many template-parameter-lists", &location);
17228 /* Parse an optional `::' token indicating that the following name is
17229 from the global namespace. If so, PARSER->SCOPE is set to the
17230 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17231 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17232 Returns the new value of PARSER->SCOPE, if the `::' token is
17233 present, and NULL_TREE otherwise. */
17236 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17240 /* Peek at the next token. */
17241 token = cp_lexer_peek_token (parser->lexer);
17242 /* If we're looking at a `::' token then we're starting from the
17243 global namespace, not our current location. */
17244 if (token->type == CPP_SCOPE)
17246 /* Consume the `::' token. */
17247 cp_lexer_consume_token (parser->lexer);
17248 /* Set the SCOPE so that we know where to start the lookup. */
17249 parser->scope = global_namespace;
17250 parser->qualifying_scope = global_namespace;
17251 parser->object_scope = NULL_TREE;
17253 return parser->scope;
17255 else if (!current_scope_valid_p)
17257 parser->scope = NULL_TREE;
17258 parser->qualifying_scope = NULL_TREE;
17259 parser->object_scope = NULL_TREE;
17265 /* Returns TRUE if the upcoming token sequence is the start of a
17266 constructor declarator. If FRIEND_P is true, the declarator is
17267 preceded by the `friend' specifier. */
17270 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17272 bool constructor_p;
17273 tree type_decl = NULL_TREE;
17274 bool nested_name_p;
17275 cp_token *next_token;
17277 /* The common case is that this is not a constructor declarator, so
17278 try to avoid doing lots of work if at all possible. It's not
17279 valid declare a constructor at function scope. */
17280 if (parser->in_function_body)
17282 /* And only certain tokens can begin a constructor declarator. */
17283 next_token = cp_lexer_peek_token (parser->lexer);
17284 if (next_token->type != CPP_NAME
17285 && next_token->type != CPP_SCOPE
17286 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17287 && next_token->type != CPP_TEMPLATE_ID)
17290 /* Parse tentatively; we are going to roll back all of the tokens
17292 cp_parser_parse_tentatively (parser);
17293 /* Assume that we are looking at a constructor declarator. */
17294 constructor_p = true;
17296 /* Look for the optional `::' operator. */
17297 cp_parser_global_scope_opt (parser,
17298 /*current_scope_valid_p=*/false);
17299 /* Look for the nested-name-specifier. */
17301 = (cp_parser_nested_name_specifier_opt (parser,
17302 /*typename_keyword_p=*/false,
17303 /*check_dependency_p=*/false,
17305 /*is_declaration=*/false)
17307 /* Outside of a class-specifier, there must be a
17308 nested-name-specifier. */
17309 if (!nested_name_p &&
17310 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17312 constructor_p = false;
17313 /* If we still think that this might be a constructor-declarator,
17314 look for a class-name. */
17319 template <typename T> struct S { S(); };
17320 template <typename T> S<T>::S ();
17322 we must recognize that the nested `S' names a class.
17325 template <typename T> S<T>::S<T> ();
17327 we must recognize that the nested `S' names a template. */
17328 type_decl = cp_parser_class_name (parser,
17329 /*typename_keyword_p=*/false,
17330 /*template_keyword_p=*/false,
17332 /*check_dependency_p=*/false,
17333 /*class_head_p=*/false,
17334 /*is_declaration=*/false);
17335 /* If there was no class-name, then this is not a constructor. */
17336 constructor_p = !cp_parser_error_occurred (parser);
17339 /* If we're still considering a constructor, we have to see a `(',
17340 to begin the parameter-declaration-clause, followed by either a
17341 `)', an `...', or a decl-specifier. We need to check for a
17342 type-specifier to avoid being fooled into thinking that:
17346 is a constructor. (It is actually a function named `f' that
17347 takes one parameter (of type `int') and returns a value of type
17350 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17353 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17354 /* A parameter declaration begins with a decl-specifier,
17355 which is either the "attribute" keyword, a storage class
17356 specifier, or (usually) a type-specifier. */
17357 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17360 tree pushed_scope = NULL_TREE;
17361 unsigned saved_num_template_parameter_lists;
17363 /* Names appearing in the type-specifier should be looked up
17364 in the scope of the class. */
17365 if (current_class_type)
17369 type = TREE_TYPE (type_decl);
17370 if (TREE_CODE (type) == TYPENAME_TYPE)
17372 type = resolve_typename_type (type,
17373 /*only_current_p=*/false);
17374 if (TREE_CODE (type) == TYPENAME_TYPE)
17376 cp_parser_abort_tentative_parse (parser);
17380 pushed_scope = push_scope (type);
17383 /* Inside the constructor parameter list, surrounding
17384 template-parameter-lists do not apply. */
17385 saved_num_template_parameter_lists
17386 = parser->num_template_parameter_lists;
17387 parser->num_template_parameter_lists = 0;
17389 /* Look for the type-specifier. */
17390 cp_parser_type_specifier (parser,
17391 CP_PARSER_FLAGS_NONE,
17392 /*decl_specs=*/NULL,
17393 /*is_declarator=*/true,
17394 /*declares_class_or_enum=*/NULL,
17395 /*is_cv_qualifier=*/NULL);
17397 parser->num_template_parameter_lists
17398 = saved_num_template_parameter_lists;
17400 /* Leave the scope of the class. */
17402 pop_scope (pushed_scope);
17404 constructor_p = !cp_parser_error_occurred (parser);
17408 constructor_p = false;
17409 /* We did not really want to consume any tokens. */
17410 cp_parser_abort_tentative_parse (parser);
17412 return constructor_p;
17415 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17416 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17417 they must be performed once we are in the scope of the function.
17419 Returns the function defined. */
17422 cp_parser_function_definition_from_specifiers_and_declarator
17423 (cp_parser* parser,
17424 cp_decl_specifier_seq *decl_specifiers,
17426 const cp_declarator *declarator)
17431 /* Begin the function-definition. */
17432 success_p = start_function (decl_specifiers, declarator, attributes);
17434 /* The things we're about to see are not directly qualified by any
17435 template headers we've seen thus far. */
17436 reset_specialization ();
17438 /* If there were names looked up in the decl-specifier-seq that we
17439 did not check, check them now. We must wait until we are in the
17440 scope of the function to perform the checks, since the function
17441 might be a friend. */
17442 perform_deferred_access_checks ();
17446 /* Skip the entire function. */
17447 cp_parser_skip_to_end_of_block_or_statement (parser);
17448 fn = error_mark_node;
17450 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17452 /* Seen already, skip it. An error message has already been output. */
17453 cp_parser_skip_to_end_of_block_or_statement (parser);
17454 fn = current_function_decl;
17455 current_function_decl = NULL_TREE;
17456 /* If this is a function from a class, pop the nested class. */
17457 if (current_class_name)
17458 pop_nested_class ();
17461 fn = cp_parser_function_definition_after_declarator (parser,
17462 /*inline_p=*/false);
17467 /* Parse the part of a function-definition that follows the
17468 declarator. INLINE_P is TRUE iff this function is an inline
17469 function defined with a class-specifier.
17471 Returns the function defined. */
17474 cp_parser_function_definition_after_declarator (cp_parser* parser,
17478 bool ctor_initializer_p = false;
17479 bool saved_in_unbraced_linkage_specification_p;
17480 bool saved_in_function_body;
17481 unsigned saved_num_template_parameter_lists;
17484 saved_in_function_body = parser->in_function_body;
17485 parser->in_function_body = true;
17486 /* If the next token is `return', then the code may be trying to
17487 make use of the "named return value" extension that G++ used to
17489 token = cp_lexer_peek_token (parser->lexer);
17490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17492 /* Consume the `return' keyword. */
17493 cp_lexer_consume_token (parser->lexer);
17494 /* Look for the identifier that indicates what value is to be
17496 cp_parser_identifier (parser);
17497 /* Issue an error message. */
17498 error ("%Hnamed return values are no longer supported",
17500 /* Skip tokens until we reach the start of the function body. */
17503 cp_token *token = cp_lexer_peek_token (parser->lexer);
17504 if (token->type == CPP_OPEN_BRACE
17505 || token->type == CPP_EOF
17506 || token->type == CPP_PRAGMA_EOL)
17508 cp_lexer_consume_token (parser->lexer);
17511 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17512 anything declared inside `f'. */
17513 saved_in_unbraced_linkage_specification_p
17514 = parser->in_unbraced_linkage_specification_p;
17515 parser->in_unbraced_linkage_specification_p = false;
17516 /* Inside the function, surrounding template-parameter-lists do not
17518 saved_num_template_parameter_lists
17519 = parser->num_template_parameter_lists;
17520 parser->num_template_parameter_lists = 0;
17521 /* If the next token is `try', then we are looking at a
17522 function-try-block. */
17523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17524 ctor_initializer_p = cp_parser_function_try_block (parser);
17525 /* A function-try-block includes the function-body, so we only do
17526 this next part if we're not processing a function-try-block. */
17529 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17531 /* Finish the function. */
17532 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17533 (inline_p ? 2 : 0));
17534 /* Generate code for it, if necessary. */
17535 expand_or_defer_fn (fn);
17536 /* Restore the saved values. */
17537 parser->in_unbraced_linkage_specification_p
17538 = saved_in_unbraced_linkage_specification_p;
17539 parser->num_template_parameter_lists
17540 = saved_num_template_parameter_lists;
17541 parser->in_function_body = saved_in_function_body;
17546 /* Parse a template-declaration, assuming that the `export' (and
17547 `extern') keywords, if present, has already been scanned. MEMBER_P
17548 is as for cp_parser_template_declaration. */
17551 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17553 tree decl = NULL_TREE;
17554 VEC (deferred_access_check,gc) *checks;
17555 tree parameter_list;
17556 bool friend_p = false;
17557 bool need_lang_pop;
17560 /* Look for the `template' keyword. */
17561 token = cp_lexer_peek_token (parser->lexer);
17562 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17566 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17568 if (at_class_scope_p () && current_function_decl)
17570 /* 14.5.2.2 [temp.mem]
17572 A local class shall not have member templates. */
17573 error ("%Hinvalid declaration of member template in local class",
17575 cp_parser_skip_to_end_of_block_or_statement (parser);
17580 A template ... shall not have C linkage. */
17581 if (current_lang_name == lang_name_c)
17583 error ("%Htemplate with C linkage", &token->location);
17584 /* Give it C++ linkage to avoid confusing other parts of the
17586 push_lang_context (lang_name_cplusplus);
17587 need_lang_pop = true;
17590 need_lang_pop = false;
17592 /* We cannot perform access checks on the template parameter
17593 declarations until we know what is being declared, just as we
17594 cannot check the decl-specifier list. */
17595 push_deferring_access_checks (dk_deferred);
17597 /* If the next token is `>', then we have an invalid
17598 specialization. Rather than complain about an invalid template
17599 parameter, issue an error message here. */
17600 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17602 cp_parser_error (parser, "invalid explicit specialization");
17603 begin_specialization ();
17604 parameter_list = NULL_TREE;
17607 /* Parse the template parameters. */
17608 parameter_list = cp_parser_template_parameter_list (parser);
17610 /* Get the deferred access checks from the parameter list. These
17611 will be checked once we know what is being declared, as for a
17612 member template the checks must be performed in the scope of the
17613 class containing the member. */
17614 checks = get_deferred_access_checks ();
17616 /* Look for the `>'. */
17617 cp_parser_skip_to_end_of_template_parameter_list (parser);
17618 /* We just processed one more parameter list. */
17619 ++parser->num_template_parameter_lists;
17620 /* If the next token is `template', there are more template
17622 if (cp_lexer_next_token_is_keyword (parser->lexer,
17624 cp_parser_template_declaration_after_export (parser, member_p);
17627 /* There are no access checks when parsing a template, as we do not
17628 know if a specialization will be a friend. */
17629 push_deferring_access_checks (dk_no_check);
17630 token = cp_lexer_peek_token (parser->lexer);
17631 decl = cp_parser_single_declaration (parser,
17634 /*explicit_specialization_p=*/false,
17636 pop_deferring_access_checks ();
17638 /* If this is a member template declaration, let the front
17640 if (member_p && !friend_p && decl)
17642 if (TREE_CODE (decl) == TYPE_DECL)
17643 cp_parser_check_access_in_redeclaration (decl, token->location);
17645 decl = finish_member_template_decl (decl);
17647 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17648 make_friend_class (current_class_type, TREE_TYPE (decl),
17649 /*complain=*/true);
17651 /* We are done with the current parameter list. */
17652 --parser->num_template_parameter_lists;
17654 pop_deferring_access_checks ();
17657 finish_template_decl (parameter_list);
17659 /* Register member declarations. */
17660 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17661 finish_member_declaration (decl);
17662 /* For the erroneous case of a template with C linkage, we pushed an
17663 implicit C++ linkage scope; exit that scope now. */
17665 pop_lang_context ();
17666 /* If DECL is a function template, we must return to parse it later.
17667 (Even though there is no definition, there might be default
17668 arguments that need handling.) */
17669 if (member_p && decl
17670 && (TREE_CODE (decl) == FUNCTION_DECL
17671 || DECL_FUNCTION_TEMPLATE_P (decl)))
17672 TREE_VALUE (parser->unparsed_functions_queues)
17673 = tree_cons (NULL_TREE, decl,
17674 TREE_VALUE (parser->unparsed_functions_queues));
17677 /* Perform the deferred access checks from a template-parameter-list.
17678 CHECKS is a TREE_LIST of access checks, as returned by
17679 get_deferred_access_checks. */
17682 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17684 ++processing_template_parmlist;
17685 perform_access_checks (checks);
17686 --processing_template_parmlist;
17689 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17690 `function-definition' sequence. MEMBER_P is true, this declaration
17691 appears in a class scope.
17693 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17694 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17697 cp_parser_single_declaration (cp_parser* parser,
17698 VEC (deferred_access_check,gc)* checks,
17700 bool explicit_specialization_p,
17703 int declares_class_or_enum;
17704 tree decl = NULL_TREE;
17705 cp_decl_specifier_seq decl_specifiers;
17706 bool function_definition_p = false;
17707 cp_token *decl_spec_token_start;
17709 /* This function is only used when processing a template
17711 gcc_assert (innermost_scope_kind () == sk_template_parms
17712 || innermost_scope_kind () == sk_template_spec);
17714 /* Defer access checks until we know what is being declared. */
17715 push_deferring_access_checks (dk_deferred);
17717 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17719 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17720 cp_parser_decl_specifier_seq (parser,
17721 CP_PARSER_FLAGS_OPTIONAL,
17723 &declares_class_or_enum);
17725 *friend_p = cp_parser_friend_p (&decl_specifiers);
17727 /* There are no template typedefs. */
17728 if (decl_specifiers.specs[(int) ds_typedef])
17730 error ("%Htemplate declaration of %qs",
17731 &decl_spec_token_start->location, "typedef");
17732 decl = error_mark_node;
17735 /* Gather up the access checks that occurred the
17736 decl-specifier-seq. */
17737 stop_deferring_access_checks ();
17739 /* Check for the declaration of a template class. */
17740 if (declares_class_or_enum)
17742 if (cp_parser_declares_only_class_p (parser))
17744 decl = shadow_tag (&decl_specifiers);
17749 friend template <typename T> struct A<T>::B;
17752 A<T>::B will be represented by a TYPENAME_TYPE, and
17753 therefore not recognized by shadow_tag. */
17754 if (friend_p && *friend_p
17756 && decl_specifiers.type
17757 && TYPE_P (decl_specifiers.type))
17758 decl = decl_specifiers.type;
17760 if (decl && decl != error_mark_node)
17761 decl = TYPE_NAME (decl);
17763 decl = error_mark_node;
17765 /* Perform access checks for template parameters. */
17766 cp_parser_perform_template_parameter_access_checks (checks);
17769 /* If it's not a template class, try for a template function. If
17770 the next token is a `;', then this declaration does not declare
17771 anything. But, if there were errors in the decl-specifiers, then
17772 the error might well have come from an attempted class-specifier.
17773 In that case, there's no need to warn about a missing declarator. */
17775 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17776 || decl_specifiers.type != error_mark_node))
17778 decl = cp_parser_init_declarator (parser,
17781 /*function_definition_allowed_p=*/true,
17783 declares_class_or_enum,
17784 &function_definition_p);
17786 /* 7.1.1-1 [dcl.stc]
17788 A storage-class-specifier shall not be specified in an explicit
17789 specialization... */
17791 && explicit_specialization_p
17792 && decl_specifiers.storage_class != sc_none)
17794 error ("%Hexplicit template specialization cannot have a storage class",
17795 &decl_spec_token_start->location);
17796 decl = error_mark_node;
17800 pop_deferring_access_checks ();
17802 /* Clear any current qualification; whatever comes next is the start
17803 of something new. */
17804 parser->scope = NULL_TREE;
17805 parser->qualifying_scope = NULL_TREE;
17806 parser->object_scope = NULL_TREE;
17807 /* Look for a trailing `;' after the declaration. */
17808 if (!function_definition_p
17809 && (decl == error_mark_node
17810 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17811 cp_parser_skip_to_end_of_block_or_statement (parser);
17816 /* Parse a cast-expression that is not the operand of a unary "&". */
17819 cp_parser_simple_cast_expression (cp_parser *parser)
17821 return cp_parser_cast_expression (parser, /*address_p=*/false,
17825 /* Parse a functional cast to TYPE. Returns an expression
17826 representing the cast. */
17829 cp_parser_functional_cast (cp_parser* parser, tree type)
17831 tree expression_list;
17835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17837 maybe_warn_cpp0x ("extended initializer lists");
17838 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17839 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17840 if (TREE_CODE (type) == TYPE_DECL)
17841 type = TREE_TYPE (type);
17842 return finish_compound_literal (type, expression_list);
17846 = cp_parser_parenthesized_expression_list (parser, false,
17848 /*allow_expansion_p=*/true,
17849 /*non_constant_p=*/NULL);
17851 cast = build_functional_cast (type, expression_list,
17852 tf_warning_or_error);
17853 /* [expr.const]/1: In an integral constant expression "only type
17854 conversions to integral or enumeration type can be used". */
17855 if (TREE_CODE (type) == TYPE_DECL)
17856 type = TREE_TYPE (type);
17857 if (cast != error_mark_node
17858 && !cast_valid_in_integral_constant_expression_p (type)
17859 && (cp_parser_non_integral_constant_expression
17860 (parser, "a call to a constructor")))
17861 return error_mark_node;
17865 /* Save the tokens that make up the body of a member function defined
17866 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17867 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17868 specifiers applied to the declaration. Returns the FUNCTION_DECL
17869 for the member function. */
17872 cp_parser_save_member_function_body (cp_parser* parser,
17873 cp_decl_specifier_seq *decl_specifiers,
17874 cp_declarator *declarator,
17881 /* Create the function-declaration. */
17882 fn = start_method (decl_specifiers, declarator, attributes);
17883 /* If something went badly wrong, bail out now. */
17884 if (fn == error_mark_node)
17886 /* If there's a function-body, skip it. */
17887 if (cp_parser_token_starts_function_definition_p
17888 (cp_lexer_peek_token (parser->lexer)))
17889 cp_parser_skip_to_end_of_block_or_statement (parser);
17890 return error_mark_node;
17893 /* Remember it, if there default args to post process. */
17894 cp_parser_save_default_args (parser, fn);
17896 /* Save away the tokens that make up the body of the
17898 first = parser->lexer->next_token;
17899 /* We can have braced-init-list mem-initializers before the fn body. */
17900 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17902 cp_lexer_consume_token (parser->lexer);
17903 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17904 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17906 /* cache_group will stop after an un-nested { } pair, too. */
17907 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17910 /* variadic mem-inits have ... after the ')'. */
17911 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17912 cp_lexer_consume_token (parser->lexer);
17915 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17916 /* Handle function try blocks. */
17917 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17918 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17919 last = parser->lexer->next_token;
17921 /* Save away the inline definition; we will process it when the
17922 class is complete. */
17923 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17924 DECL_PENDING_INLINE_P (fn) = 1;
17926 /* We need to know that this was defined in the class, so that
17927 friend templates are handled correctly. */
17928 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17930 /* We're done with the inline definition. */
17931 finish_method (fn);
17933 /* Add FN to the queue of functions to be parsed later. */
17934 TREE_VALUE (parser->unparsed_functions_queues)
17935 = tree_cons (NULL_TREE, fn,
17936 TREE_VALUE (parser->unparsed_functions_queues));
17941 /* Parse a template-argument-list, as well as the trailing ">" (but
17942 not the opening ">"). See cp_parser_template_argument_list for the
17946 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17950 tree saved_qualifying_scope;
17951 tree saved_object_scope;
17952 bool saved_greater_than_is_operator_p;
17953 bool saved_skip_evaluation;
17957 When parsing a template-id, the first non-nested `>' is taken as
17958 the end of the template-argument-list rather than a greater-than
17960 saved_greater_than_is_operator_p
17961 = parser->greater_than_is_operator_p;
17962 parser->greater_than_is_operator_p = false;
17963 /* Parsing the argument list may modify SCOPE, so we save it
17965 saved_scope = parser->scope;
17966 saved_qualifying_scope = parser->qualifying_scope;
17967 saved_object_scope = parser->object_scope;
17968 /* We need to evaluate the template arguments, even though this
17969 template-id may be nested within a "sizeof". */
17970 saved_skip_evaluation = skip_evaluation;
17971 skip_evaluation = false;
17972 /* Parse the template-argument-list itself. */
17973 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17974 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17975 arguments = NULL_TREE;
17977 arguments = cp_parser_template_argument_list (parser);
17978 /* Look for the `>' that ends the template-argument-list. If we find
17979 a '>>' instead, it's probably just a typo. */
17980 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17982 if (cxx_dialect != cxx98)
17984 /* In C++0x, a `>>' in a template argument list or cast
17985 expression is considered to be two separate `>'
17986 tokens. So, change the current token to a `>', but don't
17987 consume it: it will be consumed later when the outer
17988 template argument list (or cast expression) is parsed.
17989 Note that this replacement of `>' for `>>' is necessary
17990 even if we are parsing tentatively: in the tentative
17991 case, after calling
17992 cp_parser_enclosed_template_argument_list we will always
17993 throw away all of the template arguments and the first
17994 closing `>', either because the template argument list
17995 was erroneous or because we are replacing those tokens
17996 with a CPP_TEMPLATE_ID token. The second `>' (which will
17997 not have been thrown away) is needed either to close an
17998 outer template argument list or to complete a new-style
18000 cp_token *token = cp_lexer_peek_token (parser->lexer);
18001 token->type = CPP_GREATER;
18003 else if (!saved_greater_than_is_operator_p)
18005 /* If we're in a nested template argument list, the '>>' has
18006 to be a typo for '> >'. We emit the error message, but we
18007 continue parsing and we push a '>' as next token, so that
18008 the argument list will be parsed correctly. Note that the
18009 global source location is still on the token before the
18010 '>>', so we need to say explicitly where we want it. */
18011 cp_token *token = cp_lexer_peek_token (parser->lexer);
18012 error ("%H%<>>%> should be %<> >%> "
18013 "within a nested template argument list",
18016 token->type = CPP_GREATER;
18020 /* If this is not a nested template argument list, the '>>'
18021 is a typo for '>'. Emit an error message and continue.
18022 Same deal about the token location, but here we can get it
18023 right by consuming the '>>' before issuing the diagnostic. */
18024 cp_token *token = cp_lexer_consume_token (parser->lexer);
18025 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18026 "a template argument list", &token->location);
18030 cp_parser_skip_to_end_of_template_parameter_list (parser);
18031 /* The `>' token might be a greater-than operator again now. */
18032 parser->greater_than_is_operator_p
18033 = saved_greater_than_is_operator_p;
18034 /* Restore the SAVED_SCOPE. */
18035 parser->scope = saved_scope;
18036 parser->qualifying_scope = saved_qualifying_scope;
18037 parser->object_scope = saved_object_scope;
18038 skip_evaluation = saved_skip_evaluation;
18043 /* MEMBER_FUNCTION is a member function, or a friend. If default
18044 arguments, or the body of the function have not yet been parsed,
18048 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18050 /* If this member is a template, get the underlying
18052 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18053 member_function = DECL_TEMPLATE_RESULT (member_function);
18055 /* There should not be any class definitions in progress at this
18056 point; the bodies of members are only parsed outside of all class
18058 gcc_assert (parser->num_classes_being_defined == 0);
18059 /* While we're parsing the member functions we might encounter more
18060 classes. We want to handle them right away, but we don't want
18061 them getting mixed up with functions that are currently in the
18063 parser->unparsed_functions_queues
18064 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18066 /* Make sure that any template parameters are in scope. */
18067 maybe_begin_member_template_processing (member_function);
18069 /* If the body of the function has not yet been parsed, parse it
18071 if (DECL_PENDING_INLINE_P (member_function))
18073 tree function_scope;
18074 cp_token_cache *tokens;
18076 /* The function is no longer pending; we are processing it. */
18077 tokens = DECL_PENDING_INLINE_INFO (member_function);
18078 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18079 DECL_PENDING_INLINE_P (member_function) = 0;
18081 /* If this is a local class, enter the scope of the containing
18083 function_scope = current_function_decl;
18084 if (function_scope)
18085 push_function_context ();
18087 /* Push the body of the function onto the lexer stack. */
18088 cp_parser_push_lexer_for_tokens (parser, tokens);
18090 /* Let the front end know that we going to be defining this
18092 start_preparsed_function (member_function, NULL_TREE,
18093 SF_PRE_PARSED | SF_INCLASS_INLINE);
18095 /* Don't do access checking if it is a templated function. */
18096 if (processing_template_decl)
18097 push_deferring_access_checks (dk_no_check);
18099 /* Now, parse the body of the function. */
18100 cp_parser_function_definition_after_declarator (parser,
18101 /*inline_p=*/true);
18103 if (processing_template_decl)
18104 pop_deferring_access_checks ();
18106 /* Leave the scope of the containing function. */
18107 if (function_scope)
18108 pop_function_context ();
18109 cp_parser_pop_lexer (parser);
18112 /* Remove any template parameters from the symbol table. */
18113 maybe_end_member_template_processing ();
18115 /* Restore the queue. */
18116 parser->unparsed_functions_queues
18117 = TREE_CHAIN (parser->unparsed_functions_queues);
18120 /* If DECL contains any default args, remember it on the unparsed
18121 functions queue. */
18124 cp_parser_save_default_args (cp_parser* parser, tree decl)
18128 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18130 probe = TREE_CHAIN (probe))
18131 if (TREE_PURPOSE (probe))
18133 TREE_PURPOSE (parser->unparsed_functions_queues)
18134 = tree_cons (current_class_type, decl,
18135 TREE_PURPOSE (parser->unparsed_functions_queues));
18140 /* FN is a FUNCTION_DECL which may contains a parameter with an
18141 unparsed DEFAULT_ARG. Parse the default args now. This function
18142 assumes that the current scope is the scope in which the default
18143 argument should be processed. */
18146 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18148 bool saved_local_variables_forbidden_p;
18151 /* While we're parsing the default args, we might (due to the
18152 statement expression extension) encounter more classes. We want
18153 to handle them right away, but we don't want them getting mixed
18154 up with default args that are currently in the queue. */
18155 parser->unparsed_functions_queues
18156 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18158 /* Local variable names (and the `this' keyword) may not appear
18159 in a default argument. */
18160 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18161 parser->local_variables_forbidden_p = true;
18163 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18165 parm = TREE_CHAIN (parm))
18167 cp_token_cache *tokens;
18168 tree default_arg = TREE_PURPOSE (parm);
18170 VEC(tree,gc) *insts;
18177 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18178 /* This can happen for a friend declaration for a function
18179 already declared with default arguments. */
18182 /* Push the saved tokens for the default argument onto the parser's
18184 tokens = DEFARG_TOKENS (default_arg);
18185 cp_parser_push_lexer_for_tokens (parser, tokens);
18187 /* Parse the assignment-expression. */
18188 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18190 if (!processing_template_decl)
18191 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18193 TREE_PURPOSE (parm) = parsed_arg;
18195 /* Update any instantiations we've already created. */
18196 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18197 VEC_iterate (tree, insts, ix, copy); ix++)
18198 TREE_PURPOSE (copy) = parsed_arg;
18200 /* If the token stream has not been completely used up, then
18201 there was extra junk after the end of the default
18203 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18204 cp_parser_error (parser, "expected %<,%>");
18206 /* Revert to the main lexer. */
18207 cp_parser_pop_lexer (parser);
18210 /* Make sure no default arg is missing. */
18211 check_default_args (fn);
18213 /* Restore the state of local_variables_forbidden_p. */
18214 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18216 /* Restore the queue. */
18217 parser->unparsed_functions_queues
18218 = TREE_CHAIN (parser->unparsed_functions_queues);
18221 /* Parse the operand of `sizeof' (or a similar operator). Returns
18222 either a TYPE or an expression, depending on the form of the
18223 input. The KEYWORD indicates which kind of expression we have
18227 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18229 tree expr = NULL_TREE;
18230 const char *saved_message;
18232 bool saved_integral_constant_expression_p;
18233 bool saved_non_integral_constant_expression_p;
18234 bool pack_expansion_p = false;
18236 /* Types cannot be defined in a `sizeof' expression. Save away the
18238 saved_message = parser->type_definition_forbidden_message;
18239 /* And create the new one. */
18240 tmp = concat ("types may not be defined in %<",
18241 IDENTIFIER_POINTER (ridpointers[keyword]),
18242 "%> expressions", NULL);
18243 parser->type_definition_forbidden_message = tmp;
18245 /* The restrictions on constant-expressions do not apply inside
18246 sizeof expressions. */
18247 saved_integral_constant_expression_p
18248 = parser->integral_constant_expression_p;
18249 saved_non_integral_constant_expression_p
18250 = parser->non_integral_constant_expression_p;
18251 parser->integral_constant_expression_p = false;
18253 /* If it's a `...', then we are computing the length of a parameter
18255 if (keyword == RID_SIZEOF
18256 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18258 /* Consume the `...'. */
18259 cp_lexer_consume_token (parser->lexer);
18260 maybe_warn_variadic_templates ();
18262 /* Note that this is an expansion. */
18263 pack_expansion_p = true;
18266 /* Do not actually evaluate the expression. */
18268 /* If it's a `(', then we might be looking at the type-id
18270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18273 bool saved_in_type_id_in_expr_p;
18275 /* We can't be sure yet whether we're looking at a type-id or an
18277 cp_parser_parse_tentatively (parser);
18278 /* Consume the `('. */
18279 cp_lexer_consume_token (parser->lexer);
18280 /* Parse the type-id. */
18281 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18282 parser->in_type_id_in_expr_p = true;
18283 type = cp_parser_type_id (parser);
18284 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18285 /* Now, look for the trailing `)'. */
18286 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18287 /* If all went well, then we're done. */
18288 if (cp_parser_parse_definitely (parser))
18290 cp_decl_specifier_seq decl_specs;
18292 /* Build a trivial decl-specifier-seq. */
18293 clear_decl_specs (&decl_specs);
18294 decl_specs.type = type;
18296 /* Call grokdeclarator to figure out what type this is. */
18297 expr = grokdeclarator (NULL,
18301 /*attrlist=*/NULL);
18305 /* If the type-id production did not work out, then we must be
18306 looking at the unary-expression production. */
18308 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18311 if (pack_expansion_p)
18312 /* Build a pack expansion. */
18313 expr = make_pack_expansion (expr);
18315 /* Go back to evaluating expressions. */
18318 /* Free the message we created. */
18320 /* And restore the old one. */
18321 parser->type_definition_forbidden_message = saved_message;
18322 parser->integral_constant_expression_p
18323 = saved_integral_constant_expression_p;
18324 parser->non_integral_constant_expression_p
18325 = saved_non_integral_constant_expression_p;
18330 /* If the current declaration has no declarator, return true. */
18333 cp_parser_declares_only_class_p (cp_parser *parser)
18335 /* If the next token is a `;' or a `,' then there is no
18337 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18338 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18341 /* Update the DECL_SPECS to reflect the storage class indicated by
18345 cp_parser_set_storage_class (cp_parser *parser,
18346 cp_decl_specifier_seq *decl_specs,
18348 location_t location)
18350 cp_storage_class storage_class;
18352 if (parser->in_unbraced_linkage_specification_p)
18354 error ("%Hinvalid use of %qD in linkage specification",
18355 &location, ridpointers[keyword]);
18358 else if (decl_specs->storage_class != sc_none)
18360 decl_specs->conflicting_specifiers_p = true;
18364 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18365 && decl_specs->specs[(int) ds_thread])
18367 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18368 decl_specs->specs[(int) ds_thread] = 0;
18374 storage_class = sc_auto;
18377 storage_class = sc_register;
18380 storage_class = sc_static;
18383 storage_class = sc_extern;
18386 storage_class = sc_mutable;
18389 gcc_unreachable ();
18391 decl_specs->storage_class = storage_class;
18393 /* A storage class specifier cannot be applied alongside a typedef
18394 specifier. If there is a typedef specifier present then set
18395 conflicting_specifiers_p which will trigger an error later
18396 on in grokdeclarator. */
18397 if (decl_specs->specs[(int)ds_typedef])
18398 decl_specs->conflicting_specifiers_p = true;
18401 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18402 is true, the type is a user-defined type; otherwise it is a
18403 built-in type specified by a keyword. */
18406 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18408 location_t location,
18409 bool user_defined_p)
18411 decl_specs->any_specifiers_p = true;
18413 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18414 (with, for example, in "typedef int wchar_t;") we remember that
18415 this is what happened. In system headers, we ignore these
18416 declarations so that G++ can work with system headers that are not
18418 if (decl_specs->specs[(int) ds_typedef]
18420 && (type_spec == boolean_type_node
18421 || type_spec == char16_type_node
18422 || type_spec == char32_type_node
18423 || type_spec == wchar_type_node)
18424 && (decl_specs->type
18425 || decl_specs->specs[(int) ds_long]
18426 || decl_specs->specs[(int) ds_short]
18427 || decl_specs->specs[(int) ds_unsigned]
18428 || decl_specs->specs[(int) ds_signed]))
18430 decl_specs->redefined_builtin_type = type_spec;
18431 if (!decl_specs->type)
18433 decl_specs->type = type_spec;
18434 decl_specs->user_defined_type_p = false;
18435 decl_specs->type_location = location;
18438 else if (decl_specs->type)
18439 decl_specs->multiple_types_p = true;
18442 decl_specs->type = type_spec;
18443 decl_specs->user_defined_type_p = user_defined_p;
18444 decl_specs->redefined_builtin_type = NULL_TREE;
18445 decl_specs->type_location = location;
18449 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18450 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18453 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18455 return decl_specifiers->specs[(int) ds_friend] != 0;
18458 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18459 issue an error message indicating that TOKEN_DESC was expected.
18461 Returns the token consumed, if the token had the appropriate type.
18462 Otherwise, returns NULL. */
18465 cp_parser_require (cp_parser* parser,
18466 enum cpp_ttype type,
18467 const char* token_desc)
18469 if (cp_lexer_next_token_is (parser->lexer, type))
18470 return cp_lexer_consume_token (parser->lexer);
18473 /* Output the MESSAGE -- unless we're parsing tentatively. */
18474 if (!cp_parser_simulate_error (parser))
18476 char *message = concat ("expected ", token_desc, NULL);
18477 cp_parser_error (parser, message);
18484 /* An error message is produced if the next token is not '>'.
18485 All further tokens are skipped until the desired token is
18486 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18489 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18491 /* Current level of '< ... >'. */
18492 unsigned level = 0;
18493 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18494 unsigned nesting_depth = 0;
18496 /* Are we ready, yet? If not, issue error message. */
18497 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18500 /* Skip tokens until the desired token is found. */
18503 /* Peek at the next token. */
18504 switch (cp_lexer_peek_token (parser->lexer)->type)
18507 if (!nesting_depth)
18512 if (cxx_dialect == cxx98)
18513 /* C++0x views the `>>' operator as two `>' tokens, but
18516 else if (!nesting_depth && level-- == 0)
18518 /* We've hit a `>>' where the first `>' closes the
18519 template argument list, and the second `>' is
18520 spurious. Just consume the `>>' and stop; we've
18521 already produced at least one error. */
18522 cp_lexer_consume_token (parser->lexer);
18525 /* Fall through for C++0x, so we handle the second `>' in
18529 if (!nesting_depth && level-- == 0)
18531 /* We've reached the token we want, consume it and stop. */
18532 cp_lexer_consume_token (parser->lexer);
18537 case CPP_OPEN_PAREN:
18538 case CPP_OPEN_SQUARE:
18542 case CPP_CLOSE_PAREN:
18543 case CPP_CLOSE_SQUARE:
18544 if (nesting_depth-- == 0)
18549 case CPP_PRAGMA_EOL:
18550 case CPP_SEMICOLON:
18551 case CPP_OPEN_BRACE:
18552 case CPP_CLOSE_BRACE:
18553 /* The '>' was probably forgotten, don't look further. */
18560 /* Consume this token. */
18561 cp_lexer_consume_token (parser->lexer);
18565 /* If the next token is the indicated keyword, consume it. Otherwise,
18566 issue an error message indicating that TOKEN_DESC was expected.
18568 Returns the token consumed, if the token had the appropriate type.
18569 Otherwise, returns NULL. */
18572 cp_parser_require_keyword (cp_parser* parser,
18574 const char* token_desc)
18576 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18578 if (token && token->keyword != keyword)
18580 dyn_string_t error_msg;
18582 /* Format the error message. */
18583 error_msg = dyn_string_new (0);
18584 dyn_string_append_cstr (error_msg, "expected ");
18585 dyn_string_append_cstr (error_msg, token_desc);
18586 cp_parser_error (parser, error_msg->s);
18587 dyn_string_delete (error_msg);
18594 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18595 function-definition. */
18598 cp_parser_token_starts_function_definition_p (cp_token* token)
18600 return (/* An ordinary function-body begins with an `{'. */
18601 token->type == CPP_OPEN_BRACE
18602 /* A ctor-initializer begins with a `:'. */
18603 || token->type == CPP_COLON
18604 /* A function-try-block begins with `try'. */
18605 || token->keyword == RID_TRY
18606 /* The named return value extension begins with `return'. */
18607 || token->keyword == RID_RETURN);
18610 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18614 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18618 token = cp_lexer_peek_token (parser->lexer);
18619 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18622 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18623 C++0x) ending a template-argument. */
18626 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18630 token = cp_lexer_peek_token (parser->lexer);
18631 return (token->type == CPP_COMMA
18632 || token->type == CPP_GREATER
18633 || token->type == CPP_ELLIPSIS
18634 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18637 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18638 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18641 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18646 token = cp_lexer_peek_nth_token (parser->lexer, n);
18647 if (token->type == CPP_LESS)
18649 /* Check for the sequence `<::' in the original code. It would be lexed as
18650 `[:', where `[' is a digraph, and there is no whitespace before
18652 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18655 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18656 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18662 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18663 or none_type otherwise. */
18665 static enum tag_types
18666 cp_parser_token_is_class_key (cp_token* token)
18668 switch (token->keyword)
18673 return record_type;
18682 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18685 cp_parser_check_class_key (enum tag_types class_key, tree type)
18687 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18688 permerror (input_location, "%qs tag used in naming %q#T",
18689 class_key == union_type ? "union"
18690 : class_key == record_type ? "struct" : "class",
18694 /* Issue an error message if DECL is redeclared with different
18695 access than its original declaration [class.access.spec/3].
18696 This applies to nested classes and nested class templates.
18700 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18702 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18705 if ((TREE_PRIVATE (decl)
18706 != (current_access_specifier == access_private_node))
18707 || (TREE_PROTECTED (decl)
18708 != (current_access_specifier == access_protected_node)))
18709 error ("%H%qD redeclared with different access", &location, decl);
18712 /* Look for the `template' keyword, as a syntactic disambiguator.
18713 Return TRUE iff it is present, in which case it will be
18717 cp_parser_optional_template_keyword (cp_parser *parser)
18719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18721 /* The `template' keyword can only be used within templates;
18722 outside templates the parser can always figure out what is a
18723 template and what is not. */
18724 if (!processing_template_decl)
18726 cp_token *token = cp_lexer_peek_token (parser->lexer);
18727 error ("%H%<template%> (as a disambiguator) is only allowed "
18728 "within templates", &token->location);
18729 /* If this part of the token stream is rescanned, the same
18730 error message would be generated. So, we purge the token
18731 from the stream. */
18732 cp_lexer_purge_token (parser->lexer);
18737 /* Consume the `template' keyword. */
18738 cp_lexer_consume_token (parser->lexer);
18746 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18747 set PARSER->SCOPE, and perform other related actions. */
18750 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18753 struct tree_check *check_value;
18754 deferred_access_check *chk;
18755 VEC (deferred_access_check,gc) *checks;
18757 /* Get the stored value. */
18758 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18759 /* Perform any access checks that were deferred. */
18760 checks = check_value->checks;
18764 VEC_iterate (deferred_access_check, checks, i, chk) ;
18767 perform_or_defer_access_check (chk->binfo,
18772 /* Set the scope from the stored value. */
18773 parser->scope = check_value->value;
18774 parser->qualifying_scope = check_value->qualifying_scope;
18775 parser->object_scope = NULL_TREE;
18778 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18779 encounter the end of a block before what we were looking for. */
18782 cp_parser_cache_group (cp_parser *parser,
18783 enum cpp_ttype end,
18788 cp_token *token = cp_lexer_peek_token (parser->lexer);
18790 /* Abort a parenthesized expression if we encounter a semicolon. */
18791 if ((end == CPP_CLOSE_PAREN || depth == 0)
18792 && token->type == CPP_SEMICOLON)
18794 /* If we've reached the end of the file, stop. */
18795 if (token->type == CPP_EOF
18796 || (end != CPP_PRAGMA_EOL
18797 && token->type == CPP_PRAGMA_EOL))
18799 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18800 /* We've hit the end of an enclosing block, so there's been some
18801 kind of syntax error. */
18804 /* Consume the token. */
18805 cp_lexer_consume_token (parser->lexer);
18806 /* See if it starts a new group. */
18807 if (token->type == CPP_OPEN_BRACE)
18809 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18810 /* In theory this should probably check end == '}', but
18811 cp_parser_save_member_function_body needs it to exit
18812 after either '}' or ')' when called with ')'. */
18816 else if (token->type == CPP_OPEN_PAREN)
18818 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18819 if (depth == 0 && end == CPP_CLOSE_PAREN)
18822 else if (token->type == CPP_PRAGMA)
18823 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18824 else if (token->type == end)
18829 /* Begin parsing tentatively. We always save tokens while parsing
18830 tentatively so that if the tentative parsing fails we can restore the
18834 cp_parser_parse_tentatively (cp_parser* parser)
18836 /* Enter a new parsing context. */
18837 parser->context = cp_parser_context_new (parser->context);
18838 /* Begin saving tokens. */
18839 cp_lexer_save_tokens (parser->lexer);
18840 /* In order to avoid repetitive access control error messages,
18841 access checks are queued up until we are no longer parsing
18843 push_deferring_access_checks (dk_deferred);
18846 /* Commit to the currently active tentative parse. */
18849 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18851 cp_parser_context *context;
18854 /* Mark all of the levels as committed. */
18855 lexer = parser->lexer;
18856 for (context = parser->context; context->next; context = context->next)
18858 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18860 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18861 while (!cp_lexer_saving_tokens (lexer))
18862 lexer = lexer->next;
18863 cp_lexer_commit_tokens (lexer);
18867 /* Abort the currently active tentative parse. All consumed tokens
18868 will be rolled back, and no diagnostics will be issued. */
18871 cp_parser_abort_tentative_parse (cp_parser* parser)
18873 cp_parser_simulate_error (parser);
18874 /* Now, pretend that we want to see if the construct was
18875 successfully parsed. */
18876 cp_parser_parse_definitely (parser);
18879 /* Stop parsing tentatively. If a parse error has occurred, restore the
18880 token stream. Otherwise, commit to the tokens we have consumed.
18881 Returns true if no error occurred; false otherwise. */
18884 cp_parser_parse_definitely (cp_parser* parser)
18886 bool error_occurred;
18887 cp_parser_context *context;
18889 /* Remember whether or not an error occurred, since we are about to
18890 destroy that information. */
18891 error_occurred = cp_parser_error_occurred (parser);
18892 /* Remove the topmost context from the stack. */
18893 context = parser->context;
18894 parser->context = context->next;
18895 /* If no parse errors occurred, commit to the tentative parse. */
18896 if (!error_occurred)
18898 /* Commit to the tokens read tentatively, unless that was
18900 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18901 cp_lexer_commit_tokens (parser->lexer);
18903 pop_to_parent_deferring_access_checks ();
18905 /* Otherwise, if errors occurred, roll back our state so that things
18906 are just as they were before we began the tentative parse. */
18909 cp_lexer_rollback_tokens (parser->lexer);
18910 pop_deferring_access_checks ();
18912 /* Add the context to the front of the free list. */
18913 context->next = cp_parser_context_free_list;
18914 cp_parser_context_free_list = context;
18916 return !error_occurred;
18919 /* Returns true if we are parsing tentatively and are not committed to
18920 this tentative parse. */
18923 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18925 return (cp_parser_parsing_tentatively (parser)
18926 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18929 /* Returns nonzero iff an error has occurred during the most recent
18930 tentative parse. */
18933 cp_parser_error_occurred (cp_parser* parser)
18935 return (cp_parser_parsing_tentatively (parser)
18936 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18939 /* Returns nonzero if GNU extensions are allowed. */
18942 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18944 return parser->allow_gnu_extensions_p;
18947 /* Objective-C++ Productions */
18950 /* Parse an Objective-C expression, which feeds into a primary-expression
18954 objc-message-expression
18955 objc-string-literal
18956 objc-encode-expression
18957 objc-protocol-expression
18958 objc-selector-expression
18960 Returns a tree representation of the expression. */
18963 cp_parser_objc_expression (cp_parser* parser)
18965 /* Try to figure out what kind of declaration is present. */
18966 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18970 case CPP_OPEN_SQUARE:
18971 return cp_parser_objc_message_expression (parser);
18973 case CPP_OBJC_STRING:
18974 kwd = cp_lexer_consume_token (parser->lexer);
18975 return objc_build_string_object (kwd->u.value);
18978 switch (kwd->keyword)
18980 case RID_AT_ENCODE:
18981 return cp_parser_objc_encode_expression (parser);
18983 case RID_AT_PROTOCOL:
18984 return cp_parser_objc_protocol_expression (parser);
18986 case RID_AT_SELECTOR:
18987 return cp_parser_objc_selector_expression (parser);
18993 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18994 &kwd->location, kwd->u.value);
18995 cp_parser_skip_to_end_of_block_or_statement (parser);
18998 return error_mark_node;
19001 /* Parse an Objective-C message expression.
19003 objc-message-expression:
19004 [ objc-message-receiver objc-message-args ]
19006 Returns a representation of an Objective-C message. */
19009 cp_parser_objc_message_expression (cp_parser* parser)
19011 tree receiver, messageargs;
19013 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19014 receiver = cp_parser_objc_message_receiver (parser);
19015 messageargs = cp_parser_objc_message_args (parser);
19016 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19018 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19021 /* Parse an objc-message-receiver.
19023 objc-message-receiver:
19025 simple-type-specifier
19027 Returns a representation of the type or expression. */
19030 cp_parser_objc_message_receiver (cp_parser* parser)
19034 /* An Objective-C message receiver may be either (1) a type
19035 or (2) an expression. */
19036 cp_parser_parse_tentatively (parser);
19037 rcv = cp_parser_expression (parser, false);
19039 if (cp_parser_parse_definitely (parser))
19042 rcv = cp_parser_simple_type_specifier (parser,
19043 /*decl_specs=*/NULL,
19044 CP_PARSER_FLAGS_NONE);
19046 return objc_get_class_reference (rcv);
19049 /* Parse the arguments and selectors comprising an Objective-C message.
19054 objc-selector-args , objc-comma-args
19056 objc-selector-args:
19057 objc-selector [opt] : assignment-expression
19058 objc-selector-args objc-selector [opt] : assignment-expression
19061 assignment-expression
19062 objc-comma-args , assignment-expression
19064 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19065 selector arguments and TREE_VALUE containing a list of comma
19069 cp_parser_objc_message_args (cp_parser* parser)
19071 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19072 bool maybe_unary_selector_p = true;
19073 cp_token *token = cp_lexer_peek_token (parser->lexer);
19075 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19077 tree selector = NULL_TREE, arg;
19079 if (token->type != CPP_COLON)
19080 selector = cp_parser_objc_selector (parser);
19082 /* Detect if we have a unary selector. */
19083 if (maybe_unary_selector_p
19084 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19085 return build_tree_list (selector, NULL_TREE);
19087 maybe_unary_selector_p = false;
19088 cp_parser_require (parser, CPP_COLON, "%<:%>");
19089 arg = cp_parser_assignment_expression (parser, false);
19092 = chainon (sel_args,
19093 build_tree_list (selector, arg));
19095 token = cp_lexer_peek_token (parser->lexer);
19098 /* Handle non-selector arguments, if any. */
19099 while (token->type == CPP_COMMA)
19103 cp_lexer_consume_token (parser->lexer);
19104 arg = cp_parser_assignment_expression (parser, false);
19107 = chainon (addl_args,
19108 build_tree_list (NULL_TREE, arg));
19110 token = cp_lexer_peek_token (parser->lexer);
19113 return build_tree_list (sel_args, addl_args);
19116 /* Parse an Objective-C encode expression.
19118 objc-encode-expression:
19119 @encode objc-typename
19121 Returns an encoded representation of the type argument. */
19124 cp_parser_objc_encode_expression (cp_parser* parser)
19129 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19130 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19131 token = cp_lexer_peek_token (parser->lexer);
19132 type = complete_type (cp_parser_type_id (parser));
19133 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19137 error ("%H%<@encode%> must specify a type as an argument",
19139 return error_mark_node;
19142 return objc_build_encode_expr (type);
19145 /* Parse an Objective-C @defs expression. */
19148 cp_parser_objc_defs_expression (cp_parser *parser)
19152 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19153 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19154 name = cp_parser_identifier (parser);
19155 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19157 return objc_get_class_ivars (name);
19160 /* Parse an Objective-C protocol expression.
19162 objc-protocol-expression:
19163 @protocol ( identifier )
19165 Returns a representation of the protocol expression. */
19168 cp_parser_objc_protocol_expression (cp_parser* parser)
19172 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19173 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19174 proto = cp_parser_identifier (parser);
19175 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19177 return objc_build_protocol_expr (proto);
19180 /* Parse an Objective-C selector expression.
19182 objc-selector-expression:
19183 @selector ( objc-method-signature )
19185 objc-method-signature:
19191 objc-selector-seq objc-selector :
19193 Returns a representation of the method selector. */
19196 cp_parser_objc_selector_expression (cp_parser* parser)
19198 tree sel_seq = NULL_TREE;
19199 bool maybe_unary_selector_p = true;
19202 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19203 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19204 token = cp_lexer_peek_token (parser->lexer);
19206 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19207 || token->type == CPP_SCOPE)
19209 tree selector = NULL_TREE;
19211 if (token->type != CPP_COLON
19212 || token->type == CPP_SCOPE)
19213 selector = cp_parser_objc_selector (parser);
19215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19216 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19218 /* Detect if we have a unary selector. */
19219 if (maybe_unary_selector_p)
19221 sel_seq = selector;
19222 goto finish_selector;
19226 cp_parser_error (parser, "expected %<:%>");
19229 maybe_unary_selector_p = false;
19230 token = cp_lexer_consume_token (parser->lexer);
19232 if (token->type == CPP_SCOPE)
19235 = chainon (sel_seq,
19236 build_tree_list (selector, NULL_TREE));
19238 = chainon (sel_seq,
19239 build_tree_list (NULL_TREE, NULL_TREE));
19243 = chainon (sel_seq,
19244 build_tree_list (selector, NULL_TREE));
19246 token = cp_lexer_peek_token (parser->lexer);
19250 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19252 return objc_build_selector_expr (sel_seq);
19255 /* Parse a list of identifiers.
19257 objc-identifier-list:
19259 objc-identifier-list , identifier
19261 Returns a TREE_LIST of identifier nodes. */
19264 cp_parser_objc_identifier_list (cp_parser* parser)
19266 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19267 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19269 while (sep->type == CPP_COMMA)
19271 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19272 list = chainon (list,
19273 build_tree_list (NULL_TREE,
19274 cp_parser_identifier (parser)));
19275 sep = cp_lexer_peek_token (parser->lexer);
19281 /* Parse an Objective-C alias declaration.
19283 objc-alias-declaration:
19284 @compatibility_alias identifier identifier ;
19286 This function registers the alias mapping with the Objective-C front end.
19287 It returns nothing. */
19290 cp_parser_objc_alias_declaration (cp_parser* parser)
19294 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19295 alias = cp_parser_identifier (parser);
19296 orig = cp_parser_identifier (parser);
19297 objc_declare_alias (alias, orig);
19298 cp_parser_consume_semicolon_at_end_of_statement (parser);
19301 /* Parse an Objective-C class forward-declaration.
19303 objc-class-declaration:
19304 @class objc-identifier-list ;
19306 The function registers the forward declarations with the Objective-C
19307 front end. It returns nothing. */
19310 cp_parser_objc_class_declaration (cp_parser* parser)
19312 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19313 objc_declare_class (cp_parser_objc_identifier_list (parser));
19314 cp_parser_consume_semicolon_at_end_of_statement (parser);
19317 /* Parse a list of Objective-C protocol references.
19319 objc-protocol-refs-opt:
19320 objc-protocol-refs [opt]
19322 objc-protocol-refs:
19323 < objc-identifier-list >
19325 Returns a TREE_LIST of identifiers, if any. */
19328 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19330 tree protorefs = NULL_TREE;
19332 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19334 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19335 protorefs = cp_parser_objc_identifier_list (parser);
19336 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19342 /* Parse a Objective-C visibility specification. */
19345 cp_parser_objc_visibility_spec (cp_parser* parser)
19347 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19349 switch (vis->keyword)
19351 case RID_AT_PRIVATE:
19352 objc_set_visibility (2);
19354 case RID_AT_PROTECTED:
19355 objc_set_visibility (0);
19357 case RID_AT_PUBLIC:
19358 objc_set_visibility (1);
19364 /* Eat '@private'/'@protected'/'@public'. */
19365 cp_lexer_consume_token (parser->lexer);
19368 /* Parse an Objective-C method type. */
19371 cp_parser_objc_method_type (cp_parser* parser)
19373 objc_set_method_type
19374 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19379 /* Parse an Objective-C protocol qualifier. */
19382 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19384 tree quals = NULL_TREE, node;
19385 cp_token *token = cp_lexer_peek_token (parser->lexer);
19387 node = token->u.value;
19389 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19390 && (node == ridpointers [(int) RID_IN]
19391 || node == ridpointers [(int) RID_OUT]
19392 || node == ridpointers [(int) RID_INOUT]
19393 || node == ridpointers [(int) RID_BYCOPY]
19394 || node == ridpointers [(int) RID_BYREF]
19395 || node == ridpointers [(int) RID_ONEWAY]))
19397 quals = tree_cons (NULL_TREE, node, quals);
19398 cp_lexer_consume_token (parser->lexer);
19399 token = cp_lexer_peek_token (parser->lexer);
19400 node = token->u.value;
19406 /* Parse an Objective-C typename. */
19409 cp_parser_objc_typename (cp_parser* parser)
19411 tree type_name = NULL_TREE;
19413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19415 tree proto_quals, cp_type = NULL_TREE;
19417 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19418 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19420 /* An ObjC type name may consist of just protocol qualifiers, in which
19421 case the type shall default to 'id'. */
19422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19423 cp_type = cp_parser_type_id (parser);
19425 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19426 type_name = build_tree_list (proto_quals, cp_type);
19432 /* Check to see if TYPE refers to an Objective-C selector name. */
19435 cp_parser_objc_selector_p (enum cpp_ttype type)
19437 return (type == CPP_NAME || type == CPP_KEYWORD
19438 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19439 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19440 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19441 || type == CPP_XOR || type == CPP_XOR_EQ);
19444 /* Parse an Objective-C selector. */
19447 cp_parser_objc_selector (cp_parser* parser)
19449 cp_token *token = cp_lexer_consume_token (parser->lexer);
19451 if (!cp_parser_objc_selector_p (token->type))
19453 error ("%Hinvalid Objective-C++ selector name", &token->location);
19454 return error_mark_node;
19457 /* C++ operator names are allowed to appear in ObjC selectors. */
19458 switch (token->type)
19460 case CPP_AND_AND: return get_identifier ("and");
19461 case CPP_AND_EQ: return get_identifier ("and_eq");
19462 case CPP_AND: return get_identifier ("bitand");
19463 case CPP_OR: return get_identifier ("bitor");
19464 case CPP_COMPL: return get_identifier ("compl");
19465 case CPP_NOT: return get_identifier ("not");
19466 case CPP_NOT_EQ: return get_identifier ("not_eq");
19467 case CPP_OR_OR: return get_identifier ("or");
19468 case CPP_OR_EQ: return get_identifier ("or_eq");
19469 case CPP_XOR: return get_identifier ("xor");
19470 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19471 default: return token->u.value;
19475 /* Parse an Objective-C params list. */
19478 cp_parser_objc_method_keyword_params (cp_parser* parser)
19480 tree params = NULL_TREE;
19481 bool maybe_unary_selector_p = true;
19482 cp_token *token = cp_lexer_peek_token (parser->lexer);
19484 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19486 tree selector = NULL_TREE, type_name, identifier;
19488 if (token->type != CPP_COLON)
19489 selector = cp_parser_objc_selector (parser);
19491 /* Detect if we have a unary selector. */
19492 if (maybe_unary_selector_p
19493 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19496 maybe_unary_selector_p = false;
19497 cp_parser_require (parser, CPP_COLON, "%<:%>");
19498 type_name = cp_parser_objc_typename (parser);
19499 identifier = cp_parser_identifier (parser);
19503 objc_build_keyword_decl (selector,
19507 token = cp_lexer_peek_token (parser->lexer);
19513 /* Parse the non-keyword Objective-C params. */
19516 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19518 tree params = make_node (TREE_LIST);
19519 cp_token *token = cp_lexer_peek_token (parser->lexer);
19520 *ellipsisp = false; /* Initially, assume no ellipsis. */
19522 while (token->type == CPP_COMMA)
19524 cp_parameter_declarator *parmdecl;
19527 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19528 token = cp_lexer_peek_token (parser->lexer);
19530 if (token->type == CPP_ELLIPSIS)
19532 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19537 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19538 parm = grokdeclarator (parmdecl->declarator,
19539 &parmdecl->decl_specifiers,
19540 PARM, /*initialized=*/0,
19541 /*attrlist=*/NULL);
19543 chainon (params, build_tree_list (NULL_TREE, parm));
19544 token = cp_lexer_peek_token (parser->lexer);
19550 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19553 cp_parser_objc_interstitial_code (cp_parser* parser)
19555 cp_token *token = cp_lexer_peek_token (parser->lexer);
19557 /* If the next token is `extern' and the following token is a string
19558 literal, then we have a linkage specification. */
19559 if (token->keyword == RID_EXTERN
19560 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19561 cp_parser_linkage_specification (parser);
19562 /* Handle #pragma, if any. */
19563 else if (token->type == CPP_PRAGMA)
19564 cp_parser_pragma (parser, pragma_external);
19565 /* Allow stray semicolons. */
19566 else if (token->type == CPP_SEMICOLON)
19567 cp_lexer_consume_token (parser->lexer);
19568 /* Finally, try to parse a block-declaration, or a function-definition. */
19570 cp_parser_block_declaration (parser, /*statement_p=*/false);
19573 /* Parse a method signature. */
19576 cp_parser_objc_method_signature (cp_parser* parser)
19578 tree rettype, kwdparms, optparms;
19579 bool ellipsis = false;
19581 cp_parser_objc_method_type (parser);
19582 rettype = cp_parser_objc_typename (parser);
19583 kwdparms = cp_parser_objc_method_keyword_params (parser);
19584 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19586 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19589 /* Pars an Objective-C method prototype list. */
19592 cp_parser_objc_method_prototype_list (cp_parser* parser)
19594 cp_token *token = cp_lexer_peek_token (parser->lexer);
19596 while (token->keyword != RID_AT_END)
19598 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19600 objc_add_method_declaration
19601 (cp_parser_objc_method_signature (parser));
19602 cp_parser_consume_semicolon_at_end_of_statement (parser);
19605 /* Allow for interspersed non-ObjC++ code. */
19606 cp_parser_objc_interstitial_code (parser);
19608 token = cp_lexer_peek_token (parser->lexer);
19611 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19612 objc_finish_interface ();
19615 /* Parse an Objective-C method definition list. */
19618 cp_parser_objc_method_definition_list (cp_parser* parser)
19620 cp_token *token = cp_lexer_peek_token (parser->lexer);
19622 while (token->keyword != RID_AT_END)
19626 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19628 push_deferring_access_checks (dk_deferred);
19629 objc_start_method_definition
19630 (cp_parser_objc_method_signature (parser));
19632 /* For historical reasons, we accept an optional semicolon. */
19633 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19634 cp_lexer_consume_token (parser->lexer);
19636 perform_deferred_access_checks ();
19637 stop_deferring_access_checks ();
19638 meth = cp_parser_function_definition_after_declarator (parser,
19640 pop_deferring_access_checks ();
19641 objc_finish_method_definition (meth);
19644 /* Allow for interspersed non-ObjC++ code. */
19645 cp_parser_objc_interstitial_code (parser);
19647 token = cp_lexer_peek_token (parser->lexer);
19650 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19651 objc_finish_implementation ();
19654 /* Parse Objective-C ivars. */
19657 cp_parser_objc_class_ivars (cp_parser* parser)
19659 cp_token *token = cp_lexer_peek_token (parser->lexer);
19661 if (token->type != CPP_OPEN_BRACE)
19662 return; /* No ivars specified. */
19664 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19665 token = cp_lexer_peek_token (parser->lexer);
19667 while (token->type != CPP_CLOSE_BRACE)
19669 cp_decl_specifier_seq declspecs;
19670 int decl_class_or_enum_p;
19671 tree prefix_attributes;
19673 cp_parser_objc_visibility_spec (parser);
19675 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19678 cp_parser_decl_specifier_seq (parser,
19679 CP_PARSER_FLAGS_OPTIONAL,
19681 &decl_class_or_enum_p);
19682 prefix_attributes = declspecs.attributes;
19683 declspecs.attributes = NULL_TREE;
19685 /* Keep going until we hit the `;' at the end of the
19687 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19689 tree width = NULL_TREE, attributes, first_attribute, decl;
19690 cp_declarator *declarator = NULL;
19691 int ctor_dtor_or_conv_p;
19693 /* Check for a (possibly unnamed) bitfield declaration. */
19694 token = cp_lexer_peek_token (parser->lexer);
19695 if (token->type == CPP_COLON)
19698 if (token->type == CPP_NAME
19699 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19702 /* Get the name of the bitfield. */
19703 declarator = make_id_declarator (NULL_TREE,
19704 cp_parser_identifier (parser),
19708 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19709 /* Get the width of the bitfield. */
19711 = cp_parser_constant_expression (parser,
19712 /*allow_non_constant=*/false,
19717 /* Parse the declarator. */
19719 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19720 &ctor_dtor_or_conv_p,
19721 /*parenthesized_p=*/NULL,
19722 /*member_p=*/false);
19725 /* Look for attributes that apply to the ivar. */
19726 attributes = cp_parser_attributes_opt (parser);
19727 /* Remember which attributes are prefix attributes and
19729 first_attribute = attributes;
19730 /* Combine the attributes. */
19731 attributes = chainon (prefix_attributes, attributes);
19734 /* Create the bitfield declaration. */
19735 decl = grokbitfield (declarator, &declspecs,
19739 decl = grokfield (declarator, &declspecs,
19740 NULL_TREE, /*init_const_expr_p=*/false,
19741 NULL_TREE, attributes);
19743 /* Add the instance variable. */
19744 objc_add_instance_variable (decl);
19746 /* Reset PREFIX_ATTRIBUTES. */
19747 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19748 attributes = TREE_CHAIN (attributes);
19750 TREE_CHAIN (attributes) = NULL_TREE;
19752 token = cp_lexer_peek_token (parser->lexer);
19754 if (token->type == CPP_COMMA)
19756 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19762 cp_parser_consume_semicolon_at_end_of_statement (parser);
19763 token = cp_lexer_peek_token (parser->lexer);
19766 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19767 /* For historical reasons, we accept an optional semicolon. */
19768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19769 cp_lexer_consume_token (parser->lexer);
19772 /* Parse an Objective-C protocol declaration. */
19775 cp_parser_objc_protocol_declaration (cp_parser* parser)
19777 tree proto, protorefs;
19780 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19783 tok = cp_lexer_peek_token (parser->lexer);
19784 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19788 /* See if we have a forward declaration or a definition. */
19789 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19791 /* Try a forward declaration first. */
19792 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19794 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19796 cp_parser_consume_semicolon_at_end_of_statement (parser);
19799 /* Ok, we got a full-fledged definition (or at least should). */
19802 proto = cp_parser_identifier (parser);
19803 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19804 objc_start_protocol (proto, protorefs);
19805 cp_parser_objc_method_prototype_list (parser);
19809 /* Parse an Objective-C superclass or category. */
19812 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19815 cp_token *next = cp_lexer_peek_token (parser->lexer);
19817 *super = *categ = NULL_TREE;
19818 if (next->type == CPP_COLON)
19820 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19821 *super = cp_parser_identifier (parser);
19823 else if (next->type == CPP_OPEN_PAREN)
19825 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19826 *categ = cp_parser_identifier (parser);
19827 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19831 /* Parse an Objective-C class interface. */
19834 cp_parser_objc_class_interface (cp_parser* parser)
19836 tree name, super, categ, protos;
19838 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19839 name = cp_parser_identifier (parser);
19840 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19841 protos = cp_parser_objc_protocol_refs_opt (parser);
19843 /* We have either a class or a category on our hands. */
19845 objc_start_category_interface (name, categ, protos);
19848 objc_start_class_interface (name, super, protos);
19849 /* Handle instance variable declarations, if any. */
19850 cp_parser_objc_class_ivars (parser);
19851 objc_continue_interface ();
19854 cp_parser_objc_method_prototype_list (parser);
19857 /* Parse an Objective-C class implementation. */
19860 cp_parser_objc_class_implementation (cp_parser* parser)
19862 tree name, super, categ;
19864 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19865 name = cp_parser_identifier (parser);
19866 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19868 /* We have either a class or a category on our hands. */
19870 objc_start_category_implementation (name, categ);
19873 objc_start_class_implementation (name, super);
19874 /* Handle instance variable declarations, if any. */
19875 cp_parser_objc_class_ivars (parser);
19876 objc_continue_implementation ();
19879 cp_parser_objc_method_definition_list (parser);
19882 /* Consume the @end token and finish off the implementation. */
19885 cp_parser_objc_end_implementation (cp_parser* parser)
19887 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19888 objc_finish_implementation ();
19891 /* Parse an Objective-C declaration. */
19894 cp_parser_objc_declaration (cp_parser* parser)
19896 /* Try to figure out what kind of declaration is present. */
19897 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19899 switch (kwd->keyword)
19902 cp_parser_objc_alias_declaration (parser);
19905 cp_parser_objc_class_declaration (parser);
19907 case RID_AT_PROTOCOL:
19908 cp_parser_objc_protocol_declaration (parser);
19910 case RID_AT_INTERFACE:
19911 cp_parser_objc_class_interface (parser);
19913 case RID_AT_IMPLEMENTATION:
19914 cp_parser_objc_class_implementation (parser);
19917 cp_parser_objc_end_implementation (parser);
19920 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19921 &kwd->location, kwd->u.value);
19922 cp_parser_skip_to_end_of_block_or_statement (parser);
19926 /* Parse an Objective-C try-catch-finally statement.
19928 objc-try-catch-finally-stmt:
19929 @try compound-statement objc-catch-clause-seq [opt]
19930 objc-finally-clause [opt]
19932 objc-catch-clause-seq:
19933 objc-catch-clause objc-catch-clause-seq [opt]
19936 @catch ( exception-declaration ) compound-statement
19938 objc-finally-clause
19939 @finally compound-statement
19941 Returns NULL_TREE. */
19944 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19945 location_t location;
19948 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19949 location = cp_lexer_peek_token (parser->lexer)->location;
19950 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19951 node, lest it get absorbed into the surrounding block. */
19952 stmt = push_stmt_list ();
19953 cp_parser_compound_statement (parser, NULL, false);
19954 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19956 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19958 cp_parameter_declarator *parmdecl;
19961 cp_lexer_consume_token (parser->lexer);
19962 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19963 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19964 parm = grokdeclarator (parmdecl->declarator,
19965 &parmdecl->decl_specifiers,
19966 PARM, /*initialized=*/0,
19967 /*attrlist=*/NULL);
19968 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19969 objc_begin_catch_clause (parm);
19970 cp_parser_compound_statement (parser, NULL, false);
19971 objc_finish_catch_clause ();
19974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19976 cp_lexer_consume_token (parser->lexer);
19977 location = cp_lexer_peek_token (parser->lexer)->location;
19978 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19979 node, lest it get absorbed into the surrounding block. */
19980 stmt = push_stmt_list ();
19981 cp_parser_compound_statement (parser, NULL, false);
19982 objc_build_finally_clause (location, pop_stmt_list (stmt));
19985 return objc_finish_try_stmt ();
19988 /* Parse an Objective-C synchronized statement.
19990 objc-synchronized-stmt:
19991 @synchronized ( expression ) compound-statement
19993 Returns NULL_TREE. */
19996 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19997 location_t location;
20000 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20002 location = cp_lexer_peek_token (parser->lexer)->location;
20003 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20004 lock = cp_parser_expression (parser, false);
20005 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20007 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20008 node, lest it get absorbed into the surrounding block. */
20009 stmt = push_stmt_list ();
20010 cp_parser_compound_statement (parser, NULL, false);
20012 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20015 /* Parse an Objective-C throw statement.
20018 @throw assignment-expression [opt] ;
20020 Returns a constructed '@throw' statement. */
20023 cp_parser_objc_throw_statement (cp_parser *parser) {
20024 tree expr = NULL_TREE;
20026 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20029 expr = cp_parser_assignment_expression (parser, false);
20031 cp_parser_consume_semicolon_at_end_of_statement (parser);
20033 return objc_build_throw_stmt (expr);
20036 /* Parse an Objective-C statement. */
20039 cp_parser_objc_statement (cp_parser * parser) {
20040 /* Try to figure out what kind of declaration is present. */
20041 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20043 switch (kwd->keyword)
20046 return cp_parser_objc_try_catch_finally_statement (parser);
20047 case RID_AT_SYNCHRONIZED:
20048 return cp_parser_objc_synchronized_statement (parser);
20050 return cp_parser_objc_throw_statement (parser);
20052 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20053 &kwd->location, kwd->u.value);
20054 cp_parser_skip_to_end_of_block_or_statement (parser);
20057 return error_mark_node;
20060 /* OpenMP 2.5 parsing routines. */
20062 /* Returns name of the next clause.
20063 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20064 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20065 returned and the token is consumed. */
20067 static pragma_omp_clause
20068 cp_parser_omp_clause_name (cp_parser *parser)
20070 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20073 result = PRAGMA_OMP_CLAUSE_IF;
20074 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20075 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20076 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20077 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20078 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20080 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20081 const char *p = IDENTIFIER_POINTER (id);
20086 if (!strcmp ("collapse", p))
20087 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20088 else if (!strcmp ("copyin", p))
20089 result = PRAGMA_OMP_CLAUSE_COPYIN;
20090 else if (!strcmp ("copyprivate", p))
20091 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20094 if (!strcmp ("firstprivate", p))
20095 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20098 if (!strcmp ("lastprivate", p))
20099 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20102 if (!strcmp ("nowait", p))
20103 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20104 else if (!strcmp ("num_threads", p))
20105 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20108 if (!strcmp ("ordered", p))
20109 result = PRAGMA_OMP_CLAUSE_ORDERED;
20112 if (!strcmp ("reduction", p))
20113 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20116 if (!strcmp ("schedule", p))
20117 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20118 else if (!strcmp ("shared", p))
20119 result = PRAGMA_OMP_CLAUSE_SHARED;
20122 if (!strcmp ("untied", p))
20123 result = PRAGMA_OMP_CLAUSE_UNTIED;
20128 if (result != PRAGMA_OMP_CLAUSE_NONE)
20129 cp_lexer_consume_token (parser->lexer);
20134 /* Validate that a clause of the given type does not already exist. */
20137 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20138 const char *name, location_t location)
20142 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20143 if (OMP_CLAUSE_CODE (c) == code)
20145 error ("%Htoo many %qs clauses", &location, name);
20153 variable-list , identifier
20155 In addition, we match a closing parenthesis. An opening parenthesis
20156 will have been consumed by the caller.
20158 If KIND is nonzero, create the appropriate node and install the decl
20159 in OMP_CLAUSE_DECL and add the node to the head of the list.
20161 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20162 return the list created. */
20165 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20173 token = cp_lexer_peek_token (parser->lexer);
20174 name = cp_parser_id_expression (parser, /*template_p=*/false,
20175 /*check_dependency_p=*/true,
20176 /*template_p=*/NULL,
20177 /*declarator_p=*/false,
20178 /*optional_p=*/false);
20179 if (name == error_mark_node)
20182 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20183 if (decl == error_mark_node)
20184 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20185 else if (kind != 0)
20187 tree u = build_omp_clause (kind);
20188 OMP_CLAUSE_DECL (u) = decl;
20189 OMP_CLAUSE_CHAIN (u) = list;
20193 list = tree_cons (decl, NULL_TREE, list);
20196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20198 cp_lexer_consume_token (parser->lexer);
20201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20205 /* Try to resync to an unnested comma. Copied from
20206 cp_parser_parenthesized_expression_list. */
20208 ending = cp_parser_skip_to_closing_parenthesis (parser,
20209 /*recovering=*/true,
20211 /*consume_paren=*/true);
20219 /* Similarly, but expect leading and trailing parenthesis. This is a very
20220 common case for omp clauses. */
20223 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20225 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20226 return cp_parser_omp_var_list_no_open (parser, kind, list);
20231 collapse ( constant-expression ) */
20234 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20240 loc = cp_lexer_peek_token (parser->lexer)->location;
20241 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20244 num = cp_parser_constant_expression (parser, false, NULL);
20246 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20248 /*or_comma=*/false,
20249 /*consume_paren=*/true);
20251 if (num == error_mark_node)
20253 num = fold_non_dependent_expr (num);
20254 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20255 || !host_integerp (num, 0)
20256 || (n = tree_low_cst (num, 0)) <= 0
20259 error ("%Hcollapse argument needs positive constant integer expression",
20264 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20265 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20266 OMP_CLAUSE_CHAIN (c) = list;
20267 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20273 default ( shared | none ) */
20276 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20278 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20281 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20286 const char *p = IDENTIFIER_POINTER (id);
20291 if (strcmp ("none", p) != 0)
20293 kind = OMP_CLAUSE_DEFAULT_NONE;
20297 if (strcmp ("shared", p) != 0)
20299 kind = OMP_CLAUSE_DEFAULT_SHARED;
20306 cp_lexer_consume_token (parser->lexer);
20311 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20315 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20316 /*or_comma=*/false,
20317 /*consume_paren=*/true);
20319 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20322 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20323 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20324 OMP_CLAUSE_CHAIN (c) = list;
20325 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20331 if ( expression ) */
20334 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20338 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20341 t = cp_parser_condition (parser);
20343 if (t == error_mark_node
20344 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20345 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20346 /*or_comma=*/false,
20347 /*consume_paren=*/true);
20349 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20351 c = build_omp_clause (OMP_CLAUSE_IF);
20352 OMP_CLAUSE_IF_EXPR (c) = t;
20353 OMP_CLAUSE_CHAIN (c) = list;
20362 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20363 tree list, location_t location)
20367 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20369 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20370 OMP_CLAUSE_CHAIN (c) = list;
20375 num_threads ( expression ) */
20378 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20379 location_t location)
20383 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20386 t = cp_parser_expression (parser, false);
20388 if (t == error_mark_node
20389 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20390 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20391 /*or_comma=*/false,
20392 /*consume_paren=*/true);
20394 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20395 "num_threads", location);
20397 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20398 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20399 OMP_CLAUSE_CHAIN (c) = list;
20408 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20409 tree list, location_t location)
20413 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20414 "ordered", location);
20416 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20417 OMP_CLAUSE_CHAIN (c) = list;
20422 reduction ( reduction-operator : variable-list )
20424 reduction-operator:
20425 One of: + * - & ^ | && || */
20428 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20430 enum tree_code code;
20433 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20436 switch (cp_lexer_peek_token (parser->lexer)->type)
20448 code = BIT_AND_EXPR;
20451 code = BIT_XOR_EXPR;
20454 code = BIT_IOR_EXPR;
20457 code = TRUTH_ANDIF_EXPR;
20460 code = TRUTH_ORIF_EXPR;
20463 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20464 "%<|%>, %<&&%>, or %<||%>");
20466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20467 /*or_comma=*/false,
20468 /*consume_paren=*/true);
20471 cp_lexer_consume_token (parser->lexer);
20473 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20476 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20477 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20478 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20484 schedule ( schedule-kind )
20485 schedule ( schedule-kind , expression )
20488 static | dynamic | guided | runtime | auto */
20491 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20495 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20498 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20500 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20502 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20503 const char *p = IDENTIFIER_POINTER (id);
20508 if (strcmp ("dynamic", p) != 0)
20510 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20514 if (strcmp ("guided", p) != 0)
20516 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20520 if (strcmp ("runtime", p) != 0)
20522 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20529 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20530 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20531 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20532 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20535 cp_lexer_consume_token (parser->lexer);
20537 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20540 cp_lexer_consume_token (parser->lexer);
20542 token = cp_lexer_peek_token (parser->lexer);
20543 t = cp_parser_assignment_expression (parser, false);
20545 if (t == error_mark_node)
20547 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20548 error ("%Hschedule %<runtime%> does not take "
20549 "a %<chunk_size%> parameter", &token->location);
20550 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20551 error ("%Hschedule %<auto%> does not take "
20552 "a %<chunk_size%> parameter", &token->location);
20554 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20556 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20559 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20562 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20563 OMP_CLAUSE_CHAIN (c) = list;
20567 cp_parser_error (parser, "invalid schedule kind");
20569 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20570 /*or_comma=*/false,
20571 /*consume_paren=*/true);
20579 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20580 tree list, location_t location)
20584 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20586 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20587 OMP_CLAUSE_CHAIN (c) = list;
20591 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20592 is a bitmask in MASK. Return the list of clauses found; the result
20593 of clause default goes in *pdefault. */
20596 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20597 const char *where, cp_token *pragma_tok)
20599 tree clauses = NULL;
20601 cp_token *token = NULL;
20603 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20605 pragma_omp_clause c_kind;
20606 const char *c_name;
20607 tree prev = clauses;
20609 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20610 cp_lexer_consume_token (parser->lexer);
20612 token = cp_lexer_peek_token (parser->lexer);
20613 c_kind = cp_parser_omp_clause_name (parser);
20618 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20619 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20621 c_name = "collapse";
20623 case PRAGMA_OMP_CLAUSE_COPYIN:
20624 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20627 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20628 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20630 c_name = "copyprivate";
20632 case PRAGMA_OMP_CLAUSE_DEFAULT:
20633 clauses = cp_parser_omp_clause_default (parser, clauses,
20635 c_name = "default";
20637 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20638 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20640 c_name = "firstprivate";
20642 case PRAGMA_OMP_CLAUSE_IF:
20643 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20646 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20647 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20649 c_name = "lastprivate";
20651 case PRAGMA_OMP_CLAUSE_NOWAIT:
20652 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20655 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20656 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20658 c_name = "num_threads";
20660 case PRAGMA_OMP_CLAUSE_ORDERED:
20661 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20663 c_name = "ordered";
20665 case PRAGMA_OMP_CLAUSE_PRIVATE:
20666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20668 c_name = "private";
20670 case PRAGMA_OMP_CLAUSE_REDUCTION:
20671 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20672 c_name = "reduction";
20674 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20675 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20677 c_name = "schedule";
20679 case PRAGMA_OMP_CLAUSE_SHARED:
20680 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20684 case PRAGMA_OMP_CLAUSE_UNTIED:
20685 clauses = cp_parser_omp_clause_untied (parser, clauses,
20690 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20694 if (((mask >> c_kind) & 1) == 0)
20696 /* Remove the invalid clause(s) from the list to avoid
20697 confusing the rest of the compiler. */
20699 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20703 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20704 return finish_omp_clauses (clauses);
20711 In practice, we're also interested in adding the statement to an
20712 outer node. So it is convenient if we work around the fact that
20713 cp_parser_statement calls add_stmt. */
20716 cp_parser_begin_omp_structured_block (cp_parser *parser)
20718 unsigned save = parser->in_statement;
20720 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20721 This preserves the "not within loop or switch" style error messages
20722 for nonsense cases like
20728 if (parser->in_statement)
20729 parser->in_statement = IN_OMP_BLOCK;
20735 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20737 parser->in_statement = save;
20741 cp_parser_omp_structured_block (cp_parser *parser)
20743 tree stmt = begin_omp_structured_block ();
20744 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20746 cp_parser_statement (parser, NULL_TREE, false, NULL);
20748 cp_parser_end_omp_structured_block (parser, save);
20749 return finish_omp_structured_block (stmt);
20753 # pragma omp atomic new-line
20757 x binop= expr | x++ | ++x | x-- | --x
20759 +, *, -, /, &, ^, |, <<, >>
20761 where x is an lvalue expression with scalar type. */
20764 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20767 enum tree_code code;
20769 cp_parser_require_pragma_eol (parser, pragma_tok);
20771 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20773 switch (TREE_CODE (lhs))
20778 case PREINCREMENT_EXPR:
20779 case POSTINCREMENT_EXPR:
20780 lhs = TREE_OPERAND (lhs, 0);
20782 rhs = integer_one_node;
20785 case PREDECREMENT_EXPR:
20786 case POSTDECREMENT_EXPR:
20787 lhs = TREE_OPERAND (lhs, 0);
20789 rhs = integer_one_node;
20793 switch (cp_lexer_peek_token (parser->lexer)->type)
20799 code = TRUNC_DIV_EXPR;
20807 case CPP_LSHIFT_EQ:
20808 code = LSHIFT_EXPR;
20810 case CPP_RSHIFT_EQ:
20811 code = RSHIFT_EXPR;
20814 code = BIT_AND_EXPR;
20817 code = BIT_IOR_EXPR;
20820 code = BIT_XOR_EXPR;
20823 cp_parser_error (parser,
20824 "invalid operator for %<#pragma omp atomic%>");
20827 cp_lexer_consume_token (parser->lexer);
20829 rhs = cp_parser_expression (parser, false);
20830 if (rhs == error_mark_node)
20834 finish_omp_atomic (code, lhs, rhs);
20835 cp_parser_consume_semicolon_at_end_of_statement (parser);
20839 cp_parser_skip_to_end_of_block_or_statement (parser);
20844 # pragma omp barrier new-line */
20847 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20849 cp_parser_require_pragma_eol (parser, pragma_tok);
20850 finish_omp_barrier ();
20854 # pragma omp critical [(name)] new-line
20855 structured-block */
20858 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20860 tree stmt, name = NULL;
20862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20864 cp_lexer_consume_token (parser->lexer);
20866 name = cp_parser_identifier (parser);
20868 if (name == error_mark_node
20869 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20871 /*or_comma=*/false,
20872 /*consume_paren=*/true);
20873 if (name == error_mark_node)
20876 cp_parser_require_pragma_eol (parser, pragma_tok);
20878 stmt = cp_parser_omp_structured_block (parser);
20879 return c_finish_omp_critical (stmt, name);
20883 # pragma omp flush flush-vars[opt] new-line
20886 ( variable-list ) */
20889 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20892 (void) cp_parser_omp_var_list (parser, 0, NULL);
20893 cp_parser_require_pragma_eol (parser, pragma_tok);
20895 finish_omp_flush ();
20898 /* Helper function, to parse omp for increment expression. */
20901 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20903 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20909 cp_parser_skip_to_end_of_statement (parser);
20910 return error_mark_node;
20913 token = cp_lexer_peek_token (parser->lexer);
20914 op = binops_by_token [token->type].tree_type;
20923 cp_parser_skip_to_end_of_statement (parser);
20924 return error_mark_node;
20927 cp_lexer_consume_token (parser->lexer);
20928 rhs = cp_parser_binary_expression (parser, false,
20929 PREC_RELATIONAL_EXPRESSION);
20930 if (rhs == error_mark_node
20931 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20933 cp_parser_skip_to_end_of_statement (parser);
20934 return error_mark_node;
20937 return build2 (op, boolean_type_node, lhs, rhs);
20940 /* Helper function, to parse omp for increment expression. */
20943 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20945 cp_token *token = cp_lexer_peek_token (parser->lexer);
20951 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20953 op = (token->type == CPP_PLUS_PLUS
20954 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20955 cp_lexer_consume_token (parser->lexer);
20956 lhs = cp_parser_cast_expression (parser, false, false);
20958 return error_mark_node;
20959 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20962 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20964 return error_mark_node;
20966 token = cp_lexer_peek_token (parser->lexer);
20967 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20969 op = (token->type == CPP_PLUS_PLUS
20970 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20971 cp_lexer_consume_token (parser->lexer);
20972 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20975 op = cp_parser_assignment_operator_opt (parser);
20976 if (op == ERROR_MARK)
20977 return error_mark_node;
20979 if (op != NOP_EXPR)
20981 rhs = cp_parser_assignment_expression (parser, false);
20982 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20983 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20986 lhs = cp_parser_binary_expression (parser, false,
20987 PREC_ADDITIVE_EXPRESSION);
20988 token = cp_lexer_peek_token (parser->lexer);
20989 decl_first = lhs == decl;
20992 if (token->type != CPP_PLUS
20993 && token->type != CPP_MINUS)
20994 return error_mark_node;
20998 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20999 cp_lexer_consume_token (parser->lexer);
21000 rhs = cp_parser_binary_expression (parser, false,
21001 PREC_ADDITIVE_EXPRESSION);
21002 token = cp_lexer_peek_token (parser->lexer);
21003 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21005 if (lhs == NULL_TREE)
21007 if (op == PLUS_EXPR)
21010 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21013 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21014 NULL, tf_warning_or_error);
21017 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21021 if (rhs != decl || op == MINUS_EXPR)
21022 return error_mark_node;
21023 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21026 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21028 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21031 /* Parse the restricted form of the for statement allowed by OpenMP. */
21034 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21036 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21037 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21038 tree this_pre_body, cl;
21039 location_t loc_first;
21040 bool collapse_err = false;
21041 int i, collapse = 1, nbraces = 0;
21043 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21044 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21045 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21047 gcc_assert (collapse >= 1);
21049 declv = make_tree_vec (collapse);
21050 initv = make_tree_vec (collapse);
21051 condv = make_tree_vec (collapse);
21052 incrv = make_tree_vec (collapse);
21054 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21056 for (i = 0; i < collapse; i++)
21058 int bracecount = 0;
21059 bool add_private_clause = false;
21062 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21064 cp_parser_error (parser, "for statement expected");
21067 loc = cp_lexer_consume_token (parser->lexer)->location;
21069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21072 init = decl = real_decl = NULL;
21073 this_pre_body = push_stmt_list ();
21074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21076 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21080 integer-type var = lb
21081 random-access-iterator-type var = lb
21082 pointer-type var = lb
21084 cp_decl_specifier_seq type_specifiers;
21086 /* First, try to parse as an initialized declaration. See
21087 cp_parser_condition, from whence the bulk of this is copied. */
21089 cp_parser_parse_tentatively (parser);
21090 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21092 if (cp_parser_parse_definitely (parser))
21094 /* If parsing a type specifier seq succeeded, then this
21095 MUST be a initialized declaration. */
21096 tree asm_specification, attributes;
21097 cp_declarator *declarator;
21099 declarator = cp_parser_declarator (parser,
21100 CP_PARSER_DECLARATOR_NAMED,
21101 /*ctor_dtor_or_conv_p=*/NULL,
21102 /*parenthesized_p=*/NULL,
21103 /*member_p=*/false);
21104 attributes = cp_parser_attributes_opt (parser);
21105 asm_specification = cp_parser_asm_specification_opt (parser);
21107 if (declarator == cp_error_declarator)
21108 cp_parser_skip_to_end_of_statement (parser);
21114 decl = start_decl (declarator, &type_specifiers,
21115 /*initialized_p=*/false, attributes,
21116 /*prefix_attributes=*/NULL_TREE,
21119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21121 if (cp_lexer_next_token_is (parser->lexer,
21123 error ("parenthesized initialization is not allowed in "
21124 "OpenMP %<for%> loop");
21126 /* Trigger an error. */
21127 cp_parser_require (parser, CPP_EQ, "%<=%>");
21129 init = error_mark_node;
21130 cp_parser_skip_to_end_of_statement (parser);
21132 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21133 || type_dependent_expression_p (decl))
21135 bool is_direct_init, is_non_constant_init;
21137 init = cp_parser_initializer (parser,
21139 &is_non_constant_init);
21141 cp_finish_decl (decl, init, !is_non_constant_init,
21143 LOOKUP_ONLYCONVERTING);
21144 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21147 = tree_cons (NULL, this_pre_body, for_block);
21151 init = pop_stmt_list (this_pre_body);
21152 this_pre_body = NULL_TREE;
21157 cp_lexer_consume_token (parser->lexer);
21158 init = cp_parser_assignment_expression (parser, false);
21160 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21161 init = error_mark_node;
21163 cp_finish_decl (decl, NULL_TREE,
21164 /*init_const_expr_p=*/false,
21166 LOOKUP_ONLYCONVERTING);
21170 pop_scope (pushed_scope);
21176 /* If parsing a type specifier sequence failed, then
21177 this MUST be a simple expression. */
21178 cp_parser_parse_tentatively (parser);
21179 decl = cp_parser_primary_expression (parser, false, false,
21181 if (!cp_parser_error_occurred (parser)
21184 && CLASS_TYPE_P (TREE_TYPE (decl)))
21188 cp_parser_parse_definitely (parser);
21189 cp_parser_require (parser, CPP_EQ, "%<=%>");
21190 rhs = cp_parser_assignment_expression (parser, false);
21191 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21193 tf_warning_or_error));
21194 add_private_clause = true;
21199 cp_parser_abort_tentative_parse (parser);
21200 init = cp_parser_expression (parser, false);
21203 if (TREE_CODE (init) == MODIFY_EXPR
21204 || TREE_CODE (init) == MODOP_EXPR)
21205 real_decl = TREE_OPERAND (init, 0);
21210 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21213 this_pre_body = pop_stmt_list (this_pre_body);
21217 pre_body = push_stmt_list ();
21219 add_stmt (this_pre_body);
21220 pre_body = pop_stmt_list (pre_body);
21223 pre_body = this_pre_body;
21228 if (par_clauses != NULL && real_decl != NULL_TREE)
21231 for (c = par_clauses; *c ; )
21232 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21233 && OMP_CLAUSE_DECL (*c) == real_decl)
21235 error ("%Hiteration variable %qD should not be firstprivate",
21237 *c = OMP_CLAUSE_CHAIN (*c);
21239 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21240 && OMP_CLAUSE_DECL (*c) == real_decl)
21242 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21243 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21244 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21245 OMP_CLAUSE_DECL (l) = real_decl;
21246 OMP_CLAUSE_CHAIN (l) = clauses;
21247 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21249 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21250 CP_OMP_CLAUSE_INFO (*c) = NULL;
21251 add_private_clause = false;
21255 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21256 && OMP_CLAUSE_DECL (*c) == real_decl)
21257 add_private_clause = false;
21258 c = &OMP_CLAUSE_CHAIN (*c);
21262 if (add_private_clause)
21265 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21267 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21268 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21269 && OMP_CLAUSE_DECL (c) == decl)
21271 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21272 && OMP_CLAUSE_DECL (c) == decl)
21273 error ("%Hiteration variable %qD should not be firstprivate",
21275 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21276 && OMP_CLAUSE_DECL (c) == decl)
21277 error ("%Hiteration variable %qD should not be reduction",
21282 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21283 OMP_CLAUSE_DECL (c) = decl;
21284 c = finish_omp_clauses (c);
21287 OMP_CLAUSE_CHAIN (c) = clauses;
21294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21296 /* If decl is an iterator, preserve LHS and RHS of the relational
21297 expr until finish_omp_for. */
21299 && (type_dependent_expression_p (decl)
21300 || CLASS_TYPE_P (TREE_TYPE (decl))))
21301 cond = cp_parser_omp_for_cond (parser, decl);
21303 cond = cp_parser_condition (parser);
21305 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21310 /* If decl is an iterator, preserve the operator on decl
21311 until finish_omp_for. */
21313 && (type_dependent_expression_p (decl)
21314 || CLASS_TYPE_P (TREE_TYPE (decl))))
21315 incr = cp_parser_omp_for_incr (parser, decl);
21317 incr = cp_parser_expression (parser, false);
21320 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21322 /*or_comma=*/false,
21323 /*consume_paren=*/true);
21325 TREE_VEC_ELT (declv, i) = decl;
21326 TREE_VEC_ELT (initv, i) = init;
21327 TREE_VEC_ELT (condv, i) = cond;
21328 TREE_VEC_ELT (incrv, i) = incr;
21330 if (i == collapse - 1)
21333 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21334 in between the collapsed for loops to be still considered perfectly
21335 nested. Hopefully the final version clarifies this.
21336 For now handle (multiple) {'s and empty statements. */
21337 cp_parser_parse_tentatively (parser);
21340 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21342 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21344 cp_lexer_consume_token (parser->lexer);
21347 else if (bracecount
21348 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21349 cp_lexer_consume_token (parser->lexer);
21352 loc = cp_lexer_peek_token (parser->lexer)->location;
21353 error ("%Hnot enough collapsed for loops", &loc);
21354 collapse_err = true;
21355 cp_parser_abort_tentative_parse (parser);
21364 cp_parser_parse_definitely (parser);
21365 nbraces += bracecount;
21369 /* Note that we saved the original contents of this flag when we entered
21370 the structured block, and so we don't need to re-save it here. */
21371 parser->in_statement = IN_OMP_FOR;
21373 /* Note that the grammar doesn't call for a structured block here,
21374 though the loop as a whole is a structured block. */
21375 body = push_stmt_list ();
21376 cp_parser_statement (parser, NULL_TREE, false, NULL);
21377 body = pop_stmt_list (body);
21379 if (declv == NULL_TREE)
21382 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21383 pre_body, clauses);
21387 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21389 cp_lexer_consume_token (parser->lexer);
21392 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21393 cp_lexer_consume_token (parser->lexer);
21398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21399 error ("%Hcollapsed loops not perfectly nested", &loc);
21401 collapse_err = true;
21402 cp_parser_statement_seq_opt (parser, NULL);
21403 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21409 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21410 for_block = TREE_CHAIN (for_block);
21417 #pragma omp for for-clause[optseq] new-line
21420 #define OMP_FOR_CLAUSE_MASK \
21421 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21422 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21423 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21424 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21425 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21426 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21427 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21428 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21431 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21433 tree clauses, sb, ret;
21436 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21437 "#pragma omp for", pragma_tok);
21439 sb = begin_omp_structured_block ();
21440 save = cp_parser_begin_omp_structured_block (parser);
21442 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21444 cp_parser_end_omp_structured_block (parser, save);
21445 add_stmt (finish_omp_structured_block (sb));
21451 # pragma omp master new-line
21452 structured-block */
21455 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21457 cp_parser_require_pragma_eol (parser, pragma_tok);
21458 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21462 # pragma omp ordered new-line
21463 structured-block */
21466 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21468 cp_parser_require_pragma_eol (parser, pragma_tok);
21469 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21475 { section-sequence }
21478 section-directive[opt] structured-block
21479 section-sequence section-directive structured-block */
21482 cp_parser_omp_sections_scope (cp_parser *parser)
21484 tree stmt, substmt;
21485 bool error_suppress = false;
21488 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21491 stmt = push_stmt_list ();
21493 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21497 substmt = begin_omp_structured_block ();
21498 save = cp_parser_begin_omp_structured_block (parser);
21502 cp_parser_statement (parser, NULL_TREE, false, NULL);
21504 tok = cp_lexer_peek_token (parser->lexer);
21505 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21507 if (tok->type == CPP_CLOSE_BRACE)
21509 if (tok->type == CPP_EOF)
21513 cp_parser_end_omp_structured_block (parser, save);
21514 substmt = finish_omp_structured_block (substmt);
21515 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21516 add_stmt (substmt);
21521 tok = cp_lexer_peek_token (parser->lexer);
21522 if (tok->type == CPP_CLOSE_BRACE)
21524 if (tok->type == CPP_EOF)
21527 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21529 cp_lexer_consume_token (parser->lexer);
21530 cp_parser_require_pragma_eol (parser, tok);
21531 error_suppress = false;
21533 else if (!error_suppress)
21535 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21536 error_suppress = true;
21539 substmt = cp_parser_omp_structured_block (parser);
21540 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21541 add_stmt (substmt);
21543 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21545 substmt = pop_stmt_list (stmt);
21547 stmt = make_node (OMP_SECTIONS);
21548 TREE_TYPE (stmt) = void_type_node;
21549 OMP_SECTIONS_BODY (stmt) = substmt;
21556 # pragma omp sections sections-clause[optseq] newline
21559 #define OMP_SECTIONS_CLAUSE_MASK \
21560 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21561 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21562 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21563 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21564 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21567 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21571 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21572 "#pragma omp sections", pragma_tok);
21574 ret = cp_parser_omp_sections_scope (parser);
21576 OMP_SECTIONS_CLAUSES (ret) = clauses;
21582 # pragma parallel parallel-clause new-line
21583 # pragma parallel for parallel-for-clause new-line
21584 # pragma parallel sections parallel-sections-clause new-line */
21586 #define OMP_PARALLEL_CLAUSE_MASK \
21587 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21588 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21589 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21590 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21591 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21592 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21593 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21594 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21597 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21599 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21600 const char *p_name = "#pragma omp parallel";
21601 tree stmt, clauses, par_clause, ws_clause, block;
21602 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21607 cp_lexer_consume_token (parser->lexer);
21608 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21609 p_name = "#pragma omp parallel for";
21610 mask |= OMP_FOR_CLAUSE_MASK;
21611 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21613 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21616 const char *p = IDENTIFIER_POINTER (id);
21617 if (strcmp (p, "sections") == 0)
21619 cp_lexer_consume_token (parser->lexer);
21620 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21621 p_name = "#pragma omp parallel sections";
21622 mask |= OMP_SECTIONS_CLAUSE_MASK;
21623 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21627 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21628 block = begin_omp_parallel ();
21629 save = cp_parser_begin_omp_structured_block (parser);
21633 case PRAGMA_OMP_PARALLEL:
21634 cp_parser_statement (parser, NULL_TREE, false, NULL);
21635 par_clause = clauses;
21638 case PRAGMA_OMP_PARALLEL_FOR:
21639 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21640 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21643 case PRAGMA_OMP_PARALLEL_SECTIONS:
21644 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21645 stmt = cp_parser_omp_sections_scope (parser);
21647 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21651 gcc_unreachable ();
21654 cp_parser_end_omp_structured_block (parser, save);
21655 stmt = finish_omp_parallel (par_clause, block);
21656 if (p_kind != PRAGMA_OMP_PARALLEL)
21657 OMP_PARALLEL_COMBINED (stmt) = 1;
21662 # pragma omp single single-clause[optseq] new-line
21663 structured-block */
21665 #define OMP_SINGLE_CLAUSE_MASK \
21666 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21667 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21668 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21669 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21672 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21674 tree stmt = make_node (OMP_SINGLE);
21675 TREE_TYPE (stmt) = void_type_node;
21677 OMP_SINGLE_CLAUSES (stmt)
21678 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21679 "#pragma omp single", pragma_tok);
21680 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21682 return add_stmt (stmt);
21686 # pragma omp task task-clause[optseq] new-line
21687 structured-block */
21689 #define OMP_TASK_CLAUSE_MASK \
21690 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21691 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21692 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21693 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21694 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21695 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21698 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21700 tree clauses, block;
21703 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21704 "#pragma omp task", pragma_tok);
21705 block = begin_omp_task ();
21706 save = cp_parser_begin_omp_structured_block (parser);
21707 cp_parser_statement (parser, NULL_TREE, false, NULL);
21708 cp_parser_end_omp_structured_block (parser, save);
21709 return finish_omp_task (clauses, block);
21713 # pragma omp taskwait new-line */
21716 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21718 cp_parser_require_pragma_eol (parser, pragma_tok);
21719 finish_omp_taskwait ();
21723 # pragma omp threadprivate (variable-list) */
21726 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21730 vars = cp_parser_omp_var_list (parser, 0, NULL);
21731 cp_parser_require_pragma_eol (parser, pragma_tok);
21733 finish_omp_threadprivate (vars);
21736 /* Main entry point to OpenMP statement pragmas. */
21739 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21743 switch (pragma_tok->pragma_kind)
21745 case PRAGMA_OMP_ATOMIC:
21746 cp_parser_omp_atomic (parser, pragma_tok);
21748 case PRAGMA_OMP_CRITICAL:
21749 stmt = cp_parser_omp_critical (parser, pragma_tok);
21751 case PRAGMA_OMP_FOR:
21752 stmt = cp_parser_omp_for (parser, pragma_tok);
21754 case PRAGMA_OMP_MASTER:
21755 stmt = cp_parser_omp_master (parser, pragma_tok);
21757 case PRAGMA_OMP_ORDERED:
21758 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21760 case PRAGMA_OMP_PARALLEL:
21761 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21763 case PRAGMA_OMP_SECTIONS:
21764 stmt = cp_parser_omp_sections (parser, pragma_tok);
21766 case PRAGMA_OMP_SINGLE:
21767 stmt = cp_parser_omp_single (parser, pragma_tok);
21769 case PRAGMA_OMP_TASK:
21770 stmt = cp_parser_omp_task (parser, pragma_tok);
21773 gcc_unreachable ();
21777 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21782 static GTY (()) cp_parser *the_parser;
21785 /* Special handling for the first token or line in the file. The first
21786 thing in the file might be #pragma GCC pch_preprocess, which loads a
21787 PCH file, which is a GC collection point. So we need to handle this
21788 first pragma without benefit of an existing lexer structure.
21790 Always returns one token to the caller in *FIRST_TOKEN. This is
21791 either the true first token of the file, or the first token after
21792 the initial pragma. */
21795 cp_parser_initial_pragma (cp_token *first_token)
21799 cp_lexer_get_preprocessor_token (NULL, first_token);
21800 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21803 cp_lexer_get_preprocessor_token (NULL, first_token);
21804 if (first_token->type == CPP_STRING)
21806 name = first_token->u.value;
21808 cp_lexer_get_preprocessor_token (NULL, first_token);
21809 if (first_token->type != CPP_PRAGMA_EOL)
21810 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21811 &first_token->location);
21814 error ("%Hexpected string literal", &first_token->location);
21816 /* Skip to the end of the pragma. */
21817 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21818 cp_lexer_get_preprocessor_token (NULL, first_token);
21820 /* Now actually load the PCH file. */
21822 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21824 /* Read one more token to return to our caller. We have to do this
21825 after reading the PCH file in, since its pointers have to be
21827 cp_lexer_get_preprocessor_token (NULL, first_token);
21830 /* Normal parsing of a pragma token. Here we can (and must) use the
21834 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21836 cp_token *pragma_tok;
21839 pragma_tok = cp_lexer_consume_token (parser->lexer);
21840 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21841 parser->lexer->in_pragma = true;
21843 id = pragma_tok->pragma_kind;
21846 case PRAGMA_GCC_PCH_PREPROCESS:
21847 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21848 &pragma_tok->location);
21851 case PRAGMA_OMP_BARRIER:
21854 case pragma_compound:
21855 cp_parser_omp_barrier (parser, pragma_tok);
21858 error ("%H%<#pragma omp barrier%> may only be "
21859 "used in compound statements", &pragma_tok->location);
21866 case PRAGMA_OMP_FLUSH:
21869 case pragma_compound:
21870 cp_parser_omp_flush (parser, pragma_tok);
21873 error ("%H%<#pragma omp flush%> may only be "
21874 "used in compound statements", &pragma_tok->location);
21881 case PRAGMA_OMP_TASKWAIT:
21884 case pragma_compound:
21885 cp_parser_omp_taskwait (parser, pragma_tok);
21888 error ("%H%<#pragma omp taskwait%> may only be "
21889 "used in compound statements",
21890 &pragma_tok->location);
21897 case PRAGMA_OMP_THREADPRIVATE:
21898 cp_parser_omp_threadprivate (parser, pragma_tok);
21901 case PRAGMA_OMP_ATOMIC:
21902 case PRAGMA_OMP_CRITICAL:
21903 case PRAGMA_OMP_FOR:
21904 case PRAGMA_OMP_MASTER:
21905 case PRAGMA_OMP_ORDERED:
21906 case PRAGMA_OMP_PARALLEL:
21907 case PRAGMA_OMP_SECTIONS:
21908 case PRAGMA_OMP_SINGLE:
21909 case PRAGMA_OMP_TASK:
21910 if (context == pragma_external)
21912 cp_parser_omp_construct (parser, pragma_tok);
21915 case PRAGMA_OMP_SECTION:
21916 error ("%H%<#pragma omp section%> may only be used in "
21917 "%<#pragma omp sections%> construct", &pragma_tok->location);
21921 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21922 c_invoke_pragma_handler (id);
21926 cp_parser_error (parser, "expected declaration specifiers");
21930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21934 /* The interface the pragma parsers have to the lexer. */
21937 pragma_lex (tree *value)
21940 enum cpp_ttype ret;
21942 tok = cp_lexer_peek_token (the_parser->lexer);
21945 *value = tok->u.value;
21947 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21949 else if (ret == CPP_STRING)
21950 *value = cp_parser_string_literal (the_parser, false, false);
21953 cp_lexer_consume_token (the_parser->lexer);
21954 if (ret == CPP_KEYWORD)
21962 /* External interface. */
21964 /* Parse one entire translation unit. */
21967 c_parse_file (void)
21969 bool error_occurred;
21970 static bool already_called = false;
21972 if (already_called)
21974 sorry ("inter-module optimizations not implemented for C++");
21977 already_called = true;
21979 the_parser = cp_parser_new ();
21980 push_deferring_access_checks (flag_access_control
21981 ? dk_no_deferred : dk_no_check);
21982 error_occurred = cp_parser_translation_unit (the_parser);
21986 #include "gt-cp-parser.h"