2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct tree_check GTY(())
53 /* The value associated with the token. */
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
64 typedef struct cp_token GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
101 static const cp_token eof_token =
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
116 typedef struct cp_lexer GTY (())
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
123 size_t buffer_length;
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
142 /* True if we should output debugging information. */
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
156 typedef struct cp_token_cache GTY(())
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
167 static cp_lexer *cp_lexer_new_main
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
173 static int cp_lexer_saving_tokens
175 static cp_token_position cp_lexer_token_position
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
193 static void cp_lexer_purge_token
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
199 static void cp_lexer_commit_tokens
201 static void cp_lexer_rollback_tokens
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
225 static void cp_parser_initial_pragma
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
267 cp_lexer_new_main (void)
269 cp_token first_token;
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
284 c_common_no_more_pch ();
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
300 /* Put the first token in the buffer. */
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
316 cp_lexer_get_preprocessor_token (lexer, pos);
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
360 /* Frees all resources associated with LEXER. */
363 cp_lexer_destroy (cp_lexer *lexer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
371 /* Returns nonzero if debugging information should be output. */
373 #ifdef ENABLE_CHECKING
376 cp_lexer_debugging_p (cp_lexer *lexer)
378 return lexer->debugging_p;
381 #endif /* ENABLE_CHECKING */
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
388 return lexer->next_token - previous_p;
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
397 /* nonzero if we are presently saving tokens. */
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
405 /* Store the next token from the preprocessor in *TOKEN. Return true
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
412 static int is_extern_c = 0;
414 /* Get a new token from the preprocessor. */
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
432 if (C_IS_RESERVED_WORD (token->u.value))
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
446 token->ambiguous_p = false;
447 token->keyword = RID_MAX;
450 /* Handle Objective-C++ keywords. */
451 else if (token->type == CPP_AT_NAME)
453 token->type = CPP_KEYWORD;
454 switch (C_RID_CODE (token->u.value))
456 /* Map 'class' to '@class', 'private' to '@private', etc. */
457 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
458 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
459 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
460 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
461 case RID_THROW: token->keyword = RID_AT_THROW; break;
462 case RID_TRY: token->keyword = RID_AT_TRY; break;
463 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
464 default: token->keyword = C_RID_CODE (token->u.value);
467 else if (token->type == CPP_PRAGMA)
469 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
470 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
471 token->u.value = NULL_TREE;
475 /* Update the globals input_location and in_system_header and the
476 input file stack from TOKEN. */
478 cp_lexer_set_source_position_from_token (cp_token *token)
480 if (token->type != CPP_EOF)
482 input_location = token->location;
483 in_system_header = token->in_system_header;
484 restore_input_file_stack (token->input_file_stack_index);
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 a keyword for a decl-specifier. */
530 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 token = cp_lexer_peek_token (lexer);
535 switch (token->keyword)
537 /* Storage classes. */
544 /* Elaborated type specifiers. */
550 /* Simple type specifiers. */
562 /* GNU extensions. */
572 /* Return a pointer to the Nth token in the token stream. If N is 1,
573 then this is precisely equivalent to cp_lexer_peek_token (except
574 that it is not inline). One would like to disallow that case, but
575 there is one case (cp_parser_nth_token_starts_template_id) where
576 the caller passes a variable for N and it might be 1. */
579 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
583 /* N is 1-based, not zero-based. */
586 if (cp_lexer_debugging_p (lexer))
587 fprintf (cp_lexer_debug_stream,
588 "cp_lexer: peeking ahead %ld at token: ", (long)n);
591 token = lexer->next_token;
592 gcc_assert (!n || token != &eof_token);
596 if (token == lexer->last_token)
598 token = (cp_token *)&eof_token;
602 if (token->type != CPP_PURGED)
606 if (cp_lexer_debugging_p (lexer))
608 cp_lexer_print_token (cp_lexer_debug_stream, token);
609 putc ('\n', cp_lexer_debug_stream);
615 /* Return the next token, and advance the lexer's next_token pointer
616 to point to the next non-purged token. */
619 cp_lexer_consume_token (cp_lexer* lexer)
621 cp_token *token = lexer->next_token;
623 gcc_assert (token != &eof_token);
624 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
629 if (lexer->next_token == lexer->last_token)
631 lexer->next_token = (cp_token *)&eof_token;
636 while (lexer->next_token->type == CPP_PURGED);
638 cp_lexer_set_source_position_from_token (token);
640 /* Provide debugging output. */
641 if (cp_lexer_debugging_p (lexer))
643 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
644 cp_lexer_print_token (cp_lexer_debug_stream, token);
645 putc ('\n', cp_lexer_debug_stream);
651 /* Permanently remove the next token from the token stream, and
652 advance the next_token pointer to refer to the next non-purged
656 cp_lexer_purge_token (cp_lexer *lexer)
658 cp_token *tok = lexer->next_token;
660 gcc_assert (tok != &eof_token);
661 tok->type = CPP_PURGED;
662 tok->location = UNKNOWN_LOCATION;
663 tok->u.value = NULL_TREE;
664 tok->keyword = RID_MAX;
669 if (tok == lexer->last_token)
671 tok = (cp_token *)&eof_token;
675 while (tok->type == CPP_PURGED);
676 lexer->next_token = tok;
679 /* Permanently remove all tokens after TOK, up to, but not
680 including, the token that will be returned next by
681 cp_lexer_peek_token. */
684 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
686 cp_token *peek = lexer->next_token;
688 if (peek == &eof_token)
689 peek = lexer->last_token;
691 gcc_assert (tok < peek);
693 for ( tok += 1; tok != peek; tok += 1)
695 tok->type = CPP_PURGED;
696 tok->location = UNKNOWN_LOCATION;
697 tok->u.value = NULL_TREE;
698 tok->keyword = RID_MAX;
702 /* Begin saving tokens. All tokens consumed after this point will be
706 cp_lexer_save_tokens (cp_lexer* lexer)
708 /* Provide debugging output. */
709 if (cp_lexer_debugging_p (lexer))
710 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
712 VEC_safe_push (cp_token_position, heap,
713 lexer->saved_tokens, lexer->next_token);
716 /* Commit to the portion of the token stream most recently saved. */
719 cp_lexer_commit_tokens (cp_lexer* lexer)
721 /* Provide debugging output. */
722 if (cp_lexer_debugging_p (lexer))
723 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
725 VEC_pop (cp_token_position, lexer->saved_tokens);
728 /* Return all tokens saved since the last call to cp_lexer_save_tokens
729 to the token stream. Stop saving tokens. */
732 cp_lexer_rollback_tokens (cp_lexer* lexer)
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
738 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
741 /* Print a representation of the TOKEN on the STREAM. */
743 #ifdef ENABLE_CHECKING
746 cp_lexer_print_token (FILE * stream, cp_token *token)
748 /* We don't use cpp_type2name here because the parser defines
749 a few tokens of its own. */
750 static const char *const token_names[] = {
751 /* cpplib-defined token types */
757 /* C++ parser token types - see "Manifest constants", above. */
760 "NESTED_NAME_SPECIFIER",
764 /* If we have a name for the token, print it out. Otherwise, we
765 simply give the numeric code. */
766 gcc_assert (token->type < ARRAY_SIZE(token_names));
767 fputs (token_names[token->type], stream);
769 /* For some tokens, print the associated data. */
773 /* Some keywords have a value that is not an IDENTIFIER_NODE.
774 For example, `struct' is mapped to an INTEGER_CST. */
775 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
777 /* else fall through */
779 fputs (IDENTIFIER_POINTER (token->u.value), stream);
784 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
792 /* Start emitting debugging information. */
795 cp_lexer_start_debugging (cp_lexer* lexer)
797 lexer->debugging_p = true;
800 /* Stop emitting debugging information. */
803 cp_lexer_stop_debugging (cp_lexer* lexer)
805 lexer->debugging_p = false;
808 #endif /* ENABLE_CHECKING */
810 /* Create a new cp_token_cache, representing a range of tokens. */
812 static cp_token_cache *
813 cp_token_cache_new (cp_token *first, cp_token *last)
815 cp_token_cache *cache = GGC_NEW (cp_token_cache);
816 cache->first = first;
822 /* Decl-specifiers. */
824 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
827 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
829 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
834 /* Nothing other than the parser should be creating declarators;
835 declarators are a semi-syntactic representation of C++ entities.
836 Other parts of the front end that need to create entities (like
837 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
839 static cp_declarator *make_call_declarator
840 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
841 static cp_declarator *make_array_declarator
842 (cp_declarator *, tree);
843 static cp_declarator *make_pointer_declarator
844 (cp_cv_quals, cp_declarator *);
845 static cp_declarator *make_reference_declarator
846 (cp_cv_quals, cp_declarator *);
847 static cp_parameter_declarator *make_parameter_declarator
848 (cp_decl_specifier_seq *, cp_declarator *, tree);
849 static cp_declarator *make_ptrmem_declarator
850 (cp_cv_quals, tree, cp_declarator *);
852 /* An erroneous declarator. */
853 static cp_declarator *cp_error_declarator;
855 /* The obstack on which declarators and related data structures are
857 static struct obstack declarator_obstack;
859 /* Alloc BYTES from the declarator memory pool. */
862 alloc_declarator (size_t bytes)
864 return obstack_alloc (&declarator_obstack, bytes);
867 /* Allocate a declarator of the indicated KIND. Clear fields that are
868 common to all declarators. */
870 static cp_declarator *
871 make_declarator (cp_declarator_kind kind)
873 cp_declarator *declarator;
875 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
876 declarator->kind = kind;
877 declarator->attributes = NULL_TREE;
878 declarator->declarator = NULL;
883 /* Make a declarator for a generalized identifier. If
884 QUALIFYING_SCOPE is non-NULL, the identifier is
885 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
886 UNQUALIFIED_NAME. SFK indicates the kind of special function this
889 static cp_declarator *
890 make_id_declarator (tree qualifying_scope, tree unqualified_name,
891 special_function_kind sfk)
893 cp_declarator *declarator;
895 /* It is valid to write:
897 class C { void f(); };
901 The standard is not clear about whether `typedef const C D' is
902 legal; as of 2002-09-15 the committee is considering that
903 question. EDG 3.0 allows that syntax. Therefore, we do as
905 if (qualifying_scope && TYPE_P (qualifying_scope))
906 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
908 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
909 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
910 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
912 declarator = make_declarator (cdk_id);
913 declarator->u.id.qualifying_scope = qualifying_scope;
914 declarator->u.id.unqualified_name = unqualified_name;
915 declarator->u.id.sfk = sfk;
920 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
921 of modifiers such as const or volatile to apply to the pointer
922 type, represented as identifiers. */
925 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
927 cp_declarator *declarator;
929 declarator = make_declarator (cdk_pointer);
930 declarator->declarator = target;
931 declarator->u.pointer.qualifiers = cv_qualifiers;
932 declarator->u.pointer.class_type = NULL_TREE;
937 /* Like make_pointer_declarator -- but for references. */
940 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_reference);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
952 /* Like make_pointer_declarator -- but for a pointer to a non-static
953 member of CLASS_TYPE. */
956 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
957 cp_declarator *pointee)
959 cp_declarator *declarator;
961 declarator = make_declarator (cdk_ptrmem);
962 declarator->declarator = pointee;
963 declarator->u.pointer.qualifiers = cv_qualifiers;
964 declarator->u.pointer.class_type = class_type;
969 /* Make a declarator for the function given by TARGET, with the
970 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
971 "const"-qualified member function. The EXCEPTION_SPECIFICATION
972 indicates what exceptions can be thrown. */
975 make_call_declarator (cp_declarator *target,
976 cp_parameter_declarator *parms,
977 cp_cv_quals cv_qualifiers,
978 tree exception_specification)
980 cp_declarator *declarator;
982 declarator = make_declarator (cdk_function);
983 declarator->declarator = target;
984 declarator->u.function.parameters = parms;
985 declarator->u.function.qualifiers = cv_qualifiers;
986 declarator->u.function.exception_specification = exception_specification;
991 /* Make a declarator for an array of BOUNDS elements, each of which is
992 defined by ELEMENT. */
995 make_array_declarator (cp_declarator *element, tree bounds)
997 cp_declarator *declarator;
999 declarator = make_declarator (cdk_array);
1000 declarator->declarator = element;
1001 declarator->u.array.bounds = bounds;
1006 cp_parameter_declarator *no_parameters;
1008 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1009 DECLARATOR and DEFAULT_ARGUMENT. */
1011 cp_parameter_declarator *
1012 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1013 cp_declarator *declarator,
1014 tree default_argument)
1016 cp_parameter_declarator *parameter;
1018 parameter = ((cp_parameter_declarator *)
1019 alloc_declarator (sizeof (cp_parameter_declarator)));
1020 parameter->next = NULL;
1021 if (decl_specifiers)
1022 parameter->decl_specifiers = *decl_specifiers;
1024 clear_decl_specs (¶meter->decl_specifiers);
1025 parameter->declarator = declarator;
1026 parameter->default_argument = default_argument;
1027 parameter->ellipsis_p = false;
1032 /* Returns true iff DECLARATOR is a declaration for a function. */
1035 function_declarator_p (const cp_declarator *declarator)
1039 if (declarator->kind == cdk_function
1040 && declarator->declarator->kind == cdk_id)
1042 if (declarator->kind == cdk_id
1043 || declarator->kind == cdk_error)
1045 declarator = declarator->declarator;
1055 A cp_parser parses the token stream as specified by the C++
1056 grammar. Its job is purely parsing, not semantic analysis. For
1057 example, the parser breaks the token stream into declarators,
1058 expressions, statements, and other similar syntactic constructs.
1059 It does not check that the types of the expressions on either side
1060 of an assignment-statement are compatible, or that a function is
1061 not declared with a parameter of type `void'.
1063 The parser invokes routines elsewhere in the compiler to perform
1064 semantic analysis and to build up the abstract syntax tree for the
1067 The parser (and the template instantiation code, which is, in a
1068 way, a close relative of parsing) are the only parts of the
1069 compiler that should be calling push_scope and pop_scope, or
1070 related functions. The parser (and template instantiation code)
1071 keeps track of what scope is presently active; everything else
1072 should simply honor that. (The code that generates static
1073 initializers may also need to set the scope, in order to check
1074 access control correctly when emitting the initializers.)
1079 The parser is of the standard recursive-descent variety. Upcoming
1080 tokens in the token stream are examined in order to determine which
1081 production to use when parsing a non-terminal. Some C++ constructs
1082 require arbitrary look ahead to disambiguate. For example, it is
1083 impossible, in the general case, to tell whether a statement is an
1084 expression or declaration without scanning the entire statement.
1085 Therefore, the parser is capable of "parsing tentatively." When the
1086 parser is not sure what construct comes next, it enters this mode.
1087 Then, while we attempt to parse the construct, the parser queues up
1088 error messages, rather than issuing them immediately, and saves the
1089 tokens it consumes. If the construct is parsed successfully, the
1090 parser "commits", i.e., it issues any queued error messages and
1091 the tokens that were being preserved are permanently discarded.
1092 If, however, the construct is not parsed successfully, the parser
1093 rolls back its state completely so that it can resume parsing using
1094 a different alternative.
1099 The performance of the parser could probably be improved substantially.
1100 We could often eliminate the need to parse tentatively by looking ahead
1101 a little bit. In some places, this approach might not entirely eliminate
1102 the need to parse tentatively, but it might still speed up the average
1105 /* Flags that are passed to some parsing functions. These values can
1106 be bitwise-ored together. */
1108 typedef enum cp_parser_flags
1111 CP_PARSER_FLAGS_NONE = 0x0,
1112 /* The construct is optional. If it is not present, then no error
1113 should be issued. */
1114 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1115 /* When parsing a type-specifier, do not allow user-defined types. */
1116 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1119 /* The different kinds of declarators we want to parse. */
1121 typedef enum cp_parser_declarator_kind
1123 /* We want an abstract declarator. */
1124 CP_PARSER_DECLARATOR_ABSTRACT,
1125 /* We want a named declarator. */
1126 CP_PARSER_DECLARATOR_NAMED,
1127 /* We don't mind, but the name must be an unqualified-id. */
1128 CP_PARSER_DECLARATOR_EITHER
1129 } cp_parser_declarator_kind;
1131 /* The precedence values used to parse binary expressions. The minimum value
1132 of PREC must be 1, because zero is reserved to quickly discriminate
1133 binary operators from other tokens. */
1138 PREC_LOGICAL_OR_EXPRESSION,
1139 PREC_LOGICAL_AND_EXPRESSION,
1140 PREC_INCLUSIVE_OR_EXPRESSION,
1141 PREC_EXCLUSIVE_OR_EXPRESSION,
1142 PREC_AND_EXPRESSION,
1143 PREC_EQUALITY_EXPRESSION,
1144 PREC_RELATIONAL_EXPRESSION,
1145 PREC_SHIFT_EXPRESSION,
1146 PREC_ADDITIVE_EXPRESSION,
1147 PREC_MULTIPLICATIVE_EXPRESSION,
1149 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1152 /* A mapping from a token type to a corresponding tree node type, with a
1153 precedence value. */
1155 typedef struct cp_parser_binary_operations_map_node
1157 /* The token type. */
1158 enum cpp_ttype token_type;
1159 /* The corresponding tree code. */
1160 enum tree_code tree_type;
1161 /* The precedence of this operator. */
1162 enum cp_parser_prec prec;
1163 } cp_parser_binary_operations_map_node;
1165 /* The status of a tentative parse. */
1167 typedef enum cp_parser_status_kind
1169 /* No errors have occurred. */
1170 CP_PARSER_STATUS_KIND_NO_ERROR,
1171 /* An error has occurred. */
1172 CP_PARSER_STATUS_KIND_ERROR,
1173 /* We are committed to this tentative parse, whether or not an error
1175 CP_PARSER_STATUS_KIND_COMMITTED
1176 } cp_parser_status_kind;
1178 typedef struct cp_parser_expression_stack_entry
1180 /* Left hand side of the binary operation we are currently
1183 /* Original tree code for left hand side, if it was a binary
1184 expression itself (used for -Wparentheses). */
1185 enum tree_code lhs_type;
1186 /* Tree code for the binary operation we are parsing. */
1187 enum tree_code tree_type;
1188 /* Precedence of the binary operation we are parsing. */
1190 } cp_parser_expression_stack_entry;
1192 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1193 entries because precedence levels on the stack are monotonically
1195 typedef struct cp_parser_expression_stack_entry
1196 cp_parser_expression_stack[NUM_PREC_VALUES];
1198 /* Context that is saved and restored when parsing tentatively. */
1199 typedef struct cp_parser_context GTY (())
1201 /* If this is a tentative parsing context, the status of the
1203 enum cp_parser_status_kind status;
1204 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1205 that are looked up in this context must be looked up both in the
1206 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1207 the context of the containing expression. */
1210 /* The next parsing context in the stack. */
1211 struct cp_parser_context *next;
1212 } cp_parser_context;
1216 /* Constructors and destructors. */
1218 static cp_parser_context *cp_parser_context_new
1219 (cp_parser_context *);
1221 /* Class variables. */
1223 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1225 /* The operator-precedence table used by cp_parser_binary_expression.
1226 Transformed into an associative array (binops_by_token) by
1229 static const cp_parser_binary_operations_map_node binops[] = {
1230 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1231 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1233 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1234 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1235 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1237 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1238 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1240 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1241 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1243 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1244 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1245 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1246 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1248 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1249 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1251 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1253 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1255 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1257 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1259 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1262 /* The same as binops, but initialized by cp_parser_new so that
1263 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1265 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1267 /* Constructors and destructors. */
1269 /* Construct a new context. The context below this one on the stack
1270 is given by NEXT. */
1272 static cp_parser_context *
1273 cp_parser_context_new (cp_parser_context* next)
1275 cp_parser_context *context;
1277 /* Allocate the storage. */
1278 if (cp_parser_context_free_list != NULL)
1280 /* Pull the first entry from the free list. */
1281 context = cp_parser_context_free_list;
1282 cp_parser_context_free_list = context->next;
1283 memset (context, 0, sizeof (*context));
1286 context = GGC_CNEW (cp_parser_context);
1288 /* No errors have occurred yet in this context. */
1289 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1290 /* If this is not the bottomost context, copy information that we
1291 need from the previous context. */
1294 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1295 expression, then we are parsing one in this context, too. */
1296 context->object_type = next->object_type;
1297 /* Thread the stack. */
1298 context->next = next;
1304 /* The cp_parser structure represents the C++ parser. */
1306 typedef struct cp_parser GTY(())
1308 /* The lexer from which we are obtaining tokens. */
1311 /* The scope in which names should be looked up. If NULL_TREE, then
1312 we look up names in the scope that is currently open in the
1313 source program. If non-NULL, this is either a TYPE or
1314 NAMESPACE_DECL for the scope in which we should look. It can
1315 also be ERROR_MARK, when we've parsed a bogus scope.
1317 This value is not cleared automatically after a name is looked
1318 up, so we must be careful to clear it before starting a new look
1319 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1320 will look up `Z' in the scope of `X', rather than the current
1321 scope.) Unfortunately, it is difficult to tell when name lookup
1322 is complete, because we sometimes peek at a token, look it up,
1323 and then decide not to consume it. */
1326 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1327 last lookup took place. OBJECT_SCOPE is used if an expression
1328 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1329 respectively. QUALIFYING_SCOPE is used for an expression of the
1330 form "X::Y"; it refers to X. */
1332 tree qualifying_scope;
1334 /* A stack of parsing contexts. All but the bottom entry on the
1335 stack will be tentative contexts.
1337 We parse tentatively in order to determine which construct is in
1338 use in some situations. For example, in order to determine
1339 whether a statement is an expression-statement or a
1340 declaration-statement we parse it tentatively as a
1341 declaration-statement. If that fails, we then reparse the same
1342 token stream as an expression-statement. */
1343 cp_parser_context *context;
1345 /* True if we are parsing GNU C++. If this flag is not set, then
1346 GNU extensions are not recognized. */
1347 bool allow_gnu_extensions_p;
1349 /* TRUE if the `>' token should be interpreted as the greater-than
1350 operator. FALSE if it is the end of a template-id or
1351 template-parameter-list. */
1352 bool greater_than_is_operator_p;
1354 /* TRUE if default arguments are allowed within a parameter list
1355 that starts at this point. FALSE if only a gnu extension makes
1356 them permissible. */
1357 bool default_arg_ok_p;
1359 /* TRUE if we are parsing an integral constant-expression. See
1360 [expr.const] for a precise definition. */
1361 bool integral_constant_expression_p;
1363 /* TRUE if we are parsing an integral constant-expression -- but a
1364 non-constant expression should be permitted as well. This flag
1365 is used when parsing an array bound so that GNU variable-length
1366 arrays are tolerated. */
1367 bool allow_non_integral_constant_expression_p;
1369 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1370 been seen that makes the expression non-constant. */
1371 bool non_integral_constant_expression_p;
1373 /* TRUE if local variable names and `this' are forbidden in the
1375 bool local_variables_forbidden_p;
1377 /* TRUE if the declaration we are parsing is part of a
1378 linkage-specification of the form `extern string-literal
1380 bool in_unbraced_linkage_specification_p;
1382 /* TRUE if we are presently parsing a declarator, after the
1383 direct-declarator. */
1384 bool in_declarator_p;
1386 /* TRUE if we are presently parsing a template-argument-list. */
1387 bool in_template_argument_list_p;
1389 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1390 to IN_OMP_BLOCK if parsing OpenMP structured block and
1391 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1392 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1393 iteration-statement, OpenMP block or loop within that switch. */
1394 #define IN_SWITCH_STMT 1
1395 #define IN_ITERATION_STMT 2
1396 #define IN_OMP_BLOCK 4
1397 #define IN_OMP_FOR 8
1398 unsigned char in_statement;
1400 /* TRUE if we are presently parsing the body of a switch statement.
1401 Note that this doesn't quite overlap with in_statement above.
1402 The difference relates to giving the right sets of error messages:
1403 "case not in switch" vs "break statement used with OpenMP...". */
1404 bool in_switch_statement_p;
1406 /* TRUE if we are parsing a type-id in an expression context. In
1407 such a situation, both "type (expr)" and "type (type)" are valid
1409 bool in_type_id_in_expr_p;
1411 /* TRUE if we are currently in a header file where declarations are
1412 implicitly extern "C". */
1413 bool implicit_extern_c;
1415 /* TRUE if strings in expressions should be translated to the execution
1417 bool translate_strings_p;
1419 /* TRUE if we are presently parsing the body of a function, but not
1421 bool in_function_body;
1423 /* If non-NULL, then we are parsing a construct where new type
1424 definitions are not permitted. The string stored here will be
1425 issued as an error message if a type is defined. */
1426 const char *type_definition_forbidden_message;
1428 /* A list of lists. The outer list is a stack, used for member
1429 functions of local classes. At each level there are two sub-list,
1430 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1431 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1432 TREE_VALUE's. The functions are chained in reverse declaration
1435 The TREE_PURPOSE sublist contains those functions with default
1436 arguments that need post processing, and the TREE_VALUE sublist
1437 contains those functions with definitions that need post
1440 These lists can only be processed once the outermost class being
1441 defined is complete. */
1442 tree unparsed_functions_queues;
1444 /* The number of classes whose definitions are currently in
1446 unsigned num_classes_being_defined;
1448 /* The number of template parameter lists that apply directly to the
1449 current declaration. */
1450 unsigned num_template_parameter_lists;
1455 /* Constructors and destructors. */
1457 static cp_parser *cp_parser_new
1460 /* Routines to parse various constructs.
1462 Those that return `tree' will return the error_mark_node (rather
1463 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1464 Sometimes, they will return an ordinary node if error-recovery was
1465 attempted, even though a parse error occurred. So, to check
1466 whether or not a parse error occurred, you should always use
1467 cp_parser_error_occurred. If the construct is optional (indicated
1468 either by an `_opt' in the name of the function that does the
1469 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1470 the construct is not present. */
1472 /* Lexical conventions [gram.lex] */
1474 static tree cp_parser_identifier
1476 static tree cp_parser_string_literal
1477 (cp_parser *, bool, bool);
1479 /* Basic concepts [gram.basic] */
1481 static bool cp_parser_translation_unit
1484 /* Expressions [gram.expr] */
1486 static tree cp_parser_primary_expression
1487 (cp_parser *, bool, bool, bool, cp_id_kind *);
1488 static tree cp_parser_id_expression
1489 (cp_parser *, bool, bool, bool *, bool, bool);
1490 static tree cp_parser_unqualified_id
1491 (cp_parser *, bool, bool, bool, bool);
1492 static tree cp_parser_nested_name_specifier_opt
1493 (cp_parser *, bool, bool, bool, bool);
1494 static tree cp_parser_nested_name_specifier
1495 (cp_parser *, bool, bool, bool, bool);
1496 static tree cp_parser_class_or_namespace_name
1497 (cp_parser *, bool, bool, bool, bool, bool);
1498 static tree cp_parser_postfix_expression
1499 (cp_parser *, bool, bool);
1500 static tree cp_parser_postfix_open_square_expression
1501 (cp_parser *, tree, bool);
1502 static tree cp_parser_postfix_dot_deref_expression
1503 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1504 static tree cp_parser_parenthesized_expression_list
1505 (cp_parser *, bool, bool, bool *);
1506 static void cp_parser_pseudo_destructor_name
1507 (cp_parser *, tree *, tree *);
1508 static tree cp_parser_unary_expression
1509 (cp_parser *, bool, bool);
1510 static enum tree_code cp_parser_unary_operator
1512 static tree cp_parser_new_expression
1514 static tree cp_parser_new_placement
1516 static tree cp_parser_new_type_id
1517 (cp_parser *, tree *);
1518 static cp_declarator *cp_parser_new_declarator_opt
1520 static cp_declarator *cp_parser_direct_new_declarator
1522 static tree cp_parser_new_initializer
1524 static tree cp_parser_delete_expression
1526 static tree cp_parser_cast_expression
1527 (cp_parser *, bool, bool);
1528 static tree cp_parser_binary_expression
1529 (cp_parser *, bool);
1530 static tree cp_parser_question_colon_clause
1531 (cp_parser *, tree);
1532 static tree cp_parser_assignment_expression
1533 (cp_parser *, bool);
1534 static enum tree_code cp_parser_assignment_operator_opt
1536 static tree cp_parser_expression
1537 (cp_parser *, bool);
1538 static tree cp_parser_constant_expression
1539 (cp_parser *, bool, bool *);
1540 static tree cp_parser_builtin_offsetof
1543 /* Statements [gram.stmt.stmt] */
1545 static void cp_parser_statement
1546 (cp_parser *, tree, bool, bool *);
1547 static void cp_parser_label_for_labeled_statement
1549 static tree cp_parser_expression_statement
1550 (cp_parser *, tree);
1551 static tree cp_parser_compound_statement
1552 (cp_parser *, tree, bool);
1553 static void cp_parser_statement_seq_opt
1554 (cp_parser *, tree);
1555 static tree cp_parser_selection_statement
1556 (cp_parser *, bool *);
1557 static tree cp_parser_condition
1559 static tree cp_parser_iteration_statement
1561 static void cp_parser_for_init_statement
1563 static tree cp_parser_jump_statement
1565 static void cp_parser_declaration_statement
1568 static tree cp_parser_implicitly_scoped_statement
1569 (cp_parser *, bool *);
1570 static void cp_parser_already_scoped_statement
1573 /* Declarations [gram.dcl.dcl] */
1575 static void cp_parser_declaration_seq_opt
1577 static void cp_parser_declaration
1579 static void cp_parser_block_declaration
1580 (cp_parser *, bool);
1581 static void cp_parser_simple_declaration
1582 (cp_parser *, bool);
1583 static void cp_parser_decl_specifier_seq
1584 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1585 static tree cp_parser_storage_class_specifier_opt
1587 static tree cp_parser_function_specifier_opt
1588 (cp_parser *, cp_decl_specifier_seq *);
1589 static tree cp_parser_type_specifier
1590 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1592 static tree cp_parser_simple_type_specifier
1593 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1594 static tree cp_parser_type_name
1596 static tree cp_parser_elaborated_type_specifier
1597 (cp_parser *, bool, bool);
1598 static tree cp_parser_enum_specifier
1600 static void cp_parser_enumerator_list
1601 (cp_parser *, tree);
1602 static void cp_parser_enumerator_definition
1603 (cp_parser *, tree);
1604 static tree cp_parser_namespace_name
1606 static void cp_parser_namespace_definition
1608 static void cp_parser_namespace_body
1610 static tree cp_parser_qualified_namespace_specifier
1612 static void cp_parser_namespace_alias_definition
1614 static bool cp_parser_using_declaration
1615 (cp_parser *, bool);
1616 static void cp_parser_using_directive
1618 static void cp_parser_asm_definition
1620 static void cp_parser_linkage_specification
1623 /* Declarators [gram.dcl.decl] */
1625 static tree cp_parser_init_declarator
1626 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1627 static cp_declarator *cp_parser_declarator
1628 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1629 static cp_declarator *cp_parser_direct_declarator
1630 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1631 static enum tree_code cp_parser_ptr_operator
1632 (cp_parser *, tree *, cp_cv_quals *);
1633 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1635 static tree cp_parser_declarator_id
1636 (cp_parser *, bool);
1637 static tree cp_parser_type_id
1639 static void cp_parser_type_specifier_seq
1640 (cp_parser *, bool, cp_decl_specifier_seq *);
1641 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1643 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1644 (cp_parser *, bool *);
1645 static cp_parameter_declarator *cp_parser_parameter_declaration
1646 (cp_parser *, bool, bool *);
1647 static void cp_parser_function_body
1649 static tree cp_parser_initializer
1650 (cp_parser *, bool *, bool *);
1651 static tree cp_parser_initializer_clause
1652 (cp_parser *, bool *);
1653 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1654 (cp_parser *, bool *);
1656 static bool cp_parser_ctor_initializer_opt_and_function_body
1659 /* Classes [gram.class] */
1661 static tree cp_parser_class_name
1662 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1663 static tree cp_parser_class_specifier
1665 static tree cp_parser_class_head
1666 (cp_parser *, bool *, tree *, tree *);
1667 static enum tag_types cp_parser_class_key
1669 static void cp_parser_member_specification_opt
1671 static void cp_parser_member_declaration
1673 static tree cp_parser_pure_specifier
1675 static tree cp_parser_constant_initializer
1678 /* Derived classes [gram.class.derived] */
1680 static tree cp_parser_base_clause
1682 static tree cp_parser_base_specifier
1685 /* Special member functions [gram.special] */
1687 static tree cp_parser_conversion_function_id
1689 static tree cp_parser_conversion_type_id
1691 static cp_declarator *cp_parser_conversion_declarator_opt
1693 static bool cp_parser_ctor_initializer_opt
1695 static void cp_parser_mem_initializer_list
1697 static tree cp_parser_mem_initializer
1699 static tree cp_parser_mem_initializer_id
1702 /* Overloading [gram.over] */
1704 static tree cp_parser_operator_function_id
1706 static tree cp_parser_operator
1709 /* Templates [gram.temp] */
1711 static void cp_parser_template_declaration
1712 (cp_parser *, bool);
1713 static tree cp_parser_template_parameter_list
1715 static tree cp_parser_template_parameter
1716 (cp_parser *, bool *);
1717 static tree cp_parser_type_parameter
1719 static tree cp_parser_template_id
1720 (cp_parser *, bool, bool, bool);
1721 static tree cp_parser_template_name
1722 (cp_parser *, bool, bool, bool, bool *);
1723 static tree cp_parser_template_argument_list
1725 static tree cp_parser_template_argument
1727 static void cp_parser_explicit_instantiation
1729 static void cp_parser_explicit_specialization
1732 /* Exception handling [gram.exception] */
1734 static tree cp_parser_try_block
1736 static bool cp_parser_function_try_block
1738 static void cp_parser_handler_seq
1740 static void cp_parser_handler
1742 static tree cp_parser_exception_declaration
1744 static tree cp_parser_throw_expression
1746 static tree cp_parser_exception_specification_opt
1748 static tree cp_parser_type_id_list
1751 /* GNU Extensions */
1753 static tree cp_parser_asm_specification_opt
1755 static tree cp_parser_asm_operand_list
1757 static tree cp_parser_asm_clobber_list
1759 static tree cp_parser_attributes_opt
1761 static tree cp_parser_attribute_list
1763 static bool cp_parser_extension_opt
1764 (cp_parser *, int *);
1765 static void cp_parser_label_declaration
1768 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1769 static bool cp_parser_pragma
1770 (cp_parser *, enum pragma_context);
1772 /* Objective-C++ Productions */
1774 static tree cp_parser_objc_message_receiver
1776 static tree cp_parser_objc_message_args
1778 static tree cp_parser_objc_message_expression
1780 static tree cp_parser_objc_encode_expression
1782 static tree cp_parser_objc_defs_expression
1784 static tree cp_parser_objc_protocol_expression
1786 static tree cp_parser_objc_selector_expression
1788 static tree cp_parser_objc_expression
1790 static bool cp_parser_objc_selector_p
1792 static tree cp_parser_objc_selector
1794 static tree cp_parser_objc_protocol_refs_opt
1796 static void cp_parser_objc_declaration
1798 static tree cp_parser_objc_statement
1801 /* Utility Routines */
1803 static tree cp_parser_lookup_name
1804 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1805 static tree cp_parser_lookup_name_simple
1806 (cp_parser *, tree);
1807 static tree cp_parser_maybe_treat_template_as_class
1809 static bool cp_parser_check_declarator_template_parameters
1810 (cp_parser *, cp_declarator *);
1811 static bool cp_parser_check_template_parameters
1812 (cp_parser *, unsigned);
1813 static tree cp_parser_simple_cast_expression
1815 static tree cp_parser_global_scope_opt
1816 (cp_parser *, bool);
1817 static bool cp_parser_constructor_declarator_p
1818 (cp_parser *, bool);
1819 static tree cp_parser_function_definition_from_specifiers_and_declarator
1820 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1821 static tree cp_parser_function_definition_after_declarator
1822 (cp_parser *, bool);
1823 static void cp_parser_template_declaration_after_export
1824 (cp_parser *, bool);
1825 static void cp_parser_perform_template_parameter_access_checks
1826 (VEC (deferred_access_check,gc)*);
1827 static tree cp_parser_single_declaration
1828 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1829 static tree cp_parser_functional_cast
1830 (cp_parser *, tree);
1831 static tree cp_parser_save_member_function_body
1832 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1833 static tree cp_parser_enclosed_template_argument_list
1835 static void cp_parser_save_default_args
1836 (cp_parser *, tree);
1837 static void cp_parser_late_parsing_for_member
1838 (cp_parser *, tree);
1839 static void cp_parser_late_parsing_default_args
1840 (cp_parser *, tree);
1841 static tree cp_parser_sizeof_operand
1842 (cp_parser *, enum rid);
1843 static bool cp_parser_declares_only_class_p
1845 static void cp_parser_set_storage_class
1846 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1847 static void cp_parser_set_decl_spec_type
1848 (cp_decl_specifier_seq *, tree, bool);
1849 static bool cp_parser_friend_p
1850 (const cp_decl_specifier_seq *);
1851 static cp_token *cp_parser_require
1852 (cp_parser *, enum cpp_ttype, const char *);
1853 static cp_token *cp_parser_require_keyword
1854 (cp_parser *, enum rid, const char *);
1855 static bool cp_parser_token_starts_function_definition_p
1857 static bool cp_parser_next_token_starts_class_definition_p
1859 static bool cp_parser_next_token_ends_template_argument_p
1861 static bool cp_parser_nth_token_starts_template_argument_list_p
1862 (cp_parser *, size_t);
1863 static enum tag_types cp_parser_token_is_class_key
1865 static void cp_parser_check_class_key
1866 (enum tag_types, tree type);
1867 static void cp_parser_check_access_in_redeclaration
1869 static bool cp_parser_optional_template_keyword
1871 static void cp_parser_pre_parsed_nested_name_specifier
1873 static void cp_parser_cache_group
1874 (cp_parser *, enum cpp_ttype, unsigned);
1875 static void cp_parser_parse_tentatively
1877 static void cp_parser_commit_to_tentative_parse
1879 static void cp_parser_abort_tentative_parse
1881 static bool cp_parser_parse_definitely
1883 static inline bool cp_parser_parsing_tentatively
1885 static bool cp_parser_uncommitted_to_tentative_parse_p
1887 static void cp_parser_error
1888 (cp_parser *, const char *);
1889 static void cp_parser_name_lookup_error
1890 (cp_parser *, tree, tree, const char *);
1891 static bool cp_parser_simulate_error
1893 static bool cp_parser_check_type_definition
1895 static void cp_parser_check_for_definition_in_return_type
1896 (cp_declarator *, tree);
1897 static void cp_parser_check_for_invalid_template_id
1898 (cp_parser *, tree);
1899 static bool cp_parser_non_integral_constant_expression
1900 (cp_parser *, const char *);
1901 static void cp_parser_diagnose_invalid_type_name
1902 (cp_parser *, tree, tree);
1903 static bool cp_parser_parse_and_diagnose_invalid_type_name
1905 static int cp_parser_skip_to_closing_parenthesis
1906 (cp_parser *, bool, bool, bool);
1907 static void cp_parser_skip_to_end_of_statement
1909 static void cp_parser_consume_semicolon_at_end_of_statement
1911 static void cp_parser_skip_to_end_of_block_or_statement
1913 static void cp_parser_skip_to_closing_brace
1915 static void cp_parser_skip_to_end_of_template_parameter_list
1917 static void cp_parser_skip_to_pragma_eol
1918 (cp_parser*, cp_token *);
1919 static bool cp_parser_error_occurred
1921 static bool cp_parser_allow_gnu_extensions_p
1923 static bool cp_parser_is_string_literal
1925 static bool cp_parser_is_keyword
1926 (cp_token *, enum rid);
1927 static tree cp_parser_make_typename_type
1928 (cp_parser *, tree, tree);
1930 /* Returns nonzero if we are parsing tentatively. */
1933 cp_parser_parsing_tentatively (cp_parser* parser)
1935 return parser->context->next != NULL;
1938 /* Returns nonzero if TOKEN is a string literal. */
1941 cp_parser_is_string_literal (cp_token* token)
1943 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1946 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1949 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1951 return token->keyword == keyword;
1954 /* If not parsing tentatively, issue a diagnostic of the form
1955 FILE:LINE: MESSAGE before TOKEN
1956 where TOKEN is the next token in the input stream. MESSAGE
1957 (specified by the caller) is usually of the form "expected
1961 cp_parser_error (cp_parser* parser, const char* message)
1963 if (!cp_parser_simulate_error (parser))
1965 cp_token *token = cp_lexer_peek_token (parser->lexer);
1966 /* This diagnostic makes more sense if it is tagged to the line
1967 of the token we just peeked at. */
1968 cp_lexer_set_source_position_from_token (token);
1970 if (token->type == CPP_PRAGMA)
1972 error ("%<#pragma%> is not allowed here");
1973 cp_parser_skip_to_pragma_eol (parser, token);
1977 c_parse_error (message,
1978 /* Because c_parser_error does not understand
1979 CPP_KEYWORD, keywords are treated like
1981 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1986 /* Issue an error about name-lookup failing. NAME is the
1987 IDENTIFIER_NODE DECL is the result of
1988 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1989 the thing that we hoped to find. */
1992 cp_parser_name_lookup_error (cp_parser* parser,
1995 const char* desired)
1997 /* If name lookup completely failed, tell the user that NAME was not
1999 if (decl == error_mark_node)
2001 if (parser->scope && parser->scope != global_namespace)
2002 error ("%<%D::%D%> has not been declared",
2003 parser->scope, name);
2004 else if (parser->scope == global_namespace)
2005 error ("%<::%D%> has not been declared", name);
2006 else if (parser->object_scope
2007 && !CLASS_TYPE_P (parser->object_scope))
2008 error ("request for member %qD in non-class type %qT",
2009 name, parser->object_scope);
2010 else if (parser->object_scope)
2011 error ("%<%T::%D%> has not been declared",
2012 parser->object_scope, name);
2014 error ("%qD has not been declared", name);
2016 else if (parser->scope && parser->scope != global_namespace)
2017 error ("%<%D::%D%> %s", parser->scope, name, desired);
2018 else if (parser->scope == global_namespace)
2019 error ("%<::%D%> %s", name, desired);
2021 error ("%qD %s", name, desired);
2024 /* If we are parsing tentatively, remember that an error has occurred
2025 during this tentative parse. Returns true if the error was
2026 simulated; false if a message should be issued by the caller. */
2029 cp_parser_simulate_error (cp_parser* parser)
2031 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2033 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2039 /* Check for repeated decl-specifiers. */
2042 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2046 for (ds = ds_first; ds != ds_last; ++ds)
2048 unsigned count = decl_specs->specs[(int)ds];
2051 /* The "long" specifier is a special case because of "long long". */
2055 error ("%<long long long%> is too long for GCC");
2056 else if (pedantic && !in_system_header && warn_long_long)
2057 pedwarn ("ISO C++ does not support %<long long%>");
2061 static const char *const decl_spec_names[] = {
2077 error ("duplicate %qs", decl_spec_names[(int)ds]);
2082 /* This function is called when a type is defined. If type
2083 definitions are forbidden at this point, an error message is
2087 cp_parser_check_type_definition (cp_parser* parser)
2089 /* If types are forbidden here, issue a message. */
2090 if (parser->type_definition_forbidden_message)
2092 /* Use `%s' to print the string in case there are any escape
2093 characters in the message. */
2094 error ("%s", parser->type_definition_forbidden_message);
2100 /* This function is called when the DECLARATOR is processed. The TYPE
2101 was a type defined in the decl-specifiers. If it is invalid to
2102 define a type in the decl-specifiers for DECLARATOR, an error is
2106 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2109 /* [dcl.fct] forbids type definitions in return types.
2110 Unfortunately, it's not easy to know whether or not we are
2111 processing a return type until after the fact. */
2113 && (declarator->kind == cdk_pointer
2114 || declarator->kind == cdk_reference
2115 || declarator->kind == cdk_ptrmem))
2116 declarator = declarator->declarator;
2118 && declarator->kind == cdk_function)
2120 error ("new types may not be defined in a return type");
2121 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2126 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2127 "<" in any valid C++ program. If the next token is indeed "<",
2128 issue a message warning the user about what appears to be an
2129 invalid attempt to form a template-id. */
2132 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2135 cp_token_position start = 0;
2137 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2140 error ("%qT is not a template", type);
2141 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2142 error ("%qE is not a template", type);
2144 error ("invalid template-id");
2145 /* Remember the location of the invalid "<". */
2146 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2147 start = cp_lexer_token_position (parser->lexer, true);
2148 /* Consume the "<". */
2149 cp_lexer_consume_token (parser->lexer);
2150 /* Parse the template arguments. */
2151 cp_parser_enclosed_template_argument_list (parser);
2152 /* Permanently remove the invalid template arguments so that
2153 this error message is not issued again. */
2155 cp_lexer_purge_tokens_after (parser->lexer, start);
2159 /* If parsing an integral constant-expression, issue an error message
2160 about the fact that THING appeared and return true. Otherwise,
2161 return false. In either case, set
2162 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2165 cp_parser_non_integral_constant_expression (cp_parser *parser,
2168 parser->non_integral_constant_expression_p = true;
2169 if (parser->integral_constant_expression_p)
2171 if (!parser->allow_non_integral_constant_expression_p)
2173 error ("%s cannot appear in a constant-expression", thing);
2180 /* Emit a diagnostic for an invalid type name. SCOPE is the
2181 qualifying scope (or NULL, if none) for ID. This function commits
2182 to the current active tentative parse, if any. (Otherwise, the
2183 problematic construct might be encountered again later, resulting
2184 in duplicate error messages.) */
2187 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2189 tree decl, old_scope;
2190 /* Try to lookup the identifier. */
2191 old_scope = parser->scope;
2192 parser->scope = scope;
2193 decl = cp_parser_lookup_name_simple (parser, id);
2194 parser->scope = old_scope;
2195 /* If the lookup found a template-name, it means that the user forgot
2196 to specify an argument list. Emit a useful error message. */
2197 if (TREE_CODE (decl) == TEMPLATE_DECL)
2198 error ("invalid use of template-name %qE without an argument list", decl);
2199 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2200 error ("invalid use of destructor %qD as a type", id);
2201 else if (TREE_CODE (decl) == TYPE_DECL)
2202 /* Something like 'unsigned A a;' */
2203 error ("invalid combination of multiple type-specifiers");
2204 else if (!parser->scope)
2206 /* Issue an error message. */
2207 error ("%qE does not name a type", id);
2208 /* If we're in a template class, it's possible that the user was
2209 referring to a type from a base class. For example:
2211 template <typename T> struct A { typedef T X; };
2212 template <typename T> struct B : public A<T> { X x; };
2214 The user should have said "typename A<T>::X". */
2215 if (processing_template_decl && current_class_type
2216 && TYPE_BINFO (current_class_type))
2220 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2224 tree base_type = BINFO_TYPE (b);
2225 if (CLASS_TYPE_P (base_type)
2226 && dependent_type_p (base_type))
2229 /* Go from a particular instantiation of the
2230 template (which will have an empty TYPE_FIELDs),
2231 to the main version. */
2232 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2233 for (field = TYPE_FIELDS (base_type);
2235 field = TREE_CHAIN (field))
2236 if (TREE_CODE (field) == TYPE_DECL
2237 && DECL_NAME (field) == id)
2239 inform ("(perhaps %<typename %T::%E%> was intended)",
2240 BINFO_TYPE (b), id);
2249 /* Here we diagnose qualified-ids where the scope is actually correct,
2250 but the identifier does not resolve to a valid type name. */
2251 else if (parser->scope != error_mark_node)
2253 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2254 error ("%qE in namespace %qE does not name a type",
2256 else if (TYPE_P (parser->scope))
2257 error ("%qE in class %qT does not name a type", id, parser->scope);
2261 cp_parser_commit_to_tentative_parse (parser);
2264 /* Check for a common situation where a type-name should be present,
2265 but is not, and issue a sensible error message. Returns true if an
2266 invalid type-name was detected.
2268 The situation handled by this function are variable declarations of the
2269 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2270 Usually, `ID' should name a type, but if we got here it means that it
2271 does not. We try to emit the best possible error message depending on
2272 how exactly the id-expression looks like. */
2275 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2279 cp_parser_parse_tentatively (parser);
2280 id = cp_parser_id_expression (parser,
2281 /*template_keyword_p=*/false,
2282 /*check_dependency_p=*/true,
2283 /*template_p=*/NULL,
2284 /*declarator_p=*/true,
2285 /*optional_p=*/false);
2286 /* After the id-expression, there should be a plain identifier,
2287 otherwise this is not a simple variable declaration. Also, if
2288 the scope is dependent, we cannot do much. */
2289 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2290 || (parser->scope && TYPE_P (parser->scope)
2291 && dependent_type_p (parser->scope))
2292 || TREE_CODE (id) == TYPE_DECL)
2294 cp_parser_abort_tentative_parse (parser);
2297 if (!cp_parser_parse_definitely (parser))
2300 /* Emit a diagnostic for the invalid type. */
2301 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2302 /* Skip to the end of the declaration; there's no point in
2303 trying to process it. */
2304 cp_parser_skip_to_end_of_block_or_statement (parser);
2308 /* Consume tokens up to, and including, the next non-nested closing `)'.
2309 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2310 are doing error recovery. Returns -1 if OR_COMMA is true and we
2311 found an unnested comma. */
2314 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2319 unsigned paren_depth = 0;
2320 unsigned brace_depth = 0;
2322 if (recovering && !or_comma
2323 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2328 cp_token * token = cp_lexer_peek_token (parser->lexer);
2330 switch (token->type)
2333 case CPP_PRAGMA_EOL:
2334 /* If we've run out of tokens, then there is no closing `)'. */
2338 /* This matches the processing in skip_to_end_of_statement. */
2343 case CPP_OPEN_BRACE:
2346 case CPP_CLOSE_BRACE:
2352 if (recovering && or_comma && !brace_depth && !paren_depth)
2356 case CPP_OPEN_PAREN:
2361 case CPP_CLOSE_PAREN:
2362 if (!brace_depth && !paren_depth--)
2365 cp_lexer_consume_token (parser->lexer);
2374 /* Consume the token. */
2375 cp_lexer_consume_token (parser->lexer);
2379 /* Consume tokens until we reach the end of the current statement.
2380 Normally, that will be just before consuming a `;'. However, if a
2381 non-nested `}' comes first, then we stop before consuming that. */
2384 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2386 unsigned nesting_depth = 0;
2390 cp_token *token = cp_lexer_peek_token (parser->lexer);
2392 switch (token->type)
2395 case CPP_PRAGMA_EOL:
2396 /* If we've run out of tokens, stop. */
2400 /* If the next token is a `;', we have reached the end of the
2406 case CPP_CLOSE_BRACE:
2407 /* If this is a non-nested '}', stop before consuming it.
2408 That way, when confronted with something like:
2412 we stop before consuming the closing '}', even though we
2413 have not yet reached a `;'. */
2414 if (nesting_depth == 0)
2417 /* If it is the closing '}' for a block that we have
2418 scanned, stop -- but only after consuming the token.
2424 we will stop after the body of the erroneously declared
2425 function, but before consuming the following `typedef'
2427 if (--nesting_depth == 0)
2429 cp_lexer_consume_token (parser->lexer);
2433 case CPP_OPEN_BRACE:
2441 /* Consume the token. */
2442 cp_lexer_consume_token (parser->lexer);
2446 /* This function is called at the end of a statement or declaration.
2447 If the next token is a semicolon, it is consumed; otherwise, error
2448 recovery is attempted. */
2451 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2453 /* Look for the trailing `;'. */
2454 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2456 /* If there is additional (erroneous) input, skip to the end of
2458 cp_parser_skip_to_end_of_statement (parser);
2459 /* If the next token is now a `;', consume it. */
2460 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2461 cp_lexer_consume_token (parser->lexer);
2465 /* Skip tokens until we have consumed an entire block, or until we
2466 have consumed a non-nested `;'. */
2469 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2471 int nesting_depth = 0;
2473 while (nesting_depth >= 0)
2475 cp_token *token = cp_lexer_peek_token (parser->lexer);
2477 switch (token->type)
2480 case CPP_PRAGMA_EOL:
2481 /* If we've run out of tokens, stop. */
2485 /* Stop if this is an unnested ';'. */
2490 case CPP_CLOSE_BRACE:
2491 /* Stop if this is an unnested '}', or closes the outermost
2498 case CPP_OPEN_BRACE:
2507 /* Consume the token. */
2508 cp_lexer_consume_token (parser->lexer);
2512 /* Skip tokens until a non-nested closing curly brace is the next
2516 cp_parser_skip_to_closing_brace (cp_parser *parser)
2518 unsigned nesting_depth = 0;
2522 cp_token *token = cp_lexer_peek_token (parser->lexer);
2524 switch (token->type)
2527 case CPP_PRAGMA_EOL:
2528 /* If we've run out of tokens, stop. */
2531 case CPP_CLOSE_BRACE:
2532 /* If the next token is a non-nested `}', then we have reached
2533 the end of the current block. */
2534 if (nesting_depth-- == 0)
2538 case CPP_OPEN_BRACE:
2539 /* If it the next token is a `{', then we are entering a new
2540 block. Consume the entire block. */
2548 /* Consume the token. */
2549 cp_lexer_consume_token (parser->lexer);
2553 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2554 parameter is the PRAGMA token, allowing us to purge the entire pragma
2558 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2562 parser->lexer->in_pragma = false;
2565 token = cp_lexer_consume_token (parser->lexer);
2566 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2568 /* Ensure that the pragma is not parsed again. */
2569 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2572 /* Require pragma end of line, resyncing with it as necessary. The
2573 arguments are as for cp_parser_skip_to_pragma_eol. */
2576 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2578 parser->lexer->in_pragma = false;
2579 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2580 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2583 /* This is a simple wrapper around make_typename_type. When the id is
2584 an unresolved identifier node, we can provide a superior diagnostic
2585 using cp_parser_diagnose_invalid_type_name. */
2588 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2591 if (TREE_CODE (id) == IDENTIFIER_NODE)
2593 result = make_typename_type (scope, id, typename_type,
2594 /*complain=*/tf_none);
2595 if (result == error_mark_node)
2596 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2599 return make_typename_type (scope, id, typename_type, tf_error);
2603 /* Create a new C++ parser. */
2606 cp_parser_new (void)
2612 /* cp_lexer_new_main is called before calling ggc_alloc because
2613 cp_lexer_new_main might load a PCH file. */
2614 lexer = cp_lexer_new_main ();
2616 /* Initialize the binops_by_token so that we can get the tree
2617 directly from the token. */
2618 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2619 binops_by_token[binops[i].token_type] = binops[i];
2621 parser = GGC_CNEW (cp_parser);
2622 parser->lexer = lexer;
2623 parser->context = cp_parser_context_new (NULL);
2625 /* For now, we always accept GNU extensions. */
2626 parser->allow_gnu_extensions_p = 1;
2628 /* The `>' token is a greater-than operator, not the end of a
2630 parser->greater_than_is_operator_p = true;
2632 parser->default_arg_ok_p = true;
2634 /* We are not parsing a constant-expression. */
2635 parser->integral_constant_expression_p = false;
2636 parser->allow_non_integral_constant_expression_p = false;
2637 parser->non_integral_constant_expression_p = false;
2639 /* Local variable names are not forbidden. */
2640 parser->local_variables_forbidden_p = false;
2642 /* We are not processing an `extern "C"' declaration. */
2643 parser->in_unbraced_linkage_specification_p = false;
2645 /* We are not processing a declarator. */
2646 parser->in_declarator_p = false;
2648 /* We are not processing a template-argument-list. */
2649 parser->in_template_argument_list_p = false;
2651 /* We are not in an iteration statement. */
2652 parser->in_statement = 0;
2654 /* We are not in a switch statement. */
2655 parser->in_switch_statement_p = false;
2657 /* We are not parsing a type-id inside an expression. */
2658 parser->in_type_id_in_expr_p = false;
2660 /* Declarations aren't implicitly extern "C". */
2661 parser->implicit_extern_c = false;
2663 /* String literals should be translated to the execution character set. */
2664 parser->translate_strings_p = true;
2666 /* We are not parsing a function body. */
2667 parser->in_function_body = false;
2669 /* The unparsed function queue is empty. */
2670 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2672 /* There are no classes being defined. */
2673 parser->num_classes_being_defined = 0;
2675 /* No template parameters apply. */
2676 parser->num_template_parameter_lists = 0;
2681 /* Create a cp_lexer structure which will emit the tokens in CACHE
2682 and push it onto the parser's lexer stack. This is used for delayed
2683 parsing of in-class method bodies and default arguments, and should
2684 not be confused with tentative parsing. */
2686 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2688 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2689 lexer->next = parser->lexer;
2690 parser->lexer = lexer;
2692 /* Move the current source position to that of the first token in the
2694 cp_lexer_set_source_position_from_token (lexer->next_token);
2697 /* Pop the top lexer off the parser stack. This is never used for the
2698 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2700 cp_parser_pop_lexer (cp_parser *parser)
2702 cp_lexer *lexer = parser->lexer;
2703 parser->lexer = lexer->next;
2704 cp_lexer_destroy (lexer);
2706 /* Put the current source position back where it was before this
2707 lexer was pushed. */
2708 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2711 /* Lexical conventions [gram.lex] */
2713 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2717 cp_parser_identifier (cp_parser* parser)
2721 /* Look for the identifier. */
2722 token = cp_parser_require (parser, CPP_NAME, "identifier");
2723 /* Return the value. */
2724 return token ? token->u.value : error_mark_node;
2727 /* Parse a sequence of adjacent string constants. Returns a
2728 TREE_STRING representing the combined, nul-terminated string
2729 constant. If TRANSLATE is true, translate the string to the
2730 execution character set. If WIDE_OK is true, a wide string is
2733 C++98 [lex.string] says that if a narrow string literal token is
2734 adjacent to a wide string literal token, the behavior is undefined.
2735 However, C99 6.4.5p4 says that this results in a wide string literal.
2736 We follow C99 here, for consistency with the C front end.
2738 This code is largely lifted from lex_string() in c-lex.c.
2740 FUTURE: ObjC++ will need to handle @-strings here. */
2742 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2747 struct obstack str_ob;
2748 cpp_string str, istr, *strs;
2751 tok = cp_lexer_peek_token (parser->lexer);
2752 if (!cp_parser_is_string_literal (tok))
2754 cp_parser_error (parser, "expected string-literal");
2755 return error_mark_node;
2758 /* Try to avoid the overhead of creating and destroying an obstack
2759 for the common case of just one string. */
2760 if (!cp_parser_is_string_literal
2761 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2763 cp_lexer_consume_token (parser->lexer);
2765 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2766 str.len = TREE_STRING_LENGTH (tok->u.value);
2768 if (tok->type == CPP_WSTRING)
2775 gcc_obstack_init (&str_ob);
2780 cp_lexer_consume_token (parser->lexer);
2782 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2783 str.len = TREE_STRING_LENGTH (tok->u.value);
2784 if (tok->type == CPP_WSTRING)
2787 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2789 tok = cp_lexer_peek_token (parser->lexer);
2791 while (cp_parser_is_string_literal (tok));
2793 strs = (cpp_string *) obstack_finish (&str_ob);
2796 if (wide && !wide_ok)
2798 cp_parser_error (parser, "a wide string is invalid in this context");
2802 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2803 (parse_in, strs, count, &istr, wide))
2805 value = build_string (istr.len, (char *)istr.text);
2806 free ((void *)istr.text);
2808 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2809 value = fix_string_type (value);
2812 /* cpp_interpret_string has issued an error. */
2813 value = error_mark_node;
2816 obstack_free (&str_ob, 0);
2822 /* Basic concepts [gram.basic] */
2824 /* Parse a translation-unit.
2827 declaration-seq [opt]
2829 Returns TRUE if all went well. */
2832 cp_parser_translation_unit (cp_parser* parser)
2834 /* The address of the first non-permanent object on the declarator
2836 static void *declarator_obstack_base;
2840 /* Create the declarator obstack, if necessary. */
2841 if (!cp_error_declarator)
2843 gcc_obstack_init (&declarator_obstack);
2844 /* Create the error declarator. */
2845 cp_error_declarator = make_declarator (cdk_error);
2846 /* Create the empty parameter list. */
2847 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2848 /* Remember where the base of the declarator obstack lies. */
2849 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2852 cp_parser_declaration_seq_opt (parser);
2854 /* If there are no tokens left then all went well. */
2855 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2857 /* Get rid of the token array; we don't need it any more. */
2858 cp_lexer_destroy (parser->lexer);
2859 parser->lexer = NULL;
2861 /* This file might have been a context that's implicitly extern
2862 "C". If so, pop the lang context. (Only relevant for PCH.) */
2863 if (parser->implicit_extern_c)
2865 pop_lang_context ();
2866 parser->implicit_extern_c = false;
2870 finish_translation_unit ();
2876 cp_parser_error (parser, "expected declaration");
2880 /* Make sure the declarator obstack was fully cleaned up. */
2881 gcc_assert (obstack_next_free (&declarator_obstack)
2882 == declarator_obstack_base);
2884 /* All went well. */
2888 /* Expressions [gram.expr] */
2890 /* Parse a primary-expression.
2901 ( compound-statement )
2902 __builtin_va_arg ( assignment-expression , type-id )
2903 __builtin_offsetof ( type-id , offsetof-expression )
2905 Objective-C++ Extension:
2913 ADDRESS_P is true iff this expression was immediately preceded by
2914 "&" and therefore might denote a pointer-to-member. CAST_P is true
2915 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2916 true iff this expression is a template argument.
2918 Returns a representation of the expression. Upon return, *IDK
2919 indicates what kind of id-expression (if any) was present. */
2922 cp_parser_primary_expression (cp_parser *parser,
2925 bool template_arg_p,
2930 /* Assume the primary expression is not an id-expression. */
2931 *idk = CP_ID_KIND_NONE;
2933 /* Peek at the next token. */
2934 token = cp_lexer_peek_token (parser->lexer);
2935 switch (token->type)
2946 token = cp_lexer_consume_token (parser->lexer);
2947 /* Floating-point literals are only allowed in an integral
2948 constant expression if they are cast to an integral or
2949 enumeration type. */
2950 if (TREE_CODE (token->u.value) == REAL_CST
2951 && parser->integral_constant_expression_p
2954 /* CAST_P will be set even in invalid code like "int(2.7 +
2955 ...)". Therefore, we have to check that the next token
2956 is sure to end the cast. */
2959 cp_token *next_token;
2961 next_token = cp_lexer_peek_token (parser->lexer);
2962 if (/* The comma at the end of an
2963 enumerator-definition. */
2964 next_token->type != CPP_COMMA
2965 /* The curly brace at the end of an enum-specifier. */
2966 && next_token->type != CPP_CLOSE_BRACE
2967 /* The end of a statement. */
2968 && next_token->type != CPP_SEMICOLON
2969 /* The end of the cast-expression. */
2970 && next_token->type != CPP_CLOSE_PAREN
2971 /* The end of an array bound. */
2972 && next_token->type != CPP_CLOSE_SQUARE
2973 /* The closing ">" in a template-argument-list. */
2974 && (next_token->type != CPP_GREATER
2975 || parser->greater_than_is_operator_p))
2979 /* If we are within a cast, then the constraint that the
2980 cast is to an integral or enumeration type will be
2981 checked at that point. If we are not within a cast, then
2982 this code is invalid. */
2984 cp_parser_non_integral_constant_expression
2985 (parser, "floating-point literal");
2987 return token->u.value;
2991 /* ??? Should wide strings be allowed when parser->translate_strings_p
2992 is false (i.e. in attributes)? If not, we can kill the third
2993 argument to cp_parser_string_literal. */
2994 return cp_parser_string_literal (parser,
2995 parser->translate_strings_p,
2998 case CPP_OPEN_PAREN:
3001 bool saved_greater_than_is_operator_p;
3003 /* Consume the `('. */
3004 cp_lexer_consume_token (parser->lexer);
3005 /* Within a parenthesized expression, a `>' token is always
3006 the greater-than operator. */
3007 saved_greater_than_is_operator_p
3008 = parser->greater_than_is_operator_p;
3009 parser->greater_than_is_operator_p = true;
3010 /* If we see `( { ' then we are looking at the beginning of
3011 a GNU statement-expression. */
3012 if (cp_parser_allow_gnu_extensions_p (parser)
3013 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3015 /* Statement-expressions are not allowed by the standard. */
3017 pedwarn ("ISO C++ forbids braced-groups within expressions");
3019 /* And they're not allowed outside of a function-body; you
3020 cannot, for example, write:
3022 int i = ({ int j = 3; j + 1; });
3024 at class or namespace scope. */
3025 if (!parser->in_function_body)
3026 error ("statement-expressions are allowed only inside functions");
3027 /* Start the statement-expression. */
3028 expr = begin_stmt_expr ();
3029 /* Parse the compound-statement. */
3030 cp_parser_compound_statement (parser, expr, false);
3032 expr = finish_stmt_expr (expr, false);
3036 /* Parse the parenthesized expression. */
3037 expr = cp_parser_expression (parser, cast_p);
3038 /* Let the front end know that this expression was
3039 enclosed in parentheses. This matters in case, for
3040 example, the expression is of the form `A::B', since
3041 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3043 finish_parenthesized_expr (expr);
3045 /* The `>' token might be the end of a template-id or
3046 template-parameter-list now. */
3047 parser->greater_than_is_operator_p
3048 = saved_greater_than_is_operator_p;
3049 /* Consume the `)'. */
3050 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3051 cp_parser_skip_to_end_of_statement (parser);
3057 switch (token->keyword)
3059 /* These two are the boolean literals. */
3061 cp_lexer_consume_token (parser->lexer);
3062 return boolean_true_node;
3064 cp_lexer_consume_token (parser->lexer);
3065 return boolean_false_node;
3067 /* The `__null' literal. */
3069 cp_lexer_consume_token (parser->lexer);
3072 /* Recognize the `this' keyword. */
3074 cp_lexer_consume_token (parser->lexer);
3075 if (parser->local_variables_forbidden_p)
3077 error ("%<this%> may not be used in this context");
3078 return error_mark_node;
3080 /* Pointers cannot appear in constant-expressions. */
3081 if (cp_parser_non_integral_constant_expression (parser,
3083 return error_mark_node;
3084 return finish_this_expr ();
3086 /* The `operator' keyword can be the beginning of an
3091 case RID_FUNCTION_NAME:
3092 case RID_PRETTY_FUNCTION_NAME:
3093 case RID_C99_FUNCTION_NAME:
3094 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3095 __func__ are the names of variables -- but they are
3096 treated specially. Therefore, they are handled here,
3097 rather than relying on the generic id-expression logic
3098 below. Grammatically, these names are id-expressions.
3100 Consume the token. */
3101 token = cp_lexer_consume_token (parser->lexer);
3102 /* Look up the name. */
3103 return finish_fname (token->u.value);
3110 /* The `__builtin_va_arg' construct is used to handle
3111 `va_arg'. Consume the `__builtin_va_arg' token. */
3112 cp_lexer_consume_token (parser->lexer);
3113 /* Look for the opening `('. */
3114 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3115 /* Now, parse the assignment-expression. */
3116 expression = cp_parser_assignment_expression (parser,
3118 /* Look for the `,'. */
3119 cp_parser_require (parser, CPP_COMMA, "`,'");
3120 /* Parse the type-id. */
3121 type = cp_parser_type_id (parser);
3122 /* Look for the closing `)'. */
3123 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3124 /* Using `va_arg' in a constant-expression is not
3126 if (cp_parser_non_integral_constant_expression (parser,
3128 return error_mark_node;
3129 return build_x_va_arg (expression, type);
3133 return cp_parser_builtin_offsetof (parser);
3135 /* Objective-C++ expressions. */
3137 case RID_AT_PROTOCOL:
3138 case RID_AT_SELECTOR:
3139 return cp_parser_objc_expression (parser);
3142 cp_parser_error (parser, "expected primary-expression");
3143 return error_mark_node;
3146 /* An id-expression can start with either an identifier, a
3147 `::' as the beginning of a qualified-id, or the "operator"
3151 case CPP_TEMPLATE_ID:
3152 case CPP_NESTED_NAME_SPECIFIER:
3156 const char *error_msg;
3161 /* Parse the id-expression. */
3163 = cp_parser_id_expression (parser,
3164 /*template_keyword_p=*/false,
3165 /*check_dependency_p=*/true,
3167 /*declarator_p=*/false,
3168 /*optional_p=*/false);
3169 if (id_expression == error_mark_node)
3170 return error_mark_node;
3171 token = cp_lexer_peek_token (parser->lexer);
3172 done = (token->type != CPP_OPEN_SQUARE
3173 && token->type != CPP_OPEN_PAREN
3174 && token->type != CPP_DOT
3175 && token->type != CPP_DEREF
3176 && token->type != CPP_PLUS_PLUS
3177 && token->type != CPP_MINUS_MINUS);
3178 /* If we have a template-id, then no further lookup is
3179 required. If the template-id was for a template-class, we
3180 will sometimes have a TYPE_DECL at this point. */
3181 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3182 || TREE_CODE (id_expression) == TYPE_DECL)
3183 decl = id_expression;
3184 /* Look up the name. */
3187 tree ambiguous_decls;
3189 decl = cp_parser_lookup_name (parser, id_expression,
3192 /*is_namespace=*/false,
3193 /*check_dependency=*/true,
3195 /* If the lookup was ambiguous, an error will already have
3197 if (ambiguous_decls)
3198 return error_mark_node;
3200 /* In Objective-C++, an instance variable (ivar) may be preferred
3201 to whatever cp_parser_lookup_name() found. */
3202 decl = objc_lookup_ivar (decl, id_expression);
3204 /* If name lookup gives us a SCOPE_REF, then the
3205 qualifying scope was dependent. */
3206 if (TREE_CODE (decl) == SCOPE_REF)
3208 /* At this point, we do not know if DECL is a valid
3209 integral constant expression. We assume that it is
3210 in fact such an expression, so that code like:
3212 template <int N> struct A {
3216 is accepted. At template-instantiation time, we
3217 will check that B<N>::i is actually a constant. */
3220 /* Check to see if DECL is a local variable in a context
3221 where that is forbidden. */
3222 if (parser->local_variables_forbidden_p
3223 && local_variable_p (decl))
3225 /* It might be that we only found DECL because we are
3226 trying to be generous with pre-ISO scoping rules.
3227 For example, consider:
3231 for (int i = 0; i < 10; ++i) {}
3232 extern void f(int j = i);
3235 Here, name look up will originally find the out
3236 of scope `i'. We need to issue a warning message,
3237 but then use the global `i'. */
3238 decl = check_for_out_of_scope_variable (decl);
3239 if (local_variable_p (decl))
3241 error ("local variable %qD may not appear in this context",
3243 return error_mark_node;
3248 decl = (finish_id_expression
3249 (id_expression, decl, parser->scope,
3251 parser->integral_constant_expression_p,
3252 parser->allow_non_integral_constant_expression_p,
3253 &parser->non_integral_constant_expression_p,
3254 template_p, done, address_p,
3258 cp_parser_error (parser, error_msg);
3262 /* Anything else is an error. */
3264 /* ...unless we have an Objective-C++ message or string literal, that is. */
3265 if (c_dialect_objc ()
3266 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3267 return cp_parser_objc_expression (parser);
3269 cp_parser_error (parser, "expected primary-expression");
3270 return error_mark_node;
3274 /* Parse an id-expression.
3281 :: [opt] nested-name-specifier template [opt] unqualified-id
3283 :: operator-function-id
3286 Return a representation of the unqualified portion of the
3287 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3288 a `::' or nested-name-specifier.
3290 Often, if the id-expression was a qualified-id, the caller will
3291 want to make a SCOPE_REF to represent the qualified-id. This
3292 function does not do this in order to avoid wastefully creating
3293 SCOPE_REFs when they are not required.
3295 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3298 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3299 uninstantiated templates.
3301 If *TEMPLATE_P is non-NULL, it is set to true iff the
3302 `template' keyword is used to explicitly indicate that the entity
3303 named is a template.
3305 If DECLARATOR_P is true, the id-expression is appearing as part of
3306 a declarator, rather than as part of an expression. */
3309 cp_parser_id_expression (cp_parser *parser,
3310 bool template_keyword_p,
3311 bool check_dependency_p,
3316 bool global_scope_p;
3317 bool nested_name_specifier_p;
3319 /* Assume the `template' keyword was not used. */
3321 *template_p = template_keyword_p;
3323 /* Look for the optional `::' operator. */
3325 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3327 /* Look for the optional nested-name-specifier. */
3328 nested_name_specifier_p
3329 = (cp_parser_nested_name_specifier_opt (parser,
3330 /*typename_keyword_p=*/false,
3335 /* If there is a nested-name-specifier, then we are looking at
3336 the first qualified-id production. */
3337 if (nested_name_specifier_p)
3340 tree saved_object_scope;
3341 tree saved_qualifying_scope;
3342 tree unqualified_id;
3345 /* See if the next token is the `template' keyword. */
3347 template_p = &is_template;
3348 *template_p = cp_parser_optional_template_keyword (parser);
3349 /* Name lookup we do during the processing of the
3350 unqualified-id might obliterate SCOPE. */
3351 saved_scope = parser->scope;
3352 saved_object_scope = parser->object_scope;
3353 saved_qualifying_scope = parser->qualifying_scope;
3354 /* Process the final unqualified-id. */
3355 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3358 /*optional_p=*/false);
3359 /* Restore the SAVED_SCOPE for our caller. */
3360 parser->scope = saved_scope;
3361 parser->object_scope = saved_object_scope;
3362 parser->qualifying_scope = saved_qualifying_scope;
3364 return unqualified_id;
3366 /* Otherwise, if we are in global scope, then we are looking at one
3367 of the other qualified-id productions. */
3368 else if (global_scope_p)
3373 /* Peek at the next token. */
3374 token = cp_lexer_peek_token (parser->lexer);
3376 /* If it's an identifier, and the next token is not a "<", then
3377 we can avoid the template-id case. This is an optimization
3378 for this common case. */
3379 if (token->type == CPP_NAME
3380 && !cp_parser_nth_token_starts_template_argument_list_p
3382 return cp_parser_identifier (parser);
3384 cp_parser_parse_tentatively (parser);
3385 /* Try a template-id. */
3386 id = cp_parser_template_id (parser,
3387 /*template_keyword_p=*/false,
3388 /*check_dependency_p=*/true,
3390 /* If that worked, we're done. */
3391 if (cp_parser_parse_definitely (parser))
3394 /* Peek at the next token. (Changes in the token buffer may
3395 have invalidated the pointer obtained above.) */
3396 token = cp_lexer_peek_token (parser->lexer);
3398 switch (token->type)
3401 return cp_parser_identifier (parser);
3404 if (token->keyword == RID_OPERATOR)
3405 return cp_parser_operator_function_id (parser);
3409 cp_parser_error (parser, "expected id-expression");
3410 return error_mark_node;
3414 return cp_parser_unqualified_id (parser, template_keyword_p,
3415 /*check_dependency_p=*/true,
3420 /* Parse an unqualified-id.
3424 operator-function-id
3425 conversion-function-id
3429 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3430 keyword, in a construct like `A::template ...'.
3432 Returns a representation of unqualified-id. For the `identifier'
3433 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3434 production a BIT_NOT_EXPR is returned; the operand of the
3435 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3436 other productions, see the documentation accompanying the
3437 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3438 names are looked up in uninstantiated templates. If DECLARATOR_P
3439 is true, the unqualified-id is appearing as part of a declarator,
3440 rather than as part of an expression. */
3443 cp_parser_unqualified_id (cp_parser* parser,
3444 bool template_keyword_p,
3445 bool check_dependency_p,
3451 /* Peek at the next token. */
3452 token = cp_lexer_peek_token (parser->lexer);
3454 switch (token->type)
3460 /* We don't know yet whether or not this will be a
3462 cp_parser_parse_tentatively (parser);
3463 /* Try a template-id. */
3464 id = cp_parser_template_id (parser, template_keyword_p,
3467 /* If it worked, we're done. */
3468 if (cp_parser_parse_definitely (parser))
3470 /* Otherwise, it's an ordinary identifier. */
3471 return cp_parser_identifier (parser);
3474 case CPP_TEMPLATE_ID:
3475 return cp_parser_template_id (parser, template_keyword_p,
3482 tree qualifying_scope;
3487 /* Consume the `~' token. */
3488 cp_lexer_consume_token (parser->lexer);
3489 /* Parse the class-name. The standard, as written, seems to
3492 template <typename T> struct S { ~S (); };
3493 template <typename T> S<T>::~S() {}
3495 is invalid, since `~' must be followed by a class-name, but
3496 `S<T>' is dependent, and so not known to be a class.
3497 That's not right; we need to look in uninstantiated
3498 templates. A further complication arises from:
3500 template <typename T> void f(T t) {
3504 Here, it is not possible to look up `T' in the scope of `T'
3505 itself. We must look in both the current scope, and the
3506 scope of the containing complete expression.
3508 Yet another issue is:
3517 The standard does not seem to say that the `S' in `~S'
3518 should refer to the type `S' and not the data member
3521 /* DR 244 says that we look up the name after the "~" in the
3522 same scope as we looked up the qualifying name. That idea
3523 isn't fully worked out; it's more complicated than that. */
3524 scope = parser->scope;
3525 object_scope = parser->object_scope;
3526 qualifying_scope = parser->qualifying_scope;
3528 /* Check for invalid scopes. */
3529 if (scope == error_mark_node)
3531 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3532 cp_lexer_consume_token (parser->lexer);
3533 return error_mark_node;
3535 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3537 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3538 error ("scope %qT before %<~%> is not a class-name", scope);
3539 cp_parser_simulate_error (parser);
3540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3541 cp_lexer_consume_token (parser->lexer);
3542 return error_mark_node;
3544 gcc_assert (!scope || TYPE_P (scope));
3546 /* If the name is of the form "X::~X" it's OK. */
3547 token = cp_lexer_peek_token (parser->lexer);
3549 && token->type == CPP_NAME
3550 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3552 && constructor_name_p (token->u.value, scope))
3554 cp_lexer_consume_token (parser->lexer);
3555 return build_nt (BIT_NOT_EXPR, scope);
3558 /* If there was an explicit qualification (S::~T), first look
3559 in the scope given by the qualification (i.e., S). */
3561 type_decl = NULL_TREE;
3564 cp_parser_parse_tentatively (parser);
3565 type_decl = cp_parser_class_name (parser,
3566 /*typename_keyword_p=*/false,
3567 /*template_keyword_p=*/false,
3569 /*check_dependency=*/false,
3570 /*class_head_p=*/false,
3572 if (cp_parser_parse_definitely (parser))
3575 /* In "N::S::~S", look in "N" as well. */
3576 if (!done && scope && qualifying_scope)
3578 cp_parser_parse_tentatively (parser);
3579 parser->scope = qualifying_scope;
3580 parser->object_scope = NULL_TREE;
3581 parser->qualifying_scope = NULL_TREE;
3583 = cp_parser_class_name (parser,
3584 /*typename_keyword_p=*/false,
3585 /*template_keyword_p=*/false,
3587 /*check_dependency=*/false,
3588 /*class_head_p=*/false,
3590 if (cp_parser_parse_definitely (parser))
3593 /* In "p->S::~T", look in the scope given by "*p" as well. */
3594 else if (!done && object_scope)
3596 cp_parser_parse_tentatively (parser);
3597 parser->scope = object_scope;
3598 parser->object_scope = NULL_TREE;
3599 parser->qualifying_scope = NULL_TREE;
3601 = cp_parser_class_name (parser,
3602 /*typename_keyword_p=*/false,
3603 /*template_keyword_p=*/false,
3605 /*check_dependency=*/false,
3606 /*class_head_p=*/false,
3608 if (cp_parser_parse_definitely (parser))
3611 /* Look in the surrounding context. */
3614 parser->scope = NULL_TREE;
3615 parser->object_scope = NULL_TREE;
3616 parser->qualifying_scope = NULL_TREE;
3618 = cp_parser_class_name (parser,
3619 /*typename_keyword_p=*/false,
3620 /*template_keyword_p=*/false,
3622 /*check_dependency=*/false,
3623 /*class_head_p=*/false,
3626 /* If an error occurred, assume that the name of the
3627 destructor is the same as the name of the qualifying
3628 class. That allows us to keep parsing after running
3629 into ill-formed destructor names. */
3630 if (type_decl == error_mark_node && scope)
3631 return build_nt (BIT_NOT_EXPR, scope);
3632 else if (type_decl == error_mark_node)
3633 return error_mark_node;
3635 /* Check that destructor name and scope match. */
3636 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3638 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3639 error ("declaration of %<~%T%> as member of %qT",
3641 cp_parser_simulate_error (parser);
3642 return error_mark_node;
3647 A typedef-name that names a class shall not be used as the
3648 identifier in the declarator for a destructor declaration. */
3650 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3651 && !DECL_SELF_REFERENCE_P (type_decl)
3652 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3653 error ("typedef-name %qD used as destructor declarator",
3656 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3660 if (token->keyword == RID_OPERATOR)
3664 /* This could be a template-id, so we try that first. */
3665 cp_parser_parse_tentatively (parser);
3666 /* Try a template-id. */
3667 id = cp_parser_template_id (parser, template_keyword_p,
3668 /*check_dependency_p=*/true,
3670 /* If that worked, we're done. */
3671 if (cp_parser_parse_definitely (parser))
3673 /* We still don't know whether we're looking at an
3674 operator-function-id or a conversion-function-id. */
3675 cp_parser_parse_tentatively (parser);
3676 /* Try an operator-function-id. */
3677 id = cp_parser_operator_function_id (parser);
3678 /* If that didn't work, try a conversion-function-id. */
3679 if (!cp_parser_parse_definitely (parser))
3680 id = cp_parser_conversion_function_id (parser);
3689 cp_parser_error (parser, "expected unqualified-id");
3690 return error_mark_node;
3694 /* Parse an (optional) nested-name-specifier.
3696 nested-name-specifier:
3697 class-or-namespace-name :: nested-name-specifier [opt]
3698 class-or-namespace-name :: template nested-name-specifier [opt]
3700 PARSER->SCOPE should be set appropriately before this function is
3701 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3702 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3705 Sets PARSER->SCOPE to the class (TYPE) or namespace
3706 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3707 it unchanged if there is no nested-name-specifier. Returns the new
3708 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3710 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3711 part of a declaration and/or decl-specifier. */
3714 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3715 bool typename_keyword_p,
3716 bool check_dependency_p,
3718 bool is_declaration)
3720 bool success = false;
3721 cp_token_position start = 0;
3724 /* Remember where the nested-name-specifier starts. */
3725 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3727 start = cp_lexer_token_position (parser->lexer, false);
3728 push_deferring_access_checks (dk_deferred);
3735 tree saved_qualifying_scope;
3736 bool template_keyword_p;
3738 /* Spot cases that cannot be the beginning of a
3739 nested-name-specifier. */
3740 token = cp_lexer_peek_token (parser->lexer);
3742 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3743 the already parsed nested-name-specifier. */
3744 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3746 /* Grab the nested-name-specifier and continue the loop. */
3747 cp_parser_pre_parsed_nested_name_specifier (parser);
3748 /* If we originally encountered this nested-name-specifier
3749 with IS_DECLARATION set to false, we will not have
3750 resolved TYPENAME_TYPEs, so we must do so here. */
3752 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3754 new_scope = resolve_typename_type (parser->scope,
3755 /*only_current_p=*/false);
3756 if (new_scope != error_mark_node)
3757 parser->scope = new_scope;
3763 /* Spot cases that cannot be the beginning of a
3764 nested-name-specifier. On the second and subsequent times
3765 through the loop, we look for the `template' keyword. */
3766 if (success && token->keyword == RID_TEMPLATE)
3768 /* A template-id can start a nested-name-specifier. */
3769 else if (token->type == CPP_TEMPLATE_ID)
3773 /* If the next token is not an identifier, then it is
3774 definitely not a class-or-namespace-name. */
3775 if (token->type != CPP_NAME)
3777 /* If the following token is neither a `<' (to begin a
3778 template-id), nor a `::', then we are not looking at a
3779 nested-name-specifier. */
3780 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3781 if (token->type != CPP_SCOPE
3782 && !cp_parser_nth_token_starts_template_argument_list_p
3787 /* The nested-name-specifier is optional, so we parse
3789 cp_parser_parse_tentatively (parser);
3791 /* Look for the optional `template' keyword, if this isn't the
3792 first time through the loop. */
3794 template_keyword_p = cp_parser_optional_template_keyword (parser);
3796 template_keyword_p = false;
3798 /* Save the old scope since the name lookup we are about to do
3799 might destroy it. */
3800 old_scope = parser->scope;
3801 saved_qualifying_scope = parser->qualifying_scope;
3802 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3803 look up names in "X<T>::I" in order to determine that "Y" is
3804 a template. So, if we have a typename at this point, we make
3805 an effort to look through it. */
3807 && !typename_keyword_p
3809 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3810 parser->scope = resolve_typename_type (parser->scope,
3811 /*only_current_p=*/false);
3812 /* Parse the qualifying entity. */
3814 = cp_parser_class_or_namespace_name (parser,
3820 /* Look for the `::' token. */
3821 cp_parser_require (parser, CPP_SCOPE, "`::'");
3823 /* If we found what we wanted, we keep going; otherwise, we're
3825 if (!cp_parser_parse_definitely (parser))
3827 bool error_p = false;
3829 /* Restore the OLD_SCOPE since it was valid before the
3830 failed attempt at finding the last
3831 class-or-namespace-name. */
3832 parser->scope = old_scope;
3833 parser->qualifying_scope = saved_qualifying_scope;
3834 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3836 /* If the next token is an identifier, and the one after
3837 that is a `::', then any valid interpretation would have
3838 found a class-or-namespace-name. */
3839 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3840 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3842 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3845 token = cp_lexer_consume_token (parser->lexer);
3848 if (!token->ambiguous_p)
3851 tree ambiguous_decls;
3853 decl = cp_parser_lookup_name (parser, token->u.value,
3855 /*is_template=*/false,
3856 /*is_namespace=*/false,
3857 /*check_dependency=*/true,
3859 if (TREE_CODE (decl) == TEMPLATE_DECL)
3860 error ("%qD used without template parameters", decl);
3861 else if (ambiguous_decls)
3863 error ("reference to %qD is ambiguous",
3865 print_candidates (ambiguous_decls);
3866 decl = error_mark_node;
3869 cp_parser_name_lookup_error
3870 (parser, token->u.value, decl,
3871 "is not a class or namespace");
3873 parser->scope = error_mark_node;
3875 /* Treat this as a successful nested-name-specifier
3880 If the name found is not a class-name (clause
3881 _class_) or namespace-name (_namespace.def_), the
3882 program is ill-formed. */
3885 cp_lexer_consume_token (parser->lexer);
3889 /* We've found one valid nested-name-specifier. */
3891 /* Name lookup always gives us a DECL. */
3892 if (TREE_CODE (new_scope) == TYPE_DECL)
3893 new_scope = TREE_TYPE (new_scope);
3894 /* Uses of "template" must be followed by actual templates. */
3895 if (template_keyword_p
3896 && !(CLASS_TYPE_P (new_scope)
3897 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3898 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3899 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3900 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3901 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3902 == TEMPLATE_ID_EXPR)))
3903 pedwarn (TYPE_P (new_scope)
3904 ? "%qT is not a template"
3905 : "%qD is not a template",
3907 /* If it is a class scope, try to complete it; we are about to
3908 be looking up names inside the class. */
3909 if (TYPE_P (new_scope)
3910 /* Since checking types for dependency can be expensive,
3911 avoid doing it if the type is already complete. */
3912 && !COMPLETE_TYPE_P (new_scope)
3913 /* Do not try to complete dependent types. */
3914 && !dependent_type_p (new_scope))
3915 new_scope = complete_type (new_scope);
3916 /* Make sure we look in the right scope the next time through
3918 parser->scope = new_scope;
3921 /* If parsing tentatively, replace the sequence of tokens that makes
3922 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3923 token. That way, should we re-parse the token stream, we will
3924 not have to repeat the effort required to do the parse, nor will
3925 we issue duplicate error messages. */
3926 if (success && start)
3930 token = cp_lexer_token_at (parser->lexer, start);
3931 /* Reset the contents of the START token. */
3932 token->type = CPP_NESTED_NAME_SPECIFIER;
3933 /* Retrieve any deferred checks. Do not pop this access checks yet
3934 so the memory will not be reclaimed during token replacing below. */
3935 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3936 token->u.tree_check_value->value = parser->scope;
3937 token->u.tree_check_value->checks = get_deferred_access_checks ();
3938 token->u.tree_check_value->qualifying_scope =
3939 parser->qualifying_scope;
3940 token->keyword = RID_MAX;
3942 /* Purge all subsequent tokens. */
3943 cp_lexer_purge_tokens_after (parser->lexer, start);
3947 pop_to_parent_deferring_access_checks ();
3949 return success ? parser->scope : NULL_TREE;
3952 /* Parse a nested-name-specifier. See
3953 cp_parser_nested_name_specifier_opt for details. This function
3954 behaves identically, except that it will an issue an error if no
3955 nested-name-specifier is present. */
3958 cp_parser_nested_name_specifier (cp_parser *parser,
3959 bool typename_keyword_p,
3960 bool check_dependency_p,
3962 bool is_declaration)
3966 /* Look for the nested-name-specifier. */
3967 scope = cp_parser_nested_name_specifier_opt (parser,
3972 /* If it was not present, issue an error message. */
3975 cp_parser_error (parser, "expected nested-name-specifier");
3976 parser->scope = NULL_TREE;
3982 /* Parse a class-or-namespace-name.
3984 class-or-namespace-name:
3988 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3989 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3990 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3991 TYPE_P is TRUE iff the next name should be taken as a class-name,
3992 even the same name is declared to be another entity in the same
3995 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3996 specified by the class-or-namespace-name. If neither is found the
3997 ERROR_MARK_NODE is returned. */
4000 cp_parser_class_or_namespace_name (cp_parser *parser,
4001 bool typename_keyword_p,
4002 bool template_keyword_p,
4003 bool check_dependency_p,
4005 bool is_declaration)
4008 tree saved_qualifying_scope;
4009 tree saved_object_scope;
4013 /* Before we try to parse the class-name, we must save away the
4014 current PARSER->SCOPE since cp_parser_class_name will destroy
4016 saved_scope = parser->scope;
4017 saved_qualifying_scope = parser->qualifying_scope;
4018 saved_object_scope = parser->object_scope;
4019 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4020 there is no need to look for a namespace-name. */
4021 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4023 cp_parser_parse_tentatively (parser);
4024 scope = cp_parser_class_name (parser,
4027 type_p ? class_type : none_type,
4029 /*class_head_p=*/false,
4031 /* If that didn't work, try for a namespace-name. */
4032 if (!only_class_p && !cp_parser_parse_definitely (parser))
4034 /* Restore the saved scope. */
4035 parser->scope = saved_scope;
4036 parser->qualifying_scope = saved_qualifying_scope;
4037 parser->object_scope = saved_object_scope;
4038 /* If we are not looking at an identifier followed by the scope
4039 resolution operator, then this is not part of a
4040 nested-name-specifier. (Note that this function is only used
4041 to parse the components of a nested-name-specifier.) */
4042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4043 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4044 return error_mark_node;
4045 scope = cp_parser_namespace_name (parser);
4051 /* Parse a postfix-expression.
4055 postfix-expression [ expression ]
4056 postfix-expression ( expression-list [opt] )
4057 simple-type-specifier ( expression-list [opt] )
4058 typename :: [opt] nested-name-specifier identifier
4059 ( expression-list [opt] )
4060 typename :: [opt] nested-name-specifier template [opt] template-id
4061 ( expression-list [opt] )
4062 postfix-expression . template [opt] id-expression
4063 postfix-expression -> template [opt] id-expression
4064 postfix-expression . pseudo-destructor-name
4065 postfix-expression -> pseudo-destructor-name
4066 postfix-expression ++
4067 postfix-expression --
4068 dynamic_cast < type-id > ( expression )
4069 static_cast < type-id > ( expression )
4070 reinterpret_cast < type-id > ( expression )
4071 const_cast < type-id > ( expression )
4072 typeid ( expression )
4078 ( type-id ) { initializer-list , [opt] }
4080 This extension is a GNU version of the C99 compound-literal
4081 construct. (The C99 grammar uses `type-name' instead of `type-id',
4082 but they are essentially the same concept.)
4084 If ADDRESS_P is true, the postfix expression is the operand of the
4085 `&' operator. CAST_P is true if this expression is the target of a
4088 Returns a representation of the expression. */
4091 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4095 cp_id_kind idk = CP_ID_KIND_NONE;
4096 tree postfix_expression = NULL_TREE;
4098 /* Peek at the next token. */
4099 token = cp_lexer_peek_token (parser->lexer);
4100 /* Some of the productions are determined by keywords. */
4101 keyword = token->keyword;
4111 const char *saved_message;
4113 /* All of these can be handled in the same way from the point
4114 of view of parsing. Begin by consuming the token
4115 identifying the cast. */
4116 cp_lexer_consume_token (parser->lexer);
4118 /* New types cannot be defined in the cast. */
4119 saved_message = parser->type_definition_forbidden_message;
4120 parser->type_definition_forbidden_message
4121 = "types may not be defined in casts";
4123 /* Look for the opening `<'. */
4124 cp_parser_require (parser, CPP_LESS, "`<'");
4125 /* Parse the type to which we are casting. */
4126 type = cp_parser_type_id (parser);
4127 /* Look for the closing `>'. */
4128 cp_parser_require (parser, CPP_GREATER, "`>'");
4129 /* Restore the old message. */
4130 parser->type_definition_forbidden_message = saved_message;
4132 /* And the expression which is being cast. */
4133 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4134 expression = cp_parser_expression (parser, /*cast_p=*/true);
4135 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4137 /* Only type conversions to integral or enumeration types
4138 can be used in constant-expressions. */
4139 if (!cast_valid_in_integral_constant_expression_p (type)
4140 && (cp_parser_non_integral_constant_expression
4142 "a cast to a type other than an integral or "
4143 "enumeration type")))
4144 return error_mark_node;
4150 = build_dynamic_cast (type, expression);
4154 = build_static_cast (type, expression);
4158 = build_reinterpret_cast (type, expression);
4162 = build_const_cast (type, expression);
4173 const char *saved_message;
4174 bool saved_in_type_id_in_expr_p;
4176 /* Consume the `typeid' token. */
4177 cp_lexer_consume_token (parser->lexer);
4178 /* Look for the `(' token. */
4179 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4180 /* Types cannot be defined in a `typeid' expression. */
4181 saved_message = parser->type_definition_forbidden_message;
4182 parser->type_definition_forbidden_message
4183 = "types may not be defined in a `typeid\' expression";
4184 /* We can't be sure yet whether we're looking at a type-id or an
4186 cp_parser_parse_tentatively (parser);
4187 /* Try a type-id first. */
4188 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4189 parser->in_type_id_in_expr_p = true;
4190 type = cp_parser_type_id (parser);
4191 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4192 /* Look for the `)' token. Otherwise, we can't be sure that
4193 we're not looking at an expression: consider `typeid (int
4194 (3))', for example. */
4195 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4196 /* If all went well, simply lookup the type-id. */
4197 if (cp_parser_parse_definitely (parser))
4198 postfix_expression = get_typeid (type);
4199 /* Otherwise, fall back to the expression variant. */
4204 /* Look for an expression. */
4205 expression = cp_parser_expression (parser, /*cast_p=*/false);
4206 /* Compute its typeid. */
4207 postfix_expression = build_typeid (expression);
4208 /* Look for the `)' token. */
4209 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4211 /* Restore the saved message. */
4212 parser->type_definition_forbidden_message = saved_message;
4213 /* `typeid' may not appear in an integral constant expression. */
4214 if (cp_parser_non_integral_constant_expression(parser,
4215 "`typeid' operator"))
4216 return error_mark_node;
4223 /* The syntax permitted here is the same permitted for an
4224 elaborated-type-specifier. */
4225 type = cp_parser_elaborated_type_specifier (parser,
4226 /*is_friend=*/false,
4227 /*is_declaration=*/false);
4228 postfix_expression = cp_parser_functional_cast (parser, type);
4236 /* If the next thing is a simple-type-specifier, we may be
4237 looking at a functional cast. We could also be looking at
4238 an id-expression. So, we try the functional cast, and if
4239 that doesn't work we fall back to the primary-expression. */
4240 cp_parser_parse_tentatively (parser);
4241 /* Look for the simple-type-specifier. */
4242 type = cp_parser_simple_type_specifier (parser,
4243 /*decl_specs=*/NULL,
4244 CP_PARSER_FLAGS_NONE);
4245 /* Parse the cast itself. */
4246 if (!cp_parser_error_occurred (parser))
4248 = cp_parser_functional_cast (parser, type);
4249 /* If that worked, we're done. */
4250 if (cp_parser_parse_definitely (parser))
4253 /* If the functional-cast didn't work out, try a
4254 compound-literal. */
4255 if (cp_parser_allow_gnu_extensions_p (parser)
4256 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4258 VEC(constructor_elt,gc) *initializer_list = NULL;
4259 bool saved_in_type_id_in_expr_p;
4261 cp_parser_parse_tentatively (parser);
4262 /* Consume the `('. */
4263 cp_lexer_consume_token (parser->lexer);
4264 /* Parse the type. */
4265 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4266 parser->in_type_id_in_expr_p = true;
4267 type = cp_parser_type_id (parser);
4268 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4269 /* Look for the `)'. */
4270 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4271 /* Look for the `{'. */
4272 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4273 /* If things aren't going well, there's no need to
4275 if (!cp_parser_error_occurred (parser))
4277 bool non_constant_p;
4278 /* Parse the initializer-list. */
4280 = cp_parser_initializer_list (parser, &non_constant_p);
4281 /* Allow a trailing `,'. */
4282 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4283 cp_lexer_consume_token (parser->lexer);
4284 /* Look for the final `}'. */
4285 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4287 /* If that worked, we're definitely looking at a
4288 compound-literal expression. */
4289 if (cp_parser_parse_definitely (parser))
4291 /* Warn the user that a compound literal is not
4292 allowed in standard C++. */
4294 pedwarn ("ISO C++ forbids compound-literals");
4295 /* For simplicitly, we disallow compound literals in
4296 constant-expressions for simpliicitly. We could
4297 allow compound literals of integer type, whose
4298 initializer was a constant, in constant
4299 expressions. Permitting that usage, as a further
4300 extension, would not change the meaning of any
4301 currently accepted programs. (Of course, as
4302 compound literals are not part of ISO C++, the
4303 standard has nothing to say.) */
4304 if (cp_parser_non_integral_constant_expression
4305 (parser, "non-constant compound literals"))
4307 postfix_expression = error_mark_node;
4310 /* Form the representation of the compound-literal. */
4312 = finish_compound_literal (type, initializer_list);
4317 /* It must be a primary-expression. */
4319 = cp_parser_primary_expression (parser, address_p, cast_p,
4320 /*template_arg_p=*/false,
4326 /* Keep looping until the postfix-expression is complete. */
4329 if (idk == CP_ID_KIND_UNQUALIFIED
4330 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4331 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4332 /* It is not a Koenig lookup function call. */
4334 = unqualified_name_lookup_error (postfix_expression);
4336 /* Peek at the next token. */
4337 token = cp_lexer_peek_token (parser->lexer);
4339 switch (token->type)
4341 case CPP_OPEN_SQUARE:
4343 = cp_parser_postfix_open_square_expression (parser,
4346 idk = CP_ID_KIND_NONE;
4349 case CPP_OPEN_PAREN:
4350 /* postfix-expression ( expression-list [opt] ) */
4353 bool is_builtin_constant_p;
4354 bool saved_integral_constant_expression_p = false;
4355 bool saved_non_integral_constant_expression_p = false;
4358 is_builtin_constant_p
4359 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4360 if (is_builtin_constant_p)
4362 /* The whole point of __builtin_constant_p is to allow
4363 non-constant expressions to appear as arguments. */
4364 saved_integral_constant_expression_p
4365 = parser->integral_constant_expression_p;
4366 saved_non_integral_constant_expression_p
4367 = parser->non_integral_constant_expression_p;
4368 parser->integral_constant_expression_p = false;
4370 args = (cp_parser_parenthesized_expression_list
4371 (parser, /*is_attribute_list=*/false,
4373 /*non_constant_p=*/NULL));
4374 if (is_builtin_constant_p)
4376 parser->integral_constant_expression_p
4377 = saved_integral_constant_expression_p;
4378 parser->non_integral_constant_expression_p
4379 = saved_non_integral_constant_expression_p;
4382 if (args == error_mark_node)
4384 postfix_expression = error_mark_node;
4388 /* Function calls are not permitted in
4389 constant-expressions. */
4390 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4391 && cp_parser_non_integral_constant_expression (parser,
4394 postfix_expression = error_mark_node;
4399 if (idk == CP_ID_KIND_UNQUALIFIED)
4401 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4407 = perform_koenig_lookup (postfix_expression, args);
4411 = unqualified_fn_lookup_error (postfix_expression);
4413 /* We do not perform argument-dependent lookup if
4414 normal lookup finds a non-function, in accordance
4415 with the expected resolution of DR 218. */
4416 else if (args && is_overloaded_fn (postfix_expression))
4418 tree fn = get_first_fn (postfix_expression);
4420 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4421 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4423 /* Only do argument dependent lookup if regular
4424 lookup does not find a set of member functions.
4425 [basic.lookup.koenig]/2a */
4426 if (!DECL_FUNCTION_MEMBER_P (fn))
4430 = perform_koenig_lookup (postfix_expression, args);
4435 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4437 tree instance = TREE_OPERAND (postfix_expression, 0);
4438 tree fn = TREE_OPERAND (postfix_expression, 1);
4440 if (processing_template_decl
4441 && (type_dependent_expression_p (instance)
4442 || (!BASELINK_P (fn)
4443 && TREE_CODE (fn) != FIELD_DECL)
4444 || type_dependent_expression_p (fn)
4445 || any_type_dependent_arguments_p (args)))
4448 = build_min_nt (CALL_EXPR, postfix_expression,
4453 if (BASELINK_P (fn))
4455 = (build_new_method_call
4456 (instance, fn, args, NULL_TREE,
4457 (idk == CP_ID_KIND_QUALIFIED
4458 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4462 = finish_call_expr (postfix_expression, args,
4463 /*disallow_virtual=*/false,
4464 /*koenig_p=*/false);
4466 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4467 || TREE_CODE (postfix_expression) == MEMBER_REF
4468 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4469 postfix_expression = (build_offset_ref_call_from_tree
4470 (postfix_expression, args));
4471 else if (idk == CP_ID_KIND_QUALIFIED)
4472 /* A call to a static class member, or a namespace-scope
4475 = finish_call_expr (postfix_expression, args,
4476 /*disallow_virtual=*/true,
4479 /* All other function calls. */
4481 = finish_call_expr (postfix_expression, args,
4482 /*disallow_virtual=*/false,
4485 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4486 idk = CP_ID_KIND_NONE;
4492 /* postfix-expression . template [opt] id-expression
4493 postfix-expression . pseudo-destructor-name
4494 postfix-expression -> template [opt] id-expression
4495 postfix-expression -> pseudo-destructor-name */
4497 /* Consume the `.' or `->' operator. */
4498 cp_lexer_consume_token (parser->lexer);
4501 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4507 /* postfix-expression ++ */
4508 /* Consume the `++' token. */
4509 cp_lexer_consume_token (parser->lexer);
4510 /* Generate a representation for the complete expression. */
4512 = finish_increment_expr (postfix_expression,
4513 POSTINCREMENT_EXPR);
4514 /* Increments may not appear in constant-expressions. */
4515 if (cp_parser_non_integral_constant_expression (parser,
4517 postfix_expression = error_mark_node;
4518 idk = CP_ID_KIND_NONE;
4521 case CPP_MINUS_MINUS:
4522 /* postfix-expression -- */
4523 /* Consume the `--' token. */
4524 cp_lexer_consume_token (parser->lexer);
4525 /* Generate a representation for the complete expression. */
4527 = finish_increment_expr (postfix_expression,
4528 POSTDECREMENT_EXPR);
4529 /* Decrements may not appear in constant-expressions. */
4530 if (cp_parser_non_integral_constant_expression (parser,
4532 postfix_expression = error_mark_node;
4533 idk = CP_ID_KIND_NONE;
4537 return postfix_expression;
4541 /* We should never get here. */
4543 return error_mark_node;
4546 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4547 by cp_parser_builtin_offsetof. We're looking for
4549 postfix-expression [ expression ]
4551 FOR_OFFSETOF is set if we're being called in that context, which
4552 changes how we deal with integer constant expressions. */
4555 cp_parser_postfix_open_square_expression (cp_parser *parser,
4556 tree postfix_expression,
4561 /* Consume the `[' token. */
4562 cp_lexer_consume_token (parser->lexer);
4564 /* Parse the index expression. */
4565 /* ??? For offsetof, there is a question of what to allow here. If
4566 offsetof is not being used in an integral constant expression context,
4567 then we *could* get the right answer by computing the value at runtime.
4568 If we are in an integral constant expression context, then we might
4569 could accept any constant expression; hard to say without analysis.
4570 Rather than open the barn door too wide right away, allow only integer
4571 constant expressions here. */
4573 index = cp_parser_constant_expression (parser, false, NULL);
4575 index = cp_parser_expression (parser, /*cast_p=*/false);
4577 /* Look for the closing `]'. */
4578 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4580 /* Build the ARRAY_REF. */
4581 postfix_expression = grok_array_decl (postfix_expression, index);
4583 /* When not doing offsetof, array references are not permitted in
4584 constant-expressions. */
4586 && (cp_parser_non_integral_constant_expression
4587 (parser, "an array reference")))
4588 postfix_expression = error_mark_node;
4590 return postfix_expression;
4593 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4594 by cp_parser_builtin_offsetof. We're looking for
4596 postfix-expression . template [opt] id-expression
4597 postfix-expression . pseudo-destructor-name
4598 postfix-expression -> template [opt] id-expression
4599 postfix-expression -> pseudo-destructor-name
4601 FOR_OFFSETOF is set if we're being called in that context. That sorta
4602 limits what of the above we'll actually accept, but nevermind.
4603 TOKEN_TYPE is the "." or "->" token, which will already have been
4604 removed from the stream. */
4607 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4608 enum cpp_ttype token_type,
4609 tree postfix_expression,
4610 bool for_offsetof, cp_id_kind *idk)
4614 bool pseudo_destructor_p;
4615 tree scope = NULL_TREE;
4617 /* If this is a `->' operator, dereference the pointer. */
4618 if (token_type == CPP_DEREF)
4619 postfix_expression = build_x_arrow (postfix_expression);
4620 /* Check to see whether or not the expression is type-dependent. */
4621 dependent_p = type_dependent_expression_p (postfix_expression);
4622 /* The identifier following the `->' or `.' is not qualified. */
4623 parser->scope = NULL_TREE;
4624 parser->qualifying_scope = NULL_TREE;
4625 parser->object_scope = NULL_TREE;
4626 *idk = CP_ID_KIND_NONE;
4627 /* Enter the scope corresponding to the type of the object
4628 given by the POSTFIX_EXPRESSION. */
4629 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4631 scope = TREE_TYPE (postfix_expression);
4632 /* According to the standard, no expression should ever have
4633 reference type. Unfortunately, we do not currently match
4634 the standard in this respect in that our internal representation
4635 of an expression may have reference type even when the standard
4636 says it does not. Therefore, we have to manually obtain the
4637 underlying type here. */
4638 scope = non_reference (scope);
4639 /* The type of the POSTFIX_EXPRESSION must be complete. */
4640 if (scope == unknown_type_node)
4642 error ("%qE does not have class type", postfix_expression);
4646 scope = complete_type_or_else (scope, NULL_TREE);
4647 /* Let the name lookup machinery know that we are processing a
4648 class member access expression. */
4649 parser->context->object_type = scope;
4650 /* If something went wrong, we want to be able to discern that case,
4651 as opposed to the case where there was no SCOPE due to the type
4652 of expression being dependent. */
4654 scope = error_mark_node;
4655 /* If the SCOPE was erroneous, make the various semantic analysis
4656 functions exit quickly -- and without issuing additional error
4658 if (scope == error_mark_node)
4659 postfix_expression = error_mark_node;
4662 /* Assume this expression is not a pseudo-destructor access. */
4663 pseudo_destructor_p = false;
4665 /* If the SCOPE is a scalar type, then, if this is a valid program,
4666 we must be looking at a pseudo-destructor-name. */
4667 if (scope && SCALAR_TYPE_P (scope))
4672 cp_parser_parse_tentatively (parser);
4673 /* Parse the pseudo-destructor-name. */
4675 cp_parser_pseudo_destructor_name (parser, &s, &type);
4676 if (cp_parser_parse_definitely (parser))
4678 pseudo_destructor_p = true;
4680 = finish_pseudo_destructor_expr (postfix_expression,
4681 s, TREE_TYPE (type));
4685 if (!pseudo_destructor_p)
4687 /* If the SCOPE is not a scalar type, we are looking at an
4688 ordinary class member access expression, rather than a
4689 pseudo-destructor-name. */
4691 /* Parse the id-expression. */
4692 name = (cp_parser_id_expression
4694 cp_parser_optional_template_keyword (parser),
4695 /*check_dependency_p=*/true,
4697 /*declarator_p=*/false,
4698 /*optional_p=*/false));
4699 /* In general, build a SCOPE_REF if the member name is qualified.
4700 However, if the name was not dependent and has already been
4701 resolved; there is no need to build the SCOPE_REF. For example;
4703 struct X { void f(); };
4704 template <typename T> void f(T* t) { t->X::f(); }
4706 Even though "t" is dependent, "X::f" is not and has been resolved
4707 to a BASELINK; there is no need to include scope information. */
4709 /* But we do need to remember that there was an explicit scope for
4710 virtual function calls. */
4712 *idk = CP_ID_KIND_QUALIFIED;
4714 /* If the name is a template-id that names a type, we will get a
4715 TYPE_DECL here. That is invalid code. */
4716 if (TREE_CODE (name) == TYPE_DECL)
4718 error ("invalid use of %qD", name);
4719 postfix_expression = error_mark_node;
4723 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4725 name = build_qualified_name (/*type=*/NULL_TREE,
4729 parser->scope = NULL_TREE;
4730 parser->qualifying_scope = NULL_TREE;
4731 parser->object_scope = NULL_TREE;
4733 if (scope && name && BASELINK_P (name))
4734 adjust_result_of_qualified_name_lookup
4735 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4737 = finish_class_member_access_expr (postfix_expression, name,
4742 /* We no longer need to look up names in the scope of the object on
4743 the left-hand side of the `.' or `->' operator. */
4744 parser->context->object_type = NULL_TREE;
4746 /* Outside of offsetof, these operators may not appear in
4747 constant-expressions. */
4749 && (cp_parser_non_integral_constant_expression
4750 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4751 postfix_expression = error_mark_node;
4753 return postfix_expression;
4756 /* Parse a parenthesized expression-list.
4759 assignment-expression
4760 expression-list, assignment-expression
4765 identifier, expression-list
4767 CAST_P is true if this expression is the target of a cast.
4769 Returns a TREE_LIST. The TREE_VALUE of each node is a
4770 representation of an assignment-expression. Note that a TREE_LIST
4771 is returned even if there is only a single expression in the list.
4772 error_mark_node is returned if the ( and or ) are
4773 missing. NULL_TREE is returned on no expressions. The parentheses
4774 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4775 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4776 indicates whether or not all of the expressions in the list were
4780 cp_parser_parenthesized_expression_list (cp_parser* parser,
4781 bool is_attribute_list,
4783 bool *non_constant_p)
4785 tree expression_list = NULL_TREE;
4786 bool fold_expr_p = is_attribute_list;
4787 tree identifier = NULL_TREE;
4789 /* Assume all the expressions will be constant. */
4791 *non_constant_p = false;
4793 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4794 return error_mark_node;
4796 /* Consume expressions until there are no more. */
4797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4802 /* At the beginning of attribute lists, check to see if the
4803 next token is an identifier. */
4804 if (is_attribute_list
4805 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4809 /* Consume the identifier. */
4810 token = cp_lexer_consume_token (parser->lexer);
4811 /* Save the identifier. */
4812 identifier = token->u.value;
4816 /* Parse the next assignment-expression. */
4819 bool expr_non_constant_p;
4820 expr = (cp_parser_constant_expression
4821 (parser, /*allow_non_constant_p=*/true,
4822 &expr_non_constant_p));
4823 if (expr_non_constant_p)
4824 *non_constant_p = true;
4827 expr = cp_parser_assignment_expression (parser, cast_p);
4830 expr = fold_non_dependent_expr (expr);
4832 /* Add it to the list. We add error_mark_node
4833 expressions to the list, so that we can still tell if
4834 the correct form for a parenthesized expression-list
4835 is found. That gives better errors. */
4836 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4838 if (expr == error_mark_node)
4842 /* After the first item, attribute lists look the same as
4843 expression lists. */
4844 is_attribute_list = false;
4847 /* If the next token isn't a `,', then we are done. */
4848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4851 /* Otherwise, consume the `,' and keep going. */
4852 cp_lexer_consume_token (parser->lexer);
4855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4860 /* We try and resync to an unnested comma, as that will give the
4861 user better diagnostics. */
4862 ending = cp_parser_skip_to_closing_parenthesis (parser,
4863 /*recovering=*/true,
4865 /*consume_paren=*/true);
4869 return error_mark_node;
4872 /* We built up the list in reverse order so we must reverse it now. */
4873 expression_list = nreverse (expression_list);
4875 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4877 return expression_list;
4880 /* Parse a pseudo-destructor-name.
4882 pseudo-destructor-name:
4883 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4884 :: [opt] nested-name-specifier template template-id :: ~ type-name
4885 :: [opt] nested-name-specifier [opt] ~ type-name
4887 If either of the first two productions is used, sets *SCOPE to the
4888 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4889 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4890 or ERROR_MARK_NODE if the parse fails. */
4893 cp_parser_pseudo_destructor_name (cp_parser* parser,
4897 bool nested_name_specifier_p;
4899 /* Assume that things will not work out. */
4900 *type = error_mark_node;
4902 /* Look for the optional `::' operator. */
4903 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4904 /* Look for the optional nested-name-specifier. */
4905 nested_name_specifier_p
4906 = (cp_parser_nested_name_specifier_opt (parser,
4907 /*typename_keyword_p=*/false,
4908 /*check_dependency_p=*/true,
4910 /*is_declaration=*/true)
4912 /* Now, if we saw a nested-name-specifier, we might be doing the
4913 second production. */
4914 if (nested_name_specifier_p
4915 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4917 /* Consume the `template' keyword. */
4918 cp_lexer_consume_token (parser->lexer);
4919 /* Parse the template-id. */
4920 cp_parser_template_id (parser,
4921 /*template_keyword_p=*/true,
4922 /*check_dependency_p=*/false,
4923 /*is_declaration=*/true);
4924 /* Look for the `::' token. */
4925 cp_parser_require (parser, CPP_SCOPE, "`::'");
4927 /* If the next token is not a `~', then there might be some
4928 additional qualification. */
4929 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4931 /* Look for the type-name. */
4932 *scope = TREE_TYPE (cp_parser_type_name (parser));
4934 if (*scope == error_mark_node)
4937 /* If we don't have ::~, then something has gone wrong. Since
4938 the only caller of this function is looking for something
4939 after `.' or `->' after a scalar type, most likely the
4940 program is trying to get a member of a non-aggregate
4942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4943 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4945 cp_parser_error (parser, "request for member of non-aggregate type");
4949 /* Look for the `::' token. */
4950 cp_parser_require (parser, CPP_SCOPE, "`::'");
4955 /* Look for the `~'. */
4956 cp_parser_require (parser, CPP_COMPL, "`~'");
4957 /* Look for the type-name again. We are not responsible for
4958 checking that it matches the first type-name. */
4959 *type = cp_parser_type_name (parser);
4962 /* Parse a unary-expression.
4968 unary-operator cast-expression
4969 sizeof unary-expression
4977 __extension__ cast-expression
4978 __alignof__ unary-expression
4979 __alignof__ ( type-id )
4980 __real__ cast-expression
4981 __imag__ cast-expression
4984 ADDRESS_P is true iff the unary-expression is appearing as the
4985 operand of the `&' operator. CAST_P is true if this expression is
4986 the target of a cast.
4988 Returns a representation of the expression. */
4991 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4994 enum tree_code unary_operator;
4996 /* Peek at the next token. */
4997 token = cp_lexer_peek_token (parser->lexer);
4998 /* Some keywords give away the kind of expression. */
4999 if (token->type == CPP_KEYWORD)
5001 enum rid keyword = token->keyword;
5011 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5012 /* Consume the token. */
5013 cp_lexer_consume_token (parser->lexer);
5014 /* Parse the operand. */
5015 operand = cp_parser_sizeof_operand (parser, keyword);
5017 if (TYPE_P (operand))
5018 return cxx_sizeof_or_alignof_type (operand, op, true);
5020 return cxx_sizeof_or_alignof_expr (operand, op);
5024 return cp_parser_new_expression (parser);
5027 return cp_parser_delete_expression (parser);
5031 /* The saved value of the PEDANTIC flag. */
5035 /* Save away the PEDANTIC flag. */
5036 cp_parser_extension_opt (parser, &saved_pedantic);
5037 /* Parse the cast-expression. */
5038 expr = cp_parser_simple_cast_expression (parser);
5039 /* Restore the PEDANTIC flag. */
5040 pedantic = saved_pedantic;
5050 /* Consume the `__real__' or `__imag__' token. */
5051 cp_lexer_consume_token (parser->lexer);
5052 /* Parse the cast-expression. */
5053 expression = cp_parser_simple_cast_expression (parser);
5054 /* Create the complete representation. */
5055 return build_x_unary_op ((keyword == RID_REALPART
5056 ? REALPART_EXPR : IMAGPART_EXPR),
5066 /* Look for the `:: new' and `:: delete', which also signal the
5067 beginning of a new-expression, or delete-expression,
5068 respectively. If the next token is `::', then it might be one of
5070 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5074 /* See if the token after the `::' is one of the keywords in
5075 which we're interested. */
5076 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5077 /* If it's `new', we have a new-expression. */
5078 if (keyword == RID_NEW)
5079 return cp_parser_new_expression (parser);
5080 /* Similarly, for `delete'. */
5081 else if (keyword == RID_DELETE)
5082 return cp_parser_delete_expression (parser);
5085 /* Look for a unary operator. */
5086 unary_operator = cp_parser_unary_operator (token);
5087 /* The `++' and `--' operators can be handled similarly, even though
5088 they are not technically unary-operators in the grammar. */
5089 if (unary_operator == ERROR_MARK)
5091 if (token->type == CPP_PLUS_PLUS)
5092 unary_operator = PREINCREMENT_EXPR;
5093 else if (token->type == CPP_MINUS_MINUS)
5094 unary_operator = PREDECREMENT_EXPR;
5095 /* Handle the GNU address-of-label extension. */
5096 else if (cp_parser_allow_gnu_extensions_p (parser)
5097 && token->type == CPP_AND_AND)
5101 /* Consume the '&&' token. */
5102 cp_lexer_consume_token (parser->lexer);
5103 /* Look for the identifier. */
5104 identifier = cp_parser_identifier (parser);
5105 /* Create an expression representing the address. */
5106 return finish_label_address_expr (identifier);
5109 if (unary_operator != ERROR_MARK)
5111 tree cast_expression;
5112 tree expression = error_mark_node;
5113 const char *non_constant_p = NULL;
5115 /* Consume the operator token. */
5116 token = cp_lexer_consume_token (parser->lexer);
5117 /* Parse the cast-expression. */
5119 = cp_parser_cast_expression (parser,
5120 unary_operator == ADDR_EXPR,
5122 /* Now, build an appropriate representation. */
5123 switch (unary_operator)
5126 non_constant_p = "`*'";
5127 expression = build_x_indirect_ref (cast_expression, "unary *");
5131 non_constant_p = "`&'";
5134 expression = build_x_unary_op (unary_operator, cast_expression);
5137 case PREINCREMENT_EXPR:
5138 case PREDECREMENT_EXPR:
5139 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5142 case UNARY_PLUS_EXPR:
5144 case TRUTH_NOT_EXPR:
5145 expression = finish_unary_op_expr (unary_operator, cast_expression);
5153 && cp_parser_non_integral_constant_expression (parser,
5155 expression = error_mark_node;
5160 return cp_parser_postfix_expression (parser, address_p, cast_p);
5163 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5164 unary-operator, the corresponding tree code is returned. */
5166 static enum tree_code
5167 cp_parser_unary_operator (cp_token* token)
5169 switch (token->type)
5172 return INDIRECT_REF;
5178 return UNARY_PLUS_EXPR;
5184 return TRUTH_NOT_EXPR;
5187 return BIT_NOT_EXPR;
5194 /* Parse a new-expression.
5197 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5198 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5200 Returns a representation of the expression. */
5203 cp_parser_new_expression (cp_parser* parser)
5205 bool global_scope_p;
5211 /* Look for the optional `::' operator. */
5213 = (cp_parser_global_scope_opt (parser,
5214 /*current_scope_valid_p=*/false)
5216 /* Look for the `new' operator. */
5217 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5218 /* There's no easy way to tell a new-placement from the
5219 `( type-id )' construct. */
5220 cp_parser_parse_tentatively (parser);
5221 /* Look for a new-placement. */
5222 placement = cp_parser_new_placement (parser);
5223 /* If that didn't work out, there's no new-placement. */
5224 if (!cp_parser_parse_definitely (parser))
5225 placement = NULL_TREE;
5227 /* If the next token is a `(', then we have a parenthesized
5229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5231 /* Consume the `('. */
5232 cp_lexer_consume_token (parser->lexer);
5233 /* Parse the type-id. */
5234 type = cp_parser_type_id (parser);
5235 /* Look for the closing `)'. */
5236 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5237 /* There should not be a direct-new-declarator in this production,
5238 but GCC used to allowed this, so we check and emit a sensible error
5239 message for this case. */
5240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5242 error ("array bound forbidden after parenthesized type-id");
5243 inform ("try removing the parentheses around the type-id");
5244 cp_parser_direct_new_declarator (parser);
5248 /* Otherwise, there must be a new-type-id. */
5250 type = cp_parser_new_type_id (parser, &nelts);
5252 /* If the next token is a `(', then we have a new-initializer. */
5253 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5254 initializer = cp_parser_new_initializer (parser);
5256 initializer = NULL_TREE;
5258 /* A new-expression may not appear in an integral constant
5260 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5261 return error_mark_node;
5263 /* Create a representation of the new-expression. */
5264 return build_new (placement, type, nelts, initializer, global_scope_p);
5267 /* Parse a new-placement.
5272 Returns the same representation as for an expression-list. */
5275 cp_parser_new_placement (cp_parser* parser)
5277 tree expression_list;
5279 /* Parse the expression-list. */
5280 expression_list = (cp_parser_parenthesized_expression_list
5281 (parser, false, /*cast_p=*/false,
5282 /*non_constant_p=*/NULL));
5284 return expression_list;
5287 /* Parse a new-type-id.
5290 type-specifier-seq new-declarator [opt]
5292 Returns the TYPE allocated. If the new-type-id indicates an array
5293 type, *NELTS is set to the number of elements in the last array
5294 bound; the TYPE will not include the last array bound. */
5297 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5299 cp_decl_specifier_seq type_specifier_seq;
5300 cp_declarator *new_declarator;
5301 cp_declarator *declarator;
5302 cp_declarator *outer_declarator;
5303 const char *saved_message;
5306 /* The type-specifier sequence must not contain type definitions.
5307 (It cannot contain declarations of new types either, but if they
5308 are not definitions we will catch that because they are not
5310 saved_message = parser->type_definition_forbidden_message;
5311 parser->type_definition_forbidden_message
5312 = "types may not be defined in a new-type-id";
5313 /* Parse the type-specifier-seq. */
5314 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5315 &type_specifier_seq);
5316 /* Restore the old message. */
5317 parser->type_definition_forbidden_message = saved_message;
5318 /* Parse the new-declarator. */
5319 new_declarator = cp_parser_new_declarator_opt (parser);
5321 /* Determine the number of elements in the last array dimension, if
5324 /* Skip down to the last array dimension. */
5325 declarator = new_declarator;
5326 outer_declarator = NULL;
5327 while (declarator && (declarator->kind == cdk_pointer
5328 || declarator->kind == cdk_ptrmem))
5330 outer_declarator = declarator;
5331 declarator = declarator->declarator;
5334 && declarator->kind == cdk_array
5335 && declarator->declarator
5336 && declarator->declarator->kind == cdk_array)
5338 outer_declarator = declarator;
5339 declarator = declarator->declarator;
5342 if (declarator && declarator->kind == cdk_array)
5344 *nelts = declarator->u.array.bounds;
5345 if (*nelts == error_mark_node)
5346 *nelts = integer_one_node;
5348 if (outer_declarator)
5349 outer_declarator->declarator = declarator->declarator;
5351 new_declarator = NULL;
5354 type = groktypename (&type_specifier_seq, new_declarator);
5355 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5357 *nelts = array_type_nelts_top (type);
5358 type = TREE_TYPE (type);
5363 /* Parse an (optional) new-declarator.
5366 ptr-operator new-declarator [opt]
5367 direct-new-declarator
5369 Returns the declarator. */
5371 static cp_declarator *
5372 cp_parser_new_declarator_opt (cp_parser* parser)
5374 enum tree_code code;
5376 cp_cv_quals cv_quals;
5378 /* We don't know if there's a ptr-operator next, or not. */
5379 cp_parser_parse_tentatively (parser);
5380 /* Look for a ptr-operator. */
5381 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5382 /* If that worked, look for more new-declarators. */
5383 if (cp_parser_parse_definitely (parser))
5385 cp_declarator *declarator;
5387 /* Parse another optional declarator. */
5388 declarator = cp_parser_new_declarator_opt (parser);
5390 /* Create the representation of the declarator. */
5392 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5393 else if (code == INDIRECT_REF)
5394 declarator = make_pointer_declarator (cv_quals, declarator);
5396 declarator = make_reference_declarator (cv_quals, declarator);
5401 /* If the next token is a `[', there is a direct-new-declarator. */
5402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5403 return cp_parser_direct_new_declarator (parser);
5408 /* Parse a direct-new-declarator.
5410 direct-new-declarator:
5412 direct-new-declarator [constant-expression]
5416 static cp_declarator *
5417 cp_parser_direct_new_declarator (cp_parser* parser)
5419 cp_declarator *declarator = NULL;
5425 /* Look for the opening `['. */
5426 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5427 /* The first expression is not required to be constant. */
5430 expression = cp_parser_expression (parser, /*cast_p=*/false);
5431 /* The standard requires that the expression have integral
5432 type. DR 74 adds enumeration types. We believe that the
5433 real intent is that these expressions be handled like the
5434 expression in a `switch' condition, which also allows
5435 classes with a single conversion to integral or
5436 enumeration type. */
5437 if (!processing_template_decl)
5440 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5445 error ("expression in new-declarator must have integral "
5446 "or enumeration type");
5447 expression = error_mark_node;
5451 /* But all the other expressions must be. */
5454 = cp_parser_constant_expression (parser,
5455 /*allow_non_constant=*/false,
5457 /* Look for the closing `]'. */
5458 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5460 /* Add this bound to the declarator. */
5461 declarator = make_array_declarator (declarator, expression);
5463 /* If the next token is not a `[', then there are no more
5465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5472 /* Parse a new-initializer.
5475 ( expression-list [opt] )
5477 Returns a representation of the expression-list. If there is no
5478 expression-list, VOID_ZERO_NODE is returned. */
5481 cp_parser_new_initializer (cp_parser* parser)
5483 tree expression_list;
5485 expression_list = (cp_parser_parenthesized_expression_list
5486 (parser, false, /*cast_p=*/false,
5487 /*non_constant_p=*/NULL));
5488 if (!expression_list)
5489 expression_list = void_zero_node;
5491 return expression_list;
5494 /* Parse a delete-expression.
5497 :: [opt] delete cast-expression
5498 :: [opt] delete [ ] cast-expression
5500 Returns a representation of the expression. */
5503 cp_parser_delete_expression (cp_parser* parser)
5505 bool global_scope_p;
5509 /* Look for the optional `::' operator. */
5511 = (cp_parser_global_scope_opt (parser,
5512 /*current_scope_valid_p=*/false)
5514 /* Look for the `delete' keyword. */
5515 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5516 /* See if the array syntax is in use. */
5517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5519 /* Consume the `[' token. */
5520 cp_lexer_consume_token (parser->lexer);
5521 /* Look for the `]' token. */
5522 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5523 /* Remember that this is the `[]' construct. */
5529 /* Parse the cast-expression. */
5530 expression = cp_parser_simple_cast_expression (parser);
5532 /* A delete-expression may not appear in an integral constant
5534 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5535 return error_mark_node;
5537 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5540 /* Parse a cast-expression.
5544 ( type-id ) cast-expression
5546 ADDRESS_P is true iff the unary-expression is appearing as the
5547 operand of the `&' operator. CAST_P is true if this expression is
5548 the target of a cast.
5550 Returns a representation of the expression. */
5553 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5555 /* If it's a `(', then we might be looking at a cast. */
5556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5558 tree type = NULL_TREE;
5559 tree expr = NULL_TREE;
5560 bool compound_literal_p;
5561 const char *saved_message;
5563 /* There's no way to know yet whether or not this is a cast.
5564 For example, `(int (3))' is a unary-expression, while `(int)
5565 3' is a cast. So, we resort to parsing tentatively. */
5566 cp_parser_parse_tentatively (parser);
5567 /* Types may not be defined in a cast. */
5568 saved_message = parser->type_definition_forbidden_message;
5569 parser->type_definition_forbidden_message
5570 = "types may not be defined in casts";
5571 /* Consume the `('. */
5572 cp_lexer_consume_token (parser->lexer);
5573 /* A very tricky bit is that `(struct S) { 3 }' is a
5574 compound-literal (which we permit in C++ as an extension).
5575 But, that construct is not a cast-expression -- it is a
5576 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5577 is legal; if the compound-literal were a cast-expression,
5578 you'd need an extra set of parentheses.) But, if we parse
5579 the type-id, and it happens to be a class-specifier, then we
5580 will commit to the parse at that point, because we cannot
5581 undo the action that is done when creating a new class. So,
5582 then we cannot back up and do a postfix-expression.
5584 Therefore, we scan ahead to the closing `)', and check to see
5585 if the token after the `)' is a `{'. If so, we are not
5586 looking at a cast-expression.
5588 Save tokens so that we can put them back. */
5589 cp_lexer_save_tokens (parser->lexer);
5590 /* Skip tokens until the next token is a closing parenthesis.
5591 If we find the closing `)', and the next token is a `{', then
5592 we are looking at a compound-literal. */
5594 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5595 /*consume_paren=*/true)
5596 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5597 /* Roll back the tokens we skipped. */
5598 cp_lexer_rollback_tokens (parser->lexer);
5599 /* If we were looking at a compound-literal, simulate an error
5600 so that the call to cp_parser_parse_definitely below will
5602 if (compound_literal_p)
5603 cp_parser_simulate_error (parser);
5606 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5607 parser->in_type_id_in_expr_p = true;
5608 /* Look for the type-id. */
5609 type = cp_parser_type_id (parser);
5610 /* Look for the closing `)'. */
5611 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5615 /* Restore the saved message. */
5616 parser->type_definition_forbidden_message = saved_message;
5618 /* If ok so far, parse the dependent expression. We cannot be
5619 sure it is a cast. Consider `(T ())'. It is a parenthesized
5620 ctor of T, but looks like a cast to function returning T
5621 without a dependent expression. */
5622 if (!cp_parser_error_occurred (parser))
5623 expr = cp_parser_cast_expression (parser,
5624 /*address_p=*/false,
5627 if (cp_parser_parse_definitely (parser))
5629 /* Warn about old-style casts, if so requested. */
5630 if (warn_old_style_cast
5631 && !in_system_header
5632 && !VOID_TYPE_P (type)
5633 && current_lang_name != lang_name_c)
5634 warning (OPT_Wold_style_cast, "use of old-style cast");
5636 /* Only type conversions to integral or enumeration types
5637 can be used in constant-expressions. */
5638 if (!cast_valid_in_integral_constant_expression_p (type)
5639 && (cp_parser_non_integral_constant_expression
5641 "a cast to a type other than an integral or "
5642 "enumeration type")))
5643 return error_mark_node;
5645 /* Perform the cast. */
5646 expr = build_c_cast (type, expr);
5651 /* If we get here, then it's not a cast, so it must be a
5652 unary-expression. */
5653 return cp_parser_unary_expression (parser, address_p, cast_p);
5656 /* Parse a binary expression of the general form:
5660 pm-expression .* cast-expression
5661 pm-expression ->* cast-expression
5663 multiplicative-expression:
5665 multiplicative-expression * pm-expression
5666 multiplicative-expression / pm-expression
5667 multiplicative-expression % pm-expression
5669 additive-expression:
5670 multiplicative-expression
5671 additive-expression + multiplicative-expression
5672 additive-expression - multiplicative-expression
5676 shift-expression << additive-expression
5677 shift-expression >> additive-expression
5679 relational-expression:
5681 relational-expression < shift-expression
5682 relational-expression > shift-expression
5683 relational-expression <= shift-expression
5684 relational-expression >= shift-expression
5688 relational-expression:
5689 relational-expression <? shift-expression
5690 relational-expression >? shift-expression
5692 equality-expression:
5693 relational-expression
5694 equality-expression == relational-expression
5695 equality-expression != relational-expression
5699 and-expression & equality-expression
5701 exclusive-or-expression:
5703 exclusive-or-expression ^ and-expression
5705 inclusive-or-expression:
5706 exclusive-or-expression
5707 inclusive-or-expression | exclusive-or-expression
5709 logical-and-expression:
5710 inclusive-or-expression
5711 logical-and-expression && inclusive-or-expression
5713 logical-or-expression:
5714 logical-and-expression
5715 logical-or-expression || logical-and-expression
5717 All these are implemented with a single function like:
5720 simple-cast-expression
5721 binary-expression <token> binary-expression
5723 CAST_P is true if this expression is the target of a cast.
5725 The binops_by_token map is used to get the tree codes for each <token> type.
5726 binary-expressions are associated according to a precedence table. */
5728 #define TOKEN_PRECEDENCE(token) \
5729 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5730 ? PREC_NOT_OPERATOR \
5731 : binops_by_token[token->type].prec)
5734 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5736 cp_parser_expression_stack stack;
5737 cp_parser_expression_stack_entry *sp = &stack[0];
5740 enum tree_code tree_type, lhs_type, rhs_type;
5741 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5744 /* Parse the first expression. */
5745 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5746 lhs_type = ERROR_MARK;
5750 /* Get an operator token. */
5751 token = cp_lexer_peek_token (parser->lexer);
5753 new_prec = TOKEN_PRECEDENCE (token);
5755 /* Popping an entry off the stack means we completed a subexpression:
5756 - either we found a token which is not an operator (`>' where it is not
5757 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5758 will happen repeatedly;
5759 - or, we found an operator which has lower priority. This is the case
5760 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5762 if (new_prec <= prec)
5771 tree_type = binops_by_token[token->type].tree_type;
5773 /* We used the operator token. */
5774 cp_lexer_consume_token (parser->lexer);
5776 /* Extract another operand. It may be the RHS of this expression
5777 or the LHS of a new, higher priority expression. */
5778 rhs = cp_parser_simple_cast_expression (parser);
5779 rhs_type = ERROR_MARK;
5781 /* Get another operator token. Look up its precedence to avoid
5782 building a useless (immediately popped) stack entry for common
5783 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5784 token = cp_lexer_peek_token (parser->lexer);
5785 lookahead_prec = TOKEN_PRECEDENCE (token);
5786 if (lookahead_prec > new_prec)
5788 /* ... and prepare to parse the RHS of the new, higher priority
5789 expression. Since precedence levels on the stack are
5790 monotonically increasing, we do not have to care about
5793 sp->tree_type = tree_type;
5795 sp->lhs_type = lhs_type;
5798 lhs_type = rhs_type;
5800 new_prec = lookahead_prec;
5804 /* If the stack is not empty, we have parsed into LHS the right side
5805 (`4' in the example above) of an expression we had suspended.
5806 We can use the information on the stack to recover the LHS (`3')
5807 from the stack together with the tree code (`MULT_EXPR'), and
5808 the precedence of the higher level subexpression
5809 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5810 which will be used to actually build the additive expression. */
5813 tree_type = sp->tree_type;
5815 rhs_type = lhs_type;
5817 lhs_type = sp->lhs_type;
5820 overloaded_p = false;
5821 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5823 lhs_type = tree_type;
5825 /* If the binary operator required the use of an overloaded operator,
5826 then this expression cannot be an integral constant-expression.
5827 An overloaded operator can be used even if both operands are
5828 otherwise permissible in an integral constant-expression if at
5829 least one of the operands is of enumeration type. */
5832 && (cp_parser_non_integral_constant_expression
5833 (parser, "calls to overloaded operators")))
5834 return error_mark_node;
5841 /* Parse the `? expression : assignment-expression' part of a
5842 conditional-expression. The LOGICAL_OR_EXPR is the
5843 logical-or-expression that started the conditional-expression.
5844 Returns a representation of the entire conditional-expression.
5846 This routine is used by cp_parser_assignment_expression.
5848 ? expression : assignment-expression
5852 ? : assignment-expression */
5855 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5858 tree assignment_expr;
5860 /* Consume the `?' token. */
5861 cp_lexer_consume_token (parser->lexer);
5862 if (cp_parser_allow_gnu_extensions_p (parser)
5863 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5864 /* Implicit true clause. */
5867 /* Parse the expression. */
5868 expr = cp_parser_expression (parser, /*cast_p=*/false);
5870 /* The next token should be a `:'. */
5871 cp_parser_require (parser, CPP_COLON, "`:'");
5872 /* Parse the assignment-expression. */
5873 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5875 /* Build the conditional-expression. */
5876 return build_x_conditional_expr (logical_or_expr,
5881 /* Parse an assignment-expression.
5883 assignment-expression:
5884 conditional-expression
5885 logical-or-expression assignment-operator assignment_expression
5888 CAST_P is true if this expression is the target of a cast.
5890 Returns a representation for the expression. */
5893 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5897 /* If the next token is the `throw' keyword, then we're looking at
5898 a throw-expression. */
5899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5900 expr = cp_parser_throw_expression (parser);
5901 /* Otherwise, it must be that we are looking at a
5902 logical-or-expression. */
5905 /* Parse the binary expressions (logical-or-expression). */
5906 expr = cp_parser_binary_expression (parser, cast_p);
5907 /* If the next token is a `?' then we're actually looking at a
5908 conditional-expression. */
5909 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5910 return cp_parser_question_colon_clause (parser, expr);
5913 enum tree_code assignment_operator;
5915 /* If it's an assignment-operator, we're using the second
5918 = cp_parser_assignment_operator_opt (parser);
5919 if (assignment_operator != ERROR_MARK)
5923 /* Parse the right-hand side of the assignment. */
5924 rhs = cp_parser_assignment_expression (parser, cast_p);
5925 /* An assignment may not appear in a
5926 constant-expression. */
5927 if (cp_parser_non_integral_constant_expression (parser,
5929 return error_mark_node;
5930 /* Build the assignment expression. */
5931 expr = build_x_modify_expr (expr,
5932 assignment_operator,
5941 /* Parse an (optional) assignment-operator.
5943 assignment-operator: one of
5944 = *= /= %= += -= >>= <<= &= ^= |=
5948 assignment-operator: one of
5951 If the next token is an assignment operator, the corresponding tree
5952 code is returned, and the token is consumed. For example, for
5953 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5954 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5955 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5956 operator, ERROR_MARK is returned. */
5958 static enum tree_code
5959 cp_parser_assignment_operator_opt (cp_parser* parser)
5964 /* Peek at the next toen. */
5965 token = cp_lexer_peek_token (parser->lexer);
5967 switch (token->type)
5978 op = TRUNC_DIV_EXPR;
5982 op = TRUNC_MOD_EXPR;
6014 /* Nothing else is an assignment operator. */
6018 /* If it was an assignment operator, consume it. */
6019 if (op != ERROR_MARK)
6020 cp_lexer_consume_token (parser->lexer);
6025 /* Parse an expression.
6028 assignment-expression
6029 expression , assignment-expression
6031 CAST_P is true if this expression is the target of a cast.
6033 Returns a representation of the expression. */
6036 cp_parser_expression (cp_parser* parser, bool cast_p)
6038 tree expression = NULL_TREE;
6042 tree assignment_expression;
6044 /* Parse the next assignment-expression. */
6045 assignment_expression
6046 = cp_parser_assignment_expression (parser, cast_p);
6047 /* If this is the first assignment-expression, we can just
6050 expression = assignment_expression;
6052 expression = build_x_compound_expr (expression,
6053 assignment_expression);
6054 /* If the next token is not a comma, then we are done with the
6056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6058 /* Consume the `,'. */
6059 cp_lexer_consume_token (parser->lexer);
6060 /* A comma operator cannot appear in a constant-expression. */
6061 if (cp_parser_non_integral_constant_expression (parser,
6062 "a comma operator"))
6063 expression = error_mark_node;
6069 /* Parse a constant-expression.
6071 constant-expression:
6072 conditional-expression
6074 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6075 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6076 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6077 is false, NON_CONSTANT_P should be NULL. */
6080 cp_parser_constant_expression (cp_parser* parser,
6081 bool allow_non_constant_p,
6082 bool *non_constant_p)
6084 bool saved_integral_constant_expression_p;
6085 bool saved_allow_non_integral_constant_expression_p;
6086 bool saved_non_integral_constant_expression_p;
6089 /* It might seem that we could simply parse the
6090 conditional-expression, and then check to see if it were
6091 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6092 one that the compiler can figure out is constant, possibly after
6093 doing some simplifications or optimizations. The standard has a
6094 precise definition of constant-expression, and we must honor
6095 that, even though it is somewhat more restrictive.
6101 is not a legal declaration, because `(2, 3)' is not a
6102 constant-expression. The `,' operator is forbidden in a
6103 constant-expression. However, GCC's constant-folding machinery
6104 will fold this operation to an INTEGER_CST for `3'. */
6106 /* Save the old settings. */
6107 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6108 saved_allow_non_integral_constant_expression_p
6109 = parser->allow_non_integral_constant_expression_p;
6110 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6111 /* We are now parsing a constant-expression. */
6112 parser->integral_constant_expression_p = true;
6113 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6114 parser->non_integral_constant_expression_p = false;
6115 /* Although the grammar says "conditional-expression", we parse an
6116 "assignment-expression", which also permits "throw-expression"
6117 and the use of assignment operators. In the case that
6118 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6119 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6120 actually essential that we look for an assignment-expression.
6121 For example, cp_parser_initializer_clauses uses this function to
6122 determine whether a particular assignment-expression is in fact
6124 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6125 /* Restore the old settings. */
6126 parser->integral_constant_expression_p
6127 = saved_integral_constant_expression_p;
6128 parser->allow_non_integral_constant_expression_p
6129 = saved_allow_non_integral_constant_expression_p;
6130 if (allow_non_constant_p)
6131 *non_constant_p = parser->non_integral_constant_expression_p;
6132 else if (parser->non_integral_constant_expression_p)
6133 expression = error_mark_node;
6134 parser->non_integral_constant_expression_p
6135 = saved_non_integral_constant_expression_p;
6140 /* Parse __builtin_offsetof.
6142 offsetof-expression:
6143 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6145 offsetof-member-designator:
6147 | offsetof-member-designator "." id-expression
6148 | offsetof-member-designator "[" expression "]" */
6151 cp_parser_builtin_offsetof (cp_parser *parser)
6153 int save_ice_p, save_non_ice_p;
6157 /* We're about to accept non-integral-constant things, but will
6158 definitely yield an integral constant expression. Save and
6159 restore these values around our local parsing. */
6160 save_ice_p = parser->integral_constant_expression_p;
6161 save_non_ice_p = parser->non_integral_constant_expression_p;
6163 /* Consume the "__builtin_offsetof" token. */
6164 cp_lexer_consume_token (parser->lexer);
6165 /* Consume the opening `('. */
6166 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6167 /* Parse the type-id. */
6168 type = cp_parser_type_id (parser);
6169 /* Look for the `,'. */
6170 cp_parser_require (parser, CPP_COMMA, "`,'");
6172 /* Build the (type *)null that begins the traditional offsetof macro. */
6173 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6175 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6176 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6180 cp_token *token = cp_lexer_peek_token (parser->lexer);
6181 switch (token->type)
6183 case CPP_OPEN_SQUARE:
6184 /* offsetof-member-designator "[" expression "]" */
6185 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6189 /* offsetof-member-designator "." identifier */
6190 cp_lexer_consume_token (parser->lexer);
6191 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6195 case CPP_CLOSE_PAREN:
6196 /* Consume the ")" token. */
6197 cp_lexer_consume_token (parser->lexer);
6201 /* Error. We know the following require will fail, but
6202 that gives the proper error message. */
6203 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6204 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6205 expr = error_mark_node;
6211 /* If we're processing a template, we can't finish the semantics yet.
6212 Otherwise we can fold the entire expression now. */
6213 if (processing_template_decl)
6214 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6216 expr = finish_offsetof (expr);
6219 parser->integral_constant_expression_p = save_ice_p;
6220 parser->non_integral_constant_expression_p = save_non_ice_p;
6225 /* Statements [gram.stmt.stmt] */
6227 /* Parse a statement.
6231 expression-statement
6236 declaration-statement
6239 IN_COMPOUND is true when the statement is nested inside a
6240 cp_parser_compound_statement; this matters for certain pragmas.
6242 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6243 is a (possibly labeled) if statement which is not enclosed in braces
6244 and has an else clause. This is used to implement -Wparentheses. */
6247 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6248 bool in_compound, bool *if_p)
6252 location_t statement_location;
6257 /* There is no statement yet. */
6258 statement = NULL_TREE;
6259 /* Peek at the next token. */
6260 token = cp_lexer_peek_token (parser->lexer);
6261 /* Remember the location of the first token in the statement. */
6262 statement_location = token->location;
6263 /* If this is a keyword, then that will often determine what kind of
6264 statement we have. */
6265 if (token->type == CPP_KEYWORD)
6267 enum rid keyword = token->keyword;
6273 /* Looks like a labeled-statement with a case label.
6274 Parse the label, and then use tail recursion to parse
6276 cp_parser_label_for_labeled_statement (parser);
6281 statement = cp_parser_selection_statement (parser, if_p);
6287 statement = cp_parser_iteration_statement (parser);
6294 statement = cp_parser_jump_statement (parser);
6297 /* Objective-C++ exception-handling constructs. */
6300 case RID_AT_FINALLY:
6301 case RID_AT_SYNCHRONIZED:
6303 statement = cp_parser_objc_statement (parser);
6307 statement = cp_parser_try_block (parser);
6311 /* It might be a keyword like `int' that can start a
6312 declaration-statement. */
6316 else if (token->type == CPP_NAME)
6318 /* If the next token is a `:', then we are looking at a
6319 labeled-statement. */
6320 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6321 if (token->type == CPP_COLON)
6323 /* Looks like a labeled-statement with an ordinary label.
6324 Parse the label, and then use tail recursion to parse
6326 cp_parser_label_for_labeled_statement (parser);
6330 /* Anything that starts with a `{' must be a compound-statement. */
6331 else if (token->type == CPP_OPEN_BRACE)
6332 statement = cp_parser_compound_statement (parser, NULL, false);
6333 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6334 a statement all its own. */
6335 else if (token->type == CPP_PRAGMA)
6337 /* Only certain OpenMP pragmas are attached to statements, and thus
6338 are considered statements themselves. All others are not. In
6339 the context of a compound, accept the pragma as a "statement" and
6340 return so that we can check for a close brace. Otherwise we
6341 require a real statement and must go back and read one. */
6343 cp_parser_pragma (parser, pragma_compound);
6344 else if (!cp_parser_pragma (parser, pragma_stmt))
6348 else if (token->type == CPP_EOF)
6350 cp_parser_error (parser, "expected statement");
6354 /* Everything else must be a declaration-statement or an
6355 expression-statement. Try for the declaration-statement
6356 first, unless we are looking at a `;', in which case we know that
6357 we have an expression-statement. */
6360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6362 cp_parser_parse_tentatively (parser);
6363 /* Try to parse the declaration-statement. */
6364 cp_parser_declaration_statement (parser);
6365 /* If that worked, we're done. */
6366 if (cp_parser_parse_definitely (parser))
6369 /* Look for an expression-statement instead. */
6370 statement = cp_parser_expression_statement (parser, in_statement_expr);
6373 /* Set the line number for the statement. */
6374 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6375 SET_EXPR_LOCATION (statement, statement_location);
6378 /* Parse the label for a labeled-statement, i.e.
6381 case constant-expression :
6385 case constant-expression ... constant-expression : statement
6387 When a label is parsed without errors, the label is added to the
6388 parse tree by the finish_* functions, so this function doesn't
6389 have to return the label. */
6392 cp_parser_label_for_labeled_statement (cp_parser* parser)
6396 /* The next token should be an identifier. */
6397 token = cp_lexer_peek_token (parser->lexer);
6398 if (token->type != CPP_NAME
6399 && token->type != CPP_KEYWORD)
6401 cp_parser_error (parser, "expected labeled-statement");
6405 switch (token->keyword)
6412 /* Consume the `case' token. */
6413 cp_lexer_consume_token (parser->lexer);
6414 /* Parse the constant-expression. */
6415 expr = cp_parser_constant_expression (parser,
6416 /*allow_non_constant_p=*/false,
6419 ellipsis = cp_lexer_peek_token (parser->lexer);
6420 if (ellipsis->type == CPP_ELLIPSIS)
6422 /* Consume the `...' token. */
6423 cp_lexer_consume_token (parser->lexer);
6425 cp_parser_constant_expression (parser,
6426 /*allow_non_constant_p=*/false,
6428 /* We don't need to emit warnings here, as the common code
6429 will do this for us. */
6432 expr_hi = NULL_TREE;
6434 if (parser->in_switch_statement_p)
6435 finish_case_label (expr, expr_hi);
6437 error ("case label %qE not within a switch statement", expr);
6442 /* Consume the `default' token. */
6443 cp_lexer_consume_token (parser->lexer);
6445 if (parser->in_switch_statement_p)
6446 finish_case_label (NULL_TREE, NULL_TREE);
6448 error ("case label not within a switch statement");
6452 /* Anything else must be an ordinary label. */
6453 finish_label_stmt (cp_parser_identifier (parser));
6457 /* Require the `:' token. */
6458 cp_parser_require (parser, CPP_COLON, "`:'");
6461 /* Parse an expression-statement.
6463 expression-statement:
6466 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6467 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6468 indicates whether this expression-statement is part of an
6469 expression statement. */
6472 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6474 tree statement = NULL_TREE;
6476 /* If the next token is a ';', then there is no expression
6478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6479 statement = cp_parser_expression (parser, /*cast_p=*/false);
6481 /* Consume the final `;'. */
6482 cp_parser_consume_semicolon_at_end_of_statement (parser);
6484 if (in_statement_expr
6485 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6486 /* This is the final expression statement of a statement
6488 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6490 statement = finish_expr_stmt (statement);
6497 /* Parse a compound-statement.
6500 { statement-seq [opt] }
6502 Returns a tree representing the statement. */
6505 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6510 /* Consume the `{'. */
6511 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6512 return error_mark_node;
6513 /* Begin the compound-statement. */
6514 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6515 /* Parse an (optional) statement-seq. */
6516 cp_parser_statement_seq_opt (parser, in_statement_expr);
6517 /* Finish the compound-statement. */
6518 finish_compound_stmt (compound_stmt);
6519 /* Consume the `}'. */
6520 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6522 return compound_stmt;
6525 /* Parse an (optional) statement-seq.
6529 statement-seq [opt] statement */
6532 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6534 /* Scan statements until there aren't any more. */
6537 cp_token *token = cp_lexer_peek_token (parser->lexer);
6539 /* If we're looking at a `}', then we've run out of statements. */
6540 if (token->type == CPP_CLOSE_BRACE
6541 || token->type == CPP_EOF
6542 || token->type == CPP_PRAGMA_EOL)
6545 /* Parse the statement. */
6546 cp_parser_statement (parser, in_statement_expr, true, NULL);
6550 /* Parse a selection-statement.
6552 selection-statement:
6553 if ( condition ) statement
6554 if ( condition ) statement else statement
6555 switch ( condition ) statement
6557 Returns the new IF_STMT or SWITCH_STMT.
6559 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6560 is a (possibly labeled) if statement which is not enclosed in
6561 braces and has an else clause. This is used to implement
6565 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6573 /* Peek at the next token. */
6574 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6576 /* See what kind of keyword it is. */
6577 keyword = token->keyword;
6586 /* Look for the `('. */
6587 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6589 cp_parser_skip_to_end_of_statement (parser);
6590 return error_mark_node;
6593 /* Begin the selection-statement. */
6594 if (keyword == RID_IF)
6595 statement = begin_if_stmt ();
6597 statement = begin_switch_stmt ();
6599 /* Parse the condition. */
6600 condition = cp_parser_condition (parser);
6601 /* Look for the `)'. */
6602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6603 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6604 /*consume_paren=*/true);
6606 if (keyword == RID_IF)
6610 /* Add the condition. */
6611 finish_if_stmt_cond (condition, statement);
6613 /* Parse the then-clause. */
6614 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6615 finish_then_clause (statement);
6617 /* If the next token is `else', parse the else-clause. */
6618 if (cp_lexer_next_token_is_keyword (parser->lexer,
6621 /* Consume the `else' keyword. */
6622 cp_lexer_consume_token (parser->lexer);
6623 begin_else_clause (statement);
6624 /* Parse the else-clause. */
6625 cp_parser_implicitly_scoped_statement (parser, NULL);
6626 finish_else_clause (statement);
6628 /* If we are currently parsing a then-clause, then
6629 IF_P will not be NULL. We set it to true to
6630 indicate that this if statement has an else clause.
6631 This may trigger the Wparentheses warning below
6632 when we get back up to the parent if statement. */
6638 /* This if statement does not have an else clause. If
6639 NESTED_IF is true, then the then-clause is an if
6640 statement which does have an else clause. We warn
6641 about the potential ambiguity. */
6643 warning (OPT_Wparentheses,
6644 ("%Hsuggest explicit braces "
6645 "to avoid ambiguous %<else%>"),
6646 EXPR_LOCUS (statement));
6649 /* Now we're all done with the if-statement. */
6650 finish_if_stmt (statement);
6654 bool in_switch_statement_p;
6655 unsigned char in_statement;
6657 /* Add the condition. */
6658 finish_switch_cond (condition, statement);
6660 /* Parse the body of the switch-statement. */
6661 in_switch_statement_p = parser->in_switch_statement_p;
6662 in_statement = parser->in_statement;
6663 parser->in_switch_statement_p = true;
6664 parser->in_statement |= IN_SWITCH_STMT;
6665 cp_parser_implicitly_scoped_statement (parser, NULL);
6666 parser->in_switch_statement_p = in_switch_statement_p;
6667 parser->in_statement = in_statement;
6669 /* Now we're all done with the switch-statement. */
6670 finish_switch_stmt (statement);
6678 cp_parser_error (parser, "expected selection-statement");
6679 return error_mark_node;
6683 /* Parse a condition.
6687 type-specifier-seq declarator = assignment-expression
6692 type-specifier-seq declarator asm-specification [opt]
6693 attributes [opt] = assignment-expression
6695 Returns the expression that should be tested. */
6698 cp_parser_condition (cp_parser* parser)
6700 cp_decl_specifier_seq type_specifiers;
6701 const char *saved_message;
6703 /* Try the declaration first. */
6704 cp_parser_parse_tentatively (parser);
6705 /* New types are not allowed in the type-specifier-seq for a
6707 saved_message = parser->type_definition_forbidden_message;
6708 parser->type_definition_forbidden_message
6709 = "types may not be defined in conditions";
6710 /* Parse the type-specifier-seq. */
6711 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6713 /* Restore the saved message. */
6714 parser->type_definition_forbidden_message = saved_message;
6715 /* If all is well, we might be looking at a declaration. */
6716 if (!cp_parser_error_occurred (parser))
6719 tree asm_specification;
6721 cp_declarator *declarator;
6722 tree initializer = NULL_TREE;
6724 /* Parse the declarator. */
6725 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6726 /*ctor_dtor_or_conv_p=*/NULL,
6727 /*parenthesized_p=*/NULL,
6728 /*member_p=*/false);
6729 /* Parse the attributes. */
6730 attributes = cp_parser_attributes_opt (parser);
6731 /* Parse the asm-specification. */
6732 asm_specification = cp_parser_asm_specification_opt (parser);
6733 /* If the next token is not an `=', then we might still be
6734 looking at an expression. For example:
6738 looks like a decl-specifier-seq and a declarator -- but then
6739 there is no `=', so this is an expression. */
6740 cp_parser_require (parser, CPP_EQ, "`='");
6741 /* If we did see an `=', then we are looking at a declaration
6743 if (cp_parser_parse_definitely (parser))
6746 bool non_constant_p;
6748 /* Create the declaration. */
6749 decl = start_decl (declarator, &type_specifiers,
6750 /*initialized_p=*/true,
6751 attributes, /*prefix_attributes=*/NULL_TREE,
6753 /* Parse the assignment-expression. */
6755 = cp_parser_constant_expression (parser,
6756 /*allow_non_constant_p=*/true,
6758 if (!non_constant_p)
6759 initializer = fold_non_dependent_expr (initializer);
6761 /* Process the initializer. */
6762 cp_finish_decl (decl,
6763 initializer, !non_constant_p,
6765 LOOKUP_ONLYCONVERTING);
6768 pop_scope (pushed_scope);
6770 return convert_from_reference (decl);
6773 /* If we didn't even get past the declarator successfully, we are
6774 definitely not looking at a declaration. */
6776 cp_parser_abort_tentative_parse (parser);
6778 /* Otherwise, we are looking at an expression. */
6779 return cp_parser_expression (parser, /*cast_p=*/false);
6782 /* Parse an iteration-statement.
6784 iteration-statement:
6785 while ( condition ) statement
6786 do statement while ( expression ) ;
6787 for ( for-init-statement condition [opt] ; expression [opt] )
6790 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6793 cp_parser_iteration_statement (cp_parser* parser)
6798 unsigned char in_statement;
6800 /* Peek at the next token. */
6801 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6803 return error_mark_node;
6805 /* Remember whether or not we are already within an iteration
6807 in_statement = parser->in_statement;
6809 /* See what kind of keyword it is. */
6810 keyword = token->keyword;
6817 /* Begin the while-statement. */
6818 statement = begin_while_stmt ();
6819 /* Look for the `('. */
6820 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6821 /* Parse the condition. */
6822 condition = cp_parser_condition (parser);
6823 finish_while_stmt_cond (condition, statement);
6824 /* Look for the `)'. */
6825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6826 /* Parse the dependent statement. */
6827 parser->in_statement = IN_ITERATION_STMT;
6828 cp_parser_already_scoped_statement (parser);
6829 parser->in_statement = in_statement;
6830 /* We're done with the while-statement. */
6831 finish_while_stmt (statement);
6839 /* Begin the do-statement. */
6840 statement = begin_do_stmt ();
6841 /* Parse the body of the do-statement. */
6842 parser->in_statement = IN_ITERATION_STMT;
6843 cp_parser_implicitly_scoped_statement (parser, NULL);
6844 parser->in_statement = in_statement;
6845 finish_do_body (statement);
6846 /* Look for the `while' keyword. */
6847 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6848 /* Look for the `('. */
6849 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6850 /* Parse the expression. */
6851 expression = cp_parser_expression (parser, /*cast_p=*/false);
6852 /* We're done with the do-statement. */
6853 finish_do_stmt (expression, statement);
6854 /* Look for the `)'. */
6855 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6856 /* Look for the `;'. */
6857 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6863 tree condition = NULL_TREE;
6864 tree expression = NULL_TREE;
6866 /* Begin the for-statement. */
6867 statement = begin_for_stmt ();
6868 /* Look for the `('. */
6869 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6870 /* Parse the initialization. */
6871 cp_parser_for_init_statement (parser);
6872 finish_for_init_stmt (statement);
6874 /* If there's a condition, process it. */
6875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6876 condition = cp_parser_condition (parser);
6877 finish_for_cond (condition, statement);
6878 /* Look for the `;'. */
6879 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6881 /* If there's an expression, process it. */
6882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6883 expression = cp_parser_expression (parser, /*cast_p=*/false);
6884 finish_for_expr (expression, statement);
6885 /* Look for the `)'. */
6886 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6888 /* Parse the body of the for-statement. */
6889 parser->in_statement = IN_ITERATION_STMT;
6890 cp_parser_already_scoped_statement (parser);
6891 parser->in_statement = in_statement;
6893 /* We're done with the for-statement. */
6894 finish_for_stmt (statement);
6899 cp_parser_error (parser, "expected iteration-statement");
6900 statement = error_mark_node;
6907 /* Parse a for-init-statement.
6910 expression-statement
6911 simple-declaration */
6914 cp_parser_for_init_statement (cp_parser* parser)
6916 /* If the next token is a `;', then we have an empty
6917 expression-statement. Grammatically, this is also a
6918 simple-declaration, but an invalid one, because it does not
6919 declare anything. Therefore, if we did not handle this case
6920 specially, we would issue an error message about an invalid
6922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6924 /* We're going to speculatively look for a declaration, falling back
6925 to an expression, if necessary. */
6926 cp_parser_parse_tentatively (parser);
6927 /* Parse the declaration. */
6928 cp_parser_simple_declaration (parser,
6929 /*function_definition_allowed_p=*/false);
6930 /* If the tentative parse failed, then we shall need to look for an
6931 expression-statement. */
6932 if (cp_parser_parse_definitely (parser))
6936 cp_parser_expression_statement (parser, false);
6939 /* Parse a jump-statement.
6944 return expression [opt] ;
6952 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6955 cp_parser_jump_statement (cp_parser* parser)
6957 tree statement = error_mark_node;
6961 /* Peek at the next token. */
6962 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6964 return error_mark_node;
6966 /* See what kind of keyword it is. */
6967 keyword = token->keyword;
6971 switch (parser->in_statement)
6974 error ("break statement not within loop or switch");
6977 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6978 || parser->in_statement == IN_ITERATION_STMT);
6979 statement = finish_break_stmt ();
6982 error ("invalid exit from OpenMP structured block");
6985 error ("break statement used with OpenMP for loop");
6988 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6992 switch (parser->in_statement & ~IN_SWITCH_STMT)
6995 error ("continue statement not within a loop");
6997 case IN_ITERATION_STMT:
6999 statement = finish_continue_stmt ();
7002 error ("invalid exit from OpenMP structured block");
7007 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7014 /* If the next token is a `;', then there is no
7016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7017 expr = cp_parser_expression (parser, /*cast_p=*/false);
7020 /* Build the return-statement. */
7021 statement = finish_return_stmt (expr);
7022 /* Look for the final `;'. */
7023 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7028 /* Create the goto-statement. */
7029 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7031 /* Issue a warning about this use of a GNU extension. */
7033 pedwarn ("ISO C++ forbids computed gotos");
7034 /* Consume the '*' token. */
7035 cp_lexer_consume_token (parser->lexer);
7036 /* Parse the dependent expression. */
7037 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7040 finish_goto_stmt (cp_parser_identifier (parser));
7041 /* Look for the final `;'. */
7042 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7046 cp_parser_error (parser, "expected jump-statement");
7053 /* Parse a declaration-statement.
7055 declaration-statement:
7056 block-declaration */
7059 cp_parser_declaration_statement (cp_parser* parser)
7063 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7064 p = obstack_alloc (&declarator_obstack, 0);
7066 /* Parse the block-declaration. */
7067 cp_parser_block_declaration (parser, /*statement_p=*/true);
7069 /* Free any declarators allocated. */
7070 obstack_free (&declarator_obstack, p);
7072 /* Finish off the statement. */
7076 /* Some dependent statements (like `if (cond) statement'), are
7077 implicitly in their own scope. In other words, if the statement is
7078 a single statement (as opposed to a compound-statement), it is
7079 none-the-less treated as if it were enclosed in braces. Any
7080 declarations appearing in the dependent statement are out of scope
7081 after control passes that point. This function parses a statement,
7082 but ensures that is in its own scope, even if it is not a
7085 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7086 is a (possibly labeled) if statement which is not enclosed in
7087 braces and has an else clause. This is used to implement
7090 Returns the new statement. */
7093 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7100 /* Mark if () ; with a special NOP_EXPR. */
7101 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7103 cp_lexer_consume_token (parser->lexer);
7104 statement = add_stmt (build_empty_stmt ());
7106 /* if a compound is opened, we simply parse the statement directly. */
7107 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7108 statement = cp_parser_compound_statement (parser, NULL, false);
7109 /* If the token is not a `{', then we must take special action. */
7112 /* Create a compound-statement. */
7113 statement = begin_compound_stmt (0);
7114 /* Parse the dependent-statement. */
7115 cp_parser_statement (parser, NULL_TREE, false, if_p);
7116 /* Finish the dummy compound-statement. */
7117 finish_compound_stmt (statement);
7120 /* Return the statement. */
7124 /* For some dependent statements (like `while (cond) statement'), we
7125 have already created a scope. Therefore, even if the dependent
7126 statement is a compound-statement, we do not want to create another
7130 cp_parser_already_scoped_statement (cp_parser* parser)
7132 /* If the token is a `{', then we must take special action. */
7133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7134 cp_parser_statement (parser, NULL_TREE, false, NULL);
7137 /* Avoid calling cp_parser_compound_statement, so that we
7138 don't create a new scope. Do everything else by hand. */
7139 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7140 cp_parser_statement_seq_opt (parser, NULL_TREE);
7141 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7145 /* Declarations [gram.dcl.dcl] */
7147 /* Parse an optional declaration-sequence.
7151 declaration-seq declaration */
7154 cp_parser_declaration_seq_opt (cp_parser* parser)
7160 token = cp_lexer_peek_token (parser->lexer);
7162 if (token->type == CPP_CLOSE_BRACE
7163 || token->type == CPP_EOF
7164 || token->type == CPP_PRAGMA_EOL)
7167 if (token->type == CPP_SEMICOLON)
7169 /* A declaration consisting of a single semicolon is
7170 invalid. Allow it unless we're being pedantic. */
7171 cp_lexer_consume_token (parser->lexer);
7172 if (pedantic && !in_system_header)
7173 pedwarn ("extra %<;%>");
7177 /* If we're entering or exiting a region that's implicitly
7178 extern "C", modify the lang context appropriately. */
7179 if (!parser->implicit_extern_c && token->implicit_extern_c)
7181 push_lang_context (lang_name_c);
7182 parser->implicit_extern_c = true;
7184 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7186 pop_lang_context ();
7187 parser->implicit_extern_c = false;
7190 if (token->type == CPP_PRAGMA)
7192 /* A top-level declaration can consist solely of a #pragma.
7193 A nested declaration cannot, so this is done here and not
7194 in cp_parser_declaration. (A #pragma at block scope is
7195 handled in cp_parser_statement.) */
7196 cp_parser_pragma (parser, pragma_external);
7200 /* Parse the declaration itself. */
7201 cp_parser_declaration (parser);
7205 /* Parse a declaration.
7210 template-declaration
7211 explicit-instantiation
7212 explicit-specialization
7213 linkage-specification
7214 namespace-definition
7219 __extension__ declaration */
7222 cp_parser_declaration (cp_parser* parser)
7229 /* Check for the `__extension__' keyword. */
7230 if (cp_parser_extension_opt (parser, &saved_pedantic))
7232 /* Parse the qualified declaration. */
7233 cp_parser_declaration (parser);
7234 /* Restore the PEDANTIC flag. */
7235 pedantic = saved_pedantic;
7240 /* Try to figure out what kind of declaration is present. */
7241 token1 = *cp_lexer_peek_token (parser->lexer);
7243 if (token1.type != CPP_EOF)
7244 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7247 token2.type = CPP_EOF;
7248 token2.keyword = RID_MAX;
7251 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7252 p = obstack_alloc (&declarator_obstack, 0);
7254 /* If the next token is `extern' and the following token is a string
7255 literal, then we have a linkage specification. */
7256 if (token1.keyword == RID_EXTERN
7257 && cp_parser_is_string_literal (&token2))
7258 cp_parser_linkage_specification (parser);
7259 /* If the next token is `template', then we have either a template
7260 declaration, an explicit instantiation, or an explicit
7262 else if (token1.keyword == RID_TEMPLATE)
7264 /* `template <>' indicates a template specialization. */
7265 if (token2.type == CPP_LESS
7266 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7267 cp_parser_explicit_specialization (parser);
7268 /* `template <' indicates a template declaration. */
7269 else if (token2.type == CPP_LESS)
7270 cp_parser_template_declaration (parser, /*member_p=*/false);
7271 /* Anything else must be an explicit instantiation. */
7273 cp_parser_explicit_instantiation (parser);
7275 /* If the next token is `export', then we have a template
7277 else if (token1.keyword == RID_EXPORT)
7278 cp_parser_template_declaration (parser, /*member_p=*/false);
7279 /* If the next token is `extern', 'static' or 'inline' and the one
7280 after that is `template', we have a GNU extended explicit
7281 instantiation directive. */
7282 else if (cp_parser_allow_gnu_extensions_p (parser)
7283 && (token1.keyword == RID_EXTERN
7284 || token1.keyword == RID_STATIC
7285 || token1.keyword == RID_INLINE)
7286 && token2.keyword == RID_TEMPLATE)
7287 cp_parser_explicit_instantiation (parser);
7288 /* If the next token is `namespace', check for a named or unnamed
7289 namespace definition. */
7290 else if (token1.keyword == RID_NAMESPACE
7291 && (/* A named namespace definition. */
7292 (token2.type == CPP_NAME
7293 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7295 /* An unnamed namespace definition. */
7296 || token2.type == CPP_OPEN_BRACE
7297 || token2.keyword == RID_ATTRIBUTE))
7298 cp_parser_namespace_definition (parser);
7299 /* Objective-C++ declaration/definition. */
7300 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7301 cp_parser_objc_declaration (parser);
7302 /* We must have either a block declaration or a function
7305 /* Try to parse a block-declaration, or a function-definition. */
7306 cp_parser_block_declaration (parser, /*statement_p=*/false);
7308 /* Free any declarators allocated. */
7309 obstack_free (&declarator_obstack, p);
7312 /* Parse a block-declaration.
7317 namespace-alias-definition
7324 __extension__ block-declaration
7327 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7328 part of a declaration-statement. */
7331 cp_parser_block_declaration (cp_parser *parser,
7337 /* Check for the `__extension__' keyword. */
7338 if (cp_parser_extension_opt (parser, &saved_pedantic))
7340 /* Parse the qualified declaration. */
7341 cp_parser_block_declaration (parser, statement_p);
7342 /* Restore the PEDANTIC flag. */
7343 pedantic = saved_pedantic;
7348 /* Peek at the next token to figure out which kind of declaration is
7350 token1 = cp_lexer_peek_token (parser->lexer);
7352 /* If the next keyword is `asm', we have an asm-definition. */
7353 if (token1->keyword == RID_ASM)
7356 cp_parser_commit_to_tentative_parse (parser);
7357 cp_parser_asm_definition (parser);
7359 /* If the next keyword is `namespace', we have a
7360 namespace-alias-definition. */
7361 else if (token1->keyword == RID_NAMESPACE)
7362 cp_parser_namespace_alias_definition (parser);
7363 /* If the next keyword is `using', we have either a
7364 using-declaration or a using-directive. */
7365 else if (token1->keyword == RID_USING)
7370 cp_parser_commit_to_tentative_parse (parser);
7371 /* If the token after `using' is `namespace', then we have a
7373 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7374 if (token2->keyword == RID_NAMESPACE)
7375 cp_parser_using_directive (parser);
7376 /* Otherwise, it's a using-declaration. */
7378 cp_parser_using_declaration (parser,
7379 /*access_declaration_p=*/false);
7381 /* If the next keyword is `__label__' we have a label declaration. */
7382 else if (token1->keyword == RID_LABEL)
7385 cp_parser_commit_to_tentative_parse (parser);
7386 cp_parser_label_declaration (parser);
7388 /* Anything else must be a simple-declaration. */
7390 cp_parser_simple_declaration (parser, !statement_p);
7393 /* Parse a simple-declaration.
7396 decl-specifier-seq [opt] init-declarator-list [opt] ;
7398 init-declarator-list:
7400 init-declarator-list , init-declarator
7402 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7403 function-definition as a simple-declaration. */
7406 cp_parser_simple_declaration (cp_parser* parser,
7407 bool function_definition_allowed_p)
7409 cp_decl_specifier_seq decl_specifiers;
7410 int declares_class_or_enum;
7411 bool saw_declarator;
7413 /* Defer access checks until we know what is being declared; the
7414 checks for names appearing in the decl-specifier-seq should be
7415 done as if we were in the scope of the thing being declared. */
7416 push_deferring_access_checks (dk_deferred);
7418 /* Parse the decl-specifier-seq. We have to keep track of whether
7419 or not the decl-specifier-seq declares a named class or
7420 enumeration type, since that is the only case in which the
7421 init-declarator-list is allowed to be empty.
7425 In a simple-declaration, the optional init-declarator-list can be
7426 omitted only when declaring a class or enumeration, that is when
7427 the decl-specifier-seq contains either a class-specifier, an
7428 elaborated-type-specifier, or an enum-specifier. */
7429 cp_parser_decl_specifier_seq (parser,
7430 CP_PARSER_FLAGS_OPTIONAL,
7432 &declares_class_or_enum);
7433 /* We no longer need to defer access checks. */
7434 stop_deferring_access_checks ();
7436 /* In a block scope, a valid declaration must always have a
7437 decl-specifier-seq. By not trying to parse declarators, we can
7438 resolve the declaration/expression ambiguity more quickly. */
7439 if (!function_definition_allowed_p
7440 && !decl_specifiers.any_specifiers_p)
7442 cp_parser_error (parser, "expected declaration");
7446 /* If the next two tokens are both identifiers, the code is
7447 erroneous. The usual cause of this situation is code like:
7451 where "T" should name a type -- but does not. */
7452 if (!decl_specifiers.type
7453 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7455 /* If parsing tentatively, we should commit; we really are
7456 looking at a declaration. */
7457 cp_parser_commit_to_tentative_parse (parser);
7462 /* If we have seen at least one decl-specifier, and the next token
7463 is not a parenthesis, then we must be looking at a declaration.
7464 (After "int (" we might be looking at a functional cast.) */
7465 if (decl_specifiers.any_specifiers_p
7466 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7467 cp_parser_commit_to_tentative_parse (parser);
7469 /* Keep going until we hit the `;' at the end of the simple
7471 saw_declarator = false;
7472 while (cp_lexer_next_token_is_not (parser->lexer,
7476 bool function_definition_p;
7481 /* If we are processing next declarator, coma is expected */
7482 token = cp_lexer_peek_token (parser->lexer);
7483 gcc_assert (token->type == CPP_COMMA);
7484 cp_lexer_consume_token (parser->lexer);
7487 saw_declarator = true;
7489 /* Parse the init-declarator. */
7490 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7492 function_definition_allowed_p,
7494 declares_class_or_enum,
7495 &function_definition_p);
7496 /* If an error occurred while parsing tentatively, exit quickly.
7497 (That usually happens when in the body of a function; each
7498 statement is treated as a declaration-statement until proven
7500 if (cp_parser_error_occurred (parser))
7502 /* Handle function definitions specially. */
7503 if (function_definition_p)
7505 /* If the next token is a `,', then we are probably
7506 processing something like:
7510 which is erroneous. */
7511 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7512 error ("mixing declarations and function-definitions is forbidden");
7513 /* Otherwise, we're done with the list of declarators. */
7516 pop_deferring_access_checks ();
7520 /* The next token should be either a `,' or a `;'. */
7521 token = cp_lexer_peek_token (parser->lexer);
7522 /* If it's a `,', there are more declarators to come. */
7523 if (token->type == CPP_COMMA)
7524 /* will be consumed next time around */;
7525 /* If it's a `;', we are done. */
7526 else if (token->type == CPP_SEMICOLON)
7528 /* Anything else is an error. */
7531 /* If we have already issued an error message we don't need
7532 to issue another one. */
7533 if (decl != error_mark_node
7534 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7535 cp_parser_error (parser, "expected %<,%> or %<;%>");
7536 /* Skip tokens until we reach the end of the statement. */
7537 cp_parser_skip_to_end_of_statement (parser);
7538 /* If the next token is now a `;', consume it. */
7539 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7540 cp_lexer_consume_token (parser->lexer);
7543 /* After the first time around, a function-definition is not
7544 allowed -- even if it was OK at first. For example:
7549 function_definition_allowed_p = false;
7552 /* Issue an error message if no declarators are present, and the
7553 decl-specifier-seq does not itself declare a class or
7555 if (!saw_declarator)
7557 if (cp_parser_declares_only_class_p (parser))
7558 shadow_tag (&decl_specifiers);
7559 /* Perform any deferred access checks. */
7560 perform_deferred_access_checks ();
7563 /* Consume the `;'. */
7564 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7567 pop_deferring_access_checks ();
7570 /* Parse a decl-specifier-seq.
7573 decl-specifier-seq [opt] decl-specifier
7576 storage-class-specifier
7587 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7589 The parser flags FLAGS is used to control type-specifier parsing.
7591 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7594 1: one of the decl-specifiers is an elaborated-type-specifier
7595 (i.e., a type declaration)
7596 2: one of the decl-specifiers is an enum-specifier or a
7597 class-specifier (i.e., a type definition)
7602 cp_parser_decl_specifier_seq (cp_parser* parser,
7603 cp_parser_flags flags,
7604 cp_decl_specifier_seq *decl_specs,
7605 int* declares_class_or_enum)
7607 bool constructor_possible_p = !parser->in_declarator_p;
7609 /* Clear DECL_SPECS. */
7610 clear_decl_specs (decl_specs);
7612 /* Assume no class or enumeration type is declared. */
7613 *declares_class_or_enum = 0;
7615 /* Keep reading specifiers until there are no more to read. */
7619 bool found_decl_spec;
7622 /* Peek at the next token. */
7623 token = cp_lexer_peek_token (parser->lexer);
7624 /* Handle attributes. */
7625 if (token->keyword == RID_ATTRIBUTE)
7627 /* Parse the attributes. */
7628 decl_specs->attributes
7629 = chainon (decl_specs->attributes,
7630 cp_parser_attributes_opt (parser));
7633 /* Assume we will find a decl-specifier keyword. */
7634 found_decl_spec = true;
7635 /* If the next token is an appropriate keyword, we can simply
7636 add it to the list. */
7637 switch (token->keyword)
7642 if (!at_class_scope_p ())
7644 error ("%<friend%> used outside of class");
7645 cp_lexer_purge_token (parser->lexer);
7649 ++decl_specs->specs[(int) ds_friend];
7650 /* Consume the token. */
7651 cp_lexer_consume_token (parser->lexer);
7655 /* function-specifier:
7662 cp_parser_function_specifier_opt (parser, decl_specs);
7668 ++decl_specs->specs[(int) ds_typedef];
7669 /* Consume the token. */
7670 cp_lexer_consume_token (parser->lexer);
7671 /* A constructor declarator cannot appear in a typedef. */
7672 constructor_possible_p = false;
7673 /* The "typedef" keyword can only occur in a declaration; we
7674 may as well commit at this point. */
7675 cp_parser_commit_to_tentative_parse (parser);
7677 if (decl_specs->storage_class != sc_none)
7678 decl_specs->conflicting_specifiers_p = true;
7681 /* storage-class-specifier:
7695 /* Consume the token. */
7696 cp_lexer_consume_token (parser->lexer);
7697 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7700 /* Consume the token. */
7701 cp_lexer_consume_token (parser->lexer);
7702 ++decl_specs->specs[(int) ds_thread];
7706 /* We did not yet find a decl-specifier yet. */
7707 found_decl_spec = false;
7711 /* Constructors are a special case. The `S' in `S()' is not a
7712 decl-specifier; it is the beginning of the declarator. */
7715 && constructor_possible_p
7716 && (cp_parser_constructor_declarator_p
7717 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7719 /* If we don't have a DECL_SPEC yet, then we must be looking at
7720 a type-specifier. */
7721 if (!found_decl_spec && !constructor_p)
7723 int decl_spec_declares_class_or_enum;
7724 bool is_cv_qualifier;
7728 = cp_parser_type_specifier (parser, flags,
7730 /*is_declaration=*/true,
7731 &decl_spec_declares_class_or_enum,
7734 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7736 /* If this type-specifier referenced a user-defined type
7737 (a typedef, class-name, etc.), then we can't allow any
7738 more such type-specifiers henceforth.
7742 The longest sequence of decl-specifiers that could
7743 possibly be a type name is taken as the
7744 decl-specifier-seq of a declaration. The sequence shall
7745 be self-consistent as described below.
7749 As a general rule, at most one type-specifier is allowed
7750 in the complete decl-specifier-seq of a declaration. The
7751 only exceptions are the following:
7753 -- const or volatile can be combined with any other
7756 -- signed or unsigned can be combined with char, long,
7764 void g (const int Pc);
7766 Here, Pc is *not* part of the decl-specifier seq; it's
7767 the declarator. Therefore, once we see a type-specifier
7768 (other than a cv-qualifier), we forbid any additional
7769 user-defined types. We *do* still allow things like `int
7770 int' to be considered a decl-specifier-seq, and issue the
7771 error message later. */
7772 if (type_spec && !is_cv_qualifier)
7773 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7774 /* A constructor declarator cannot follow a type-specifier. */
7777 constructor_possible_p = false;
7778 found_decl_spec = true;
7782 /* If we still do not have a DECL_SPEC, then there are no more
7784 if (!found_decl_spec)
7787 decl_specs->any_specifiers_p = true;
7788 /* After we see one decl-specifier, further decl-specifiers are
7790 flags |= CP_PARSER_FLAGS_OPTIONAL;
7793 cp_parser_check_decl_spec (decl_specs);
7795 /* Don't allow a friend specifier with a class definition. */
7796 if (decl_specs->specs[(int) ds_friend] != 0
7797 && (*declares_class_or_enum & 2))
7798 error ("class definition may not be declared a friend");
7801 /* Parse an (optional) storage-class-specifier.
7803 storage-class-specifier:
7812 storage-class-specifier:
7815 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7818 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7820 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7828 /* Consume the token. */
7829 return cp_lexer_consume_token (parser->lexer)->u.value;
7836 /* Parse an (optional) function-specifier.
7843 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7844 Updates DECL_SPECS, if it is non-NULL. */
7847 cp_parser_function_specifier_opt (cp_parser* parser,
7848 cp_decl_specifier_seq *decl_specs)
7850 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7854 ++decl_specs->specs[(int) ds_inline];
7858 /* 14.5.2.3 [temp.mem]
7860 A member function template shall not be virtual. */
7861 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7862 error ("templates may not be %<virtual%>");
7863 else if (decl_specs)
7864 ++decl_specs->specs[(int) ds_virtual];
7869 ++decl_specs->specs[(int) ds_explicit];
7876 /* Consume the token. */
7877 return cp_lexer_consume_token (parser->lexer)->u.value;
7880 /* Parse a linkage-specification.
7882 linkage-specification:
7883 extern string-literal { declaration-seq [opt] }
7884 extern string-literal declaration */
7887 cp_parser_linkage_specification (cp_parser* parser)
7891 /* Look for the `extern' keyword. */
7892 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7894 /* Look for the string-literal. */
7895 linkage = cp_parser_string_literal (parser, false, false);
7897 /* Transform the literal into an identifier. If the literal is a
7898 wide-character string, or contains embedded NULs, then we can't
7899 handle it as the user wants. */
7900 if (strlen (TREE_STRING_POINTER (linkage))
7901 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7903 cp_parser_error (parser, "invalid linkage-specification");
7904 /* Assume C++ linkage. */
7905 linkage = lang_name_cplusplus;
7908 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7910 /* We're now using the new linkage. */
7911 push_lang_context (linkage);
7913 /* If the next token is a `{', then we're using the first
7915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7917 /* Consume the `{' token. */
7918 cp_lexer_consume_token (parser->lexer);
7919 /* Parse the declarations. */
7920 cp_parser_declaration_seq_opt (parser);
7921 /* Look for the closing `}'. */
7922 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7924 /* Otherwise, there's just one declaration. */
7927 bool saved_in_unbraced_linkage_specification_p;
7929 saved_in_unbraced_linkage_specification_p
7930 = parser->in_unbraced_linkage_specification_p;
7931 parser->in_unbraced_linkage_specification_p = true;
7932 cp_parser_declaration (parser);
7933 parser->in_unbraced_linkage_specification_p
7934 = saved_in_unbraced_linkage_specification_p;
7937 /* We're done with the linkage-specification. */
7938 pop_lang_context ();
7941 /* Special member functions [gram.special] */
7943 /* Parse a conversion-function-id.
7945 conversion-function-id:
7946 operator conversion-type-id
7948 Returns an IDENTIFIER_NODE representing the operator. */
7951 cp_parser_conversion_function_id (cp_parser* parser)
7955 tree saved_qualifying_scope;
7956 tree saved_object_scope;
7957 tree pushed_scope = NULL_TREE;
7959 /* Look for the `operator' token. */
7960 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7961 return error_mark_node;
7962 /* When we parse the conversion-type-id, the current scope will be
7963 reset. However, we need that information in able to look up the
7964 conversion function later, so we save it here. */
7965 saved_scope = parser->scope;
7966 saved_qualifying_scope = parser->qualifying_scope;
7967 saved_object_scope = parser->object_scope;
7968 /* We must enter the scope of the class so that the names of
7969 entities declared within the class are available in the
7970 conversion-type-id. For example, consider:
7977 S::operator I() { ... }
7979 In order to see that `I' is a type-name in the definition, we
7980 must be in the scope of `S'. */
7982 pushed_scope = push_scope (saved_scope);
7983 /* Parse the conversion-type-id. */
7984 type = cp_parser_conversion_type_id (parser);
7985 /* Leave the scope of the class, if any. */
7987 pop_scope (pushed_scope);
7988 /* Restore the saved scope. */
7989 parser->scope = saved_scope;
7990 parser->qualifying_scope = saved_qualifying_scope;
7991 parser->object_scope = saved_object_scope;
7992 /* If the TYPE is invalid, indicate failure. */
7993 if (type == error_mark_node)
7994 return error_mark_node;
7995 return mangle_conv_op_name_for_type (type);
7998 /* Parse a conversion-type-id:
8001 type-specifier-seq conversion-declarator [opt]
8003 Returns the TYPE specified. */
8006 cp_parser_conversion_type_id (cp_parser* parser)
8009 cp_decl_specifier_seq type_specifiers;
8010 cp_declarator *declarator;
8011 tree type_specified;
8013 /* Parse the attributes. */
8014 attributes = cp_parser_attributes_opt (parser);
8015 /* Parse the type-specifiers. */
8016 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8018 /* If that didn't work, stop. */
8019 if (type_specifiers.type == error_mark_node)
8020 return error_mark_node;
8021 /* Parse the conversion-declarator. */
8022 declarator = cp_parser_conversion_declarator_opt (parser);
8024 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8025 /*initialized=*/0, &attributes);
8027 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8028 return type_specified;
8031 /* Parse an (optional) conversion-declarator.
8033 conversion-declarator:
8034 ptr-operator conversion-declarator [opt]
8038 static cp_declarator *
8039 cp_parser_conversion_declarator_opt (cp_parser* parser)
8041 enum tree_code code;
8043 cp_cv_quals cv_quals;
8045 /* We don't know if there's a ptr-operator next, or not. */
8046 cp_parser_parse_tentatively (parser);
8047 /* Try the ptr-operator. */
8048 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8049 /* If it worked, look for more conversion-declarators. */
8050 if (cp_parser_parse_definitely (parser))
8052 cp_declarator *declarator;
8054 /* Parse another optional declarator. */
8055 declarator = cp_parser_conversion_declarator_opt (parser);
8057 /* Create the representation of the declarator. */
8059 declarator = make_ptrmem_declarator (cv_quals, class_type,
8061 else if (code == INDIRECT_REF)
8062 declarator = make_pointer_declarator (cv_quals, declarator);
8064 declarator = make_reference_declarator (cv_quals, declarator);
8072 /* Parse an (optional) ctor-initializer.
8075 : mem-initializer-list
8077 Returns TRUE iff the ctor-initializer was actually present. */
8080 cp_parser_ctor_initializer_opt (cp_parser* parser)
8082 /* If the next token is not a `:', then there is no
8083 ctor-initializer. */
8084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8086 /* Do default initialization of any bases and members. */
8087 if (DECL_CONSTRUCTOR_P (current_function_decl))
8088 finish_mem_initializers (NULL_TREE);
8093 /* Consume the `:' token. */
8094 cp_lexer_consume_token (parser->lexer);
8095 /* And the mem-initializer-list. */
8096 cp_parser_mem_initializer_list (parser);
8101 /* Parse a mem-initializer-list.
8103 mem-initializer-list:
8105 mem-initializer , mem-initializer-list */
8108 cp_parser_mem_initializer_list (cp_parser* parser)
8110 tree mem_initializer_list = NULL_TREE;
8112 /* Let the semantic analysis code know that we are starting the
8113 mem-initializer-list. */
8114 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8115 error ("only constructors take base initializers");
8117 /* Loop through the list. */
8120 tree mem_initializer;
8122 /* Parse the mem-initializer. */
8123 mem_initializer = cp_parser_mem_initializer (parser);
8124 /* Add it to the list, unless it was erroneous. */
8125 if (mem_initializer != error_mark_node)
8127 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8128 mem_initializer_list = mem_initializer;
8130 /* If the next token is not a `,', we're done. */
8131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8133 /* Consume the `,' token. */
8134 cp_lexer_consume_token (parser->lexer);
8137 /* Perform semantic analysis. */
8138 if (DECL_CONSTRUCTOR_P (current_function_decl))
8139 finish_mem_initializers (mem_initializer_list);
8142 /* Parse a mem-initializer.
8145 mem-initializer-id ( expression-list [opt] )
8150 ( expression-list [opt] )
8152 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8153 class) or FIELD_DECL (for a non-static data member) to initialize;
8154 the TREE_VALUE is the expression-list. An empty initialization
8155 list is represented by void_list_node. */
8158 cp_parser_mem_initializer (cp_parser* parser)
8160 tree mem_initializer_id;
8161 tree expression_list;
8164 /* Find out what is being initialized. */
8165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8167 pedwarn ("anachronistic old-style base class initializer");
8168 mem_initializer_id = NULL_TREE;
8171 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8172 member = expand_member_init (mem_initializer_id);
8173 if (member && !DECL_P (member))
8174 in_base_initializer = 1;
8177 = cp_parser_parenthesized_expression_list (parser, false,
8179 /*non_constant_p=*/NULL);
8180 if (expression_list == error_mark_node)
8181 return error_mark_node;
8182 if (!expression_list)
8183 expression_list = void_type_node;
8185 in_base_initializer = 0;
8187 return member ? build_tree_list (member, expression_list) : error_mark_node;
8190 /* Parse a mem-initializer-id.
8193 :: [opt] nested-name-specifier [opt] class-name
8196 Returns a TYPE indicating the class to be initializer for the first
8197 production. Returns an IDENTIFIER_NODE indicating the data member
8198 to be initialized for the second production. */
8201 cp_parser_mem_initializer_id (cp_parser* parser)
8203 bool global_scope_p;
8204 bool nested_name_specifier_p;
8205 bool template_p = false;
8208 /* `typename' is not allowed in this context ([temp.res]). */
8209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8211 error ("keyword %<typename%> not allowed in this context (a qualified "
8212 "member initializer is implicitly a type)");
8213 cp_lexer_consume_token (parser->lexer);
8215 /* Look for the optional `::' operator. */
8217 = (cp_parser_global_scope_opt (parser,
8218 /*current_scope_valid_p=*/false)
8220 /* Look for the optional nested-name-specifier. The simplest way to
8225 The keyword `typename' is not permitted in a base-specifier or
8226 mem-initializer; in these contexts a qualified name that
8227 depends on a template-parameter is implicitly assumed to be a
8230 is to assume that we have seen the `typename' keyword at this
8232 nested_name_specifier_p
8233 = (cp_parser_nested_name_specifier_opt (parser,
8234 /*typename_keyword_p=*/true,
8235 /*check_dependency_p=*/true,
8237 /*is_declaration=*/true)
8239 if (nested_name_specifier_p)
8240 template_p = cp_parser_optional_template_keyword (parser);
8241 /* If there is a `::' operator or a nested-name-specifier, then we
8242 are definitely looking for a class-name. */
8243 if (global_scope_p || nested_name_specifier_p)
8244 return cp_parser_class_name (parser,
8245 /*typename_keyword_p=*/true,
8246 /*template_keyword_p=*/template_p,
8248 /*check_dependency_p=*/true,
8249 /*class_head_p=*/false,
8250 /*is_declaration=*/true);
8251 /* Otherwise, we could also be looking for an ordinary identifier. */
8252 cp_parser_parse_tentatively (parser);
8253 /* Try a class-name. */
8254 id = cp_parser_class_name (parser,
8255 /*typename_keyword_p=*/true,
8256 /*template_keyword_p=*/false,
8258 /*check_dependency_p=*/true,
8259 /*class_head_p=*/false,
8260 /*is_declaration=*/true);
8261 /* If we found one, we're done. */
8262 if (cp_parser_parse_definitely (parser))
8264 /* Otherwise, look for an ordinary identifier. */
8265 return cp_parser_identifier (parser);
8268 /* Overloading [gram.over] */
8270 /* Parse an operator-function-id.
8272 operator-function-id:
8275 Returns an IDENTIFIER_NODE for the operator which is a
8276 human-readable spelling of the identifier, e.g., `operator +'. */
8279 cp_parser_operator_function_id (cp_parser* parser)
8281 /* Look for the `operator' keyword. */
8282 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8283 return error_mark_node;
8284 /* And then the name of the operator itself. */
8285 return cp_parser_operator (parser);
8288 /* Parse an operator.
8291 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8292 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8293 || ++ -- , ->* -> () []
8300 Returns an IDENTIFIER_NODE for the operator which is a
8301 human-readable spelling of the identifier, e.g., `operator +'. */
8304 cp_parser_operator (cp_parser* parser)
8306 tree id = NULL_TREE;
8309 /* Peek at the next token. */
8310 token = cp_lexer_peek_token (parser->lexer);
8311 /* Figure out which operator we have. */
8312 switch (token->type)
8318 /* The keyword should be either `new' or `delete'. */
8319 if (token->keyword == RID_NEW)
8321 else if (token->keyword == RID_DELETE)
8326 /* Consume the `new' or `delete' token. */
8327 cp_lexer_consume_token (parser->lexer);
8329 /* Peek at the next token. */
8330 token = cp_lexer_peek_token (parser->lexer);
8331 /* If it's a `[' token then this is the array variant of the
8333 if (token->type == CPP_OPEN_SQUARE)
8335 /* Consume the `[' token. */
8336 cp_lexer_consume_token (parser->lexer);
8337 /* Look for the `]' token. */
8338 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8339 id = ansi_opname (op == NEW_EXPR
8340 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8342 /* Otherwise, we have the non-array variant. */
8344 id = ansi_opname (op);
8350 id = ansi_opname (PLUS_EXPR);
8354 id = ansi_opname (MINUS_EXPR);
8358 id = ansi_opname (MULT_EXPR);
8362 id = ansi_opname (TRUNC_DIV_EXPR);
8366 id = ansi_opname (TRUNC_MOD_EXPR);
8370 id = ansi_opname (BIT_XOR_EXPR);
8374 id = ansi_opname (BIT_AND_EXPR);
8378 id = ansi_opname (BIT_IOR_EXPR);
8382 id = ansi_opname (BIT_NOT_EXPR);
8386 id = ansi_opname (TRUTH_NOT_EXPR);
8390 id = ansi_assopname (NOP_EXPR);
8394 id = ansi_opname (LT_EXPR);
8398 id = ansi_opname (GT_EXPR);
8402 id = ansi_assopname (PLUS_EXPR);
8406 id = ansi_assopname (MINUS_EXPR);
8410 id = ansi_assopname (MULT_EXPR);
8414 id = ansi_assopname (TRUNC_DIV_EXPR);
8418 id = ansi_assopname (TRUNC_MOD_EXPR);
8422 id = ansi_assopname (BIT_XOR_EXPR);
8426 id = ansi_assopname (BIT_AND_EXPR);
8430 id = ansi_assopname (BIT_IOR_EXPR);
8434 id = ansi_opname (LSHIFT_EXPR);
8438 id = ansi_opname (RSHIFT_EXPR);
8442 id = ansi_assopname (LSHIFT_EXPR);
8446 id = ansi_assopname (RSHIFT_EXPR);
8450 id = ansi_opname (EQ_EXPR);
8454 id = ansi_opname (NE_EXPR);
8458 id = ansi_opname (LE_EXPR);
8461 case CPP_GREATER_EQ:
8462 id = ansi_opname (GE_EXPR);
8466 id = ansi_opname (TRUTH_ANDIF_EXPR);
8470 id = ansi_opname (TRUTH_ORIF_EXPR);
8474 id = ansi_opname (POSTINCREMENT_EXPR);
8477 case CPP_MINUS_MINUS:
8478 id = ansi_opname (PREDECREMENT_EXPR);
8482 id = ansi_opname (COMPOUND_EXPR);
8485 case CPP_DEREF_STAR:
8486 id = ansi_opname (MEMBER_REF);
8490 id = ansi_opname (COMPONENT_REF);
8493 case CPP_OPEN_PAREN:
8494 /* Consume the `('. */
8495 cp_lexer_consume_token (parser->lexer);
8496 /* Look for the matching `)'. */
8497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8498 return ansi_opname (CALL_EXPR);
8500 case CPP_OPEN_SQUARE:
8501 /* Consume the `['. */
8502 cp_lexer_consume_token (parser->lexer);
8503 /* Look for the matching `]'. */
8504 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8505 return ansi_opname (ARRAY_REF);
8508 /* Anything else is an error. */
8512 /* If we have selected an identifier, we need to consume the
8515 cp_lexer_consume_token (parser->lexer);
8516 /* Otherwise, no valid operator name was present. */
8519 cp_parser_error (parser, "expected operator");
8520 id = error_mark_node;
8526 /* Parse a template-declaration.
8528 template-declaration:
8529 export [opt] template < template-parameter-list > declaration
8531 If MEMBER_P is TRUE, this template-declaration occurs within a
8534 The grammar rule given by the standard isn't correct. What
8537 template-declaration:
8538 export [opt] template-parameter-list-seq
8539 decl-specifier-seq [opt] init-declarator [opt] ;
8540 export [opt] template-parameter-list-seq
8543 template-parameter-list-seq:
8544 template-parameter-list-seq [opt]
8545 template < template-parameter-list > */
8548 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8550 /* Check for `export'. */
8551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8553 /* Consume the `export' token. */
8554 cp_lexer_consume_token (parser->lexer);
8555 /* Warn that we do not support `export'. */
8556 warning (0, "keyword %<export%> not implemented, and will be ignored");
8559 cp_parser_template_declaration_after_export (parser, member_p);
8562 /* Parse a template-parameter-list.
8564 template-parameter-list:
8566 template-parameter-list , template-parameter
8568 Returns a TREE_LIST. Each node represents a template parameter.
8569 The nodes are connected via their TREE_CHAINs. */
8572 cp_parser_template_parameter_list (cp_parser* parser)
8574 tree parameter_list = NULL_TREE;
8576 begin_template_parm_list ();
8583 /* Parse the template-parameter. */
8584 parameter = cp_parser_template_parameter (parser, &is_non_type);
8585 /* Add it to the list. */
8586 if (parameter != error_mark_node)
8587 parameter_list = process_template_parm (parameter_list,
8592 tree err_parm = build_tree_list (parameter, parameter);
8593 TREE_VALUE (err_parm) = error_mark_node;
8594 parameter_list = chainon (parameter_list, err_parm);
8597 /* Peek at the next token. */
8598 token = cp_lexer_peek_token (parser->lexer);
8599 /* If it's not a `,', we're done. */
8600 if (token->type != CPP_COMMA)
8602 /* Otherwise, consume the `,' token. */
8603 cp_lexer_consume_token (parser->lexer);
8606 return end_template_parm_list (parameter_list);
8609 /* Parse a template-parameter.
8613 parameter-declaration
8615 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8616 the parameter. The TREE_PURPOSE is the default value, if any.
8617 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8618 iff this parameter is a non-type parameter. */
8621 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8624 cp_parameter_declarator *parameter_declarator;
8627 /* Assume it is a type parameter or a template parameter. */
8628 *is_non_type = false;
8629 /* Peek at the next token. */
8630 token = cp_lexer_peek_token (parser->lexer);
8631 /* If it is `class' or `template', we have a type-parameter. */
8632 if (token->keyword == RID_TEMPLATE)
8633 return cp_parser_type_parameter (parser);
8634 /* If it is `class' or `typename' we do not know yet whether it is a
8635 type parameter or a non-type parameter. Consider:
8637 template <typename T, typename T::X X> ...
8641 template <class C, class D*> ...
8643 Here, the first parameter is a type parameter, and the second is
8644 a non-type parameter. We can tell by looking at the token after
8645 the identifier -- if it is a `,', `=', or `>' then we have a type
8647 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8649 /* Peek at the token after `class' or `typename'. */
8650 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8651 /* If it's an identifier, skip it. */
8652 if (token->type == CPP_NAME)
8653 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8654 /* Now, see if the token looks like the end of a template
8656 if (token->type == CPP_COMMA
8657 || token->type == CPP_EQ
8658 || token->type == CPP_GREATER)
8659 return cp_parser_type_parameter (parser);
8662 /* Otherwise, it is a non-type parameter.
8666 When parsing a default template-argument for a non-type
8667 template-parameter, the first non-nested `>' is taken as the end
8668 of the template parameter-list rather than a greater-than
8670 *is_non_type = true;
8671 parameter_declarator
8672 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8673 /*parenthesized_p=*/NULL);
8674 parm = grokdeclarator (parameter_declarator->declarator,
8675 ¶meter_declarator->decl_specifiers,
8676 PARM, /*initialized=*/0,
8678 if (parm == error_mark_node)
8679 return error_mark_node;
8680 return build_tree_list (parameter_declarator->default_argument, parm);
8683 /* Parse a type-parameter.
8686 class identifier [opt]
8687 class identifier [opt] = type-id
8688 typename identifier [opt]
8689 typename identifier [opt] = type-id
8690 template < template-parameter-list > class identifier [opt]
8691 template < template-parameter-list > class identifier [opt]
8694 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8695 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8696 the declaration of the parameter. */
8699 cp_parser_type_parameter (cp_parser* parser)
8704 /* Look for a keyword to tell us what kind of parameter this is. */
8705 token = cp_parser_require (parser, CPP_KEYWORD,
8706 "`class', `typename', or `template'");
8708 return error_mark_node;
8710 switch (token->keyword)
8716 tree default_argument;
8718 /* If the next token is an identifier, then it names the
8720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8721 identifier = cp_parser_identifier (parser);
8723 identifier = NULL_TREE;
8725 /* Create the parameter. */
8726 parameter = finish_template_type_parm (class_type_node, identifier);
8728 /* If the next token is an `=', we have a default argument. */
8729 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8731 /* Consume the `=' token. */
8732 cp_lexer_consume_token (parser->lexer);
8733 /* Parse the default-argument. */
8734 push_deferring_access_checks (dk_no_deferred);
8735 default_argument = cp_parser_type_id (parser);
8736 pop_deferring_access_checks ();
8739 default_argument = NULL_TREE;
8741 /* Create the combined representation of the parameter and the
8742 default argument. */
8743 parameter = build_tree_list (default_argument, parameter);
8749 tree parameter_list;
8751 tree default_argument;
8753 /* Look for the `<'. */
8754 cp_parser_require (parser, CPP_LESS, "`<'");
8755 /* Parse the template-parameter-list. */
8756 parameter_list = cp_parser_template_parameter_list (parser);
8757 /* Look for the `>'. */
8758 cp_parser_require (parser, CPP_GREATER, "`>'");
8759 /* Look for the `class' keyword. */
8760 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8761 /* If the next token is an `=', then there is a
8762 default-argument. If the next token is a `>', we are at
8763 the end of the parameter-list. If the next token is a `,',
8764 then we are at the end of this parameter. */
8765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8766 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8767 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8769 identifier = cp_parser_identifier (parser);
8770 /* Treat invalid names as if the parameter were nameless. */
8771 if (identifier == error_mark_node)
8772 identifier = NULL_TREE;
8775 identifier = NULL_TREE;
8777 /* Create the template parameter. */
8778 parameter = finish_template_template_parm (class_type_node,
8781 /* If the next token is an `=', then there is a
8782 default-argument. */
8783 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8787 /* Consume the `='. */
8788 cp_lexer_consume_token (parser->lexer);
8789 /* Parse the id-expression. */
8790 push_deferring_access_checks (dk_no_deferred);
8792 = cp_parser_id_expression (parser,
8793 /*template_keyword_p=*/false,
8794 /*check_dependency_p=*/true,
8795 /*template_p=*/&is_template,
8796 /*declarator_p=*/false,
8797 /*optional_p=*/false);
8798 if (TREE_CODE (default_argument) == TYPE_DECL)
8799 /* If the id-expression was a template-id that refers to
8800 a template-class, we already have the declaration here,
8801 so no further lookup is needed. */
8804 /* Look up the name. */
8806 = cp_parser_lookup_name (parser, default_argument,
8808 /*is_template=*/is_template,
8809 /*is_namespace=*/false,
8810 /*check_dependency=*/true,
8811 /*ambiguous_decls=*/NULL);
8812 /* See if the default argument is valid. */
8814 = check_template_template_default_arg (default_argument);
8815 pop_deferring_access_checks ();
8818 default_argument = NULL_TREE;
8820 /* Create the combined representation of the parameter and the
8821 default argument. */
8822 parameter = build_tree_list (default_argument, parameter);
8834 /* Parse a template-id.
8837 template-name < template-argument-list [opt] >
8839 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8840 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8841 returned. Otherwise, if the template-name names a function, or set
8842 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8843 names a class, returns a TYPE_DECL for the specialization.
8845 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8846 uninstantiated templates. */
8849 cp_parser_template_id (cp_parser *parser,
8850 bool template_keyword_p,
8851 bool check_dependency_p,
8852 bool is_declaration)
8858 cp_token_position start_of_id = 0;
8859 deferred_access_check *chk;
8860 VEC (deferred_access_check,gc) *access_check;
8861 cp_token *next_token, *next_token_2;
8864 /* If the next token corresponds to a template-id, there is no need
8866 next_token = cp_lexer_peek_token (parser->lexer);
8867 if (next_token->type == CPP_TEMPLATE_ID)
8869 struct tree_check *check_value;
8871 /* Get the stored value. */
8872 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8873 /* Perform any access checks that were deferred. */
8874 access_check = check_value->checks;
8878 VEC_iterate (deferred_access_check, access_check, i, chk) ;
8881 perform_or_defer_access_check (chk->binfo,
8886 /* Return the stored value. */
8887 return check_value->value;
8890 /* Avoid performing name lookup if there is no possibility of
8891 finding a template-id. */
8892 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8893 || (next_token->type == CPP_NAME
8894 && !cp_parser_nth_token_starts_template_argument_list_p
8897 cp_parser_error (parser, "expected template-id");
8898 return error_mark_node;
8901 /* Remember where the template-id starts. */
8902 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8903 start_of_id = cp_lexer_token_position (parser->lexer, false);
8905 push_deferring_access_checks (dk_deferred);
8907 /* Parse the template-name. */
8908 is_identifier = false;
8909 template = cp_parser_template_name (parser, template_keyword_p,
8913 if (template == error_mark_node || is_identifier)
8915 pop_deferring_access_checks ();
8919 /* If we find the sequence `[:' after a template-name, it's probably
8920 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8921 parse correctly the argument list. */
8922 next_token = cp_lexer_peek_token (parser->lexer);
8923 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8924 if (next_token->type == CPP_OPEN_SQUARE
8925 && next_token->flags & DIGRAPH
8926 && next_token_2->type == CPP_COLON
8927 && !(next_token_2->flags & PREV_WHITE))
8929 cp_parser_parse_tentatively (parser);
8930 /* Change `:' into `::'. */
8931 next_token_2->type = CPP_SCOPE;
8932 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8934 cp_lexer_consume_token (parser->lexer);
8935 /* Parse the arguments. */
8936 arguments = cp_parser_enclosed_template_argument_list (parser);
8937 if (!cp_parser_parse_definitely (parser))
8939 /* If we couldn't parse an argument list, then we revert our changes
8940 and return simply an error. Maybe this is not a template-id
8942 next_token_2->type = CPP_COLON;
8943 cp_parser_error (parser, "expected %<<%>");
8944 pop_deferring_access_checks ();
8945 return error_mark_node;
8947 /* Otherwise, emit an error about the invalid digraph, but continue
8948 parsing because we got our argument list. */
8949 pedwarn ("%<<::%> cannot begin a template-argument list");
8950 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8951 "between %<<%> and %<::%>");
8952 if (!flag_permissive)
8957 inform ("(if you use -fpermissive G++ will accept your code)");
8964 /* Look for the `<' that starts the template-argument-list. */
8965 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8967 pop_deferring_access_checks ();
8968 return error_mark_node;
8970 /* Parse the arguments. */
8971 arguments = cp_parser_enclosed_template_argument_list (parser);
8974 /* Build a representation of the specialization. */
8975 if (TREE_CODE (template) == IDENTIFIER_NODE)
8976 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8977 else if (DECL_CLASS_TEMPLATE_P (template)
8978 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8980 bool entering_scope;
8981 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8982 template (rather than some instantiation thereof) only if
8983 is not nested within some other construct. For example, in
8984 "template <typename T> void f(T) { A<T>::", A<T> is just an
8985 instantiation of A. */
8986 entering_scope = (template_parm_scope_p ()
8987 && cp_lexer_next_token_is (parser->lexer,
8990 = finish_template_type (template, arguments, entering_scope);
8994 /* If it's not a class-template or a template-template, it should be
8995 a function-template. */
8996 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8997 || TREE_CODE (template) == OVERLOAD
8998 || BASELINK_P (template)));
9000 template_id = lookup_template_function (template, arguments);
9003 /* If parsing tentatively, replace the sequence of tokens that makes
9004 up the template-id with a CPP_TEMPLATE_ID token. That way,
9005 should we re-parse the token stream, we will not have to repeat
9006 the effort required to do the parse, nor will we issue duplicate
9007 error messages about problems during instantiation of the
9011 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9013 /* Reset the contents of the START_OF_ID token. */
9014 token->type = CPP_TEMPLATE_ID;
9015 /* Retrieve any deferred checks. Do not pop this access checks yet
9016 so the memory will not be reclaimed during token replacing below. */
9017 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9018 token->u.tree_check_value->value = template_id;
9019 token->u.tree_check_value->checks = get_deferred_access_checks ();
9020 token->keyword = RID_MAX;
9022 /* Purge all subsequent tokens. */
9023 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9025 /* ??? Can we actually assume that, if template_id ==
9026 error_mark_node, we will have issued a diagnostic to the
9027 user, as opposed to simply marking the tentative parse as
9029 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9030 error ("parse error in template argument list");
9033 pop_deferring_access_checks ();
9037 /* Parse a template-name.
9042 The standard should actually say:
9046 operator-function-id
9048 A defect report has been filed about this issue.
9050 A conversion-function-id cannot be a template name because they cannot
9051 be part of a template-id. In fact, looking at this code:
9055 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9056 It is impossible to call a templated conversion-function-id with an
9057 explicit argument list, since the only allowed template parameter is
9058 the type to which it is converting.
9060 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9061 `template' keyword, in a construction like:
9065 In that case `f' is taken to be a template-name, even though there
9066 is no way of knowing for sure.
9068 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9069 name refers to a set of overloaded functions, at least one of which
9070 is a template, or an IDENTIFIER_NODE with the name of the template,
9071 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9072 names are looked up inside uninstantiated templates. */
9075 cp_parser_template_name (cp_parser* parser,
9076 bool template_keyword_p,
9077 bool check_dependency_p,
9078 bool is_declaration,
9079 bool *is_identifier)
9085 /* If the next token is `operator', then we have either an
9086 operator-function-id or a conversion-function-id. */
9087 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9089 /* We don't know whether we're looking at an
9090 operator-function-id or a conversion-function-id. */
9091 cp_parser_parse_tentatively (parser);
9092 /* Try an operator-function-id. */
9093 identifier = cp_parser_operator_function_id (parser);
9094 /* If that didn't work, try a conversion-function-id. */
9095 if (!cp_parser_parse_definitely (parser))
9097 cp_parser_error (parser, "expected template-name");
9098 return error_mark_node;
9101 /* Look for the identifier. */
9103 identifier = cp_parser_identifier (parser);
9105 /* If we didn't find an identifier, we don't have a template-id. */
9106 if (identifier == error_mark_node)
9107 return error_mark_node;
9109 /* If the name immediately followed the `template' keyword, then it
9110 is a template-name. However, if the next token is not `<', then
9111 we do not treat it as a template-name, since it is not being used
9112 as part of a template-id. This enables us to handle constructs
9115 template <typename T> struct S { S(); };
9116 template <typename T> S<T>::S();
9118 correctly. We would treat `S' as a template -- if it were `S<T>'
9119 -- but we do not if there is no `<'. */
9121 if (processing_template_decl
9122 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9124 /* In a declaration, in a dependent context, we pretend that the
9125 "template" keyword was present in order to improve error
9126 recovery. For example, given:
9128 template <typename T> void f(T::X<int>);
9130 we want to treat "X<int>" as a template-id. */
9132 && !template_keyword_p
9133 && parser->scope && TYPE_P (parser->scope)
9134 && check_dependency_p
9135 && dependent_type_p (parser->scope)
9136 /* Do not do this for dtors (or ctors), since they never
9137 need the template keyword before their name. */
9138 && !constructor_name_p (identifier, parser->scope))
9140 cp_token_position start = 0;
9142 /* Explain what went wrong. */
9143 error ("non-template %qD used as template", identifier);
9144 inform ("use %<%T::template %D%> to indicate that it is a template",
9145 parser->scope, identifier);
9146 /* If parsing tentatively, find the location of the "<" token. */
9147 if (cp_parser_simulate_error (parser))
9148 start = cp_lexer_token_position (parser->lexer, true);
9149 /* Parse the template arguments so that we can issue error
9150 messages about them. */
9151 cp_lexer_consume_token (parser->lexer);
9152 cp_parser_enclosed_template_argument_list (parser);
9153 /* Skip tokens until we find a good place from which to
9154 continue parsing. */
9155 cp_parser_skip_to_closing_parenthesis (parser,
9156 /*recovering=*/true,
9158 /*consume_paren=*/false);
9159 /* If parsing tentatively, permanently remove the
9160 template argument list. That will prevent duplicate
9161 error messages from being issued about the missing
9162 "template" keyword. */
9164 cp_lexer_purge_tokens_after (parser->lexer, start);
9166 *is_identifier = true;
9170 /* If the "template" keyword is present, then there is generally
9171 no point in doing name-lookup, so we just return IDENTIFIER.
9172 But, if the qualifying scope is non-dependent then we can
9173 (and must) do name-lookup normally. */
9174 if (template_keyword_p
9176 || (TYPE_P (parser->scope)
9177 && dependent_type_p (parser->scope))))
9181 /* Look up the name. */
9182 decl = cp_parser_lookup_name (parser, identifier,
9184 /*is_template=*/false,
9185 /*is_namespace=*/false,
9187 /*ambiguous_decls=*/NULL);
9188 decl = maybe_get_template_decl_from_type_decl (decl);
9190 /* If DECL is a template, then the name was a template-name. */
9191 if (TREE_CODE (decl) == TEMPLATE_DECL)
9195 tree fn = NULL_TREE;
9197 /* The standard does not explicitly indicate whether a name that
9198 names a set of overloaded declarations, some of which are
9199 templates, is a template-name. However, such a name should
9200 be a template-name; otherwise, there is no way to form a
9201 template-id for the overloaded templates. */
9202 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9203 if (TREE_CODE (fns) == OVERLOAD)
9204 for (fn = fns; fn; fn = OVL_NEXT (fn))
9205 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9210 /* The name does not name a template. */
9211 cp_parser_error (parser, "expected template-name");
9212 return error_mark_node;
9216 /* If DECL is dependent, and refers to a function, then just return
9217 its name; we will look it up again during template instantiation. */
9218 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9220 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9221 if (TYPE_P (scope) && dependent_type_p (scope))
9228 /* Parse a template-argument-list.
9230 template-argument-list:
9232 template-argument-list , template-argument
9234 Returns a TREE_VEC containing the arguments. */
9237 cp_parser_template_argument_list (cp_parser* parser)
9239 tree fixed_args[10];
9240 unsigned n_args = 0;
9241 unsigned alloced = 10;
9242 tree *arg_ary = fixed_args;
9244 bool saved_in_template_argument_list_p;
9246 bool saved_non_ice_p;
9248 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9249 parser->in_template_argument_list_p = true;
9250 /* Even if the template-id appears in an integral
9251 constant-expression, the contents of the argument list do
9253 saved_ice_p = parser->integral_constant_expression_p;
9254 parser->integral_constant_expression_p = false;
9255 saved_non_ice_p = parser->non_integral_constant_expression_p;
9256 parser->non_integral_constant_expression_p = false;
9257 /* Parse the arguments. */
9263 /* Consume the comma. */
9264 cp_lexer_consume_token (parser->lexer);
9266 /* Parse the template-argument. */
9267 argument = cp_parser_template_argument (parser);
9268 if (n_args == alloced)
9272 if (arg_ary == fixed_args)
9274 arg_ary = XNEWVEC (tree, alloced);
9275 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9278 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9280 arg_ary[n_args++] = argument;
9282 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9284 vec = make_tree_vec (n_args);
9287 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9289 if (arg_ary != fixed_args)
9291 parser->non_integral_constant_expression_p = saved_non_ice_p;
9292 parser->integral_constant_expression_p = saved_ice_p;
9293 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9297 /* Parse a template-argument.
9300 assignment-expression
9304 The representation is that of an assignment-expression, type-id, or
9305 id-expression -- except that the qualified id-expression is
9306 evaluated, so that the value returned is either a DECL or an
9309 Although the standard says "assignment-expression", it forbids
9310 throw-expressions or assignments in the template argument.
9311 Therefore, we use "conditional-expression" instead. */
9314 cp_parser_template_argument (cp_parser* parser)
9319 bool maybe_type_id = false;
9323 /* There's really no way to know what we're looking at, so we just
9324 try each alternative in order.
9328 In a template-argument, an ambiguity between a type-id and an
9329 expression is resolved to a type-id, regardless of the form of
9330 the corresponding template-parameter.
9332 Therefore, we try a type-id first. */
9333 cp_parser_parse_tentatively (parser);
9334 argument = cp_parser_type_id (parser);
9335 /* If there was no error parsing the type-id but the next token is a '>>',
9336 we probably found a typo for '> >'. But there are type-id which are
9337 also valid expressions. For instance:
9339 struct X { int operator >> (int); };
9340 template <int V> struct Foo {};
9343 Here 'X()' is a valid type-id of a function type, but the user just
9344 wanted to write the expression "X() >> 5". Thus, we remember that we
9345 found a valid type-id, but we still try to parse the argument as an
9346 expression to see what happens. */
9347 if (!cp_parser_error_occurred (parser)
9348 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9350 maybe_type_id = true;
9351 cp_parser_abort_tentative_parse (parser);
9355 /* If the next token isn't a `,' or a `>', then this argument wasn't
9356 really finished. This means that the argument is not a valid
9358 if (!cp_parser_next_token_ends_template_argument_p (parser))
9359 cp_parser_error (parser, "expected template-argument");
9360 /* If that worked, we're done. */
9361 if (cp_parser_parse_definitely (parser))
9364 /* We're still not sure what the argument will be. */
9365 cp_parser_parse_tentatively (parser);
9366 /* Try a template. */
9367 argument = cp_parser_id_expression (parser,
9368 /*template_keyword_p=*/false,
9369 /*check_dependency_p=*/true,
9371 /*declarator_p=*/false,
9372 /*optional_p=*/false);
9373 /* If the next token isn't a `,' or a `>', then this argument wasn't
9375 if (!cp_parser_next_token_ends_template_argument_p (parser))
9376 cp_parser_error (parser, "expected template-argument");
9377 if (!cp_parser_error_occurred (parser))
9379 /* Figure out what is being referred to. If the id-expression
9380 was for a class template specialization, then we will have a
9381 TYPE_DECL at this point. There is no need to do name lookup
9382 at this point in that case. */
9383 if (TREE_CODE (argument) != TYPE_DECL)
9384 argument = cp_parser_lookup_name (parser, argument,
9386 /*is_template=*/template_p,
9387 /*is_namespace=*/false,
9388 /*check_dependency=*/true,
9389 /*ambiguous_decls=*/NULL);
9390 if (TREE_CODE (argument) != TEMPLATE_DECL
9391 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9392 cp_parser_error (parser, "expected template-name");
9394 if (cp_parser_parse_definitely (parser))
9396 /* It must be a non-type argument. There permitted cases are given
9397 in [temp.arg.nontype]:
9399 -- an integral constant-expression of integral or enumeration
9402 -- the name of a non-type template-parameter; or
9404 -- the name of an object or function with external linkage...
9406 -- the address of an object or function with external linkage...
9408 -- a pointer to member... */
9409 /* Look for a non-type template parameter. */
9410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9412 cp_parser_parse_tentatively (parser);
9413 argument = cp_parser_primary_expression (parser,
9416 /*template_arg_p=*/true,
9418 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9419 || !cp_parser_next_token_ends_template_argument_p (parser))
9420 cp_parser_simulate_error (parser);
9421 if (cp_parser_parse_definitely (parser))
9425 /* If the next token is "&", the argument must be the address of an
9426 object or function with external linkage. */
9427 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9429 cp_lexer_consume_token (parser->lexer);
9430 /* See if we might have an id-expression. */
9431 token = cp_lexer_peek_token (parser->lexer);
9432 if (token->type == CPP_NAME
9433 || token->keyword == RID_OPERATOR
9434 || token->type == CPP_SCOPE
9435 || token->type == CPP_TEMPLATE_ID
9436 || token->type == CPP_NESTED_NAME_SPECIFIER)
9438 cp_parser_parse_tentatively (parser);
9439 argument = cp_parser_primary_expression (parser,
9442 /*template_arg_p=*/true,
9444 if (cp_parser_error_occurred (parser)
9445 || !cp_parser_next_token_ends_template_argument_p (parser))
9446 cp_parser_abort_tentative_parse (parser);
9449 if (TREE_CODE (argument) == INDIRECT_REF)
9451 gcc_assert (REFERENCE_REF_P (argument));
9452 argument = TREE_OPERAND (argument, 0);
9455 if (TREE_CODE (argument) == VAR_DECL)
9457 /* A variable without external linkage might still be a
9458 valid constant-expression, so no error is issued here
9459 if the external-linkage check fails. */
9460 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9461 cp_parser_simulate_error (parser);
9463 else if (is_overloaded_fn (argument))
9464 /* All overloaded functions are allowed; if the external
9465 linkage test does not pass, an error will be issued
9469 && (TREE_CODE (argument) == OFFSET_REF
9470 || TREE_CODE (argument) == SCOPE_REF))
9471 /* A pointer-to-member. */
9473 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9476 cp_parser_simulate_error (parser);
9478 if (cp_parser_parse_definitely (parser))
9481 argument = build_x_unary_op (ADDR_EXPR, argument);
9486 /* If the argument started with "&", there are no other valid
9487 alternatives at this point. */
9490 cp_parser_error (parser, "invalid non-type template argument");
9491 return error_mark_node;
9494 /* If the argument wasn't successfully parsed as a type-id followed
9495 by '>>', the argument can only be a constant expression now.
9496 Otherwise, we try parsing the constant-expression tentatively,
9497 because the argument could really be a type-id. */
9499 cp_parser_parse_tentatively (parser);
9500 argument = cp_parser_constant_expression (parser,
9501 /*allow_non_constant_p=*/false,
9502 /*non_constant_p=*/NULL);
9503 argument = fold_non_dependent_expr (argument);
9506 if (!cp_parser_next_token_ends_template_argument_p (parser))
9507 cp_parser_error (parser, "expected template-argument");
9508 if (cp_parser_parse_definitely (parser))
9510 /* We did our best to parse the argument as a non type-id, but that
9511 was the only alternative that matched (albeit with a '>' after
9512 it). We can assume it's just a typo from the user, and a
9513 diagnostic will then be issued. */
9514 return cp_parser_type_id (parser);
9517 /* Parse an explicit-instantiation.
9519 explicit-instantiation:
9520 template declaration
9522 Although the standard says `declaration', what it really means is:
9524 explicit-instantiation:
9525 template decl-specifier-seq [opt] declarator [opt] ;
9527 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9528 supposed to be allowed. A defect report has been filed about this
9533 explicit-instantiation:
9534 storage-class-specifier template
9535 decl-specifier-seq [opt] declarator [opt] ;
9536 function-specifier template
9537 decl-specifier-seq [opt] declarator [opt] ; */
9540 cp_parser_explicit_instantiation (cp_parser* parser)
9542 int declares_class_or_enum;
9543 cp_decl_specifier_seq decl_specifiers;
9544 tree extension_specifier = NULL_TREE;
9546 /* Look for an (optional) storage-class-specifier or
9547 function-specifier. */
9548 if (cp_parser_allow_gnu_extensions_p (parser))
9551 = cp_parser_storage_class_specifier_opt (parser);
9552 if (!extension_specifier)
9554 = cp_parser_function_specifier_opt (parser,
9555 /*decl_specs=*/NULL);
9558 /* Look for the `template' keyword. */
9559 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9560 /* Let the front end know that we are processing an explicit
9562 begin_explicit_instantiation ();
9563 /* [temp.explicit] says that we are supposed to ignore access
9564 control while processing explicit instantiation directives. */
9565 push_deferring_access_checks (dk_no_check);
9566 /* Parse a decl-specifier-seq. */
9567 cp_parser_decl_specifier_seq (parser,
9568 CP_PARSER_FLAGS_OPTIONAL,
9570 &declares_class_or_enum);
9571 /* If there was exactly one decl-specifier, and it declared a class,
9572 and there's no declarator, then we have an explicit type
9574 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9578 type = check_tag_decl (&decl_specifiers);
9579 /* Turn access control back on for names used during
9580 template instantiation. */
9581 pop_deferring_access_checks ();
9583 do_type_instantiation (type, extension_specifier,
9584 /*complain=*/tf_error);
9588 cp_declarator *declarator;
9591 /* Parse the declarator. */
9593 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9594 /*ctor_dtor_or_conv_p=*/NULL,
9595 /*parenthesized_p=*/NULL,
9596 /*member_p=*/false);
9597 if (declares_class_or_enum & 2)
9598 cp_parser_check_for_definition_in_return_type (declarator,
9599 decl_specifiers.type);
9600 if (declarator != cp_error_declarator)
9602 decl = grokdeclarator (declarator, &decl_specifiers,
9603 NORMAL, 0, &decl_specifiers.attributes);
9604 /* Turn access control back on for names used during
9605 template instantiation. */
9606 pop_deferring_access_checks ();
9607 /* Do the explicit instantiation. */
9608 do_decl_instantiation (decl, extension_specifier);
9612 pop_deferring_access_checks ();
9613 /* Skip the body of the explicit instantiation. */
9614 cp_parser_skip_to_end_of_statement (parser);
9617 /* We're done with the instantiation. */
9618 end_explicit_instantiation ();
9620 cp_parser_consume_semicolon_at_end_of_statement (parser);
9623 /* Parse an explicit-specialization.
9625 explicit-specialization:
9626 template < > declaration
9628 Although the standard says `declaration', what it really means is:
9630 explicit-specialization:
9631 template <> decl-specifier [opt] init-declarator [opt] ;
9632 template <> function-definition
9633 template <> explicit-specialization
9634 template <> template-declaration */
9637 cp_parser_explicit_specialization (cp_parser* parser)
9640 /* Look for the `template' keyword. */
9641 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9642 /* Look for the `<'. */
9643 cp_parser_require (parser, CPP_LESS, "`<'");
9644 /* Look for the `>'. */
9645 cp_parser_require (parser, CPP_GREATER, "`>'");
9646 /* We have processed another parameter list. */
9647 ++parser->num_template_parameter_lists;
9650 A template ... explicit specialization ... shall not have C
9652 if (current_lang_name == lang_name_c)
9654 error ("template specialization with C linkage");
9655 /* Give it C++ linkage to avoid confusing other parts of the
9657 push_lang_context (lang_name_cplusplus);
9658 need_lang_pop = true;
9661 need_lang_pop = false;
9662 /* Let the front end know that we are beginning a specialization. */
9663 if (!begin_specialization ())
9665 end_specialization ();
9666 cp_parser_skip_to_end_of_block_or_statement (parser);
9670 /* If the next keyword is `template', we need to figure out whether
9671 or not we're looking a template-declaration. */
9672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9674 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9675 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9676 cp_parser_template_declaration_after_export (parser,
9677 /*member_p=*/false);
9679 cp_parser_explicit_specialization (parser);
9682 /* Parse the dependent declaration. */
9683 cp_parser_single_declaration (parser,
9687 /* We're done with the specialization. */
9688 end_specialization ();
9689 /* For the erroneous case of a template with C linkage, we pushed an
9690 implicit C++ linkage scope; exit that scope now. */
9692 pop_lang_context ();
9693 /* We're done with this parameter list. */
9694 --parser->num_template_parameter_lists;
9697 /* Parse a type-specifier.
9700 simple-type-specifier
9703 elaborated-type-specifier
9711 Returns a representation of the type-specifier. For a
9712 class-specifier, enum-specifier, or elaborated-type-specifier, a
9713 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9715 The parser flags FLAGS is used to control type-specifier parsing.
9717 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9718 in a decl-specifier-seq.
9720 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9721 class-specifier, enum-specifier, or elaborated-type-specifier, then
9722 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9723 if a type is declared; 2 if it is defined. Otherwise, it is set to
9726 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9727 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9731 cp_parser_type_specifier (cp_parser* parser,
9732 cp_parser_flags flags,
9733 cp_decl_specifier_seq *decl_specs,
9734 bool is_declaration,
9735 int* declares_class_or_enum,
9736 bool* is_cv_qualifier)
9738 tree type_spec = NULL_TREE;
9741 cp_decl_spec ds = ds_last;
9743 /* Assume this type-specifier does not declare a new type. */
9744 if (declares_class_or_enum)
9745 *declares_class_or_enum = 0;
9746 /* And that it does not specify a cv-qualifier. */
9747 if (is_cv_qualifier)
9748 *is_cv_qualifier = false;
9749 /* Peek at the next token. */
9750 token = cp_lexer_peek_token (parser->lexer);
9752 /* If we're looking at a keyword, we can use that to guide the
9753 production we choose. */
9754 keyword = token->keyword;
9758 /* Look for the enum-specifier. */
9759 type_spec = cp_parser_enum_specifier (parser);
9760 /* If that worked, we're done. */
9763 if (declares_class_or_enum)
9764 *declares_class_or_enum = 2;
9766 cp_parser_set_decl_spec_type (decl_specs,
9768 /*user_defined_p=*/true);
9772 goto elaborated_type_specifier;
9774 /* Any of these indicate either a class-specifier, or an
9775 elaborated-type-specifier. */
9779 /* Parse tentatively so that we can back up if we don't find a
9781 cp_parser_parse_tentatively (parser);
9782 /* Look for the class-specifier. */
9783 type_spec = cp_parser_class_specifier (parser);
9784 /* If that worked, we're done. */
9785 if (cp_parser_parse_definitely (parser))
9787 if (declares_class_or_enum)
9788 *declares_class_or_enum = 2;
9790 cp_parser_set_decl_spec_type (decl_specs,
9792 /*user_defined_p=*/true);
9797 elaborated_type_specifier:
9798 /* We're declaring (not defining) a class or enum. */
9799 if (declares_class_or_enum)
9800 *declares_class_or_enum = 1;
9804 /* Look for an elaborated-type-specifier. */
9806 = (cp_parser_elaborated_type_specifier
9808 decl_specs && decl_specs->specs[(int) ds_friend],
9811 cp_parser_set_decl_spec_type (decl_specs,
9813 /*user_defined_p=*/true);
9818 if (is_cv_qualifier)
9819 *is_cv_qualifier = true;
9824 if (is_cv_qualifier)
9825 *is_cv_qualifier = true;
9830 if (is_cv_qualifier)
9831 *is_cv_qualifier = true;
9835 /* The `__complex__' keyword is a GNU extension. */
9843 /* Handle simple keywords. */
9848 ++decl_specs->specs[(int)ds];
9849 decl_specs->any_specifiers_p = true;
9851 return cp_lexer_consume_token (parser->lexer)->u.value;
9854 /* If we do not already have a type-specifier, assume we are looking
9855 at a simple-type-specifier. */
9856 type_spec = cp_parser_simple_type_specifier (parser,
9860 /* If we didn't find a type-specifier, and a type-specifier was not
9861 optional in this context, issue an error message. */
9862 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9864 cp_parser_error (parser, "expected type specifier");
9865 return error_mark_node;
9871 /* Parse a simple-type-specifier.
9873 simple-type-specifier:
9874 :: [opt] nested-name-specifier [opt] type-name
9875 :: [opt] nested-name-specifier template template-id
9890 simple-type-specifier:
9891 __typeof__ unary-expression
9892 __typeof__ ( type-id )
9894 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9895 appropriately updated. */
9898 cp_parser_simple_type_specifier (cp_parser* parser,
9899 cp_decl_specifier_seq *decl_specs,
9900 cp_parser_flags flags)
9902 tree type = NULL_TREE;
9905 /* Peek at the next token. */
9906 token = cp_lexer_peek_token (parser->lexer);
9908 /* If we're looking at a keyword, things are easy. */
9909 switch (token->keyword)
9913 decl_specs->explicit_char_p = true;
9914 type = char_type_node;
9917 type = wchar_type_node;
9920 type = boolean_type_node;
9924 ++decl_specs->specs[(int) ds_short];
9925 type = short_integer_type_node;
9929 decl_specs->explicit_int_p = true;
9930 type = integer_type_node;
9934 ++decl_specs->specs[(int) ds_long];
9935 type = long_integer_type_node;
9939 ++decl_specs->specs[(int) ds_signed];
9940 type = integer_type_node;
9944 ++decl_specs->specs[(int) ds_unsigned];
9945 type = unsigned_type_node;
9948 type = float_type_node;
9951 type = double_type_node;
9954 type = void_type_node;
9958 /* Consume the `typeof' token. */
9959 cp_lexer_consume_token (parser->lexer);
9960 /* Parse the operand to `typeof'. */
9961 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9962 /* If it is not already a TYPE, take its type. */
9964 type = finish_typeof (type);
9967 cp_parser_set_decl_spec_type (decl_specs, type,
9968 /*user_defined_p=*/true);
9976 /* If the type-specifier was for a built-in type, we're done. */
9981 /* Record the type. */
9983 && (token->keyword != RID_SIGNED
9984 && token->keyword != RID_UNSIGNED
9985 && token->keyword != RID_SHORT
9986 && token->keyword != RID_LONG))
9987 cp_parser_set_decl_spec_type (decl_specs,
9989 /*user_defined=*/false);
9991 decl_specs->any_specifiers_p = true;
9993 /* Consume the token. */
9994 id = cp_lexer_consume_token (parser->lexer)->u.value;
9996 /* There is no valid C++ program where a non-template type is
9997 followed by a "<". That usually indicates that the user thought
9998 that the type was a template. */
9999 cp_parser_check_for_invalid_template_id (parser, type);
10001 return TYPE_NAME (type);
10004 /* The type-specifier must be a user-defined type. */
10005 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10010 /* Don't gobble tokens or issue error messages if this is an
10011 optional type-specifier. */
10012 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10013 cp_parser_parse_tentatively (parser);
10015 /* Look for the optional `::' operator. */
10017 = (cp_parser_global_scope_opt (parser,
10018 /*current_scope_valid_p=*/false)
10020 /* Look for the nested-name specifier. */
10022 = (cp_parser_nested_name_specifier_opt (parser,
10023 /*typename_keyword_p=*/false,
10024 /*check_dependency_p=*/true,
10026 /*is_declaration=*/false)
10028 /* If we have seen a nested-name-specifier, and the next token
10029 is `template', then we are using the template-id production. */
10031 && cp_parser_optional_template_keyword (parser))
10033 /* Look for the template-id. */
10034 type = cp_parser_template_id (parser,
10035 /*template_keyword_p=*/true,
10036 /*check_dependency_p=*/true,
10037 /*is_declaration=*/false);
10038 /* If the template-id did not name a type, we are out of
10040 if (TREE_CODE (type) != TYPE_DECL)
10042 cp_parser_error (parser, "expected template-id for type");
10046 /* Otherwise, look for a type-name. */
10048 type = cp_parser_type_name (parser);
10049 /* Keep track of all name-lookups performed in class scopes. */
10053 && TREE_CODE (type) == TYPE_DECL
10054 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10055 maybe_note_name_used_in_class (DECL_NAME (type), type);
10056 /* If it didn't work out, we don't have a TYPE. */
10057 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10058 && !cp_parser_parse_definitely (parser))
10060 if (type && decl_specs)
10061 cp_parser_set_decl_spec_type (decl_specs, type,
10062 /*user_defined=*/true);
10065 /* If we didn't get a type-name, issue an error message. */
10066 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10068 cp_parser_error (parser, "expected type-name");
10069 return error_mark_node;
10072 /* There is no valid C++ program where a non-template type is
10073 followed by a "<". That usually indicates that the user thought
10074 that the type was a template. */
10075 if (type && type != error_mark_node)
10077 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10078 If it is, then the '<'...'>' enclose protocol names rather than
10079 template arguments, and so everything is fine. */
10080 if (c_dialect_objc ()
10081 && (objc_is_id (type) || objc_is_class_name (type)))
10083 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10084 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10086 /* Clobber the "unqualified" type previously entered into
10087 DECL_SPECS with the new, improved protocol-qualified version. */
10089 decl_specs->type = qual_type;
10094 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10100 /* Parse a type-name.
10113 Returns a TYPE_DECL for the type. */
10116 cp_parser_type_name (cp_parser* parser)
10121 /* We can't know yet whether it is a class-name or not. */
10122 cp_parser_parse_tentatively (parser);
10123 /* Try a class-name. */
10124 type_decl = cp_parser_class_name (parser,
10125 /*typename_keyword_p=*/false,
10126 /*template_keyword_p=*/false,
10128 /*check_dependency_p=*/true,
10129 /*class_head_p=*/false,
10130 /*is_declaration=*/false);
10131 /* If it's not a class-name, keep looking. */
10132 if (!cp_parser_parse_definitely (parser))
10134 /* It must be a typedef-name or an enum-name. */
10135 identifier = cp_parser_identifier (parser);
10136 if (identifier == error_mark_node)
10137 return error_mark_node;
10139 /* Look up the type-name. */
10140 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10142 if (TREE_CODE (type_decl) != TYPE_DECL
10143 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10145 /* See if this is an Objective-C type. */
10146 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10147 tree type = objc_get_protocol_qualified_type (identifier, protos);
10149 type_decl = TYPE_NAME (type);
10152 /* Issue an error if we did not find a type-name. */
10153 if (TREE_CODE (type_decl) != TYPE_DECL)
10155 if (!cp_parser_simulate_error (parser))
10156 cp_parser_name_lookup_error (parser, identifier, type_decl,
10158 type_decl = error_mark_node;
10160 /* Remember that the name was used in the definition of the
10161 current class so that we can check later to see if the
10162 meaning would have been different after the class was
10163 entirely defined. */
10164 else if (type_decl != error_mark_node
10166 maybe_note_name_used_in_class (identifier, type_decl);
10173 /* Parse an elaborated-type-specifier. Note that the grammar given
10174 here incorporates the resolution to DR68.
10176 elaborated-type-specifier:
10177 class-key :: [opt] nested-name-specifier [opt] identifier
10178 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10179 enum :: [opt] nested-name-specifier [opt] identifier
10180 typename :: [opt] nested-name-specifier identifier
10181 typename :: [opt] nested-name-specifier template [opt]
10186 elaborated-type-specifier:
10187 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10188 class-key attributes :: [opt] nested-name-specifier [opt]
10189 template [opt] template-id
10190 enum attributes :: [opt] nested-name-specifier [opt] identifier
10192 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10193 declared `friend'. If IS_DECLARATION is TRUE, then this
10194 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10195 something is being declared.
10197 Returns the TYPE specified. */
10200 cp_parser_elaborated_type_specifier (cp_parser* parser,
10202 bool is_declaration)
10204 enum tag_types tag_type;
10206 tree type = NULL_TREE;
10207 tree attributes = NULL_TREE;
10209 /* See if we're looking at the `enum' keyword. */
10210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10212 /* Consume the `enum' token. */
10213 cp_lexer_consume_token (parser->lexer);
10214 /* Remember that it's an enumeration type. */
10215 tag_type = enum_type;
10216 /* Parse the attributes. */
10217 attributes = cp_parser_attributes_opt (parser);
10219 /* Or, it might be `typename'. */
10220 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10223 /* Consume the `typename' token. */
10224 cp_lexer_consume_token (parser->lexer);
10225 /* Remember that it's a `typename' type. */
10226 tag_type = typename_type;
10227 /* The `typename' keyword is only allowed in templates. */
10228 if (!processing_template_decl)
10229 pedwarn ("using %<typename%> outside of template");
10231 /* Otherwise it must be a class-key. */
10234 tag_type = cp_parser_class_key (parser);
10235 if (tag_type == none_type)
10236 return error_mark_node;
10237 /* Parse the attributes. */
10238 attributes = cp_parser_attributes_opt (parser);
10241 /* Look for the `::' operator. */
10242 cp_parser_global_scope_opt (parser,
10243 /*current_scope_valid_p=*/false);
10244 /* Look for the nested-name-specifier. */
10245 if (tag_type == typename_type)
10247 if (!cp_parser_nested_name_specifier (parser,
10248 /*typename_keyword_p=*/true,
10249 /*check_dependency_p=*/true,
10252 return error_mark_node;
10255 /* Even though `typename' is not present, the proposed resolution
10256 to Core Issue 180 says that in `class A<T>::B', `B' should be
10257 considered a type-name, even if `A<T>' is dependent. */
10258 cp_parser_nested_name_specifier_opt (parser,
10259 /*typename_keyword_p=*/true,
10260 /*check_dependency_p=*/true,
10263 /* For everything but enumeration types, consider a template-id.
10264 For an enumeration type, consider only a plain identifier. */
10265 if (tag_type != enum_type)
10267 bool template_p = false;
10270 /* Allow the `template' keyword. */
10271 template_p = cp_parser_optional_template_keyword (parser);
10272 /* If we didn't see `template', we don't know if there's a
10273 template-id or not. */
10275 cp_parser_parse_tentatively (parser);
10276 /* Parse the template-id. */
10277 decl = cp_parser_template_id (parser, template_p,
10278 /*check_dependency_p=*/true,
10280 /* If we didn't find a template-id, look for an ordinary
10282 if (!template_p && !cp_parser_parse_definitely (parser))
10284 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10285 in effect, then we must assume that, upon instantiation, the
10286 template will correspond to a class. */
10287 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10288 && tag_type == typename_type)
10289 type = make_typename_type (parser->scope, decl,
10291 /*complain=*/tf_error);
10293 type = TREE_TYPE (decl);
10298 identifier = cp_parser_identifier (parser);
10300 if (identifier == error_mark_node)
10302 parser->scope = NULL_TREE;
10303 return error_mark_node;
10306 /* For a `typename', we needn't call xref_tag. */
10307 if (tag_type == typename_type
10308 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10309 return cp_parser_make_typename_type (parser, parser->scope,
10311 /* Look up a qualified name in the usual way. */
10316 decl = cp_parser_lookup_name (parser, identifier,
10318 /*is_template=*/false,
10319 /*is_namespace=*/false,
10320 /*check_dependency=*/true,
10321 /*ambiguous_decls=*/NULL);
10323 /* If we are parsing friend declaration, DECL may be a
10324 TEMPLATE_DECL tree node here. However, we need to check
10325 whether this TEMPLATE_DECL results in valid code. Consider
10326 the following example:
10329 template <class T> class C {};
10332 template <class T> friend class N::C; // #1, valid code
10334 template <class T> class Y {
10335 friend class N::C; // #2, invalid code
10338 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10339 name lookup of `N::C'. We see that friend declaration must
10340 be template for the code to be valid. Note that
10341 processing_template_decl does not work here since it is
10342 always 1 for the above two cases. */
10344 decl = (cp_parser_maybe_treat_template_as_class
10345 (decl, /*tag_name_p=*/is_friend
10346 && parser->num_template_parameter_lists));
10348 if (TREE_CODE (decl) != TYPE_DECL)
10350 cp_parser_diagnose_invalid_type_name (parser,
10353 return error_mark_node;
10356 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10358 bool allow_template = (parser->num_template_parameter_lists
10359 || DECL_SELF_REFERENCE_P (decl));
10360 type = check_elaborated_type_specifier (tag_type, decl,
10363 if (type == error_mark_node)
10364 return error_mark_node;
10367 type = TREE_TYPE (decl);
10371 /* An elaborated-type-specifier sometimes introduces a new type and
10372 sometimes names an existing type. Normally, the rule is that it
10373 introduces a new type only if there is not an existing type of
10374 the same name already in scope. For example, given:
10377 void f() { struct S s; }
10379 the `struct S' in the body of `f' is the same `struct S' as in
10380 the global scope; the existing definition is used. However, if
10381 there were no global declaration, this would introduce a new
10382 local class named `S'.
10384 An exception to this rule applies to the following code:
10386 namespace N { struct S; }
10388 Here, the elaborated-type-specifier names a new type
10389 unconditionally; even if there is already an `S' in the
10390 containing scope this declaration names a new type.
10391 This exception only applies if the elaborated-type-specifier
10392 forms the complete declaration:
10396 A declaration consisting solely of `class-key identifier ;' is
10397 either a redeclaration of the name in the current scope or a
10398 forward declaration of the identifier as a class name. It
10399 introduces the name into the current scope.
10401 We are in this situation precisely when the next token is a `;'.
10403 An exception to the exception is that a `friend' declaration does
10404 *not* name a new type; i.e., given:
10406 struct S { friend struct T; };
10408 `T' is not a new type in the scope of `S'.
10410 Also, `new struct S' or `sizeof (struct S)' never results in the
10411 definition of a new type; a new type can only be declared in a
10412 declaration context. */
10418 /* Friends have special name lookup rules. */
10419 ts = ts_within_enclosing_non_class;
10420 else if (is_declaration
10421 && cp_lexer_next_token_is (parser->lexer,
10423 /* This is a `class-key identifier ;' */
10429 (parser->num_template_parameter_lists
10430 && (cp_parser_next_token_starts_class_definition_p (parser)
10431 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10432 /* An unqualified name was used to reference this type, so
10433 there were no qualifying templates. */
10434 if (!cp_parser_check_template_parameters (parser,
10435 /*num_templates=*/0))
10436 return error_mark_node;
10437 type = xref_tag (tag_type, identifier, ts, template_p);
10441 if (type == error_mark_node)
10442 return error_mark_node;
10444 /* Allow attributes on forward declarations of classes. */
10447 if (TREE_CODE (type) == TYPENAME_TYPE)
10448 warning (OPT_Wattributes,
10449 "attributes ignored on uninstantiated type");
10450 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10451 && ! processing_explicit_instantiation)
10452 warning (OPT_Wattributes,
10453 "attributes ignored on template instantiation");
10454 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10455 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10457 warning (OPT_Wattributes,
10458 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10461 if (tag_type != enum_type)
10462 cp_parser_check_class_key (tag_type, type);
10464 /* A "<" cannot follow an elaborated type specifier. If that
10465 happens, the user was probably trying to form a template-id. */
10466 cp_parser_check_for_invalid_template_id (parser, type);
10471 /* Parse an enum-specifier.
10474 enum identifier [opt] { enumerator-list [opt] }
10477 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10480 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10481 if the token stream isn't an enum-specifier after all. */
10484 cp_parser_enum_specifier (cp_parser* parser)
10490 /* Parse tentatively so that we can back up if we don't find a
10492 cp_parser_parse_tentatively (parser);
10494 /* Caller guarantees that the current token is 'enum', an identifier
10495 possibly follows, and the token after that is an opening brace.
10496 If we don't have an identifier, fabricate an anonymous name for
10497 the enumeration being defined. */
10498 cp_lexer_consume_token (parser->lexer);
10500 attributes = cp_parser_attributes_opt (parser);
10502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10503 identifier = cp_parser_identifier (parser);
10505 identifier = make_anon_name ();
10507 /* Look for the `{' but don't consume it yet. */
10508 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10509 cp_parser_simulate_error (parser);
10511 if (!cp_parser_parse_definitely (parser))
10514 /* Issue an error message if type-definitions are forbidden here. */
10515 if (!cp_parser_check_type_definition (parser))
10516 type = error_mark_node;
10518 /* Create the new type. We do this before consuming the opening
10519 brace so the enum will be recorded as being on the line of its
10520 tag (or the 'enum' keyword, if there is no tag). */
10521 type = start_enum (identifier);
10523 /* Consume the opening brace. */
10524 cp_lexer_consume_token (parser->lexer);
10526 if (type == error_mark_node)
10528 cp_parser_skip_to_end_of_block_or_statement (parser);
10529 return error_mark_node;
10532 /* If the next token is not '}', then there are some enumerators. */
10533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10534 cp_parser_enumerator_list (parser, type);
10536 /* Consume the final '}'. */
10537 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10539 /* Look for trailing attributes to apply to this enumeration, and
10540 apply them if appropriate. */
10541 if (cp_parser_allow_gnu_extensions_p (parser))
10543 tree trailing_attr = cp_parser_attributes_opt (parser);
10544 cplus_decl_attributes (&type,
10546 (int) ATTR_FLAG_TYPE_IN_PLACE);
10549 /* Finish up the enumeration. */
10550 finish_enum (type);
10555 /* Parse an enumerator-list. The enumerators all have the indicated
10559 enumerator-definition
10560 enumerator-list , enumerator-definition */
10563 cp_parser_enumerator_list (cp_parser* parser, tree type)
10567 /* Parse an enumerator-definition. */
10568 cp_parser_enumerator_definition (parser, type);
10570 /* If the next token is not a ',', we've reached the end of
10572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10574 /* Otherwise, consume the `,' and keep going. */
10575 cp_lexer_consume_token (parser->lexer);
10576 /* If the next token is a `}', there is a trailing comma. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10579 if (pedantic && !in_system_header)
10580 pedwarn ("comma at end of enumerator list");
10586 /* Parse an enumerator-definition. The enumerator has the indicated
10589 enumerator-definition:
10591 enumerator = constant-expression
10597 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10602 /* Look for the identifier. */
10603 identifier = cp_parser_identifier (parser);
10604 if (identifier == error_mark_node)
10607 /* If the next token is an '=', then there is an explicit value. */
10608 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10610 /* Consume the `=' token. */
10611 cp_lexer_consume_token (parser->lexer);
10612 /* Parse the value. */
10613 value = cp_parser_constant_expression (parser,
10614 /*allow_non_constant_p=*/false,
10620 /* Create the enumerator. */
10621 build_enumerator (identifier, value, type);
10624 /* Parse a namespace-name.
10627 original-namespace-name
10630 Returns the NAMESPACE_DECL for the namespace. */
10633 cp_parser_namespace_name (cp_parser* parser)
10636 tree namespace_decl;
10638 /* Get the name of the namespace. */
10639 identifier = cp_parser_identifier (parser);
10640 if (identifier == error_mark_node)
10641 return error_mark_node;
10643 /* Look up the identifier in the currently active scope. Look only
10644 for namespaces, due to:
10646 [basic.lookup.udir]
10648 When looking up a namespace-name in a using-directive or alias
10649 definition, only namespace names are considered.
10653 [basic.lookup.qual]
10655 During the lookup of a name preceding the :: scope resolution
10656 operator, object, function, and enumerator names are ignored.
10658 (Note that cp_parser_class_or_namespace_name only calls this
10659 function if the token after the name is the scope resolution
10661 namespace_decl = cp_parser_lookup_name (parser, identifier,
10663 /*is_template=*/false,
10664 /*is_namespace=*/true,
10665 /*check_dependency=*/true,
10666 /*ambiguous_decls=*/NULL);
10667 /* If it's not a namespace, issue an error. */
10668 if (namespace_decl == error_mark_node
10669 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10671 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10672 error ("%qD is not a namespace-name", identifier);
10673 cp_parser_error (parser, "expected namespace-name");
10674 namespace_decl = error_mark_node;
10677 return namespace_decl;
10680 /* Parse a namespace-definition.
10682 namespace-definition:
10683 named-namespace-definition
10684 unnamed-namespace-definition
10686 named-namespace-definition:
10687 original-namespace-definition
10688 extension-namespace-definition
10690 original-namespace-definition:
10691 namespace identifier { namespace-body }
10693 extension-namespace-definition:
10694 namespace original-namespace-name { namespace-body }
10696 unnamed-namespace-definition:
10697 namespace { namespace-body } */
10700 cp_parser_namespace_definition (cp_parser* parser)
10702 tree identifier, attribs;
10704 /* Look for the `namespace' keyword. */
10705 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10707 /* Get the name of the namespace. We do not attempt to distinguish
10708 between an original-namespace-definition and an
10709 extension-namespace-definition at this point. The semantic
10710 analysis routines are responsible for that. */
10711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10712 identifier = cp_parser_identifier (parser);
10714 identifier = NULL_TREE;
10716 /* Parse any specified attributes. */
10717 attribs = cp_parser_attributes_opt (parser);
10719 /* Look for the `{' to start the namespace. */
10720 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10721 /* Start the namespace. */
10722 push_namespace_with_attribs (identifier, attribs);
10723 /* Parse the body of the namespace. */
10724 cp_parser_namespace_body (parser);
10725 /* Finish the namespace. */
10727 /* Look for the final `}'. */
10728 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10731 /* Parse a namespace-body.
10734 declaration-seq [opt] */
10737 cp_parser_namespace_body (cp_parser* parser)
10739 cp_parser_declaration_seq_opt (parser);
10742 /* Parse a namespace-alias-definition.
10744 namespace-alias-definition:
10745 namespace identifier = qualified-namespace-specifier ; */
10748 cp_parser_namespace_alias_definition (cp_parser* parser)
10751 tree namespace_specifier;
10753 /* Look for the `namespace' keyword. */
10754 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10755 /* Look for the identifier. */
10756 identifier = cp_parser_identifier (parser);
10757 if (identifier == error_mark_node)
10759 /* Look for the `=' token. */
10760 cp_parser_require (parser, CPP_EQ, "`='");
10761 /* Look for the qualified-namespace-specifier. */
10762 namespace_specifier
10763 = cp_parser_qualified_namespace_specifier (parser);
10764 /* Look for the `;' token. */
10765 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10767 /* Register the alias in the symbol table. */
10768 do_namespace_alias (identifier, namespace_specifier);
10771 /* Parse a qualified-namespace-specifier.
10773 qualified-namespace-specifier:
10774 :: [opt] nested-name-specifier [opt] namespace-name
10776 Returns a NAMESPACE_DECL corresponding to the specified
10780 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10782 /* Look for the optional `::'. */
10783 cp_parser_global_scope_opt (parser,
10784 /*current_scope_valid_p=*/false);
10786 /* Look for the optional nested-name-specifier. */
10787 cp_parser_nested_name_specifier_opt (parser,
10788 /*typename_keyword_p=*/false,
10789 /*check_dependency_p=*/true,
10791 /*is_declaration=*/true);
10793 return cp_parser_namespace_name (parser);
10796 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10797 access declaration.
10800 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10801 using :: unqualified-id ;
10803 access-declaration:
10809 cp_parser_using_declaration (cp_parser* parser,
10810 bool access_declaration_p)
10813 bool typename_p = false;
10814 bool global_scope_p;
10819 if (access_declaration_p)
10820 cp_parser_parse_tentatively (parser);
10823 /* Look for the `using' keyword. */
10824 cp_parser_require_keyword (parser, RID_USING, "`using'");
10826 /* Peek at the next token. */
10827 token = cp_lexer_peek_token (parser->lexer);
10828 /* See if it's `typename'. */
10829 if (token->keyword == RID_TYPENAME)
10831 /* Remember that we've seen it. */
10833 /* Consume the `typename' token. */
10834 cp_lexer_consume_token (parser->lexer);
10838 /* Look for the optional global scope qualification. */
10840 = (cp_parser_global_scope_opt (parser,
10841 /*current_scope_valid_p=*/false)
10844 /* If we saw `typename', or didn't see `::', then there must be a
10845 nested-name-specifier present. */
10846 if (typename_p || !global_scope_p)
10847 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10848 /*check_dependency_p=*/true,
10850 /*is_declaration=*/true);
10851 /* Otherwise, we could be in either of the two productions. In that
10852 case, treat the nested-name-specifier as optional. */
10854 qscope = cp_parser_nested_name_specifier_opt (parser,
10855 /*typename_keyword_p=*/false,
10856 /*check_dependency_p=*/true,
10858 /*is_declaration=*/true);
10860 qscope = global_namespace;
10862 if (access_declaration_p && cp_parser_error_occurred (parser))
10863 /* Something has already gone wrong; there's no need to parse
10864 further. Since an error has occurred, the return value of
10865 cp_parser_parse_definitely will be false, as required. */
10866 return cp_parser_parse_definitely (parser);
10868 /* Parse the unqualified-id. */
10869 identifier = cp_parser_unqualified_id (parser,
10870 /*template_keyword_p=*/false,
10871 /*check_dependency_p=*/true,
10872 /*declarator_p=*/true,
10873 /*optional_p=*/false);
10875 if (access_declaration_p)
10877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10878 cp_parser_simulate_error (parser);
10879 if (!cp_parser_parse_definitely (parser))
10883 /* The function we call to handle a using-declaration is different
10884 depending on what scope we are in. */
10885 if (qscope == error_mark_node || identifier == error_mark_node)
10887 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10888 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10889 /* [namespace.udecl]
10891 A using declaration shall not name a template-id. */
10892 error ("a template-id may not appear in a using-declaration");
10895 if (at_class_scope_p ())
10897 /* Create the USING_DECL. */
10898 decl = do_class_using_decl (parser->scope, identifier);
10899 /* Add it to the list of members in this class. */
10900 finish_member_declaration (decl);
10904 decl = cp_parser_lookup_name_simple (parser, identifier);
10905 if (decl == error_mark_node)
10906 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10907 else if (!at_namespace_scope_p ())
10908 do_local_using_decl (decl, qscope, identifier);
10910 do_toplevel_using_decl (decl, qscope, identifier);
10914 /* Look for the final `;'. */
10915 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10920 /* Parse a using-directive.
10923 using namespace :: [opt] nested-name-specifier [opt]
10924 namespace-name ; */
10927 cp_parser_using_directive (cp_parser* parser)
10929 tree namespace_decl;
10932 /* Look for the `using' keyword. */
10933 cp_parser_require_keyword (parser, RID_USING, "`using'");
10934 /* And the `namespace' keyword. */
10935 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10936 /* Look for the optional `::' operator. */
10937 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10938 /* And the optional nested-name-specifier. */
10939 cp_parser_nested_name_specifier_opt (parser,
10940 /*typename_keyword_p=*/false,
10941 /*check_dependency_p=*/true,
10943 /*is_declaration=*/true);
10944 /* Get the namespace being used. */
10945 namespace_decl = cp_parser_namespace_name (parser);
10946 /* And any specified attributes. */
10947 attribs = cp_parser_attributes_opt (parser);
10948 /* Update the symbol table. */
10949 parse_using_directive (namespace_decl, attribs);
10950 /* Look for the final `;'. */
10951 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10954 /* Parse an asm-definition.
10957 asm ( string-literal ) ;
10962 asm volatile [opt] ( string-literal ) ;
10963 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10964 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10965 : asm-operand-list [opt] ) ;
10966 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10967 : asm-operand-list [opt]
10968 : asm-operand-list [opt] ) ; */
10971 cp_parser_asm_definition (cp_parser* parser)
10974 tree outputs = NULL_TREE;
10975 tree inputs = NULL_TREE;
10976 tree clobbers = NULL_TREE;
10978 bool volatile_p = false;
10979 bool extended_p = false;
10981 /* Look for the `asm' keyword. */
10982 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10983 /* See if the next token is `volatile'. */
10984 if (cp_parser_allow_gnu_extensions_p (parser)
10985 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10987 /* Remember that we saw the `volatile' keyword. */
10989 /* Consume the token. */
10990 cp_lexer_consume_token (parser->lexer);
10992 /* Look for the opening `('. */
10993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10995 /* Look for the string. */
10996 string = cp_parser_string_literal (parser, false, false);
10997 if (string == error_mark_node)
10999 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11000 /*consume_paren=*/true);
11004 /* If we're allowing GNU extensions, check for the extended assembly
11005 syntax. Unfortunately, the `:' tokens need not be separated by
11006 a space in C, and so, for compatibility, we tolerate that here
11007 too. Doing that means that we have to treat the `::' operator as
11009 if (cp_parser_allow_gnu_extensions_p (parser)
11010 && parser->in_function_body
11011 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11012 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11014 bool inputs_p = false;
11015 bool clobbers_p = false;
11017 /* The extended syntax was used. */
11020 /* Look for outputs. */
11021 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11023 /* Consume the `:'. */
11024 cp_lexer_consume_token (parser->lexer);
11025 /* Parse the output-operands. */
11026 if (cp_lexer_next_token_is_not (parser->lexer,
11028 && cp_lexer_next_token_is_not (parser->lexer,
11030 && cp_lexer_next_token_is_not (parser->lexer,
11032 outputs = cp_parser_asm_operand_list (parser);
11034 /* If the next token is `::', there are no outputs, and the
11035 next token is the beginning of the inputs. */
11036 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11037 /* The inputs are coming next. */
11040 /* Look for inputs. */
11042 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11044 /* Consume the `:' or `::'. */
11045 cp_lexer_consume_token (parser->lexer);
11046 /* Parse the output-operands. */
11047 if (cp_lexer_next_token_is_not (parser->lexer,
11049 && cp_lexer_next_token_is_not (parser->lexer,
11051 inputs = cp_parser_asm_operand_list (parser);
11053 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11054 /* The clobbers are coming next. */
11057 /* Look for clobbers. */
11059 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11061 /* Consume the `:' or `::'. */
11062 cp_lexer_consume_token (parser->lexer);
11063 /* Parse the clobbers. */
11064 if (cp_lexer_next_token_is_not (parser->lexer,
11066 clobbers = cp_parser_asm_clobber_list (parser);
11069 /* Look for the closing `)'. */
11070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11071 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11072 /*consume_paren=*/true);
11073 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11075 /* Create the ASM_EXPR. */
11076 if (parser->in_function_body)
11078 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11080 /* If the extended syntax was not used, mark the ASM_EXPR. */
11083 tree temp = asm_stmt;
11084 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11085 temp = TREE_OPERAND (temp, 0);
11087 ASM_INPUT_P (temp) = 1;
11091 cgraph_add_asm_node (string);
11094 /* Declarators [gram.dcl.decl] */
11096 /* Parse an init-declarator.
11099 declarator initializer [opt]
11104 declarator asm-specification [opt] attributes [opt] initializer [opt]
11106 function-definition:
11107 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11109 decl-specifier-seq [opt] declarator function-try-block
11113 function-definition:
11114 __extension__ function-definition
11116 The DECL_SPECIFIERS apply to this declarator. Returns a
11117 representation of the entity declared. If MEMBER_P is TRUE, then
11118 this declarator appears in a class scope. The new DECL created by
11119 this declarator is returned.
11121 The CHECKS are access checks that should be performed once we know
11122 what entity is being declared (and, therefore, what classes have
11125 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11126 for a function-definition here as well. If the declarator is a
11127 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11128 be TRUE upon return. By that point, the function-definition will
11129 have been completely parsed.
11131 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11135 cp_parser_init_declarator (cp_parser* parser,
11136 cp_decl_specifier_seq *decl_specifiers,
11137 VEC (deferred_access_check,gc)* checks,
11138 bool function_definition_allowed_p,
11140 int declares_class_or_enum,
11141 bool* function_definition_p)
11144 cp_declarator *declarator;
11145 tree prefix_attributes;
11147 tree asm_specification;
11149 tree decl = NULL_TREE;
11151 bool is_initialized;
11152 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11153 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11155 enum cpp_ttype initialization_kind;
11156 bool is_parenthesized_init = false;
11157 bool is_non_constant_init;
11158 int ctor_dtor_or_conv_p;
11160 tree pushed_scope = NULL;
11162 /* Gather the attributes that were provided with the
11163 decl-specifiers. */
11164 prefix_attributes = decl_specifiers->attributes;
11166 /* Assume that this is not the declarator for a function
11168 if (function_definition_p)
11169 *function_definition_p = false;
11171 /* Defer access checks while parsing the declarator; we cannot know
11172 what names are accessible until we know what is being
11174 resume_deferring_access_checks ();
11176 /* Parse the declarator. */
11178 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11179 &ctor_dtor_or_conv_p,
11180 /*parenthesized_p=*/NULL,
11181 /*member_p=*/false);
11182 /* Gather up the deferred checks. */
11183 stop_deferring_access_checks ();
11185 /* If the DECLARATOR was erroneous, there's no need to go
11187 if (declarator == cp_error_declarator)
11188 return error_mark_node;
11190 /* Check that the number of template-parameter-lists is OK. */
11191 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11192 return error_mark_node;
11194 if (declares_class_or_enum & 2)
11195 cp_parser_check_for_definition_in_return_type (declarator,
11196 decl_specifiers->type);
11198 /* Figure out what scope the entity declared by the DECLARATOR is
11199 located in. `grokdeclarator' sometimes changes the scope, so
11200 we compute it now. */
11201 scope = get_scope_of_declarator (declarator);
11203 /* If we're allowing GNU extensions, look for an asm-specification
11205 if (cp_parser_allow_gnu_extensions_p (parser))
11207 /* Look for an asm-specification. */
11208 asm_specification = cp_parser_asm_specification_opt (parser);
11209 /* And attributes. */
11210 attributes = cp_parser_attributes_opt (parser);
11214 asm_specification = NULL_TREE;
11215 attributes = NULL_TREE;
11218 /* Peek at the next token. */
11219 token = cp_lexer_peek_token (parser->lexer);
11220 /* Check to see if the token indicates the start of a
11221 function-definition. */
11222 if (cp_parser_token_starts_function_definition_p (token))
11224 if (!function_definition_allowed_p)
11226 /* If a function-definition should not appear here, issue an
11228 cp_parser_error (parser,
11229 "a function-definition is not allowed here");
11230 return error_mark_node;
11234 /* Neither attributes nor an asm-specification are allowed
11235 on a function-definition. */
11236 if (asm_specification)
11237 error ("an asm-specification is not allowed on a function-definition");
11239 error ("attributes are not allowed on a function-definition");
11240 /* This is a function-definition. */
11241 *function_definition_p = true;
11243 /* Parse the function definition. */
11245 decl = cp_parser_save_member_function_body (parser,
11248 prefix_attributes);
11251 = (cp_parser_function_definition_from_specifiers_and_declarator
11252 (parser, decl_specifiers, prefix_attributes, declarator));
11260 Only in function declarations for constructors, destructors, and
11261 type conversions can the decl-specifier-seq be omitted.
11263 We explicitly postpone this check past the point where we handle
11264 function-definitions because we tolerate function-definitions
11265 that are missing their return types in some modes. */
11266 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11268 cp_parser_error (parser,
11269 "expected constructor, destructor, or type conversion");
11270 return error_mark_node;
11273 /* An `=' or an `(' indicates an initializer. */
11274 if (token->type == CPP_EQ
11275 || token->type == CPP_OPEN_PAREN)
11277 is_initialized = true;
11278 initialization_kind = token->type;
11282 /* If the init-declarator isn't initialized and isn't followed by a
11283 `,' or `;', it's not a valid init-declarator. */
11284 if (token->type != CPP_COMMA
11285 && token->type != CPP_SEMICOLON)
11287 cp_parser_error (parser, "expected initializer");
11288 return error_mark_node;
11290 is_initialized = false;
11291 initialization_kind = CPP_EOF;
11294 /* Because start_decl has side-effects, we should only call it if we
11295 know we're going ahead. By this point, we know that we cannot
11296 possibly be looking at any other construct. */
11297 cp_parser_commit_to_tentative_parse (parser);
11299 /* If the decl specifiers were bad, issue an error now that we're
11300 sure this was intended to be a declarator. Then continue
11301 declaring the variable(s), as int, to try to cut down on further
11303 if (decl_specifiers->any_specifiers_p
11304 && decl_specifiers->type == error_mark_node)
11306 cp_parser_error (parser, "invalid type in declaration");
11307 decl_specifiers->type = integer_type_node;
11310 /* Check to see whether or not this declaration is a friend. */
11311 friend_p = cp_parser_friend_p (decl_specifiers);
11313 /* Enter the newly declared entry in the symbol table. If we're
11314 processing a declaration in a class-specifier, we wait until
11315 after processing the initializer. */
11318 if (parser->in_unbraced_linkage_specification_p)
11319 decl_specifiers->storage_class = sc_extern;
11320 decl = start_decl (declarator, decl_specifiers,
11321 is_initialized, attributes, prefix_attributes,
11325 /* Enter the SCOPE. That way unqualified names appearing in the
11326 initializer will be looked up in SCOPE. */
11327 pushed_scope = push_scope (scope);
11329 /* Perform deferred access control checks, now that we know in which
11330 SCOPE the declared entity resides. */
11331 if (!member_p && decl)
11333 tree saved_current_function_decl = NULL_TREE;
11335 /* If the entity being declared is a function, pretend that we
11336 are in its scope. If it is a `friend', it may have access to
11337 things that would not otherwise be accessible. */
11338 if (TREE_CODE (decl) == FUNCTION_DECL)
11340 saved_current_function_decl = current_function_decl;
11341 current_function_decl = decl;
11344 /* Perform access checks for template parameters. */
11345 cp_parser_perform_template_parameter_access_checks (checks);
11347 /* Perform the access control checks for the declarator and the
11348 the decl-specifiers. */
11349 perform_deferred_access_checks ();
11351 /* Restore the saved value. */
11352 if (TREE_CODE (decl) == FUNCTION_DECL)
11353 current_function_decl = saved_current_function_decl;
11356 /* Parse the initializer. */
11357 initializer = NULL_TREE;
11358 is_parenthesized_init = false;
11359 is_non_constant_init = true;
11360 if (is_initialized)
11362 if (function_declarator_p (declarator))
11364 if (initialization_kind == CPP_EQ)
11365 initializer = cp_parser_pure_specifier (parser);
11368 /* If the declaration was erroneous, we don't really
11369 know what the user intended, so just silently
11370 consume the initializer. */
11371 if (decl != error_mark_node)
11372 error ("initializer provided for function");
11373 cp_parser_skip_to_closing_parenthesis (parser,
11374 /*recovering=*/true,
11375 /*or_comma=*/false,
11376 /*consume_paren=*/true);
11380 initializer = cp_parser_initializer (parser,
11381 &is_parenthesized_init,
11382 &is_non_constant_init);
11385 /* The old parser allows attributes to appear after a parenthesized
11386 initializer. Mark Mitchell proposed removing this functionality
11387 on the GCC mailing lists on 2002-08-13. This parser accepts the
11388 attributes -- but ignores them. */
11389 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11390 if (cp_parser_attributes_opt (parser))
11391 warning (OPT_Wattributes,
11392 "attributes after parenthesized initializer ignored");
11394 /* For an in-class declaration, use `grokfield' to create the
11400 pop_scope (pushed_scope);
11401 pushed_scope = false;
11403 decl = grokfield (declarator, decl_specifiers,
11404 initializer, !is_non_constant_init,
11405 /*asmspec=*/NULL_TREE,
11406 prefix_attributes);
11407 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11408 cp_parser_save_default_args (parser, decl);
11411 /* Finish processing the declaration. But, skip friend
11413 if (!friend_p && decl && decl != error_mark_node)
11415 cp_finish_decl (decl,
11416 initializer, !is_non_constant_init,
11418 /* If the initializer is in parentheses, then this is
11419 a direct-initialization, which means that an
11420 `explicit' constructor is OK. Otherwise, an
11421 `explicit' constructor cannot be used. */
11422 ((is_parenthesized_init || !is_initialized)
11423 ? 0 : LOOKUP_ONLYCONVERTING));
11425 if (!friend_p && pushed_scope)
11426 pop_scope (pushed_scope);
11431 /* Parse a declarator.
11435 ptr-operator declarator
11437 abstract-declarator:
11438 ptr-operator abstract-declarator [opt]
11439 direct-abstract-declarator
11444 attributes [opt] direct-declarator
11445 attributes [opt] ptr-operator declarator
11447 abstract-declarator:
11448 attributes [opt] ptr-operator abstract-declarator [opt]
11449 attributes [opt] direct-abstract-declarator
11451 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11452 detect constructor, destructor or conversion operators. It is set
11453 to -1 if the declarator is a name, and +1 if it is a
11454 function. Otherwise it is set to zero. Usually you just want to
11455 test for >0, but internally the negative value is used.
11457 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11458 a decl-specifier-seq unless it declares a constructor, destructor,
11459 or conversion. It might seem that we could check this condition in
11460 semantic analysis, rather than parsing, but that makes it difficult
11461 to handle something like `f()'. We want to notice that there are
11462 no decl-specifiers, and therefore realize that this is an
11463 expression, not a declaration.)
11465 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11466 the declarator is a direct-declarator of the form "(...)".
11468 MEMBER_P is true iff this declarator is a member-declarator. */
11470 static cp_declarator *
11471 cp_parser_declarator (cp_parser* parser,
11472 cp_parser_declarator_kind dcl_kind,
11473 int* ctor_dtor_or_conv_p,
11474 bool* parenthesized_p,
11478 cp_declarator *declarator;
11479 enum tree_code code;
11480 cp_cv_quals cv_quals;
11482 tree attributes = NULL_TREE;
11484 /* Assume this is not a constructor, destructor, or type-conversion
11486 if (ctor_dtor_or_conv_p)
11487 *ctor_dtor_or_conv_p = 0;
11489 if (cp_parser_allow_gnu_extensions_p (parser))
11490 attributes = cp_parser_attributes_opt (parser);
11492 /* Peek at the next token. */
11493 token = cp_lexer_peek_token (parser->lexer);
11495 /* Check for the ptr-operator production. */
11496 cp_parser_parse_tentatively (parser);
11497 /* Parse the ptr-operator. */
11498 code = cp_parser_ptr_operator (parser,
11501 /* If that worked, then we have a ptr-operator. */
11502 if (cp_parser_parse_definitely (parser))
11504 /* If a ptr-operator was found, then this declarator was not
11506 if (parenthesized_p)
11507 *parenthesized_p = true;
11508 /* The dependent declarator is optional if we are parsing an
11509 abstract-declarator. */
11510 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11511 cp_parser_parse_tentatively (parser);
11513 /* Parse the dependent declarator. */
11514 declarator = cp_parser_declarator (parser, dcl_kind,
11515 /*ctor_dtor_or_conv_p=*/NULL,
11516 /*parenthesized_p=*/NULL,
11517 /*member_p=*/false);
11519 /* If we are parsing an abstract-declarator, we must handle the
11520 case where the dependent declarator is absent. */
11521 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11522 && !cp_parser_parse_definitely (parser))
11525 /* Build the representation of the ptr-operator. */
11527 declarator = make_ptrmem_declarator (cv_quals,
11530 else if (code == INDIRECT_REF)
11531 declarator = make_pointer_declarator (cv_quals, declarator);
11533 declarator = make_reference_declarator (cv_quals, declarator);
11535 /* Everything else is a direct-declarator. */
11538 if (parenthesized_p)
11539 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11541 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11542 ctor_dtor_or_conv_p,
11546 if (attributes && declarator && declarator != cp_error_declarator)
11547 declarator->attributes = attributes;
11552 /* Parse a direct-declarator or direct-abstract-declarator.
11556 direct-declarator ( parameter-declaration-clause )
11557 cv-qualifier-seq [opt]
11558 exception-specification [opt]
11559 direct-declarator [ constant-expression [opt] ]
11562 direct-abstract-declarator:
11563 direct-abstract-declarator [opt]
11564 ( parameter-declaration-clause )
11565 cv-qualifier-seq [opt]
11566 exception-specification [opt]
11567 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11568 ( abstract-declarator )
11570 Returns a representation of the declarator. DCL_KIND is
11571 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11572 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11573 we are parsing a direct-declarator. It is
11574 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11575 of ambiguity we prefer an abstract declarator, as per
11576 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11577 cp_parser_declarator. */
11579 static cp_declarator *
11580 cp_parser_direct_declarator (cp_parser* parser,
11581 cp_parser_declarator_kind dcl_kind,
11582 int* ctor_dtor_or_conv_p,
11586 cp_declarator *declarator = NULL;
11587 tree scope = NULL_TREE;
11588 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11589 bool saved_in_declarator_p = parser->in_declarator_p;
11591 tree pushed_scope = NULL_TREE;
11595 /* Peek at the next token. */
11596 token = cp_lexer_peek_token (parser->lexer);
11597 if (token->type == CPP_OPEN_PAREN)
11599 /* This is either a parameter-declaration-clause, or a
11600 parenthesized declarator. When we know we are parsing a
11601 named declarator, it must be a parenthesized declarator
11602 if FIRST is true. For instance, `(int)' is a
11603 parameter-declaration-clause, with an omitted
11604 direct-abstract-declarator. But `((*))', is a
11605 parenthesized abstract declarator. Finally, when T is a
11606 template parameter `(T)' is a
11607 parameter-declaration-clause, and not a parenthesized
11610 We first try and parse a parameter-declaration-clause,
11611 and then try a nested declarator (if FIRST is true).
11613 It is not an error for it not to be a
11614 parameter-declaration-clause, even when FIRST is
11620 The first is the declaration of a function while the
11621 second is a the definition of a variable, including its
11624 Having seen only the parenthesis, we cannot know which of
11625 these two alternatives should be selected. Even more
11626 complex are examples like:
11631 The former is a function-declaration; the latter is a
11632 variable initialization.
11634 Thus again, we try a parameter-declaration-clause, and if
11635 that fails, we back out and return. */
11637 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11639 cp_parameter_declarator *params;
11640 unsigned saved_num_template_parameter_lists;
11642 /* In a member-declarator, the only valid interpretation
11643 of a parenthesis is the start of a
11644 parameter-declaration-clause. (It is invalid to
11645 initialize a static data member with a parenthesized
11646 initializer; only the "=" form of initialization is
11649 cp_parser_parse_tentatively (parser);
11651 /* Consume the `('. */
11652 cp_lexer_consume_token (parser->lexer);
11655 /* If this is going to be an abstract declarator, we're
11656 in a declarator and we can't have default args. */
11657 parser->default_arg_ok_p = false;
11658 parser->in_declarator_p = true;
11661 /* Inside the function parameter list, surrounding
11662 template-parameter-lists do not apply. */
11663 saved_num_template_parameter_lists
11664 = parser->num_template_parameter_lists;
11665 parser->num_template_parameter_lists = 0;
11667 /* Parse the parameter-declaration-clause. */
11668 params = cp_parser_parameter_declaration_clause (parser);
11670 parser->num_template_parameter_lists
11671 = saved_num_template_parameter_lists;
11673 /* If all went well, parse the cv-qualifier-seq and the
11674 exception-specification. */
11675 if (member_p || cp_parser_parse_definitely (parser))
11677 cp_cv_quals cv_quals;
11678 tree exception_specification;
11680 if (ctor_dtor_or_conv_p)
11681 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11683 /* Consume the `)'. */
11684 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11686 /* Parse the cv-qualifier-seq. */
11687 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11688 /* And the exception-specification. */
11689 exception_specification
11690 = cp_parser_exception_specification_opt (parser);
11692 /* Create the function-declarator. */
11693 declarator = make_call_declarator (declarator,
11696 exception_specification);
11697 /* Any subsequent parameter lists are to do with
11698 return type, so are not those of the declared
11700 parser->default_arg_ok_p = false;
11702 /* Repeat the main loop. */
11707 /* If this is the first, we can try a parenthesized
11711 bool saved_in_type_id_in_expr_p;
11713 parser->default_arg_ok_p = saved_default_arg_ok_p;
11714 parser->in_declarator_p = saved_in_declarator_p;
11716 /* Consume the `('. */
11717 cp_lexer_consume_token (parser->lexer);
11718 /* Parse the nested declarator. */
11719 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11720 parser->in_type_id_in_expr_p = true;
11722 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11723 /*parenthesized_p=*/NULL,
11725 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11727 /* Expect a `)'. */
11728 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11729 declarator = cp_error_declarator;
11730 if (declarator == cp_error_declarator)
11733 goto handle_declarator;
11735 /* Otherwise, we must be done. */
11739 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11740 && token->type == CPP_OPEN_SQUARE)
11742 /* Parse an array-declarator. */
11745 if (ctor_dtor_or_conv_p)
11746 *ctor_dtor_or_conv_p = 0;
11749 parser->default_arg_ok_p = false;
11750 parser->in_declarator_p = true;
11751 /* Consume the `['. */
11752 cp_lexer_consume_token (parser->lexer);
11753 /* Peek at the next token. */
11754 token = cp_lexer_peek_token (parser->lexer);
11755 /* If the next token is `]', then there is no
11756 constant-expression. */
11757 if (token->type != CPP_CLOSE_SQUARE)
11759 bool non_constant_p;
11762 = cp_parser_constant_expression (parser,
11763 /*allow_non_constant=*/true,
11765 if (!non_constant_p)
11766 bounds = fold_non_dependent_expr (bounds);
11767 /* Normally, the array bound must be an integral constant
11768 expression. However, as an extension, we allow VLAs
11769 in function scopes. */
11770 else if (!parser->in_function_body)
11772 error ("array bound is not an integer constant");
11773 bounds = error_mark_node;
11777 bounds = NULL_TREE;
11778 /* Look for the closing `]'. */
11779 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11781 declarator = cp_error_declarator;
11785 declarator = make_array_declarator (declarator, bounds);
11787 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11789 tree qualifying_scope;
11790 tree unqualified_name;
11791 special_function_kind sfk;
11794 /* Parse a declarator-id */
11795 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11797 cp_parser_parse_tentatively (parser);
11799 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11800 qualifying_scope = parser->scope;
11803 if (!cp_parser_parse_definitely (parser))
11804 unqualified_name = error_mark_node;
11805 else if (unqualified_name
11806 && (qualifying_scope
11807 || (TREE_CODE (unqualified_name)
11808 != IDENTIFIER_NODE)))
11810 cp_parser_error (parser, "expected unqualified-id");
11811 unqualified_name = error_mark_node;
11815 if (!unqualified_name)
11817 if (unqualified_name == error_mark_node)
11819 declarator = cp_error_declarator;
11823 if (qualifying_scope && at_namespace_scope_p ()
11824 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11826 /* In the declaration of a member of a template class
11827 outside of the class itself, the SCOPE will sometimes
11828 be a TYPENAME_TYPE. For example, given:
11830 template <typename T>
11831 int S<T>::R::i = 3;
11833 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11834 this context, we must resolve S<T>::R to an ordinary
11835 type, rather than a typename type.
11837 The reason we normally avoid resolving TYPENAME_TYPEs
11838 is that a specialization of `S' might render
11839 `S<T>::R' not a type. However, if `S' is
11840 specialized, then this `i' will not be used, so there
11841 is no harm in resolving the types here. */
11844 /* Resolve the TYPENAME_TYPE. */
11845 type = resolve_typename_type (qualifying_scope,
11846 /*only_current_p=*/false);
11847 /* If that failed, the declarator is invalid. */
11848 if (type == error_mark_node)
11849 error ("%<%T::%D%> is not a type",
11850 TYPE_CONTEXT (qualifying_scope),
11851 TYPE_IDENTIFIER (qualifying_scope));
11852 qualifying_scope = type;
11856 if (unqualified_name)
11860 if (qualifying_scope
11861 && CLASS_TYPE_P (qualifying_scope))
11862 class_type = qualifying_scope;
11864 class_type = current_class_type;
11866 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11868 tree name_type = TREE_TYPE (unqualified_name);
11869 if (class_type && same_type_p (name_type, class_type))
11871 if (qualifying_scope
11872 && CLASSTYPE_USE_TEMPLATE (name_type))
11874 error ("invalid use of constructor as a template");
11875 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11876 "name the constructor in a qualified name",
11878 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11879 class_type, name_type);
11880 declarator = cp_error_declarator;
11884 unqualified_name = constructor_name (class_type);
11888 /* We do not attempt to print the declarator
11889 here because we do not have enough
11890 information about its original syntactic
11892 cp_parser_error (parser, "invalid declarator");
11893 declarator = cp_error_declarator;
11900 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11901 sfk = sfk_destructor;
11902 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11903 sfk = sfk_conversion;
11904 else if (/* There's no way to declare a constructor
11905 for an anonymous type, even if the type
11906 got a name for linkage purposes. */
11907 !TYPE_WAS_ANONYMOUS (class_type)
11908 && constructor_name_p (unqualified_name,
11911 unqualified_name = constructor_name (class_type);
11912 sfk = sfk_constructor;
11915 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11916 *ctor_dtor_or_conv_p = -1;
11919 declarator = make_id_declarator (qualifying_scope,
11922 declarator->id_loc = token->location;
11924 handle_declarator:;
11925 scope = get_scope_of_declarator (declarator);
11927 /* Any names that appear after the declarator-id for a
11928 member are looked up in the containing scope. */
11929 pushed_scope = push_scope (scope);
11930 parser->in_declarator_p = true;
11931 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11932 || (declarator && declarator->kind == cdk_id))
11933 /* Default args are only allowed on function
11935 parser->default_arg_ok_p = saved_default_arg_ok_p;
11937 parser->default_arg_ok_p = false;
11946 /* For an abstract declarator, we might wind up with nothing at this
11947 point. That's an error; the declarator is not optional. */
11949 cp_parser_error (parser, "expected declarator");
11951 /* If we entered a scope, we must exit it now. */
11953 pop_scope (pushed_scope);
11955 parser->default_arg_ok_p = saved_default_arg_ok_p;
11956 parser->in_declarator_p = saved_in_declarator_p;
11961 /* Parse a ptr-operator.
11964 * cv-qualifier-seq [opt]
11966 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11971 & cv-qualifier-seq [opt]
11973 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11974 Returns ADDR_EXPR if a reference was used. In the case of a
11975 pointer-to-member, *TYPE is filled in with the TYPE containing the
11976 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11977 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11978 ERROR_MARK if an error occurred. */
11980 static enum tree_code
11981 cp_parser_ptr_operator (cp_parser* parser,
11983 cp_cv_quals *cv_quals)
11985 enum tree_code code = ERROR_MARK;
11988 /* Assume that it's not a pointer-to-member. */
11990 /* And that there are no cv-qualifiers. */
11991 *cv_quals = TYPE_UNQUALIFIED;
11993 /* Peek at the next token. */
11994 token = cp_lexer_peek_token (parser->lexer);
11995 /* If it's a `*' or `&' we have a pointer or reference. */
11996 if (token->type == CPP_MULT || token->type == CPP_AND)
11998 /* Remember which ptr-operator we were processing. */
11999 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12001 /* Consume the `*' or `&'. */
12002 cp_lexer_consume_token (parser->lexer);
12004 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12005 `&', if we are allowing GNU extensions. (The only qualifier
12006 that can legally appear after `&' is `restrict', but that is
12007 enforced during semantic analysis. */
12008 if (code == INDIRECT_REF
12009 || cp_parser_allow_gnu_extensions_p (parser))
12010 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12014 /* Try the pointer-to-member case. */
12015 cp_parser_parse_tentatively (parser);
12016 /* Look for the optional `::' operator. */
12017 cp_parser_global_scope_opt (parser,
12018 /*current_scope_valid_p=*/false);
12019 /* Look for the nested-name specifier. */
12020 cp_parser_nested_name_specifier (parser,
12021 /*typename_keyword_p=*/false,
12022 /*check_dependency_p=*/true,
12024 /*is_declaration=*/false);
12025 /* If we found it, and the next token is a `*', then we are
12026 indeed looking at a pointer-to-member operator. */
12027 if (!cp_parser_error_occurred (parser)
12028 && cp_parser_require (parser, CPP_MULT, "`*'"))
12030 /* Indicate that the `*' operator was used. */
12031 code = INDIRECT_REF;
12033 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12034 error ("%qD is a namespace", parser->scope);
12037 /* The type of which the member is a member is given by the
12039 *type = parser->scope;
12040 /* The next name will not be qualified. */
12041 parser->scope = NULL_TREE;
12042 parser->qualifying_scope = NULL_TREE;
12043 parser->object_scope = NULL_TREE;
12044 /* Look for the optional cv-qualifier-seq. */
12045 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12048 /* If that didn't work we don't have a ptr-operator. */
12049 if (!cp_parser_parse_definitely (parser))
12050 cp_parser_error (parser, "expected ptr-operator");
12056 /* Parse an (optional) cv-qualifier-seq.
12059 cv-qualifier cv-qualifier-seq [opt]
12070 Returns a bitmask representing the cv-qualifiers. */
12073 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12075 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12080 cp_cv_quals cv_qualifier;
12082 /* Peek at the next token. */
12083 token = cp_lexer_peek_token (parser->lexer);
12084 /* See if it's a cv-qualifier. */
12085 switch (token->keyword)
12088 cv_qualifier = TYPE_QUAL_CONST;
12092 cv_qualifier = TYPE_QUAL_VOLATILE;
12096 cv_qualifier = TYPE_QUAL_RESTRICT;
12100 cv_qualifier = TYPE_UNQUALIFIED;
12107 if (cv_quals & cv_qualifier)
12109 error ("duplicate cv-qualifier");
12110 cp_lexer_purge_token (parser->lexer);
12114 cp_lexer_consume_token (parser->lexer);
12115 cv_quals |= cv_qualifier;
12122 /* Parse a declarator-id.
12126 :: [opt] nested-name-specifier [opt] type-name
12128 In the `id-expression' case, the value returned is as for
12129 cp_parser_id_expression if the id-expression was an unqualified-id.
12130 If the id-expression was a qualified-id, then a SCOPE_REF is
12131 returned. The first operand is the scope (either a NAMESPACE_DECL
12132 or TREE_TYPE), but the second is still just a representation of an
12136 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12139 /* The expression must be an id-expression. Assume that qualified
12140 names are the names of types so that:
12143 int S<T>::R::i = 3;
12145 will work; we must treat `S<T>::R' as the name of a type.
12146 Similarly, assume that qualified names are templates, where
12150 int S<T>::R<T>::i = 3;
12153 id = cp_parser_id_expression (parser,
12154 /*template_keyword_p=*/false,
12155 /*check_dependency_p=*/false,
12156 /*template_p=*/NULL,
12157 /*declarator_p=*/true,
12159 if (id && BASELINK_P (id))
12160 id = BASELINK_FUNCTIONS (id);
12164 /* Parse a type-id.
12167 type-specifier-seq abstract-declarator [opt]
12169 Returns the TYPE specified. */
12172 cp_parser_type_id (cp_parser* parser)
12174 cp_decl_specifier_seq type_specifier_seq;
12175 cp_declarator *abstract_declarator;
12177 /* Parse the type-specifier-seq. */
12178 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12179 &type_specifier_seq);
12180 if (type_specifier_seq.type == error_mark_node)
12181 return error_mark_node;
12183 /* There might or might not be an abstract declarator. */
12184 cp_parser_parse_tentatively (parser);
12185 /* Look for the declarator. */
12186 abstract_declarator
12187 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12188 /*parenthesized_p=*/NULL,
12189 /*member_p=*/false);
12190 /* Check to see if there really was a declarator. */
12191 if (!cp_parser_parse_definitely (parser))
12192 abstract_declarator = NULL;
12194 return groktypename (&type_specifier_seq, abstract_declarator);
12197 /* Parse a type-specifier-seq.
12199 type-specifier-seq:
12200 type-specifier type-specifier-seq [opt]
12204 type-specifier-seq:
12205 attributes type-specifier-seq [opt]
12207 If IS_CONDITION is true, we are at the start of a "condition",
12208 e.g., we've just seen "if (".
12210 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12213 cp_parser_type_specifier_seq (cp_parser* parser,
12215 cp_decl_specifier_seq *type_specifier_seq)
12217 bool seen_type_specifier = false;
12218 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12220 /* Clear the TYPE_SPECIFIER_SEQ. */
12221 clear_decl_specs (type_specifier_seq);
12223 /* Parse the type-specifiers and attributes. */
12226 tree type_specifier;
12227 bool is_cv_qualifier;
12229 /* Check for attributes first. */
12230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12232 type_specifier_seq->attributes =
12233 chainon (type_specifier_seq->attributes,
12234 cp_parser_attributes_opt (parser));
12238 /* Look for the type-specifier. */
12239 type_specifier = cp_parser_type_specifier (parser,
12241 type_specifier_seq,
12242 /*is_declaration=*/false,
12245 if (!type_specifier)
12247 /* If the first type-specifier could not be found, this is not a
12248 type-specifier-seq at all. */
12249 if (!seen_type_specifier)
12251 cp_parser_error (parser, "expected type-specifier");
12252 type_specifier_seq->type = error_mark_node;
12255 /* If subsequent type-specifiers could not be found, the
12256 type-specifier-seq is complete. */
12260 seen_type_specifier = true;
12261 /* The standard says that a condition can be:
12263 type-specifier-seq declarator = assignment-expression
12270 we should treat the "S" as a declarator, not as a
12271 type-specifier. The standard doesn't say that explicitly for
12272 type-specifier-seq, but it does say that for
12273 decl-specifier-seq in an ordinary declaration. Perhaps it
12274 would be clearer just to allow a decl-specifier-seq here, and
12275 then add a semantic restriction that if any decl-specifiers
12276 that are not type-specifiers appear, the program is invalid. */
12277 if (is_condition && !is_cv_qualifier)
12278 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12281 cp_parser_check_decl_spec (type_specifier_seq);
12284 /* Parse a parameter-declaration-clause.
12286 parameter-declaration-clause:
12287 parameter-declaration-list [opt] ... [opt]
12288 parameter-declaration-list , ...
12290 Returns a representation for the parameter declarations. A return
12291 value of NULL indicates a parameter-declaration-clause consisting
12292 only of an ellipsis. */
12294 static cp_parameter_declarator *
12295 cp_parser_parameter_declaration_clause (cp_parser* parser)
12297 cp_parameter_declarator *parameters;
12302 /* Peek at the next token. */
12303 token = cp_lexer_peek_token (parser->lexer);
12304 /* Check for trivial parameter-declaration-clauses. */
12305 if (token->type == CPP_ELLIPSIS)
12307 /* Consume the `...' token. */
12308 cp_lexer_consume_token (parser->lexer);
12311 else if (token->type == CPP_CLOSE_PAREN)
12312 /* There are no parameters. */
12314 #ifndef NO_IMPLICIT_EXTERN_C
12315 if (in_system_header && current_class_type == NULL
12316 && current_lang_name == lang_name_c)
12320 return no_parameters;
12322 /* Check for `(void)', too, which is a special case. */
12323 else if (token->keyword == RID_VOID
12324 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12325 == CPP_CLOSE_PAREN))
12327 /* Consume the `void' token. */
12328 cp_lexer_consume_token (parser->lexer);
12329 /* There are no parameters. */
12330 return no_parameters;
12333 /* Parse the parameter-declaration-list. */
12334 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12335 /* If a parse error occurred while parsing the
12336 parameter-declaration-list, then the entire
12337 parameter-declaration-clause is erroneous. */
12341 /* Peek at the next token. */
12342 token = cp_lexer_peek_token (parser->lexer);
12343 /* If it's a `,', the clause should terminate with an ellipsis. */
12344 if (token->type == CPP_COMMA)
12346 /* Consume the `,'. */
12347 cp_lexer_consume_token (parser->lexer);
12348 /* Expect an ellipsis. */
12350 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12352 /* It might also be `...' if the optional trailing `,' was
12354 else if (token->type == CPP_ELLIPSIS)
12356 /* Consume the `...' token. */
12357 cp_lexer_consume_token (parser->lexer);
12358 /* And remember that we saw it. */
12362 ellipsis_p = false;
12364 /* Finish the parameter list. */
12365 if (parameters && ellipsis_p)
12366 parameters->ellipsis_p = true;
12371 /* Parse a parameter-declaration-list.
12373 parameter-declaration-list:
12374 parameter-declaration
12375 parameter-declaration-list , parameter-declaration
12377 Returns a representation of the parameter-declaration-list, as for
12378 cp_parser_parameter_declaration_clause. However, the
12379 `void_list_node' is never appended to the list. Upon return,
12380 *IS_ERROR will be true iff an error occurred. */
12382 static cp_parameter_declarator *
12383 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12385 cp_parameter_declarator *parameters = NULL;
12386 cp_parameter_declarator **tail = ¶meters;
12387 bool saved_in_unbraced_linkage_specification_p;
12389 /* Assume all will go well. */
12391 /* The special considerations that apply to a function within an
12392 unbraced linkage specifications do not apply to the parameters
12393 to the function. */
12394 saved_in_unbraced_linkage_specification_p
12395 = parser->in_unbraced_linkage_specification_p;
12396 parser->in_unbraced_linkage_specification_p = false;
12398 /* Look for more parameters. */
12401 cp_parameter_declarator *parameter;
12402 bool parenthesized_p;
12403 /* Parse the parameter. */
12405 = cp_parser_parameter_declaration (parser,
12406 /*template_parm_p=*/false,
12409 /* If a parse error occurred parsing the parameter declaration,
12410 then the entire parameter-declaration-list is erroneous. */
12417 /* Add the new parameter to the list. */
12419 tail = ¶meter->next;
12421 /* Peek at the next token. */
12422 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12423 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12424 /* These are for Objective-C++ */
12425 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12426 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12427 /* The parameter-declaration-list is complete. */
12429 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12433 /* Peek at the next token. */
12434 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12435 /* If it's an ellipsis, then the list is complete. */
12436 if (token->type == CPP_ELLIPSIS)
12438 /* Otherwise, there must be more parameters. Consume the
12440 cp_lexer_consume_token (parser->lexer);
12441 /* When parsing something like:
12443 int i(float f, double d)
12445 we can tell after seeing the declaration for "f" that we
12446 are not looking at an initialization of a variable "i",
12447 but rather at the declaration of a function "i".
12449 Due to the fact that the parsing of template arguments
12450 (as specified to a template-id) requires backtracking we
12451 cannot use this technique when inside a template argument
12453 if (!parser->in_template_argument_list_p
12454 && !parser->in_type_id_in_expr_p
12455 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12456 /* However, a parameter-declaration of the form
12457 "foat(f)" (which is a valid declaration of a
12458 parameter "f") can also be interpreted as an
12459 expression (the conversion of "f" to "float"). */
12460 && !parenthesized_p)
12461 cp_parser_commit_to_tentative_parse (parser);
12465 cp_parser_error (parser, "expected %<,%> or %<...%>");
12466 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12467 cp_parser_skip_to_closing_parenthesis (parser,
12468 /*recovering=*/true,
12469 /*or_comma=*/false,
12470 /*consume_paren=*/false);
12475 parser->in_unbraced_linkage_specification_p
12476 = saved_in_unbraced_linkage_specification_p;
12481 /* Parse a parameter declaration.
12483 parameter-declaration:
12484 decl-specifier-seq declarator
12485 decl-specifier-seq declarator = assignment-expression
12486 decl-specifier-seq abstract-declarator [opt]
12487 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12489 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12490 declares a template parameter. (In that case, a non-nested `>'
12491 token encountered during the parsing of the assignment-expression
12492 is not interpreted as a greater-than operator.)
12494 Returns a representation of the parameter, or NULL if an error
12495 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12496 true iff the declarator is of the form "(p)". */
12498 static cp_parameter_declarator *
12499 cp_parser_parameter_declaration (cp_parser *parser,
12500 bool template_parm_p,
12501 bool *parenthesized_p)
12503 int declares_class_or_enum;
12504 bool greater_than_is_operator_p;
12505 cp_decl_specifier_seq decl_specifiers;
12506 cp_declarator *declarator;
12507 tree default_argument;
12509 const char *saved_message;
12511 /* In a template parameter, `>' is not an operator.
12515 When parsing a default template-argument for a non-type
12516 template-parameter, the first non-nested `>' is taken as the end
12517 of the template parameter-list rather than a greater-than
12519 greater_than_is_operator_p = !template_parm_p;
12521 /* Type definitions may not appear in parameter types. */
12522 saved_message = parser->type_definition_forbidden_message;
12523 parser->type_definition_forbidden_message
12524 = "types may not be defined in parameter types";
12526 /* Parse the declaration-specifiers. */
12527 cp_parser_decl_specifier_seq (parser,
12528 CP_PARSER_FLAGS_NONE,
12530 &declares_class_or_enum);
12531 /* If an error occurred, there's no reason to attempt to parse the
12532 rest of the declaration. */
12533 if (cp_parser_error_occurred (parser))
12535 parser->type_definition_forbidden_message = saved_message;
12539 /* Peek at the next token. */
12540 token = cp_lexer_peek_token (parser->lexer);
12541 /* If the next token is a `)', `,', `=', `>', or `...', then there
12542 is no declarator. */
12543 if (token->type == CPP_CLOSE_PAREN
12544 || token->type == CPP_COMMA
12545 || token->type == CPP_EQ
12546 || token->type == CPP_ELLIPSIS
12547 || token->type == CPP_GREATER)
12550 if (parenthesized_p)
12551 *parenthesized_p = false;
12553 /* Otherwise, there should be a declarator. */
12556 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12557 parser->default_arg_ok_p = false;
12559 /* After seeing a decl-specifier-seq, if the next token is not a
12560 "(", there is no possibility that the code is a valid
12561 expression. Therefore, if parsing tentatively, we commit at
12563 if (!parser->in_template_argument_list_p
12564 /* In an expression context, having seen:
12568 we cannot be sure whether we are looking at a
12569 function-type (taking a "char" as a parameter) or a cast
12570 of some object of type "char" to "int". */
12571 && !parser->in_type_id_in_expr_p
12572 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12573 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12574 cp_parser_commit_to_tentative_parse (parser);
12575 /* Parse the declarator. */
12576 declarator = cp_parser_declarator (parser,
12577 CP_PARSER_DECLARATOR_EITHER,
12578 /*ctor_dtor_or_conv_p=*/NULL,
12580 /*member_p=*/false);
12581 parser->default_arg_ok_p = saved_default_arg_ok_p;
12582 /* After the declarator, allow more attributes. */
12583 decl_specifiers.attributes
12584 = chainon (decl_specifiers.attributes,
12585 cp_parser_attributes_opt (parser));
12588 /* The restriction on defining new types applies only to the type
12589 of the parameter, not to the default argument. */
12590 parser->type_definition_forbidden_message = saved_message;
12592 /* If the next token is `=', then process a default argument. */
12593 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12595 bool saved_greater_than_is_operator_p;
12596 /* Consume the `='. */
12597 cp_lexer_consume_token (parser->lexer);
12599 /* If we are defining a class, then the tokens that make up the
12600 default argument must be saved and processed later. */
12601 if (!template_parm_p && at_class_scope_p ()
12602 && TYPE_BEING_DEFINED (current_class_type))
12604 unsigned depth = 0;
12605 cp_token *first_token;
12608 /* Add tokens until we have processed the entire default
12609 argument. We add the range [first_token, token). */
12610 first_token = cp_lexer_peek_token (parser->lexer);
12615 /* Peek at the next token. */
12616 token = cp_lexer_peek_token (parser->lexer);
12617 /* What we do depends on what token we have. */
12618 switch (token->type)
12620 /* In valid code, a default argument must be
12621 immediately followed by a `,' `)', or `...'. */
12623 case CPP_CLOSE_PAREN:
12625 /* If we run into a non-nested `;', `}', or `]',
12626 then the code is invalid -- but the default
12627 argument is certainly over. */
12628 case CPP_SEMICOLON:
12629 case CPP_CLOSE_BRACE:
12630 case CPP_CLOSE_SQUARE:
12633 /* Update DEPTH, if necessary. */
12634 else if (token->type == CPP_CLOSE_PAREN
12635 || token->type == CPP_CLOSE_BRACE
12636 || token->type == CPP_CLOSE_SQUARE)
12640 case CPP_OPEN_PAREN:
12641 case CPP_OPEN_SQUARE:
12642 case CPP_OPEN_BRACE:
12647 /* If we see a non-nested `>', and `>' is not an
12648 operator, then it marks the end of the default
12650 if (!depth && !greater_than_is_operator_p)
12654 /* If we run out of tokens, issue an error message. */
12656 case CPP_PRAGMA_EOL:
12657 error ("file ends in default argument");
12663 /* In these cases, we should look for template-ids.
12664 For example, if the default argument is
12665 `X<int, double>()', we need to do name lookup to
12666 figure out whether or not `X' is a template; if
12667 so, the `,' does not end the default argument.
12669 That is not yet done. */
12676 /* If we've reached the end, stop. */
12680 /* Add the token to the token block. */
12681 token = cp_lexer_consume_token (parser->lexer);
12684 /* Create a DEFAULT_ARG to represented the unparsed default
12686 default_argument = make_node (DEFAULT_ARG);
12687 DEFARG_TOKENS (default_argument)
12688 = cp_token_cache_new (first_token, token);
12689 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12691 /* Outside of a class definition, we can just parse the
12692 assignment-expression. */
12695 bool saved_local_variables_forbidden_p;
12697 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12699 saved_greater_than_is_operator_p
12700 = parser->greater_than_is_operator_p;
12701 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12702 /* Local variable names (and the `this' keyword) may not
12703 appear in a default argument. */
12704 saved_local_variables_forbidden_p
12705 = parser->local_variables_forbidden_p;
12706 parser->local_variables_forbidden_p = true;
12707 /* The default argument expression may cause implicitly
12708 defined member functions to be synthesized, which will
12709 result in garbage collection. We must treat this
12710 situation as if we were within the body of function so as
12711 to avoid collecting live data on the stack. */
12713 /* Parse the assignment-expression. */
12714 if (template_parm_p)
12715 push_deferring_access_checks (dk_no_deferred);
12717 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12718 if (template_parm_p)
12719 pop_deferring_access_checks ();
12720 /* Restore saved state. */
12722 parser->greater_than_is_operator_p
12723 = saved_greater_than_is_operator_p;
12724 parser->local_variables_forbidden_p
12725 = saved_local_variables_forbidden_p;
12727 if (!parser->default_arg_ok_p)
12729 if (!flag_pedantic_errors)
12730 warning (0, "deprecated use of default argument for parameter of non-function");
12733 error ("default arguments are only permitted for function parameters");
12734 default_argument = NULL_TREE;
12739 default_argument = NULL_TREE;
12741 return make_parameter_declarator (&decl_specifiers,
12746 /* Parse a function-body.
12749 compound_statement */
12752 cp_parser_function_body (cp_parser *parser)
12754 cp_parser_compound_statement (parser, NULL, false);
12757 /* Parse a ctor-initializer-opt followed by a function-body. Return
12758 true if a ctor-initializer was present. */
12761 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12764 bool ctor_initializer_p;
12766 /* Begin the function body. */
12767 body = begin_function_body ();
12768 /* Parse the optional ctor-initializer. */
12769 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12770 /* Parse the function-body. */
12771 cp_parser_function_body (parser);
12772 /* Finish the function body. */
12773 finish_function_body (body);
12775 return ctor_initializer_p;
12778 /* Parse an initializer.
12781 = initializer-clause
12782 ( expression-list )
12784 Returns an expression representing the initializer. If no
12785 initializer is present, NULL_TREE is returned.
12787 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12788 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12789 set to FALSE if there is no initializer present. If there is an
12790 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12791 is set to true; otherwise it is set to false. */
12794 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12795 bool* non_constant_p)
12800 /* Peek at the next token. */
12801 token = cp_lexer_peek_token (parser->lexer);
12803 /* Let our caller know whether or not this initializer was
12805 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12806 /* Assume that the initializer is constant. */
12807 *non_constant_p = false;
12809 if (token->type == CPP_EQ)
12811 /* Consume the `='. */
12812 cp_lexer_consume_token (parser->lexer);
12813 /* Parse the initializer-clause. */
12814 init = cp_parser_initializer_clause (parser, non_constant_p);
12816 else if (token->type == CPP_OPEN_PAREN)
12817 init = cp_parser_parenthesized_expression_list (parser, false,
12822 /* Anything else is an error. */
12823 cp_parser_error (parser, "expected initializer");
12824 init = error_mark_node;
12830 /* Parse an initializer-clause.
12832 initializer-clause:
12833 assignment-expression
12834 { initializer-list , [opt] }
12837 Returns an expression representing the initializer.
12839 If the `assignment-expression' production is used the value
12840 returned is simply a representation for the expression.
12842 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12843 the elements of the initializer-list (or NULL, if the last
12844 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12845 NULL_TREE. There is no way to detect whether or not the optional
12846 trailing `,' was provided. NON_CONSTANT_P is as for
12847 cp_parser_initializer. */
12850 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12854 /* Assume the expression is constant. */
12855 *non_constant_p = false;
12857 /* If it is not a `{', then we are looking at an
12858 assignment-expression. */
12859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12862 = cp_parser_constant_expression (parser,
12863 /*allow_non_constant_p=*/true,
12865 if (!*non_constant_p)
12866 initializer = fold_non_dependent_expr (initializer);
12870 /* Consume the `{' token. */
12871 cp_lexer_consume_token (parser->lexer);
12872 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12873 initializer = make_node (CONSTRUCTOR);
12874 /* If it's not a `}', then there is a non-trivial initializer. */
12875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12877 /* Parse the initializer list. */
12878 CONSTRUCTOR_ELTS (initializer)
12879 = cp_parser_initializer_list (parser, non_constant_p);
12880 /* A trailing `,' token is allowed. */
12881 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12882 cp_lexer_consume_token (parser->lexer);
12884 /* Now, there should be a trailing `}'. */
12885 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12888 return initializer;
12891 /* Parse an initializer-list.
12895 initializer-list , initializer-clause
12900 identifier : initializer-clause
12901 initializer-list, identifier : initializer-clause
12903 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12904 for the initializer. If the INDEX of the elt is non-NULL, it is the
12905 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12906 as for cp_parser_initializer. */
12908 static VEC(constructor_elt,gc) *
12909 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12911 VEC(constructor_elt,gc) *v = NULL;
12913 /* Assume all of the expressions are constant. */
12914 *non_constant_p = false;
12916 /* Parse the rest of the list. */
12922 bool clause_non_constant_p;
12924 /* If the next token is an identifier and the following one is a
12925 colon, we are looking at the GNU designated-initializer
12927 if (cp_parser_allow_gnu_extensions_p (parser)
12928 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12929 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12931 /* Warn the user that they are using an extension. */
12933 pedwarn ("ISO C++ does not allow designated initializers");
12934 /* Consume the identifier. */
12935 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12936 /* Consume the `:'. */
12937 cp_lexer_consume_token (parser->lexer);
12940 identifier = NULL_TREE;
12942 /* Parse the initializer. */
12943 initializer = cp_parser_initializer_clause (parser,
12944 &clause_non_constant_p);
12945 /* If any clause is non-constant, so is the entire initializer. */
12946 if (clause_non_constant_p)
12947 *non_constant_p = true;
12949 /* Add it to the vector. */
12950 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12952 /* If the next token is not a comma, we have reached the end of
12954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12957 /* Peek at the next token. */
12958 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12959 /* If the next token is a `}', then we're still done. An
12960 initializer-clause can have a trailing `,' after the
12961 initializer-list and before the closing `}'. */
12962 if (token->type == CPP_CLOSE_BRACE)
12965 /* Consume the `,' token. */
12966 cp_lexer_consume_token (parser->lexer);
12972 /* Classes [gram.class] */
12974 /* Parse a class-name.
12980 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12981 to indicate that names looked up in dependent types should be
12982 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12983 keyword has been used to indicate that the name that appears next
12984 is a template. TAG_TYPE indicates the explicit tag given before
12985 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12986 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12987 is the class being defined in a class-head.
12989 Returns the TYPE_DECL representing the class. */
12992 cp_parser_class_name (cp_parser *parser,
12993 bool typename_keyword_p,
12994 bool template_keyword_p,
12995 enum tag_types tag_type,
12996 bool check_dependency_p,
12998 bool is_declaration)
13005 /* All class-names start with an identifier. */
13006 token = cp_lexer_peek_token (parser->lexer);
13007 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13009 cp_parser_error (parser, "expected class-name");
13010 return error_mark_node;
13013 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13014 to a template-id, so we save it here. */
13015 scope = parser->scope;
13016 if (scope == error_mark_node)
13017 return error_mark_node;
13019 /* Any name names a type if we're following the `typename' keyword
13020 in a qualified name where the enclosing scope is type-dependent. */
13021 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13022 && dependent_type_p (scope));
13023 /* Handle the common case (an identifier, but not a template-id)
13025 if (token->type == CPP_NAME
13026 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13028 cp_token *identifier_token;
13032 /* Look for the identifier. */
13033 identifier_token = cp_lexer_peek_token (parser->lexer);
13034 ambiguous_p = identifier_token->ambiguous_p;
13035 identifier = cp_parser_identifier (parser);
13036 /* If the next token isn't an identifier, we are certainly not
13037 looking at a class-name. */
13038 if (identifier == error_mark_node)
13039 decl = error_mark_node;
13040 /* If we know this is a type-name, there's no need to look it
13042 else if (typename_p)
13046 tree ambiguous_decls;
13047 /* If we already know that this lookup is ambiguous, then
13048 we've already issued an error message; there's no reason
13052 cp_parser_simulate_error (parser);
13053 return error_mark_node;
13055 /* If the next token is a `::', then the name must be a type
13058 [basic.lookup.qual]
13060 During the lookup for a name preceding the :: scope
13061 resolution operator, object, function, and enumerator
13062 names are ignored. */
13063 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13064 tag_type = typename_type;
13065 /* Look up the name. */
13066 decl = cp_parser_lookup_name (parser, identifier,
13068 /*is_template=*/false,
13069 /*is_namespace=*/false,
13070 check_dependency_p,
13072 if (ambiguous_decls)
13074 error ("reference to %qD is ambiguous", identifier);
13075 print_candidates (ambiguous_decls);
13076 if (cp_parser_parsing_tentatively (parser))
13078 identifier_token->ambiguous_p = true;
13079 cp_parser_simulate_error (parser);
13081 return error_mark_node;
13087 /* Try a template-id. */
13088 decl = cp_parser_template_id (parser, template_keyword_p,
13089 check_dependency_p,
13091 if (decl == error_mark_node)
13092 return error_mark_node;
13095 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13097 /* If this is a typename, create a TYPENAME_TYPE. */
13098 if (typename_p && decl != error_mark_node)
13100 decl = make_typename_type (scope, decl, typename_type,
13101 /*complain=*/tf_error);
13102 if (decl != error_mark_node)
13103 decl = TYPE_NAME (decl);
13106 /* Check to see that it is really the name of a class. */
13107 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13108 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13109 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13110 /* Situations like this:
13112 template <typename T> struct A {
13113 typename T::template X<int>::I i;
13116 are problematic. Is `T::template X<int>' a class-name? The
13117 standard does not seem to be definitive, but there is no other
13118 valid interpretation of the following `::'. Therefore, those
13119 names are considered class-names. */
13121 decl = make_typename_type (scope, decl, tag_type, tf_error);
13122 if (decl != error_mark_node)
13123 decl = TYPE_NAME (decl);
13125 else if (TREE_CODE (decl) != TYPE_DECL
13126 || TREE_TYPE (decl) == error_mark_node
13127 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13128 decl = error_mark_node;
13130 if (decl == error_mark_node)
13131 cp_parser_error (parser, "expected class-name");
13136 /* Parse a class-specifier.
13139 class-head { member-specification [opt] }
13141 Returns the TREE_TYPE representing the class. */
13144 cp_parser_class_specifier (cp_parser* parser)
13148 tree attributes = NULL_TREE;
13149 int has_trailing_semicolon;
13150 bool nested_name_specifier_p;
13151 unsigned saved_num_template_parameter_lists;
13152 bool saved_in_function_body;
13153 tree old_scope = NULL_TREE;
13154 tree scope = NULL_TREE;
13157 push_deferring_access_checks (dk_no_deferred);
13159 /* Parse the class-head. */
13160 type = cp_parser_class_head (parser,
13161 &nested_name_specifier_p,
13164 /* If the class-head was a semantic disaster, skip the entire body
13168 cp_parser_skip_to_end_of_block_or_statement (parser);
13169 pop_deferring_access_checks ();
13170 return error_mark_node;
13173 /* Look for the `{'. */
13174 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13176 pop_deferring_access_checks ();
13177 return error_mark_node;
13180 /* Process the base classes. If they're invalid, skip the
13181 entire class body. */
13182 if (!xref_basetypes (type, bases))
13184 cp_parser_skip_to_closing_brace (parser);
13186 /* Consuming the closing brace yields better error messages
13188 cp_lexer_consume_token (parser->lexer);
13189 pop_deferring_access_checks ();
13190 return error_mark_node;
13193 /* Issue an error message if type-definitions are forbidden here. */
13194 cp_parser_check_type_definition (parser);
13195 /* Remember that we are defining one more class. */
13196 ++parser->num_classes_being_defined;
13197 /* Inside the class, surrounding template-parameter-lists do not
13199 saved_num_template_parameter_lists
13200 = parser->num_template_parameter_lists;
13201 parser->num_template_parameter_lists = 0;
13202 /* We are not in a function body. */
13203 saved_in_function_body = parser->in_function_body;
13204 parser->in_function_body = false;
13206 /* Start the class. */
13207 if (nested_name_specifier_p)
13209 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13210 old_scope = push_inner_scope (scope);
13212 type = begin_class_definition (type, attributes);
13214 if (type == error_mark_node)
13215 /* If the type is erroneous, skip the entire body of the class. */
13216 cp_parser_skip_to_closing_brace (parser);
13218 /* Parse the member-specification. */
13219 cp_parser_member_specification_opt (parser);
13221 /* Look for the trailing `}'. */
13222 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13223 /* We get better error messages by noticing a common problem: a
13224 missing trailing `;'. */
13225 token = cp_lexer_peek_token (parser->lexer);
13226 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13227 /* Look for trailing attributes to apply to this class. */
13228 if (cp_parser_allow_gnu_extensions_p (parser))
13229 attributes = cp_parser_attributes_opt (parser);
13230 if (type != error_mark_node)
13231 type = finish_struct (type, attributes);
13232 if (nested_name_specifier_p)
13233 pop_inner_scope (old_scope, scope);
13234 /* If this class is not itself within the scope of another class,
13235 then we need to parse the bodies of all of the queued function
13236 definitions. Note that the queued functions defined in a class
13237 are not always processed immediately following the
13238 class-specifier for that class. Consider:
13241 struct B { void f() { sizeof (A); } };
13244 If `f' were processed before the processing of `A' were
13245 completed, there would be no way to compute the size of `A'.
13246 Note that the nesting we are interested in here is lexical --
13247 not the semantic nesting given by TYPE_CONTEXT. In particular,
13250 struct A { struct B; };
13251 struct A::B { void f() { } };
13253 there is no need to delay the parsing of `A::B::f'. */
13254 if (--parser->num_classes_being_defined == 0)
13258 tree class_type = NULL_TREE;
13259 tree pushed_scope = NULL_TREE;
13261 /* In a first pass, parse default arguments to the functions.
13262 Then, in a second pass, parse the bodies of the functions.
13263 This two-phased approach handles cases like:
13271 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13272 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13273 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13274 TREE_PURPOSE (parser->unparsed_functions_queues)
13275 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13277 fn = TREE_VALUE (queue_entry);
13278 /* If there are default arguments that have not yet been processed,
13279 take care of them now. */
13280 if (class_type != TREE_PURPOSE (queue_entry))
13283 pop_scope (pushed_scope);
13284 class_type = TREE_PURPOSE (queue_entry);
13285 pushed_scope = push_scope (class_type);
13287 /* Make sure that any template parameters are in scope. */
13288 maybe_begin_member_template_processing (fn);
13289 /* Parse the default argument expressions. */
13290 cp_parser_late_parsing_default_args (parser, fn);
13291 /* Remove any template parameters from the symbol table. */
13292 maybe_end_member_template_processing ();
13295 pop_scope (pushed_scope);
13296 /* Now parse the body of the functions. */
13297 for (TREE_VALUE (parser->unparsed_functions_queues)
13298 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13299 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13300 TREE_VALUE (parser->unparsed_functions_queues)
13301 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13303 /* Figure out which function we need to process. */
13304 fn = TREE_VALUE (queue_entry);
13305 /* Parse the function. */
13306 cp_parser_late_parsing_for_member (parser, fn);
13310 /* Put back any saved access checks. */
13311 pop_deferring_access_checks ();
13313 /* Restore saved state. */
13314 parser->in_function_body = saved_in_function_body;
13315 parser->num_template_parameter_lists
13316 = saved_num_template_parameter_lists;
13321 /* Parse a class-head.
13324 class-key identifier [opt] base-clause [opt]
13325 class-key nested-name-specifier identifier base-clause [opt]
13326 class-key nested-name-specifier [opt] template-id
13330 class-key attributes identifier [opt] base-clause [opt]
13331 class-key attributes nested-name-specifier identifier base-clause [opt]
13332 class-key attributes nested-name-specifier [opt] template-id
13335 Returns the TYPE of the indicated class. Sets
13336 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13337 involving a nested-name-specifier was used, and FALSE otherwise.
13339 Returns error_mark_node if this is not a class-head.
13341 Returns NULL_TREE if the class-head is syntactically valid, but
13342 semantically invalid in a way that means we should skip the entire
13343 body of the class. */
13346 cp_parser_class_head (cp_parser* parser,
13347 bool* nested_name_specifier_p,
13348 tree *attributes_p,
13351 tree nested_name_specifier;
13352 enum tag_types class_key;
13353 tree id = NULL_TREE;
13354 tree type = NULL_TREE;
13356 bool template_id_p = false;
13357 bool qualified_p = false;
13358 bool invalid_nested_name_p = false;
13359 bool invalid_explicit_specialization_p = false;
13360 tree pushed_scope = NULL_TREE;
13361 unsigned num_templates;
13363 /* Assume no nested-name-specifier will be present. */
13364 *nested_name_specifier_p = false;
13365 /* Assume no template parameter lists will be used in defining the
13369 /* Look for the class-key. */
13370 class_key = cp_parser_class_key (parser);
13371 if (class_key == none_type)
13372 return error_mark_node;
13374 /* Parse the attributes. */
13375 attributes = cp_parser_attributes_opt (parser);
13377 /* If the next token is `::', that is invalid -- but sometimes
13378 people do try to write:
13382 Handle this gracefully by accepting the extra qualifier, and then
13383 issuing an error about it later if this really is a
13384 class-head. If it turns out just to be an elaborated type
13385 specifier, remain silent. */
13386 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13387 qualified_p = true;
13389 push_deferring_access_checks (dk_no_check);
13391 /* Determine the name of the class. Begin by looking for an
13392 optional nested-name-specifier. */
13393 nested_name_specifier
13394 = cp_parser_nested_name_specifier_opt (parser,
13395 /*typename_keyword_p=*/false,
13396 /*check_dependency_p=*/false,
13398 /*is_declaration=*/false);
13399 /* If there was a nested-name-specifier, then there *must* be an
13401 if (nested_name_specifier)
13403 /* Although the grammar says `identifier', it really means
13404 `class-name' or `template-name'. You are only allowed to
13405 define a class that has already been declared with this
13408 The proposed resolution for Core Issue 180 says that wherever
13409 you see `class T::X' you should treat `X' as a type-name.
13411 It is OK to define an inaccessible class; for example:
13413 class A { class B; };
13416 We do not know if we will see a class-name, or a
13417 template-name. We look for a class-name first, in case the
13418 class-name is a template-id; if we looked for the
13419 template-name first we would stop after the template-name. */
13420 cp_parser_parse_tentatively (parser);
13421 type = cp_parser_class_name (parser,
13422 /*typename_keyword_p=*/false,
13423 /*template_keyword_p=*/false,
13425 /*check_dependency_p=*/false,
13426 /*class_head_p=*/true,
13427 /*is_declaration=*/false);
13428 /* If that didn't work, ignore the nested-name-specifier. */
13429 if (!cp_parser_parse_definitely (parser))
13431 invalid_nested_name_p = true;
13432 id = cp_parser_identifier (parser);
13433 if (id == error_mark_node)
13436 /* If we could not find a corresponding TYPE, treat this
13437 declaration like an unqualified declaration. */
13438 if (type == error_mark_node)
13439 nested_name_specifier = NULL_TREE;
13440 /* Otherwise, count the number of templates used in TYPE and its
13441 containing scopes. */
13446 for (scope = TREE_TYPE (type);
13447 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13448 scope = (TYPE_P (scope)
13449 ? TYPE_CONTEXT (scope)
13450 : DECL_CONTEXT (scope)))
13452 && CLASS_TYPE_P (scope)
13453 && CLASSTYPE_TEMPLATE_INFO (scope)
13454 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13455 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13459 /* Otherwise, the identifier is optional. */
13462 /* We don't know whether what comes next is a template-id,
13463 an identifier, or nothing at all. */
13464 cp_parser_parse_tentatively (parser);
13465 /* Check for a template-id. */
13466 id = cp_parser_template_id (parser,
13467 /*template_keyword_p=*/false,
13468 /*check_dependency_p=*/true,
13469 /*is_declaration=*/true);
13470 /* If that didn't work, it could still be an identifier. */
13471 if (!cp_parser_parse_definitely (parser))
13473 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13474 id = cp_parser_identifier (parser);
13480 template_id_p = true;
13485 pop_deferring_access_checks ();
13488 cp_parser_check_for_invalid_template_id (parser, id);
13490 /* If it's not a `:' or a `{' then we can't really be looking at a
13491 class-head, since a class-head only appears as part of a
13492 class-specifier. We have to detect this situation before calling
13493 xref_tag, since that has irreversible side-effects. */
13494 if (!cp_parser_next_token_starts_class_definition_p (parser))
13496 cp_parser_error (parser, "expected %<{%> or %<:%>");
13497 return error_mark_node;
13500 /* At this point, we're going ahead with the class-specifier, even
13501 if some other problem occurs. */
13502 cp_parser_commit_to_tentative_parse (parser);
13503 /* Issue the error about the overly-qualified name now. */
13505 cp_parser_error (parser,
13506 "global qualification of class name is invalid");
13507 else if (invalid_nested_name_p)
13508 cp_parser_error (parser,
13509 "qualified name does not name a class");
13510 else if (nested_name_specifier)
13514 /* Reject typedef-names in class heads. */
13515 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13517 error ("invalid class name in declaration of %qD", type);
13522 /* Figure out in what scope the declaration is being placed. */
13523 scope = current_scope ();
13524 /* If that scope does not contain the scope in which the
13525 class was originally declared, the program is invalid. */
13526 if (scope && !is_ancestor (scope, nested_name_specifier))
13528 error ("declaration of %qD in %qD which does not enclose %qD",
13529 type, scope, nested_name_specifier);
13535 A declarator-id shall not be qualified exception of the
13536 definition of a ... nested class outside of its class
13537 ... [or] a the definition or explicit instantiation of a
13538 class member of a namespace outside of its namespace. */
13539 if (scope == nested_name_specifier)
13541 pedwarn ("extra qualification ignored");
13542 nested_name_specifier = NULL_TREE;
13546 /* An explicit-specialization must be preceded by "template <>". If
13547 it is not, try to recover gracefully. */
13548 if (at_namespace_scope_p ()
13549 && parser->num_template_parameter_lists == 0
13552 error ("an explicit specialization must be preceded by %<template <>%>");
13553 invalid_explicit_specialization_p = true;
13554 /* Take the same action that would have been taken by
13555 cp_parser_explicit_specialization. */
13556 ++parser->num_template_parameter_lists;
13557 begin_specialization ();
13559 /* There must be no "return" statements between this point and the
13560 end of this function; set "type "to the correct return value and
13561 use "goto done;" to return. */
13562 /* Make sure that the right number of template parameters were
13564 if (!cp_parser_check_template_parameters (parser, num_templates))
13566 /* If something went wrong, there is no point in even trying to
13567 process the class-definition. */
13572 /* Look up the type. */
13575 type = TREE_TYPE (id);
13576 type = maybe_process_partial_specialization (type);
13577 if (nested_name_specifier)
13578 pushed_scope = push_scope (nested_name_specifier);
13580 else if (nested_name_specifier)
13586 template <typename T> struct S { struct T };
13587 template <typename T> struct S<T>::T { };
13589 we will get a TYPENAME_TYPE when processing the definition of
13590 `S::T'. We need to resolve it to the actual type before we
13591 try to define it. */
13592 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13594 class_type = resolve_typename_type (TREE_TYPE (type),
13595 /*only_current_p=*/false);
13596 if (class_type != error_mark_node)
13597 type = TYPE_NAME (class_type);
13600 cp_parser_error (parser, "could not resolve typename type");
13601 type = error_mark_node;
13605 maybe_process_partial_specialization (TREE_TYPE (type));
13606 class_type = current_class_type;
13607 /* Enter the scope indicated by the nested-name-specifier. */
13608 pushed_scope = push_scope (nested_name_specifier);
13609 /* Get the canonical version of this type. */
13610 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13611 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13612 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13614 type = push_template_decl (type);
13615 if (type == error_mark_node)
13622 type = TREE_TYPE (type);
13623 *nested_name_specifier_p = true;
13625 else /* The name is not a nested name. */
13627 /* If the class was unnamed, create a dummy name. */
13629 id = make_anon_name ();
13630 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13631 parser->num_template_parameter_lists);
13634 /* Indicate whether this class was declared as a `class' or as a
13636 if (TREE_CODE (type) == RECORD_TYPE)
13637 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13638 cp_parser_check_class_key (class_key, type);
13640 /* If this type was already complete, and we see another definition,
13641 that's an error. */
13642 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13644 error ("redefinition of %q#T", type);
13645 error ("previous definition of %q+#T", type);
13649 else if (type == error_mark_node)
13652 /* We will have entered the scope containing the class; the names of
13653 base classes should be looked up in that context. For example:
13655 struct A { struct B {}; struct C; };
13656 struct A::C : B {};
13659 *bases = NULL_TREE;
13661 /* Get the list of base-classes, if there is one. */
13662 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13663 *bases = cp_parser_base_clause (parser);
13666 /* Leave the scope given by the nested-name-specifier. We will
13667 enter the class scope itself while processing the members. */
13669 pop_scope (pushed_scope);
13671 if (invalid_explicit_specialization_p)
13673 end_specialization ();
13674 --parser->num_template_parameter_lists;
13676 *attributes_p = attributes;
13680 /* Parse a class-key.
13687 Returns the kind of class-key specified, or none_type to indicate
13690 static enum tag_types
13691 cp_parser_class_key (cp_parser* parser)
13694 enum tag_types tag_type;
13696 /* Look for the class-key. */
13697 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13701 /* Check to see if the TOKEN is a class-key. */
13702 tag_type = cp_parser_token_is_class_key (token);
13704 cp_parser_error (parser, "expected class-key");
13708 /* Parse an (optional) member-specification.
13710 member-specification:
13711 member-declaration member-specification [opt]
13712 access-specifier : member-specification [opt] */
13715 cp_parser_member_specification_opt (cp_parser* parser)
13722 /* Peek at the next token. */
13723 token = cp_lexer_peek_token (parser->lexer);
13724 /* If it's a `}', or EOF then we've seen all the members. */
13725 if (token->type == CPP_CLOSE_BRACE
13726 || token->type == CPP_EOF
13727 || token->type == CPP_PRAGMA_EOL)
13730 /* See if this token is a keyword. */
13731 keyword = token->keyword;
13735 case RID_PROTECTED:
13737 /* Consume the access-specifier. */
13738 cp_lexer_consume_token (parser->lexer);
13739 /* Remember which access-specifier is active. */
13740 current_access_specifier = token->u.value;
13741 /* Look for the `:'. */
13742 cp_parser_require (parser, CPP_COLON, "`:'");
13746 /* Accept #pragmas at class scope. */
13747 if (token->type == CPP_PRAGMA)
13749 cp_parser_pragma (parser, pragma_external);
13753 /* Otherwise, the next construction must be a
13754 member-declaration. */
13755 cp_parser_member_declaration (parser);
13760 /* Parse a member-declaration.
13762 member-declaration:
13763 decl-specifier-seq [opt] member-declarator-list [opt] ;
13764 function-definition ; [opt]
13765 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13767 template-declaration
13769 member-declarator-list:
13771 member-declarator-list , member-declarator
13774 declarator pure-specifier [opt]
13775 declarator constant-initializer [opt]
13776 identifier [opt] : constant-expression
13780 member-declaration:
13781 __extension__ member-declaration
13784 declarator attributes [opt] pure-specifier [opt]
13785 declarator attributes [opt] constant-initializer [opt]
13786 identifier [opt] attributes [opt] : constant-expression */
13789 cp_parser_member_declaration (cp_parser* parser)
13791 cp_decl_specifier_seq decl_specifiers;
13792 tree prefix_attributes;
13794 int declares_class_or_enum;
13797 int saved_pedantic;
13799 /* Check for the `__extension__' keyword. */
13800 if (cp_parser_extension_opt (parser, &saved_pedantic))
13803 cp_parser_member_declaration (parser);
13804 /* Restore the old value of the PEDANTIC flag. */
13805 pedantic = saved_pedantic;
13810 /* Check for a template-declaration. */
13811 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13813 /* An explicit specialization here is an error condition, and we
13814 expect the specialization handler to detect and report this. */
13815 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13816 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13817 cp_parser_explicit_specialization (parser);
13819 cp_parser_template_declaration (parser, /*member_p=*/true);
13824 /* Check for a using-declaration. */
13825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13827 /* Parse the using-declaration. */
13828 cp_parser_using_declaration (parser,
13829 /*access_declaration_p=*/false);
13833 /* Check for @defs. */
13834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13837 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13838 ivar = ivar_chains;
13842 ivar = TREE_CHAIN (member);
13843 TREE_CHAIN (member) = NULL_TREE;
13844 finish_member_declaration (member);
13849 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13852 /* Parse the decl-specifier-seq. */
13853 cp_parser_decl_specifier_seq (parser,
13854 CP_PARSER_FLAGS_OPTIONAL,
13856 &declares_class_or_enum);
13857 prefix_attributes = decl_specifiers.attributes;
13858 decl_specifiers.attributes = NULL_TREE;
13859 /* Check for an invalid type-name. */
13860 if (!decl_specifiers.type
13861 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13863 /* If there is no declarator, then the decl-specifier-seq should
13865 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13867 /* If there was no decl-specifier-seq, and the next token is a
13868 `;', then we have something like:
13874 Each member-declaration shall declare at least one member
13875 name of the class. */
13876 if (!decl_specifiers.any_specifiers_p)
13878 cp_token *token = cp_lexer_peek_token (parser->lexer);
13879 if (pedantic && !token->in_system_header)
13880 pedwarn ("%Hextra %<;%>", &token->location);
13886 /* See if this declaration is a friend. */
13887 friend_p = cp_parser_friend_p (&decl_specifiers);
13888 /* If there were decl-specifiers, check to see if there was
13889 a class-declaration. */
13890 type = check_tag_decl (&decl_specifiers);
13891 /* Nested classes have already been added to the class, but
13892 a `friend' needs to be explicitly registered. */
13895 /* If the `friend' keyword was present, the friend must
13896 be introduced with a class-key. */
13897 if (!declares_class_or_enum)
13898 error ("a class-key must be used when declaring a friend");
13901 template <typename T> struct A {
13902 friend struct A<T>::B;
13905 A<T>::B will be represented by a TYPENAME_TYPE, and
13906 therefore not recognized by check_tag_decl. */
13908 && decl_specifiers.type
13909 && TYPE_P (decl_specifiers.type))
13910 type = decl_specifiers.type;
13911 if (!type || !TYPE_P (type))
13912 error ("friend declaration does not name a class or "
13915 make_friend_class (current_class_type, type,
13916 /*complain=*/true);
13918 /* If there is no TYPE, an error message will already have
13920 else if (!type || type == error_mark_node)
13922 /* An anonymous aggregate has to be handled specially; such
13923 a declaration really declares a data member (with a
13924 particular type), as opposed to a nested class. */
13925 else if (ANON_AGGR_TYPE_P (type))
13927 /* Remove constructors and such from TYPE, now that we
13928 know it is an anonymous aggregate. */
13929 fixup_anonymous_aggr (type);
13930 /* And make the corresponding data member. */
13931 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13932 /* Add it to the class. */
13933 finish_member_declaration (decl);
13936 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13941 /* See if these declarations will be friends. */
13942 friend_p = cp_parser_friend_p (&decl_specifiers);
13944 /* Keep going until we hit the `;' at the end of the
13946 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13948 tree attributes = NULL_TREE;
13949 tree first_attribute;
13951 /* Peek at the next token. */
13952 token = cp_lexer_peek_token (parser->lexer);
13954 /* Check for a bitfield declaration. */
13955 if (token->type == CPP_COLON
13956 || (token->type == CPP_NAME
13957 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13963 /* Get the name of the bitfield. Note that we cannot just
13964 check TOKEN here because it may have been invalidated by
13965 the call to cp_lexer_peek_nth_token above. */
13966 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13967 identifier = cp_parser_identifier (parser);
13969 identifier = NULL_TREE;
13971 /* Consume the `:' token. */
13972 cp_lexer_consume_token (parser->lexer);
13973 /* Get the width of the bitfield. */
13975 = cp_parser_constant_expression (parser,
13976 /*allow_non_constant=*/false,
13979 /* Look for attributes that apply to the bitfield. */
13980 attributes = cp_parser_attributes_opt (parser);
13981 /* Remember which attributes are prefix attributes and
13983 first_attribute = attributes;
13984 /* Combine the attributes. */
13985 attributes = chainon (prefix_attributes, attributes);
13987 /* Create the bitfield declaration. */
13988 decl = grokbitfield (identifier
13989 ? make_id_declarator (NULL_TREE,
13995 /* Apply the attributes. */
13996 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14000 cp_declarator *declarator;
14002 tree asm_specification;
14003 int ctor_dtor_or_conv_p;
14005 /* Parse the declarator. */
14007 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14008 &ctor_dtor_or_conv_p,
14009 /*parenthesized_p=*/NULL,
14010 /*member_p=*/true);
14012 /* If something went wrong parsing the declarator, make sure
14013 that we at least consume some tokens. */
14014 if (declarator == cp_error_declarator)
14016 /* Skip to the end of the statement. */
14017 cp_parser_skip_to_end_of_statement (parser);
14018 /* If the next token is not a semicolon, that is
14019 probably because we just skipped over the body of
14020 a function. So, we consume a semicolon if
14021 present, but do not issue an error message if it
14023 if (cp_lexer_next_token_is (parser->lexer,
14025 cp_lexer_consume_token (parser->lexer);
14029 if (declares_class_or_enum & 2)
14030 cp_parser_check_for_definition_in_return_type
14031 (declarator, decl_specifiers.type);
14033 /* Look for an asm-specification. */
14034 asm_specification = cp_parser_asm_specification_opt (parser);
14035 /* Look for attributes that apply to the declaration. */
14036 attributes = cp_parser_attributes_opt (parser);
14037 /* Remember which attributes are prefix attributes and
14039 first_attribute = attributes;
14040 /* Combine the attributes. */
14041 attributes = chainon (prefix_attributes, attributes);
14043 /* If it's an `=', then we have a constant-initializer or a
14044 pure-specifier. It is not correct to parse the
14045 initializer before registering the member declaration
14046 since the member declaration should be in scope while
14047 its initializer is processed. However, the rest of the
14048 front end does not yet provide an interface that allows
14049 us to handle this correctly. */
14050 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14054 A pure-specifier shall be used only in the declaration of
14055 a virtual function.
14057 A member-declarator can contain a constant-initializer
14058 only if it declares a static member of integral or
14061 Therefore, if the DECLARATOR is for a function, we look
14062 for a pure-specifier; otherwise, we look for a
14063 constant-initializer. When we call `grokfield', it will
14064 perform more stringent semantics checks. */
14065 if (function_declarator_p (declarator))
14066 initializer = cp_parser_pure_specifier (parser);
14068 /* Parse the initializer. */
14069 initializer = cp_parser_constant_initializer (parser);
14071 /* Otherwise, there is no initializer. */
14073 initializer = NULL_TREE;
14075 /* See if we are probably looking at a function
14076 definition. We are certainly not looking at a
14077 member-declarator. Calling `grokfield' has
14078 side-effects, so we must not do it unless we are sure
14079 that we are looking at a member-declarator. */
14080 if (cp_parser_token_starts_function_definition_p
14081 (cp_lexer_peek_token (parser->lexer)))
14083 /* The grammar does not allow a pure-specifier to be
14084 used when a member function is defined. (It is
14085 possible that this fact is an oversight in the
14086 standard, since a pure function may be defined
14087 outside of the class-specifier. */
14089 error ("pure-specifier on function-definition");
14090 decl = cp_parser_save_member_function_body (parser,
14094 /* If the member was not a friend, declare it here. */
14096 finish_member_declaration (decl);
14097 /* Peek at the next token. */
14098 token = cp_lexer_peek_token (parser->lexer);
14099 /* If the next token is a semicolon, consume it. */
14100 if (token->type == CPP_SEMICOLON)
14101 cp_lexer_consume_token (parser->lexer);
14105 /* Create the declaration. */
14106 decl = grokfield (declarator, &decl_specifiers,
14107 initializer, /*init_const_expr_p=*/true,
14112 /* Reset PREFIX_ATTRIBUTES. */
14113 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14114 attributes = TREE_CHAIN (attributes);
14116 TREE_CHAIN (attributes) = NULL_TREE;
14118 /* If there is any qualification still in effect, clear it
14119 now; we will be starting fresh with the next declarator. */
14120 parser->scope = NULL_TREE;
14121 parser->qualifying_scope = NULL_TREE;
14122 parser->object_scope = NULL_TREE;
14123 /* If it's a `,', then there are more declarators. */
14124 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14125 cp_lexer_consume_token (parser->lexer);
14126 /* If the next token isn't a `;', then we have a parse error. */
14127 else if (cp_lexer_next_token_is_not (parser->lexer,
14130 cp_parser_error (parser, "expected %<;%>");
14131 /* Skip tokens until we find a `;'. */
14132 cp_parser_skip_to_end_of_statement (parser);
14139 /* Add DECL to the list of members. */
14141 finish_member_declaration (decl);
14143 if (TREE_CODE (decl) == FUNCTION_DECL)
14144 cp_parser_save_default_args (parser, decl);
14149 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14152 /* Parse a pure-specifier.
14157 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14158 Otherwise, ERROR_MARK_NODE is returned. */
14161 cp_parser_pure_specifier (cp_parser* parser)
14165 /* Look for the `=' token. */
14166 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14167 return error_mark_node;
14168 /* Look for the `0' token. */
14169 token = cp_lexer_consume_token (parser->lexer);
14170 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14171 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14173 cp_parser_error (parser,
14174 "invalid pure specifier (only `= 0' is allowed)");
14175 cp_parser_skip_to_end_of_statement (parser);
14176 return error_mark_node;
14178 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14180 error ("templates may not be %<virtual%>");
14181 return error_mark_node;
14184 return integer_zero_node;
14187 /* Parse a constant-initializer.
14189 constant-initializer:
14190 = constant-expression
14192 Returns a representation of the constant-expression. */
14195 cp_parser_constant_initializer (cp_parser* parser)
14197 /* Look for the `=' token. */
14198 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14199 return error_mark_node;
14201 /* It is invalid to write:
14203 struct S { static const int i = { 7 }; };
14206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14208 cp_parser_error (parser,
14209 "a brace-enclosed initializer is not allowed here");
14210 /* Consume the opening brace. */
14211 cp_lexer_consume_token (parser->lexer);
14212 /* Skip the initializer. */
14213 cp_parser_skip_to_closing_brace (parser);
14214 /* Look for the trailing `}'. */
14215 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14217 return error_mark_node;
14220 return cp_parser_constant_expression (parser,
14221 /*allow_non_constant=*/false,
14225 /* Derived classes [gram.class.derived] */
14227 /* Parse a base-clause.
14230 : base-specifier-list
14232 base-specifier-list:
14234 base-specifier-list , base-specifier
14236 Returns a TREE_LIST representing the base-classes, in the order in
14237 which they were declared. The representation of each node is as
14238 described by cp_parser_base_specifier.
14240 In the case that no bases are specified, this function will return
14241 NULL_TREE, not ERROR_MARK_NODE. */
14244 cp_parser_base_clause (cp_parser* parser)
14246 tree bases = NULL_TREE;
14248 /* Look for the `:' that begins the list. */
14249 cp_parser_require (parser, CPP_COLON, "`:'");
14251 /* Scan the base-specifier-list. */
14257 /* Look for the base-specifier. */
14258 base = cp_parser_base_specifier (parser);
14259 /* Add BASE to the front of the list. */
14260 if (base != error_mark_node)
14262 TREE_CHAIN (base) = bases;
14265 /* Peek at the next token. */
14266 token = cp_lexer_peek_token (parser->lexer);
14267 /* If it's not a comma, then the list is complete. */
14268 if (token->type != CPP_COMMA)
14270 /* Consume the `,'. */
14271 cp_lexer_consume_token (parser->lexer);
14274 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14275 base class had a qualified name. However, the next name that
14276 appears is certainly not qualified. */
14277 parser->scope = NULL_TREE;
14278 parser->qualifying_scope = NULL_TREE;
14279 parser->object_scope = NULL_TREE;
14281 return nreverse (bases);
14284 /* Parse a base-specifier.
14287 :: [opt] nested-name-specifier [opt] class-name
14288 virtual access-specifier [opt] :: [opt] nested-name-specifier
14290 access-specifier virtual [opt] :: [opt] nested-name-specifier
14293 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14294 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14295 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14296 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14299 cp_parser_base_specifier (cp_parser* parser)
14303 bool virtual_p = false;
14304 bool duplicate_virtual_error_issued_p = false;
14305 bool duplicate_access_error_issued_p = false;
14306 bool class_scope_p, template_p;
14307 tree access = access_default_node;
14310 /* Process the optional `virtual' and `access-specifier'. */
14313 /* Peek at the next token. */
14314 token = cp_lexer_peek_token (parser->lexer);
14315 /* Process `virtual'. */
14316 switch (token->keyword)
14319 /* If `virtual' appears more than once, issue an error. */
14320 if (virtual_p && !duplicate_virtual_error_issued_p)
14322 cp_parser_error (parser,
14323 "%<virtual%> specified more than once in base-specified");
14324 duplicate_virtual_error_issued_p = true;
14329 /* Consume the `virtual' token. */
14330 cp_lexer_consume_token (parser->lexer);
14335 case RID_PROTECTED:
14337 /* If more than one access specifier appears, issue an
14339 if (access != access_default_node
14340 && !duplicate_access_error_issued_p)
14342 cp_parser_error (parser,
14343 "more than one access specifier in base-specified");
14344 duplicate_access_error_issued_p = true;
14347 access = ridpointers[(int) token->keyword];
14349 /* Consume the access-specifier. */
14350 cp_lexer_consume_token (parser->lexer);
14359 /* It is not uncommon to see programs mechanically, erroneously, use
14360 the 'typename' keyword to denote (dependent) qualified types
14361 as base classes. */
14362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14364 if (!processing_template_decl)
14365 error ("keyword %<typename%> not allowed outside of templates");
14367 error ("keyword %<typename%> not allowed in this context "
14368 "(the base class is implicitly a type)");
14369 cp_lexer_consume_token (parser->lexer);
14372 /* Look for the optional `::' operator. */
14373 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14374 /* Look for the nested-name-specifier. The simplest way to
14379 The keyword `typename' is not permitted in a base-specifier or
14380 mem-initializer; in these contexts a qualified name that
14381 depends on a template-parameter is implicitly assumed to be a
14384 is to pretend that we have seen the `typename' keyword at this
14386 cp_parser_nested_name_specifier_opt (parser,
14387 /*typename_keyword_p=*/true,
14388 /*check_dependency_p=*/true,
14390 /*is_declaration=*/true);
14391 /* If the base class is given by a qualified name, assume that names
14392 we see are type names or templates, as appropriate. */
14393 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14394 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14396 /* Finally, look for the class-name. */
14397 type = cp_parser_class_name (parser,
14401 /*check_dependency_p=*/true,
14402 /*class_head_p=*/false,
14403 /*is_declaration=*/true);
14405 if (type == error_mark_node)
14406 return error_mark_node;
14408 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14411 /* Exception handling [gram.exception] */
14413 /* Parse an (optional) exception-specification.
14415 exception-specification:
14416 throw ( type-id-list [opt] )
14418 Returns a TREE_LIST representing the exception-specification. The
14419 TREE_VALUE of each node is a type. */
14422 cp_parser_exception_specification_opt (cp_parser* parser)
14427 /* Peek at the next token. */
14428 token = cp_lexer_peek_token (parser->lexer);
14429 /* If it's not `throw', then there's no exception-specification. */
14430 if (!cp_parser_is_keyword (token, RID_THROW))
14433 /* Consume the `throw'. */
14434 cp_lexer_consume_token (parser->lexer);
14436 /* Look for the `('. */
14437 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14439 /* Peek at the next token. */
14440 token = cp_lexer_peek_token (parser->lexer);
14441 /* If it's not a `)', then there is a type-id-list. */
14442 if (token->type != CPP_CLOSE_PAREN)
14444 const char *saved_message;
14446 /* Types may not be defined in an exception-specification. */
14447 saved_message = parser->type_definition_forbidden_message;
14448 parser->type_definition_forbidden_message
14449 = "types may not be defined in an exception-specification";
14450 /* Parse the type-id-list. */
14451 type_id_list = cp_parser_type_id_list (parser);
14452 /* Restore the saved message. */
14453 parser->type_definition_forbidden_message = saved_message;
14456 type_id_list = empty_except_spec;
14458 /* Look for the `)'. */
14459 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14461 return type_id_list;
14464 /* Parse an (optional) type-id-list.
14468 type-id-list , type-id
14470 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14471 in the order that the types were presented. */
14474 cp_parser_type_id_list (cp_parser* parser)
14476 tree types = NULL_TREE;
14483 /* Get the next type-id. */
14484 type = cp_parser_type_id (parser);
14485 /* Add it to the list. */
14486 types = add_exception_specifier (types, type, /*complain=*/1);
14487 /* Peek at the next token. */
14488 token = cp_lexer_peek_token (parser->lexer);
14489 /* If it is not a `,', we are done. */
14490 if (token->type != CPP_COMMA)
14492 /* Consume the `,'. */
14493 cp_lexer_consume_token (parser->lexer);
14496 return nreverse (types);
14499 /* Parse a try-block.
14502 try compound-statement handler-seq */
14505 cp_parser_try_block (cp_parser* parser)
14509 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14510 try_block = begin_try_block ();
14511 cp_parser_compound_statement (parser, NULL, true);
14512 finish_try_block (try_block);
14513 cp_parser_handler_seq (parser);
14514 finish_handler_sequence (try_block);
14519 /* Parse a function-try-block.
14521 function-try-block:
14522 try ctor-initializer [opt] function-body handler-seq */
14525 cp_parser_function_try_block (cp_parser* parser)
14527 tree compound_stmt;
14529 bool ctor_initializer_p;
14531 /* Look for the `try' keyword. */
14532 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14534 /* Let the rest of the front-end know where we are. */
14535 try_block = begin_function_try_block (&compound_stmt);
14536 /* Parse the function-body. */
14538 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14539 /* We're done with the `try' part. */
14540 finish_function_try_block (try_block);
14541 /* Parse the handlers. */
14542 cp_parser_handler_seq (parser);
14543 /* We're done with the handlers. */
14544 finish_function_handler_sequence (try_block, compound_stmt);
14546 return ctor_initializer_p;
14549 /* Parse a handler-seq.
14552 handler handler-seq [opt] */
14555 cp_parser_handler_seq (cp_parser* parser)
14561 /* Parse the handler. */
14562 cp_parser_handler (parser);
14563 /* Peek at the next token. */
14564 token = cp_lexer_peek_token (parser->lexer);
14565 /* If it's not `catch' then there are no more handlers. */
14566 if (!cp_parser_is_keyword (token, RID_CATCH))
14571 /* Parse a handler.
14574 catch ( exception-declaration ) compound-statement */
14577 cp_parser_handler (cp_parser* parser)
14582 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14583 handler = begin_handler ();
14584 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14585 declaration = cp_parser_exception_declaration (parser);
14586 finish_handler_parms (declaration, handler);
14587 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14588 cp_parser_compound_statement (parser, NULL, false);
14589 finish_handler (handler);
14592 /* Parse an exception-declaration.
14594 exception-declaration:
14595 type-specifier-seq declarator
14596 type-specifier-seq abstract-declarator
14600 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14601 ellipsis variant is used. */
14604 cp_parser_exception_declaration (cp_parser* parser)
14606 cp_decl_specifier_seq type_specifiers;
14607 cp_declarator *declarator;
14608 const char *saved_message;
14610 /* If it's an ellipsis, it's easy to handle. */
14611 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14613 /* Consume the `...' token. */
14614 cp_lexer_consume_token (parser->lexer);
14618 /* Types may not be defined in exception-declarations. */
14619 saved_message = parser->type_definition_forbidden_message;
14620 parser->type_definition_forbidden_message
14621 = "types may not be defined in exception-declarations";
14623 /* Parse the type-specifier-seq. */
14624 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14626 /* If it's a `)', then there is no declarator. */
14627 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14630 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14631 /*ctor_dtor_or_conv_p=*/NULL,
14632 /*parenthesized_p=*/NULL,
14633 /*member_p=*/false);
14635 /* Restore the saved message. */
14636 parser->type_definition_forbidden_message = saved_message;
14638 if (!type_specifiers.any_specifiers_p)
14639 return error_mark_node;
14641 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14644 /* Parse a throw-expression.
14647 throw assignment-expression [opt]
14649 Returns a THROW_EXPR representing the throw-expression. */
14652 cp_parser_throw_expression (cp_parser* parser)
14657 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14658 token = cp_lexer_peek_token (parser->lexer);
14659 /* Figure out whether or not there is an assignment-expression
14660 following the "throw" keyword. */
14661 if (token->type == CPP_COMMA
14662 || token->type == CPP_SEMICOLON
14663 || token->type == CPP_CLOSE_PAREN
14664 || token->type == CPP_CLOSE_SQUARE
14665 || token->type == CPP_CLOSE_BRACE
14666 || token->type == CPP_COLON)
14667 expression = NULL_TREE;
14669 expression = cp_parser_assignment_expression (parser,
14672 return build_throw (expression);
14675 /* GNU Extensions */
14677 /* Parse an (optional) asm-specification.
14680 asm ( string-literal )
14682 If the asm-specification is present, returns a STRING_CST
14683 corresponding to the string-literal. Otherwise, returns
14687 cp_parser_asm_specification_opt (cp_parser* parser)
14690 tree asm_specification;
14692 /* Peek at the next token. */
14693 token = cp_lexer_peek_token (parser->lexer);
14694 /* If the next token isn't the `asm' keyword, then there's no
14695 asm-specification. */
14696 if (!cp_parser_is_keyword (token, RID_ASM))
14699 /* Consume the `asm' token. */
14700 cp_lexer_consume_token (parser->lexer);
14701 /* Look for the `('. */
14702 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14704 /* Look for the string-literal. */
14705 asm_specification = cp_parser_string_literal (parser, false, false);
14707 /* Look for the `)'. */
14708 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14710 return asm_specification;
14713 /* Parse an asm-operand-list.
14717 asm-operand-list , asm-operand
14720 string-literal ( expression )
14721 [ string-literal ] string-literal ( expression )
14723 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14724 each node is the expression. The TREE_PURPOSE is itself a
14725 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14726 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14727 is a STRING_CST for the string literal before the parenthesis. */
14730 cp_parser_asm_operand_list (cp_parser* parser)
14732 tree asm_operands = NULL_TREE;
14736 tree string_literal;
14740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14742 /* Consume the `[' token. */
14743 cp_lexer_consume_token (parser->lexer);
14744 /* Read the operand name. */
14745 name = cp_parser_identifier (parser);
14746 if (name != error_mark_node)
14747 name = build_string (IDENTIFIER_LENGTH (name),
14748 IDENTIFIER_POINTER (name));
14749 /* Look for the closing `]'. */
14750 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14754 /* Look for the string-literal. */
14755 string_literal = cp_parser_string_literal (parser, false, false);
14757 /* Look for the `('. */
14758 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14759 /* Parse the expression. */
14760 expression = cp_parser_expression (parser, /*cast_p=*/false);
14761 /* Look for the `)'. */
14762 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14764 /* Add this operand to the list. */
14765 asm_operands = tree_cons (build_tree_list (name, string_literal),
14768 /* If the next token is not a `,', there are no more
14770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14772 /* Consume the `,'. */
14773 cp_lexer_consume_token (parser->lexer);
14776 return nreverse (asm_operands);
14779 /* Parse an asm-clobber-list.
14783 asm-clobber-list , string-literal
14785 Returns a TREE_LIST, indicating the clobbers in the order that they
14786 appeared. The TREE_VALUE of each node is a STRING_CST. */
14789 cp_parser_asm_clobber_list (cp_parser* parser)
14791 tree clobbers = NULL_TREE;
14795 tree string_literal;
14797 /* Look for the string literal. */
14798 string_literal = cp_parser_string_literal (parser, false, false);
14799 /* Add it to the list. */
14800 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14801 /* If the next token is not a `,', then the list is
14803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14805 /* Consume the `,' token. */
14806 cp_lexer_consume_token (parser->lexer);
14812 /* Parse an (optional) series of attributes.
14815 attributes attribute
14818 __attribute__ (( attribute-list [opt] ))
14820 The return value is as for cp_parser_attribute_list. */
14823 cp_parser_attributes_opt (cp_parser* parser)
14825 tree attributes = NULL_TREE;
14830 tree attribute_list;
14832 /* Peek at the next token. */
14833 token = cp_lexer_peek_token (parser->lexer);
14834 /* If it's not `__attribute__', then we're done. */
14835 if (token->keyword != RID_ATTRIBUTE)
14838 /* Consume the `__attribute__' keyword. */
14839 cp_lexer_consume_token (parser->lexer);
14840 /* Look for the two `(' tokens. */
14841 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14842 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14844 /* Peek at the next token. */
14845 token = cp_lexer_peek_token (parser->lexer);
14846 if (token->type != CPP_CLOSE_PAREN)
14847 /* Parse the attribute-list. */
14848 attribute_list = cp_parser_attribute_list (parser);
14850 /* If the next token is a `)', then there is no attribute
14852 attribute_list = NULL;
14854 /* Look for the two `)' tokens. */
14855 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14856 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14858 /* Add these new attributes to the list. */
14859 attributes = chainon (attributes, attribute_list);
14865 /* Parse an attribute-list.
14869 attribute-list , attribute
14873 identifier ( identifier )
14874 identifier ( identifier , expression-list )
14875 identifier ( expression-list )
14877 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14878 to an attribute. The TREE_PURPOSE of each node is the identifier
14879 indicating which attribute is in use. The TREE_VALUE represents
14880 the arguments, if any. */
14883 cp_parser_attribute_list (cp_parser* parser)
14885 tree attribute_list = NULL_TREE;
14886 bool save_translate_strings_p = parser->translate_strings_p;
14888 parser->translate_strings_p = false;
14895 /* Look for the identifier. We also allow keywords here; for
14896 example `__attribute__ ((const))' is legal. */
14897 token = cp_lexer_peek_token (parser->lexer);
14898 if (token->type == CPP_NAME
14899 || token->type == CPP_KEYWORD)
14901 tree arguments = NULL_TREE;
14903 /* Consume the token. */
14904 token = cp_lexer_consume_token (parser->lexer);
14906 /* Save away the identifier that indicates which attribute
14908 identifier = token->u.value;
14909 attribute = build_tree_list (identifier, NULL_TREE);
14911 /* Peek at the next token. */
14912 token = cp_lexer_peek_token (parser->lexer);
14913 /* If it's an `(', then parse the attribute arguments. */
14914 if (token->type == CPP_OPEN_PAREN)
14916 arguments = cp_parser_parenthesized_expression_list
14917 (parser, true, /*cast_p=*/false,
14918 /*non_constant_p=*/NULL);
14919 /* Save the arguments away. */
14920 TREE_VALUE (attribute) = arguments;
14923 if (arguments != error_mark_node)
14925 /* Add this attribute to the list. */
14926 TREE_CHAIN (attribute) = attribute_list;
14927 attribute_list = attribute;
14930 token = cp_lexer_peek_token (parser->lexer);
14932 /* Now, look for more attributes. If the next token isn't a
14933 `,', we're done. */
14934 if (token->type != CPP_COMMA)
14937 /* Consume the comma and keep going. */
14938 cp_lexer_consume_token (parser->lexer);
14940 parser->translate_strings_p = save_translate_strings_p;
14942 /* We built up the list in reverse order. */
14943 return nreverse (attribute_list);
14946 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14947 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14948 current value of the PEDANTIC flag, regardless of whether or not
14949 the `__extension__' keyword is present. The caller is responsible
14950 for restoring the value of the PEDANTIC flag. */
14953 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14955 /* Save the old value of the PEDANTIC flag. */
14956 *saved_pedantic = pedantic;
14958 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14960 /* Consume the `__extension__' token. */
14961 cp_lexer_consume_token (parser->lexer);
14962 /* We're not being pedantic while the `__extension__' keyword is
14972 /* Parse a label declaration.
14975 __label__ label-declarator-seq ;
14977 label-declarator-seq:
14978 identifier , label-declarator-seq
14982 cp_parser_label_declaration (cp_parser* parser)
14984 /* Look for the `__label__' keyword. */
14985 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14991 /* Look for an identifier. */
14992 identifier = cp_parser_identifier (parser);
14993 /* If we failed, stop. */
14994 if (identifier == error_mark_node)
14996 /* Declare it as a label. */
14997 finish_label_decl (identifier);
14998 /* If the next token is a `;', stop. */
14999 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15001 /* Look for the `,' separating the label declarations. */
15002 cp_parser_require (parser, CPP_COMMA, "`,'");
15005 /* Look for the final `;'. */
15006 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15009 /* Support Functions */
15011 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15012 NAME should have one of the representations used for an
15013 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15014 is returned. If PARSER->SCOPE is a dependent type, then a
15015 SCOPE_REF is returned.
15017 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15018 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15019 was formed. Abstractly, such entities should not be passed to this
15020 function, because they do not need to be looked up, but it is
15021 simpler to check for this special case here, rather than at the
15024 In cases not explicitly covered above, this function returns a
15025 DECL, OVERLOAD, or baselink representing the result of the lookup.
15026 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15029 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15030 (e.g., "struct") that was used. In that case bindings that do not
15031 refer to types are ignored.
15033 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15036 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15039 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15042 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15043 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15044 NULL_TREE otherwise. */
15047 cp_parser_lookup_name (cp_parser *parser, tree name,
15048 enum tag_types tag_type,
15051 bool check_dependency,
15052 tree *ambiguous_decls)
15056 tree object_type = parser->context->object_type;
15058 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15059 flags |= LOOKUP_COMPLAIN;
15061 /* Assume that the lookup will be unambiguous. */
15062 if (ambiguous_decls)
15063 *ambiguous_decls = NULL_TREE;
15065 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15066 no longer valid. Note that if we are parsing tentatively, and
15067 the parse fails, OBJECT_TYPE will be automatically restored. */
15068 parser->context->object_type = NULL_TREE;
15070 if (name == error_mark_node)
15071 return error_mark_node;
15073 /* A template-id has already been resolved; there is no lookup to
15075 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15077 if (BASELINK_P (name))
15079 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15080 == TEMPLATE_ID_EXPR);
15084 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15085 it should already have been checked to make sure that the name
15086 used matches the type being destroyed. */
15087 if (TREE_CODE (name) == BIT_NOT_EXPR)
15091 /* Figure out to which type this destructor applies. */
15093 type = parser->scope;
15094 else if (object_type)
15095 type = object_type;
15097 type = current_class_type;
15098 /* If that's not a class type, there is no destructor. */
15099 if (!type || !CLASS_TYPE_P (type))
15100 return error_mark_node;
15101 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15102 lazily_declare_fn (sfk_destructor, type);
15103 if (!CLASSTYPE_DESTRUCTORS (type))
15104 return error_mark_node;
15105 /* If it was a class type, return the destructor. */
15106 return CLASSTYPE_DESTRUCTORS (type);
15109 /* By this point, the NAME should be an ordinary identifier. If
15110 the id-expression was a qualified name, the qualifying scope is
15111 stored in PARSER->SCOPE at this point. */
15112 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15114 /* Perform the lookup. */
15119 if (parser->scope == error_mark_node)
15120 return error_mark_node;
15122 /* If the SCOPE is dependent, the lookup must be deferred until
15123 the template is instantiated -- unless we are explicitly
15124 looking up names in uninstantiated templates. Even then, we
15125 cannot look up the name if the scope is not a class type; it
15126 might, for example, be a template type parameter. */
15127 dependent_p = (TYPE_P (parser->scope)
15128 && !(parser->in_declarator_p
15129 && currently_open_class (parser->scope))
15130 && dependent_type_p (parser->scope));
15131 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15138 /* The resolution to Core Issue 180 says that `struct
15139 A::B' should be considered a type-name, even if `A'
15141 type = make_typename_type (parser->scope, name, tag_type,
15142 /*complain=*/tf_error);
15143 decl = TYPE_NAME (type);
15145 else if (is_template
15146 && (cp_parser_next_token_ends_template_argument_p (parser)
15147 || cp_lexer_next_token_is (parser->lexer,
15149 decl = make_unbound_class_template (parser->scope,
15151 /*complain=*/tf_error);
15153 decl = build_qualified_name (/*type=*/NULL_TREE,
15154 parser->scope, name,
15159 tree pushed_scope = NULL_TREE;
15161 /* If PARSER->SCOPE is a dependent type, then it must be a
15162 class type, and we must not be checking dependencies;
15163 otherwise, we would have processed this lookup above. So
15164 that PARSER->SCOPE is not considered a dependent base by
15165 lookup_member, we must enter the scope here. */
15167 pushed_scope = push_scope (parser->scope);
15168 /* If the PARSER->SCOPE is a template specialization, it
15169 may be instantiated during name lookup. In that case,
15170 errors may be issued. Even if we rollback the current
15171 tentative parse, those errors are valid. */
15172 decl = lookup_qualified_name (parser->scope, name,
15173 tag_type != none_type,
15174 /*complain=*/true);
15176 pop_scope (pushed_scope);
15178 parser->qualifying_scope = parser->scope;
15179 parser->object_scope = NULL_TREE;
15181 else if (object_type)
15183 tree object_decl = NULL_TREE;
15184 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15185 OBJECT_TYPE is not a class. */
15186 if (CLASS_TYPE_P (object_type))
15187 /* If the OBJECT_TYPE is a template specialization, it may
15188 be instantiated during name lookup. In that case, errors
15189 may be issued. Even if we rollback the current tentative
15190 parse, those errors are valid. */
15191 object_decl = lookup_member (object_type,
15194 tag_type != none_type);
15195 /* Look it up in the enclosing context, too. */
15196 decl = lookup_name_real (name, tag_type != none_type,
15198 /*block_p=*/true, is_namespace, flags);
15199 parser->object_scope = object_type;
15200 parser->qualifying_scope = NULL_TREE;
15202 decl = object_decl;
15206 decl = lookup_name_real (name, tag_type != none_type,
15208 /*block_p=*/true, is_namespace, flags);
15209 parser->qualifying_scope = NULL_TREE;
15210 parser->object_scope = NULL_TREE;
15213 /* If the lookup failed, let our caller know. */
15214 if (!decl || decl == error_mark_node)
15215 return error_mark_node;
15217 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15218 if (TREE_CODE (decl) == TREE_LIST)
15220 if (ambiguous_decls)
15221 *ambiguous_decls = decl;
15222 /* The error message we have to print is too complicated for
15223 cp_parser_error, so we incorporate its actions directly. */
15224 if (!cp_parser_simulate_error (parser))
15226 error ("reference to %qD is ambiguous", name);
15227 print_candidates (decl);
15229 return error_mark_node;
15232 gcc_assert (DECL_P (decl)
15233 || TREE_CODE (decl) == OVERLOAD
15234 || TREE_CODE (decl) == SCOPE_REF
15235 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15236 || BASELINK_P (decl));
15238 /* If we have resolved the name of a member declaration, check to
15239 see if the declaration is accessible. When the name resolves to
15240 set of overloaded functions, accessibility is checked when
15241 overload resolution is done.
15243 During an explicit instantiation, access is not checked at all,
15244 as per [temp.explicit]. */
15246 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15251 /* Like cp_parser_lookup_name, but for use in the typical case where
15252 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15253 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15256 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15258 return cp_parser_lookup_name (parser, name,
15260 /*is_template=*/false,
15261 /*is_namespace=*/false,
15262 /*check_dependency=*/true,
15263 /*ambiguous_decls=*/NULL);
15266 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15267 the current context, return the TYPE_DECL. If TAG_NAME_P is
15268 true, the DECL indicates the class being defined in a class-head,
15269 or declared in an elaborated-type-specifier.
15271 Otherwise, return DECL. */
15274 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15276 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15277 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15280 template <typename T> struct B;
15283 template <typename T> struct A::B {};
15285 Similarly, in an elaborated-type-specifier:
15287 namespace N { struct X{}; }
15290 template <typename T> friend struct N::X;
15293 However, if the DECL refers to a class type, and we are in
15294 the scope of the class, then the name lookup automatically
15295 finds the TYPE_DECL created by build_self_reference rather
15296 than a TEMPLATE_DECL. For example, in:
15298 template <class T> struct S {
15302 there is no need to handle such case. */
15304 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15305 return DECL_TEMPLATE_RESULT (decl);
15310 /* If too many, or too few, template-parameter lists apply to the
15311 declarator, issue an error message. Returns TRUE if all went well,
15312 and FALSE otherwise. */
15315 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15316 cp_declarator *declarator)
15318 unsigned num_templates;
15320 /* We haven't seen any classes that involve template parameters yet. */
15323 switch (declarator->kind)
15326 if (declarator->u.id.qualifying_scope)
15331 scope = declarator->u.id.qualifying_scope;
15332 member = declarator->u.id.unqualified_name;
15334 while (scope && CLASS_TYPE_P (scope))
15336 /* You're supposed to have one `template <...>'
15337 for every template class, but you don't need one
15338 for a full specialization. For example:
15340 template <class T> struct S{};
15341 template <> struct S<int> { void f(); };
15342 void S<int>::f () {}
15344 is correct; there shouldn't be a `template <>' for
15345 the definition of `S<int>::f'. */
15346 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15347 /* If SCOPE does not have template information of any
15348 kind, then it is not a template, nor is it nested
15349 within a template. */
15351 if (explicit_class_specialization_p (scope))
15353 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15356 scope = TYPE_CONTEXT (scope);
15359 else if (TREE_CODE (declarator->u.id.unqualified_name)
15360 == TEMPLATE_ID_EXPR)
15361 /* If the DECLARATOR has the form `X<y>' then it uses one
15362 additional level of template parameters. */
15365 return cp_parser_check_template_parameters (parser,
15371 case cdk_reference:
15373 return (cp_parser_check_declarator_template_parameters
15374 (parser, declarator->declarator));
15380 gcc_unreachable ();
15385 /* NUM_TEMPLATES were used in the current declaration. If that is
15386 invalid, return FALSE and issue an error messages. Otherwise,
15390 cp_parser_check_template_parameters (cp_parser* parser,
15391 unsigned num_templates)
15393 /* If there are more template classes than parameter lists, we have
15396 template <class T> void S<T>::R<T>::f (); */
15397 if (parser->num_template_parameter_lists < num_templates)
15399 error ("too few template-parameter-lists");
15402 /* If there are the same number of template classes and parameter
15403 lists, that's OK. */
15404 if (parser->num_template_parameter_lists == num_templates)
15406 /* If there are more, but only one more, then we are referring to a
15407 member template. That's OK too. */
15408 if (parser->num_template_parameter_lists == num_templates + 1)
15410 /* Otherwise, there are too many template parameter lists. We have
15413 template <class T> template <class U> void S::f(); */
15414 error ("too many template-parameter-lists");
15418 /* Parse an optional `::' token indicating that the following name is
15419 from the global namespace. If so, PARSER->SCOPE is set to the
15420 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15421 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15422 Returns the new value of PARSER->SCOPE, if the `::' token is
15423 present, and NULL_TREE otherwise. */
15426 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15430 /* Peek at the next token. */
15431 token = cp_lexer_peek_token (parser->lexer);
15432 /* If we're looking at a `::' token then we're starting from the
15433 global namespace, not our current location. */
15434 if (token->type == CPP_SCOPE)
15436 /* Consume the `::' token. */
15437 cp_lexer_consume_token (parser->lexer);
15438 /* Set the SCOPE so that we know where to start the lookup. */
15439 parser->scope = global_namespace;
15440 parser->qualifying_scope = global_namespace;
15441 parser->object_scope = NULL_TREE;
15443 return parser->scope;
15445 else if (!current_scope_valid_p)
15447 parser->scope = NULL_TREE;
15448 parser->qualifying_scope = NULL_TREE;
15449 parser->object_scope = NULL_TREE;
15455 /* Returns TRUE if the upcoming token sequence is the start of a
15456 constructor declarator. If FRIEND_P is true, the declarator is
15457 preceded by the `friend' specifier. */
15460 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15462 bool constructor_p;
15463 tree type_decl = NULL_TREE;
15464 bool nested_name_p;
15465 cp_token *next_token;
15467 /* The common case is that this is not a constructor declarator, so
15468 try to avoid doing lots of work if at all possible. It's not
15469 valid declare a constructor at function scope. */
15470 if (parser->in_function_body)
15472 /* And only certain tokens can begin a constructor declarator. */
15473 next_token = cp_lexer_peek_token (parser->lexer);
15474 if (next_token->type != CPP_NAME
15475 && next_token->type != CPP_SCOPE
15476 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15477 && next_token->type != CPP_TEMPLATE_ID)
15480 /* Parse tentatively; we are going to roll back all of the tokens
15482 cp_parser_parse_tentatively (parser);
15483 /* Assume that we are looking at a constructor declarator. */
15484 constructor_p = true;
15486 /* Look for the optional `::' operator. */
15487 cp_parser_global_scope_opt (parser,
15488 /*current_scope_valid_p=*/false);
15489 /* Look for the nested-name-specifier. */
15491 = (cp_parser_nested_name_specifier_opt (parser,
15492 /*typename_keyword_p=*/false,
15493 /*check_dependency_p=*/false,
15495 /*is_declaration=*/false)
15497 /* Outside of a class-specifier, there must be a
15498 nested-name-specifier. */
15499 if (!nested_name_p &&
15500 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15502 constructor_p = false;
15503 /* If we still think that this might be a constructor-declarator,
15504 look for a class-name. */
15509 template <typename T> struct S { S(); };
15510 template <typename T> S<T>::S ();
15512 we must recognize that the nested `S' names a class.
15515 template <typename T> S<T>::S<T> ();
15517 we must recognize that the nested `S' names a template. */
15518 type_decl = cp_parser_class_name (parser,
15519 /*typename_keyword_p=*/false,
15520 /*template_keyword_p=*/false,
15522 /*check_dependency_p=*/false,
15523 /*class_head_p=*/false,
15524 /*is_declaration=*/false);
15525 /* If there was no class-name, then this is not a constructor. */
15526 constructor_p = !cp_parser_error_occurred (parser);
15529 /* If we're still considering a constructor, we have to see a `(',
15530 to begin the parameter-declaration-clause, followed by either a
15531 `)', an `...', or a decl-specifier. We need to check for a
15532 type-specifier to avoid being fooled into thinking that:
15536 is a constructor. (It is actually a function named `f' that
15537 takes one parameter (of type `int') and returns a value of type
15540 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15543 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15544 /* A parameter declaration begins with a decl-specifier,
15545 which is either the "attribute" keyword, a storage class
15546 specifier, or (usually) a type-specifier. */
15547 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15550 tree pushed_scope = NULL_TREE;
15551 unsigned saved_num_template_parameter_lists;
15553 /* Names appearing in the type-specifier should be looked up
15554 in the scope of the class. */
15555 if (current_class_type)
15559 type = TREE_TYPE (type_decl);
15560 if (TREE_CODE (type) == TYPENAME_TYPE)
15562 type = resolve_typename_type (type,
15563 /*only_current_p=*/false);
15564 if (type == error_mark_node)
15566 cp_parser_abort_tentative_parse (parser);
15570 pushed_scope = push_scope (type);
15573 /* Inside the constructor parameter list, surrounding
15574 template-parameter-lists do not apply. */
15575 saved_num_template_parameter_lists
15576 = parser->num_template_parameter_lists;
15577 parser->num_template_parameter_lists = 0;
15579 /* Look for the type-specifier. */
15580 cp_parser_type_specifier (parser,
15581 CP_PARSER_FLAGS_NONE,
15582 /*decl_specs=*/NULL,
15583 /*is_declarator=*/true,
15584 /*declares_class_or_enum=*/NULL,
15585 /*is_cv_qualifier=*/NULL);
15587 parser->num_template_parameter_lists
15588 = saved_num_template_parameter_lists;
15590 /* Leave the scope of the class. */
15592 pop_scope (pushed_scope);
15594 constructor_p = !cp_parser_error_occurred (parser);
15598 constructor_p = false;
15599 /* We did not really want to consume any tokens. */
15600 cp_parser_abort_tentative_parse (parser);
15602 return constructor_p;
15605 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15606 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15607 they must be performed once we are in the scope of the function.
15609 Returns the function defined. */
15612 cp_parser_function_definition_from_specifiers_and_declarator
15613 (cp_parser* parser,
15614 cp_decl_specifier_seq *decl_specifiers,
15616 const cp_declarator *declarator)
15621 /* Begin the function-definition. */
15622 success_p = start_function (decl_specifiers, declarator, attributes);
15624 /* The things we're about to see are not directly qualified by any
15625 template headers we've seen thus far. */
15626 reset_specialization ();
15628 /* If there were names looked up in the decl-specifier-seq that we
15629 did not check, check them now. We must wait until we are in the
15630 scope of the function to perform the checks, since the function
15631 might be a friend. */
15632 perform_deferred_access_checks ();
15636 /* Skip the entire function. */
15637 cp_parser_skip_to_end_of_block_or_statement (parser);
15638 fn = error_mark_node;
15641 fn = cp_parser_function_definition_after_declarator (parser,
15642 /*inline_p=*/false);
15647 /* Parse the part of a function-definition that follows the
15648 declarator. INLINE_P is TRUE iff this function is an inline
15649 function defined with a class-specifier.
15651 Returns the function defined. */
15654 cp_parser_function_definition_after_declarator (cp_parser* parser,
15658 bool ctor_initializer_p = false;
15659 bool saved_in_unbraced_linkage_specification_p;
15660 bool saved_in_function_body;
15661 unsigned saved_num_template_parameter_lists;
15663 saved_in_function_body = parser->in_function_body;
15664 parser->in_function_body = true;
15665 /* If the next token is `return', then the code may be trying to
15666 make use of the "named return value" extension that G++ used to
15668 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15670 /* Consume the `return' keyword. */
15671 cp_lexer_consume_token (parser->lexer);
15672 /* Look for the identifier that indicates what value is to be
15674 cp_parser_identifier (parser);
15675 /* Issue an error message. */
15676 error ("named return values are no longer supported");
15677 /* Skip tokens until we reach the start of the function body. */
15680 cp_token *token = cp_lexer_peek_token (parser->lexer);
15681 if (token->type == CPP_OPEN_BRACE
15682 || token->type == CPP_EOF
15683 || token->type == CPP_PRAGMA_EOL)
15685 cp_lexer_consume_token (parser->lexer);
15688 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15689 anything declared inside `f'. */
15690 saved_in_unbraced_linkage_specification_p
15691 = parser->in_unbraced_linkage_specification_p;
15692 parser->in_unbraced_linkage_specification_p = false;
15693 /* Inside the function, surrounding template-parameter-lists do not
15695 saved_num_template_parameter_lists
15696 = parser->num_template_parameter_lists;
15697 parser->num_template_parameter_lists = 0;
15698 /* If the next token is `try', then we are looking at a
15699 function-try-block. */
15700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15701 ctor_initializer_p = cp_parser_function_try_block (parser);
15702 /* A function-try-block includes the function-body, so we only do
15703 this next part if we're not processing a function-try-block. */
15706 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15708 /* Finish the function. */
15709 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15710 (inline_p ? 2 : 0));
15711 /* Generate code for it, if necessary. */
15712 expand_or_defer_fn (fn);
15713 /* Restore the saved values. */
15714 parser->in_unbraced_linkage_specification_p
15715 = saved_in_unbraced_linkage_specification_p;
15716 parser->num_template_parameter_lists
15717 = saved_num_template_parameter_lists;
15718 parser->in_function_body = saved_in_function_body;
15723 /* Parse a template-declaration, assuming that the `export' (and
15724 `extern') keywords, if present, has already been scanned. MEMBER_P
15725 is as for cp_parser_template_declaration. */
15728 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15730 tree decl = NULL_TREE;
15731 VEC (deferred_access_check,gc) *checks;
15732 tree parameter_list;
15733 bool friend_p = false;
15734 bool need_lang_pop;
15736 /* Look for the `template' keyword. */
15737 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15741 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15743 if (at_class_scope_p () && current_function_decl)
15745 /* 14.5.2.2 [temp.mem]
15747 A local class shall not have member templates. */
15748 error ("invalid declaration of member template in local class");
15749 cp_parser_skip_to_end_of_block_or_statement (parser);
15754 A template ... shall not have C linkage. */
15755 if (current_lang_name == lang_name_c)
15757 error ("template with C linkage");
15758 /* Give it C++ linkage to avoid confusing other parts of the
15760 push_lang_context (lang_name_cplusplus);
15761 need_lang_pop = true;
15764 need_lang_pop = false;
15766 /* We cannot perform access checks on the template parameter
15767 declarations until we know what is being declared, just as we
15768 cannot check the decl-specifier list. */
15769 push_deferring_access_checks (dk_deferred);
15771 /* If the next token is `>', then we have an invalid
15772 specialization. Rather than complain about an invalid template
15773 parameter, issue an error message here. */
15774 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15776 cp_parser_error (parser, "invalid explicit specialization");
15777 begin_specialization ();
15778 parameter_list = NULL_TREE;
15781 /* Parse the template parameters. */
15782 parameter_list = cp_parser_template_parameter_list (parser);
15784 /* Get the deferred access checks from the parameter list. These
15785 will be checked once we know what is being declared, as for a
15786 member template the checks must be performed in the scope of the
15787 class containing the member. */
15788 checks = get_deferred_access_checks ();
15790 /* Look for the `>'. */
15791 cp_parser_skip_to_end_of_template_parameter_list (parser);
15792 /* We just processed one more parameter list. */
15793 ++parser->num_template_parameter_lists;
15794 /* If the next token is `template', there are more template
15796 if (cp_lexer_next_token_is_keyword (parser->lexer,
15798 cp_parser_template_declaration_after_export (parser, member_p);
15801 /* There are no access checks when parsing a template, as we do not
15802 know if a specialization will be a friend. */
15803 push_deferring_access_checks (dk_no_check);
15804 decl = cp_parser_single_declaration (parser,
15808 pop_deferring_access_checks ();
15810 /* If this is a member template declaration, let the front
15812 if (member_p && !friend_p && decl)
15814 if (TREE_CODE (decl) == TYPE_DECL)
15815 cp_parser_check_access_in_redeclaration (decl);
15817 decl = finish_member_template_decl (decl);
15819 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15820 make_friend_class (current_class_type, TREE_TYPE (decl),
15821 /*complain=*/true);
15823 /* We are done with the current parameter list. */
15824 --parser->num_template_parameter_lists;
15826 pop_deferring_access_checks ();
15829 finish_template_decl (parameter_list);
15831 /* Register member declarations. */
15832 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15833 finish_member_declaration (decl);
15834 /* For the erroneous case of a template with C linkage, we pushed an
15835 implicit C++ linkage scope; exit that scope now. */
15837 pop_lang_context ();
15838 /* If DECL is a function template, we must return to parse it later.
15839 (Even though there is no definition, there might be default
15840 arguments that need handling.) */
15841 if (member_p && decl
15842 && (TREE_CODE (decl) == FUNCTION_DECL
15843 || DECL_FUNCTION_TEMPLATE_P (decl)))
15844 TREE_VALUE (parser->unparsed_functions_queues)
15845 = tree_cons (NULL_TREE, decl,
15846 TREE_VALUE (parser->unparsed_functions_queues));
15849 /* Perform the deferred access checks from a template-parameter-list.
15850 CHECKS is a TREE_LIST of access checks, as returned by
15851 get_deferred_access_checks. */
15854 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15856 ++processing_template_parmlist;
15857 perform_access_checks (checks);
15858 --processing_template_parmlist;
15861 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15862 `function-definition' sequence. MEMBER_P is true, this declaration
15863 appears in a class scope.
15865 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15866 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15869 cp_parser_single_declaration (cp_parser* parser,
15870 VEC (deferred_access_check,gc)* checks,
15874 int declares_class_or_enum;
15875 tree decl = NULL_TREE;
15876 cp_decl_specifier_seq decl_specifiers;
15877 bool function_definition_p = false;
15879 /* This function is only used when processing a template
15881 gcc_assert (innermost_scope_kind () == sk_template_parms
15882 || innermost_scope_kind () == sk_template_spec);
15884 /* Defer access checks until we know what is being declared. */
15885 push_deferring_access_checks (dk_deferred);
15887 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15889 cp_parser_decl_specifier_seq (parser,
15890 CP_PARSER_FLAGS_OPTIONAL,
15892 &declares_class_or_enum);
15894 *friend_p = cp_parser_friend_p (&decl_specifiers);
15896 /* There are no template typedefs. */
15897 if (decl_specifiers.specs[(int) ds_typedef])
15899 error ("template declaration of %qs", "typedef");
15900 decl = error_mark_node;
15903 /* Gather up the access checks that occurred the
15904 decl-specifier-seq. */
15905 stop_deferring_access_checks ();
15907 /* Check for the declaration of a template class. */
15908 if (declares_class_or_enum)
15910 if (cp_parser_declares_only_class_p (parser))
15912 decl = shadow_tag (&decl_specifiers);
15917 friend template <typename T> struct A<T>::B;
15920 A<T>::B will be represented by a TYPENAME_TYPE, and
15921 therefore not recognized by shadow_tag. */
15922 if (friend_p && *friend_p
15924 && decl_specifiers.type
15925 && TYPE_P (decl_specifiers.type))
15926 decl = decl_specifiers.type;
15928 if (decl && decl != error_mark_node)
15929 decl = TYPE_NAME (decl);
15931 decl = error_mark_node;
15933 /* Perform access checks for template parameters. */
15934 cp_parser_perform_template_parameter_access_checks (checks);
15937 /* If it's not a template class, try for a template function. If
15938 the next token is a `;', then this declaration does not declare
15939 anything. But, if there were errors in the decl-specifiers, then
15940 the error might well have come from an attempted class-specifier.
15941 In that case, there's no need to warn about a missing declarator. */
15943 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15944 || decl_specifiers.type != error_mark_node))
15945 decl = cp_parser_init_declarator (parser,
15948 /*function_definition_allowed_p=*/true,
15950 declares_class_or_enum,
15951 &function_definition_p);
15953 pop_deferring_access_checks ();
15955 /* Clear any current qualification; whatever comes next is the start
15956 of something new. */
15957 parser->scope = NULL_TREE;
15958 parser->qualifying_scope = NULL_TREE;
15959 parser->object_scope = NULL_TREE;
15960 /* Look for a trailing `;' after the declaration. */
15961 if (!function_definition_p
15962 && (decl == error_mark_node
15963 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15964 cp_parser_skip_to_end_of_block_or_statement (parser);
15969 /* Parse a cast-expression that is not the operand of a unary "&". */
15972 cp_parser_simple_cast_expression (cp_parser *parser)
15974 return cp_parser_cast_expression (parser, /*address_p=*/false,
15978 /* Parse a functional cast to TYPE. Returns an expression
15979 representing the cast. */
15982 cp_parser_functional_cast (cp_parser* parser, tree type)
15984 tree expression_list;
15988 = cp_parser_parenthesized_expression_list (parser, false,
15990 /*non_constant_p=*/NULL);
15992 cast = build_functional_cast (type, expression_list);
15993 /* [expr.const]/1: In an integral constant expression "only type
15994 conversions to integral or enumeration type can be used". */
15995 if (TREE_CODE (type) == TYPE_DECL)
15996 type = TREE_TYPE (type);
15997 if (cast != error_mark_node
15998 && !cast_valid_in_integral_constant_expression_p (type)
15999 && (cp_parser_non_integral_constant_expression
16000 (parser, "a call to a constructor")))
16001 return error_mark_node;
16005 /* Save the tokens that make up the body of a member function defined
16006 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16007 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16008 specifiers applied to the declaration. Returns the FUNCTION_DECL
16009 for the member function. */
16012 cp_parser_save_member_function_body (cp_parser* parser,
16013 cp_decl_specifier_seq *decl_specifiers,
16014 cp_declarator *declarator,
16021 /* Create the function-declaration. */
16022 fn = start_method (decl_specifiers, declarator, attributes);
16023 /* If something went badly wrong, bail out now. */
16024 if (fn == error_mark_node)
16026 /* If there's a function-body, skip it. */
16027 if (cp_parser_token_starts_function_definition_p
16028 (cp_lexer_peek_token (parser->lexer)))
16029 cp_parser_skip_to_end_of_block_or_statement (parser);
16030 return error_mark_node;
16033 /* Remember it, if there default args to post process. */
16034 cp_parser_save_default_args (parser, fn);
16036 /* Save away the tokens that make up the body of the
16038 first = parser->lexer->next_token;
16039 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16040 /* Handle function try blocks. */
16041 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16042 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16043 last = parser->lexer->next_token;
16045 /* Save away the inline definition; we will process it when the
16046 class is complete. */
16047 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16048 DECL_PENDING_INLINE_P (fn) = 1;
16050 /* We need to know that this was defined in the class, so that
16051 friend templates are handled correctly. */
16052 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16054 /* We're done with the inline definition. */
16055 finish_method (fn);
16057 /* Add FN to the queue of functions to be parsed later. */
16058 TREE_VALUE (parser->unparsed_functions_queues)
16059 = tree_cons (NULL_TREE, fn,
16060 TREE_VALUE (parser->unparsed_functions_queues));
16065 /* Parse a template-argument-list, as well as the trailing ">" (but
16066 not the opening ">"). See cp_parser_template_argument_list for the
16070 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16074 tree saved_qualifying_scope;
16075 tree saved_object_scope;
16076 bool saved_greater_than_is_operator_p;
16077 bool saved_skip_evaluation;
16081 When parsing a template-id, the first non-nested `>' is taken as
16082 the end of the template-argument-list rather than a greater-than
16084 saved_greater_than_is_operator_p
16085 = parser->greater_than_is_operator_p;
16086 parser->greater_than_is_operator_p = false;
16087 /* Parsing the argument list may modify SCOPE, so we save it
16089 saved_scope = parser->scope;
16090 saved_qualifying_scope = parser->qualifying_scope;
16091 saved_object_scope = parser->object_scope;
16092 /* We need to evaluate the template arguments, even though this
16093 template-id may be nested within a "sizeof". */
16094 saved_skip_evaluation = skip_evaluation;
16095 skip_evaluation = false;
16096 /* Parse the template-argument-list itself. */
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16098 arguments = NULL_TREE;
16100 arguments = cp_parser_template_argument_list (parser);
16101 /* Look for the `>' that ends the template-argument-list. If we find
16102 a '>>' instead, it's probably just a typo. */
16103 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16105 if (!saved_greater_than_is_operator_p)
16107 /* If we're in a nested template argument list, the '>>' has
16108 to be a typo for '> >'. We emit the error message, but we
16109 continue parsing and we push a '>' as next token, so that
16110 the argument list will be parsed correctly. Note that the
16111 global source location is still on the token before the
16112 '>>', so we need to say explicitly where we want it. */
16113 cp_token *token = cp_lexer_peek_token (parser->lexer);
16114 error ("%H%<>>%> should be %<> >%> "
16115 "within a nested template argument list",
16118 /* ??? Proper recovery should terminate two levels of
16119 template argument list here. */
16120 token->type = CPP_GREATER;
16124 /* If this is not a nested template argument list, the '>>'
16125 is a typo for '>'. Emit an error message and continue.
16126 Same deal about the token location, but here we can get it
16127 right by consuming the '>>' before issuing the diagnostic. */
16128 cp_lexer_consume_token (parser->lexer);
16129 error ("spurious %<>>%>, use %<>%> to terminate "
16130 "a template argument list");
16134 cp_parser_skip_to_end_of_template_parameter_list (parser);
16135 /* The `>' token might be a greater-than operator again now. */
16136 parser->greater_than_is_operator_p
16137 = saved_greater_than_is_operator_p;
16138 /* Restore the SAVED_SCOPE. */
16139 parser->scope = saved_scope;
16140 parser->qualifying_scope = saved_qualifying_scope;
16141 parser->object_scope = saved_object_scope;
16142 skip_evaluation = saved_skip_evaluation;
16147 /* MEMBER_FUNCTION is a member function, or a friend. If default
16148 arguments, or the body of the function have not yet been parsed,
16152 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16154 /* If this member is a template, get the underlying
16156 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16157 member_function = DECL_TEMPLATE_RESULT (member_function);
16159 /* There should not be any class definitions in progress at this
16160 point; the bodies of members are only parsed outside of all class
16162 gcc_assert (parser->num_classes_being_defined == 0);
16163 /* While we're parsing the member functions we might encounter more
16164 classes. We want to handle them right away, but we don't want
16165 them getting mixed up with functions that are currently in the
16167 parser->unparsed_functions_queues
16168 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16170 /* Make sure that any template parameters are in scope. */
16171 maybe_begin_member_template_processing (member_function);
16173 /* If the body of the function has not yet been parsed, parse it
16175 if (DECL_PENDING_INLINE_P (member_function))
16177 tree function_scope;
16178 cp_token_cache *tokens;
16180 /* The function is no longer pending; we are processing it. */
16181 tokens = DECL_PENDING_INLINE_INFO (member_function);
16182 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16183 DECL_PENDING_INLINE_P (member_function) = 0;
16185 /* If this is a local class, enter the scope of the containing
16187 function_scope = current_function_decl;
16188 if (function_scope)
16189 push_function_context_to (function_scope);
16192 /* Push the body of the function onto the lexer stack. */
16193 cp_parser_push_lexer_for_tokens (parser, tokens);
16195 /* Let the front end know that we going to be defining this
16197 start_preparsed_function (member_function, NULL_TREE,
16198 SF_PRE_PARSED | SF_INCLASS_INLINE);
16200 /* Don't do access checking if it is a templated function. */
16201 if (processing_template_decl)
16202 push_deferring_access_checks (dk_no_check);
16204 /* Now, parse the body of the function. */
16205 cp_parser_function_definition_after_declarator (parser,
16206 /*inline_p=*/true);
16208 if (processing_template_decl)
16209 pop_deferring_access_checks ();
16211 /* Leave the scope of the containing function. */
16212 if (function_scope)
16213 pop_function_context_from (function_scope);
16214 cp_parser_pop_lexer (parser);
16217 /* Remove any template parameters from the symbol table. */
16218 maybe_end_member_template_processing ();
16220 /* Restore the queue. */
16221 parser->unparsed_functions_queues
16222 = TREE_CHAIN (parser->unparsed_functions_queues);
16225 /* If DECL contains any default args, remember it on the unparsed
16226 functions queue. */
16229 cp_parser_save_default_args (cp_parser* parser, tree decl)
16233 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16235 probe = TREE_CHAIN (probe))
16236 if (TREE_PURPOSE (probe))
16238 TREE_PURPOSE (parser->unparsed_functions_queues)
16239 = tree_cons (current_class_type, decl,
16240 TREE_PURPOSE (parser->unparsed_functions_queues));
16245 /* FN is a FUNCTION_DECL which may contains a parameter with an
16246 unparsed DEFAULT_ARG. Parse the default args now. This function
16247 assumes that the current scope is the scope in which the default
16248 argument should be processed. */
16251 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16253 bool saved_local_variables_forbidden_p;
16256 /* While we're parsing the default args, we might (due to the
16257 statement expression extension) encounter more classes. We want
16258 to handle them right away, but we don't want them getting mixed
16259 up with default args that are currently in the queue. */
16260 parser->unparsed_functions_queues
16261 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16263 /* Local variable names (and the `this' keyword) may not appear
16264 in a default argument. */
16265 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16266 parser->local_variables_forbidden_p = true;
16268 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16270 parm = TREE_CHAIN (parm))
16272 cp_token_cache *tokens;
16273 tree default_arg = TREE_PURPOSE (parm);
16275 VEC(tree,gc) *insts;
16282 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16283 /* This can happen for a friend declaration for a function
16284 already declared with default arguments. */
16287 /* Push the saved tokens for the default argument onto the parser's
16289 tokens = DEFARG_TOKENS (default_arg);
16290 cp_parser_push_lexer_for_tokens (parser, tokens);
16292 /* Parse the assignment-expression. */
16293 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16295 if (!processing_template_decl)
16296 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16298 TREE_PURPOSE (parm) = parsed_arg;
16300 /* Update any instantiations we've already created. */
16301 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16302 VEC_iterate (tree, insts, ix, copy); ix++)
16303 TREE_PURPOSE (copy) = parsed_arg;
16305 /* If the token stream has not been completely used up, then
16306 there was extra junk after the end of the default
16308 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16309 cp_parser_error (parser, "expected %<,%>");
16311 /* Revert to the main lexer. */
16312 cp_parser_pop_lexer (parser);
16315 /* Make sure no default arg is missing. */
16316 check_default_args (fn);
16318 /* Restore the state of local_variables_forbidden_p. */
16319 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16321 /* Restore the queue. */
16322 parser->unparsed_functions_queues
16323 = TREE_CHAIN (parser->unparsed_functions_queues);
16326 /* Parse the operand of `sizeof' (or a similar operator). Returns
16327 either a TYPE or an expression, depending on the form of the
16328 input. The KEYWORD indicates which kind of expression we have
16332 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16334 static const char *format;
16335 tree expr = NULL_TREE;
16336 const char *saved_message;
16337 bool saved_integral_constant_expression_p;
16338 bool saved_non_integral_constant_expression_p;
16340 /* Initialize FORMAT the first time we get here. */
16342 format = "types may not be defined in '%s' expressions";
16344 /* Types cannot be defined in a `sizeof' expression. Save away the
16346 saved_message = parser->type_definition_forbidden_message;
16347 /* And create the new one. */
16348 parser->type_definition_forbidden_message
16349 = XNEWVEC (const char, strlen (format)
16350 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16352 sprintf ((char *) parser->type_definition_forbidden_message,
16353 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16355 /* The restrictions on constant-expressions do not apply inside
16356 sizeof expressions. */
16357 saved_integral_constant_expression_p
16358 = parser->integral_constant_expression_p;
16359 saved_non_integral_constant_expression_p
16360 = parser->non_integral_constant_expression_p;
16361 parser->integral_constant_expression_p = false;
16363 /* Do not actually evaluate the expression. */
16365 /* If it's a `(', then we might be looking at the type-id
16367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16370 bool saved_in_type_id_in_expr_p;
16372 /* We can't be sure yet whether we're looking at a type-id or an
16374 cp_parser_parse_tentatively (parser);
16375 /* Consume the `('. */
16376 cp_lexer_consume_token (parser->lexer);
16377 /* Parse the type-id. */
16378 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16379 parser->in_type_id_in_expr_p = true;
16380 type = cp_parser_type_id (parser);
16381 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16382 /* Now, look for the trailing `)'. */
16383 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16384 /* If all went well, then we're done. */
16385 if (cp_parser_parse_definitely (parser))
16387 cp_decl_specifier_seq decl_specs;
16389 /* Build a trivial decl-specifier-seq. */
16390 clear_decl_specs (&decl_specs);
16391 decl_specs.type = type;
16393 /* Call grokdeclarator to figure out what type this is. */
16394 expr = grokdeclarator (NULL,
16398 /*attrlist=*/NULL);
16402 /* If the type-id production did not work out, then we must be
16403 looking at the unary-expression production. */
16405 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16407 /* Go back to evaluating expressions. */
16410 /* Free the message we created. */
16411 free ((char *) parser->type_definition_forbidden_message);
16412 /* And restore the old one. */
16413 parser->type_definition_forbidden_message = saved_message;
16414 parser->integral_constant_expression_p
16415 = saved_integral_constant_expression_p;
16416 parser->non_integral_constant_expression_p
16417 = saved_non_integral_constant_expression_p;
16422 /* If the current declaration has no declarator, return true. */
16425 cp_parser_declares_only_class_p (cp_parser *parser)
16427 /* If the next token is a `;' or a `,' then there is no
16429 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16430 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16433 /* Update the DECL_SPECS to reflect the storage class indicated by
16437 cp_parser_set_storage_class (cp_parser *parser,
16438 cp_decl_specifier_seq *decl_specs,
16441 cp_storage_class storage_class;
16443 if (parser->in_unbraced_linkage_specification_p)
16445 error ("invalid use of %qD in linkage specification",
16446 ridpointers[keyword]);
16449 else if (decl_specs->storage_class != sc_none)
16451 decl_specs->conflicting_specifiers_p = true;
16455 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16456 && decl_specs->specs[(int) ds_thread])
16458 error ("%<__thread%> before %qD", ridpointers[keyword]);
16459 decl_specs->specs[(int) ds_thread] = 0;
16465 storage_class = sc_auto;
16468 storage_class = sc_register;
16471 storage_class = sc_static;
16474 storage_class = sc_extern;
16477 storage_class = sc_mutable;
16480 gcc_unreachable ();
16482 decl_specs->storage_class = storage_class;
16484 /* A storage class specifier cannot be applied alongside a typedef
16485 specifier. If there is a typedef specifier present then set
16486 conflicting_specifiers_p which will trigger an error later
16487 on in grokdeclarator. */
16488 if (decl_specs->specs[(int)ds_typedef])
16489 decl_specs->conflicting_specifiers_p = true;
16492 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16493 is true, the type is a user-defined type; otherwise it is a
16494 built-in type specified by a keyword. */
16497 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16499 bool user_defined_p)
16501 decl_specs->any_specifiers_p = true;
16503 /* If the user tries to redeclare bool or wchar_t (with, for
16504 example, in "typedef int wchar_t;") we remember that this is what
16505 happened. In system headers, we ignore these declarations so
16506 that G++ can work with system headers that are not C++-safe. */
16507 if (decl_specs->specs[(int) ds_typedef]
16509 && (type_spec == boolean_type_node
16510 || type_spec == wchar_type_node)
16511 && (decl_specs->type
16512 || decl_specs->specs[(int) ds_long]
16513 || decl_specs->specs[(int) ds_short]
16514 || decl_specs->specs[(int) ds_unsigned]
16515 || decl_specs->specs[(int) ds_signed]))
16517 decl_specs->redefined_builtin_type = type_spec;
16518 if (!decl_specs->type)
16520 decl_specs->type = type_spec;
16521 decl_specs->user_defined_type_p = false;
16524 else if (decl_specs->type)
16525 decl_specs->multiple_types_p = true;
16528 decl_specs->type = type_spec;
16529 decl_specs->user_defined_type_p = user_defined_p;
16530 decl_specs->redefined_builtin_type = NULL_TREE;
16534 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16535 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16538 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16540 return decl_specifiers->specs[(int) ds_friend] != 0;
16543 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16544 issue an error message indicating that TOKEN_DESC was expected.
16546 Returns the token consumed, if the token had the appropriate type.
16547 Otherwise, returns NULL. */
16550 cp_parser_require (cp_parser* parser,
16551 enum cpp_ttype type,
16552 const char* token_desc)
16554 if (cp_lexer_next_token_is (parser->lexer, type))
16555 return cp_lexer_consume_token (parser->lexer);
16558 /* Output the MESSAGE -- unless we're parsing tentatively. */
16559 if (!cp_parser_simulate_error (parser))
16561 char *message = concat ("expected ", token_desc, NULL);
16562 cp_parser_error (parser, message);
16569 /* An error message is produced if the next token is not '>'.
16570 All further tokens are skipped until the desired token is
16571 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16574 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16576 /* Current level of '< ... >'. */
16577 unsigned level = 0;
16578 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16579 unsigned nesting_depth = 0;
16581 /* Are we ready, yet? If not, issue error message. */
16582 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16585 /* Skip tokens until the desired token is found. */
16588 /* Peek at the next token. */
16589 switch (cp_lexer_peek_token (parser->lexer)->type)
16592 if (!nesting_depth)
16597 if (!nesting_depth && level-- == 0)
16599 /* We've reached the token we want, consume it and stop. */
16600 cp_lexer_consume_token (parser->lexer);
16605 case CPP_OPEN_PAREN:
16606 case CPP_OPEN_SQUARE:
16610 case CPP_CLOSE_PAREN:
16611 case CPP_CLOSE_SQUARE:
16612 if (nesting_depth-- == 0)
16617 case CPP_PRAGMA_EOL:
16618 case CPP_SEMICOLON:
16619 case CPP_OPEN_BRACE:
16620 case CPP_CLOSE_BRACE:
16621 /* The '>' was probably forgotten, don't look further. */
16628 /* Consume this token. */
16629 cp_lexer_consume_token (parser->lexer);
16633 /* If the next token is the indicated keyword, consume it. Otherwise,
16634 issue an error message indicating that TOKEN_DESC was expected.
16636 Returns the token consumed, if the token had the appropriate type.
16637 Otherwise, returns NULL. */
16640 cp_parser_require_keyword (cp_parser* parser,
16642 const char* token_desc)
16644 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16646 if (token && token->keyword != keyword)
16648 dyn_string_t error_msg;
16650 /* Format the error message. */
16651 error_msg = dyn_string_new (0);
16652 dyn_string_append_cstr (error_msg, "expected ");
16653 dyn_string_append_cstr (error_msg, token_desc);
16654 cp_parser_error (parser, error_msg->s);
16655 dyn_string_delete (error_msg);
16662 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16663 function-definition. */
16666 cp_parser_token_starts_function_definition_p (cp_token* token)
16668 return (/* An ordinary function-body begins with an `{'. */
16669 token->type == CPP_OPEN_BRACE
16670 /* A ctor-initializer begins with a `:'. */
16671 || token->type == CPP_COLON
16672 /* A function-try-block begins with `try'. */
16673 || token->keyword == RID_TRY
16674 /* The named return value extension begins with `return'. */
16675 || token->keyword == RID_RETURN);
16678 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16682 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16686 token = cp_lexer_peek_token (parser->lexer);
16687 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16690 /* Returns TRUE iff the next token is the "," or ">" ending a
16691 template-argument. */
16694 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16698 token = cp_lexer_peek_token (parser->lexer);
16699 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16702 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16703 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16706 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16711 token = cp_lexer_peek_nth_token (parser->lexer, n);
16712 if (token->type == CPP_LESS)
16714 /* Check for the sequence `<::' in the original code. It would be lexed as
16715 `[:', where `[' is a digraph, and there is no whitespace before
16717 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16720 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16721 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16727 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16728 or none_type otherwise. */
16730 static enum tag_types
16731 cp_parser_token_is_class_key (cp_token* token)
16733 switch (token->keyword)
16738 return record_type;
16747 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16750 cp_parser_check_class_key (enum tag_types class_key, tree type)
16752 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16753 pedwarn ("%qs tag used in naming %q#T",
16754 class_key == union_type ? "union"
16755 : class_key == record_type ? "struct" : "class",
16759 /* Issue an error message if DECL is redeclared with different
16760 access than its original declaration [class.access.spec/3].
16761 This applies to nested classes and nested class templates.
16765 cp_parser_check_access_in_redeclaration (tree decl)
16767 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16770 if ((TREE_PRIVATE (decl)
16771 != (current_access_specifier == access_private_node))
16772 || (TREE_PROTECTED (decl)
16773 != (current_access_specifier == access_protected_node)))
16774 error ("%qD redeclared with different access", decl);
16777 /* Look for the `template' keyword, as a syntactic disambiguator.
16778 Return TRUE iff it is present, in which case it will be
16782 cp_parser_optional_template_keyword (cp_parser *parser)
16784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16786 /* The `template' keyword can only be used within templates;
16787 outside templates the parser can always figure out what is a
16788 template and what is not. */
16789 if (!processing_template_decl)
16791 error ("%<template%> (as a disambiguator) is only allowed "
16792 "within templates");
16793 /* If this part of the token stream is rescanned, the same
16794 error message would be generated. So, we purge the token
16795 from the stream. */
16796 cp_lexer_purge_token (parser->lexer);
16801 /* Consume the `template' keyword. */
16802 cp_lexer_consume_token (parser->lexer);
16810 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16811 set PARSER->SCOPE, and perform other related actions. */
16814 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16817 struct tree_check *check_value;
16818 deferred_access_check *chk;
16819 VEC (deferred_access_check,gc) *checks;
16821 /* Get the stored value. */
16822 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16823 /* Perform any access checks that were deferred. */
16824 checks = check_value->checks;
16828 VEC_iterate (deferred_access_check, checks, i, chk) ;
16831 perform_or_defer_access_check (chk->binfo,
16836 /* Set the scope from the stored value. */
16837 parser->scope = check_value->value;
16838 parser->qualifying_scope = check_value->qualifying_scope;
16839 parser->object_scope = NULL_TREE;
16842 /* Consume tokens up through a non-nested END token. */
16845 cp_parser_cache_group (cp_parser *parser,
16846 enum cpp_ttype end,
16853 /* Abort a parenthesized expression if we encounter a brace. */
16854 if ((end == CPP_CLOSE_PAREN || depth == 0)
16855 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16857 /* If we've reached the end of the file, stop. */
16858 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16859 || (end != CPP_PRAGMA_EOL
16860 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16862 /* Consume the next token. */
16863 token = cp_lexer_consume_token (parser->lexer);
16864 /* See if it starts a new group. */
16865 if (token->type == CPP_OPEN_BRACE)
16867 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16871 else if (token->type == CPP_OPEN_PAREN)
16872 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16873 else if (token->type == CPP_PRAGMA)
16874 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16875 else if (token->type == end)
16880 /* Begin parsing tentatively. We always save tokens while parsing
16881 tentatively so that if the tentative parsing fails we can restore the
16885 cp_parser_parse_tentatively (cp_parser* parser)
16887 /* Enter a new parsing context. */
16888 parser->context = cp_parser_context_new (parser->context);
16889 /* Begin saving tokens. */
16890 cp_lexer_save_tokens (parser->lexer);
16891 /* In order to avoid repetitive access control error messages,
16892 access checks are queued up until we are no longer parsing
16894 push_deferring_access_checks (dk_deferred);
16897 /* Commit to the currently active tentative parse. */
16900 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16902 cp_parser_context *context;
16905 /* Mark all of the levels as committed. */
16906 lexer = parser->lexer;
16907 for (context = parser->context; context->next; context = context->next)
16909 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16911 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16912 while (!cp_lexer_saving_tokens (lexer))
16913 lexer = lexer->next;
16914 cp_lexer_commit_tokens (lexer);
16918 /* Abort the currently active tentative parse. All consumed tokens
16919 will be rolled back, and no diagnostics will be issued. */
16922 cp_parser_abort_tentative_parse (cp_parser* parser)
16924 cp_parser_simulate_error (parser);
16925 /* Now, pretend that we want to see if the construct was
16926 successfully parsed. */
16927 cp_parser_parse_definitely (parser);
16930 /* Stop parsing tentatively. If a parse error has occurred, restore the
16931 token stream. Otherwise, commit to the tokens we have consumed.
16932 Returns true if no error occurred; false otherwise. */
16935 cp_parser_parse_definitely (cp_parser* parser)
16937 bool error_occurred;
16938 cp_parser_context *context;
16940 /* Remember whether or not an error occurred, since we are about to
16941 destroy that information. */
16942 error_occurred = cp_parser_error_occurred (parser);
16943 /* Remove the topmost context from the stack. */
16944 context = parser->context;
16945 parser->context = context->next;
16946 /* If no parse errors occurred, commit to the tentative parse. */
16947 if (!error_occurred)
16949 /* Commit to the tokens read tentatively, unless that was
16951 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16952 cp_lexer_commit_tokens (parser->lexer);
16954 pop_to_parent_deferring_access_checks ();
16956 /* Otherwise, if errors occurred, roll back our state so that things
16957 are just as they were before we began the tentative parse. */
16960 cp_lexer_rollback_tokens (parser->lexer);
16961 pop_deferring_access_checks ();
16963 /* Add the context to the front of the free list. */
16964 context->next = cp_parser_context_free_list;
16965 cp_parser_context_free_list = context;
16967 return !error_occurred;
16970 /* Returns true if we are parsing tentatively and are not committed to
16971 this tentative parse. */
16974 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16976 return (cp_parser_parsing_tentatively (parser)
16977 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16980 /* Returns nonzero iff an error has occurred during the most recent
16981 tentative parse. */
16984 cp_parser_error_occurred (cp_parser* parser)
16986 return (cp_parser_parsing_tentatively (parser)
16987 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16990 /* Returns nonzero if GNU extensions are allowed. */
16993 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16995 return parser->allow_gnu_extensions_p;
16998 /* Objective-C++ Productions */
17001 /* Parse an Objective-C expression, which feeds into a primary-expression
17005 objc-message-expression
17006 objc-string-literal
17007 objc-encode-expression
17008 objc-protocol-expression
17009 objc-selector-expression
17011 Returns a tree representation of the expression. */
17014 cp_parser_objc_expression (cp_parser* parser)
17016 /* Try to figure out what kind of declaration is present. */
17017 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17021 case CPP_OPEN_SQUARE:
17022 return cp_parser_objc_message_expression (parser);
17024 case CPP_OBJC_STRING:
17025 kwd = cp_lexer_consume_token (parser->lexer);
17026 return objc_build_string_object (kwd->u.value);
17029 switch (kwd->keyword)
17031 case RID_AT_ENCODE:
17032 return cp_parser_objc_encode_expression (parser);
17034 case RID_AT_PROTOCOL:
17035 return cp_parser_objc_protocol_expression (parser);
17037 case RID_AT_SELECTOR:
17038 return cp_parser_objc_selector_expression (parser);
17044 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17045 cp_parser_skip_to_end_of_block_or_statement (parser);
17048 return error_mark_node;
17051 /* Parse an Objective-C message expression.
17053 objc-message-expression:
17054 [ objc-message-receiver objc-message-args ]
17056 Returns a representation of an Objective-C message. */
17059 cp_parser_objc_message_expression (cp_parser* parser)
17061 tree receiver, messageargs;
17063 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17064 receiver = cp_parser_objc_message_receiver (parser);
17065 messageargs = cp_parser_objc_message_args (parser);
17066 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17068 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17071 /* Parse an objc-message-receiver.
17073 objc-message-receiver:
17075 simple-type-specifier
17077 Returns a representation of the type or expression. */
17080 cp_parser_objc_message_receiver (cp_parser* parser)
17084 /* An Objective-C message receiver may be either (1) a type
17085 or (2) an expression. */
17086 cp_parser_parse_tentatively (parser);
17087 rcv = cp_parser_expression (parser, false);
17089 if (cp_parser_parse_definitely (parser))
17092 rcv = cp_parser_simple_type_specifier (parser,
17093 /*decl_specs=*/NULL,
17094 CP_PARSER_FLAGS_NONE);
17096 return objc_get_class_reference (rcv);
17099 /* Parse the arguments and selectors comprising an Objective-C message.
17104 objc-selector-args , objc-comma-args
17106 objc-selector-args:
17107 objc-selector [opt] : assignment-expression
17108 objc-selector-args objc-selector [opt] : assignment-expression
17111 assignment-expression
17112 objc-comma-args , assignment-expression
17114 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17115 selector arguments and TREE_VALUE containing a list of comma
17119 cp_parser_objc_message_args (cp_parser* parser)
17121 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17122 bool maybe_unary_selector_p = true;
17123 cp_token *token = cp_lexer_peek_token (parser->lexer);
17125 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17127 tree selector = NULL_TREE, arg;
17129 if (token->type != CPP_COLON)
17130 selector = cp_parser_objc_selector (parser);
17132 /* Detect if we have a unary selector. */
17133 if (maybe_unary_selector_p
17134 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17135 return build_tree_list (selector, NULL_TREE);
17137 maybe_unary_selector_p = false;
17138 cp_parser_require (parser, CPP_COLON, "`:'");
17139 arg = cp_parser_assignment_expression (parser, false);
17142 = chainon (sel_args,
17143 build_tree_list (selector, arg));
17145 token = cp_lexer_peek_token (parser->lexer);
17148 /* Handle non-selector arguments, if any. */
17149 while (token->type == CPP_COMMA)
17153 cp_lexer_consume_token (parser->lexer);
17154 arg = cp_parser_assignment_expression (parser, false);
17157 = chainon (addl_args,
17158 build_tree_list (NULL_TREE, arg));
17160 token = cp_lexer_peek_token (parser->lexer);
17163 return build_tree_list (sel_args, addl_args);
17166 /* Parse an Objective-C encode expression.
17168 objc-encode-expression:
17169 @encode objc-typename
17171 Returns an encoded representation of the type argument. */
17174 cp_parser_objc_encode_expression (cp_parser* parser)
17178 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17179 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17180 type = complete_type (cp_parser_type_id (parser));
17181 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17185 error ("%<@encode%> must specify a type as an argument");
17186 return error_mark_node;
17189 return objc_build_encode_expr (type);
17192 /* Parse an Objective-C @defs expression. */
17195 cp_parser_objc_defs_expression (cp_parser *parser)
17199 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17200 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17201 name = cp_parser_identifier (parser);
17202 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17204 return objc_get_class_ivars (name);
17207 /* Parse an Objective-C protocol expression.
17209 objc-protocol-expression:
17210 @protocol ( identifier )
17212 Returns a representation of the protocol expression. */
17215 cp_parser_objc_protocol_expression (cp_parser* parser)
17219 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17220 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17221 proto = cp_parser_identifier (parser);
17222 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17224 return objc_build_protocol_expr (proto);
17227 /* Parse an Objective-C selector expression.
17229 objc-selector-expression:
17230 @selector ( objc-method-signature )
17232 objc-method-signature:
17238 objc-selector-seq objc-selector :
17240 Returns a representation of the method selector. */
17243 cp_parser_objc_selector_expression (cp_parser* parser)
17245 tree sel_seq = NULL_TREE;
17246 bool maybe_unary_selector_p = true;
17249 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17250 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17251 token = cp_lexer_peek_token (parser->lexer);
17253 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17254 || token->type == CPP_SCOPE)
17256 tree selector = NULL_TREE;
17258 if (token->type != CPP_COLON
17259 || token->type == CPP_SCOPE)
17260 selector = cp_parser_objc_selector (parser);
17262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17263 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17265 /* Detect if we have a unary selector. */
17266 if (maybe_unary_selector_p)
17268 sel_seq = selector;
17269 goto finish_selector;
17273 cp_parser_error (parser, "expected %<:%>");
17276 maybe_unary_selector_p = false;
17277 token = cp_lexer_consume_token (parser->lexer);
17279 if (token->type == CPP_SCOPE)
17282 = chainon (sel_seq,
17283 build_tree_list (selector, NULL_TREE));
17285 = chainon (sel_seq,
17286 build_tree_list (NULL_TREE, NULL_TREE));
17290 = chainon (sel_seq,
17291 build_tree_list (selector, NULL_TREE));
17293 token = cp_lexer_peek_token (parser->lexer);
17297 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17299 return objc_build_selector_expr (sel_seq);
17302 /* Parse a list of identifiers.
17304 objc-identifier-list:
17306 objc-identifier-list , identifier
17308 Returns a TREE_LIST of identifier nodes. */
17311 cp_parser_objc_identifier_list (cp_parser* parser)
17313 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17314 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17316 while (sep->type == CPP_COMMA)
17318 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17319 list = chainon (list,
17320 build_tree_list (NULL_TREE,
17321 cp_parser_identifier (parser)));
17322 sep = cp_lexer_peek_token (parser->lexer);
17328 /* Parse an Objective-C alias declaration.
17330 objc-alias-declaration:
17331 @compatibility_alias identifier identifier ;
17333 This function registers the alias mapping with the Objective-C front-end.
17334 It returns nothing. */
17337 cp_parser_objc_alias_declaration (cp_parser* parser)
17341 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17342 alias = cp_parser_identifier (parser);
17343 orig = cp_parser_identifier (parser);
17344 objc_declare_alias (alias, orig);
17345 cp_parser_consume_semicolon_at_end_of_statement (parser);
17348 /* Parse an Objective-C class forward-declaration.
17350 objc-class-declaration:
17351 @class objc-identifier-list ;
17353 The function registers the forward declarations with the Objective-C
17354 front-end. It returns nothing. */
17357 cp_parser_objc_class_declaration (cp_parser* parser)
17359 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17360 objc_declare_class (cp_parser_objc_identifier_list (parser));
17361 cp_parser_consume_semicolon_at_end_of_statement (parser);
17364 /* Parse a list of Objective-C protocol references.
17366 objc-protocol-refs-opt:
17367 objc-protocol-refs [opt]
17369 objc-protocol-refs:
17370 < objc-identifier-list >
17372 Returns a TREE_LIST of identifiers, if any. */
17375 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17377 tree protorefs = NULL_TREE;
17379 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17381 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17382 protorefs = cp_parser_objc_identifier_list (parser);
17383 cp_parser_require (parser, CPP_GREATER, "`>'");
17389 /* Parse a Objective-C visibility specification. */
17392 cp_parser_objc_visibility_spec (cp_parser* parser)
17394 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17396 switch (vis->keyword)
17398 case RID_AT_PRIVATE:
17399 objc_set_visibility (2);
17401 case RID_AT_PROTECTED:
17402 objc_set_visibility (0);
17404 case RID_AT_PUBLIC:
17405 objc_set_visibility (1);
17411 /* Eat '@private'/'@protected'/'@public'. */
17412 cp_lexer_consume_token (parser->lexer);
17415 /* Parse an Objective-C method type. */
17418 cp_parser_objc_method_type (cp_parser* parser)
17420 objc_set_method_type
17421 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17426 /* Parse an Objective-C protocol qualifier. */
17429 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17431 tree quals = NULL_TREE, node;
17432 cp_token *token = cp_lexer_peek_token (parser->lexer);
17434 node = token->u.value;
17436 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17437 && (node == ridpointers [(int) RID_IN]
17438 || node == ridpointers [(int) RID_OUT]
17439 || node == ridpointers [(int) RID_INOUT]
17440 || node == ridpointers [(int) RID_BYCOPY]
17441 || node == ridpointers [(int) RID_BYREF]
17442 || node == ridpointers [(int) RID_ONEWAY]))
17444 quals = tree_cons (NULL_TREE, node, quals);
17445 cp_lexer_consume_token (parser->lexer);
17446 token = cp_lexer_peek_token (parser->lexer);
17447 node = token->u.value;
17453 /* Parse an Objective-C typename. */
17456 cp_parser_objc_typename (cp_parser* parser)
17458 tree typename = NULL_TREE;
17460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17462 tree proto_quals, cp_type = NULL_TREE;
17464 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17465 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17467 /* An ObjC type name may consist of just protocol qualifiers, in which
17468 case the type shall default to 'id'. */
17469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17470 cp_type = cp_parser_type_id (parser);
17472 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17473 typename = build_tree_list (proto_quals, cp_type);
17479 /* Check to see if TYPE refers to an Objective-C selector name. */
17482 cp_parser_objc_selector_p (enum cpp_ttype type)
17484 return (type == CPP_NAME || type == CPP_KEYWORD
17485 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17486 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17487 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17488 || type == CPP_XOR || type == CPP_XOR_EQ);
17491 /* Parse an Objective-C selector. */
17494 cp_parser_objc_selector (cp_parser* parser)
17496 cp_token *token = cp_lexer_consume_token (parser->lexer);
17498 if (!cp_parser_objc_selector_p (token->type))
17500 error ("invalid Objective-C++ selector name");
17501 return error_mark_node;
17504 /* C++ operator names are allowed to appear in ObjC selectors. */
17505 switch (token->type)
17507 case CPP_AND_AND: return get_identifier ("and");
17508 case CPP_AND_EQ: return get_identifier ("and_eq");
17509 case CPP_AND: return get_identifier ("bitand");
17510 case CPP_OR: return get_identifier ("bitor");
17511 case CPP_COMPL: return get_identifier ("compl");
17512 case CPP_NOT: return get_identifier ("not");
17513 case CPP_NOT_EQ: return get_identifier ("not_eq");
17514 case CPP_OR_OR: return get_identifier ("or");
17515 case CPP_OR_EQ: return get_identifier ("or_eq");
17516 case CPP_XOR: return get_identifier ("xor");
17517 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17518 default: return token->u.value;
17522 /* Parse an Objective-C params list. */
17525 cp_parser_objc_method_keyword_params (cp_parser* parser)
17527 tree params = NULL_TREE;
17528 bool maybe_unary_selector_p = true;
17529 cp_token *token = cp_lexer_peek_token (parser->lexer);
17531 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17533 tree selector = NULL_TREE, typename, identifier;
17535 if (token->type != CPP_COLON)
17536 selector = cp_parser_objc_selector (parser);
17538 /* Detect if we have a unary selector. */
17539 if (maybe_unary_selector_p
17540 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17543 maybe_unary_selector_p = false;
17544 cp_parser_require (parser, CPP_COLON, "`:'");
17545 typename = cp_parser_objc_typename (parser);
17546 identifier = cp_parser_identifier (parser);
17550 objc_build_keyword_decl (selector,
17554 token = cp_lexer_peek_token (parser->lexer);
17560 /* Parse the non-keyword Objective-C params. */
17563 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17565 tree params = make_node (TREE_LIST);
17566 cp_token *token = cp_lexer_peek_token (parser->lexer);
17567 *ellipsisp = false; /* Initially, assume no ellipsis. */
17569 while (token->type == CPP_COMMA)
17571 cp_parameter_declarator *parmdecl;
17574 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17575 token = cp_lexer_peek_token (parser->lexer);
17577 if (token->type == CPP_ELLIPSIS)
17579 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17584 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17585 parm = grokdeclarator (parmdecl->declarator,
17586 &parmdecl->decl_specifiers,
17587 PARM, /*initialized=*/0,
17588 /*attrlist=*/NULL);
17590 chainon (params, build_tree_list (NULL_TREE, parm));
17591 token = cp_lexer_peek_token (parser->lexer);
17597 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17600 cp_parser_objc_interstitial_code (cp_parser* parser)
17602 cp_token *token = cp_lexer_peek_token (parser->lexer);
17604 /* If the next token is `extern' and the following token is a string
17605 literal, then we have a linkage specification. */
17606 if (token->keyword == RID_EXTERN
17607 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17608 cp_parser_linkage_specification (parser);
17609 /* Handle #pragma, if any. */
17610 else if (token->type == CPP_PRAGMA)
17611 cp_parser_pragma (parser, pragma_external);
17612 /* Allow stray semicolons. */
17613 else if (token->type == CPP_SEMICOLON)
17614 cp_lexer_consume_token (parser->lexer);
17615 /* Finally, try to parse a block-declaration, or a function-definition. */
17617 cp_parser_block_declaration (parser, /*statement_p=*/false);
17620 /* Parse a method signature. */
17623 cp_parser_objc_method_signature (cp_parser* parser)
17625 tree rettype, kwdparms, optparms;
17626 bool ellipsis = false;
17628 cp_parser_objc_method_type (parser);
17629 rettype = cp_parser_objc_typename (parser);
17630 kwdparms = cp_parser_objc_method_keyword_params (parser);
17631 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17633 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17636 /* Pars an Objective-C method prototype list. */
17639 cp_parser_objc_method_prototype_list (cp_parser* parser)
17641 cp_token *token = cp_lexer_peek_token (parser->lexer);
17643 while (token->keyword != RID_AT_END)
17645 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17647 objc_add_method_declaration
17648 (cp_parser_objc_method_signature (parser));
17649 cp_parser_consume_semicolon_at_end_of_statement (parser);
17652 /* Allow for interspersed non-ObjC++ code. */
17653 cp_parser_objc_interstitial_code (parser);
17655 token = cp_lexer_peek_token (parser->lexer);
17658 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17659 objc_finish_interface ();
17662 /* Parse an Objective-C method definition list. */
17665 cp_parser_objc_method_definition_list (cp_parser* parser)
17667 cp_token *token = cp_lexer_peek_token (parser->lexer);
17669 while (token->keyword != RID_AT_END)
17673 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17675 push_deferring_access_checks (dk_deferred);
17676 objc_start_method_definition
17677 (cp_parser_objc_method_signature (parser));
17679 /* For historical reasons, we accept an optional semicolon. */
17680 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17681 cp_lexer_consume_token (parser->lexer);
17683 perform_deferred_access_checks ();
17684 stop_deferring_access_checks ();
17685 meth = cp_parser_function_definition_after_declarator (parser,
17687 pop_deferring_access_checks ();
17688 objc_finish_method_definition (meth);
17691 /* Allow for interspersed non-ObjC++ code. */
17692 cp_parser_objc_interstitial_code (parser);
17694 token = cp_lexer_peek_token (parser->lexer);
17697 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17698 objc_finish_implementation ();
17701 /* Parse Objective-C ivars. */
17704 cp_parser_objc_class_ivars (cp_parser* parser)
17706 cp_token *token = cp_lexer_peek_token (parser->lexer);
17708 if (token->type != CPP_OPEN_BRACE)
17709 return; /* No ivars specified. */
17711 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17712 token = cp_lexer_peek_token (parser->lexer);
17714 while (token->type != CPP_CLOSE_BRACE)
17716 cp_decl_specifier_seq declspecs;
17717 int decl_class_or_enum_p;
17718 tree prefix_attributes;
17720 cp_parser_objc_visibility_spec (parser);
17722 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17725 cp_parser_decl_specifier_seq (parser,
17726 CP_PARSER_FLAGS_OPTIONAL,
17728 &decl_class_or_enum_p);
17729 prefix_attributes = declspecs.attributes;
17730 declspecs.attributes = NULL_TREE;
17732 /* Keep going until we hit the `;' at the end of the
17734 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17736 tree width = NULL_TREE, attributes, first_attribute, decl;
17737 cp_declarator *declarator = NULL;
17738 int ctor_dtor_or_conv_p;
17740 /* Check for a (possibly unnamed) bitfield declaration. */
17741 token = cp_lexer_peek_token (parser->lexer);
17742 if (token->type == CPP_COLON)
17745 if (token->type == CPP_NAME
17746 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17749 /* Get the name of the bitfield. */
17750 declarator = make_id_declarator (NULL_TREE,
17751 cp_parser_identifier (parser),
17755 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17756 /* Get the width of the bitfield. */
17758 = cp_parser_constant_expression (parser,
17759 /*allow_non_constant=*/false,
17764 /* Parse the declarator. */
17766 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17767 &ctor_dtor_or_conv_p,
17768 /*parenthesized_p=*/NULL,
17769 /*member_p=*/false);
17772 /* Look for attributes that apply to the ivar. */
17773 attributes = cp_parser_attributes_opt (parser);
17774 /* Remember which attributes are prefix attributes and
17776 first_attribute = attributes;
17777 /* Combine the attributes. */
17778 attributes = chainon (prefix_attributes, attributes);
17782 /* Create the bitfield declaration. */
17783 decl = grokbitfield (declarator, &declspecs, width);
17784 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17787 decl = grokfield (declarator, &declspecs,
17788 NULL_TREE, /*init_const_expr_p=*/false,
17789 NULL_TREE, attributes);
17791 /* Add the instance variable. */
17792 objc_add_instance_variable (decl);
17794 /* Reset PREFIX_ATTRIBUTES. */
17795 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17796 attributes = TREE_CHAIN (attributes);
17798 TREE_CHAIN (attributes) = NULL_TREE;
17800 token = cp_lexer_peek_token (parser->lexer);
17802 if (token->type == CPP_COMMA)
17804 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17810 cp_parser_consume_semicolon_at_end_of_statement (parser);
17811 token = cp_lexer_peek_token (parser->lexer);
17814 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17815 /* For historical reasons, we accept an optional semicolon. */
17816 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17817 cp_lexer_consume_token (parser->lexer);
17820 /* Parse an Objective-C protocol declaration. */
17823 cp_parser_objc_protocol_declaration (cp_parser* parser)
17825 tree proto, protorefs;
17828 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17831 error ("identifier expected after %<@protocol%>");
17835 /* See if we have a forward declaration or a definition. */
17836 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17838 /* Try a forward declaration first. */
17839 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17841 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17843 cp_parser_consume_semicolon_at_end_of_statement (parser);
17846 /* Ok, we got a full-fledged definition (or at least should). */
17849 proto = cp_parser_identifier (parser);
17850 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17851 objc_start_protocol (proto, protorefs);
17852 cp_parser_objc_method_prototype_list (parser);
17856 /* Parse an Objective-C superclass or category. */
17859 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17862 cp_token *next = cp_lexer_peek_token (parser->lexer);
17864 *super = *categ = NULL_TREE;
17865 if (next->type == CPP_COLON)
17867 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17868 *super = cp_parser_identifier (parser);
17870 else if (next->type == CPP_OPEN_PAREN)
17872 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17873 *categ = cp_parser_identifier (parser);
17874 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17878 /* Parse an Objective-C class interface. */
17881 cp_parser_objc_class_interface (cp_parser* parser)
17883 tree name, super, categ, protos;
17885 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17886 name = cp_parser_identifier (parser);
17887 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17888 protos = cp_parser_objc_protocol_refs_opt (parser);
17890 /* We have either a class or a category on our hands. */
17892 objc_start_category_interface (name, categ, protos);
17895 objc_start_class_interface (name, super, protos);
17896 /* Handle instance variable declarations, if any. */
17897 cp_parser_objc_class_ivars (parser);
17898 objc_continue_interface ();
17901 cp_parser_objc_method_prototype_list (parser);
17904 /* Parse an Objective-C class implementation. */
17907 cp_parser_objc_class_implementation (cp_parser* parser)
17909 tree name, super, categ;
17911 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17912 name = cp_parser_identifier (parser);
17913 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17915 /* We have either a class or a category on our hands. */
17917 objc_start_category_implementation (name, categ);
17920 objc_start_class_implementation (name, super);
17921 /* Handle instance variable declarations, if any. */
17922 cp_parser_objc_class_ivars (parser);
17923 objc_continue_implementation ();
17926 cp_parser_objc_method_definition_list (parser);
17929 /* Consume the @end token and finish off the implementation. */
17932 cp_parser_objc_end_implementation (cp_parser* parser)
17934 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17935 objc_finish_implementation ();
17938 /* Parse an Objective-C declaration. */
17941 cp_parser_objc_declaration (cp_parser* parser)
17943 /* Try to figure out what kind of declaration is present. */
17944 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17946 switch (kwd->keyword)
17949 cp_parser_objc_alias_declaration (parser);
17952 cp_parser_objc_class_declaration (parser);
17954 case RID_AT_PROTOCOL:
17955 cp_parser_objc_protocol_declaration (parser);
17957 case RID_AT_INTERFACE:
17958 cp_parser_objc_class_interface (parser);
17960 case RID_AT_IMPLEMENTATION:
17961 cp_parser_objc_class_implementation (parser);
17964 cp_parser_objc_end_implementation (parser);
17967 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17968 cp_parser_skip_to_end_of_block_or_statement (parser);
17972 /* Parse an Objective-C try-catch-finally statement.
17974 objc-try-catch-finally-stmt:
17975 @try compound-statement objc-catch-clause-seq [opt]
17976 objc-finally-clause [opt]
17978 objc-catch-clause-seq:
17979 objc-catch-clause objc-catch-clause-seq [opt]
17982 @catch ( exception-declaration ) compound-statement
17984 objc-finally-clause
17985 @finally compound-statement
17987 Returns NULL_TREE. */
17990 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17991 location_t location;
17994 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17995 location = cp_lexer_peek_token (parser->lexer)->location;
17996 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17997 node, lest it get absorbed into the surrounding block. */
17998 stmt = push_stmt_list ();
17999 cp_parser_compound_statement (parser, NULL, false);
18000 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18002 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18004 cp_parameter_declarator *parmdecl;
18007 cp_lexer_consume_token (parser->lexer);
18008 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18009 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18010 parm = grokdeclarator (parmdecl->declarator,
18011 &parmdecl->decl_specifiers,
18012 PARM, /*initialized=*/0,
18013 /*attrlist=*/NULL);
18014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18015 objc_begin_catch_clause (parm);
18016 cp_parser_compound_statement (parser, NULL, false);
18017 objc_finish_catch_clause ();
18020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18022 cp_lexer_consume_token (parser->lexer);
18023 location = cp_lexer_peek_token (parser->lexer)->location;
18024 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18025 node, lest it get absorbed into the surrounding block. */
18026 stmt = push_stmt_list ();
18027 cp_parser_compound_statement (parser, NULL, false);
18028 objc_build_finally_clause (location, pop_stmt_list (stmt));
18031 return objc_finish_try_stmt ();
18034 /* Parse an Objective-C synchronized statement.
18036 objc-synchronized-stmt:
18037 @synchronized ( expression ) compound-statement
18039 Returns NULL_TREE. */
18042 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18043 location_t location;
18046 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18048 location = cp_lexer_peek_token (parser->lexer)->location;
18049 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18050 lock = cp_parser_expression (parser, false);
18051 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18053 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18054 node, lest it get absorbed into the surrounding block. */
18055 stmt = push_stmt_list ();
18056 cp_parser_compound_statement (parser, NULL, false);
18058 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18061 /* Parse an Objective-C throw statement.
18064 @throw assignment-expression [opt] ;
18066 Returns a constructed '@throw' statement. */
18069 cp_parser_objc_throw_statement (cp_parser *parser) {
18070 tree expr = NULL_TREE;
18072 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18075 expr = cp_parser_assignment_expression (parser, false);
18077 cp_parser_consume_semicolon_at_end_of_statement (parser);
18079 return objc_build_throw_stmt (expr);
18082 /* Parse an Objective-C statement. */
18085 cp_parser_objc_statement (cp_parser * parser) {
18086 /* Try to figure out what kind of declaration is present. */
18087 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18089 switch (kwd->keyword)
18092 return cp_parser_objc_try_catch_finally_statement (parser);
18093 case RID_AT_SYNCHRONIZED:
18094 return cp_parser_objc_synchronized_statement (parser);
18096 return cp_parser_objc_throw_statement (parser);
18098 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18099 cp_parser_skip_to_end_of_block_or_statement (parser);
18102 return error_mark_node;
18105 /* OpenMP 2.5 parsing routines. */
18107 /* All OpenMP clauses. OpenMP 2.5. */
18108 typedef enum pragma_omp_clause {
18109 PRAGMA_OMP_CLAUSE_NONE = 0,
18111 PRAGMA_OMP_CLAUSE_COPYIN,
18112 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
18113 PRAGMA_OMP_CLAUSE_DEFAULT,
18114 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
18115 PRAGMA_OMP_CLAUSE_IF,
18116 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
18117 PRAGMA_OMP_CLAUSE_NOWAIT,
18118 PRAGMA_OMP_CLAUSE_NUM_THREADS,
18119 PRAGMA_OMP_CLAUSE_ORDERED,
18120 PRAGMA_OMP_CLAUSE_PRIVATE,
18121 PRAGMA_OMP_CLAUSE_REDUCTION,
18122 PRAGMA_OMP_CLAUSE_SCHEDULE,
18123 PRAGMA_OMP_CLAUSE_SHARED
18124 } pragma_omp_clause;
18126 /* Returns name of the next clause.
18127 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18128 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18129 returned and the token is consumed. */
18131 static pragma_omp_clause
18132 cp_parser_omp_clause_name (cp_parser *parser)
18134 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18137 result = PRAGMA_OMP_CLAUSE_IF;
18138 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18139 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18140 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18141 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18142 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18145 const char *p = IDENTIFIER_POINTER (id);
18150 if (!strcmp ("copyin", p))
18151 result = PRAGMA_OMP_CLAUSE_COPYIN;
18152 else if (!strcmp ("copyprivate", p))
18153 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18156 if (!strcmp ("firstprivate", p))
18157 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18160 if (!strcmp ("lastprivate", p))
18161 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18164 if (!strcmp ("nowait", p))
18165 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18166 else if (!strcmp ("num_threads", p))
18167 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18170 if (!strcmp ("ordered", p))
18171 result = PRAGMA_OMP_CLAUSE_ORDERED;
18174 if (!strcmp ("reduction", p))
18175 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18178 if (!strcmp ("schedule", p))
18179 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18180 else if (!strcmp ("shared", p))
18181 result = PRAGMA_OMP_CLAUSE_SHARED;
18186 if (result != PRAGMA_OMP_CLAUSE_NONE)
18187 cp_lexer_consume_token (parser->lexer);
18192 /* Validate that a clause of the given type does not already exist. */
18195 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18199 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18200 if (OMP_CLAUSE_CODE (c) == code)
18202 error ("too many %qs clauses", name);
18210 variable-list , identifier
18212 In addition, we match a closing parenthesis. An opening parenthesis
18213 will have been consumed by the caller.
18215 If KIND is nonzero, create the appropriate node and install the decl
18216 in OMP_CLAUSE_DECL and add the node to the head of the list.
18218 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18219 return the list created. */
18222 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18229 name = cp_parser_id_expression (parser, /*template_p=*/false,
18230 /*check_dependency_p=*/true,
18231 /*template_p=*/NULL,
18232 /*declarator_p=*/false,
18233 /*optional_p=*/false);
18234 if (name == error_mark_node)
18237 decl = cp_parser_lookup_name_simple (parser, name);
18238 if (decl == error_mark_node)
18239 cp_parser_name_lookup_error (parser, name, decl, NULL);
18240 else if (kind != 0)
18242 tree u = build_omp_clause (kind);
18243 OMP_CLAUSE_DECL (u) = decl;
18244 OMP_CLAUSE_CHAIN (u) = list;
18248 list = tree_cons (decl, NULL_TREE, list);
18251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18253 cp_lexer_consume_token (parser->lexer);
18256 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18260 /* Try to resync to an unnested comma. Copied from
18261 cp_parser_parenthesized_expression_list. */
18263 ending = cp_parser_skip_to_closing_parenthesis (parser,
18264 /*recovering=*/true,
18266 /*consume_paren=*/true);
18274 /* Similarly, but expect leading and trailing parenthesis. This is a very
18275 common case for omp clauses. */
18278 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18280 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18281 return cp_parser_omp_var_list_no_open (parser, kind, list);
18286 default ( shared | none ) */
18289 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18291 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18294 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18298 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18299 const char *p = IDENTIFIER_POINTER (id);
18304 if (strcmp ("none", p) != 0)
18306 kind = OMP_CLAUSE_DEFAULT_NONE;
18310 if (strcmp ("shared", p) != 0)
18312 kind = OMP_CLAUSE_DEFAULT_SHARED;
18319 cp_lexer_consume_token (parser->lexer);
18324 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18327 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18328 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18329 /*or_comma=*/false,
18330 /*consume_paren=*/true);
18332 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18335 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18336 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18337 OMP_CLAUSE_CHAIN (c) = list;
18338 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18344 if ( expression ) */
18347 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18354 t = cp_parser_condition (parser);
18356 if (t == error_mark_node
18357 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18359 /*or_comma=*/false,
18360 /*consume_paren=*/true);
18362 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18364 c = build_omp_clause (OMP_CLAUSE_IF);
18365 OMP_CLAUSE_IF_EXPR (c) = t;
18366 OMP_CLAUSE_CHAIN (c) = list;
18375 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18379 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18381 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18382 OMP_CLAUSE_CHAIN (c) = list;
18387 num_threads ( expression ) */
18390 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18397 t = cp_parser_expression (parser, false);
18399 if (t == error_mark_node
18400 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18402 /*or_comma=*/false,
18403 /*consume_paren=*/true);
18405 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18407 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18408 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18409 OMP_CLAUSE_CHAIN (c) = list;
18418 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18422 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18424 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18425 OMP_CLAUSE_CHAIN (c) = list;
18430 reduction ( reduction-operator : variable-list )
18432 reduction-operator:
18433 One of: + * - & ^ | && || */
18436 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18438 enum tree_code code;
18441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18444 switch (cp_lexer_peek_token (parser->lexer)->type)
18456 code = BIT_AND_EXPR;
18459 code = BIT_XOR_EXPR;
18462 code = BIT_IOR_EXPR;
18465 code = TRUTH_ANDIF_EXPR;
18468 code = TRUTH_ORIF_EXPR;
18471 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18474 /*or_comma=*/false,
18475 /*consume_paren=*/true);
18478 cp_lexer_consume_token (parser->lexer);
18480 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18483 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18484 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18485 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18491 schedule ( schedule-kind )
18492 schedule ( schedule-kind , expression )
18495 static | dynamic | guided | runtime */
18498 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18505 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18507 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18509 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18510 const char *p = IDENTIFIER_POINTER (id);
18515 if (strcmp ("dynamic", p) != 0)
18517 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18521 if (strcmp ("guided", p) != 0)
18523 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18527 if (strcmp ("runtime", p) != 0)
18529 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18536 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18537 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18540 cp_lexer_consume_token (parser->lexer);
18542 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18544 cp_lexer_consume_token (parser->lexer);
18546 t = cp_parser_assignment_expression (parser, false);
18548 if (t == error_mark_node)
18550 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18551 error ("schedule %<runtime%> does not take "
18552 "a %<chunk_size%> parameter");
18554 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18556 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18559 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18562 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18563 OMP_CLAUSE_CHAIN (c) = list;
18567 cp_parser_error (parser, "invalid schedule kind");
18569 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18570 /*or_comma=*/false,
18571 /*consume_paren=*/true);
18575 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18576 is a bitmask in MASK. Return the list of clauses found; the result
18577 of clause default goes in *pdefault. */
18580 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18581 const char *where, cp_token *pragma_tok)
18583 tree clauses = NULL;
18585 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18587 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18588 const char *c_name;
18589 tree prev = clauses;
18593 case PRAGMA_OMP_CLAUSE_COPYIN:
18594 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18597 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18598 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18600 c_name = "copyprivate";
18602 case PRAGMA_OMP_CLAUSE_DEFAULT:
18603 clauses = cp_parser_omp_clause_default (parser, clauses);
18604 c_name = "default";
18606 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18607 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18609 c_name = "firstprivate";
18611 case PRAGMA_OMP_CLAUSE_IF:
18612 clauses = cp_parser_omp_clause_if (parser, clauses);
18615 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18616 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18618 c_name = "lastprivate";
18620 case PRAGMA_OMP_CLAUSE_NOWAIT:
18621 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18624 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18625 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18626 c_name = "num_threads";
18628 case PRAGMA_OMP_CLAUSE_ORDERED:
18629 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18630 c_name = "ordered";
18632 case PRAGMA_OMP_CLAUSE_PRIVATE:
18633 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18635 c_name = "private";
18637 case PRAGMA_OMP_CLAUSE_REDUCTION:
18638 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18639 c_name = "reduction";
18641 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18642 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18643 c_name = "schedule";
18645 case PRAGMA_OMP_CLAUSE_SHARED:
18646 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18651 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18655 if (((mask >> c_kind) & 1) == 0)
18657 /* Remove the invalid clause(s) from the list to avoid
18658 confusing the rest of the compiler. */
18660 error ("%qs is not valid for %qs", c_name, where);
18664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18665 return finish_omp_clauses (clauses);
18672 In practice, we're also interested in adding the statement to an
18673 outer node. So it is convenient if we work around the fact that
18674 cp_parser_statement calls add_stmt. */
18677 cp_parser_begin_omp_structured_block (cp_parser *parser)
18679 unsigned save = parser->in_statement;
18681 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18682 This preserves the "not within loop or switch" style error messages
18683 for nonsense cases like
18689 if (parser->in_statement)
18690 parser->in_statement = IN_OMP_BLOCK;
18696 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18698 parser->in_statement = save;
18702 cp_parser_omp_structured_block (cp_parser *parser)
18704 tree stmt = begin_omp_structured_block ();
18705 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18707 cp_parser_statement (parser, NULL_TREE, false, NULL);
18709 cp_parser_end_omp_structured_block (parser, save);
18710 return finish_omp_structured_block (stmt);
18714 # pragma omp atomic new-line
18718 x binop= expr | x++ | ++x | x-- | --x
18720 +, *, -, /, &, ^, |, <<, >>
18722 where x is an lvalue expression with scalar type. */
18725 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18728 enum tree_code code;
18730 cp_parser_require_pragma_eol (parser, pragma_tok);
18732 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18734 switch (TREE_CODE (lhs))
18739 case PREINCREMENT_EXPR:
18740 case POSTINCREMENT_EXPR:
18741 lhs = TREE_OPERAND (lhs, 0);
18743 rhs = integer_one_node;
18746 case PREDECREMENT_EXPR:
18747 case POSTDECREMENT_EXPR:
18748 lhs = TREE_OPERAND (lhs, 0);
18750 rhs = integer_one_node;
18754 switch (cp_lexer_peek_token (parser->lexer)->type)
18760 code = TRUNC_DIV_EXPR;
18768 case CPP_LSHIFT_EQ:
18769 code = LSHIFT_EXPR;
18771 case CPP_RSHIFT_EQ:
18772 code = RSHIFT_EXPR;
18775 code = BIT_AND_EXPR;
18778 code = BIT_IOR_EXPR;
18781 code = BIT_XOR_EXPR;
18784 cp_parser_error (parser,
18785 "invalid operator for %<#pragma omp atomic%>");
18788 cp_lexer_consume_token (parser->lexer);
18790 rhs = cp_parser_expression (parser, false);
18791 if (rhs == error_mark_node)
18795 finish_omp_atomic (code, lhs, rhs);
18796 cp_parser_consume_semicolon_at_end_of_statement (parser);
18800 cp_parser_skip_to_end_of_block_or_statement (parser);
18805 # pragma omp barrier new-line */
18808 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18810 cp_parser_require_pragma_eol (parser, pragma_tok);
18811 finish_omp_barrier ();
18815 # pragma omp critical [(name)] new-line
18816 structured-block */
18819 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18821 tree stmt, name = NULL;
18823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18825 cp_lexer_consume_token (parser->lexer);
18827 name = cp_parser_identifier (parser);
18829 if (name == error_mark_node
18830 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18831 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18832 /*or_comma=*/false,
18833 /*consume_paren=*/true);
18834 if (name == error_mark_node)
18837 cp_parser_require_pragma_eol (parser, pragma_tok);
18839 stmt = cp_parser_omp_structured_block (parser);
18840 return c_finish_omp_critical (stmt, name);
18844 # pragma omp flush flush-vars[opt] new-line
18847 ( variable-list ) */
18850 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18853 (void) cp_parser_omp_var_list (parser, 0, NULL);
18854 cp_parser_require_pragma_eol (parser, pragma_tok);
18856 finish_omp_flush ();
18859 /* Parse the restricted form of the for statment allowed by OpenMP. */
18862 cp_parser_omp_for_loop (cp_parser *parser)
18864 tree init, cond, incr, body, decl, pre_body;
18867 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18869 cp_parser_error (parser, "for statement expected");
18872 loc = cp_lexer_consume_token (parser->lexer)->location;
18873 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18876 init = decl = NULL;
18877 pre_body = push_stmt_list ();
18878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18880 cp_decl_specifier_seq type_specifiers;
18882 /* First, try to parse as an initialized declaration. See
18883 cp_parser_condition, from whence the bulk of this is copied. */
18885 cp_parser_parse_tentatively (parser);
18886 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18888 if (!cp_parser_error_occurred (parser))
18890 tree asm_specification, attributes;
18891 cp_declarator *declarator;
18893 declarator = cp_parser_declarator (parser,
18894 CP_PARSER_DECLARATOR_NAMED,
18895 /*ctor_dtor_or_conv_p=*/NULL,
18896 /*parenthesized_p=*/NULL,
18897 /*member_p=*/false);
18898 attributes = cp_parser_attributes_opt (parser);
18899 asm_specification = cp_parser_asm_specification_opt (parser);
18901 cp_parser_require (parser, CPP_EQ, "`='");
18902 if (cp_parser_parse_definitely (parser))
18906 decl = start_decl (declarator, &type_specifiers,
18907 /*initialized_p=*/false, attributes,
18908 /*prefix_attributes=*/NULL_TREE,
18911 init = cp_parser_assignment_expression (parser, false);
18913 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18914 asm_specification, LOOKUP_ONLYCONVERTING);
18917 pop_scope (pushed_scope);
18921 cp_parser_abort_tentative_parse (parser);
18923 /* If parsing as an initialized declaration failed, try again as
18924 a simple expression. */
18926 init = cp_parser_expression (parser, false);
18928 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18929 pre_body = pop_stmt_list (pre_body);
18932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18933 cond = cp_parser_condition (parser);
18934 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18938 incr = cp_parser_expression (parser, false);
18940 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18942 /*or_comma=*/false,
18943 /*consume_paren=*/true);
18945 /* Note that we saved the original contents of this flag when we entered
18946 the structured block, and so we don't need to re-save it here. */
18947 parser->in_statement = IN_OMP_FOR;
18949 /* Note that the grammar doesn't call for a structured block here,
18950 though the loop as a whole is a structured block. */
18951 body = push_stmt_list ();
18952 cp_parser_statement (parser, NULL_TREE, false, NULL);
18953 body = pop_stmt_list (body);
18955 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18959 #pragma omp for for-clause[optseq] new-line
18962 #define OMP_FOR_CLAUSE_MASK \
18963 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18964 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18965 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18966 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18967 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18968 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18969 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18972 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18974 tree clauses, sb, ret;
18977 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18978 "#pragma omp for", pragma_tok);
18980 sb = begin_omp_structured_block ();
18981 save = cp_parser_begin_omp_structured_block (parser);
18983 ret = cp_parser_omp_for_loop (parser);
18985 OMP_FOR_CLAUSES (ret) = clauses;
18987 cp_parser_end_omp_structured_block (parser, save);
18988 add_stmt (finish_omp_structured_block (sb));
18994 # pragma omp master new-line
18995 structured-block */
18998 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19000 cp_parser_require_pragma_eol (parser, pragma_tok);
19001 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19005 # pragma omp ordered new-line
19006 structured-block */
19009 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19011 cp_parser_require_pragma_eol (parser, pragma_tok);
19012 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19018 { section-sequence }
19021 section-directive[opt] structured-block
19022 section-sequence section-directive structured-block */
19025 cp_parser_omp_sections_scope (cp_parser *parser)
19027 tree stmt, substmt;
19028 bool error_suppress = false;
19031 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19034 stmt = push_stmt_list ();
19036 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19040 substmt = begin_omp_structured_block ();
19041 save = cp_parser_begin_omp_structured_block (parser);
19045 cp_parser_statement (parser, NULL_TREE, false, NULL);
19047 tok = cp_lexer_peek_token (parser->lexer);
19048 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19050 if (tok->type == CPP_CLOSE_BRACE)
19052 if (tok->type == CPP_EOF)
19056 cp_parser_end_omp_structured_block (parser, save);
19057 substmt = finish_omp_structured_block (substmt);
19058 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19059 add_stmt (substmt);
19064 tok = cp_lexer_peek_token (parser->lexer);
19065 if (tok->type == CPP_CLOSE_BRACE)
19067 if (tok->type == CPP_EOF)
19070 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19072 cp_lexer_consume_token (parser->lexer);
19073 cp_parser_require_pragma_eol (parser, tok);
19074 error_suppress = false;
19076 else if (!error_suppress)
19078 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19079 error_suppress = true;
19082 substmt = cp_parser_omp_structured_block (parser);
19083 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19084 add_stmt (substmt);
19086 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19088 substmt = pop_stmt_list (stmt);
19090 stmt = make_node (OMP_SECTIONS);
19091 TREE_TYPE (stmt) = void_type_node;
19092 OMP_SECTIONS_BODY (stmt) = substmt;
19099 # pragma omp sections sections-clause[optseq] newline
19102 #define OMP_SECTIONS_CLAUSE_MASK \
19103 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19104 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19105 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19106 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19107 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19110 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19114 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19115 "#pragma omp sections", pragma_tok);
19117 ret = cp_parser_omp_sections_scope (parser);
19119 OMP_SECTIONS_CLAUSES (ret) = clauses;
19125 # pragma parallel parallel-clause new-line
19126 # pragma parallel for parallel-for-clause new-line
19127 # pragma parallel sections parallel-sections-clause new-line */
19129 #define OMP_PARALLEL_CLAUSE_MASK \
19130 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19131 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19132 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19133 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19134 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19135 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19136 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19137 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19140 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19142 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19143 const char *p_name = "#pragma omp parallel";
19144 tree stmt, clauses, par_clause, ws_clause, block;
19145 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19150 cp_lexer_consume_token (parser->lexer);
19151 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19152 p_name = "#pragma omp parallel for";
19153 mask |= OMP_FOR_CLAUSE_MASK;
19154 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19156 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19158 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19159 const char *p = IDENTIFIER_POINTER (id);
19160 if (strcmp (p, "sections") == 0)
19162 cp_lexer_consume_token (parser->lexer);
19163 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19164 p_name = "#pragma omp parallel sections";
19165 mask |= OMP_SECTIONS_CLAUSE_MASK;
19166 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19170 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19171 block = begin_omp_parallel ();
19172 save = cp_parser_begin_omp_structured_block (parser);
19176 case PRAGMA_OMP_PARALLEL:
19177 cp_parser_already_scoped_statement (parser);
19178 par_clause = clauses;
19181 case PRAGMA_OMP_PARALLEL_FOR:
19182 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19183 stmt = cp_parser_omp_for_loop (parser);
19185 OMP_FOR_CLAUSES (stmt) = ws_clause;
19188 case PRAGMA_OMP_PARALLEL_SECTIONS:
19189 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19190 stmt = cp_parser_omp_sections_scope (parser);
19192 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19196 gcc_unreachable ();
19199 cp_parser_end_omp_structured_block (parser, save);
19200 stmt = finish_omp_parallel (par_clause, block);
19201 if (p_kind != PRAGMA_OMP_PARALLEL)
19202 OMP_PARALLEL_COMBINED (stmt) = 1;
19207 # pragma omp single single-clause[optseq] new-line
19208 structured-block */
19210 #define OMP_SINGLE_CLAUSE_MASK \
19211 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19212 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19213 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19214 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19217 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19219 tree stmt = make_node (OMP_SINGLE);
19220 TREE_TYPE (stmt) = void_type_node;
19222 OMP_SINGLE_CLAUSES (stmt)
19223 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19224 "#pragma omp single", pragma_tok);
19225 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19227 return add_stmt (stmt);
19231 # pragma omp threadprivate (variable-list) */
19234 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19238 vars = cp_parser_omp_var_list (parser, 0, NULL);
19239 cp_parser_require_pragma_eol (parser, pragma_tok);
19241 if (!targetm.have_tls)
19242 sorry ("threadprivate variables not supported in this target");
19244 finish_omp_threadprivate (vars);
19247 /* Main entry point to OpenMP statement pragmas. */
19250 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19254 switch (pragma_tok->pragma_kind)
19256 case PRAGMA_OMP_ATOMIC:
19257 cp_parser_omp_atomic (parser, pragma_tok);
19259 case PRAGMA_OMP_CRITICAL:
19260 stmt = cp_parser_omp_critical (parser, pragma_tok);
19262 case PRAGMA_OMP_FOR:
19263 stmt = cp_parser_omp_for (parser, pragma_tok);
19265 case PRAGMA_OMP_MASTER:
19266 stmt = cp_parser_omp_master (parser, pragma_tok);
19268 case PRAGMA_OMP_ORDERED:
19269 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19271 case PRAGMA_OMP_PARALLEL:
19272 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19274 case PRAGMA_OMP_SECTIONS:
19275 stmt = cp_parser_omp_sections (parser, pragma_tok);
19277 case PRAGMA_OMP_SINGLE:
19278 stmt = cp_parser_omp_single (parser, pragma_tok);
19281 gcc_unreachable ();
19285 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19290 static GTY (()) cp_parser *the_parser;
19293 /* Special handling for the first token or line in the file. The first
19294 thing in the file might be #pragma GCC pch_preprocess, which loads a
19295 PCH file, which is a GC collection point. So we need to handle this
19296 first pragma without benefit of an existing lexer structure.
19298 Always returns one token to the caller in *FIRST_TOKEN. This is
19299 either the true first token of the file, or the first token after
19300 the initial pragma. */
19303 cp_parser_initial_pragma (cp_token *first_token)
19307 cp_lexer_get_preprocessor_token (NULL, first_token);
19308 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19311 cp_lexer_get_preprocessor_token (NULL, first_token);
19312 if (first_token->type == CPP_STRING)
19314 name = first_token->u.value;
19316 cp_lexer_get_preprocessor_token (NULL, first_token);
19317 if (first_token->type != CPP_PRAGMA_EOL)
19318 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19321 error ("expected string literal");
19323 /* Skip to the end of the pragma. */
19324 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19325 cp_lexer_get_preprocessor_token (NULL, first_token);
19327 /* Now actually load the PCH file. */
19329 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19331 /* Read one more token to return to our caller. We have to do this
19332 after reading the PCH file in, since its pointers have to be
19334 cp_lexer_get_preprocessor_token (NULL, first_token);
19337 /* Normal parsing of a pragma token. Here we can (and must) use the
19341 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19343 cp_token *pragma_tok;
19346 pragma_tok = cp_lexer_consume_token (parser->lexer);
19347 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19348 parser->lexer->in_pragma = true;
19350 id = pragma_tok->pragma_kind;
19353 case PRAGMA_GCC_PCH_PREPROCESS:
19354 error ("%<#pragma GCC pch_preprocess%> must be first");
19357 case PRAGMA_OMP_BARRIER:
19360 case pragma_compound:
19361 cp_parser_omp_barrier (parser, pragma_tok);
19364 error ("%<#pragma omp barrier%> may only be "
19365 "used in compound statements");
19372 case PRAGMA_OMP_FLUSH:
19375 case pragma_compound:
19376 cp_parser_omp_flush (parser, pragma_tok);
19379 error ("%<#pragma omp flush%> may only be "
19380 "used in compound statements");
19387 case PRAGMA_OMP_THREADPRIVATE:
19388 cp_parser_omp_threadprivate (parser, pragma_tok);
19391 case PRAGMA_OMP_ATOMIC:
19392 case PRAGMA_OMP_CRITICAL:
19393 case PRAGMA_OMP_FOR:
19394 case PRAGMA_OMP_MASTER:
19395 case PRAGMA_OMP_ORDERED:
19396 case PRAGMA_OMP_PARALLEL:
19397 case PRAGMA_OMP_SECTIONS:
19398 case PRAGMA_OMP_SINGLE:
19399 if (context == pragma_external)
19401 cp_parser_omp_construct (parser, pragma_tok);
19404 case PRAGMA_OMP_SECTION:
19405 error ("%<#pragma omp section%> may only be used in "
19406 "%<#pragma omp sections%> construct");
19410 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19411 c_invoke_pragma_handler (id);
19415 cp_parser_error (parser, "expected declaration specifiers");
19419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19423 /* The interface the pragma parsers have to the lexer. */
19426 pragma_lex (tree *value)
19429 enum cpp_ttype ret;
19431 tok = cp_lexer_peek_token (the_parser->lexer);
19434 *value = tok->u.value;
19436 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19438 else if (ret == CPP_STRING)
19439 *value = cp_parser_string_literal (the_parser, false, false);
19442 cp_lexer_consume_token (the_parser->lexer);
19443 if (ret == CPP_KEYWORD)
19451 /* External interface. */
19453 /* Parse one entire translation unit. */
19456 c_parse_file (void)
19458 bool error_occurred;
19459 static bool already_called = false;
19461 if (already_called)
19463 sorry ("inter-module optimizations not implemented for C++");
19466 already_called = true;
19468 the_parser = cp_parser_new ();
19469 push_deferring_access_checks (flag_access_control
19470 ? dk_no_deferred : dk_no_check);
19471 error_occurred = cp_parser_translation_unit (the_parser);
19475 /* This variable must be provided by every front end. */
19479 #include "gt-cp-parser.h"