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"
41 /* APPLE LOCAL C* language */
42 #include "tree-iterator.h"
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 /* A token's value and its associated deferred access checks and
53 struct tree_check GTY(())
55 /* The value associated with the token. */
57 /* The checks that have been associated with value. */
58 VEC (deferred_access_check, gc)* checks;
59 /* The token's qualifying scope (used when it is a
60 CPP_NESTED_NAME_SPECIFIER). */
61 tree qualifying_scope;
66 typedef struct cp_token GTY (())
68 /* The kind of token. */
69 ENUM_BITFIELD (cpp_ttype) type : 8;
70 /* If this token is a keyword, this value indicates which keyword.
71 Otherwise, this value is RID_MAX. */
72 ENUM_BITFIELD (rid) keyword : 8;
75 /* Identifier for the pragma. */
76 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
77 /* True if this token is from a system header. */
78 BOOL_BITFIELD in_system_header : 1;
79 /* True if this token is from a context where it is implicitly extern "C" */
80 BOOL_BITFIELD implicit_extern_c : 1;
81 /* True for a CPP_NAME token that is not a keyword (i.e., for which
82 KEYWORD is RID_MAX) iff this name was looked up and found to be
83 ambiguous. An error has already been reported. */
84 BOOL_BITFIELD ambiguous_p : 1;
85 /* The input file stack index at which this token was found. */
86 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
87 /* The value associated with this token, if any. */
88 union cp_token_value {
89 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
90 struct tree_check* GTY((tag ("1"))) tree_check_value;
91 /* Use for all other tokens. */
92 tree GTY((tag ("0"))) value;
93 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
94 /* The location at which this token was found. */
98 /* We use a stack of token pointer for saving token sets. */
99 typedef struct cp_token *cp_token_position;
100 DEF_VEC_P (cp_token_position);
101 DEF_VEC_ALLOC_P (cp_token_position,heap);
103 static const cp_token eof_token =
105 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
106 #if USE_MAPPED_LOCATION
113 /* The cp_lexer structure represents the C++ lexer. It is responsible
114 for managing the token stream from the preprocessor and supplying
115 it to the parser. Tokens are never added to the cp_lexer after
118 typedef struct cp_lexer GTY (())
120 /* The memory allocated for the buffer. NULL if this lexer does not
121 own the token buffer. */
122 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
123 /* If the lexer owns the buffer, this is the number of tokens in the
125 size_t buffer_length;
127 /* A pointer just past the last available token. The tokens
128 in this lexer are [buffer, last_token). */
129 cp_token_position GTY ((skip)) last_token;
131 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
132 no more available tokens. */
133 cp_token_position GTY ((skip)) next_token;
135 /* A stack indicating positions at which cp_lexer_save_tokens was
136 called. The top entry is the most recent position at which we
137 began saving tokens. If the stack is non-empty, we are saving
139 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
141 /* The next lexer in a linked list of lexers. */
142 struct cp_lexer *next;
144 /* True if we should output debugging information. */
147 /* True if we're in the context of parsing a pragma, and should not
148 increment past the end-of-line marker. */
152 /* cp_token_cache is a range of tokens. There is no need to represent
153 allocate heap memory for it, since tokens are never removed from the
154 lexer's array. There is also no need for the GC to walk through
155 a cp_token_cache, since everything in here is referenced through
158 typedef struct cp_token_cache GTY(())
160 /* The beginning of the token range. */
161 cp_token * GTY((skip)) first;
163 /* Points immediately after the last token in the range. */
164 cp_token * GTY ((skip)) last;
169 static cp_lexer *cp_lexer_new_main
171 static cp_lexer *cp_lexer_new_from_tokens
172 (cp_token_cache *tokens);
173 static void cp_lexer_destroy
175 static int cp_lexer_saving_tokens
177 static cp_token_position cp_lexer_token_position
179 static cp_token *cp_lexer_token_at
180 (cp_lexer *, cp_token_position);
181 static void cp_lexer_get_preprocessor_token
182 (cp_lexer *, cp_token *);
183 static inline cp_token *cp_lexer_peek_token
185 static cp_token *cp_lexer_peek_nth_token
186 (cp_lexer *, size_t);
187 static inline bool cp_lexer_next_token_is
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_not
190 (cp_lexer *, enum cpp_ttype);
191 static bool cp_lexer_next_token_is_keyword
192 (cp_lexer *, enum rid);
193 static cp_token *cp_lexer_consume_token
195 static void cp_lexer_purge_token
197 static void cp_lexer_purge_tokens_after
198 (cp_lexer *, cp_token_position);
199 static void cp_lexer_save_tokens
201 static void cp_lexer_commit_tokens
203 static void cp_lexer_rollback_tokens
205 #ifdef ENABLE_CHECKING
206 static void cp_lexer_print_token
207 (FILE *, cp_token *);
208 static inline bool cp_lexer_debugging_p
210 static void cp_lexer_start_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 static void cp_lexer_stop_debugging
213 (cp_lexer *) ATTRIBUTE_UNUSED;
215 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
216 about passing NULL to functions that require non-NULL arguments
217 (fputs, fprintf). It will never be used, so all we need is a value
218 of the right type that's guaranteed not to be NULL. */
219 #define cp_lexer_debug_stream stdout
220 #define cp_lexer_print_token(str, tok) (void) 0
221 #define cp_lexer_debugging_p(lexer) 0
222 #endif /* ENABLE_CHECKING */
224 static cp_token_cache *cp_token_cache_new
225 (cp_token *, cp_token *);
227 static void cp_parser_initial_pragma
230 /* Manifest constants. */
231 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
232 #define CP_SAVED_TOKEN_STACK 5
234 /* A token type for keywords, as opposed to ordinary identifiers. */
235 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
237 /* A token type for template-ids. If a template-id is processed while
238 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
239 the value of the CPP_TEMPLATE_ID is whatever was returned by
240 cp_parser_template_id. */
241 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
243 /* A token type for nested-name-specifiers. If a
244 nested-name-specifier is processed while parsing tentatively, it is
245 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
246 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
247 cp_parser_nested_name_specifier_opt. */
248 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
250 /* A token type for tokens that are not tokens at all; these are used
251 to represent slots in the array where there used to be a token
252 that has now been deleted. */
253 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
255 /* The number of token types, including C++-specific ones. */
256 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
260 #ifdef ENABLE_CHECKING
261 /* The stream to which debugging output should be written. */
262 static FILE *cp_lexer_debug_stream;
263 #endif /* ENABLE_CHECKING */
265 /* Create a new main C++ lexer, the lexer that gets tokens from the
269 cp_lexer_new_main (void)
271 cp_token first_token;
278 /* It's possible that parsing the first pragma will load a PCH file,
279 which is a GC collection point. So we have to do that before
280 allocating any memory. */
281 cp_parser_initial_pragma (&first_token);
283 /* Tell c_lex_with_flags not to merge string constants. */
284 c_lex_return_raw_strings = true;
286 c_common_no_more_pch ();
288 /* Allocate the memory. */
289 lexer = GGC_CNEW (cp_lexer);
291 #ifdef ENABLE_CHECKING
292 /* Initially we are not debugging. */
293 lexer->debugging_p = false;
294 #endif /* ENABLE_CHECKING */
295 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
296 CP_SAVED_TOKEN_STACK);
298 /* Create the buffer. */
299 alloc = CP_LEXER_BUFFER_SIZE;
300 buffer = GGC_NEWVEC (cp_token, alloc);
302 /* Put the first token in the buffer. */
307 /* Get the remaining tokens from the preprocessor. */
308 while (pos->type != CPP_EOF)
315 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
316 pos = buffer + space;
318 cp_lexer_get_preprocessor_token (lexer, pos);
320 lexer->buffer = buffer;
321 lexer->buffer_length = alloc - space;
322 lexer->last_token = pos;
323 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
325 /* Subsequent preprocessor diagnostics should use compiler
326 diagnostic functions to get the compiler source location. */
327 cpp_get_options (parse_in)->client_diagnostic = true;
328 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
330 gcc_assert (lexer->next_token->type != CPP_PURGED);
334 /* Create a new lexer whose token stream is primed with the tokens in
335 CACHE. When these tokens are exhausted, no new tokens will be read. */
338 cp_lexer_new_from_tokens (cp_token_cache *cache)
340 cp_token *first = cache->first;
341 cp_token *last = cache->last;
342 cp_lexer *lexer = GGC_CNEW (cp_lexer);
344 /* We do not own the buffer. */
345 lexer->buffer = NULL;
346 lexer->buffer_length = 0;
347 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
348 lexer->last_token = last;
350 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
351 CP_SAVED_TOKEN_STACK);
353 #ifdef ENABLE_CHECKING
354 /* Initially we are not debugging. */
355 lexer->debugging_p = false;
358 gcc_assert (lexer->next_token->type != CPP_PURGED);
362 /* Frees all resources associated with LEXER. */
365 cp_lexer_destroy (cp_lexer *lexer)
368 ggc_free (lexer->buffer);
369 VEC_free (cp_token_position, heap, lexer->saved_tokens);
373 /* Returns nonzero if debugging information should be output. */
375 #ifdef ENABLE_CHECKING
378 cp_lexer_debugging_p (cp_lexer *lexer)
380 return lexer->debugging_p;
383 #endif /* ENABLE_CHECKING */
385 static inline cp_token_position
386 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
388 gcc_assert (!previous_p || lexer->next_token != &eof_token);
390 return lexer->next_token - previous_p;
393 static inline cp_token *
394 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
399 /* nonzero if we are presently saving tokens. */
402 cp_lexer_saving_tokens (const cp_lexer* lexer)
404 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
407 /* Store the next token from the preprocessor in *TOKEN. Return true
411 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
414 static int is_extern_c = 0;
416 /* Get a new token from the preprocessor. */
418 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
419 token->input_file_stack_index = input_file_stack_tick;
420 token->keyword = RID_MAX;
421 token->pragma_kind = PRAGMA_NONE;
422 token->in_system_header = in_system_header;
424 /* On some systems, some header files are surrounded by an
425 implicit extern "C" block. Set a flag in the token if it
426 comes from such a header. */
427 is_extern_c += pending_lang_change;
428 pending_lang_change = 0;
429 token->implicit_extern_c = is_extern_c > 0;
431 /* Check to see if this token is a keyword. */
432 if (token->type == CPP_NAME)
434 if (C_IS_RESERVED_WORD (token->u.value))
436 /* Mark this token as a keyword. */
437 token->type = CPP_KEYWORD;
438 /* Record which keyword. */
439 token->keyword = C_RID_CODE (token->u.value);
440 /* Update the value. Some keywords are mapped to particular
441 entities, rather than simply having the value of the
442 corresponding IDENTIFIER_NODE. For example, `__const' is
443 mapped to `const'. */
444 token->u.value = ridpointers[token->keyword];
448 token->ambiguous_p = false;
449 token->keyword = RID_MAX;
452 /* Handle Objective-C++ keywords. */
453 else if (token->type == CPP_AT_NAME)
455 token->type = CPP_KEYWORD;
456 switch (C_RID_CODE (token->u.value))
458 /* Map 'class' to '@class', 'private' to '@private', etc. */
459 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
460 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
461 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
462 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
463 case RID_THROW: token->keyword = RID_AT_THROW; break;
464 case RID_TRY: token->keyword = RID_AT_TRY; break;
465 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
466 default: token->keyword = C_RID_CODE (token->u.value);
469 else if (token->type == CPP_PRAGMA)
471 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
472 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
473 token->u.value = NULL_TREE;
477 /* Update the globals input_location and in_system_header and the
478 input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
485 in_system_header = token->in_system_header;
486 restore_input_file_stack (token->input_file_stack_index);
490 /* Return a pointer to the next token in the token stream, but do not
493 static inline cp_token *
494 cp_lexer_peek_token (cp_lexer *lexer)
496 if (cp_lexer_debugging_p (lexer))
498 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
499 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
500 putc ('\n', cp_lexer_debug_stream);
502 return lexer->next_token;
505 /* Return true if the next token has the indicated TYPE. */
508 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
510 return cp_lexer_peek_token (lexer)->type == type;
513 /* Return true if the next token does not have the indicated TYPE. */
516 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
518 return !cp_lexer_next_token_is (lexer, type);
521 /* Return true if the next token is the indicated KEYWORD. */
524 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
526 return cp_lexer_peek_token (lexer)->keyword == keyword;
529 /* Return true if the next token is a keyword for a decl-specifier. */
532 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 token = cp_lexer_peek_token (lexer);
537 switch (token->keyword)
539 /* Storage classes. */
546 /* Elaborated type specifiers. */
552 /* Simple type specifiers. */
564 /* GNU extensions. */
574 /* Return a pointer to the Nth token in the token stream. If N is 1,
575 then this is precisely equivalent to cp_lexer_peek_token (except
576 that it is not inline). One would like to disallow that case, but
577 there is one case (cp_parser_nth_token_starts_template_id) where
578 the caller passes a variable for N and it might be 1. */
581 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
585 /* N is 1-based, not zero-based. */
588 if (cp_lexer_debugging_p (lexer))
589 fprintf (cp_lexer_debug_stream,
590 "cp_lexer: peeking ahead %ld at token: ", (long)n);
593 token = lexer->next_token;
594 gcc_assert (!n || token != &eof_token);
598 if (token == lexer->last_token)
600 token = (cp_token *)&eof_token;
604 if (token->type != CPP_PURGED)
608 if (cp_lexer_debugging_p (lexer))
610 cp_lexer_print_token (cp_lexer_debug_stream, token);
611 putc ('\n', cp_lexer_debug_stream);
617 /* Return the next token, and advance the lexer's next_token pointer
618 to point to the next non-purged token. */
621 cp_lexer_consume_token (cp_lexer* lexer)
623 cp_token *token = lexer->next_token;
625 gcc_assert (token != &eof_token);
626 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
631 if (lexer->next_token == lexer->last_token)
633 lexer->next_token = (cp_token *)&eof_token;
638 while (lexer->next_token->type == CPP_PURGED);
640 cp_lexer_set_source_position_from_token (token);
642 /* Provide debugging output. */
643 if (cp_lexer_debugging_p (lexer))
645 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
646 cp_lexer_print_token (cp_lexer_debug_stream, token);
647 putc ('\n', cp_lexer_debug_stream);
653 /* Permanently remove the next token from the token stream, and
654 advance the next_token pointer to refer to the next non-purged
658 cp_lexer_purge_token (cp_lexer *lexer)
660 cp_token *tok = lexer->next_token;
662 gcc_assert (tok != &eof_token);
663 tok->type = CPP_PURGED;
664 tok->location = UNKNOWN_LOCATION;
665 tok->u.value = NULL_TREE;
666 tok->keyword = RID_MAX;
671 if (tok == lexer->last_token)
673 tok = (cp_token *)&eof_token;
677 while (tok->type == CPP_PURGED);
678 lexer->next_token = tok;
681 /* Permanently remove all tokens after TOK, up to, but not
682 including, the token that will be returned next by
683 cp_lexer_peek_token. */
686 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
688 cp_token *peek = lexer->next_token;
690 if (peek == &eof_token)
691 peek = lexer->last_token;
693 gcc_assert (tok < peek);
695 for ( tok += 1; tok != peek; tok += 1)
697 tok->type = CPP_PURGED;
698 tok->location = UNKNOWN_LOCATION;
699 tok->u.value = NULL_TREE;
700 tok->keyword = RID_MAX;
704 /* Begin saving tokens. All tokens consumed after this point will be
708 cp_lexer_save_tokens (cp_lexer* lexer)
710 /* Provide debugging output. */
711 if (cp_lexer_debugging_p (lexer))
712 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
714 VEC_safe_push (cp_token_position, heap,
715 lexer->saved_tokens, lexer->next_token);
718 /* Commit to the portion of the token stream most recently saved. */
721 cp_lexer_commit_tokens (cp_lexer* lexer)
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
727 VEC_pop (cp_token_position, lexer->saved_tokens);
730 /* Return all tokens saved since the last call to cp_lexer_save_tokens
731 to the token stream. Stop saving tokens. */
734 cp_lexer_rollback_tokens (cp_lexer* lexer)
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
740 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
743 /* Print a representation of the TOKEN on the STREAM. */
745 #ifdef ENABLE_CHECKING
748 cp_lexer_print_token (FILE * stream, cp_token *token)
750 /* We don't use cpp_type2name here because the parser defines
751 a few tokens of its own. */
752 static const char *const token_names[] = {
753 /* cpplib-defined token types */
759 /* C++ parser token types - see "Manifest constants", above. */
762 "NESTED_NAME_SPECIFIER",
766 /* If we have a name for the token, print it out. Otherwise, we
767 simply give the numeric code. */
768 gcc_assert (token->type < ARRAY_SIZE(token_names));
769 fputs (token_names[token->type], stream);
771 /* For some tokens, print the associated data. */
775 /* Some keywords have a value that is not an IDENTIFIER_NODE.
776 For example, `struct' is mapped to an INTEGER_CST. */
777 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
779 /* else fall through */
781 fputs (IDENTIFIER_POINTER (token->u.value), stream);
786 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
794 /* Start emitting debugging information. */
797 cp_lexer_start_debugging (cp_lexer* lexer)
799 lexer->debugging_p = true;
802 /* Stop emitting debugging information. */
805 cp_lexer_stop_debugging (cp_lexer* lexer)
807 lexer->debugging_p = false;
810 #endif /* ENABLE_CHECKING */
812 /* Create a new cp_token_cache, representing a range of tokens. */
814 static cp_token_cache *
815 cp_token_cache_new (cp_token *first, cp_token *last)
817 cp_token_cache *cache = GGC_NEW (cp_token_cache);
818 cache->first = first;
824 /* Decl-specifiers. */
826 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
829 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
831 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
836 /* Nothing other than the parser should be creating declarators;
837 declarators are a semi-syntactic representation of C++ entities.
838 Other parts of the front end that need to create entities (like
839 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
841 static cp_declarator *make_call_declarator
842 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
843 static cp_declarator *make_array_declarator
844 (cp_declarator *, tree);
845 static cp_declarator *make_pointer_declarator
846 (cp_cv_quals, cp_declarator *);
847 static cp_declarator *make_reference_declarator
848 (cp_cv_quals, cp_declarator *);
849 static cp_parameter_declarator *make_parameter_declarator
850 (cp_decl_specifier_seq *, cp_declarator *, tree);
851 static cp_declarator *make_ptrmem_declarator
852 (cp_cv_quals, tree, cp_declarator *);
854 /* An erroneous declarator. */
855 static cp_declarator *cp_error_declarator;
857 /* The obstack on which declarators and related data structures are
859 static struct obstack declarator_obstack;
861 /* Alloc BYTES from the declarator memory pool. */
864 alloc_declarator (size_t bytes)
866 return obstack_alloc (&declarator_obstack, bytes);
869 /* Allocate a declarator of the indicated KIND. Clear fields that are
870 common to all declarators. */
872 static cp_declarator *
873 make_declarator (cp_declarator_kind kind)
875 cp_declarator *declarator;
877 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
878 declarator->kind = kind;
879 declarator->attributes = NULL_TREE;
880 declarator->declarator = NULL;
885 /* Make a declarator for a generalized identifier. If
886 QUALIFYING_SCOPE is non-NULL, the identifier is
887 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
888 UNQUALIFIED_NAME. SFK indicates the kind of special function this
891 static cp_declarator *
892 make_id_declarator (tree qualifying_scope, tree unqualified_name,
893 special_function_kind sfk)
895 cp_declarator *declarator;
897 /* It is valid to write:
899 class C { void f(); };
903 The standard is not clear about whether `typedef const C D' is
904 legal; as of 2002-09-15 the committee is considering that
905 question. EDG 3.0 allows that syntax. Therefore, we do as
907 if (qualifying_scope && TYPE_P (qualifying_scope))
908 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
910 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
911 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
912 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
914 declarator = make_declarator (cdk_id);
915 declarator->u.id.qualifying_scope = qualifying_scope;
916 declarator->u.id.unqualified_name = unqualified_name;
917 declarator->u.id.sfk = sfk;
922 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
923 of modifiers such as const or volatile to apply to the pointer
924 type, represented as identifiers. */
927 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
929 cp_declarator *declarator;
931 declarator = make_declarator (cdk_pointer);
932 declarator->declarator = target;
933 declarator->u.pointer.qualifiers = cv_qualifiers;
934 declarator->u.pointer.class_type = NULL_TREE;
939 /* Like make_pointer_declarator -- but for references. */
942 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_reference);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
954 /* Like make_pointer_declarator -- but for a pointer to a non-static
955 member of CLASS_TYPE. */
958 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
959 cp_declarator *pointee)
961 cp_declarator *declarator;
963 declarator = make_declarator (cdk_ptrmem);
964 declarator->declarator = pointee;
965 declarator->u.pointer.qualifiers = cv_qualifiers;
966 declarator->u.pointer.class_type = class_type;
971 /* Make a declarator for the function given by TARGET, with the
972 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
973 "const"-qualified member function. The EXCEPTION_SPECIFICATION
974 indicates what exceptions can be thrown. */
977 make_call_declarator (cp_declarator *target,
978 cp_parameter_declarator *parms,
979 cp_cv_quals cv_qualifiers,
980 tree exception_specification)
982 cp_declarator *declarator;
984 declarator = make_declarator (cdk_function);
985 declarator->declarator = target;
986 declarator->u.function.parameters = parms;
987 declarator->u.function.qualifiers = cv_qualifiers;
988 declarator->u.function.exception_specification = exception_specification;
993 /* Make a declarator for an array of BOUNDS elements, each of which is
994 defined by ELEMENT. */
997 make_array_declarator (cp_declarator *element, tree bounds)
999 cp_declarator *declarator;
1001 declarator = make_declarator (cdk_array);
1002 declarator->declarator = element;
1003 declarator->u.array.bounds = bounds;
1008 cp_parameter_declarator *no_parameters;
1010 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1011 DECLARATOR and DEFAULT_ARGUMENT. */
1013 cp_parameter_declarator *
1014 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1015 cp_declarator *declarator,
1016 tree default_argument)
1018 cp_parameter_declarator *parameter;
1020 parameter = ((cp_parameter_declarator *)
1021 alloc_declarator (sizeof (cp_parameter_declarator)));
1022 parameter->next = NULL;
1023 if (decl_specifiers)
1024 parameter->decl_specifiers = *decl_specifiers;
1026 clear_decl_specs (¶meter->decl_specifiers);
1027 parameter->declarator = declarator;
1028 parameter->default_argument = default_argument;
1029 parameter->ellipsis_p = false;
1034 /* Returns true iff DECLARATOR is a declaration for a function. */
1037 function_declarator_p (const cp_declarator *declarator)
1041 if (declarator->kind == cdk_function
1042 && declarator->declarator->kind == cdk_id)
1044 if (declarator->kind == cdk_id
1045 || declarator->kind == cdk_error)
1047 declarator = declarator->declarator;
1057 A cp_parser parses the token stream as specified by the C++
1058 grammar. Its job is purely parsing, not semantic analysis. For
1059 example, the parser breaks the token stream into declarators,
1060 expressions, statements, and other similar syntactic constructs.
1061 It does not check that the types of the expressions on either side
1062 of an assignment-statement are compatible, or that a function is
1063 not declared with a parameter of type `void'.
1065 The parser invokes routines elsewhere in the compiler to perform
1066 semantic analysis and to build up the abstract syntax tree for the
1069 The parser (and the template instantiation code, which is, in a
1070 way, a close relative of parsing) are the only parts of the
1071 compiler that should be calling push_scope and pop_scope, or
1072 related functions. The parser (and template instantiation code)
1073 keeps track of what scope is presently active; everything else
1074 should simply honor that. (The code that generates static
1075 initializers may also need to set the scope, in order to check
1076 access control correctly when emitting the initializers.)
1081 The parser is of the standard recursive-descent variety. Upcoming
1082 tokens in the token stream are examined in order to determine which
1083 production to use when parsing a non-terminal. Some C++ constructs
1084 require arbitrary look ahead to disambiguate. For example, it is
1085 impossible, in the general case, to tell whether a statement is an
1086 expression or declaration without scanning the entire statement.
1087 Therefore, the parser is capable of "parsing tentatively." When the
1088 parser is not sure what construct comes next, it enters this mode.
1089 Then, while we attempt to parse the construct, the parser queues up
1090 error messages, rather than issuing them immediately, and saves the
1091 tokens it consumes. If the construct is parsed successfully, the
1092 parser "commits", i.e., it issues any queued error messages and
1093 the tokens that were being preserved are permanently discarded.
1094 If, however, the construct is not parsed successfully, the parser
1095 rolls back its state completely so that it can resume parsing using
1096 a different alternative.
1101 The performance of the parser could probably be improved substantially.
1102 We could often eliminate the need to parse tentatively by looking ahead
1103 a little bit. In some places, this approach might not entirely eliminate
1104 the need to parse tentatively, but it might still speed up the average
1107 /* Flags that are passed to some parsing functions. These values can
1108 be bitwise-ored together. */
1110 typedef enum cp_parser_flags
1113 CP_PARSER_FLAGS_NONE = 0x0,
1114 /* The construct is optional. If it is not present, then no error
1115 should be issued. */
1116 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1117 /* When parsing a type-specifier, do not allow user-defined types. */
1118 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1121 /* The different kinds of declarators we want to parse. */
1123 typedef enum cp_parser_declarator_kind
1125 /* APPLE LOCAL begin blocks 6339747 */
1126 /* We want a block declarator. */
1127 CP_PARSER_DECLARATOR_BLOCK,
1128 /* APPLE LOCAL end blocks 6339747 */
1129 /* We want an abstract declarator. */
1130 CP_PARSER_DECLARATOR_ABSTRACT,
1131 /* We want a named declarator. */
1132 CP_PARSER_DECLARATOR_NAMED,
1133 /* We don't mind, but the name must be an unqualified-id. */
1134 CP_PARSER_DECLARATOR_EITHER
1135 } cp_parser_declarator_kind;
1137 /* The precedence values used to parse binary expressions. The minimum value
1138 of PREC must be 1, because zero is reserved to quickly discriminate
1139 binary operators from other tokens. */
1144 PREC_LOGICAL_OR_EXPRESSION,
1145 PREC_LOGICAL_AND_EXPRESSION,
1146 PREC_INCLUSIVE_OR_EXPRESSION,
1147 PREC_EXCLUSIVE_OR_EXPRESSION,
1148 PREC_AND_EXPRESSION,
1149 PREC_EQUALITY_EXPRESSION,
1150 PREC_RELATIONAL_EXPRESSION,
1151 PREC_SHIFT_EXPRESSION,
1152 PREC_ADDITIVE_EXPRESSION,
1153 PREC_MULTIPLICATIVE_EXPRESSION,
1155 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1158 /* A mapping from a token type to a corresponding tree node type, with a
1159 precedence value. */
1161 typedef struct cp_parser_binary_operations_map_node
1163 /* The token type. */
1164 enum cpp_ttype token_type;
1165 /* The corresponding tree code. */
1166 enum tree_code tree_type;
1167 /* The precedence of this operator. */
1168 enum cp_parser_prec prec;
1169 } cp_parser_binary_operations_map_node;
1171 /* The status of a tentative parse. */
1173 typedef enum cp_parser_status_kind
1175 /* No errors have occurred. */
1176 CP_PARSER_STATUS_KIND_NO_ERROR,
1177 /* An error has occurred. */
1178 CP_PARSER_STATUS_KIND_ERROR,
1179 /* We are committed to this tentative parse, whether or not an error
1181 CP_PARSER_STATUS_KIND_COMMITTED
1182 } cp_parser_status_kind;
1184 typedef struct cp_parser_expression_stack_entry
1186 /* Left hand side of the binary operation we are currently
1189 /* Original tree code for left hand side, if it was a binary
1190 expression itself (used for -Wparentheses). */
1191 enum tree_code lhs_type;
1192 /* Tree code for the binary operation we are parsing. */
1193 enum tree_code tree_type;
1194 /* Precedence of the binary operation we are parsing. */
1196 } cp_parser_expression_stack_entry;
1198 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1199 entries because precedence levels on the stack are monotonically
1201 typedef struct cp_parser_expression_stack_entry
1202 cp_parser_expression_stack[NUM_PREC_VALUES];
1204 /* Context that is saved and restored when parsing tentatively. */
1205 typedef struct cp_parser_context GTY (())
1207 /* If this is a tentative parsing context, the status of the
1209 enum cp_parser_status_kind status;
1210 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1211 that are looked up in this context must be looked up both in the
1212 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1213 the context of the containing expression. */
1216 /* The next parsing context in the stack. */
1217 struct cp_parser_context *next;
1218 } cp_parser_context;
1222 /* Constructors and destructors. */
1224 static cp_parser_context *cp_parser_context_new
1225 (cp_parser_context *);
1227 /* Class variables. */
1229 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1231 /* The operator-precedence table used by cp_parser_binary_expression.
1232 Transformed into an associative array (binops_by_token) by
1235 static const cp_parser_binary_operations_map_node binops[] = {
1236 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1237 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1239 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1240 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1241 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1243 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1244 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1246 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1247 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1249 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1250 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1251 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1252 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1254 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1255 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1257 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1259 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1261 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1263 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1265 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1268 /* The same as binops, but initialized by cp_parser_new so that
1269 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1271 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1273 /* Constructors and destructors. */
1275 /* Construct a new context. The context below this one on the stack
1276 is given by NEXT. */
1278 static cp_parser_context *
1279 cp_parser_context_new (cp_parser_context* next)
1281 cp_parser_context *context;
1283 /* Allocate the storage. */
1284 if (cp_parser_context_free_list != NULL)
1286 /* Pull the first entry from the free list. */
1287 context = cp_parser_context_free_list;
1288 cp_parser_context_free_list = context->next;
1289 memset (context, 0, sizeof (*context));
1292 context = GGC_CNEW (cp_parser_context);
1294 /* No errors have occurred yet in this context. */
1295 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1296 /* If this is not the bottomost context, copy information that we
1297 need from the previous context. */
1300 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1301 expression, then we are parsing one in this context, too. */
1302 context->object_type = next->object_type;
1303 /* Thread the stack. */
1304 context->next = next;
1310 /* The cp_parser structure represents the C++ parser. */
1312 typedef struct cp_parser GTY(())
1314 /* The lexer from which we are obtaining tokens. */
1317 /* The scope in which names should be looked up. If NULL_TREE, then
1318 we look up names in the scope that is currently open in the
1319 source program. If non-NULL, this is either a TYPE or
1320 NAMESPACE_DECL for the scope in which we should look. It can
1321 also be ERROR_MARK, when we've parsed a bogus scope.
1323 This value is not cleared automatically after a name is looked
1324 up, so we must be careful to clear it before starting a new look
1325 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1326 will look up `Z' in the scope of `X', rather than the current
1327 scope.) Unfortunately, it is difficult to tell when name lookup
1328 is complete, because we sometimes peek at a token, look it up,
1329 and then decide not to consume it. */
1332 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1333 last lookup took place. OBJECT_SCOPE is used if an expression
1334 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1335 respectively. QUALIFYING_SCOPE is used for an expression of the
1336 form "X::Y"; it refers to X. */
1338 tree qualifying_scope;
1340 /* A stack of parsing contexts. All but the bottom entry on the
1341 stack will be tentative contexts.
1343 We parse tentatively in order to determine which construct is in
1344 use in some situations. For example, in order to determine
1345 whether a statement is an expression-statement or a
1346 declaration-statement we parse it tentatively as a
1347 declaration-statement. If that fails, we then reparse the same
1348 token stream as an expression-statement. */
1349 cp_parser_context *context;
1351 /* True if we are parsing GNU C++. If this flag is not set, then
1352 GNU extensions are not recognized. */
1353 bool allow_gnu_extensions_p;
1355 /* TRUE if the `>' token should be interpreted as the greater-than
1356 operator. FALSE if it is the end of a template-id or
1357 template-parameter-list. */
1358 bool greater_than_is_operator_p;
1360 /* TRUE if default arguments are allowed within a parameter list
1361 that starts at this point. FALSE if only a gnu extension makes
1362 them permissible. */
1363 bool default_arg_ok_p;
1365 /* TRUE if we are parsing an integral constant-expression. See
1366 [expr.const] for a precise definition. */
1367 bool integral_constant_expression_p;
1369 /* TRUE if we are parsing an integral constant-expression -- but a
1370 non-constant expression should be permitted as well. This flag
1371 is used when parsing an array bound so that GNU variable-length
1372 arrays are tolerated. */
1373 bool allow_non_integral_constant_expression_p;
1375 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1376 been seen that makes the expression non-constant. */
1377 bool non_integral_constant_expression_p;
1379 /* TRUE if local variable names and `this' are forbidden in the
1381 bool local_variables_forbidden_p;
1383 /* TRUE if the declaration we are parsing is part of a
1384 linkage-specification of the form `extern string-literal
1386 bool in_unbraced_linkage_specification_p;
1388 /* TRUE if we are presently parsing a declarator, after the
1389 direct-declarator. */
1390 bool in_declarator_p;
1392 /* TRUE if we are presently parsing a template-argument-list. */
1393 bool in_template_argument_list_p;
1395 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1396 to IN_OMP_BLOCK if parsing OpenMP structured block and
1397 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1398 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1399 iteration-statement, OpenMP block or loop within that switch. */
1400 #define IN_SWITCH_STMT 1
1401 #define IN_ITERATION_STMT 2
1402 #define IN_OMP_BLOCK 4
1403 #define IN_OMP_FOR 8
1404 unsigned char in_statement;
1406 /* TRUE if we are presently parsing the body of a switch statement.
1407 Note that this doesn't quite overlap with in_statement above.
1408 The difference relates to giving the right sets of error messages:
1409 "case not in switch" vs "break statement used with OpenMP...". */
1410 bool in_switch_statement_p;
1412 /* TRUE if we are parsing a type-id in an expression context. In
1413 such a situation, both "type (expr)" and "type (type)" are valid
1415 bool in_type_id_in_expr_p;
1417 /* TRUE if we are currently in a header file where declarations are
1418 implicitly extern "C". */
1419 bool implicit_extern_c;
1421 /* TRUE if strings in expressions should be translated to the execution
1423 bool translate_strings_p;
1425 /* TRUE if we are presently parsing the body of a function, but not
1427 bool in_function_body;
1429 /* If non-NULL, then we are parsing a construct where new type
1430 definitions are not permitted. The string stored here will be
1431 issued as an error message if a type is defined. */
1432 const char *type_definition_forbidden_message;
1434 /* A list of lists. The outer list is a stack, used for member
1435 functions of local classes. At each level there are two sub-list,
1436 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1437 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1438 TREE_VALUE's. The functions are chained in reverse declaration
1441 The TREE_PURPOSE sublist contains those functions with default
1442 arguments that need post processing, and the TREE_VALUE sublist
1443 contains those functions with definitions that need post
1446 These lists can only be processed once the outermost class being
1447 defined is complete. */
1448 tree unparsed_functions_queues;
1450 /* The number of classes whose definitions are currently in
1452 unsigned num_classes_being_defined;
1454 /* The number of template parameter lists that apply directly to the
1455 current declaration. */
1456 unsigned num_template_parameter_lists;
1461 /* Constructors and destructors. */
1463 static cp_parser *cp_parser_new
1466 /* Routines to parse various constructs.
1468 Those that return `tree' will return the error_mark_node (rather
1469 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1470 Sometimes, they will return an ordinary node if error-recovery was
1471 attempted, even though a parse error occurred. So, to check
1472 whether or not a parse error occurred, you should always use
1473 cp_parser_error_occurred. If the construct is optional (indicated
1474 either by an `_opt' in the name of the function that does the
1475 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1476 the construct is not present. */
1478 /* Lexical conventions [gram.lex] */
1480 static tree cp_parser_identifier
1482 static tree cp_parser_string_literal
1483 (cp_parser *, bool, bool);
1485 /* Basic concepts [gram.basic] */
1487 static bool cp_parser_translation_unit
1490 /* Expressions [gram.expr] */
1492 static tree cp_parser_primary_expression
1493 (cp_parser *, bool, bool, bool, cp_id_kind *);
1494 static tree cp_parser_id_expression
1495 (cp_parser *, bool, bool, bool *, bool, bool);
1496 static tree cp_parser_unqualified_id
1497 (cp_parser *, bool, bool, bool, bool);
1498 static tree cp_parser_nested_name_specifier_opt
1499 (cp_parser *, bool, bool, bool, bool);
1500 static tree cp_parser_nested_name_specifier
1501 (cp_parser *, bool, bool, bool, bool);
1502 static tree cp_parser_class_or_namespace_name
1503 (cp_parser *, bool, bool, bool, bool, bool);
1504 static tree cp_parser_postfix_expression
1505 (cp_parser *, bool, bool);
1506 static tree cp_parser_postfix_open_square_expression
1507 (cp_parser *, tree, bool);
1508 static tree cp_parser_postfix_dot_deref_expression
1509 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1510 static tree cp_parser_parenthesized_expression_list
1511 (cp_parser *, bool, bool, bool *);
1512 static void cp_parser_pseudo_destructor_name
1513 (cp_parser *, tree *, tree *);
1514 static tree cp_parser_unary_expression
1515 (cp_parser *, bool, bool);
1516 static enum tree_code cp_parser_unary_operator
1518 static tree cp_parser_new_expression
1520 static tree cp_parser_new_placement
1522 static tree cp_parser_new_type_id
1523 (cp_parser *, tree *);
1524 static cp_declarator *cp_parser_new_declarator_opt
1526 static cp_declarator *cp_parser_direct_new_declarator
1528 static tree cp_parser_new_initializer
1530 static tree cp_parser_delete_expression
1532 static tree cp_parser_cast_expression
1533 (cp_parser *, bool, bool);
1534 static tree cp_parser_binary_expression
1535 (cp_parser *, bool);
1536 static tree cp_parser_question_colon_clause
1537 (cp_parser *, tree);
1538 static tree cp_parser_assignment_expression
1539 (cp_parser *, bool);
1540 static enum tree_code cp_parser_assignment_operator_opt
1542 static tree cp_parser_expression
1543 (cp_parser *, bool);
1544 static tree cp_parser_constant_expression
1545 (cp_parser *, bool, bool *);
1546 static tree cp_parser_builtin_offsetof
1548 /* APPLE LOCAL begin blocks 6040305 (ca) */
1549 static tree cp_parser_block_literal_expr (cp_parser *);
1550 /* APPLE LOCAL end blocks 6040305 (ca) */
1551 /* APPLE LOCAL begin C* language */
1552 static void objc_foreach_stmt
1553 (cp_parser *, tree);
1554 /* APPLE LOCAL end C* language */
1555 /* APPLE LOCAL begin C* property (Radar 4436866) */
1556 static void objc_cp_parser_at_property
1558 static void objc_cp_parse_property_decl
1560 /* APPLE LOCAL end C* property (Radar 4436866) */
1561 /* APPLE LOCAL begin radar 4548636 */
1562 static bool objc_attr_follwed_by_at_keyword
1564 /* APPLE LOCAL end radar 4548636 */
1566 /* Statements [gram.stmt.stmt] */
1568 static void cp_parser_statement
1569 (cp_parser *, tree, bool, bool *);
1570 static void cp_parser_label_for_labeled_statement
1572 static tree cp_parser_expression_statement
1573 (cp_parser *, tree);
1574 static tree cp_parser_compound_statement
1575 /* APPLE LOCAL radar 5982990 */
1576 (cp_parser *, tree, bool, bool);
1577 static void cp_parser_statement_seq_opt
1578 (cp_parser *, tree);
1579 static tree cp_parser_selection_statement
1580 (cp_parser *, bool *);
1581 static tree cp_parser_condition
1583 static tree cp_parser_iteration_statement
1585 static void cp_parser_for_init_statement
1587 static tree cp_parser_jump_statement
1589 static void cp_parser_declaration_statement
1592 static tree cp_parser_implicitly_scoped_statement
1593 (cp_parser *, bool *);
1594 static void cp_parser_already_scoped_statement
1597 /* Declarations [gram.dcl.dcl] */
1599 static void cp_parser_declaration_seq_opt
1601 static void cp_parser_declaration
1603 static void cp_parser_block_declaration
1604 (cp_parser *, bool);
1605 static void cp_parser_simple_declaration
1606 (cp_parser *, bool);
1607 static void cp_parser_decl_specifier_seq
1608 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1609 static tree cp_parser_storage_class_specifier_opt
1611 static tree cp_parser_function_specifier_opt
1612 (cp_parser *, cp_decl_specifier_seq *);
1613 static tree cp_parser_type_specifier
1614 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1616 static tree cp_parser_simple_type_specifier
1617 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1618 static tree cp_parser_type_name
1620 static tree cp_parser_elaborated_type_specifier
1621 (cp_parser *, bool, bool);
1622 static tree cp_parser_enum_specifier
1624 static void cp_parser_enumerator_list
1625 (cp_parser *, tree);
1626 static void cp_parser_enumerator_definition
1627 (cp_parser *, tree);
1628 static tree cp_parser_namespace_name
1630 static void cp_parser_namespace_definition
1632 static void cp_parser_namespace_body
1634 static tree cp_parser_qualified_namespace_specifier
1636 static void cp_parser_namespace_alias_definition
1638 static bool cp_parser_using_declaration
1639 (cp_parser *, bool);
1640 static void cp_parser_using_directive
1642 static void cp_parser_asm_definition
1644 static void cp_parser_linkage_specification
1647 /* Declarators [gram.dcl.decl] */
1649 static tree cp_parser_init_declarator
1650 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1651 static cp_declarator *cp_parser_declarator
1652 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1653 static cp_declarator *cp_parser_direct_declarator
1654 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1655 static enum tree_code cp_parser_ptr_operator
1656 (cp_parser *, tree *, cp_cv_quals *);
1657 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1659 static tree cp_parser_declarator_id
1660 (cp_parser *, bool);
1661 static tree cp_parser_type_id
1663 static void cp_parser_type_specifier_seq
1664 (cp_parser *, bool, cp_decl_specifier_seq *);
1665 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1667 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1668 (cp_parser *, bool *);
1669 static cp_parameter_declarator *cp_parser_parameter_declaration
1670 (cp_parser *, bool, bool *);
1671 static void cp_parser_function_body
1673 static tree cp_parser_initializer
1674 (cp_parser *, bool *, bool *);
1675 static tree cp_parser_initializer_clause
1676 (cp_parser *, bool *);
1677 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1678 (cp_parser *, bool *);
1680 static bool cp_parser_ctor_initializer_opt_and_function_body
1683 /* Classes [gram.class] */
1685 static tree cp_parser_class_name
1686 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1687 static tree cp_parser_class_specifier
1689 static tree cp_parser_class_head
1690 (cp_parser *, bool *, tree *, tree *);
1691 static enum tag_types cp_parser_class_key
1693 static void cp_parser_member_specification_opt
1695 static void cp_parser_member_declaration
1697 static tree cp_parser_pure_specifier
1699 static tree cp_parser_constant_initializer
1702 /* Derived classes [gram.class.derived] */
1704 static tree cp_parser_base_clause
1706 static tree cp_parser_base_specifier
1709 /* Special member functions [gram.special] */
1711 static tree cp_parser_conversion_function_id
1713 static tree cp_parser_conversion_type_id
1715 static cp_declarator *cp_parser_conversion_declarator_opt
1717 static bool cp_parser_ctor_initializer_opt
1719 static void cp_parser_mem_initializer_list
1721 static tree cp_parser_mem_initializer
1723 static tree cp_parser_mem_initializer_id
1726 /* Overloading [gram.over] */
1728 static tree cp_parser_operator_function_id
1730 static tree cp_parser_operator
1733 /* Templates [gram.temp] */
1735 static void cp_parser_template_declaration
1736 (cp_parser *, bool);
1737 static tree cp_parser_template_parameter_list
1739 static tree cp_parser_template_parameter
1740 (cp_parser *, bool *);
1741 static tree cp_parser_type_parameter
1743 static tree cp_parser_template_id
1744 (cp_parser *, bool, bool, bool);
1745 static tree cp_parser_template_name
1746 (cp_parser *, bool, bool, bool, bool *);
1747 static tree cp_parser_template_argument_list
1749 static tree cp_parser_template_argument
1751 static void cp_parser_explicit_instantiation
1753 static void cp_parser_explicit_specialization
1756 /* Exception handling [gram.exception] */
1758 static tree cp_parser_try_block
1760 static bool cp_parser_function_try_block
1762 static void cp_parser_handler_seq
1764 static void cp_parser_handler
1766 static tree cp_parser_exception_declaration
1768 static tree cp_parser_throw_expression
1770 static tree cp_parser_exception_specification_opt
1772 static tree cp_parser_type_id_list
1775 /* GNU Extensions */
1777 static tree cp_parser_asm_specification_opt
1779 static tree cp_parser_asm_operand_list
1781 static tree cp_parser_asm_clobber_list
1783 static tree cp_parser_attributes_opt
1785 static tree cp_parser_attribute_list
1787 static bool cp_parser_extension_opt
1788 (cp_parser *, int *);
1789 static void cp_parser_label_declaration
1792 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1793 static bool cp_parser_pragma
1794 (cp_parser *, enum pragma_context);
1796 /* Objective-C++ Productions */
1798 static tree cp_parser_objc_message_receiver
1800 static tree cp_parser_objc_message_args
1802 static tree cp_parser_objc_message_expression
1804 /* APPLE LOCAL begin radar 5277239 */
1805 static tree cp_parser_objc_reference_expression
1806 (cp_parser *, tree);
1807 /* APPLE LOCAL end radar 5277239 */
1808 static tree cp_parser_objc_encode_expression
1810 static tree cp_parser_objc_defs_expression
1812 static tree cp_parser_objc_protocol_expression
1814 static tree cp_parser_objc_selector_expression
1816 static tree cp_parser_objc_expression
1818 static bool cp_parser_objc_selector_p
1820 static tree cp_parser_objc_selector
1822 /* APPLE LOCAL begin radar 3803157 - objc attribute */
1823 static void cp_parser_objc_maybe_attributes
1824 (cp_parser *, tree *);
1825 static tree cp_parser_objc_identifier_list
1827 /* APPLE LOCAL end radar 3803157 - objc attribute */
1828 static tree cp_parser_objc_protocol_refs_opt
1830 static void cp_parser_objc_declaration
1832 static tree cp_parser_objc_statement
1835 /* Utility Routines */
1837 static tree cp_parser_lookup_name
1838 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1839 static tree cp_parser_lookup_name_simple
1840 (cp_parser *, tree);
1841 static tree cp_parser_maybe_treat_template_as_class
1843 static bool cp_parser_check_declarator_template_parameters
1844 (cp_parser *, cp_declarator *);
1845 static bool cp_parser_check_template_parameters
1846 (cp_parser *, unsigned);
1847 static tree cp_parser_simple_cast_expression
1849 static tree cp_parser_global_scope_opt
1850 (cp_parser *, bool);
1851 static bool cp_parser_constructor_declarator_p
1852 (cp_parser *, bool);
1853 static tree cp_parser_function_definition_from_specifiers_and_declarator
1854 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1855 static tree cp_parser_function_definition_after_declarator
1856 (cp_parser *, bool);
1857 static void cp_parser_template_declaration_after_export
1858 (cp_parser *, bool);
1859 static void cp_parser_perform_template_parameter_access_checks
1860 (VEC (deferred_access_check,gc)*);
1861 static tree cp_parser_single_declaration
1862 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1863 static tree cp_parser_functional_cast
1864 (cp_parser *, tree);
1865 static tree cp_parser_save_member_function_body
1866 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1867 static tree cp_parser_enclosed_template_argument_list
1869 static void cp_parser_save_default_args
1870 (cp_parser *, tree);
1871 static void cp_parser_late_parsing_for_member
1872 (cp_parser *, tree);
1873 static void cp_parser_late_parsing_default_args
1874 (cp_parser *, tree);
1875 static tree cp_parser_sizeof_operand
1876 (cp_parser *, enum rid);
1877 static bool cp_parser_declares_only_class_p
1879 static void cp_parser_set_storage_class
1880 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1881 static void cp_parser_set_decl_spec_type
1882 (cp_decl_specifier_seq *, tree, bool);
1883 static bool cp_parser_friend_p
1884 (const cp_decl_specifier_seq *);
1885 static cp_token *cp_parser_require
1886 (cp_parser *, enum cpp_ttype, const char *);
1887 static cp_token *cp_parser_require_keyword
1888 (cp_parser *, enum rid, const char *);
1889 static bool cp_parser_token_starts_function_definition_p
1891 static bool cp_parser_next_token_starts_class_definition_p
1893 static bool cp_parser_next_token_ends_template_argument_p
1895 static bool cp_parser_nth_token_starts_template_argument_list_p
1896 (cp_parser *, size_t);
1897 static enum tag_types cp_parser_token_is_class_key
1899 static void cp_parser_check_class_key
1900 (enum tag_types, tree type);
1901 static void cp_parser_check_access_in_redeclaration
1903 static bool cp_parser_optional_template_keyword
1905 static void cp_parser_pre_parsed_nested_name_specifier
1907 static void cp_parser_cache_group
1908 (cp_parser *, enum cpp_ttype, unsigned);
1909 static void cp_parser_parse_tentatively
1911 static void cp_parser_commit_to_tentative_parse
1913 static void cp_parser_abort_tentative_parse
1915 static bool cp_parser_parse_definitely
1917 static inline bool cp_parser_parsing_tentatively
1919 static bool cp_parser_uncommitted_to_tentative_parse_p
1921 static void cp_parser_error
1922 (cp_parser *, const char *);
1923 static void cp_parser_name_lookup_error
1924 (cp_parser *, tree, tree, const char *);
1925 static bool cp_parser_simulate_error
1927 static bool cp_parser_check_type_definition
1929 static void cp_parser_check_for_definition_in_return_type
1930 (cp_declarator *, tree);
1931 static void cp_parser_check_for_invalid_template_id
1932 (cp_parser *, tree);
1933 static bool cp_parser_non_integral_constant_expression
1934 (cp_parser *, const char *);
1935 static void cp_parser_diagnose_invalid_type_name
1936 (cp_parser *, tree, tree);
1937 static bool cp_parser_parse_and_diagnose_invalid_type_name
1939 static int cp_parser_skip_to_closing_parenthesis
1940 (cp_parser *, bool, bool, bool);
1941 static void cp_parser_skip_to_end_of_statement
1943 static void cp_parser_consume_semicolon_at_end_of_statement
1945 static void cp_parser_skip_to_end_of_block_or_statement
1947 static void cp_parser_skip_to_closing_brace
1949 static void cp_parser_skip_to_end_of_template_parameter_list
1951 static void cp_parser_skip_to_pragma_eol
1952 (cp_parser*, cp_token *);
1953 static bool cp_parser_error_occurred
1955 static bool cp_parser_allow_gnu_extensions_p
1957 static bool cp_parser_is_string_literal
1959 static bool cp_parser_is_keyword
1960 (cp_token *, enum rid);
1961 static tree cp_parser_make_typename_type
1962 (cp_parser *, tree, tree);
1964 /* Returns nonzero if we are parsing tentatively. */
1967 cp_parser_parsing_tentatively (cp_parser* parser)
1969 return parser->context->next != NULL;
1972 /* Returns nonzero if TOKEN is a string literal. */
1975 cp_parser_is_string_literal (cp_token* token)
1977 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1980 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1983 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1985 return token->keyword == keyword;
1988 /* If not parsing tentatively, issue a diagnostic of the form
1989 FILE:LINE: MESSAGE before TOKEN
1990 where TOKEN is the next token in the input stream. MESSAGE
1991 (specified by the caller) is usually of the form "expected
1995 cp_parser_error (cp_parser* parser, const char* message)
1997 if (!cp_parser_simulate_error (parser))
1999 cp_token *token = cp_lexer_peek_token (parser->lexer);
2000 /* This diagnostic makes more sense if it is tagged to the line
2001 of the token we just peeked at. */
2002 cp_lexer_set_source_position_from_token (token);
2004 if (token->type == CPP_PRAGMA)
2006 error ("%<#pragma%> is not allowed here");
2007 cp_parser_skip_to_pragma_eol (parser, token);
2011 c_parse_error (message,
2012 /* Because c_parser_error does not understand
2013 CPP_KEYWORD, keywords are treated like
2015 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2020 /* Issue an error about name-lookup failing. NAME is the
2021 IDENTIFIER_NODE DECL is the result of
2022 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2023 the thing that we hoped to find. */
2026 cp_parser_name_lookup_error (cp_parser* parser,
2029 const char* desired)
2031 /* If name lookup completely failed, tell the user that NAME was not
2033 if (decl == error_mark_node)
2035 if (parser->scope && parser->scope != global_namespace)
2036 error ("%<%D::%D%> has not been declared",
2037 parser->scope, name);
2038 else if (parser->scope == global_namespace)
2039 error ("%<::%D%> has not been declared", name);
2040 else if (parser->object_scope
2041 && !CLASS_TYPE_P (parser->object_scope))
2042 error ("request for member %qD in non-class type %qT",
2043 name, parser->object_scope);
2044 else if (parser->object_scope)
2045 error ("%<%T::%D%> has not been declared",
2046 parser->object_scope, name);
2048 error ("%qD has not been declared", name);
2050 else if (parser->scope && parser->scope != global_namespace)
2051 error ("%<%D::%D%> %s", parser->scope, name, desired);
2052 else if (parser->scope == global_namespace)
2053 error ("%<::%D%> %s", name, desired);
2055 error ("%qD %s", name, desired);
2058 /* If we are parsing tentatively, remember that an error has occurred
2059 during this tentative parse. Returns true if the error was
2060 simulated; false if a message should be issued by the caller. */
2063 cp_parser_simulate_error (cp_parser* parser)
2065 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2067 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2073 /* Check for repeated decl-specifiers. */
2076 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2080 for (ds = ds_first; ds != ds_last; ++ds)
2082 unsigned count = decl_specs->specs[(int)ds];
2085 /* The "long" specifier is a special case because of "long long". */
2089 error ("%<long long long%> is too long for GCC");
2090 else if (pedantic && !in_system_header && warn_long_long)
2091 pedwarn ("ISO C++ does not support %<long long%>");
2095 static const char *const decl_spec_names[] = {
2111 error ("duplicate %qs", decl_spec_names[(int)ds]);
2116 /* This function is called when a type is defined. If type
2117 definitions are forbidden at this point, an error message is
2121 cp_parser_check_type_definition (cp_parser* parser)
2123 /* If types are forbidden here, issue a message. */
2124 if (parser->type_definition_forbidden_message)
2126 /* Use `%s' to print the string in case there are any escape
2127 characters in the message. */
2128 error ("%s", parser->type_definition_forbidden_message);
2134 /* This function is called when the DECLARATOR is processed. The TYPE
2135 was a type defined in the decl-specifiers. If it is invalid to
2136 define a type in the decl-specifiers for DECLARATOR, an error is
2140 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2143 /* [dcl.fct] forbids type definitions in return types.
2144 Unfortunately, it's not easy to know whether or not we are
2145 processing a return type until after the fact. */
2147 && (declarator->kind == cdk_pointer
2148 || declarator->kind == cdk_reference
2149 || declarator->kind == cdk_ptrmem))
2150 declarator = declarator->declarator;
2152 && declarator->kind == cdk_function)
2154 error ("new types may not be defined in a return type");
2155 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2160 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2161 "<" in any valid C++ program. If the next token is indeed "<",
2162 issue a message warning the user about what appears to be an
2163 invalid attempt to form a template-id. */
2166 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2169 cp_token_position start = 0;
2171 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2174 error ("%qT is not a template", type);
2175 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2176 error ("%qE is not a template", type);
2178 error ("invalid template-id");
2179 /* Remember the location of the invalid "<". */
2180 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2181 start = cp_lexer_token_position (parser->lexer, true);
2182 /* Consume the "<". */
2183 cp_lexer_consume_token (parser->lexer);
2184 /* Parse the template arguments. */
2185 cp_parser_enclosed_template_argument_list (parser);
2186 /* Permanently remove the invalid template arguments so that
2187 this error message is not issued again. */
2189 cp_lexer_purge_tokens_after (parser->lexer, start);
2193 /* If parsing an integral constant-expression, issue an error message
2194 about the fact that THING appeared and return true. Otherwise,
2195 return false. In either case, set
2196 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2199 cp_parser_non_integral_constant_expression (cp_parser *parser,
2202 parser->non_integral_constant_expression_p = true;
2203 if (parser->integral_constant_expression_p)
2205 if (!parser->allow_non_integral_constant_expression_p)
2207 error ("%s cannot appear in a constant-expression", thing);
2214 /* Emit a diagnostic for an invalid type name. SCOPE is the
2215 qualifying scope (or NULL, if none) for ID. This function commits
2216 to the current active tentative parse, if any. (Otherwise, the
2217 problematic construct might be encountered again later, resulting
2218 in duplicate error messages.) */
2221 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2223 tree decl, old_scope;
2224 /* Try to lookup the identifier. */
2225 old_scope = parser->scope;
2226 parser->scope = scope;
2227 decl = cp_parser_lookup_name_simple (parser, id);
2228 parser->scope = old_scope;
2229 /* If the lookup found a template-name, it means that the user forgot
2230 to specify an argument list. Emit a useful error message. */
2231 if (TREE_CODE (decl) == TEMPLATE_DECL)
2232 error ("invalid use of template-name %qE without an argument list", decl);
2233 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2234 error ("invalid use of destructor %qD as a type", id);
2235 else if (TREE_CODE (decl) == TYPE_DECL)
2236 /* Something like 'unsigned A a;' */
2237 error ("invalid combination of multiple type-specifiers");
2238 else if (!parser->scope)
2240 /* Issue an error message. */
2241 error ("%qE does not name a type", id);
2242 /* If we're in a template class, it's possible that the user was
2243 referring to a type from a base class. For example:
2245 template <typename T> struct A { typedef T X; };
2246 template <typename T> struct B : public A<T> { X x; };
2248 The user should have said "typename A<T>::X". */
2249 if (processing_template_decl && current_class_type
2250 && TYPE_BINFO (current_class_type))
2254 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2258 tree base_type = BINFO_TYPE (b);
2259 if (CLASS_TYPE_P (base_type)
2260 && dependent_type_p (base_type))
2263 /* Go from a particular instantiation of the
2264 template (which will have an empty TYPE_FIELDs),
2265 to the main version. */
2266 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2267 for (field = TYPE_FIELDS (base_type);
2269 field = TREE_CHAIN (field))
2270 if (TREE_CODE (field) == TYPE_DECL
2271 && DECL_NAME (field) == id)
2273 inform ("(perhaps %<typename %T::%E%> was intended)",
2274 BINFO_TYPE (b), id);
2283 /* Here we diagnose qualified-ids where the scope is actually correct,
2284 but the identifier does not resolve to a valid type name. */
2285 else if (parser->scope != error_mark_node)
2287 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2288 error ("%qE in namespace %qE does not name a type",
2290 else if (TYPE_P (parser->scope))
2291 error ("%qE in class %qT does not name a type", id, parser->scope);
2295 cp_parser_commit_to_tentative_parse (parser);
2298 /* Check for a common situation where a type-name should be present,
2299 but is not, and issue a sensible error message. Returns true if an
2300 invalid type-name was detected.
2302 The situation handled by this function are variable declarations of the
2303 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2304 Usually, `ID' should name a type, but if we got here it means that it
2305 does not. We try to emit the best possible error message depending on
2306 how exactly the id-expression looks like. */
2309 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2313 cp_parser_parse_tentatively (parser);
2314 id = cp_parser_id_expression (parser,
2315 /*template_keyword_p=*/false,
2316 /*check_dependency_p=*/true,
2317 /*template_p=*/NULL,
2318 /*declarator_p=*/true,
2319 /*optional_p=*/false);
2320 /* After the id-expression, there should be a plain identifier,
2321 otherwise this is not a simple variable declaration. Also, if
2322 the scope is dependent, we cannot do much. */
2323 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2324 || (parser->scope && TYPE_P (parser->scope)
2325 && dependent_type_p (parser->scope))
2326 || TREE_CODE (id) == TYPE_DECL)
2328 cp_parser_abort_tentative_parse (parser);
2331 if (!cp_parser_parse_definitely (parser))
2334 /* Emit a diagnostic for the invalid type. */
2335 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2336 /* Skip to the end of the declaration; there's no point in
2337 trying to process it. */
2338 cp_parser_skip_to_end_of_block_or_statement (parser);
2342 /* Consume tokens up to, and including, the next non-nested closing `)'.
2343 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2344 are doing error recovery. Returns -1 if OR_COMMA is true and we
2345 found an unnested comma. */
2348 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2353 unsigned paren_depth = 0;
2354 unsigned brace_depth = 0;
2356 if (recovering && !or_comma
2357 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2362 cp_token * token = cp_lexer_peek_token (parser->lexer);
2364 switch (token->type)
2367 case CPP_PRAGMA_EOL:
2368 /* If we've run out of tokens, then there is no closing `)'. */
2372 /* This matches the processing in skip_to_end_of_statement. */
2377 case CPP_OPEN_BRACE:
2380 case CPP_CLOSE_BRACE:
2386 if (recovering && or_comma && !brace_depth && !paren_depth)
2390 case CPP_OPEN_PAREN:
2395 case CPP_CLOSE_PAREN:
2396 if (!brace_depth && !paren_depth--)
2399 cp_lexer_consume_token (parser->lexer);
2408 /* Consume the token. */
2409 cp_lexer_consume_token (parser->lexer);
2413 /* Consume tokens until we reach the end of the current statement.
2414 Normally, that will be just before consuming a `;'. However, if a
2415 non-nested `}' comes first, then we stop before consuming that. */
2418 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2420 unsigned nesting_depth = 0;
2424 cp_token *token = cp_lexer_peek_token (parser->lexer);
2426 switch (token->type)
2429 case CPP_PRAGMA_EOL:
2430 /* If we've run out of tokens, stop. */
2434 /* If the next token is a `;', we have reached the end of the
2440 case CPP_CLOSE_BRACE:
2441 /* If this is a non-nested '}', stop before consuming it.
2442 That way, when confronted with something like:
2446 we stop before consuming the closing '}', even though we
2447 have not yet reached a `;'. */
2448 if (nesting_depth == 0)
2451 /* If it is the closing '}' for a block that we have
2452 scanned, stop -- but only after consuming the token.
2458 we will stop after the body of the erroneously declared
2459 function, but before consuming the following `typedef'
2461 if (--nesting_depth == 0)
2463 cp_lexer_consume_token (parser->lexer);
2467 case CPP_OPEN_BRACE:
2475 /* Consume the token. */
2476 cp_lexer_consume_token (parser->lexer);
2480 /* APPLE LOCAL begin radar 5277239 */
2481 /* This routine checks that type_decl is a class or class object followed by a '.'
2482 which is an alternative syntax to class-method messaging [class-name class-method]
2486 cp_objc_property_reference_prefix (cp_parser *parser, tree type)
2488 return c_dialect_objc () && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
2489 && (objc_is_id (type) || objc_is_class_name (type));
2491 /* APPLE LOCAL end radar 5277239 */
2492 /* APPLE LOCAL begin C* property (Radar 4436866, 4591909) */
2493 /* This routine parses the propery declarations. */
2496 objc_cp_parse_property_decl (cp_parser *parser)
2498 int declares_class_or_enum;
2499 cp_decl_specifier_seq declspecs;
2501 cp_parser_decl_specifier_seq (parser,
2502 CP_PARSER_FLAGS_NONE,
2504 &declares_class_or_enum);
2505 /* Keep going until we hit the `;' at the end of the declaration. */
2506 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
2510 cp_declarator *declarator
2511 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
2513 property = grokdeclarator (declarator, &declspecs, NORMAL,0, NULL);
2514 /* Revover from any kind of error in property declaration. */
2515 if (property == error_mark_node || property == NULL_TREE)
2517 /* Add to property list. */
2518 objc_add_property_variable (copy_node (property));
2519 if (token->type == CPP_COMMA)
2521 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
2524 else if (token->type == CPP_EOF)
2527 cp_lexer_consume_token (parser->lexer); /* Eat ';'. */
2530 /* This function parses a @property declaration inside an objective class
2531 or its implementation. */
2534 objc_cp_parser_at_property (cp_parser *parser)
2538 objc_set_property_attr (0, NULL_TREE);
2539 /* Consume @property */
2540 cp_lexer_consume_token (parser->lexer);
2541 token = cp_lexer_peek_token (parser->lexer);
2542 if (token->type == CPP_OPEN_PAREN)
2544 cp_lexer_consume_token (parser->lexer);
2545 while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF)
2548 /* property has attribute list. */
2550 node = cp_parser_identifier (parser);
2551 if (node == ridpointers [(int) RID_READONLY])
2553 /* Do the readyonly thing. */
2554 objc_set_property_attr (1, NULL_TREE);
2556 else if (node == ridpointers [(int) RID_GETTER]
2557 || node == ridpointers [(int) RID_SETTER])
2559 /* Do the getter/setter attribute. */
2560 token = cp_lexer_consume_token (parser->lexer);
2561 if (token->type == CPP_EQ)
2563 /* APPLE LOCAL radar 4675792 */
2564 tree attr_ident = cp_parser_objc_selector (parser);
2566 if (node == ridpointers [(int) RID_GETTER])
2571 /* Consume the ':' which must always follow the setter name. */
2572 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
2573 cp_lexer_consume_token (parser->lexer);
2575 objc_set_property_attr (num, attr_ident);
2579 error ("getter/setter attribute must be followed by '='");
2583 /* APPLE LOCAL begin radar 4947014 - objc atomic property */
2584 else if (node == ridpointers [(int) RID_NONATOMIC])
2586 objc_set_property_attr (13, NULL_TREE);
2588 /* APPLE LOCAL end radar 4947014 - objc atomic property */
2591 error ("unknown property attribute");
2594 /* APPLE LOCAL begin radar 6302949 */
2595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
2596 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
2597 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
2598 warning (0, "property attributes must be separated by a comma");
2599 /* APPLE LOCAL end radar 6302949 */
2600 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
2601 cp_lexer_consume_token (parser->lexer);
2602 token = cp_lexer_peek_token (parser->lexer);
2604 if (token->type != CPP_CLOSE_PAREN)
2606 error ("syntax error in @property's attribute declaration");
2609 cp_lexer_consume_token (parser->lexer);
2611 objc_cp_parse_property_decl (parser);
2613 /* APPLE LOCAL end C* property (Radar 4436866, 4591909) */
2615 /* This function is called at the end of a statement or declaration.
2616 If the next token is a semicolon, it is consumed; otherwise, error
2617 recovery is attempted. */
2620 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2622 /* Look for the trailing `;'. */
2623 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2625 /* If there is additional (erroneous) input, skip to the end of
2627 cp_parser_skip_to_end_of_statement (parser);
2628 /* If the next token is now a `;', consume it. */
2629 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2630 cp_lexer_consume_token (parser->lexer);
2634 /* Skip tokens until we have consumed an entire block, or until we
2635 have consumed a non-nested `;'. */
2638 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2640 int nesting_depth = 0;
2642 while (nesting_depth >= 0)
2644 cp_token *token = cp_lexer_peek_token (parser->lexer);
2646 switch (token->type)
2649 case CPP_PRAGMA_EOL:
2650 /* If we've run out of tokens, stop. */
2654 /* Stop if this is an unnested ';'. */
2659 case CPP_CLOSE_BRACE:
2660 /* Stop if this is an unnested '}', or closes the outermost
2667 case CPP_OPEN_BRACE:
2676 /* Consume the token. */
2677 cp_lexer_consume_token (parser->lexer);
2681 /* Skip tokens until a non-nested closing curly brace is the next
2685 cp_parser_skip_to_closing_brace (cp_parser *parser)
2687 unsigned nesting_depth = 0;
2691 cp_token *token = cp_lexer_peek_token (parser->lexer);
2693 switch (token->type)
2696 case CPP_PRAGMA_EOL:
2697 /* If we've run out of tokens, stop. */
2700 case CPP_CLOSE_BRACE:
2701 /* If the next token is a non-nested `}', then we have reached
2702 the end of the current block. */
2703 if (nesting_depth-- == 0)
2707 case CPP_OPEN_BRACE:
2708 /* If it the next token is a `{', then we are entering a new
2709 block. Consume the entire block. */
2717 /* Consume the token. */
2718 cp_lexer_consume_token (parser->lexer);
2722 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2723 parameter is the PRAGMA token, allowing us to purge the entire pragma
2727 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2731 parser->lexer->in_pragma = false;
2734 token = cp_lexer_consume_token (parser->lexer);
2735 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2737 /* Ensure that the pragma is not parsed again. */
2738 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2741 /* Require pragma end of line, resyncing with it as necessary. The
2742 arguments are as for cp_parser_skip_to_pragma_eol. */
2745 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2747 parser->lexer->in_pragma = false;
2748 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2749 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2752 /* This is a simple wrapper around make_typename_type. When the id is
2753 an unresolved identifier node, we can provide a superior diagnostic
2754 using cp_parser_diagnose_invalid_type_name. */
2757 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2760 if (TREE_CODE (id) == IDENTIFIER_NODE)
2762 result = make_typename_type (scope, id, typename_type,
2763 /*complain=*/tf_none);
2764 if (result == error_mark_node)
2765 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2768 return make_typename_type (scope, id, typename_type, tf_error);
2772 /* Create a new C++ parser. */
2775 cp_parser_new (void)
2781 /* cp_lexer_new_main is called before calling ggc_alloc because
2782 cp_lexer_new_main might load a PCH file. */
2783 lexer = cp_lexer_new_main ();
2785 /* Initialize the binops_by_token so that we can get the tree
2786 directly from the token. */
2787 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2788 binops_by_token[binops[i].token_type] = binops[i];
2790 parser = GGC_CNEW (cp_parser);
2791 parser->lexer = lexer;
2792 parser->context = cp_parser_context_new (NULL);
2794 /* For now, we always accept GNU extensions. */
2795 parser->allow_gnu_extensions_p = 1;
2797 /* The `>' token is a greater-than operator, not the end of a
2799 parser->greater_than_is_operator_p = true;
2801 parser->default_arg_ok_p = true;
2803 /* We are not parsing a constant-expression. */
2804 parser->integral_constant_expression_p = false;
2805 parser->allow_non_integral_constant_expression_p = false;
2806 parser->non_integral_constant_expression_p = false;
2808 /* Local variable names are not forbidden. */
2809 parser->local_variables_forbidden_p = false;
2811 /* We are not processing an `extern "C"' declaration. */
2812 parser->in_unbraced_linkage_specification_p = false;
2814 /* We are not processing a declarator. */
2815 parser->in_declarator_p = false;
2817 /* We are not processing a template-argument-list. */
2818 parser->in_template_argument_list_p = false;
2820 /* We are not in an iteration statement. */
2821 parser->in_statement = 0;
2823 /* We are not in a switch statement. */
2824 parser->in_switch_statement_p = false;
2826 /* We are not parsing a type-id inside an expression. */
2827 parser->in_type_id_in_expr_p = false;
2829 /* Declarations aren't implicitly extern "C". */
2830 parser->implicit_extern_c = false;
2832 /* String literals should be translated to the execution character set. */
2833 parser->translate_strings_p = true;
2835 /* We are not parsing a function body. */
2836 parser->in_function_body = false;
2838 /* The unparsed function queue is empty. */
2839 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2841 /* There are no classes being defined. */
2842 parser->num_classes_being_defined = 0;
2844 /* No template parameters apply. */
2845 parser->num_template_parameter_lists = 0;
2850 /* Create a cp_lexer structure which will emit the tokens in CACHE
2851 and push it onto the parser's lexer stack. This is used for delayed
2852 parsing of in-class method bodies and default arguments, and should
2853 not be confused with tentative parsing. */
2855 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2857 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2858 lexer->next = parser->lexer;
2859 parser->lexer = lexer;
2861 /* Move the current source position to that of the first token in the
2863 cp_lexer_set_source_position_from_token (lexer->next_token);
2866 /* Pop the top lexer off the parser stack. This is never used for the
2867 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2869 cp_parser_pop_lexer (cp_parser *parser)
2871 cp_lexer *lexer = parser->lexer;
2872 parser->lexer = lexer->next;
2873 cp_lexer_destroy (lexer);
2875 /* Put the current source position back where it was before this
2876 lexer was pushed. */
2877 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2880 /* Lexical conventions [gram.lex] */
2882 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2886 cp_parser_identifier (cp_parser* parser)
2890 /* Look for the identifier. */
2891 token = cp_parser_require (parser, CPP_NAME, "identifier");
2892 /* Return the value. */
2893 return token ? token->u.value : error_mark_node;
2896 /* Parse a sequence of adjacent string constants. Returns a
2897 TREE_STRING representing the combined, nul-terminated string
2898 constant. If TRANSLATE is true, translate the string to the
2899 execution character set. If WIDE_OK is true, a wide string is
2902 C++98 [lex.string] says that if a narrow string literal token is
2903 adjacent to a wide string literal token, the behavior is undefined.
2904 However, C99 6.4.5p4 says that this results in a wide string literal.
2905 We follow C99 here, for consistency with the C front end.
2907 This code is largely lifted from lex_string() in c-lex.c.
2909 FUTURE: ObjC++ will need to handle @-strings here. */
2911 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2916 struct obstack str_ob;
2917 cpp_string str, istr, *strs;
2920 tok = cp_lexer_peek_token (parser->lexer);
2921 if (!cp_parser_is_string_literal (tok))
2923 cp_parser_error (parser, "expected string-literal");
2924 return error_mark_node;
2927 /* Try to avoid the overhead of creating and destroying an obstack
2928 for the common case of just one string. */
2929 if (!cp_parser_is_string_literal
2930 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2932 cp_lexer_consume_token (parser->lexer);
2934 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2935 str.len = TREE_STRING_LENGTH (tok->u.value);
2937 if (tok->type == CPP_WSTRING)
2944 gcc_obstack_init (&str_ob);
2949 cp_lexer_consume_token (parser->lexer);
2951 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2952 str.len = TREE_STRING_LENGTH (tok->u.value);
2953 if (tok->type == CPP_WSTRING)
2956 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2958 tok = cp_lexer_peek_token (parser->lexer);
2960 while (cp_parser_is_string_literal (tok));
2962 strs = (cpp_string *) obstack_finish (&str_ob);
2965 if (wide && !wide_ok)
2967 cp_parser_error (parser, "a wide string is invalid in this context");
2971 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2972 (parse_in, strs, count, &istr, wide))
2974 value = build_string (istr.len, (char *)istr.text);
2975 free ((void *)istr.text);
2977 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2978 value = fix_string_type (value);
2981 /* cpp_interpret_string has issued an error. */
2982 value = error_mark_node;
2985 obstack_free (&str_ob, 0);
2991 /* Basic concepts [gram.basic] */
2993 /* Parse a translation-unit.
2996 declaration-seq [opt]
2998 Returns TRUE if all went well. */
3001 cp_parser_translation_unit (cp_parser* parser)
3003 /* The address of the first non-permanent object on the declarator
3005 static void *declarator_obstack_base;
3009 /* Create the declarator obstack, if necessary. */
3010 if (!cp_error_declarator)
3012 gcc_obstack_init (&declarator_obstack);
3013 /* Create the error declarator. */
3014 cp_error_declarator = make_declarator (cdk_error);
3015 /* Create the empty parameter list. */
3016 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3017 /* Remember where the base of the declarator obstack lies. */
3018 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3021 cp_parser_declaration_seq_opt (parser);
3023 /* If there are no tokens left then all went well. */
3024 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3026 /* Get rid of the token array; we don't need it any more. */
3027 cp_lexer_destroy (parser->lexer);
3028 parser->lexer = NULL;
3030 /* This file might have been a context that's implicitly extern
3031 "C". If so, pop the lang context. (Only relevant for PCH.) */
3032 if (parser->implicit_extern_c)
3034 pop_lang_context ();
3035 parser->implicit_extern_c = false;
3039 finish_translation_unit ();
3045 cp_parser_error (parser, "expected declaration");
3049 /* Make sure the declarator obstack was fully cleaned up. */
3050 gcc_assert (obstack_next_free (&declarator_obstack)
3051 == declarator_obstack_base);
3053 /* All went well. */
3057 /* Expressions [gram.expr] */
3059 /* Parse a primary-expression.
3070 ( compound-statement )
3071 __builtin_va_arg ( assignment-expression , type-id )
3072 __builtin_offsetof ( type-id , offsetof-expression )
3073 APPLE LOCAL blocks 6040305 (cf)
3076 Objective-C++ Extension:
3084 ADDRESS_P is true iff this expression was immediately preceded by
3085 "&" and therefore might denote a pointer-to-member. CAST_P is true
3086 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3087 true iff this expression is a template argument.
3089 Returns a representation of the expression. Upon return, *IDK
3090 indicates what kind of id-expression (if any) was present. */
3093 cp_parser_primary_expression (cp_parser *parser,
3096 bool template_arg_p,
3101 /* Assume the primary expression is not an id-expression. */
3102 *idk = CP_ID_KIND_NONE;
3104 /* Peek at the next token. */
3105 token = cp_lexer_peek_token (parser->lexer);
3106 switch (token->type)
3108 /* APPLE LOCAL begin blocks 6040305 (cf) */
3112 tree expr = cp_parser_block_literal_expr (parser);
3115 cp_parser_error (parser, "expected primary-expression");
3116 return error_mark_node;
3117 /* APPLE LOCAL end blocks 6040305 (cf) */
3127 token = cp_lexer_consume_token (parser->lexer);
3128 /* Floating-point literals are only allowed in an integral
3129 constant expression if they are cast to an integral or
3130 enumeration type. */
3131 if (TREE_CODE (token->u.value) == REAL_CST
3132 && parser->integral_constant_expression_p
3135 /* CAST_P will be set even in invalid code like "int(2.7 +
3136 ...)". Therefore, we have to check that the next token
3137 is sure to end the cast. */
3140 cp_token *next_token;
3142 next_token = cp_lexer_peek_token (parser->lexer);
3143 if (/* The comma at the end of an
3144 enumerator-definition. */
3145 next_token->type != CPP_COMMA
3146 /* The curly brace at the end of an enum-specifier. */
3147 && next_token->type != CPP_CLOSE_BRACE
3148 /* The end of a statement. */
3149 && next_token->type != CPP_SEMICOLON
3150 /* The end of the cast-expression. */
3151 && next_token->type != CPP_CLOSE_PAREN
3152 /* The end of an array bound. */
3153 && next_token->type != CPP_CLOSE_SQUARE
3154 /* The closing ">" in a template-argument-list. */
3155 && (next_token->type != CPP_GREATER
3156 || parser->greater_than_is_operator_p))
3160 /* If we are within a cast, then the constraint that the
3161 cast is to an integral or enumeration type will be
3162 checked at that point. If we are not within a cast, then
3163 this code is invalid. */
3165 cp_parser_non_integral_constant_expression
3166 (parser, "floating-point literal");
3168 return token->u.value;
3172 /* ??? Should wide strings be allowed when parser->translate_strings_p
3173 is false (i.e. in attributes)? If not, we can kill the third
3174 argument to cp_parser_string_literal. */
3175 return cp_parser_string_literal (parser,
3176 parser->translate_strings_p,
3179 case CPP_OPEN_PAREN:
3182 bool saved_greater_than_is_operator_p;
3184 /* Consume the `('. */
3185 cp_lexer_consume_token (parser->lexer);
3186 /* Within a parenthesized expression, a `>' token is always
3187 the greater-than operator. */
3188 saved_greater_than_is_operator_p
3189 = parser->greater_than_is_operator_p;
3190 parser->greater_than_is_operator_p = true;
3191 /* If we see `( { ' then we are looking at the beginning of
3192 a GNU statement-expression. */
3193 if (cp_parser_allow_gnu_extensions_p (parser)
3194 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3196 /* Statement-expressions are not allowed by the standard. */
3198 pedwarn ("ISO C++ forbids braced-groups within expressions");
3200 /* And they're not allowed outside of a function-body; you
3201 cannot, for example, write:
3203 int i = ({ int j = 3; j + 1; });
3205 at class or namespace scope. */
3206 if (!parser->in_function_body)
3207 error ("statement-expressions are allowed only inside functions");
3208 /* Start the statement-expression. */
3209 expr = begin_stmt_expr ();
3210 /* Parse the compound-statement. */
3211 /* APPLE LOCAL radar 5982990 */
3212 cp_parser_compound_statement (parser, expr, false, false);
3214 expr = finish_stmt_expr (expr, false);
3218 /* Parse the parenthesized expression. */
3219 expr = cp_parser_expression (parser, cast_p);
3220 /* Let the front end know that this expression was
3221 enclosed in parentheses. This matters in case, for
3222 example, the expression is of the form `A::B', since
3223 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3225 finish_parenthesized_expr (expr);
3227 /* The `>' token might be the end of a template-id or
3228 template-parameter-list now. */
3229 parser->greater_than_is_operator_p
3230 = saved_greater_than_is_operator_p;
3231 /* Consume the `)'. */
3232 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3233 cp_parser_skip_to_end_of_statement (parser);
3239 switch (token->keyword)
3241 /* These two are the boolean literals. */
3243 cp_lexer_consume_token (parser->lexer);
3244 return boolean_true_node;
3246 cp_lexer_consume_token (parser->lexer);
3247 return boolean_false_node;
3249 /* The `__null' literal. */
3251 cp_lexer_consume_token (parser->lexer);
3254 /* Recognize the `this' keyword. */
3256 cp_lexer_consume_token (parser->lexer);
3257 if (parser->local_variables_forbidden_p)
3259 error ("%<this%> may not be used in this context");
3260 return error_mark_node;
3262 /* Pointers cannot appear in constant-expressions. */
3263 if (cp_parser_non_integral_constant_expression (parser,
3265 return error_mark_node;
3266 return finish_this_expr ();
3268 /* The `operator' keyword can be the beginning of an
3273 case RID_FUNCTION_NAME:
3274 case RID_PRETTY_FUNCTION_NAME:
3275 case RID_C99_FUNCTION_NAME:
3276 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3277 __func__ are the names of variables -- but they are
3278 treated specially. Therefore, they are handled here,
3279 rather than relying on the generic id-expression logic
3280 below. Grammatically, these names are id-expressions.
3282 Consume the token. */
3283 token = cp_lexer_consume_token (parser->lexer);
3284 /* Look up the name. */
3285 return finish_fname (token->u.value);
3292 /* The `__builtin_va_arg' construct is used to handle
3293 `va_arg'. Consume the `__builtin_va_arg' token. */
3294 cp_lexer_consume_token (parser->lexer);
3295 /* Look for the opening `('. */
3296 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3297 /* Now, parse the assignment-expression. */
3298 expression = cp_parser_assignment_expression (parser,
3300 /* Look for the `,'. */
3301 cp_parser_require (parser, CPP_COMMA, "`,'");
3302 /* Parse the type-id. */
3303 type = cp_parser_type_id (parser);
3304 /* Look for the closing `)'. */
3305 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3306 /* Using `va_arg' in a constant-expression is not
3308 if (cp_parser_non_integral_constant_expression (parser,
3310 return error_mark_node;
3311 return build_x_va_arg (expression, type);
3315 return cp_parser_builtin_offsetof (parser);
3317 /* Objective-C++ expressions. */
3319 case RID_AT_PROTOCOL:
3320 case RID_AT_SELECTOR:
3321 return cp_parser_objc_expression (parser);
3324 cp_parser_error (parser, "expected primary-expression");
3325 return error_mark_node;
3328 /* An id-expression can start with either an identifier, a
3329 `::' as the beginning of a qualified-id, or the "operator"
3333 case CPP_TEMPLATE_ID:
3334 case CPP_NESTED_NAME_SPECIFIER:
3338 const char *error_msg;
3343 /* Parse the id-expression. */
3345 = cp_parser_id_expression (parser,
3346 /*template_keyword_p=*/false,
3347 /*check_dependency_p=*/true,
3349 /*declarator_p=*/false,
3350 /*optional_p=*/false);
3351 if (id_expression == error_mark_node)
3352 return error_mark_node;
3353 token = cp_lexer_peek_token (parser->lexer);
3354 done = (token->type != CPP_OPEN_SQUARE
3355 && token->type != CPP_OPEN_PAREN
3356 && token->type != CPP_DOT
3357 && token->type != CPP_DEREF
3358 && token->type != CPP_PLUS_PLUS
3359 && token->type != CPP_MINUS_MINUS);
3360 /* If we have a template-id, then no further lookup is
3361 required. If the template-id was for a template-class, we
3362 will sometimes have a TYPE_DECL at this point. */
3363 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3364 || TREE_CODE (id_expression) == TYPE_DECL)
3365 decl = id_expression;
3366 /* Look up the name. */
3369 tree ambiguous_decls;
3371 decl = cp_parser_lookup_name (parser, id_expression,
3374 /*is_namespace=*/false,
3375 /*check_dependency=*/true,
3377 /* If the lookup was ambiguous, an error will already have
3379 if (ambiguous_decls)
3380 return error_mark_node;
3382 /* APPLE LOCAL begin radar 5277239 */
3383 if (TREE_CODE (decl) == TYPE_DECL
3384 && cp_objc_property_reference_prefix (parser, TREE_TYPE (decl)))
3385 return cp_parser_objc_reference_expression (parser, decl);
3386 /* APPLE LOCAL end radar 5277239 */
3387 /* In Objective-C++, an instance variable (ivar) may be preferred
3388 to whatever cp_parser_lookup_name() found. */
3389 decl = objc_lookup_ivar (decl, id_expression);
3391 /* If name lookup gives us a SCOPE_REF, then the
3392 qualifying scope was dependent. */
3393 if (TREE_CODE (decl) == SCOPE_REF)
3395 /* At this point, we do not know if DECL is a valid
3396 integral constant expression. We assume that it is
3397 in fact such an expression, so that code like:
3399 template <int N> struct A {
3403 is accepted. At template-instantiation time, we
3404 will check that B<N>::i is actually a constant. */
3407 /* Check to see if DECL is a local variable in a context
3408 where that is forbidden. */
3409 if (parser->local_variables_forbidden_p
3410 && local_variable_p (decl))
3412 /* It might be that we only found DECL because we are
3413 trying to be generous with pre-ISO scoping rules.
3414 For example, consider:
3418 for (int i = 0; i < 10; ++i) {}
3419 extern void f(int j = i);
3422 Here, name look up will originally find the out
3423 of scope `i'. We need to issue a warning message,
3424 but then use the global `i'. */
3425 decl = check_for_out_of_scope_variable (decl);
3426 if (local_variable_p (decl))
3428 error ("local variable %qD may not appear in this context",
3430 return error_mark_node;
3435 decl = (finish_id_expression
3436 (id_expression, decl, parser->scope,
3438 parser->integral_constant_expression_p,
3439 parser->allow_non_integral_constant_expression_p,
3440 &parser->non_integral_constant_expression_p,
3441 template_p, done, address_p,
3445 cp_parser_error (parser, error_msg);
3449 /* Anything else is an error. */
3451 /* ...unless we have an Objective-C++ message or string literal, that is. */
3452 if (c_dialect_objc ()
3453 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3454 return cp_parser_objc_expression (parser);
3456 cp_parser_error (parser, "expected primary-expression");
3457 return error_mark_node;
3461 /* Parse an id-expression.
3468 :: [opt] nested-name-specifier template [opt] unqualified-id
3470 :: operator-function-id
3473 Return a representation of the unqualified portion of the
3474 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3475 a `::' or nested-name-specifier.
3477 Often, if the id-expression was a qualified-id, the caller will
3478 want to make a SCOPE_REF to represent the qualified-id. This
3479 function does not do this in order to avoid wastefully creating
3480 SCOPE_REFs when they are not required.
3482 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3485 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3486 uninstantiated templates.
3488 If *TEMPLATE_P is non-NULL, it is set to true iff the
3489 `template' keyword is used to explicitly indicate that the entity
3490 named is a template.
3492 If DECLARATOR_P is true, the id-expression is appearing as part of
3493 a declarator, rather than as part of an expression. */
3496 cp_parser_id_expression (cp_parser *parser,
3497 bool template_keyword_p,
3498 bool check_dependency_p,
3503 bool global_scope_p;
3504 bool nested_name_specifier_p;
3506 /* Assume the `template' keyword was not used. */
3508 *template_p = template_keyword_p;
3510 /* Look for the optional `::' operator. */
3512 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3514 /* Look for the optional nested-name-specifier. */
3515 nested_name_specifier_p
3516 = (cp_parser_nested_name_specifier_opt (parser,
3517 /*typename_keyword_p=*/false,
3522 /* If there is a nested-name-specifier, then we are looking at
3523 the first qualified-id production. */
3524 if (nested_name_specifier_p)
3527 tree saved_object_scope;
3528 tree saved_qualifying_scope;
3529 tree unqualified_id;
3532 /* See if the next token is the `template' keyword. */
3534 template_p = &is_template;
3535 *template_p = cp_parser_optional_template_keyword (parser);
3536 /* Name lookup we do during the processing of the
3537 unqualified-id might obliterate SCOPE. */
3538 saved_scope = parser->scope;
3539 saved_object_scope = parser->object_scope;
3540 saved_qualifying_scope = parser->qualifying_scope;
3541 /* Process the final unqualified-id. */
3542 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3545 /*optional_p=*/false);
3546 /* Restore the SAVED_SCOPE for our caller. */
3547 parser->scope = saved_scope;
3548 parser->object_scope = saved_object_scope;
3549 parser->qualifying_scope = saved_qualifying_scope;
3551 return unqualified_id;
3553 /* Otherwise, if we are in global scope, then we are looking at one
3554 of the other qualified-id productions. */
3555 else if (global_scope_p)
3560 /* Peek at the next token. */
3561 token = cp_lexer_peek_token (parser->lexer);
3563 /* If it's an identifier, and the next token is not a "<", then
3564 we can avoid the template-id case. This is an optimization
3565 for this common case. */
3566 if (token->type == CPP_NAME
3567 && !cp_parser_nth_token_starts_template_argument_list_p
3569 return cp_parser_identifier (parser);
3571 cp_parser_parse_tentatively (parser);
3572 /* Try a template-id. */
3573 id = cp_parser_template_id (parser,
3574 /*template_keyword_p=*/false,
3575 /*check_dependency_p=*/true,
3577 /* If that worked, we're done. */
3578 if (cp_parser_parse_definitely (parser))
3581 /* Peek at the next token. (Changes in the token buffer may
3582 have invalidated the pointer obtained above.) */
3583 token = cp_lexer_peek_token (parser->lexer);
3585 switch (token->type)
3588 return cp_parser_identifier (parser);
3591 if (token->keyword == RID_OPERATOR)
3592 return cp_parser_operator_function_id (parser);
3596 cp_parser_error (parser, "expected id-expression");
3597 return error_mark_node;
3601 return cp_parser_unqualified_id (parser, template_keyword_p,
3602 /*check_dependency_p=*/true,
3607 /* Parse an unqualified-id.
3611 operator-function-id
3612 conversion-function-id
3616 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3617 keyword, in a construct like `A::template ...'.
3619 Returns a representation of unqualified-id. For the `identifier'
3620 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3621 production a BIT_NOT_EXPR is returned; the operand of the
3622 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3623 other productions, see the documentation accompanying the
3624 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3625 names are looked up in uninstantiated templates. If DECLARATOR_P
3626 is true, the unqualified-id is appearing as part of a declarator,
3627 rather than as part of an expression. */
3630 cp_parser_unqualified_id (cp_parser* parser,
3631 bool template_keyword_p,
3632 bool check_dependency_p,
3638 /* Peek at the next token. */
3639 token = cp_lexer_peek_token (parser->lexer);
3641 switch (token->type)
3647 /* We don't know yet whether or not this will be a
3649 cp_parser_parse_tentatively (parser);
3650 /* Try a template-id. */
3651 id = cp_parser_template_id (parser, template_keyword_p,
3654 /* If it worked, we're done. */
3655 if (cp_parser_parse_definitely (parser))
3657 /* Otherwise, it's an ordinary identifier. */
3658 return cp_parser_identifier (parser);
3661 case CPP_TEMPLATE_ID:
3662 return cp_parser_template_id (parser, template_keyword_p,
3669 tree qualifying_scope;
3674 /* Consume the `~' token. */
3675 cp_lexer_consume_token (parser->lexer);
3676 /* Parse the class-name. The standard, as written, seems to
3679 template <typename T> struct S { ~S (); };
3680 template <typename T> S<T>::~S() {}
3682 is invalid, since `~' must be followed by a class-name, but
3683 `S<T>' is dependent, and so not known to be a class.
3684 That's not right; we need to look in uninstantiated
3685 templates. A further complication arises from:
3687 template <typename T> void f(T t) {
3691 Here, it is not possible to look up `T' in the scope of `T'
3692 itself. We must look in both the current scope, and the
3693 scope of the containing complete expression.
3695 Yet another issue is:
3704 The standard does not seem to say that the `S' in `~S'
3705 should refer to the type `S' and not the data member
3708 /* DR 244 says that we look up the name after the "~" in the
3709 same scope as we looked up the qualifying name. That idea
3710 isn't fully worked out; it's more complicated than that. */
3711 scope = parser->scope;
3712 object_scope = parser->object_scope;
3713 qualifying_scope = parser->qualifying_scope;
3715 /* Check for invalid scopes. */
3716 if (scope == error_mark_node)
3718 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3719 cp_lexer_consume_token (parser->lexer);
3720 return error_mark_node;
3722 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3724 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3725 error ("scope %qT before %<~%> is not a class-name", scope);
3726 cp_parser_simulate_error (parser);
3727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3728 cp_lexer_consume_token (parser->lexer);
3729 return error_mark_node;
3731 gcc_assert (!scope || TYPE_P (scope));
3733 /* If the name is of the form "X::~X" it's OK. */
3734 token = cp_lexer_peek_token (parser->lexer);
3736 && token->type == CPP_NAME
3737 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3739 && constructor_name_p (token->u.value, scope))
3741 cp_lexer_consume_token (parser->lexer);
3742 return build_nt (BIT_NOT_EXPR, scope);
3745 /* If there was an explicit qualification (S::~T), first look
3746 in the scope given by the qualification (i.e., S). */
3748 type_decl = NULL_TREE;
3751 cp_parser_parse_tentatively (parser);
3752 type_decl = cp_parser_class_name (parser,
3753 /*typename_keyword_p=*/false,
3754 /*template_keyword_p=*/false,
3756 /*check_dependency=*/false,
3757 /*class_head_p=*/false,
3759 if (cp_parser_parse_definitely (parser))
3762 /* In "N::S::~S", look in "N" as well. */
3763 if (!done && scope && qualifying_scope)
3765 cp_parser_parse_tentatively (parser);
3766 parser->scope = qualifying_scope;
3767 parser->object_scope = NULL_TREE;
3768 parser->qualifying_scope = NULL_TREE;
3770 = cp_parser_class_name (parser,
3771 /*typename_keyword_p=*/false,
3772 /*template_keyword_p=*/false,
3774 /*check_dependency=*/false,
3775 /*class_head_p=*/false,
3777 if (cp_parser_parse_definitely (parser))
3780 /* In "p->S::~T", look in the scope given by "*p" as well. */
3781 else if (!done && object_scope)
3783 cp_parser_parse_tentatively (parser);
3784 parser->scope = object_scope;
3785 parser->object_scope = NULL_TREE;
3786 parser->qualifying_scope = NULL_TREE;
3788 = cp_parser_class_name (parser,
3789 /*typename_keyword_p=*/false,
3790 /*template_keyword_p=*/false,
3792 /*check_dependency=*/false,
3793 /*class_head_p=*/false,
3795 if (cp_parser_parse_definitely (parser))
3798 /* Look in the surrounding context. */
3801 parser->scope = NULL_TREE;
3802 parser->object_scope = NULL_TREE;
3803 parser->qualifying_scope = NULL_TREE;
3805 = cp_parser_class_name (parser,
3806 /*typename_keyword_p=*/false,
3807 /*template_keyword_p=*/false,
3809 /*check_dependency=*/false,
3810 /*class_head_p=*/false,
3813 /* If an error occurred, assume that the name of the
3814 destructor is the same as the name of the qualifying
3815 class. That allows us to keep parsing after running
3816 into ill-formed destructor names. */
3817 if (type_decl == error_mark_node && scope)
3818 return build_nt (BIT_NOT_EXPR, scope);
3819 else if (type_decl == error_mark_node)
3820 return error_mark_node;
3822 /* Check that destructor name and scope match. */
3823 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3825 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3826 error ("declaration of %<~%T%> as member of %qT",
3828 cp_parser_simulate_error (parser);
3829 return error_mark_node;
3834 A typedef-name that names a class shall not be used as the
3835 identifier in the declarator for a destructor declaration. */
3837 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3838 && !DECL_SELF_REFERENCE_P (type_decl)
3839 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3840 error ("typedef-name %qD used as destructor declarator",
3843 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3847 if (token->keyword == RID_OPERATOR)
3851 /* This could be a template-id, so we try that first. */
3852 cp_parser_parse_tentatively (parser);
3853 /* Try a template-id. */
3854 id = cp_parser_template_id (parser, template_keyword_p,
3855 /*check_dependency_p=*/true,
3857 /* If that worked, we're done. */
3858 if (cp_parser_parse_definitely (parser))
3860 /* We still don't know whether we're looking at an
3861 operator-function-id or a conversion-function-id. */
3862 cp_parser_parse_tentatively (parser);
3863 /* Try an operator-function-id. */
3864 id = cp_parser_operator_function_id (parser);
3865 /* If that didn't work, try a conversion-function-id. */
3866 if (!cp_parser_parse_definitely (parser))
3867 id = cp_parser_conversion_function_id (parser);
3876 cp_parser_error (parser, "expected unqualified-id");
3877 return error_mark_node;
3881 /* Parse an (optional) nested-name-specifier.
3883 nested-name-specifier:
3884 class-or-namespace-name :: nested-name-specifier [opt]
3885 class-or-namespace-name :: template nested-name-specifier [opt]
3887 PARSER->SCOPE should be set appropriately before this function is
3888 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3889 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3892 Sets PARSER->SCOPE to the class (TYPE) or namespace
3893 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3894 it unchanged if there is no nested-name-specifier. Returns the new
3895 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3897 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3898 part of a declaration and/or decl-specifier. */
3901 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3902 bool typename_keyword_p,
3903 bool check_dependency_p,
3905 bool is_declaration)
3907 bool success = false;
3908 cp_token_position start = 0;
3911 /* Remember where the nested-name-specifier starts. */
3912 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3914 start = cp_lexer_token_position (parser->lexer, false);
3915 push_deferring_access_checks (dk_deferred);
3922 tree saved_qualifying_scope;
3923 bool template_keyword_p;
3925 /* Spot cases that cannot be the beginning of a
3926 nested-name-specifier. */
3927 token = cp_lexer_peek_token (parser->lexer);
3929 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3930 the already parsed nested-name-specifier. */
3931 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3933 /* Grab the nested-name-specifier and continue the loop. */
3934 cp_parser_pre_parsed_nested_name_specifier (parser);
3935 /* If we originally encountered this nested-name-specifier
3936 with IS_DECLARATION set to false, we will not have
3937 resolved TYPENAME_TYPEs, so we must do so here. */
3939 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3941 new_scope = resolve_typename_type (parser->scope,
3942 /*only_current_p=*/false);
3943 if (new_scope != error_mark_node)
3944 parser->scope = new_scope;
3950 /* Spot cases that cannot be the beginning of a
3951 nested-name-specifier. On the second and subsequent times
3952 through the loop, we look for the `template' keyword. */
3953 if (success && token->keyword == RID_TEMPLATE)
3955 /* A template-id can start a nested-name-specifier. */
3956 else if (token->type == CPP_TEMPLATE_ID)
3960 /* If the next token is not an identifier, then it is
3961 definitely not a class-or-namespace-name. */
3962 if (token->type != CPP_NAME)
3964 /* If the following token is neither a `<' (to begin a
3965 template-id), nor a `::', then we are not looking at a
3966 nested-name-specifier. */
3967 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3968 if (token->type != CPP_SCOPE
3969 && !cp_parser_nth_token_starts_template_argument_list_p
3974 /* The nested-name-specifier is optional, so we parse
3976 cp_parser_parse_tentatively (parser);
3978 /* Look for the optional `template' keyword, if this isn't the
3979 first time through the loop. */
3981 template_keyword_p = cp_parser_optional_template_keyword (parser);
3983 template_keyword_p = false;
3985 /* Save the old scope since the name lookup we are about to do
3986 might destroy it. */
3987 old_scope = parser->scope;
3988 saved_qualifying_scope = parser->qualifying_scope;
3989 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3990 look up names in "X<T>::I" in order to determine that "Y" is
3991 a template. So, if we have a typename at this point, we make
3992 an effort to look through it. */
3994 && !typename_keyword_p
3996 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3997 parser->scope = resolve_typename_type (parser->scope,
3998 /*only_current_p=*/false);
3999 /* Parse the qualifying entity. */
4001 = cp_parser_class_or_namespace_name (parser,
4007 /* Look for the `::' token. */
4008 cp_parser_require (parser, CPP_SCOPE, "`::'");
4010 /* If we found what we wanted, we keep going; otherwise, we're
4012 if (!cp_parser_parse_definitely (parser))
4014 bool error_p = false;
4016 /* Restore the OLD_SCOPE since it was valid before the
4017 failed attempt at finding the last
4018 class-or-namespace-name. */
4019 parser->scope = old_scope;
4020 parser->qualifying_scope = saved_qualifying_scope;
4021 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4023 /* If the next token is an identifier, and the one after
4024 that is a `::', then any valid interpretation would have
4025 found a class-or-namespace-name. */
4026 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4027 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4029 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4032 token = cp_lexer_consume_token (parser->lexer);
4035 if (!token->ambiguous_p)
4038 tree ambiguous_decls;
4040 decl = cp_parser_lookup_name (parser, token->u.value,
4042 /*is_template=*/false,
4043 /*is_namespace=*/false,
4044 /*check_dependency=*/true,
4046 if (TREE_CODE (decl) == TEMPLATE_DECL)
4047 error ("%qD used without template parameters", decl);
4048 else if (ambiguous_decls)
4050 error ("reference to %qD is ambiguous",
4052 print_candidates (ambiguous_decls);
4053 decl = error_mark_node;
4056 cp_parser_name_lookup_error
4057 (parser, token->u.value, decl,
4058 "is not a class or namespace");
4060 parser->scope = error_mark_node;
4062 /* Treat this as a successful nested-name-specifier
4067 If the name found is not a class-name (clause
4068 _class_) or namespace-name (_namespace.def_), the
4069 program is ill-formed. */
4072 cp_lexer_consume_token (parser->lexer);
4076 /* We've found one valid nested-name-specifier. */
4078 /* Name lookup always gives us a DECL. */
4079 if (TREE_CODE (new_scope) == TYPE_DECL)
4080 new_scope = TREE_TYPE (new_scope);
4081 /* Uses of "template" must be followed by actual templates. */
4082 if (template_keyword_p
4083 && !(CLASS_TYPE_P (new_scope)
4084 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4085 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4086 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4087 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4088 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4089 == TEMPLATE_ID_EXPR)))
4090 pedwarn (TYPE_P (new_scope)
4091 ? "%qT is not a template"
4092 : "%qD is not a template",
4094 /* If it is a class scope, try to complete it; we are about to
4095 be looking up names inside the class. */
4096 if (TYPE_P (new_scope)
4097 /* Since checking types for dependency can be expensive,
4098 avoid doing it if the type is already complete. */
4099 && !COMPLETE_TYPE_P (new_scope)
4100 /* Do not try to complete dependent types. */
4101 && !dependent_type_p (new_scope))
4102 new_scope = complete_type (new_scope);
4103 /* Make sure we look in the right scope the next time through
4105 parser->scope = new_scope;
4108 /* If parsing tentatively, replace the sequence of tokens that makes
4109 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4110 token. That way, should we re-parse the token stream, we will
4111 not have to repeat the effort required to do the parse, nor will
4112 we issue duplicate error messages. */
4113 if (success && start)
4117 token = cp_lexer_token_at (parser->lexer, start);
4118 /* Reset the contents of the START token. */
4119 token->type = CPP_NESTED_NAME_SPECIFIER;
4120 /* Retrieve any deferred checks. Do not pop this access checks yet
4121 so the memory will not be reclaimed during token replacing below. */
4122 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4123 token->u.tree_check_value->value = parser->scope;
4124 token->u.tree_check_value->checks = get_deferred_access_checks ();
4125 token->u.tree_check_value->qualifying_scope =
4126 parser->qualifying_scope;
4127 token->keyword = RID_MAX;
4129 /* Purge all subsequent tokens. */
4130 cp_lexer_purge_tokens_after (parser->lexer, start);
4134 pop_to_parent_deferring_access_checks ();
4136 return success ? parser->scope : NULL_TREE;
4139 /* Parse a nested-name-specifier. See
4140 cp_parser_nested_name_specifier_opt for details. This function
4141 behaves identically, except that it will an issue an error if no
4142 nested-name-specifier is present. */
4145 cp_parser_nested_name_specifier (cp_parser *parser,
4146 bool typename_keyword_p,
4147 bool check_dependency_p,
4149 bool is_declaration)
4153 /* Look for the nested-name-specifier. */
4154 scope = cp_parser_nested_name_specifier_opt (parser,
4159 /* If it was not present, issue an error message. */
4162 cp_parser_error (parser, "expected nested-name-specifier");
4163 parser->scope = NULL_TREE;
4169 /* Parse a class-or-namespace-name.
4171 class-or-namespace-name:
4175 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4176 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4177 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4178 TYPE_P is TRUE iff the next name should be taken as a class-name,
4179 even the same name is declared to be another entity in the same
4182 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4183 specified by the class-or-namespace-name. If neither is found the
4184 ERROR_MARK_NODE is returned. */
4187 cp_parser_class_or_namespace_name (cp_parser *parser,
4188 bool typename_keyword_p,
4189 bool template_keyword_p,
4190 bool check_dependency_p,
4192 bool is_declaration)
4195 tree saved_qualifying_scope;
4196 tree saved_object_scope;
4200 /* Before we try to parse the class-name, we must save away the
4201 current PARSER->SCOPE since cp_parser_class_name will destroy
4203 saved_scope = parser->scope;
4204 saved_qualifying_scope = parser->qualifying_scope;
4205 saved_object_scope = parser->object_scope;
4206 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4207 there is no need to look for a namespace-name. */
4208 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4210 cp_parser_parse_tentatively (parser);
4211 scope = cp_parser_class_name (parser,
4214 type_p ? class_type : none_type,
4216 /*class_head_p=*/false,
4218 /* If that didn't work, try for a namespace-name. */
4219 if (!only_class_p && !cp_parser_parse_definitely (parser))
4221 /* Restore the saved scope. */
4222 parser->scope = saved_scope;
4223 parser->qualifying_scope = saved_qualifying_scope;
4224 parser->object_scope = saved_object_scope;
4225 /* If we are not looking at an identifier followed by the scope
4226 resolution operator, then this is not part of a
4227 nested-name-specifier. (Note that this function is only used
4228 to parse the components of a nested-name-specifier.) */
4229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4230 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4231 return error_mark_node;
4232 scope = cp_parser_namespace_name (parser);
4238 /* Parse a postfix-expression.
4242 postfix-expression [ expression ]
4243 postfix-expression ( expression-list [opt] )
4244 simple-type-specifier ( expression-list [opt] )
4245 typename :: [opt] nested-name-specifier identifier
4246 ( expression-list [opt] )
4247 typename :: [opt] nested-name-specifier template [opt] template-id
4248 ( expression-list [opt] )
4249 postfix-expression . template [opt] id-expression
4250 postfix-expression -> template [opt] id-expression
4251 postfix-expression . pseudo-destructor-name
4252 postfix-expression -> pseudo-destructor-name
4253 postfix-expression ++
4254 postfix-expression --
4255 dynamic_cast < type-id > ( expression )
4256 static_cast < type-id > ( expression )
4257 reinterpret_cast < type-id > ( expression )
4258 const_cast < type-id > ( expression )
4259 typeid ( expression )
4265 ( type-id ) { initializer-list , [opt] }
4267 This extension is a GNU version of the C99 compound-literal
4268 construct. (The C99 grammar uses `type-name' instead of `type-id',
4269 but they are essentially the same concept.)
4271 If ADDRESS_P is true, the postfix expression is the operand of the
4272 `&' operator. CAST_P is true if this expression is the target of a
4275 Returns a representation of the expression. */
4278 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4282 cp_id_kind idk = CP_ID_KIND_NONE;
4283 tree postfix_expression = NULL_TREE;
4285 /* Peek at the next token. */
4286 token = cp_lexer_peek_token (parser->lexer);
4287 /* Some of the productions are determined by keywords. */
4288 keyword = token->keyword;
4298 const char *saved_message;
4300 /* All of these can be handled in the same way from the point
4301 of view of parsing. Begin by consuming the token
4302 identifying the cast. */
4303 cp_lexer_consume_token (parser->lexer);
4305 /* New types cannot be defined in the cast. */
4306 saved_message = parser->type_definition_forbidden_message;
4307 parser->type_definition_forbidden_message
4308 = "types may not be defined in casts";
4310 /* Look for the opening `<'. */
4311 cp_parser_require (parser, CPP_LESS, "`<'");
4312 /* Parse the type to which we are casting. */
4313 type = cp_parser_type_id (parser);
4314 /* Look for the closing `>'. */
4315 cp_parser_require (parser, CPP_GREATER, "`>'");
4316 /* Restore the old message. */
4317 parser->type_definition_forbidden_message = saved_message;
4319 /* And the expression which is being cast. */
4320 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4321 expression = cp_parser_expression (parser, /*cast_p=*/true);
4322 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4324 /* Only type conversions to integral or enumeration types
4325 can be used in constant-expressions. */
4326 if (!cast_valid_in_integral_constant_expression_p (type)
4327 && (cp_parser_non_integral_constant_expression
4329 "a cast to a type other than an integral or "
4330 "enumeration type")))
4331 return error_mark_node;
4337 = build_dynamic_cast (type, expression);
4341 = build_static_cast (type, expression);
4345 = build_reinterpret_cast (type, expression);
4349 = build_const_cast (type, expression);
4360 const char *saved_message;
4361 bool saved_in_type_id_in_expr_p;
4363 /* Consume the `typeid' token. */
4364 cp_lexer_consume_token (parser->lexer);
4365 /* Look for the `(' token. */
4366 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4367 /* Types cannot be defined in a `typeid' expression. */
4368 saved_message = parser->type_definition_forbidden_message;
4369 parser->type_definition_forbidden_message
4370 = "types may not be defined in a `typeid\' expression";
4371 /* We can't be sure yet whether we're looking at a type-id or an
4373 cp_parser_parse_tentatively (parser);
4374 /* Try a type-id first. */
4375 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4376 parser->in_type_id_in_expr_p = true;
4377 type = cp_parser_type_id (parser);
4378 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4379 /* Look for the `)' token. Otherwise, we can't be sure that
4380 we're not looking at an expression: consider `typeid (int
4381 (3))', for example. */
4382 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4383 /* If all went well, simply lookup the type-id. */
4384 if (cp_parser_parse_definitely (parser))
4385 postfix_expression = get_typeid (type);
4386 /* Otherwise, fall back to the expression variant. */
4391 /* Look for an expression. */
4392 expression = cp_parser_expression (parser, /*cast_p=*/false);
4393 /* Compute its typeid. */
4394 postfix_expression = build_typeid (expression);
4395 /* Look for the `)' token. */
4396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4398 /* Restore the saved message. */
4399 parser->type_definition_forbidden_message = saved_message;
4400 /* `typeid' may not appear in an integral constant expression. */
4401 if (cp_parser_non_integral_constant_expression(parser,
4402 "`typeid' operator"))
4403 return error_mark_node;
4410 /* The syntax permitted here is the same permitted for an
4411 elaborated-type-specifier. */
4412 type = cp_parser_elaborated_type_specifier (parser,
4413 /*is_friend=*/false,
4414 /*is_declaration=*/false);
4415 postfix_expression = cp_parser_functional_cast (parser, type);
4423 /* If the next thing is a simple-type-specifier, we may be
4424 looking at a functional cast. We could also be looking at
4425 an id-expression. So, we try the functional cast, and if
4426 that doesn't work we fall back to the primary-expression. */
4427 cp_parser_parse_tentatively (parser);
4428 /* Look for the simple-type-specifier. */
4429 type = cp_parser_simple_type_specifier (parser,
4430 /*decl_specs=*/NULL,
4431 CP_PARSER_FLAGS_NONE);
4432 /* Parse the cast itself. */
4433 if (!cp_parser_error_occurred (parser))
4435 = cp_parser_functional_cast (parser, type);
4436 /* If that worked, we're done. */
4437 if (cp_parser_parse_definitely (parser))
4440 /* If the functional-cast didn't work out, try a
4441 compound-literal. */
4442 if (cp_parser_allow_gnu_extensions_p (parser)
4443 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4445 VEC(constructor_elt,gc) *initializer_list = NULL;
4446 bool saved_in_type_id_in_expr_p;
4448 cp_parser_parse_tentatively (parser);
4449 /* Consume the `('. */
4450 cp_lexer_consume_token (parser->lexer);
4451 /* Parse the type. */
4452 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4453 parser->in_type_id_in_expr_p = true;
4454 type = cp_parser_type_id (parser);
4455 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4456 /* Look for the `)'. */
4457 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4458 /* Look for the `{'. */
4459 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4460 /* If things aren't going well, there's no need to
4462 if (!cp_parser_error_occurred (parser))
4464 bool non_constant_p;
4465 /* Parse the initializer-list. */
4467 = cp_parser_initializer_list (parser, &non_constant_p);
4468 /* Allow a trailing `,'. */
4469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4470 cp_lexer_consume_token (parser->lexer);
4471 /* Look for the final `}'. */
4472 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4474 /* If that worked, we're definitely looking at a
4475 compound-literal expression. */
4476 if (cp_parser_parse_definitely (parser))
4478 /* Warn the user that a compound literal is not
4479 allowed in standard C++. */
4481 pedwarn ("ISO C++ forbids compound-literals");
4482 /* For simplicitly, we disallow compound literals in
4483 constant-expressions for simpliicitly. We could
4484 allow compound literals of integer type, whose
4485 initializer was a constant, in constant
4486 expressions. Permitting that usage, as a further
4487 extension, would not change the meaning of any
4488 currently accepted programs. (Of course, as
4489 compound literals are not part of ISO C++, the
4490 standard has nothing to say.) */
4491 if (cp_parser_non_integral_constant_expression
4492 (parser, "non-constant compound literals"))
4494 postfix_expression = error_mark_node;
4497 /* Form the representation of the compound-literal. */
4499 = finish_compound_literal (type, initializer_list);
4504 /* It must be a primary-expression. */
4506 = cp_parser_primary_expression (parser, address_p, cast_p,
4507 /*template_arg_p=*/false,
4513 /* Keep looping until the postfix-expression is complete. */
4516 if (idk == CP_ID_KIND_UNQUALIFIED
4517 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4518 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4519 /* It is not a Koenig lookup function call. */
4521 = unqualified_name_lookup_error (postfix_expression);
4523 /* Peek at the next token. */
4524 token = cp_lexer_peek_token (parser->lexer);
4526 switch (token->type)
4528 case CPP_OPEN_SQUARE:
4530 = cp_parser_postfix_open_square_expression (parser,
4533 idk = CP_ID_KIND_NONE;
4536 case CPP_OPEN_PAREN:
4537 /* postfix-expression ( expression-list [opt] ) */
4540 bool is_builtin_constant_p;
4541 bool saved_integral_constant_expression_p = false;
4542 bool saved_non_integral_constant_expression_p = false;
4545 is_builtin_constant_p
4546 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4547 if (is_builtin_constant_p)
4549 /* The whole point of __builtin_constant_p is to allow
4550 non-constant expressions to appear as arguments. */
4551 saved_integral_constant_expression_p
4552 = parser->integral_constant_expression_p;
4553 saved_non_integral_constant_expression_p
4554 = parser->non_integral_constant_expression_p;
4555 parser->integral_constant_expression_p = false;
4557 args = (cp_parser_parenthesized_expression_list
4558 (parser, /*is_attribute_list=*/false,
4560 /*non_constant_p=*/NULL));
4561 if (is_builtin_constant_p)
4563 parser->integral_constant_expression_p
4564 = saved_integral_constant_expression_p;
4565 parser->non_integral_constant_expression_p
4566 = saved_non_integral_constant_expression_p;
4569 if (args == error_mark_node)
4571 postfix_expression = error_mark_node;
4575 /* Function calls are not permitted in
4576 constant-expressions. */
4577 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4578 && cp_parser_non_integral_constant_expression (parser,
4581 postfix_expression = error_mark_node;
4586 if (idk == CP_ID_KIND_UNQUALIFIED)
4588 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4594 = perform_koenig_lookup (postfix_expression, args);
4598 = unqualified_fn_lookup_error (postfix_expression);
4600 /* We do not perform argument-dependent lookup if
4601 normal lookup finds a non-function, in accordance
4602 with the expected resolution of DR 218. */
4603 else if (args && is_overloaded_fn (postfix_expression))
4605 tree fn = get_first_fn (postfix_expression);
4607 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4608 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4610 /* Only do argument dependent lookup if regular
4611 lookup does not find a set of member functions.
4612 [basic.lookup.koenig]/2a */
4613 if (!DECL_FUNCTION_MEMBER_P (fn))
4617 = perform_koenig_lookup (postfix_expression, args);
4622 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4624 tree instance = TREE_OPERAND (postfix_expression, 0);
4625 tree fn = TREE_OPERAND (postfix_expression, 1);
4627 if (processing_template_decl
4628 && (type_dependent_expression_p (instance)
4629 || (!BASELINK_P (fn)
4630 && TREE_CODE (fn) != FIELD_DECL)
4631 || type_dependent_expression_p (fn)
4632 || any_type_dependent_arguments_p (args)))
4635 = build_min_nt (CALL_EXPR, postfix_expression,
4640 if (BASELINK_P (fn))
4642 = (build_new_method_call
4643 (instance, fn, args, NULL_TREE,
4644 (idk == CP_ID_KIND_QUALIFIED
4645 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4649 = finish_call_expr (postfix_expression, args,
4650 /*disallow_virtual=*/false,
4651 /*koenig_p=*/false);
4653 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4654 || TREE_CODE (postfix_expression) == MEMBER_REF
4655 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4656 postfix_expression = (build_offset_ref_call_from_tree
4657 (postfix_expression, args));
4658 else if (idk == CP_ID_KIND_QUALIFIED)
4659 /* A call to a static class member, or a namespace-scope
4662 = finish_call_expr (postfix_expression, args,
4663 /*disallow_virtual=*/true,
4666 /* All other function calls. */
4668 = finish_call_expr (postfix_expression, args,
4669 /*disallow_virtual=*/false,
4672 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4673 idk = CP_ID_KIND_NONE;
4679 /* postfix-expression . template [opt] id-expression
4680 postfix-expression . pseudo-destructor-name
4681 postfix-expression -> template [opt] id-expression
4682 postfix-expression -> pseudo-destructor-name */
4684 /* Consume the `.' or `->' operator. */
4685 cp_lexer_consume_token (parser->lexer);
4688 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4694 /* postfix-expression ++ */
4695 /* Consume the `++' token. */
4696 cp_lexer_consume_token (parser->lexer);
4697 /* Generate a representation for the complete expression. */
4699 = finish_increment_expr (postfix_expression,
4700 POSTINCREMENT_EXPR);
4701 /* Increments may not appear in constant-expressions. */
4702 if (cp_parser_non_integral_constant_expression (parser,
4704 postfix_expression = error_mark_node;
4705 idk = CP_ID_KIND_NONE;
4708 case CPP_MINUS_MINUS:
4709 /* postfix-expression -- */
4710 /* Consume the `--' token. */
4711 cp_lexer_consume_token (parser->lexer);
4712 /* Generate a representation for the complete expression. */
4714 = finish_increment_expr (postfix_expression,
4715 POSTDECREMENT_EXPR);
4716 /* Decrements may not appear in constant-expressions. */
4717 if (cp_parser_non_integral_constant_expression (parser,
4719 postfix_expression = error_mark_node;
4720 idk = CP_ID_KIND_NONE;
4724 return postfix_expression;
4728 /* We should never get here. */
4730 return error_mark_node;
4733 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4734 by cp_parser_builtin_offsetof. We're looking for
4736 postfix-expression [ expression ]
4738 FOR_OFFSETOF is set if we're being called in that context, which
4739 changes how we deal with integer constant expressions. */
4742 cp_parser_postfix_open_square_expression (cp_parser *parser,
4743 tree postfix_expression,
4748 /* Consume the `[' token. */
4749 cp_lexer_consume_token (parser->lexer);
4751 /* Parse the index expression. */
4752 /* ??? For offsetof, there is a question of what to allow here. If
4753 offsetof is not being used in an integral constant expression context,
4754 then we *could* get the right answer by computing the value at runtime.
4755 If we are in an integral constant expression context, then we might
4756 could accept any constant expression; hard to say without analysis.
4757 Rather than open the barn door too wide right away, allow only integer
4758 constant expressions here. */
4760 index = cp_parser_constant_expression (parser, false, NULL);
4762 index = cp_parser_expression (parser, /*cast_p=*/false);
4764 /* Look for the closing `]'. */
4765 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4767 /* Build the ARRAY_REF. */
4768 postfix_expression = grok_array_decl (postfix_expression, index);
4770 /* When not doing offsetof, array references are not permitted in
4771 constant-expressions. */
4773 && (cp_parser_non_integral_constant_expression
4774 (parser, "an array reference")))
4775 postfix_expression = error_mark_node;
4777 return postfix_expression;
4780 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4781 by cp_parser_builtin_offsetof. We're looking for
4783 postfix-expression . template [opt] id-expression
4784 postfix-expression . pseudo-destructor-name
4785 postfix-expression -> template [opt] id-expression
4786 postfix-expression -> pseudo-destructor-name
4788 FOR_OFFSETOF is set if we're being called in that context. That sorta
4789 limits what of the above we'll actually accept, but nevermind.
4790 TOKEN_TYPE is the "." or "->" token, which will already have been
4791 removed from the stream. */
4794 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4795 enum cpp_ttype token_type,
4796 tree postfix_expression,
4797 bool for_offsetof, cp_id_kind *idk)
4801 bool pseudo_destructor_p;
4802 tree scope = NULL_TREE;
4804 /* If this is a `->' operator, dereference the pointer. */
4805 if (token_type == CPP_DEREF)
4806 postfix_expression = build_x_arrow (postfix_expression);
4807 /* Check to see whether or not the expression is type-dependent. */
4808 dependent_p = type_dependent_expression_p (postfix_expression);
4809 /* The identifier following the `->' or `.' is not qualified. */
4810 parser->scope = NULL_TREE;
4811 parser->qualifying_scope = NULL_TREE;
4812 parser->object_scope = NULL_TREE;
4813 *idk = CP_ID_KIND_NONE;
4814 /* Enter the scope corresponding to the type of the object
4815 given by the POSTFIX_EXPRESSION. */
4816 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4818 scope = TREE_TYPE (postfix_expression);
4819 /* According to the standard, no expression should ever have
4820 reference type. Unfortunately, we do not currently match
4821 the standard in this respect in that our internal representation
4822 of an expression may have reference type even when the standard
4823 says it does not. Therefore, we have to manually obtain the
4824 underlying type here. */
4825 scope = non_reference (scope);
4826 /* The type of the POSTFIX_EXPRESSION must be complete. */
4827 if (scope == unknown_type_node)
4829 error ("%qE does not have class type", postfix_expression);
4833 scope = complete_type_or_else (scope, NULL_TREE);
4834 /* Let the name lookup machinery know that we are processing a
4835 class member access expression. */
4836 parser->context->object_type = scope;
4837 /* If something went wrong, we want to be able to discern that case,
4838 as opposed to the case where there was no SCOPE due to the type
4839 of expression being dependent. */
4841 scope = error_mark_node;
4842 /* If the SCOPE was erroneous, make the various semantic analysis
4843 functions exit quickly -- and without issuing additional error
4845 if (scope == error_mark_node)
4846 postfix_expression = error_mark_node;
4849 /* Assume this expression is not a pseudo-destructor access. */
4850 pseudo_destructor_p = false;
4852 /* If the SCOPE is a scalar type, then, if this is a valid program,
4853 we must be looking at a pseudo-destructor-name. */
4854 if (scope && SCALAR_TYPE_P (scope))
4859 cp_parser_parse_tentatively (parser);
4860 /* Parse the pseudo-destructor-name. */
4862 cp_parser_pseudo_destructor_name (parser, &s, &type);
4863 if (cp_parser_parse_definitely (parser))
4865 pseudo_destructor_p = true;
4867 = finish_pseudo_destructor_expr (postfix_expression,
4868 s, TREE_TYPE (type));
4872 if (!pseudo_destructor_p)
4874 /* If the SCOPE is not a scalar type, we are looking at an
4875 ordinary class member access expression, rather than a
4876 pseudo-destructor-name. */
4878 /* Parse the id-expression. */
4879 name = (cp_parser_id_expression
4881 cp_parser_optional_template_keyword (parser),
4882 /*check_dependency_p=*/true,
4884 /*declarator_p=*/false,
4885 /*optional_p=*/false));
4886 /* In general, build a SCOPE_REF if the member name is qualified.
4887 However, if the name was not dependent and has already been
4888 resolved; there is no need to build the SCOPE_REF. For example;
4890 struct X { void f(); };
4891 template <typename T> void f(T* t) { t->X::f(); }
4893 Even though "t" is dependent, "X::f" is not and has been resolved
4894 to a BASELINK; there is no need to include scope information. */
4896 /* But we do need to remember that there was an explicit scope for
4897 virtual function calls. */
4899 *idk = CP_ID_KIND_QUALIFIED;
4901 /* If the name is a template-id that names a type, we will get a
4902 TYPE_DECL here. That is invalid code. */
4903 if (TREE_CODE (name) == TYPE_DECL)
4905 error ("invalid use of %qD", name);
4906 postfix_expression = error_mark_node;
4910 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4912 name = build_qualified_name (/*type=*/NULL_TREE,
4916 parser->scope = NULL_TREE;
4917 parser->qualifying_scope = NULL_TREE;
4918 parser->object_scope = NULL_TREE;
4920 if (scope && name && BASELINK_P (name))
4921 adjust_result_of_qualified_name_lookup
4922 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4924 = finish_class_member_access_expr (postfix_expression, name,
4929 /* We no longer need to look up names in the scope of the object on
4930 the left-hand side of the `.' or `->' operator. */
4931 parser->context->object_type = NULL_TREE;
4933 /* Outside of offsetof, these operators may not appear in
4934 constant-expressions. */
4936 && (cp_parser_non_integral_constant_expression
4937 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4938 postfix_expression = error_mark_node;
4940 return postfix_expression;
4943 /* Parse a parenthesized expression-list.
4946 assignment-expression
4947 expression-list, assignment-expression
4952 identifier, expression-list
4954 CAST_P is true if this expression is the target of a cast.
4956 Returns a TREE_LIST. The TREE_VALUE of each node is a
4957 representation of an assignment-expression. Note that a TREE_LIST
4958 is returned even if there is only a single expression in the list.
4959 error_mark_node is returned if the ( and or ) are
4960 missing. NULL_TREE is returned on no expressions. The parentheses
4961 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4962 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4963 indicates whether or not all of the expressions in the list were
4967 cp_parser_parenthesized_expression_list (cp_parser* parser,
4968 bool is_attribute_list,
4970 bool *non_constant_p)
4972 tree expression_list = NULL_TREE;
4973 bool fold_expr_p = is_attribute_list;
4974 tree identifier = NULL_TREE;
4976 /* Assume all the expressions will be constant. */
4978 *non_constant_p = false;
4980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4981 return error_mark_node;
4983 /* Consume expressions until there are no more. */
4984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4989 /* At the beginning of attribute lists, check to see if the
4990 next token is an identifier. */
4991 if (is_attribute_list
4992 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4996 /* Consume the identifier. */
4997 token = cp_lexer_consume_token (parser->lexer);
4998 /* Save the identifier. */
4999 identifier = token->u.value;
5003 /* Parse the next assignment-expression. */
5006 bool expr_non_constant_p;
5007 expr = (cp_parser_constant_expression
5008 (parser, /*allow_non_constant_p=*/true,
5009 &expr_non_constant_p));
5010 if (expr_non_constant_p)
5011 *non_constant_p = true;
5014 expr = cp_parser_assignment_expression (parser, cast_p);
5017 expr = fold_non_dependent_expr (expr);
5019 /* Add it to the list. We add error_mark_node
5020 expressions to the list, so that we can still tell if
5021 the correct form for a parenthesized expression-list
5022 is found. That gives better errors. */
5023 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5025 if (expr == error_mark_node)
5029 /* After the first item, attribute lists look the same as
5030 expression lists. */
5031 is_attribute_list = false;
5034 /* If the next token isn't a `,', then we are done. */
5035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5038 /* Otherwise, consume the `,' and keep going. */
5039 cp_lexer_consume_token (parser->lexer);
5042 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5047 /* We try and resync to an unnested comma, as that will give the
5048 user better diagnostics. */
5049 ending = cp_parser_skip_to_closing_parenthesis (parser,
5050 /*recovering=*/true,
5052 /*consume_paren=*/true);
5056 return error_mark_node;
5059 /* We built up the list in reverse order so we must reverse it now. */
5060 expression_list = nreverse (expression_list);
5062 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5064 return expression_list;
5067 /* Parse a pseudo-destructor-name.
5069 pseudo-destructor-name:
5070 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5071 :: [opt] nested-name-specifier template template-id :: ~ type-name
5072 :: [opt] nested-name-specifier [opt] ~ type-name
5074 If either of the first two productions is used, sets *SCOPE to the
5075 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5076 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5077 or ERROR_MARK_NODE if the parse fails. */
5080 cp_parser_pseudo_destructor_name (cp_parser* parser,
5084 bool nested_name_specifier_p;
5086 /* Assume that things will not work out. */
5087 *type = error_mark_node;
5089 /* Look for the optional `::' operator. */
5090 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5091 /* Look for the optional nested-name-specifier. */
5092 nested_name_specifier_p
5093 = (cp_parser_nested_name_specifier_opt (parser,
5094 /*typename_keyword_p=*/false,
5095 /*check_dependency_p=*/true,
5097 /*is_declaration=*/true)
5099 /* Now, if we saw a nested-name-specifier, we might be doing the
5100 second production. */
5101 if (nested_name_specifier_p
5102 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5104 /* Consume the `template' keyword. */
5105 cp_lexer_consume_token (parser->lexer);
5106 /* Parse the template-id. */
5107 cp_parser_template_id (parser,
5108 /*template_keyword_p=*/true,
5109 /*check_dependency_p=*/false,
5110 /*is_declaration=*/true);
5111 /* Look for the `::' token. */
5112 cp_parser_require (parser, CPP_SCOPE, "`::'");
5114 /* If the next token is not a `~', then there might be some
5115 additional qualification. */
5116 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5118 /* Look for the type-name. */
5119 *scope = TREE_TYPE (cp_parser_type_name (parser));
5121 if (*scope == error_mark_node)
5124 /* If we don't have ::~, then something has gone wrong. Since
5125 the only caller of this function is looking for something
5126 after `.' or `->' after a scalar type, most likely the
5127 program is trying to get a member of a non-aggregate
5129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5130 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5132 cp_parser_error (parser, "request for member of non-aggregate type");
5136 /* Look for the `::' token. */
5137 cp_parser_require (parser, CPP_SCOPE, "`::'");
5142 /* Look for the `~'. */
5143 cp_parser_require (parser, CPP_COMPL, "`~'");
5144 /* Look for the type-name again. We are not responsible for
5145 checking that it matches the first type-name. */
5146 *type = cp_parser_type_name (parser);
5149 /* Parse a unary-expression.
5155 unary-operator cast-expression
5156 sizeof unary-expression
5164 __extension__ cast-expression
5165 __alignof__ unary-expression
5166 __alignof__ ( type-id )
5167 __real__ cast-expression
5168 __imag__ cast-expression
5171 ADDRESS_P is true iff the unary-expression is appearing as the
5172 operand of the `&' operator. CAST_P is true if this expression is
5173 the target of a cast.
5175 Returns a representation of the expression. */
5178 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5181 enum tree_code unary_operator;
5183 /* Peek at the next token. */
5184 token = cp_lexer_peek_token (parser->lexer);
5185 /* Some keywords give away the kind of expression. */
5186 if (token->type == CPP_KEYWORD)
5188 enum rid keyword = token->keyword;
5198 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5199 /* Consume the token. */
5200 cp_lexer_consume_token (parser->lexer);
5201 /* Parse the operand. */
5202 operand = cp_parser_sizeof_operand (parser, keyword);
5204 if (TYPE_P (operand))
5205 return cxx_sizeof_or_alignof_type (operand, op, true);
5207 return cxx_sizeof_or_alignof_expr (operand, op);
5211 return cp_parser_new_expression (parser);
5214 return cp_parser_delete_expression (parser);
5218 /* The saved value of the PEDANTIC flag. */
5222 /* Save away the PEDANTIC flag. */
5223 cp_parser_extension_opt (parser, &saved_pedantic);
5224 /* Parse the cast-expression. */
5225 expr = cp_parser_simple_cast_expression (parser);
5226 /* Restore the PEDANTIC flag. */
5227 pedantic = saved_pedantic;
5237 /* Consume the `__real__' or `__imag__' token. */
5238 cp_lexer_consume_token (parser->lexer);
5239 /* Parse the cast-expression. */
5240 expression = cp_parser_simple_cast_expression (parser);
5241 /* Create the complete representation. */
5242 return build_x_unary_op ((keyword == RID_REALPART
5243 ? REALPART_EXPR : IMAGPART_EXPR),
5253 /* Look for the `:: new' and `:: delete', which also signal the
5254 beginning of a new-expression, or delete-expression,
5255 respectively. If the next token is `::', then it might be one of
5257 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5261 /* See if the token after the `::' is one of the keywords in
5262 which we're interested. */
5263 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5264 /* If it's `new', we have a new-expression. */
5265 if (keyword == RID_NEW)
5266 return cp_parser_new_expression (parser);
5267 /* Similarly, for `delete'. */
5268 else if (keyword == RID_DELETE)
5269 return cp_parser_delete_expression (parser);
5272 /* Look for a unary operator. */
5273 unary_operator = cp_parser_unary_operator (token);
5274 /* The `++' and `--' operators can be handled similarly, even though
5275 they are not technically unary-operators in the grammar. */
5276 if (unary_operator == ERROR_MARK)
5278 if (token->type == CPP_PLUS_PLUS)
5279 unary_operator = PREINCREMENT_EXPR;
5280 else if (token->type == CPP_MINUS_MINUS)
5281 unary_operator = PREDECREMENT_EXPR;
5282 /* Handle the GNU address-of-label extension. */
5283 else if (cp_parser_allow_gnu_extensions_p (parser)
5284 && token->type == CPP_AND_AND)
5288 /* Consume the '&&' token. */
5289 cp_lexer_consume_token (parser->lexer);
5290 /* Look for the identifier. */
5291 identifier = cp_parser_identifier (parser);
5292 /* Create an expression representing the address. */
5293 return finish_label_address_expr (identifier);
5296 if (unary_operator != ERROR_MARK)
5298 tree cast_expression;
5299 tree expression = error_mark_node;
5300 const char *non_constant_p = NULL;
5302 /* Consume the operator token. */
5303 token = cp_lexer_consume_token (parser->lexer);
5304 /* Parse the cast-expression. */
5306 = cp_parser_cast_expression (parser,
5307 unary_operator == ADDR_EXPR,
5309 /* Now, build an appropriate representation. */
5310 switch (unary_operator)
5313 non_constant_p = "`*'";
5314 expression = build_x_indirect_ref (cast_expression, "unary *");
5318 non_constant_p = "`&'";
5321 expression = build_x_unary_op (unary_operator, cast_expression);
5324 case PREINCREMENT_EXPR:
5325 case PREDECREMENT_EXPR:
5326 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5329 case UNARY_PLUS_EXPR:
5331 case TRUTH_NOT_EXPR:
5332 expression = finish_unary_op_expr (unary_operator, cast_expression);
5340 && cp_parser_non_integral_constant_expression (parser,
5342 expression = error_mark_node;
5347 return cp_parser_postfix_expression (parser, address_p, cast_p);
5350 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5351 unary-operator, the corresponding tree code is returned. */
5353 static enum tree_code
5354 cp_parser_unary_operator (cp_token* token)
5356 switch (token->type)
5359 return INDIRECT_REF;
5365 return UNARY_PLUS_EXPR;
5371 return TRUTH_NOT_EXPR;
5374 return BIT_NOT_EXPR;
5381 /* Parse a new-expression.
5384 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5385 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5387 Returns a representation of the expression. */
5390 cp_parser_new_expression (cp_parser* parser)
5392 bool global_scope_p;
5398 /* Look for the optional `::' operator. */
5400 = (cp_parser_global_scope_opt (parser,
5401 /*current_scope_valid_p=*/false)
5403 /* Look for the `new' operator. */
5404 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5405 /* There's no easy way to tell a new-placement from the
5406 `( type-id )' construct. */
5407 cp_parser_parse_tentatively (parser);
5408 /* Look for a new-placement. */
5409 placement = cp_parser_new_placement (parser);
5410 /* If that didn't work out, there's no new-placement. */
5411 if (!cp_parser_parse_definitely (parser))
5412 placement = NULL_TREE;
5414 /* If the next token is a `(', then we have a parenthesized
5416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5418 /* Consume the `('. */
5419 cp_lexer_consume_token (parser->lexer);
5420 /* Parse the type-id. */
5421 type = cp_parser_type_id (parser);
5422 /* Look for the closing `)'. */
5423 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5424 /* There should not be a direct-new-declarator in this production,
5425 but GCC used to allowed this, so we check and emit a sensible error
5426 message for this case. */
5427 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5429 error ("array bound forbidden after parenthesized type-id");
5430 inform ("try removing the parentheses around the type-id");
5431 cp_parser_direct_new_declarator (parser);
5435 /* Otherwise, there must be a new-type-id. */
5437 type = cp_parser_new_type_id (parser, &nelts);
5439 /* If the next token is a `(', then we have a new-initializer. */
5440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5441 initializer = cp_parser_new_initializer (parser);
5443 initializer = NULL_TREE;
5445 /* A new-expression may not appear in an integral constant
5447 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5448 return error_mark_node;
5450 /* Create a representation of the new-expression. */
5451 return build_new (placement, type, nelts, initializer, global_scope_p);
5454 /* Parse a new-placement.
5459 Returns the same representation as for an expression-list. */
5462 cp_parser_new_placement (cp_parser* parser)
5464 tree expression_list;
5466 /* Parse the expression-list. */
5467 expression_list = (cp_parser_parenthesized_expression_list
5468 (parser, false, /*cast_p=*/false,
5469 /*non_constant_p=*/NULL));
5471 return expression_list;
5474 /* Parse a new-type-id.
5477 type-specifier-seq new-declarator [opt]
5479 Returns the TYPE allocated. If the new-type-id indicates an array
5480 type, *NELTS is set to the number of elements in the last array
5481 bound; the TYPE will not include the last array bound. */
5484 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5486 cp_decl_specifier_seq type_specifier_seq;
5487 cp_declarator *new_declarator;
5488 cp_declarator *declarator;
5489 cp_declarator *outer_declarator;
5490 const char *saved_message;
5493 /* The type-specifier sequence must not contain type definitions.
5494 (It cannot contain declarations of new types either, but if they
5495 are not definitions we will catch that because they are not
5497 saved_message = parser->type_definition_forbidden_message;
5498 parser->type_definition_forbidden_message
5499 = "types may not be defined in a new-type-id";
5500 /* Parse the type-specifier-seq. */
5501 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5502 &type_specifier_seq);
5503 /* Restore the old message. */
5504 parser->type_definition_forbidden_message = saved_message;
5505 /* Parse the new-declarator. */
5506 new_declarator = cp_parser_new_declarator_opt (parser);
5508 /* Determine the number of elements in the last array dimension, if
5511 /* Skip down to the last array dimension. */
5512 declarator = new_declarator;
5513 outer_declarator = NULL;
5514 while (declarator && (declarator->kind == cdk_pointer
5515 || declarator->kind == cdk_ptrmem))
5517 outer_declarator = declarator;
5518 declarator = declarator->declarator;
5521 && declarator->kind == cdk_array
5522 && declarator->declarator
5523 && declarator->declarator->kind == cdk_array)
5525 outer_declarator = declarator;
5526 declarator = declarator->declarator;
5529 if (declarator && declarator->kind == cdk_array)
5531 *nelts = declarator->u.array.bounds;
5532 if (*nelts == error_mark_node)
5533 *nelts = integer_one_node;
5535 if (outer_declarator)
5536 outer_declarator->declarator = declarator->declarator;
5538 new_declarator = NULL;
5541 type = groktypename (&type_specifier_seq, new_declarator);
5542 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5544 *nelts = array_type_nelts_top (type);
5545 type = TREE_TYPE (type);
5550 /* Parse an (optional) new-declarator.
5553 ptr-operator new-declarator [opt]
5554 direct-new-declarator
5556 Returns the declarator. */
5558 static cp_declarator *
5559 cp_parser_new_declarator_opt (cp_parser* parser)
5561 enum tree_code code;
5563 cp_cv_quals cv_quals;
5565 /* We don't know if there's a ptr-operator next, or not. */
5566 cp_parser_parse_tentatively (parser);
5567 /* Look for a ptr-operator. */
5568 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5569 /* If that worked, look for more new-declarators. */
5570 if (cp_parser_parse_definitely (parser))
5572 cp_declarator *declarator;
5574 /* Parse another optional declarator. */
5575 declarator = cp_parser_new_declarator_opt (parser);
5577 /* Create the representation of the declarator. */
5579 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5580 else if (code == INDIRECT_REF)
5581 declarator = make_pointer_declarator (cv_quals, declarator);
5583 declarator = make_reference_declarator (cv_quals, declarator);
5588 /* If the next token is a `[', there is a direct-new-declarator. */
5589 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5590 return cp_parser_direct_new_declarator (parser);
5595 /* Parse a direct-new-declarator.
5597 direct-new-declarator:
5599 direct-new-declarator [constant-expression]
5603 static cp_declarator *
5604 cp_parser_direct_new_declarator (cp_parser* parser)
5606 cp_declarator *declarator = NULL;
5612 /* Look for the opening `['. */
5613 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5614 /* The first expression is not required to be constant. */
5617 expression = cp_parser_expression (parser, /*cast_p=*/false);
5618 /* The standard requires that the expression have integral
5619 type. DR 74 adds enumeration types. We believe that the
5620 real intent is that these expressions be handled like the
5621 expression in a `switch' condition, which also allows
5622 classes with a single conversion to integral or
5623 enumeration type. */
5624 if (!processing_template_decl)
5627 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5632 error ("expression in new-declarator must have integral "
5633 "or enumeration type");
5634 expression = error_mark_node;
5638 /* But all the other expressions must be. */
5641 = cp_parser_constant_expression (parser,
5642 /*allow_non_constant=*/false,
5644 /* Look for the closing `]'. */
5645 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5647 /* Add this bound to the declarator. */
5648 declarator = make_array_declarator (declarator, expression);
5650 /* If the next token is not a `[', then there are no more
5652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5659 /* Parse a new-initializer.
5662 ( expression-list [opt] )
5664 Returns a representation of the expression-list. If there is no
5665 expression-list, VOID_ZERO_NODE is returned. */
5668 cp_parser_new_initializer (cp_parser* parser)
5670 tree expression_list;
5672 expression_list = (cp_parser_parenthesized_expression_list
5673 (parser, false, /*cast_p=*/false,
5674 /*non_constant_p=*/NULL));
5675 if (!expression_list)
5676 expression_list = void_zero_node;
5678 return expression_list;
5681 /* Parse a delete-expression.
5684 :: [opt] delete cast-expression
5685 :: [opt] delete [ ] cast-expression
5687 Returns a representation of the expression. */
5690 cp_parser_delete_expression (cp_parser* parser)
5692 bool global_scope_p;
5696 /* Look for the optional `::' operator. */
5698 = (cp_parser_global_scope_opt (parser,
5699 /*current_scope_valid_p=*/false)
5701 /* Look for the `delete' keyword. */
5702 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5703 /* See if the array syntax is in use. */
5704 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5706 /* Consume the `[' token. */
5707 cp_lexer_consume_token (parser->lexer);
5708 /* Look for the `]' token. */
5709 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5710 /* Remember that this is the `[]' construct. */
5716 /* Parse the cast-expression. */
5717 expression = cp_parser_simple_cast_expression (parser);
5719 /* A delete-expression may not appear in an integral constant
5721 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5722 return error_mark_node;
5724 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5727 /* Parse a cast-expression.
5731 ( type-id ) cast-expression
5733 ADDRESS_P is true iff the unary-expression is appearing as the
5734 operand of the `&' operator. CAST_P is true if this expression is
5735 the target of a cast.
5737 Returns a representation of the expression. */
5740 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5742 /* If it's a `(', then we might be looking at a cast. */
5743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5745 tree type = NULL_TREE;
5746 tree expr = NULL_TREE;
5747 bool compound_literal_p;
5748 const char *saved_message;
5750 /* There's no way to know yet whether or not this is a cast.
5751 For example, `(int (3))' is a unary-expression, while `(int)
5752 3' is a cast. So, we resort to parsing tentatively. */
5753 cp_parser_parse_tentatively (parser);
5754 /* Types may not be defined in a cast. */
5755 saved_message = parser->type_definition_forbidden_message;
5756 parser->type_definition_forbidden_message
5757 = "types may not be defined in casts";
5758 /* Consume the `('. */
5759 cp_lexer_consume_token (parser->lexer);
5760 /* A very tricky bit is that `(struct S) { 3 }' is a
5761 compound-literal (which we permit in C++ as an extension).
5762 But, that construct is not a cast-expression -- it is a
5763 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5764 is legal; if the compound-literal were a cast-expression,
5765 you'd need an extra set of parentheses.) But, if we parse
5766 the type-id, and it happens to be a class-specifier, then we
5767 will commit to the parse at that point, because we cannot
5768 undo the action that is done when creating a new class. So,
5769 then we cannot back up and do a postfix-expression.
5771 Therefore, we scan ahead to the closing `)', and check to see
5772 if the token after the `)' is a `{'. If so, we are not
5773 looking at a cast-expression.
5775 Save tokens so that we can put them back. */
5776 cp_lexer_save_tokens (parser->lexer);
5777 /* Skip tokens until the next token is a closing parenthesis.
5778 If we find the closing `)', and the next token is a `{', then
5779 we are looking at a compound-literal. */
5781 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5782 /*consume_paren=*/true)
5783 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5784 /* Roll back the tokens we skipped. */
5785 cp_lexer_rollback_tokens (parser->lexer);
5786 /* If we were looking at a compound-literal, simulate an error
5787 so that the call to cp_parser_parse_definitely below will
5789 if (compound_literal_p)
5790 cp_parser_simulate_error (parser);
5793 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5794 parser->in_type_id_in_expr_p = true;
5795 /* Look for the type-id. */
5796 type = cp_parser_type_id (parser);
5797 /* Look for the closing `)'. */
5798 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5799 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5802 /* Restore the saved message. */
5803 parser->type_definition_forbidden_message = saved_message;
5805 /* If ok so far, parse the dependent expression. We cannot be
5806 sure it is a cast. Consider `(T ())'. It is a parenthesized
5807 ctor of T, but looks like a cast to function returning T
5808 without a dependent expression. */
5809 if (!cp_parser_error_occurred (parser))
5810 expr = cp_parser_cast_expression (parser,
5811 /*address_p=*/false,
5814 if (cp_parser_parse_definitely (parser))
5816 /* Warn about old-style casts, if so requested. */
5817 if (warn_old_style_cast
5818 && !in_system_header
5819 && !VOID_TYPE_P (type)
5820 && current_lang_name != lang_name_c)
5821 warning (OPT_Wold_style_cast, "use of old-style cast");
5823 /* Only type conversions to integral or enumeration types
5824 can be used in constant-expressions. */
5825 if (!cast_valid_in_integral_constant_expression_p (type)
5826 && (cp_parser_non_integral_constant_expression
5828 "a cast to a type other than an integral or "
5829 "enumeration type")))
5830 return error_mark_node;
5832 /* Perform the cast. */
5833 expr = build_c_cast (type, expr);
5838 /* If we get here, then it's not a cast, so it must be a
5839 unary-expression. */
5840 return cp_parser_unary_expression (parser, address_p, cast_p);
5843 /* Parse a binary expression of the general form:
5847 pm-expression .* cast-expression
5848 pm-expression ->* cast-expression
5850 multiplicative-expression:
5852 multiplicative-expression * pm-expression
5853 multiplicative-expression / pm-expression
5854 multiplicative-expression % pm-expression
5856 additive-expression:
5857 multiplicative-expression
5858 additive-expression + multiplicative-expression
5859 additive-expression - multiplicative-expression
5863 shift-expression << additive-expression
5864 shift-expression >> additive-expression
5866 relational-expression:
5868 relational-expression < shift-expression
5869 relational-expression > shift-expression
5870 relational-expression <= shift-expression
5871 relational-expression >= shift-expression
5875 relational-expression:
5876 relational-expression <? shift-expression
5877 relational-expression >? shift-expression
5879 equality-expression:
5880 relational-expression
5881 equality-expression == relational-expression
5882 equality-expression != relational-expression
5886 and-expression & equality-expression
5888 exclusive-or-expression:
5890 exclusive-or-expression ^ and-expression
5892 inclusive-or-expression:
5893 exclusive-or-expression
5894 inclusive-or-expression | exclusive-or-expression
5896 logical-and-expression:
5897 inclusive-or-expression
5898 logical-and-expression && inclusive-or-expression
5900 logical-or-expression:
5901 logical-and-expression
5902 logical-or-expression || logical-and-expression
5904 All these are implemented with a single function like:
5907 simple-cast-expression
5908 binary-expression <token> binary-expression
5910 CAST_P is true if this expression is the target of a cast.
5912 The binops_by_token map is used to get the tree codes for each <token> type.
5913 binary-expressions are associated according to a precedence table. */
5915 #define TOKEN_PRECEDENCE(token) \
5916 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5917 ? PREC_NOT_OPERATOR \
5918 : binops_by_token[token->type].prec)
5921 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5923 cp_parser_expression_stack stack;
5924 cp_parser_expression_stack_entry *sp = &stack[0];
5927 enum tree_code tree_type, lhs_type, rhs_type;
5928 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5931 /* Parse the first expression. */
5932 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5933 lhs_type = ERROR_MARK;
5937 /* Get an operator token. */
5938 token = cp_lexer_peek_token (parser->lexer);
5940 new_prec = TOKEN_PRECEDENCE (token);
5942 /* Popping an entry off the stack means we completed a subexpression:
5943 - either we found a token which is not an operator (`>' where it is not
5944 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5945 will happen repeatedly;
5946 - or, we found an operator which has lower priority. This is the case
5947 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5949 if (new_prec <= prec)
5958 tree_type = binops_by_token[token->type].tree_type;
5960 /* We used the operator token. */
5961 cp_lexer_consume_token (parser->lexer);
5963 /* Extract another operand. It may be the RHS of this expression
5964 or the LHS of a new, higher priority expression. */
5965 rhs = cp_parser_simple_cast_expression (parser);
5966 rhs_type = ERROR_MARK;
5968 /* Get another operator token. Look up its precedence to avoid
5969 building a useless (immediately popped) stack entry for common
5970 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5971 token = cp_lexer_peek_token (parser->lexer);
5972 lookahead_prec = TOKEN_PRECEDENCE (token);
5973 if (lookahead_prec > new_prec)
5975 /* ... and prepare to parse the RHS of the new, higher priority
5976 expression. Since precedence levels on the stack are
5977 monotonically increasing, we do not have to care about
5980 sp->tree_type = tree_type;
5982 sp->lhs_type = lhs_type;
5985 lhs_type = rhs_type;
5987 new_prec = lookahead_prec;
5991 /* If the stack is not empty, we have parsed into LHS the right side
5992 (`4' in the example above) of an expression we had suspended.
5993 We can use the information on the stack to recover the LHS (`3')
5994 from the stack together with the tree code (`MULT_EXPR'), and
5995 the precedence of the higher level subexpression
5996 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5997 which will be used to actually build the additive expression. */
6000 tree_type = sp->tree_type;
6002 rhs_type = lhs_type;
6004 lhs_type = sp->lhs_type;
6007 overloaded_p = false;
6008 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6010 lhs_type = tree_type;
6012 /* If the binary operator required the use of an overloaded operator,
6013 then this expression cannot be an integral constant-expression.
6014 An overloaded operator can be used even if both operands are
6015 otherwise permissible in an integral constant-expression if at
6016 least one of the operands is of enumeration type. */
6019 && (cp_parser_non_integral_constant_expression
6020 (parser, "calls to overloaded operators")))
6021 return error_mark_node;
6028 /* Parse the `? expression : assignment-expression' part of a
6029 conditional-expression. The LOGICAL_OR_EXPR is the
6030 logical-or-expression that started the conditional-expression.
6031 Returns a representation of the entire conditional-expression.
6033 This routine is used by cp_parser_assignment_expression.
6035 ? expression : assignment-expression
6039 ? : assignment-expression */
6042 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6045 tree assignment_expr;
6047 /* Consume the `?' token. */
6048 cp_lexer_consume_token (parser->lexer);
6049 if (cp_parser_allow_gnu_extensions_p (parser)
6050 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6051 /* Implicit true clause. */
6054 /* Parse the expression. */
6055 expr = cp_parser_expression (parser, /*cast_p=*/false);
6057 /* The next token should be a `:'. */
6058 cp_parser_require (parser, CPP_COLON, "`:'");
6059 /* Parse the assignment-expression. */
6060 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6062 /* Build the conditional-expression. */
6063 return build_x_conditional_expr (logical_or_expr,
6068 /* Parse an assignment-expression.
6070 assignment-expression:
6071 conditional-expression
6072 logical-or-expression assignment-operator assignment_expression
6075 CAST_P is true if this expression is the target of a cast.
6077 Returns a representation for the expression. */
6080 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6084 /* If the next token is the `throw' keyword, then we're looking at
6085 a throw-expression. */
6086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6087 expr = cp_parser_throw_expression (parser);
6088 /* Otherwise, it must be that we are looking at a
6089 logical-or-expression. */
6092 /* Parse the binary expressions (logical-or-expression). */
6093 expr = cp_parser_binary_expression (parser, cast_p);
6094 /* If the next token is a `?' then we're actually looking at a
6095 conditional-expression. */
6096 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6097 return cp_parser_question_colon_clause (parser, expr);
6100 enum tree_code assignment_operator;
6102 /* If it's an assignment-operator, we're using the second
6105 = cp_parser_assignment_operator_opt (parser);
6106 if (assignment_operator != ERROR_MARK)
6110 /* Parse the right-hand side of the assignment. */
6111 rhs = cp_parser_assignment_expression (parser, cast_p);
6112 /* An assignment may not appear in a
6113 constant-expression. */
6114 if (cp_parser_non_integral_constant_expression (parser,
6116 return error_mark_node;
6117 /* Build the assignment expression. */
6118 expr = build_x_modify_expr (expr,
6119 assignment_operator,
6128 /* Parse an (optional) assignment-operator.
6130 assignment-operator: one of
6131 = *= /= %= += -= >>= <<= &= ^= |=
6135 assignment-operator: one of
6138 If the next token is an assignment operator, the corresponding tree
6139 code is returned, and the token is consumed. For example, for
6140 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6141 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6142 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6143 operator, ERROR_MARK is returned. */
6145 static enum tree_code
6146 cp_parser_assignment_operator_opt (cp_parser* parser)
6151 /* Peek at the next toen. */
6152 token = cp_lexer_peek_token (parser->lexer);
6154 switch (token->type)
6165 op = TRUNC_DIV_EXPR;
6169 op = TRUNC_MOD_EXPR;
6201 /* Nothing else is an assignment operator. */
6205 /* If it was an assignment operator, consume it. */
6206 if (op != ERROR_MARK)
6207 cp_lexer_consume_token (parser->lexer);
6212 /* Parse an expression.
6215 assignment-expression
6216 expression , assignment-expression
6218 CAST_P is true if this expression is the target of a cast.
6220 Returns a representation of the expression. */
6223 cp_parser_expression (cp_parser* parser, bool cast_p)
6225 tree expression = NULL_TREE;
6229 tree assignment_expression;
6231 /* Parse the next assignment-expression. */
6232 assignment_expression
6233 = cp_parser_assignment_expression (parser, cast_p);
6234 /* If this is the first assignment-expression, we can just
6237 expression = assignment_expression;
6239 expression = build_x_compound_expr (expression,
6240 assignment_expression);
6241 /* If the next token is not a comma, then we are done with the
6243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6245 /* Consume the `,'. */
6246 cp_lexer_consume_token (parser->lexer);
6247 /* A comma operator cannot appear in a constant-expression. */
6248 if (cp_parser_non_integral_constant_expression (parser,
6249 "a comma operator"))
6250 expression = error_mark_node;
6256 /* Parse a constant-expression.
6258 constant-expression:
6259 conditional-expression
6261 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6262 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6263 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6264 is false, NON_CONSTANT_P should be NULL. */
6267 cp_parser_constant_expression (cp_parser* parser,
6268 bool allow_non_constant_p,
6269 bool *non_constant_p)
6271 bool saved_integral_constant_expression_p;
6272 bool saved_allow_non_integral_constant_expression_p;
6273 bool saved_non_integral_constant_expression_p;
6276 /* It might seem that we could simply parse the
6277 conditional-expression, and then check to see if it were
6278 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6279 one that the compiler can figure out is constant, possibly after
6280 doing some simplifications or optimizations. The standard has a
6281 precise definition of constant-expression, and we must honor
6282 that, even though it is somewhat more restrictive.
6288 is not a legal declaration, because `(2, 3)' is not a
6289 constant-expression. The `,' operator is forbidden in a
6290 constant-expression. However, GCC's constant-folding machinery
6291 will fold this operation to an INTEGER_CST for `3'. */
6293 /* Save the old settings. */
6294 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6295 saved_allow_non_integral_constant_expression_p
6296 = parser->allow_non_integral_constant_expression_p;
6297 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6298 /* We are now parsing a constant-expression. */
6299 parser->integral_constant_expression_p = true;
6300 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6301 parser->non_integral_constant_expression_p = false;
6302 /* Although the grammar says "conditional-expression", we parse an
6303 "assignment-expression", which also permits "throw-expression"
6304 and the use of assignment operators. In the case that
6305 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6306 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6307 actually essential that we look for an assignment-expression.
6308 For example, cp_parser_initializer_clauses uses this function to
6309 determine whether a particular assignment-expression is in fact
6311 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6312 /* Restore the old settings. */
6313 parser->integral_constant_expression_p
6314 = saved_integral_constant_expression_p;
6315 parser->allow_non_integral_constant_expression_p
6316 = saved_allow_non_integral_constant_expression_p;
6317 if (allow_non_constant_p)
6318 *non_constant_p = parser->non_integral_constant_expression_p;
6319 else if (parser->non_integral_constant_expression_p)
6320 expression = error_mark_node;
6321 parser->non_integral_constant_expression_p
6322 = saved_non_integral_constant_expression_p;
6327 /* Parse __builtin_offsetof.
6329 offsetof-expression:
6330 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6332 offsetof-member-designator:
6334 | offsetof-member-designator "." id-expression
6335 | offsetof-member-designator "[" expression "]" */
6338 cp_parser_builtin_offsetof (cp_parser *parser)
6340 int save_ice_p, save_non_ice_p;
6344 /* We're about to accept non-integral-constant things, but will
6345 definitely yield an integral constant expression. Save and
6346 restore these values around our local parsing. */
6347 save_ice_p = parser->integral_constant_expression_p;
6348 save_non_ice_p = parser->non_integral_constant_expression_p;
6350 /* Consume the "__builtin_offsetof" token. */
6351 cp_lexer_consume_token (parser->lexer);
6352 /* Consume the opening `('. */
6353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6354 /* Parse the type-id. */
6355 type = cp_parser_type_id (parser);
6356 /* Look for the `,'. */
6357 cp_parser_require (parser, CPP_COMMA, "`,'");
6359 /* Build the (type *)null that begins the traditional offsetof macro. */
6360 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6362 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6363 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6367 cp_token *token = cp_lexer_peek_token (parser->lexer);
6368 switch (token->type)
6370 case CPP_OPEN_SQUARE:
6371 /* offsetof-member-designator "[" expression "]" */
6372 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6376 /* offsetof-member-designator "." identifier */
6377 cp_lexer_consume_token (parser->lexer);
6378 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6382 case CPP_CLOSE_PAREN:
6383 /* Consume the ")" token. */
6384 cp_lexer_consume_token (parser->lexer);
6388 /* Error. We know the following require will fail, but
6389 that gives the proper error message. */
6390 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6391 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6392 expr = error_mark_node;
6398 /* If we're processing a template, we can't finish the semantics yet.
6399 Otherwise we can fold the entire expression now. */
6400 if (processing_template_decl)
6401 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6403 expr = finish_offsetof (expr);
6406 parser->integral_constant_expression_p = save_ice_p;
6407 parser->non_integral_constant_expression_p = save_non_ice_p;
6412 /* Statements [gram.stmt.stmt] */
6414 /* Parse a statement.
6418 expression-statement
6423 declaration-statement
6426 IN_COMPOUND is true when the statement is nested inside a
6427 cp_parser_compound_statement; this matters for certain pragmas.
6429 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6430 is a (possibly labeled) if statement which is not enclosed in braces
6431 and has an else clause. This is used to implement -Wparentheses. */
6434 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6435 bool in_compound, bool *if_p)
6439 location_t statement_location;
6444 /* There is no statement yet. */
6445 statement = NULL_TREE;
6446 /* Peek at the next token. */
6447 token = cp_lexer_peek_token (parser->lexer);
6448 /* Remember the location of the first token in the statement. */
6449 statement_location = token->location;
6450 /* If this is a keyword, then that will often determine what kind of
6451 statement we have. */
6452 if (token->type == CPP_KEYWORD)
6454 enum rid keyword = token->keyword;
6460 /* Looks like a labeled-statement with a case label.
6461 Parse the label, and then use tail recursion to parse
6463 cp_parser_label_for_labeled_statement (parser);
6468 statement = cp_parser_selection_statement (parser, if_p);
6474 statement = cp_parser_iteration_statement (parser);
6481 statement = cp_parser_jump_statement (parser);
6484 /* Objective-C++ exception-handling constructs. */
6487 case RID_AT_FINALLY:
6488 case RID_AT_SYNCHRONIZED:
6490 statement = cp_parser_objc_statement (parser);
6494 statement = cp_parser_try_block (parser);
6498 /* It might be a keyword like `int' that can start a
6499 declaration-statement. */
6503 else if (token->type == CPP_NAME)
6505 /* If the next token is a `:', then we are looking at a
6506 labeled-statement. */
6507 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6508 if (token->type == CPP_COLON)
6510 /* Looks like a labeled-statement with an ordinary label.
6511 Parse the label, and then use tail recursion to parse
6513 cp_parser_label_for_labeled_statement (parser);
6517 /* Anything that starts with a `{' must be a compound-statement. */
6518 else if (token->type == CPP_OPEN_BRACE)
6519 /* APPLE LOCAL radar 5982990 */
6520 statement = cp_parser_compound_statement (parser, NULL, false, false);
6521 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6522 a statement all its own. */
6523 else if (token->type == CPP_PRAGMA)
6525 /* Only certain OpenMP pragmas are attached to statements, and thus
6526 are considered statements themselves. All others are not. In
6527 the context of a compound, accept the pragma as a "statement" and
6528 return so that we can check for a close brace. Otherwise we
6529 require a real statement and must go back and read one. */
6531 cp_parser_pragma (parser, pragma_compound);
6532 else if (!cp_parser_pragma (parser, pragma_stmt))
6536 else if (token->type == CPP_EOF)
6538 cp_parser_error (parser, "expected statement");
6542 /* Everything else must be a declaration-statement or an
6543 expression-statement. Try for the declaration-statement
6544 first, unless we are looking at a `;', in which case we know that
6545 we have an expression-statement. */
6548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6550 cp_parser_parse_tentatively (parser);
6551 /* Try to parse the declaration-statement. */
6552 cp_parser_declaration_statement (parser);
6553 /* If that worked, we're done. */
6554 if (cp_parser_parse_definitely (parser))
6557 /* Look for an expression-statement instead. */
6558 statement = cp_parser_expression_statement (parser, in_statement_expr);
6561 /* Set the line number for the statement. */
6562 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6563 SET_EXPR_LOCATION (statement, statement_location);
6566 /* Parse the label for a labeled-statement, i.e.
6569 case constant-expression :
6573 case constant-expression ... constant-expression : statement
6575 When a label is parsed without errors, the label is added to the
6576 parse tree by the finish_* functions, so this function doesn't
6577 have to return the label. */
6580 cp_parser_label_for_labeled_statement (cp_parser* parser)
6584 /* The next token should be an identifier. */
6585 token = cp_lexer_peek_token (parser->lexer);
6586 if (token->type != CPP_NAME
6587 && token->type != CPP_KEYWORD)
6589 cp_parser_error (parser, "expected labeled-statement");
6593 switch (token->keyword)
6600 /* Consume the `case' token. */
6601 cp_lexer_consume_token (parser->lexer);
6602 /* Parse the constant-expression. */
6603 expr = cp_parser_constant_expression (parser,
6604 /*allow_non_constant_p=*/false,
6607 ellipsis = cp_lexer_peek_token (parser->lexer);
6608 if (ellipsis->type == CPP_ELLIPSIS)
6610 /* Consume the `...' token. */
6611 cp_lexer_consume_token (parser->lexer);
6613 cp_parser_constant_expression (parser,
6614 /*allow_non_constant_p=*/false,
6616 /* We don't need to emit warnings here, as the common code
6617 will do this for us. */
6620 expr_hi = NULL_TREE;
6622 if (parser->in_switch_statement_p)
6623 finish_case_label (expr, expr_hi);
6625 error ("case label %qE not within a switch statement", expr);
6630 /* Consume the `default' token. */
6631 cp_lexer_consume_token (parser->lexer);
6633 if (parser->in_switch_statement_p)
6634 finish_case_label (NULL_TREE, NULL_TREE);
6636 error ("case label not within a switch statement");
6640 /* Anything else must be an ordinary label. */
6641 finish_label_stmt (cp_parser_identifier (parser));
6645 /* Require the `:' token. */
6646 cp_parser_require (parser, CPP_COLON, "`:'");
6649 /* Parse an expression-statement.
6651 expression-statement:
6654 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6655 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6656 indicates whether this expression-statement is part of an
6657 expression statement. */
6660 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6662 tree statement = NULL_TREE;
6664 /* If the next token is a ';', then there is no expression
6666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6667 statement = cp_parser_expression (parser, /*cast_p=*/false);
6669 /* Consume the final `;'. */
6670 cp_parser_consume_semicolon_at_end_of_statement (parser);
6672 if (in_statement_expr
6673 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6674 /* This is the final expression statement of a statement
6676 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6678 statement = finish_expr_stmt (statement);
6685 /* Parse a compound-statement.
6688 { statement-seq [opt] }
6690 Returns a tree representing the statement. */
6693 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6694 /* APPLE LOCAL radar 5982990 */
6695 bool in_try, bool objc_sjlj_exceptions)
6699 /* Consume the `{'. */
6700 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6701 return error_mark_node;
6702 /* Begin the compound-statement. */
6703 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6704 /* Parse an (optional) statement-seq. */
6705 cp_parser_statement_seq_opt (parser, in_statement_expr);
6706 /* APPLE LOCAL begin radar 5982990 */
6707 if (objc_sjlj_exceptions)
6708 objc_mark_locals_volatile (NULL);
6709 /* APPLE LOCAL end radar 5982990 */
6710 /* Finish the compound-statement. */
6711 finish_compound_stmt (compound_stmt);
6712 /* Consume the `}'. */
6713 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6715 return compound_stmt;
6718 /* Parse an (optional) statement-seq.
6722 statement-seq [opt] statement */
6725 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6727 /* APPLE LOCAL begin omit calls to empty destructors 5559195 */
6728 tree class_type = DECL_CONTEXT (current_function_decl);
6730 bool determine_destructor_triviality =
6731 DECL_DESTRUCTOR_P (current_function_decl) && class_type != NULL_TREE
6732 && !CLASSTYPE_DESTRUCTOR_TRIVIALITY_FINAL (class_type);
6734 /* Assume that the destructor is trivial at first, and mark nontrivial if
6735 any statement is parsed. */
6736 if (determine_destructor_triviality)
6738 CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (class_type) = 0;
6739 CLASSTYPE_DESTRUCTOR_TRIVIALITY_FINAL (class_type) = 1;
6741 /* APPLE LOCAL end omit calls to empty destructors 5559195 */
6743 /* Scan statements until there aren't any more. */
6746 cp_token *token = cp_lexer_peek_token (parser->lexer);
6748 /* APPLE LOCAL begin ObjC++ 4185810 */
6749 /* If we're looking at a `}', then we've run out of
6750 statements; the same is true if we have reached the end
6751 of file, or have stumbled upon a stray 'else' or '@end'. */
6752 if (token->type == CPP_CLOSE_BRACE
6753 || token->type == CPP_EOF
6754 || token->type == CPP_PRAGMA_EOL
6755 || (token->type == CPP_KEYWORD
6756 && (token->keyword == RID_ELSE
6757 || token->keyword == RID_AT_END)))
6758 /* APPLE LOCAL end ObjC++ 4185810 */
6761 /* APPLE LOCAL begin omit calls to empty destructors 5559195 */
6762 if (determine_destructor_triviality)
6763 CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (class_type) = 1;
6764 /* APPLE LOCAL end omit calls to empty destructors 5559195 */
6766 /* Parse the statement. */
6767 cp_parser_statement (parser, in_statement_expr, true, NULL);
6771 /* Parse a selection-statement.
6773 selection-statement:
6774 if ( condition ) statement
6775 if ( condition ) statement else statement
6776 switch ( condition ) statement
6778 Returns the new IF_STMT or SWITCH_STMT.
6780 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6781 is a (possibly labeled) if statement which is not enclosed in
6782 braces and has an else clause. This is used to implement
6786 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6794 /* Peek at the next token. */
6795 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6797 /* See what kind of keyword it is. */
6798 keyword = token->keyword;
6807 /* Look for the `('. */
6808 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6810 cp_parser_skip_to_end_of_statement (parser);
6811 return error_mark_node;
6814 /* Begin the selection-statement. */
6815 if (keyword == RID_IF)
6816 statement = begin_if_stmt ();
6818 statement = begin_switch_stmt ();
6820 /* Parse the condition. */
6821 condition = cp_parser_condition (parser);
6822 /* Look for the `)'. */
6823 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6824 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6825 /*consume_paren=*/true);
6827 if (keyword == RID_IF)
6831 /* Add the condition. */
6832 finish_if_stmt_cond (condition, statement);
6834 /* Parse the then-clause. */
6835 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6836 finish_then_clause (statement);
6838 /* If the next token is `else', parse the else-clause. */
6839 if (cp_lexer_next_token_is_keyword (parser->lexer,
6842 /* Consume the `else' keyword. */
6843 cp_lexer_consume_token (parser->lexer);
6844 begin_else_clause (statement);
6845 /* Parse the else-clause. */
6846 cp_parser_implicitly_scoped_statement (parser, NULL);
6847 finish_else_clause (statement);
6849 /* If we are currently parsing a then-clause, then
6850 IF_P will not be NULL. We set it to true to
6851 indicate that this if statement has an else clause.
6852 This may trigger the Wparentheses warning below
6853 when we get back up to the parent if statement. */
6859 /* This if statement does not have an else clause. If
6860 NESTED_IF is true, then the then-clause is an if
6861 statement which does have an else clause. We warn
6862 about the potential ambiguity. */
6864 warning (OPT_Wparentheses,
6865 ("%Hsuggest explicit braces "
6866 "to avoid ambiguous %<else%>"),
6867 EXPR_LOCUS (statement));
6870 /* Now we're all done with the if-statement. */
6871 finish_if_stmt (statement);
6875 bool in_switch_statement_p;
6876 unsigned char in_statement;
6878 /* Add the condition. */
6879 finish_switch_cond (condition, statement);
6881 /* Parse the body of the switch-statement. */
6882 in_switch_statement_p = parser->in_switch_statement_p;
6883 in_statement = parser->in_statement;
6884 parser->in_switch_statement_p = true;
6885 parser->in_statement |= IN_SWITCH_STMT;
6886 cp_parser_implicitly_scoped_statement (parser, NULL);
6887 parser->in_switch_statement_p = in_switch_statement_p;
6888 parser->in_statement = in_statement;
6890 /* Now we're all done with the switch-statement. */
6891 finish_switch_stmt (statement);
6899 cp_parser_error (parser, "expected selection-statement");
6900 return error_mark_node;
6904 /* Parse a condition.
6908 type-specifier-seq declarator = assignment-expression
6913 type-specifier-seq declarator asm-specification [opt]
6914 attributes [opt] = assignment-expression
6916 Returns the expression that should be tested. */
6919 cp_parser_condition (cp_parser* parser)
6921 cp_decl_specifier_seq type_specifiers;
6922 const char *saved_message;
6924 /* Try the declaration first. */
6925 cp_parser_parse_tentatively (parser);
6926 /* New types are not allowed in the type-specifier-seq for a
6928 saved_message = parser->type_definition_forbidden_message;
6929 parser->type_definition_forbidden_message
6930 = "types may not be defined in conditions";
6931 /* Parse the type-specifier-seq. */
6932 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6934 /* Restore the saved message. */
6935 parser->type_definition_forbidden_message = saved_message;
6936 /* If all is well, we might be looking at a declaration. */
6937 if (!cp_parser_error_occurred (parser))
6940 tree asm_specification;
6942 cp_declarator *declarator;
6943 tree initializer = NULL_TREE;
6945 /* Parse the declarator. */
6946 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6947 /*ctor_dtor_or_conv_p=*/NULL,
6948 /*parenthesized_p=*/NULL,
6949 /*member_p=*/false);
6950 /* Parse the attributes. */
6951 attributes = cp_parser_attributes_opt (parser);
6952 /* Parse the asm-specification. */
6953 asm_specification = cp_parser_asm_specification_opt (parser);
6954 /* If the next token is not an `=', then we might still be
6955 looking at an expression. For example:
6959 looks like a decl-specifier-seq and a declarator -- but then
6960 there is no `=', so this is an expression. */
6961 cp_parser_require (parser, CPP_EQ, "`='");
6962 /* If we did see an `=', then we are looking at a declaration
6964 if (cp_parser_parse_definitely (parser))
6967 bool non_constant_p;
6969 /* Create the declaration. */
6970 decl = start_decl (declarator, &type_specifiers,
6971 /*initialized_p=*/true,
6972 attributes, /*prefix_attributes=*/NULL_TREE,
6974 /* Parse the assignment-expression. */
6976 = cp_parser_constant_expression (parser,
6977 /*allow_non_constant_p=*/true,
6979 if (!non_constant_p)
6980 initializer = fold_non_dependent_expr (initializer);
6982 /* Process the initializer. */
6983 cp_finish_decl (decl,
6984 initializer, !non_constant_p,
6986 LOOKUP_ONLYCONVERTING);
6989 pop_scope (pushed_scope);
6991 return convert_from_reference (decl);
6994 /* If we didn't even get past the declarator successfully, we are
6995 definitely not looking at a declaration. */
6997 cp_parser_abort_tentative_parse (parser);
6999 /* Otherwise, we are looking at an expression. */
7000 return cp_parser_expression (parser, /*cast_p=*/false);
7003 /* APPLE LOCAL begin radar 4631818 */
7004 /* This routine looks for objective-c++'s foreach statement by scanning for-loop
7005 header looking for either 1) 'for (type selector in...)' or 2) 'for (selector in...)'
7006 where selector is already declared in outer scope. If it failed, it undoes the lexical
7007 look-ahead and returns false. If it succeeded, it adds the 'selector' to the statement
7008 list and returns true. At success, lexer points to token following the 'in' keyword.
7012 cp_parser_parse_foreach_stmt (cp_parser *parser)
7014 int decl_spec_declares_class_or_enum;
7015 bool is_cv_qualifier;
7017 cp_decl_specifier_seq decl_specs;
7020 bool is_legit_foreach = false;
7021 cp_declarator *declarator;
7023 /* Exclude class/struct/enum type definition in for-loop header, which is
7024 aparently legal in c++. Otherwise, it causes side-effect (type is enterred
7025 in function's scope) when type is re-parsed. */
7026 token = cp_lexer_peek_token (parser->lexer);
7027 if (cp_parser_token_is_class_key (token) || token->keyword == RID_ENUM)
7030 cp_parser_parse_tentatively (parser);
7031 clear_decl_specs (&decl_specs);
7033 = cp_parser_type_specifier (parser, CP_PARSER_FLAGS_OPTIONAL,
7035 /*is_declaration=*/true,
7036 &decl_spec_declares_class_or_enum,
7039 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7041 /*parenthesized_p=*/NULL,
7042 /*member_p=*/false);
7043 if (declarator == cp_error_declarator)
7045 cp_parser_abort_tentative_parse (parser);
7049 token = cp_lexer_peek_token (parser->lexer);
7051 node = token->u.value;
7052 if (node && TREE_CODE (node) == IDENTIFIER_NODE
7053 && node == ridpointers [(int) RID_IN])
7055 enum cpp_ttype nt = cp_lexer_peek_nth_token (parser->lexer, 2)->type;
7059 case CPP_OPEN_PAREN:
7061 case CPP_PLUS: case CPP_PLUS_PLUS:
7062 case CPP_MINUS: case CPP_MINUS_MINUS:
7063 case CPP_OPEN_SQUARE:
7064 is_legit_foreach = true;
7069 if (is_legit_foreach)
7071 tree pushed_scope = NULL;
7075 /* we have: 'for (type selector in...)' */
7076 cp_parser_commit_to_tentative_parse (parser);
7077 decl = start_decl (declarator, &decl_specs,
7078 false /*is_initialized*/,
7079 NULL_TREE /*attributes*/,
7080 NULL_TREE /*prefix_attributes*/,
7082 /* APPLE LOCAL begin radar 5130983 */
7083 if (!decl || decl == error_mark_node)
7085 error ("selector is undeclared");
7086 is_legit_foreach = false;
7089 cp_finish_decl (decl,
7090 NULL_TREE /*initializer*/,
7091 false /*init_const_expr_p=*/,
7092 NULL_TREE /*asm_specification*/,
7097 /* we have: 'for (selector in...)' */
7098 /* Parse it as an expression. */
7099 cp_parser_abort_tentative_parse (parser);
7100 statement = cp_parser_expression (parser, /*cast_p=*/false);
7101 add_stmt (statement);
7103 /* APPLE LOCAL end radar 5130983 */
7104 /* Consume the 'in' token */
7105 cp_lexer_consume_token (parser->lexer);
7108 cp_parser_abort_tentative_parse (parser);
7109 return is_legit_foreach;
7111 /* APPLE LOCAL end radar 4631818 */
7113 /* Parse an iteration-statement.
7115 iteration-statement:
7116 for ( for-init-statement condition [opt] ; expression [opt] )
7119 APPLE LOCAL begin for-fsf-4_4 3274130 5295549
7122 while attributes [opt] ( condition ) statement
7123 do attributes [opt] statement while ( expression ) ;
7124 for attributes [opt]
7125 ( for-init-statement condition [opt] ; expression [opt] )
7128 APPLE LOCAL end for-fsf-4_4 3274130 5295549
7129 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7132 cp_parser_iteration_statement (cp_parser* parser)
7136 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7137 tree statement, attributes;
7138 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7139 unsigned char in_statement;
7141 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7142 /* Get the keyword at the start of the loop. */
7143 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7144 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7146 return error_mark_node;
7148 /* Remember whether or not we are already within an iteration
7150 in_statement = parser->in_statement;
7152 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7153 /* Parse the attributes, if any. */
7154 attributes = cp_parser_attributes_opt (parser);
7156 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7157 /* See what kind of keyword it is. */
7158 keyword = token->keyword;
7165 /* Begin the while-statement. */
7166 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7167 statement = begin_while_stmt (attributes);
7168 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7169 /* Look for the `('. */
7170 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7171 /* Parse the condition. */
7172 condition = cp_parser_condition (parser);
7173 finish_while_stmt_cond (condition, statement);
7174 /* Look for the `)'. */
7175 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7176 /* Parse the dependent statement. */
7177 parser->in_statement = IN_ITERATION_STMT;
7178 cp_parser_already_scoped_statement (parser);
7179 parser->in_statement = in_statement;
7180 /* We're done with the while-statement. */
7181 finish_while_stmt (statement);
7189 /* Begin the do-statement. */
7190 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7191 statement = begin_do_stmt (attributes);
7192 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7193 /* Parse the body of the do-statement. */
7194 parser->in_statement = IN_ITERATION_STMT;
7195 cp_parser_implicitly_scoped_statement (parser, NULL);
7196 parser->in_statement = in_statement;
7197 finish_do_body (statement);
7198 /* Look for the `while' keyword. */
7199 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7200 /* Look for the `('. */
7201 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7202 /* Parse the expression. */
7203 expression = cp_parser_expression (parser, /*cast_p=*/false);
7204 /* We're done with the do-statement. */
7205 finish_do_stmt (expression, statement);
7206 /* Look for the `)'. */
7207 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7208 /* Look for the `;'. */
7209 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7215 tree condition = NULL_TREE;
7216 tree expression = NULL_TREE;
7218 /* Begin the for-statement. */
7219 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
7220 statement = begin_for_stmt (attributes);
7221 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
7222 /* Look for the `('. */
7223 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7224 /* Parse the initialization. */
7225 cp_parser_for_init_statement (parser);
7226 finish_for_init_stmt (statement);
7228 /* If there's a condition, process it. */
7229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7230 condition = cp_parser_condition (parser);
7231 finish_for_cond (condition, statement);
7232 /* Look for the `;'. */
7233 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7235 /* If there's an expression, process it. */
7236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7237 expression = cp_parser_expression (parser, /*cast_p=*/false);
7238 finish_for_expr (expression, statement);
7239 /* Look for the `)'. */
7240 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7242 /* Parse the body of the for-statement. */
7243 parser->in_statement = IN_ITERATION_STMT;
7244 cp_parser_already_scoped_statement (parser);
7245 parser->in_statement = in_statement;
7247 /* We're done with the for-statement. */
7248 finish_for_stmt (statement);
7253 cp_parser_error (parser, "expected iteration-statement");
7254 statement = error_mark_node;
7261 /* Parse a for-init-statement.
7264 expression-statement
7265 simple-declaration */
7268 cp_parser_for_init_statement (cp_parser* parser)
7270 /* If the next token is a `;', then we have an empty
7271 expression-statement. Grammatically, this is also a
7272 simple-declaration, but an invalid one, because it does not
7273 declare anything. Therefore, if we did not handle this case
7274 specially, we would issue an error message about an invalid
7276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7278 /* We're going to speculatively look for a declaration, falling back
7279 to an expression, if necessary. */
7280 cp_parser_parse_tentatively (parser);
7281 /* Parse the declaration. */
7282 cp_parser_simple_declaration (parser,
7283 /*function_definition_allowed_p=*/false);
7284 /* If the tentative parse failed, then we shall need to look for an
7285 expression-statement. */
7286 if (cp_parser_parse_definitely (parser))
7290 cp_parser_expression_statement (parser, false);
7293 /* Parse a jump-statement.
7298 return expression [opt] ;
7306 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7309 cp_parser_jump_statement (cp_parser* parser)
7311 tree statement = error_mark_node;
7315 /* Peek at the next token. */
7316 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7318 return error_mark_node;
7320 /* See what kind of keyword it is. */
7321 keyword = token->keyword;
7325 switch (parser->in_statement)
7328 error ("break statement not within loop or switch");
7331 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
7332 || parser->in_statement == IN_ITERATION_STMT);
7333 statement = finish_break_stmt ();
7336 error ("invalid exit from OpenMP structured block");
7339 error ("break statement used with OpenMP for loop");
7342 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7346 switch (parser->in_statement & ~IN_SWITCH_STMT)
7349 error ("continue statement not within a loop");
7351 case IN_ITERATION_STMT:
7353 statement = finish_continue_stmt ();
7356 error ("invalid exit from OpenMP structured block");
7361 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7368 /* If the next token is a `;', then there is no
7370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7371 expr = cp_parser_expression (parser, /*cast_p=*/false);
7374 /* Build the return-statement. */
7375 statement = finish_return_stmt (expr);
7376 /* Look for the final `;'. */
7377 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7382 /* APPLE LOCAL begin blocks 6040305 (cb) */
7384 error ("goto not allowed in block literal");
7385 /* APPLE LOCAL end blocks 6040305 (cb) */
7386 /* Create the goto-statement. */
7387 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7389 /* Issue a warning about this use of a GNU extension. */
7391 pedwarn ("ISO C++ forbids computed gotos");
7392 /* Consume the '*' token. */
7393 cp_lexer_consume_token (parser->lexer);
7394 /* Parse the dependent expression. */
7395 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7398 finish_goto_stmt (cp_parser_identifier (parser));
7399 /* Look for the final `;'. */
7400 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7404 cp_parser_error (parser, "expected jump-statement");
7411 /* Parse a declaration-statement.
7413 declaration-statement:
7414 block-declaration */
7417 cp_parser_declaration_statement (cp_parser* parser)
7421 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7422 p = obstack_alloc (&declarator_obstack, 0);
7424 /* Parse the block-declaration. */
7425 cp_parser_block_declaration (parser, /*statement_p=*/true);
7427 /* Free any declarators allocated. */
7428 obstack_free (&declarator_obstack, p);
7430 /* Finish off the statement. */
7434 /* Some dependent statements (like `if (cond) statement'), are
7435 implicitly in their own scope. In other words, if the statement is
7436 a single statement (as opposed to a compound-statement), it is
7437 none-the-less treated as if it were enclosed in braces. Any
7438 declarations appearing in the dependent statement are out of scope
7439 after control passes that point. This function parses a statement,
7440 but ensures that is in its own scope, even if it is not a
7443 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7444 is a (possibly labeled) if statement which is not enclosed in
7445 braces and has an else clause. This is used to implement
7448 Returns the new statement. */
7451 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7458 /* Mark if () ; with a special NOP_EXPR. */
7459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7461 cp_lexer_consume_token (parser->lexer);
7462 statement = add_stmt (build_empty_stmt ());
7464 /* if a compound is opened, we simply parse the statement directly. */
7465 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7466 /* APPLE LOCAL radar 5982990 */
7467 statement = cp_parser_compound_statement (parser, NULL, false, false);
7468 /* If the token is not a `{', then we must take special action. */
7471 /* Create a compound-statement. */
7472 statement = begin_compound_stmt (0);
7473 /* Parse the dependent-statement. */
7474 cp_parser_statement (parser, NULL_TREE, false, if_p);
7475 /* Finish the dummy compound-statement. */
7476 finish_compound_stmt (statement);
7479 /* Return the statement. */
7483 /* For some dependent statements (like `while (cond) statement'), we
7484 have already created a scope. Therefore, even if the dependent
7485 statement is a compound-statement, we do not want to create another
7489 cp_parser_already_scoped_statement (cp_parser* parser)
7491 /* If the token is a `{', then we must take special action. */
7492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7493 cp_parser_statement (parser, NULL_TREE, false, NULL);
7496 /* Avoid calling cp_parser_compound_statement, so that we
7497 don't create a new scope. Do everything else by hand. */
7498 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7499 cp_parser_statement_seq_opt (parser, NULL_TREE);
7500 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7504 /* Declarations [gram.dcl.dcl] */
7506 /* Parse an optional declaration-sequence.
7510 declaration-seq declaration */
7513 cp_parser_declaration_seq_opt (cp_parser* parser)
7519 token = cp_lexer_peek_token (parser->lexer);
7521 if (token->type == CPP_CLOSE_BRACE
7522 || token->type == CPP_EOF
7523 || token->type == CPP_PRAGMA_EOL)
7526 if (token->type == CPP_SEMICOLON)
7528 /* A declaration consisting of a single semicolon is
7529 invalid. Allow it unless we're being pedantic. */
7530 cp_lexer_consume_token (parser->lexer);
7531 if (pedantic && !in_system_header)
7532 pedwarn ("extra %<;%>");
7536 /* If we're entering or exiting a region that's implicitly
7537 extern "C", modify the lang context appropriately. */
7538 if (!parser->implicit_extern_c && token->implicit_extern_c)
7540 push_lang_context (lang_name_c);
7541 parser->implicit_extern_c = true;
7543 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7545 pop_lang_context ();
7546 parser->implicit_extern_c = false;
7549 if (token->type == CPP_PRAGMA)
7551 /* A top-level declaration can consist solely of a #pragma.
7552 A nested declaration cannot, so this is done here and not
7553 in cp_parser_declaration. (A #pragma at block scope is
7554 handled in cp_parser_statement.) */
7555 cp_parser_pragma (parser, pragma_external);
7559 /* Parse the declaration itself. */
7560 cp_parser_declaration (parser);
7564 /* Parse a declaration.
7569 template-declaration
7570 explicit-instantiation
7571 explicit-specialization
7572 linkage-specification
7573 namespace-definition
7578 __extension__ declaration */
7581 cp_parser_declaration (cp_parser* parser)
7588 /* Check for the `__extension__' keyword. */
7589 if (cp_parser_extension_opt (parser, &saved_pedantic))
7591 /* Parse the qualified declaration. */
7592 cp_parser_declaration (parser);
7593 /* Restore the PEDANTIC flag. */
7594 pedantic = saved_pedantic;
7599 /* Try to figure out what kind of declaration is present. */
7600 token1 = *cp_lexer_peek_token (parser->lexer);
7602 if (token1.type != CPP_EOF)
7603 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7606 token2.type = CPP_EOF;
7607 token2.keyword = RID_MAX;
7610 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7611 p = obstack_alloc (&declarator_obstack, 0);
7613 /* If the next token is `extern' and the following token is a string
7614 literal, then we have a linkage specification. */
7615 if (token1.keyword == RID_EXTERN
7616 && cp_parser_is_string_literal (&token2))
7617 cp_parser_linkage_specification (parser);
7618 /* If the next token is `template', then we have either a template
7619 declaration, an explicit instantiation, or an explicit
7621 else if (token1.keyword == RID_TEMPLATE)
7623 /* `template <>' indicates a template specialization. */
7624 if (token2.type == CPP_LESS
7625 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7626 cp_parser_explicit_specialization (parser);
7627 /* `template <' indicates a template declaration. */
7628 else if (token2.type == CPP_LESS)
7629 cp_parser_template_declaration (parser, /*member_p=*/false);
7630 /* Anything else must be an explicit instantiation. */
7632 cp_parser_explicit_instantiation (parser);
7634 /* If the next token is `export', then we have a template
7636 else if (token1.keyword == RID_EXPORT)
7637 cp_parser_template_declaration (parser, /*member_p=*/false);
7638 /* If the next token is `extern', 'static' or 'inline' and the one
7639 after that is `template', we have a GNU extended explicit
7640 instantiation directive. */
7641 else if (cp_parser_allow_gnu_extensions_p (parser)
7642 && (token1.keyword == RID_EXTERN
7643 || token1.keyword == RID_STATIC
7644 || token1.keyword == RID_INLINE)
7645 && token2.keyword == RID_TEMPLATE)
7646 cp_parser_explicit_instantiation (parser);
7647 /* If the next token is `namespace', check for a named or unnamed
7648 namespace definition. */
7649 else if (token1.keyword == RID_NAMESPACE
7650 && (/* A named namespace definition. */
7651 (token2.type == CPP_NAME
7652 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7654 /* An unnamed namespace definition. */
7655 || token2.type == CPP_OPEN_BRACE
7656 || token2.keyword == RID_ATTRIBUTE))
7657 cp_parser_namespace_definition (parser);
7658 /* Objective-C++ declaration/definition. */
7659 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7660 cp_parser_objc_declaration (parser);
7661 /* We must have either a block declaration or a function
7664 /* Try to parse a block-declaration, or a function-definition. */
7665 cp_parser_block_declaration (parser, /*statement_p=*/false);
7667 /* Free any declarators allocated. */
7668 obstack_free (&declarator_obstack, p);
7671 /* Parse a block-declaration.
7676 namespace-alias-definition
7683 __extension__ block-declaration
7686 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7687 part of a declaration-statement. */
7690 cp_parser_block_declaration (cp_parser *parser,
7696 /* Check for the `__extension__' keyword. */
7697 if (cp_parser_extension_opt (parser, &saved_pedantic))
7699 /* Parse the qualified declaration. */
7700 cp_parser_block_declaration (parser, statement_p);
7701 /* Restore the PEDANTIC flag. */
7702 pedantic = saved_pedantic;
7707 /* Peek at the next token to figure out which kind of declaration is
7709 token1 = cp_lexer_peek_token (parser->lexer);
7711 /* If the next keyword is `asm', we have an asm-definition. */
7712 if (token1->keyword == RID_ASM)
7715 cp_parser_commit_to_tentative_parse (parser);
7716 cp_parser_asm_definition (parser);
7718 /* If the next keyword is `namespace', we have a
7719 namespace-alias-definition. */
7720 else if (token1->keyword == RID_NAMESPACE)
7721 cp_parser_namespace_alias_definition (parser);
7722 /* If the next keyword is `using', we have either a
7723 using-declaration or a using-directive. */
7724 else if (token1->keyword == RID_USING)
7729 cp_parser_commit_to_tentative_parse (parser);
7730 /* If the token after `using' is `namespace', then we have a
7732 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7733 if (token2->keyword == RID_NAMESPACE)
7734 cp_parser_using_directive (parser);
7735 /* Otherwise, it's a using-declaration. */
7737 cp_parser_using_declaration (parser,
7738 /*access_declaration_p=*/false);
7740 /* If the next keyword is `__label__' we have a label declaration. */
7741 else if (token1->keyword == RID_LABEL)
7744 cp_parser_commit_to_tentative_parse (parser);
7745 cp_parser_label_declaration (parser);
7747 /* Anything else must be a simple-declaration. */
7749 cp_parser_simple_declaration (parser, !statement_p);
7752 /* Parse a simple-declaration.
7755 decl-specifier-seq [opt] init-declarator-list [opt] ;
7757 init-declarator-list:
7759 init-declarator-list , init-declarator
7761 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7762 function-definition as a simple-declaration. */
7765 cp_parser_simple_declaration (cp_parser* parser,
7766 bool function_definition_allowed_p)
7768 cp_decl_specifier_seq decl_specifiers;
7769 int declares_class_or_enum;
7770 bool saw_declarator;
7772 /* Defer access checks until we know what is being declared; the
7773 checks for names appearing in the decl-specifier-seq should be
7774 done as if we were in the scope of the thing being declared. */
7775 push_deferring_access_checks (dk_deferred);
7777 /* Parse the decl-specifier-seq. We have to keep track of whether
7778 or not the decl-specifier-seq declares a named class or
7779 enumeration type, since that is the only case in which the
7780 init-declarator-list is allowed to be empty.
7784 In a simple-declaration, the optional init-declarator-list can be
7785 omitted only when declaring a class or enumeration, that is when
7786 the decl-specifier-seq contains either a class-specifier, an
7787 elaborated-type-specifier, or an enum-specifier. */
7788 cp_parser_decl_specifier_seq (parser,
7789 CP_PARSER_FLAGS_OPTIONAL,
7791 &declares_class_or_enum);
7792 /* We no longer need to defer access checks. */
7793 stop_deferring_access_checks ();
7795 /* In a block scope, a valid declaration must always have a
7796 decl-specifier-seq. By not trying to parse declarators, we can
7797 resolve the declaration/expression ambiguity more quickly. */
7798 if (!function_definition_allowed_p
7799 && !decl_specifiers.any_specifiers_p)
7801 cp_parser_error (parser, "expected declaration");
7805 /* If the next two tokens are both identifiers, the code is
7806 erroneous. The usual cause of this situation is code like:
7810 where "T" should name a type -- but does not. */
7811 if (!decl_specifiers.type
7812 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7814 /* If parsing tentatively, we should commit; we really are
7815 looking at a declaration. */
7816 cp_parser_commit_to_tentative_parse (parser);
7821 /* If we have seen at least one decl-specifier, and the next token
7822 is not a parenthesis, then we must be looking at a declaration.
7823 (After "int (" we might be looking at a functional cast.) */
7824 if (decl_specifiers.any_specifiers_p
7825 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7826 cp_parser_commit_to_tentative_parse (parser);
7828 /* Keep going until we hit the `;' at the end of the simple
7830 saw_declarator = false;
7831 while (cp_lexer_next_token_is_not (parser->lexer,
7835 bool function_definition_p;
7840 /* If we are processing next declarator, coma is expected */
7841 token = cp_lexer_peek_token (parser->lexer);
7842 gcc_assert (token->type == CPP_COMMA);
7843 cp_lexer_consume_token (parser->lexer);
7846 saw_declarator = true;
7848 /* Parse the init-declarator. */
7849 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7851 function_definition_allowed_p,
7853 declares_class_or_enum,
7854 &function_definition_p);
7855 /* If an error occurred while parsing tentatively, exit quickly.
7856 (That usually happens when in the body of a function; each
7857 statement is treated as a declaration-statement until proven
7859 if (cp_parser_error_occurred (parser))
7861 /* Handle function definitions specially. */
7862 if (function_definition_p)
7864 /* If the next token is a `,', then we are probably
7865 processing something like:
7869 which is erroneous. */
7870 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7871 error ("mixing declarations and function-definitions is forbidden");
7872 /* Otherwise, we're done with the list of declarators. */
7875 pop_deferring_access_checks ();
7879 /* The next token should be either a `,' or a `;'. */
7880 token = cp_lexer_peek_token (parser->lexer);
7881 /* If it's a `,', there are more declarators to come. */
7882 if (token->type == CPP_COMMA)
7883 /* will be consumed next time around */;
7884 /* If it's a `;', we are done. */
7885 else if (token->type == CPP_SEMICOLON)
7887 /* Anything else is an error. */
7890 /* If we have already issued an error message we don't need
7891 to issue another one. */
7892 if (decl != error_mark_node
7893 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7894 cp_parser_error (parser, "expected %<,%> or %<;%>");
7895 /* Skip tokens until we reach the end of the statement. */
7896 cp_parser_skip_to_end_of_statement (parser);
7897 /* If the next token is now a `;', consume it. */
7898 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7899 cp_lexer_consume_token (parser->lexer);
7902 /* After the first time around, a function-definition is not
7903 allowed -- even if it was OK at first. For example:
7908 function_definition_allowed_p = false;
7911 /* Issue an error message if no declarators are present, and the
7912 decl-specifier-seq does not itself declare a class or
7914 if (!saw_declarator)
7916 if (cp_parser_declares_only_class_p (parser))
7917 shadow_tag (&decl_specifiers);
7918 /* Perform any deferred access checks. */
7919 perform_deferred_access_checks ();
7922 /* Consume the `;'. */
7923 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7926 pop_deferring_access_checks ();
7929 /* Parse a decl-specifier-seq.
7932 decl-specifier-seq [opt] decl-specifier
7935 storage-class-specifier
7946 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7948 The parser flags FLAGS is used to control type-specifier parsing.
7950 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7953 1: one of the decl-specifiers is an elaborated-type-specifier
7954 (i.e., a type declaration)
7955 2: one of the decl-specifiers is an enum-specifier or a
7956 class-specifier (i.e., a type definition)
7961 cp_parser_decl_specifier_seq (cp_parser* parser,
7962 cp_parser_flags flags,
7963 cp_decl_specifier_seq *decl_specs,
7964 int* declares_class_or_enum)
7966 bool constructor_possible_p = !parser->in_declarator_p;
7968 /* Clear DECL_SPECS. */
7969 clear_decl_specs (decl_specs);
7971 /* Assume no class or enumeration type is declared. */
7972 *declares_class_or_enum = 0;
7974 /* Keep reading specifiers until there are no more to read. */
7978 bool found_decl_spec;
7981 /* Peek at the next token. */
7982 token = cp_lexer_peek_token (parser->lexer);
7983 /* Handle attributes. */
7984 if (token->keyword == RID_ATTRIBUTE)
7986 /* Parse the attributes. */
7987 decl_specs->attributes
7988 = chainon (decl_specs->attributes,
7989 cp_parser_attributes_opt (parser));
7992 /* Assume we will find a decl-specifier keyword. */
7993 found_decl_spec = true;
7994 /* If the next token is an appropriate keyword, we can simply
7995 add it to the list. */
7996 switch (token->keyword)
8001 if (!at_class_scope_p ())
8003 error ("%<friend%> used outside of class");
8004 cp_lexer_purge_token (parser->lexer);
8008 ++decl_specs->specs[(int) ds_friend];
8009 /* Consume the token. */
8010 cp_lexer_consume_token (parser->lexer);
8014 /* function-specifier:
8021 cp_parser_function_specifier_opt (parser, decl_specs);
8027 ++decl_specs->specs[(int) ds_typedef];
8028 /* Consume the token. */
8029 cp_lexer_consume_token (parser->lexer);
8030 /* A constructor declarator cannot appear in a typedef. */
8031 constructor_possible_p = false;
8032 /* The "typedef" keyword can only occur in a declaration; we
8033 may as well commit at this point. */
8034 cp_parser_commit_to_tentative_parse (parser);
8036 if (decl_specs->storage_class != sc_none)
8037 decl_specs->conflicting_specifiers_p = true;
8040 /* storage-class-specifier:
8054 /* Consume the token. */
8055 cp_lexer_consume_token (parser->lexer);
8056 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8059 /* Consume the token. */
8060 cp_lexer_consume_token (parser->lexer);
8061 ++decl_specs->specs[(int) ds_thread];
8065 /* We did not yet find a decl-specifier yet. */
8066 found_decl_spec = false;
8070 /* Constructors are a special case. The `S' in `S()' is not a
8071 decl-specifier; it is the beginning of the declarator. */
8074 && constructor_possible_p
8075 && (cp_parser_constructor_declarator_p
8076 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8078 /* If we don't have a DECL_SPEC yet, then we must be looking at
8079 a type-specifier. */
8080 if (!found_decl_spec && !constructor_p)
8082 int decl_spec_declares_class_or_enum;
8083 bool is_cv_qualifier;
8087 = cp_parser_type_specifier (parser, flags,
8089 /*is_declaration=*/true,
8090 &decl_spec_declares_class_or_enum,
8093 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8095 /* If this type-specifier referenced a user-defined type
8096 (a typedef, class-name, etc.), then we can't allow any
8097 more such type-specifiers henceforth.
8101 The longest sequence of decl-specifiers that could
8102 possibly be a type name is taken as the
8103 decl-specifier-seq of a declaration. The sequence shall
8104 be self-consistent as described below.
8108 As a general rule, at most one type-specifier is allowed
8109 in the complete decl-specifier-seq of a declaration. The
8110 only exceptions are the following:
8112 -- const or volatile can be combined with any other
8115 -- signed or unsigned can be combined with char, long,
8123 void g (const int Pc);
8125 Here, Pc is *not* part of the decl-specifier seq; it's
8126 the declarator. Therefore, once we see a type-specifier
8127 (other than a cv-qualifier), we forbid any additional
8128 user-defined types. We *do* still allow things like `int
8129 int' to be considered a decl-specifier-seq, and issue the
8130 error message later. */
8131 if (type_spec && !is_cv_qualifier)
8132 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8133 /* A constructor declarator cannot follow a type-specifier. */
8136 constructor_possible_p = false;
8137 found_decl_spec = true;
8141 /* If we still do not have a DECL_SPEC, then there are no more
8143 if (!found_decl_spec)
8146 decl_specs->any_specifiers_p = true;
8147 /* After we see one decl-specifier, further decl-specifiers are
8149 flags |= CP_PARSER_FLAGS_OPTIONAL;
8152 cp_parser_check_decl_spec (decl_specs);
8154 /* Don't allow a friend specifier with a class definition. */
8155 if (decl_specs->specs[(int) ds_friend] != 0
8156 && (*declares_class_or_enum & 2))
8157 error ("class definition may not be declared a friend");
8160 /* Parse an (optional) storage-class-specifier.
8162 storage-class-specifier:
8171 storage-class-specifier:
8174 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8177 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8179 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8187 /* Consume the token. */
8188 return cp_lexer_consume_token (parser->lexer)->u.value;
8195 /* Parse an (optional) function-specifier.
8202 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8203 Updates DECL_SPECS, if it is non-NULL. */
8206 cp_parser_function_specifier_opt (cp_parser* parser,
8207 cp_decl_specifier_seq *decl_specs)
8209 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8213 ++decl_specs->specs[(int) ds_inline];
8217 /* 14.5.2.3 [temp.mem]
8219 A member function template shall not be virtual. */
8220 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8221 error ("templates may not be %<virtual%>");
8222 else if (decl_specs)
8223 ++decl_specs->specs[(int) ds_virtual];
8228 ++decl_specs->specs[(int) ds_explicit];
8235 /* Consume the token. */
8236 return cp_lexer_consume_token (parser->lexer)->u.value;
8239 /* Parse a linkage-specification.
8241 linkage-specification:
8242 extern string-literal { declaration-seq [opt] }
8243 extern string-literal declaration */
8246 cp_parser_linkage_specification (cp_parser* parser)
8250 /* Look for the `extern' keyword. */
8251 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8253 /* Look for the string-literal. */
8254 linkage = cp_parser_string_literal (parser, false, false);
8256 /* Transform the literal into an identifier. If the literal is a
8257 wide-character string, or contains embedded NULs, then we can't
8258 handle it as the user wants. */
8259 if (strlen (TREE_STRING_POINTER (linkage))
8260 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8262 cp_parser_error (parser, "invalid linkage-specification");
8263 /* Assume C++ linkage. */
8264 linkage = lang_name_cplusplus;
8267 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8269 /* We're now using the new linkage. */
8270 push_lang_context (linkage);
8272 /* If the next token is a `{', then we're using the first
8274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8276 /* Consume the `{' token. */
8277 cp_lexer_consume_token (parser->lexer);
8278 /* Parse the declarations. */
8279 cp_parser_declaration_seq_opt (parser);
8280 /* Look for the closing `}'. */
8281 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8283 /* Otherwise, there's just one declaration. */
8286 bool saved_in_unbraced_linkage_specification_p;
8288 saved_in_unbraced_linkage_specification_p
8289 = parser->in_unbraced_linkage_specification_p;
8290 parser->in_unbraced_linkage_specification_p = true;
8291 cp_parser_declaration (parser);
8292 parser->in_unbraced_linkage_specification_p
8293 = saved_in_unbraced_linkage_specification_p;
8296 /* We're done with the linkage-specification. */
8297 pop_lang_context ();
8300 /* Special member functions [gram.special] */
8302 /* Parse a conversion-function-id.
8304 conversion-function-id:
8305 operator conversion-type-id
8307 Returns an IDENTIFIER_NODE representing the operator. */
8310 cp_parser_conversion_function_id (cp_parser* parser)
8314 tree saved_qualifying_scope;
8315 tree saved_object_scope;
8316 tree pushed_scope = NULL_TREE;
8318 /* Look for the `operator' token. */
8319 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8320 return error_mark_node;
8321 /* When we parse the conversion-type-id, the current scope will be
8322 reset. However, we need that information in able to look up the
8323 conversion function later, so we save it here. */
8324 saved_scope = parser->scope;
8325 saved_qualifying_scope = parser->qualifying_scope;
8326 saved_object_scope = parser->object_scope;
8327 /* We must enter the scope of the class so that the names of
8328 entities declared within the class are available in the
8329 conversion-type-id. For example, consider:
8336 S::operator I() { ... }
8338 In order to see that `I' is a type-name in the definition, we
8339 must be in the scope of `S'. */
8341 pushed_scope = push_scope (saved_scope);
8342 /* Parse the conversion-type-id. */
8343 type = cp_parser_conversion_type_id (parser);
8344 /* Leave the scope of the class, if any. */
8346 pop_scope (pushed_scope);
8347 /* Restore the saved scope. */
8348 parser->scope = saved_scope;
8349 parser->qualifying_scope = saved_qualifying_scope;
8350 parser->object_scope = saved_object_scope;
8351 /* If the TYPE is invalid, indicate failure. */
8352 if (type == error_mark_node)
8353 return error_mark_node;
8354 return mangle_conv_op_name_for_type (type);
8357 /* Parse a conversion-type-id:
8360 type-specifier-seq conversion-declarator [opt]
8362 Returns the TYPE specified. */
8365 cp_parser_conversion_type_id (cp_parser* parser)
8368 cp_decl_specifier_seq type_specifiers;
8369 cp_declarator *declarator;
8370 tree type_specified;
8372 /* Parse the attributes. */
8373 attributes = cp_parser_attributes_opt (parser);
8374 /* Parse the type-specifiers. */
8375 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8377 /* If that didn't work, stop. */
8378 if (type_specifiers.type == error_mark_node)
8379 return error_mark_node;
8380 /* Parse the conversion-declarator. */
8381 declarator = cp_parser_conversion_declarator_opt (parser);
8383 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8384 /*initialized=*/0, &attributes);
8386 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8387 return type_specified;
8390 /* Parse an (optional) conversion-declarator.
8392 conversion-declarator:
8393 ptr-operator conversion-declarator [opt]
8397 static cp_declarator *
8398 cp_parser_conversion_declarator_opt (cp_parser* parser)
8400 enum tree_code code;
8402 cp_cv_quals cv_quals;
8404 /* We don't know if there's a ptr-operator next, or not. */
8405 cp_parser_parse_tentatively (parser);
8406 /* Try the ptr-operator. */
8407 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8408 /* If it worked, look for more conversion-declarators. */
8409 if (cp_parser_parse_definitely (parser))
8411 cp_declarator *declarator;
8413 /* Parse another optional declarator. */
8414 declarator = cp_parser_conversion_declarator_opt (parser);
8416 /* Create the representation of the declarator. */
8418 declarator = make_ptrmem_declarator (cv_quals, class_type,
8420 else if (code == INDIRECT_REF)
8421 declarator = make_pointer_declarator (cv_quals, declarator);
8423 declarator = make_reference_declarator (cv_quals, declarator);
8431 /* Parse an (optional) ctor-initializer.
8434 : mem-initializer-list
8436 Returns TRUE iff the ctor-initializer was actually present. */
8439 cp_parser_ctor_initializer_opt (cp_parser* parser)
8441 /* If the next token is not a `:', then there is no
8442 ctor-initializer. */
8443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8445 /* Do default initialization of any bases and members. */
8446 if (DECL_CONSTRUCTOR_P (current_function_decl))
8447 finish_mem_initializers (NULL_TREE);
8452 /* Consume the `:' token. */
8453 cp_lexer_consume_token (parser->lexer);
8454 /* And the mem-initializer-list. */
8455 cp_parser_mem_initializer_list (parser);
8460 /* Parse a mem-initializer-list.
8462 mem-initializer-list:
8464 mem-initializer , mem-initializer-list */
8467 cp_parser_mem_initializer_list (cp_parser* parser)
8469 tree mem_initializer_list = NULL_TREE;
8471 /* Let the semantic analysis code know that we are starting the
8472 mem-initializer-list. */
8473 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8474 error ("only constructors take base initializers");
8476 /* Loop through the list. */
8479 tree mem_initializer;
8481 /* Parse the mem-initializer. */
8482 mem_initializer = cp_parser_mem_initializer (parser);
8483 /* Add it to the list, unless it was erroneous. */
8484 if (mem_initializer != error_mark_node)
8486 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8487 mem_initializer_list = mem_initializer;
8489 /* If the next token is not a `,', we're done. */
8490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8492 /* Consume the `,' token. */
8493 cp_lexer_consume_token (parser->lexer);
8496 /* Perform semantic analysis. */
8497 if (DECL_CONSTRUCTOR_P (current_function_decl))
8498 finish_mem_initializers (mem_initializer_list);
8501 /* Parse a mem-initializer.
8504 mem-initializer-id ( expression-list [opt] )
8509 ( expression-list [opt] )
8511 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8512 class) or FIELD_DECL (for a non-static data member) to initialize;
8513 the TREE_VALUE is the expression-list. An empty initialization
8514 list is represented by void_list_node. */
8517 cp_parser_mem_initializer (cp_parser* parser)
8519 tree mem_initializer_id;
8520 tree expression_list;
8523 /* Find out what is being initialized. */
8524 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8526 pedwarn ("anachronistic old-style base class initializer");
8527 mem_initializer_id = NULL_TREE;
8530 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8531 member = expand_member_init (mem_initializer_id);
8532 if (member && !DECL_P (member))
8533 in_base_initializer = 1;
8536 = cp_parser_parenthesized_expression_list (parser, false,
8538 /*non_constant_p=*/NULL);
8539 if (expression_list == error_mark_node)
8540 return error_mark_node;
8541 if (!expression_list)
8542 expression_list = void_type_node;
8544 in_base_initializer = 0;
8546 return member ? build_tree_list (member, expression_list) : error_mark_node;
8549 /* Parse a mem-initializer-id.
8552 :: [opt] nested-name-specifier [opt] class-name
8555 Returns a TYPE indicating the class to be initializer for the first
8556 production. Returns an IDENTIFIER_NODE indicating the data member
8557 to be initialized for the second production. */
8560 cp_parser_mem_initializer_id (cp_parser* parser)
8562 bool global_scope_p;
8563 bool nested_name_specifier_p;
8564 bool template_p = false;
8567 /* `typename' is not allowed in this context ([temp.res]). */
8568 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8570 error ("keyword %<typename%> not allowed in this context (a qualified "
8571 "member initializer is implicitly a type)");
8572 cp_lexer_consume_token (parser->lexer);
8574 /* Look for the optional `::' operator. */
8576 = (cp_parser_global_scope_opt (parser,
8577 /*current_scope_valid_p=*/false)
8579 /* Look for the optional nested-name-specifier. The simplest way to
8584 The keyword `typename' is not permitted in a base-specifier or
8585 mem-initializer; in these contexts a qualified name that
8586 depends on a template-parameter is implicitly assumed to be a
8589 is to assume that we have seen the `typename' keyword at this
8591 nested_name_specifier_p
8592 = (cp_parser_nested_name_specifier_opt (parser,
8593 /*typename_keyword_p=*/true,
8594 /*check_dependency_p=*/true,
8596 /*is_declaration=*/true)
8598 if (nested_name_specifier_p)
8599 template_p = cp_parser_optional_template_keyword (parser);
8600 /* If there is a `::' operator or a nested-name-specifier, then we
8601 are definitely looking for a class-name. */
8602 if (global_scope_p || nested_name_specifier_p)
8603 return cp_parser_class_name (parser,
8604 /*typename_keyword_p=*/true,
8605 /*template_keyword_p=*/template_p,
8607 /*check_dependency_p=*/true,
8608 /*class_head_p=*/false,
8609 /*is_declaration=*/true);
8610 /* Otherwise, we could also be looking for an ordinary identifier. */
8611 cp_parser_parse_tentatively (parser);
8612 /* Try a class-name. */
8613 id = cp_parser_class_name (parser,
8614 /*typename_keyword_p=*/true,
8615 /*template_keyword_p=*/false,
8617 /*check_dependency_p=*/true,
8618 /*class_head_p=*/false,
8619 /*is_declaration=*/true);
8620 /* If we found one, we're done. */
8621 if (cp_parser_parse_definitely (parser))
8623 /* Otherwise, look for an ordinary identifier. */
8624 return cp_parser_identifier (parser);
8627 /* Overloading [gram.over] */
8629 /* Parse an operator-function-id.
8631 operator-function-id:
8634 Returns an IDENTIFIER_NODE for the operator which is a
8635 human-readable spelling of the identifier, e.g., `operator +'. */
8638 cp_parser_operator_function_id (cp_parser* parser)
8640 /* Look for the `operator' keyword. */
8641 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8642 return error_mark_node;
8643 /* And then the name of the operator itself. */
8644 return cp_parser_operator (parser);
8647 /* Parse an operator.
8650 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8651 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8652 || ++ -- , ->* -> () []
8659 Returns an IDENTIFIER_NODE for the operator which is a
8660 human-readable spelling of the identifier, e.g., `operator +'. */
8663 cp_parser_operator (cp_parser* parser)
8665 tree id = NULL_TREE;
8668 /* Peek at the next token. */
8669 token = cp_lexer_peek_token (parser->lexer);
8670 /* Figure out which operator we have. */
8671 switch (token->type)
8677 /* The keyword should be either `new' or `delete'. */
8678 if (token->keyword == RID_NEW)
8680 else if (token->keyword == RID_DELETE)
8685 /* Consume the `new' or `delete' token. */
8686 cp_lexer_consume_token (parser->lexer);
8688 /* Peek at the next token. */
8689 token = cp_lexer_peek_token (parser->lexer);
8690 /* If it's a `[' token then this is the array variant of the
8692 if (token->type == CPP_OPEN_SQUARE)
8694 /* Consume the `[' token. */
8695 cp_lexer_consume_token (parser->lexer);
8696 /* Look for the `]' token. */
8697 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8698 id = ansi_opname (op == NEW_EXPR
8699 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8701 /* Otherwise, we have the non-array variant. */
8703 id = ansi_opname (op);
8709 id = ansi_opname (PLUS_EXPR);
8713 id = ansi_opname (MINUS_EXPR);
8717 id = ansi_opname (MULT_EXPR);
8721 id = ansi_opname (TRUNC_DIV_EXPR);
8725 id = ansi_opname (TRUNC_MOD_EXPR);
8729 id = ansi_opname (BIT_XOR_EXPR);
8733 id = ansi_opname (BIT_AND_EXPR);
8737 id = ansi_opname (BIT_IOR_EXPR);
8741 id = ansi_opname (BIT_NOT_EXPR);
8745 id = ansi_opname (TRUTH_NOT_EXPR);
8749 id = ansi_assopname (NOP_EXPR);
8753 id = ansi_opname (LT_EXPR);
8757 id = ansi_opname (GT_EXPR);
8761 id = ansi_assopname (PLUS_EXPR);
8765 id = ansi_assopname (MINUS_EXPR);
8769 id = ansi_assopname (MULT_EXPR);
8773 id = ansi_assopname (TRUNC_DIV_EXPR);
8777 id = ansi_assopname (TRUNC_MOD_EXPR);
8781 id = ansi_assopname (BIT_XOR_EXPR);
8785 id = ansi_assopname (BIT_AND_EXPR);
8789 id = ansi_assopname (BIT_IOR_EXPR);
8793 id = ansi_opname (LSHIFT_EXPR);
8797 id = ansi_opname (RSHIFT_EXPR);
8801 id = ansi_assopname (LSHIFT_EXPR);
8805 id = ansi_assopname (RSHIFT_EXPR);
8809 id = ansi_opname (EQ_EXPR);
8813 id = ansi_opname (NE_EXPR);
8817 id = ansi_opname (LE_EXPR);
8820 case CPP_GREATER_EQ:
8821 id = ansi_opname (GE_EXPR);
8825 id = ansi_opname (TRUTH_ANDIF_EXPR);
8829 id = ansi_opname (TRUTH_ORIF_EXPR);
8833 id = ansi_opname (POSTINCREMENT_EXPR);
8836 case CPP_MINUS_MINUS:
8837 id = ansi_opname (PREDECREMENT_EXPR);
8841 id = ansi_opname (COMPOUND_EXPR);
8844 case CPP_DEREF_STAR:
8845 id = ansi_opname (MEMBER_REF);
8849 id = ansi_opname (COMPONENT_REF);
8852 case CPP_OPEN_PAREN:
8853 /* Consume the `('. */
8854 cp_lexer_consume_token (parser->lexer);
8855 /* Look for the matching `)'. */
8856 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8857 return ansi_opname (CALL_EXPR);
8859 case CPP_OPEN_SQUARE:
8860 /* Consume the `['. */
8861 cp_lexer_consume_token (parser->lexer);
8862 /* Look for the matching `]'. */
8863 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8864 return ansi_opname (ARRAY_REF);
8867 /* Anything else is an error. */
8871 /* If we have selected an identifier, we need to consume the
8874 cp_lexer_consume_token (parser->lexer);
8875 /* Otherwise, no valid operator name was present. */
8878 cp_parser_error (parser, "expected operator");
8879 id = error_mark_node;
8885 /* Parse a template-declaration.
8887 template-declaration:
8888 export [opt] template < template-parameter-list > declaration
8890 If MEMBER_P is TRUE, this template-declaration occurs within a
8893 The grammar rule given by the standard isn't correct. What
8896 template-declaration:
8897 export [opt] template-parameter-list-seq
8898 decl-specifier-seq [opt] init-declarator [opt] ;
8899 export [opt] template-parameter-list-seq
8902 template-parameter-list-seq:
8903 template-parameter-list-seq [opt]
8904 template < template-parameter-list > */
8907 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8909 /* Check for `export'. */
8910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8912 /* Consume the `export' token. */
8913 cp_lexer_consume_token (parser->lexer);
8914 /* Warn that we do not support `export'. */
8915 warning (0, "keyword %<export%> not implemented, and will be ignored");
8918 cp_parser_template_declaration_after_export (parser, member_p);
8921 /* Parse a template-parameter-list.
8923 template-parameter-list:
8925 template-parameter-list , template-parameter
8927 Returns a TREE_LIST. Each node represents a template parameter.
8928 The nodes are connected via their TREE_CHAINs. */
8931 cp_parser_template_parameter_list (cp_parser* parser)
8933 tree parameter_list = NULL_TREE;
8935 begin_template_parm_list ();
8942 /* Parse the template-parameter. */
8943 parameter = cp_parser_template_parameter (parser, &is_non_type);
8944 /* Add it to the list. */
8945 if (parameter != error_mark_node)
8946 parameter_list = process_template_parm (parameter_list,
8951 tree err_parm = build_tree_list (parameter, parameter);
8952 TREE_VALUE (err_parm) = error_mark_node;
8953 parameter_list = chainon (parameter_list, err_parm);
8956 /* Peek at the next token. */
8957 token = cp_lexer_peek_token (parser->lexer);
8958 /* If it's not a `,', we're done. */
8959 if (token->type != CPP_COMMA)
8961 /* Otherwise, consume the `,' token. */
8962 cp_lexer_consume_token (parser->lexer);
8965 return end_template_parm_list (parameter_list);
8968 /* Parse a template-parameter.
8972 parameter-declaration
8974 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8975 the parameter. The TREE_PURPOSE is the default value, if any.
8976 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8977 iff this parameter is a non-type parameter. */
8980 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8983 cp_parameter_declarator *parameter_declarator;
8986 /* Assume it is a type parameter or a template parameter. */
8987 *is_non_type = false;
8988 /* Peek at the next token. */
8989 token = cp_lexer_peek_token (parser->lexer);
8990 /* If it is `class' or `template', we have a type-parameter. */
8991 if (token->keyword == RID_TEMPLATE)
8992 return cp_parser_type_parameter (parser);
8993 /* If it is `class' or `typename' we do not know yet whether it is a
8994 type parameter or a non-type parameter. Consider:
8996 template <typename T, typename T::X X> ...
9000 template <class C, class D*> ...
9002 Here, the first parameter is a type parameter, and the second is
9003 a non-type parameter. We can tell by looking at the token after
9004 the identifier -- if it is a `,', `=', or `>' then we have a type
9006 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9008 /* Peek at the token after `class' or `typename'. */
9009 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9010 /* If it's an identifier, skip it. */
9011 if (token->type == CPP_NAME)
9012 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9013 /* Now, see if the token looks like the end of a template
9015 if (token->type == CPP_COMMA
9016 || token->type == CPP_EQ
9017 || token->type == CPP_GREATER)
9018 return cp_parser_type_parameter (parser);
9021 /* Otherwise, it is a non-type parameter.
9025 When parsing a default template-argument for a non-type
9026 template-parameter, the first non-nested `>' is taken as the end
9027 of the template parameter-list rather than a greater-than
9029 *is_non_type = true;
9030 parameter_declarator
9031 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9032 /*parenthesized_p=*/NULL);
9033 parm = grokdeclarator (parameter_declarator->declarator,
9034 ¶meter_declarator->decl_specifiers,
9035 PARM, /*initialized=*/0,
9037 if (parm == error_mark_node)
9038 return error_mark_node;
9039 return build_tree_list (parameter_declarator->default_argument, parm);
9042 /* Parse a type-parameter.
9045 class identifier [opt]
9046 class identifier [opt] = type-id
9047 typename identifier [opt]
9048 typename identifier [opt] = type-id
9049 template < template-parameter-list > class identifier [opt]
9050 template < template-parameter-list > class identifier [opt]
9053 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9054 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9055 the declaration of the parameter. */
9058 cp_parser_type_parameter (cp_parser* parser)
9063 /* Look for a keyword to tell us what kind of parameter this is. */
9064 token = cp_parser_require (parser, CPP_KEYWORD,
9065 "`class', `typename', or `template'");
9067 return error_mark_node;
9069 switch (token->keyword)
9075 tree default_argument;
9077 /* If the next token is an identifier, then it names the
9079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9080 identifier = cp_parser_identifier (parser);
9082 identifier = NULL_TREE;
9084 /* Create the parameter. */
9085 parameter = finish_template_type_parm (class_type_node, identifier);
9087 /* If the next token is an `=', we have a default argument. */
9088 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9090 /* Consume the `=' token. */
9091 cp_lexer_consume_token (parser->lexer);
9092 /* Parse the default-argument. */
9093 push_deferring_access_checks (dk_no_deferred);
9094 default_argument = cp_parser_type_id (parser);
9095 pop_deferring_access_checks ();
9098 default_argument = NULL_TREE;
9100 /* Create the combined representation of the parameter and the
9101 default argument. */
9102 parameter = build_tree_list (default_argument, parameter);
9108 tree parameter_list;
9110 tree default_argument;
9112 /* Look for the `<'. */
9113 cp_parser_require (parser, CPP_LESS, "`<'");
9114 /* Parse the template-parameter-list. */
9115 parameter_list = cp_parser_template_parameter_list (parser);
9116 /* Look for the `>'. */
9117 cp_parser_require (parser, CPP_GREATER, "`>'");
9118 /* Look for the `class' keyword. */
9119 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9120 /* If the next token is an `=', then there is a
9121 default-argument. If the next token is a `>', we are at
9122 the end of the parameter-list. If the next token is a `,',
9123 then we are at the end of this parameter. */
9124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9125 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9126 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9128 identifier = cp_parser_identifier (parser);
9129 /* Treat invalid names as if the parameter were nameless. */
9130 if (identifier == error_mark_node)
9131 identifier = NULL_TREE;
9134 identifier = NULL_TREE;
9136 /* Create the template parameter. */
9137 parameter = finish_template_template_parm (class_type_node,
9140 /* If the next token is an `=', then there is a
9141 default-argument. */
9142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9146 /* Consume the `='. */
9147 cp_lexer_consume_token (parser->lexer);
9148 /* Parse the id-expression. */
9149 push_deferring_access_checks (dk_no_deferred);
9151 = cp_parser_id_expression (parser,
9152 /*template_keyword_p=*/false,
9153 /*check_dependency_p=*/true,
9154 /*template_p=*/&is_template,
9155 /*declarator_p=*/false,
9156 /*optional_p=*/false);
9157 if (TREE_CODE (default_argument) == TYPE_DECL)
9158 /* If the id-expression was a template-id that refers to
9159 a template-class, we already have the declaration here,
9160 so no further lookup is needed. */
9163 /* Look up the name. */
9165 = cp_parser_lookup_name (parser, default_argument,
9167 /*is_template=*/is_template,
9168 /*is_namespace=*/false,
9169 /*check_dependency=*/true,
9170 /*ambiguous_decls=*/NULL);
9171 /* See if the default argument is valid. */
9173 = check_template_template_default_arg (default_argument);
9174 pop_deferring_access_checks ();
9177 default_argument = NULL_TREE;
9179 /* Create the combined representation of the parameter and the
9180 default argument. */
9181 parameter = build_tree_list (default_argument, parameter);
9193 /* Parse a template-id.
9196 template-name < template-argument-list [opt] >
9198 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9199 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9200 returned. Otherwise, if the template-name names a function, or set
9201 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9202 names a class, returns a TYPE_DECL for the specialization.
9204 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9205 uninstantiated templates. */
9208 cp_parser_template_id (cp_parser *parser,
9209 bool template_keyword_p,
9210 bool check_dependency_p,
9211 bool is_declaration)
9217 cp_token_position start_of_id = 0;
9218 deferred_access_check *chk;
9219 VEC (deferred_access_check,gc) *access_check;
9220 cp_token *next_token, *next_token_2;
9223 /* If the next token corresponds to a template-id, there is no need
9225 next_token = cp_lexer_peek_token (parser->lexer);
9226 if (next_token->type == CPP_TEMPLATE_ID)
9228 struct tree_check *check_value;
9230 /* Get the stored value. */
9231 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9232 /* Perform any access checks that were deferred. */
9233 access_check = check_value->checks;
9237 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9240 perform_or_defer_access_check (chk->binfo,
9245 /* Return the stored value. */
9246 return check_value->value;
9249 /* Avoid performing name lookup if there is no possibility of
9250 finding a template-id. */
9251 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9252 || (next_token->type == CPP_NAME
9253 && !cp_parser_nth_token_starts_template_argument_list_p
9256 cp_parser_error (parser, "expected template-id");
9257 return error_mark_node;
9260 /* Remember where the template-id starts. */
9261 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9262 start_of_id = cp_lexer_token_position (parser->lexer, false);
9264 push_deferring_access_checks (dk_deferred);
9266 /* Parse the template-name. */
9267 is_identifier = false;
9268 template = cp_parser_template_name (parser, template_keyword_p,
9272 if (template == error_mark_node || is_identifier)
9274 pop_deferring_access_checks ();
9278 /* If we find the sequence `[:' after a template-name, it's probably
9279 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9280 parse correctly the argument list. */
9281 next_token = cp_lexer_peek_token (parser->lexer);
9282 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9283 if (next_token->type == CPP_OPEN_SQUARE
9284 && next_token->flags & DIGRAPH
9285 && next_token_2->type == CPP_COLON
9286 && !(next_token_2->flags & PREV_WHITE))
9288 cp_parser_parse_tentatively (parser);
9289 /* Change `:' into `::'. */
9290 next_token_2->type = CPP_SCOPE;
9291 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9293 cp_lexer_consume_token (parser->lexer);
9294 /* Parse the arguments. */
9295 arguments = cp_parser_enclosed_template_argument_list (parser);
9296 if (!cp_parser_parse_definitely (parser))
9298 /* If we couldn't parse an argument list, then we revert our changes
9299 and return simply an error. Maybe this is not a template-id
9301 next_token_2->type = CPP_COLON;
9302 cp_parser_error (parser, "expected %<<%>");
9303 pop_deferring_access_checks ();
9304 return error_mark_node;
9306 /* Otherwise, emit an error about the invalid digraph, but continue
9307 parsing because we got our argument list. */
9308 pedwarn ("%<<::%> cannot begin a template-argument list");
9309 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9310 "between %<<%> and %<::%>");
9311 if (!flag_permissive)
9316 inform ("(if you use -fpermissive G++ will accept your code)");
9323 /* Look for the `<' that starts the template-argument-list. */
9324 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9326 pop_deferring_access_checks ();
9327 return error_mark_node;
9329 /* Parse the arguments. */
9330 arguments = cp_parser_enclosed_template_argument_list (parser);
9333 /* Build a representation of the specialization. */
9334 if (TREE_CODE (template) == IDENTIFIER_NODE)
9335 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9336 else if (DECL_CLASS_TEMPLATE_P (template)
9337 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9339 bool entering_scope;
9340 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9341 template (rather than some instantiation thereof) only if
9342 is not nested within some other construct. For example, in
9343 "template <typename T> void f(T) { A<T>::", A<T> is just an
9344 instantiation of A. */
9345 entering_scope = (template_parm_scope_p ()
9346 && cp_lexer_next_token_is (parser->lexer,
9349 = finish_template_type (template, arguments, entering_scope);
9353 /* If it's not a class-template or a template-template, it should be
9354 a function-template. */
9355 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9356 || TREE_CODE (template) == OVERLOAD
9357 || BASELINK_P (template)));
9359 template_id = lookup_template_function (template, arguments);
9362 /* If parsing tentatively, replace the sequence of tokens that makes
9363 up the template-id with a CPP_TEMPLATE_ID token. That way,
9364 should we re-parse the token stream, we will not have to repeat
9365 the effort required to do the parse, nor will we issue duplicate
9366 error messages about problems during instantiation of the
9370 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9372 /* Reset the contents of the START_OF_ID token. */
9373 token->type = CPP_TEMPLATE_ID;
9374 /* Retrieve any deferred checks. Do not pop this access checks yet
9375 so the memory will not be reclaimed during token replacing below. */
9376 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9377 token->u.tree_check_value->value = template_id;
9378 token->u.tree_check_value->checks = get_deferred_access_checks ();
9379 token->keyword = RID_MAX;
9381 /* Purge all subsequent tokens. */
9382 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9384 /* ??? Can we actually assume that, if template_id ==
9385 error_mark_node, we will have issued a diagnostic to the
9386 user, as opposed to simply marking the tentative parse as
9388 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9389 error ("parse error in template argument list");
9392 pop_deferring_access_checks ();
9396 /* Parse a template-name.
9401 The standard should actually say:
9405 operator-function-id
9407 A defect report has been filed about this issue.
9409 A conversion-function-id cannot be a template name because they cannot
9410 be part of a template-id. In fact, looking at this code:
9414 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9415 It is impossible to call a templated conversion-function-id with an
9416 explicit argument list, since the only allowed template parameter is
9417 the type to which it is converting.
9419 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9420 `template' keyword, in a construction like:
9424 In that case `f' is taken to be a template-name, even though there
9425 is no way of knowing for sure.
9427 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9428 name refers to a set of overloaded functions, at least one of which
9429 is a template, or an IDENTIFIER_NODE with the name of the template,
9430 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9431 names are looked up inside uninstantiated templates. */
9434 cp_parser_template_name (cp_parser* parser,
9435 bool template_keyword_p,
9436 bool check_dependency_p,
9437 bool is_declaration,
9438 bool *is_identifier)
9444 /* If the next token is `operator', then we have either an
9445 operator-function-id or a conversion-function-id. */
9446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9448 /* We don't know whether we're looking at an
9449 operator-function-id or a conversion-function-id. */
9450 cp_parser_parse_tentatively (parser);
9451 /* Try an operator-function-id. */
9452 identifier = cp_parser_operator_function_id (parser);
9453 /* If that didn't work, try a conversion-function-id. */
9454 if (!cp_parser_parse_definitely (parser))
9456 cp_parser_error (parser, "expected template-name");
9457 return error_mark_node;
9460 /* Look for the identifier. */
9462 identifier = cp_parser_identifier (parser);
9464 /* If we didn't find an identifier, we don't have a template-id. */
9465 if (identifier == error_mark_node)
9466 return error_mark_node;
9468 /* If the name immediately followed the `template' keyword, then it
9469 is a template-name. However, if the next token is not `<', then
9470 we do not treat it as a template-name, since it is not being used
9471 as part of a template-id. This enables us to handle constructs
9474 template <typename T> struct S { S(); };
9475 template <typename T> S<T>::S();
9477 correctly. We would treat `S' as a template -- if it were `S<T>'
9478 -- but we do not if there is no `<'. */
9480 if (processing_template_decl
9481 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9483 /* In a declaration, in a dependent context, we pretend that the
9484 "template" keyword was present in order to improve error
9485 recovery. For example, given:
9487 template <typename T> void f(T::X<int>);
9489 we want to treat "X<int>" as a template-id. */
9491 && !template_keyword_p
9492 && parser->scope && TYPE_P (parser->scope)
9493 && check_dependency_p
9494 && dependent_type_p (parser->scope)
9495 /* Do not do this for dtors (or ctors), since they never
9496 need the template keyword before their name. */
9497 && !constructor_name_p (identifier, parser->scope))
9499 cp_token_position start = 0;
9501 /* Explain what went wrong. */
9502 error ("non-template %qD used as template", identifier);
9503 inform ("use %<%T::template %D%> to indicate that it is a template",
9504 parser->scope, identifier);
9505 /* If parsing tentatively, find the location of the "<" token. */
9506 if (cp_parser_simulate_error (parser))
9507 start = cp_lexer_token_position (parser->lexer, true);
9508 /* Parse the template arguments so that we can issue error
9509 messages about them. */
9510 cp_lexer_consume_token (parser->lexer);
9511 cp_parser_enclosed_template_argument_list (parser);
9512 /* Skip tokens until we find a good place from which to
9513 continue parsing. */
9514 cp_parser_skip_to_closing_parenthesis (parser,
9515 /*recovering=*/true,
9517 /*consume_paren=*/false);
9518 /* If parsing tentatively, permanently remove the
9519 template argument list. That will prevent duplicate
9520 error messages from being issued about the missing
9521 "template" keyword. */
9523 cp_lexer_purge_tokens_after (parser->lexer, start);
9525 *is_identifier = true;
9529 /* If the "template" keyword is present, then there is generally
9530 no point in doing name-lookup, so we just return IDENTIFIER.
9531 But, if the qualifying scope is non-dependent then we can
9532 (and must) do name-lookup normally. */
9533 if (template_keyword_p
9535 || (TYPE_P (parser->scope)
9536 && dependent_type_p (parser->scope))))
9540 /* Look up the name. */
9541 decl = cp_parser_lookup_name (parser, identifier,
9543 /*is_template=*/false,
9544 /*is_namespace=*/false,
9546 /*ambiguous_decls=*/NULL);
9547 decl = maybe_get_template_decl_from_type_decl (decl);
9549 /* If DECL is a template, then the name was a template-name. */
9550 if (TREE_CODE (decl) == TEMPLATE_DECL)
9554 tree fn = NULL_TREE;
9556 /* The standard does not explicitly indicate whether a name that
9557 names a set of overloaded declarations, some of which are
9558 templates, is a template-name. However, such a name should
9559 be a template-name; otherwise, there is no way to form a
9560 template-id for the overloaded templates. */
9561 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9562 if (TREE_CODE (fns) == OVERLOAD)
9563 for (fn = fns; fn; fn = OVL_NEXT (fn))
9564 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9569 /* The name does not name a template. */
9570 cp_parser_error (parser, "expected template-name");
9571 return error_mark_node;
9575 /* If DECL is dependent, and refers to a function, then just return
9576 its name; we will look it up again during template instantiation. */
9577 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9579 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9580 if (TYPE_P (scope) && dependent_type_p (scope))
9587 /* Parse a template-argument-list.
9589 template-argument-list:
9591 template-argument-list , template-argument
9593 Returns a TREE_VEC containing the arguments. */
9596 cp_parser_template_argument_list (cp_parser* parser)
9598 tree fixed_args[10];
9599 unsigned n_args = 0;
9600 unsigned alloced = 10;
9601 tree *arg_ary = fixed_args;
9603 bool saved_in_template_argument_list_p;
9605 bool saved_non_ice_p;
9607 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9608 parser->in_template_argument_list_p = true;
9609 /* Even if the template-id appears in an integral
9610 constant-expression, the contents of the argument list do
9612 saved_ice_p = parser->integral_constant_expression_p;
9613 parser->integral_constant_expression_p = false;
9614 saved_non_ice_p = parser->non_integral_constant_expression_p;
9615 parser->non_integral_constant_expression_p = false;
9616 /* Parse the arguments. */
9622 /* Consume the comma. */
9623 cp_lexer_consume_token (parser->lexer);
9625 /* Parse the template-argument. */
9626 argument = cp_parser_template_argument (parser);
9627 if (n_args == alloced)
9631 if (arg_ary == fixed_args)
9633 arg_ary = XNEWVEC (tree, alloced);
9634 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9637 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9639 arg_ary[n_args++] = argument;
9641 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9643 vec = make_tree_vec (n_args);
9646 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9648 if (arg_ary != fixed_args)
9650 parser->non_integral_constant_expression_p = saved_non_ice_p;
9651 parser->integral_constant_expression_p = saved_ice_p;
9652 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9656 /* Parse a template-argument.
9659 assignment-expression
9663 The representation is that of an assignment-expression, type-id, or
9664 id-expression -- except that the qualified id-expression is
9665 evaluated, so that the value returned is either a DECL or an
9668 Although the standard says "assignment-expression", it forbids
9669 throw-expressions or assignments in the template argument.
9670 Therefore, we use "conditional-expression" instead. */
9673 cp_parser_template_argument (cp_parser* parser)
9678 bool maybe_type_id = false;
9682 /* There's really no way to know what we're looking at, so we just
9683 try each alternative in order.
9687 In a template-argument, an ambiguity between a type-id and an
9688 expression is resolved to a type-id, regardless of the form of
9689 the corresponding template-parameter.
9691 Therefore, we try a type-id first. */
9692 cp_parser_parse_tentatively (parser);
9693 argument = cp_parser_type_id (parser);
9694 /* If there was no error parsing the type-id but the next token is a '>>',
9695 we probably found a typo for '> >'. But there are type-id which are
9696 also valid expressions. For instance:
9698 struct X { int operator >> (int); };
9699 template <int V> struct Foo {};
9702 Here 'X()' is a valid type-id of a function type, but the user just
9703 wanted to write the expression "X() >> 5". Thus, we remember that we
9704 found a valid type-id, but we still try to parse the argument as an
9705 expression to see what happens. */
9706 if (!cp_parser_error_occurred (parser)
9707 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9709 maybe_type_id = true;
9710 cp_parser_abort_tentative_parse (parser);
9714 /* If the next token isn't a `,' or a `>', then this argument wasn't
9715 really finished. This means that the argument is not a valid
9717 if (!cp_parser_next_token_ends_template_argument_p (parser))
9718 cp_parser_error (parser, "expected template-argument");
9719 /* If that worked, we're done. */
9720 if (cp_parser_parse_definitely (parser))
9723 /* We're still not sure what the argument will be. */
9724 cp_parser_parse_tentatively (parser);
9725 /* Try a template. */
9726 argument = cp_parser_id_expression (parser,
9727 /*template_keyword_p=*/false,
9728 /*check_dependency_p=*/true,
9730 /*declarator_p=*/false,
9731 /*optional_p=*/false);
9732 /* If the next token isn't a `,' or a `>', then this argument wasn't
9734 if (!cp_parser_next_token_ends_template_argument_p (parser))
9735 cp_parser_error (parser, "expected template-argument");
9736 if (!cp_parser_error_occurred (parser))
9738 /* Figure out what is being referred to. If the id-expression
9739 was for a class template specialization, then we will have a
9740 TYPE_DECL at this point. There is no need to do name lookup
9741 at this point in that case. */
9742 if (TREE_CODE (argument) != TYPE_DECL)
9743 argument = cp_parser_lookup_name (parser, argument,
9745 /*is_template=*/template_p,
9746 /*is_namespace=*/false,
9747 /*check_dependency=*/true,
9748 /*ambiguous_decls=*/NULL);
9749 if (TREE_CODE (argument) != TEMPLATE_DECL
9750 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9751 cp_parser_error (parser, "expected template-name");
9753 if (cp_parser_parse_definitely (parser))
9755 /* It must be a non-type argument. There permitted cases are given
9756 in [temp.arg.nontype]:
9758 -- an integral constant-expression of integral or enumeration
9761 -- the name of a non-type template-parameter; or
9763 -- the name of an object or function with external linkage...
9765 -- the address of an object or function with external linkage...
9767 -- a pointer to member... */
9768 /* Look for a non-type template parameter. */
9769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9771 cp_parser_parse_tentatively (parser);
9772 argument = cp_parser_primary_expression (parser,
9775 /*template_arg_p=*/true,
9777 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9778 || !cp_parser_next_token_ends_template_argument_p (parser))
9779 cp_parser_simulate_error (parser);
9780 if (cp_parser_parse_definitely (parser))
9784 /* If the next token is "&", the argument must be the address of an
9785 object or function with external linkage. */
9786 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9788 cp_lexer_consume_token (parser->lexer);
9789 /* See if we might have an id-expression. */
9790 token = cp_lexer_peek_token (parser->lexer);
9791 if (token->type == CPP_NAME
9792 || token->keyword == RID_OPERATOR
9793 || token->type == CPP_SCOPE
9794 || token->type == CPP_TEMPLATE_ID
9795 || token->type == CPP_NESTED_NAME_SPECIFIER)
9797 cp_parser_parse_tentatively (parser);
9798 argument = cp_parser_primary_expression (parser,
9801 /*template_arg_p=*/true,
9803 if (cp_parser_error_occurred (parser)
9804 || !cp_parser_next_token_ends_template_argument_p (parser))
9805 cp_parser_abort_tentative_parse (parser);
9808 if (TREE_CODE (argument) == INDIRECT_REF)
9810 gcc_assert (REFERENCE_REF_P (argument));
9811 argument = TREE_OPERAND (argument, 0);
9814 if (TREE_CODE (argument) == VAR_DECL)
9816 /* A variable without external linkage might still be a
9817 valid constant-expression, so no error is issued here
9818 if the external-linkage check fails. */
9819 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9820 cp_parser_simulate_error (parser);
9822 else if (is_overloaded_fn (argument))
9823 /* All overloaded functions are allowed; if the external
9824 linkage test does not pass, an error will be issued
9828 && (TREE_CODE (argument) == OFFSET_REF
9829 || TREE_CODE (argument) == SCOPE_REF))
9830 /* A pointer-to-member. */
9832 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9835 cp_parser_simulate_error (parser);
9837 if (cp_parser_parse_definitely (parser))
9840 argument = build_x_unary_op (ADDR_EXPR, argument);
9845 /* If the argument started with "&", there are no other valid
9846 alternatives at this point. */
9849 cp_parser_error (parser, "invalid non-type template argument");
9850 return error_mark_node;
9853 /* If the argument wasn't successfully parsed as a type-id followed
9854 by '>>', the argument can only be a constant expression now.
9855 Otherwise, we try parsing the constant-expression tentatively,
9856 because the argument could really be a type-id. */
9858 cp_parser_parse_tentatively (parser);
9859 argument = cp_parser_constant_expression (parser,
9860 /*allow_non_constant_p=*/false,
9861 /*non_constant_p=*/NULL);
9862 argument = fold_non_dependent_expr (argument);
9865 if (!cp_parser_next_token_ends_template_argument_p (parser))
9866 cp_parser_error (parser, "expected template-argument");
9867 if (cp_parser_parse_definitely (parser))
9869 /* We did our best to parse the argument as a non type-id, but that
9870 was the only alternative that matched (albeit with a '>' after
9871 it). We can assume it's just a typo from the user, and a
9872 diagnostic will then be issued. */
9873 return cp_parser_type_id (parser);
9876 /* Parse an explicit-instantiation.
9878 explicit-instantiation:
9879 template declaration
9881 Although the standard says `declaration', what it really means is:
9883 explicit-instantiation:
9884 template decl-specifier-seq [opt] declarator [opt] ;
9886 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9887 supposed to be allowed. A defect report has been filed about this
9892 explicit-instantiation:
9893 storage-class-specifier template
9894 decl-specifier-seq [opt] declarator [opt] ;
9895 function-specifier template
9896 decl-specifier-seq [opt] declarator [opt] ; */
9899 cp_parser_explicit_instantiation (cp_parser* parser)
9901 int declares_class_or_enum;
9902 cp_decl_specifier_seq decl_specifiers;
9903 tree extension_specifier = NULL_TREE;
9905 /* Look for an (optional) storage-class-specifier or
9906 function-specifier. */
9907 if (cp_parser_allow_gnu_extensions_p (parser))
9910 = cp_parser_storage_class_specifier_opt (parser);
9911 if (!extension_specifier)
9913 = cp_parser_function_specifier_opt (parser,
9914 /*decl_specs=*/NULL);
9917 /* Look for the `template' keyword. */
9918 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9919 /* Let the front end know that we are processing an explicit
9921 begin_explicit_instantiation ();
9922 /* [temp.explicit] says that we are supposed to ignore access
9923 control while processing explicit instantiation directives. */
9924 push_deferring_access_checks (dk_no_check);
9925 /* Parse a decl-specifier-seq. */
9926 cp_parser_decl_specifier_seq (parser,
9927 CP_PARSER_FLAGS_OPTIONAL,
9929 &declares_class_or_enum);
9930 /* If there was exactly one decl-specifier, and it declared a class,
9931 and there's no declarator, then we have an explicit type
9933 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9937 type = check_tag_decl (&decl_specifiers);
9938 /* Turn access control back on for names used during
9939 template instantiation. */
9940 pop_deferring_access_checks ();
9942 do_type_instantiation (type, extension_specifier,
9943 /*complain=*/tf_error);
9947 cp_declarator *declarator;
9950 /* Parse the declarator. */
9952 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9953 /*ctor_dtor_or_conv_p=*/NULL,
9954 /*parenthesized_p=*/NULL,
9955 /*member_p=*/false);
9956 if (declares_class_or_enum & 2)
9957 cp_parser_check_for_definition_in_return_type (declarator,
9958 decl_specifiers.type);
9959 if (declarator != cp_error_declarator)
9961 decl = grokdeclarator (declarator, &decl_specifiers,
9962 NORMAL, 0, &decl_specifiers.attributes);
9963 /* Turn access control back on for names used during
9964 template instantiation. */
9965 pop_deferring_access_checks ();
9966 /* Do the explicit instantiation. */
9967 do_decl_instantiation (decl, extension_specifier);
9971 pop_deferring_access_checks ();
9972 /* Skip the body of the explicit instantiation. */
9973 cp_parser_skip_to_end_of_statement (parser);
9976 /* We're done with the instantiation. */
9977 end_explicit_instantiation ();
9979 cp_parser_consume_semicolon_at_end_of_statement (parser);
9982 /* Parse an explicit-specialization.
9984 explicit-specialization:
9985 template < > declaration
9987 Although the standard says `declaration', what it really means is:
9989 explicit-specialization:
9990 template <> decl-specifier [opt] init-declarator [opt] ;
9991 template <> function-definition
9992 template <> explicit-specialization
9993 template <> template-declaration */
9996 cp_parser_explicit_specialization (cp_parser* parser)
9999 /* Look for the `template' keyword. */
10000 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10001 /* Look for the `<'. */
10002 cp_parser_require (parser, CPP_LESS, "`<'");
10003 /* Look for the `>'. */
10004 cp_parser_require (parser, CPP_GREATER, "`>'");
10005 /* We have processed another parameter list. */
10006 ++parser->num_template_parameter_lists;
10009 A template ... explicit specialization ... shall not have C
10011 if (current_lang_name == lang_name_c)
10013 error ("template specialization with C linkage");
10014 /* Give it C++ linkage to avoid confusing other parts of the
10016 push_lang_context (lang_name_cplusplus);
10017 need_lang_pop = true;
10020 need_lang_pop = false;
10021 /* Let the front end know that we are beginning a specialization. */
10022 if (!begin_specialization ())
10024 end_specialization ();
10025 cp_parser_skip_to_end_of_block_or_statement (parser);
10029 /* If the next keyword is `template', we need to figure out whether
10030 or not we're looking a template-declaration. */
10031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10033 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10034 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10035 cp_parser_template_declaration_after_export (parser,
10036 /*member_p=*/false);
10038 cp_parser_explicit_specialization (parser);
10041 /* Parse the dependent declaration. */
10042 cp_parser_single_declaration (parser,
10044 /*member_p=*/false,
10045 /*friend_p=*/NULL);
10046 /* We're done with the specialization. */
10047 end_specialization ();
10048 /* For the erroneous case of a template with C linkage, we pushed an
10049 implicit C++ linkage scope; exit that scope now. */
10051 pop_lang_context ();
10052 /* We're done with this parameter list. */
10053 --parser->num_template_parameter_lists;
10056 /* Parse a type-specifier.
10059 simple-type-specifier
10062 elaborated-type-specifier
10070 Returns a representation of the type-specifier. For a
10071 class-specifier, enum-specifier, or elaborated-type-specifier, a
10072 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10074 The parser flags FLAGS is used to control type-specifier parsing.
10076 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10077 in a decl-specifier-seq.
10079 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10080 class-specifier, enum-specifier, or elaborated-type-specifier, then
10081 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10082 if a type is declared; 2 if it is defined. Otherwise, it is set to
10085 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10086 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10087 is set to FALSE. */
10090 cp_parser_type_specifier (cp_parser* parser,
10091 cp_parser_flags flags,
10092 cp_decl_specifier_seq *decl_specs,
10093 bool is_declaration,
10094 int* declares_class_or_enum,
10095 bool* is_cv_qualifier)
10097 tree type_spec = NULL_TREE;
10100 cp_decl_spec ds = ds_last;
10102 /* Assume this type-specifier does not declare a new type. */
10103 if (declares_class_or_enum)
10104 *declares_class_or_enum = 0;
10105 /* And that it does not specify a cv-qualifier. */
10106 if (is_cv_qualifier)
10107 *is_cv_qualifier = false;
10108 /* Peek at the next token. */
10109 token = cp_lexer_peek_token (parser->lexer);
10111 /* If we're looking at a keyword, we can use that to guide the
10112 production we choose. */
10113 keyword = token->keyword;
10117 /* Look for the enum-specifier. */
10118 type_spec = cp_parser_enum_specifier (parser);
10119 /* If that worked, we're done. */
10122 if (declares_class_or_enum)
10123 *declares_class_or_enum = 2;
10125 cp_parser_set_decl_spec_type (decl_specs,
10127 /*user_defined_p=*/true);
10131 goto elaborated_type_specifier;
10133 /* Any of these indicate either a class-specifier, or an
10134 elaborated-type-specifier. */
10138 /* Parse tentatively so that we can back up if we don't find a
10139 class-specifier. */
10140 cp_parser_parse_tentatively (parser);
10141 /* Look for the class-specifier. */
10142 type_spec = cp_parser_class_specifier (parser);
10143 /* If that worked, we're done. */
10144 if (cp_parser_parse_definitely (parser))
10146 if (declares_class_or_enum)
10147 *declares_class_or_enum = 2;
10149 cp_parser_set_decl_spec_type (decl_specs,
10151 /*user_defined_p=*/true);
10155 /* Fall through. */
10156 elaborated_type_specifier:
10157 /* We're declaring (not defining) a class or enum. */
10158 if (declares_class_or_enum)
10159 *declares_class_or_enum = 1;
10161 /* Fall through. */
10163 /* Look for an elaborated-type-specifier. */
10165 = (cp_parser_elaborated_type_specifier
10167 decl_specs && decl_specs->specs[(int) ds_friend],
10170 cp_parser_set_decl_spec_type (decl_specs,
10172 /*user_defined_p=*/true);
10177 if (is_cv_qualifier)
10178 *is_cv_qualifier = true;
10183 if (is_cv_qualifier)
10184 *is_cv_qualifier = true;
10189 if (is_cv_qualifier)
10190 *is_cv_qualifier = true;
10194 /* The `__complex__' keyword is a GNU extension. */
10202 /* Handle simple keywords. */
10207 ++decl_specs->specs[(int)ds];
10208 decl_specs->any_specifiers_p = true;
10210 return cp_lexer_consume_token (parser->lexer)->u.value;
10213 /* If we do not already have a type-specifier, assume we are looking
10214 at a simple-type-specifier. */
10215 type_spec = cp_parser_simple_type_specifier (parser,
10219 /* If we didn't find a type-specifier, and a type-specifier was not
10220 optional in this context, issue an error message. */
10221 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10223 cp_parser_error (parser, "expected type specifier");
10224 return error_mark_node;
10230 /* Parse a simple-type-specifier.
10232 simple-type-specifier:
10233 :: [opt] nested-name-specifier [opt] type-name
10234 :: [opt] nested-name-specifier template template-id
10249 simple-type-specifier:
10250 __typeof__ unary-expression
10251 __typeof__ ( type-id )
10253 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10254 appropriately updated. */
10257 cp_parser_simple_type_specifier (cp_parser* parser,
10258 cp_decl_specifier_seq *decl_specs,
10259 cp_parser_flags flags)
10261 tree type = NULL_TREE;
10264 /* Peek at the next token. */
10265 token = cp_lexer_peek_token (parser->lexer);
10267 /* If we're looking at a keyword, things are easy. */
10268 switch (token->keyword)
10272 decl_specs->explicit_char_p = true;
10273 type = char_type_node;
10276 type = wchar_type_node;
10279 type = boolean_type_node;
10283 ++decl_specs->specs[(int) ds_short];
10284 type = short_integer_type_node;
10288 decl_specs->explicit_int_p = true;
10289 type = integer_type_node;
10293 ++decl_specs->specs[(int) ds_long];
10294 type = long_integer_type_node;
10298 ++decl_specs->specs[(int) ds_signed];
10299 type = integer_type_node;
10303 ++decl_specs->specs[(int) ds_unsigned];
10304 type = unsigned_type_node;
10307 type = float_type_node;
10310 type = double_type_node;
10313 type = void_type_node;
10317 /* Consume the `typeof' token. */
10318 cp_lexer_consume_token (parser->lexer);
10319 /* Parse the operand to `typeof'. */
10320 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10321 /* If it is not already a TYPE, take its type. */
10322 if (!TYPE_P (type))
10323 type = finish_typeof (type);
10326 cp_parser_set_decl_spec_type (decl_specs, type,
10327 /*user_defined_p=*/true);
10335 /* If the type-specifier was for a built-in type, we're done. */
10340 /* Record the type. */
10342 && (token->keyword != RID_SIGNED
10343 && token->keyword != RID_UNSIGNED
10344 && token->keyword != RID_SHORT
10345 && token->keyword != RID_LONG))
10346 cp_parser_set_decl_spec_type (decl_specs,
10348 /*user_defined=*/false);
10350 decl_specs->any_specifiers_p = true;
10352 /* Consume the token. */
10353 id = cp_lexer_consume_token (parser->lexer)->u.value;
10355 /* There is no valid C++ program where a non-template type is
10356 followed by a "<". That usually indicates that the user thought
10357 that the type was a template. */
10358 cp_parser_check_for_invalid_template_id (parser, type);
10360 return TYPE_NAME (type);
10363 /* The type-specifier must be a user-defined type. */
10364 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10369 /* Don't gobble tokens or issue error messages if this is an
10370 optional type-specifier. */
10371 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10372 cp_parser_parse_tentatively (parser);
10374 /* Look for the optional `::' operator. */
10376 = (cp_parser_global_scope_opt (parser,
10377 /*current_scope_valid_p=*/false)
10379 /* Look for the nested-name specifier. */
10381 = (cp_parser_nested_name_specifier_opt (parser,
10382 /*typename_keyword_p=*/false,
10383 /*check_dependency_p=*/true,
10385 /*is_declaration=*/false)
10387 /* If we have seen a nested-name-specifier, and the next token
10388 is `template', then we are using the template-id production. */
10390 && cp_parser_optional_template_keyword (parser))
10392 /* Look for the template-id. */
10393 type = cp_parser_template_id (parser,
10394 /*template_keyword_p=*/true,
10395 /*check_dependency_p=*/true,
10396 /*is_declaration=*/false);
10397 /* If the template-id did not name a type, we are out of
10399 if (TREE_CODE (type) != TYPE_DECL)
10401 cp_parser_error (parser, "expected template-id for type");
10405 /* Otherwise, look for a type-name. */
10407 type = cp_parser_type_name (parser);
10408 /* Keep track of all name-lookups performed in class scopes. */
10412 && TREE_CODE (type) == TYPE_DECL
10413 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10414 maybe_note_name_used_in_class (DECL_NAME (type), type);
10415 /* If it didn't work out, we don't have a TYPE. */
10416 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10417 && !cp_parser_parse_definitely (parser))
10419 if (type && decl_specs)
10420 cp_parser_set_decl_spec_type (decl_specs, type,
10421 /*user_defined=*/true);
10424 /* If we didn't get a type-name, issue an error message. */
10425 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10427 cp_parser_error (parser, "expected type-name");
10428 return error_mark_node;
10431 /* There is no valid C++ program where a non-template type is
10432 followed by a "<". That usually indicates that the user thought
10433 that the type was a template. */
10434 if (type && type != error_mark_node)
10436 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10437 If it is, then the '<'...'>' enclose protocol names rather than
10438 template arguments, and so everything is fine. */
10439 if (c_dialect_objc ()
10440 && (objc_is_id (type) || objc_is_class_name (type)))
10442 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10443 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10445 /* Clobber the "unqualified" type previously entered into
10446 DECL_SPECS with the new, improved protocol-qualified version. */
10448 decl_specs->type = qual_type;
10453 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10459 /* Parse a type-name.
10472 Returns a TYPE_DECL for the type. */
10475 cp_parser_type_name (cp_parser* parser)
10480 /* We can't know yet whether it is a class-name or not. */
10481 cp_parser_parse_tentatively (parser);
10482 /* Try a class-name. */
10483 type_decl = cp_parser_class_name (parser,
10484 /*typename_keyword_p=*/false,
10485 /*template_keyword_p=*/false,
10487 /*check_dependency_p=*/true,
10488 /*class_head_p=*/false,
10489 /*is_declaration=*/false);
10490 /* If it's not a class-name, keep looking. */
10491 if (!cp_parser_parse_definitely (parser))
10493 /* It must be a typedef-name or an enum-name. */
10494 identifier = cp_parser_identifier (parser);
10495 if (identifier == error_mark_node)
10496 return error_mark_node;
10498 /* Look up the type-name. */
10499 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10501 if (TREE_CODE (type_decl) != TYPE_DECL
10502 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10504 /* See if this is an Objective-C type. */
10505 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10506 tree type = objc_get_protocol_qualified_type (identifier, protos);
10508 type_decl = TYPE_NAME (type);
10511 /* Issue an error if we did not find a type-name. */
10512 if (TREE_CODE (type_decl) != TYPE_DECL)
10514 if (!cp_parser_simulate_error (parser))
10515 cp_parser_name_lookup_error (parser, identifier, type_decl,
10517 type_decl = error_mark_node;
10519 /* Remember that the name was used in the definition of the
10520 current class so that we can check later to see if the
10521 meaning would have been different after the class was
10522 entirely defined. */
10523 else if (type_decl != error_mark_node
10525 maybe_note_name_used_in_class (identifier, type_decl);
10532 /* Parse an elaborated-type-specifier. Note that the grammar given
10533 here incorporates the resolution to DR68.
10535 elaborated-type-specifier:
10536 class-key :: [opt] nested-name-specifier [opt] identifier
10537 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10538 enum :: [opt] nested-name-specifier [opt] identifier
10539 typename :: [opt] nested-name-specifier identifier
10540 typename :: [opt] nested-name-specifier template [opt]
10545 elaborated-type-specifier:
10546 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10547 class-key attributes :: [opt] nested-name-specifier [opt]
10548 template [opt] template-id
10549 enum attributes :: [opt] nested-name-specifier [opt] identifier
10551 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10552 declared `friend'. If IS_DECLARATION is TRUE, then this
10553 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10554 something is being declared.
10556 Returns the TYPE specified. */
10559 cp_parser_elaborated_type_specifier (cp_parser* parser,
10561 bool is_declaration)
10563 enum tag_types tag_type;
10565 tree type = NULL_TREE;
10566 tree attributes = NULL_TREE;
10568 /* See if we're looking at the `enum' keyword. */
10569 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10571 /* Consume the `enum' token. */
10572 cp_lexer_consume_token (parser->lexer);
10573 /* Remember that it's an enumeration type. */
10574 tag_type = enum_type;
10575 /* Parse the attributes. */
10576 attributes = cp_parser_attributes_opt (parser);
10578 /* Or, it might be `typename'. */
10579 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10582 /* Consume the `typename' token. */
10583 cp_lexer_consume_token (parser->lexer);
10584 /* Remember that it's a `typename' type. */
10585 tag_type = typename_type;
10586 /* The `typename' keyword is only allowed in templates. */
10587 if (!processing_template_decl)
10588 pedwarn ("using %<typename%> outside of template");
10590 /* Otherwise it must be a class-key. */
10593 tag_type = cp_parser_class_key (parser);
10594 if (tag_type == none_type)
10595 return error_mark_node;
10596 /* Parse the attributes. */
10597 attributes = cp_parser_attributes_opt (parser);
10600 /* Look for the `::' operator. */
10601 cp_parser_global_scope_opt (parser,
10602 /*current_scope_valid_p=*/false);
10603 /* Look for the nested-name-specifier. */
10604 if (tag_type == typename_type)
10606 if (!cp_parser_nested_name_specifier (parser,
10607 /*typename_keyword_p=*/true,
10608 /*check_dependency_p=*/true,
10611 return error_mark_node;
10614 /* Even though `typename' is not present, the proposed resolution
10615 to Core Issue 180 says that in `class A<T>::B', `B' should be
10616 considered a type-name, even if `A<T>' is dependent. */
10617 cp_parser_nested_name_specifier_opt (parser,
10618 /*typename_keyword_p=*/true,
10619 /*check_dependency_p=*/true,
10622 /* For everything but enumeration types, consider a template-id.
10623 For an enumeration type, consider only a plain identifier. */
10624 if (tag_type != enum_type)
10626 bool template_p = false;
10629 /* Allow the `template' keyword. */
10630 template_p = cp_parser_optional_template_keyword (parser);
10631 /* If we didn't see `template', we don't know if there's a
10632 template-id or not. */
10634 cp_parser_parse_tentatively (parser);
10635 /* Parse the template-id. */
10636 decl = cp_parser_template_id (parser, template_p,
10637 /*check_dependency_p=*/true,
10639 /* If we didn't find a template-id, look for an ordinary
10641 if (!template_p && !cp_parser_parse_definitely (parser))
10643 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10644 in effect, then we must assume that, upon instantiation, the
10645 template will correspond to a class. */
10646 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10647 && tag_type == typename_type)
10648 type = make_typename_type (parser->scope, decl,
10650 /*complain=*/tf_error);
10652 type = TREE_TYPE (decl);
10657 identifier = cp_parser_identifier (parser);
10659 if (identifier == error_mark_node)
10661 parser->scope = NULL_TREE;
10662 return error_mark_node;
10665 /* For a `typename', we needn't call xref_tag. */
10666 if (tag_type == typename_type
10667 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10668 return cp_parser_make_typename_type (parser, parser->scope,
10670 /* Look up a qualified name in the usual way. */
10674 tree ambiguous_decls;
10676 decl = cp_parser_lookup_name (parser, identifier,
10678 /*is_template=*/false,
10679 /*is_namespace=*/false,
10680 /*check_dependency=*/true,
10683 /* If the lookup was ambiguous, an error will already have been
10685 if (ambiguous_decls)
10686 return error_mark_node;
10688 /* If we are parsing friend declaration, DECL may be a
10689 TEMPLATE_DECL tree node here. However, we need to check
10690 whether this TEMPLATE_DECL results in valid code. Consider
10691 the following example:
10694 template <class T> class C {};
10697 template <class T> friend class N::C; // #1, valid code
10699 template <class T> class Y {
10700 friend class N::C; // #2, invalid code
10703 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10704 name lookup of `N::C'. We see that friend declaration must
10705 be template for the code to be valid. Note that
10706 processing_template_decl does not work here since it is
10707 always 1 for the above two cases. */
10709 decl = (cp_parser_maybe_treat_template_as_class
10710 (decl, /*tag_name_p=*/is_friend
10711 && parser->num_template_parameter_lists));
10713 if (TREE_CODE (decl) != TYPE_DECL)
10715 cp_parser_diagnose_invalid_type_name (parser,
10718 return error_mark_node;
10721 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10723 bool allow_template = (parser->num_template_parameter_lists
10724 || DECL_SELF_REFERENCE_P (decl));
10725 type = check_elaborated_type_specifier (tag_type, decl,
10728 if (type == error_mark_node)
10729 return error_mark_node;
10732 type = TREE_TYPE (decl);
10736 /* An elaborated-type-specifier sometimes introduces a new type and
10737 sometimes names an existing type. Normally, the rule is that it
10738 introduces a new type only if there is not an existing type of
10739 the same name already in scope. For example, given:
10742 void f() { struct S s; }
10744 the `struct S' in the body of `f' is the same `struct S' as in
10745 the global scope; the existing definition is used. However, if
10746 there were no global declaration, this would introduce a new
10747 local class named `S'.
10749 An exception to this rule applies to the following code:
10751 namespace N { struct S; }
10753 Here, the elaborated-type-specifier names a new type
10754 unconditionally; even if there is already an `S' in the
10755 containing scope this declaration names a new type.
10756 This exception only applies if the elaborated-type-specifier
10757 forms the complete declaration:
10761 A declaration consisting solely of `class-key identifier ;' is
10762 either a redeclaration of the name in the current scope or a
10763 forward declaration of the identifier as a class name. It
10764 introduces the name into the current scope.
10766 We are in this situation precisely when the next token is a `;'.
10768 An exception to the exception is that a `friend' declaration does
10769 *not* name a new type; i.e., given:
10771 struct S { friend struct T; };
10773 `T' is not a new type in the scope of `S'.
10775 Also, `new struct S' or `sizeof (struct S)' never results in the
10776 definition of a new type; a new type can only be declared in a
10777 declaration context. */
10783 /* Friends have special name lookup rules. */
10784 ts = ts_within_enclosing_non_class;
10785 else if (is_declaration
10786 && cp_lexer_next_token_is (parser->lexer,
10788 /* This is a `class-key identifier ;' */
10794 (parser->num_template_parameter_lists
10795 && (cp_parser_next_token_starts_class_definition_p (parser)
10796 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10797 /* An unqualified name was used to reference this type, so
10798 there were no qualifying templates. */
10799 if (!cp_parser_check_template_parameters (parser,
10800 /*num_templates=*/0))
10801 return error_mark_node;
10802 type = xref_tag (tag_type, identifier, ts, template_p);
10806 if (type == error_mark_node)
10807 return error_mark_node;
10809 /* Allow attributes on forward declarations of classes. */
10812 if (TREE_CODE (type) == TYPENAME_TYPE)
10813 warning (OPT_Wattributes,
10814 "attributes ignored on uninstantiated type");
10815 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10816 && ! processing_explicit_instantiation)
10817 warning (OPT_Wattributes,
10818 "attributes ignored on template instantiation");
10819 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10820 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10822 warning (OPT_Wattributes,
10823 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10826 if (tag_type != enum_type)
10827 cp_parser_check_class_key (tag_type, type);
10829 /* A "<" cannot follow an elaborated type specifier. If that
10830 happens, the user was probably trying to form a template-id. */
10831 cp_parser_check_for_invalid_template_id (parser, type);
10836 /* Parse an enum-specifier.
10839 enum identifier [opt] { enumerator-list [opt] }
10842 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10845 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10846 if the token stream isn't an enum-specifier after all. */
10849 cp_parser_enum_specifier (cp_parser* parser)
10855 /* Parse tentatively so that we can back up if we don't find a
10857 cp_parser_parse_tentatively (parser);
10859 /* Caller guarantees that the current token is 'enum', an identifier
10860 possibly follows, and the token after that is an opening brace.
10861 If we don't have an identifier, fabricate an anonymous name for
10862 the enumeration being defined. */
10863 cp_lexer_consume_token (parser->lexer);
10865 attributes = cp_parser_attributes_opt (parser);
10867 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10868 identifier = cp_parser_identifier (parser);
10870 identifier = make_anon_name ();
10872 /* Look for the `{' but don't consume it yet. */
10873 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10874 cp_parser_simulate_error (parser);
10876 if (!cp_parser_parse_definitely (parser))
10879 /* Issue an error message if type-definitions are forbidden here. */
10880 if (!cp_parser_check_type_definition (parser))
10881 type = error_mark_node;
10883 /* Create the new type. We do this before consuming the opening
10884 brace so the enum will be recorded as being on the line of its
10885 tag (or the 'enum' keyword, if there is no tag). */
10886 type = start_enum (identifier);
10888 /* Consume the opening brace. */
10889 cp_lexer_consume_token (parser->lexer);
10891 if (type == error_mark_node)
10893 cp_parser_skip_to_end_of_block_or_statement (parser);
10894 return error_mark_node;
10897 /* If the next token is not '}', then there are some enumerators. */
10898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10899 cp_parser_enumerator_list (parser, type);
10901 /* Consume the final '}'. */
10902 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10904 /* Look for trailing attributes to apply to this enumeration, and
10905 apply them if appropriate. */
10906 if (cp_parser_allow_gnu_extensions_p (parser))
10908 tree trailing_attr = cp_parser_attributes_opt (parser);
10909 cplus_decl_attributes (&type,
10911 (int) ATTR_FLAG_TYPE_IN_PLACE);
10914 /* Finish up the enumeration. */
10915 finish_enum (type);
10920 /* Parse an enumerator-list. The enumerators all have the indicated
10924 enumerator-definition
10925 enumerator-list , enumerator-definition */
10928 cp_parser_enumerator_list (cp_parser* parser, tree type)
10932 /* Parse an enumerator-definition. */
10933 cp_parser_enumerator_definition (parser, type);
10935 /* If the next token is not a ',', we've reached the end of
10937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10939 /* Otherwise, consume the `,' and keep going. */
10940 cp_lexer_consume_token (parser->lexer);
10941 /* If the next token is a `}', there is a trailing comma. */
10942 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10944 if (pedantic && !in_system_header)
10945 pedwarn ("comma at end of enumerator list");
10951 /* Parse an enumerator-definition. The enumerator has the indicated
10954 enumerator-definition:
10956 enumerator = constant-expression
10962 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10967 /* Look for the identifier. */
10968 identifier = cp_parser_identifier (parser);
10969 if (identifier == error_mark_node)
10972 /* If the next token is an '=', then there is an explicit value. */
10973 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10975 /* Consume the `=' token. */
10976 cp_lexer_consume_token (parser->lexer);
10977 /* Parse the value. */
10978 value = cp_parser_constant_expression (parser,
10979 /*allow_non_constant_p=*/false,
10985 /* Create the enumerator. */
10986 build_enumerator (identifier, value, type);
10989 /* Parse a namespace-name.
10992 original-namespace-name
10995 Returns the NAMESPACE_DECL for the namespace. */
10998 cp_parser_namespace_name (cp_parser* parser)
11001 tree namespace_decl;
11003 /* Get the name of the namespace. */
11004 identifier = cp_parser_identifier (parser);
11005 if (identifier == error_mark_node)
11006 return error_mark_node;
11008 /* Look up the identifier in the currently active scope. Look only
11009 for namespaces, due to:
11011 [basic.lookup.udir]
11013 When looking up a namespace-name in a using-directive or alias
11014 definition, only namespace names are considered.
11018 [basic.lookup.qual]
11020 During the lookup of a name preceding the :: scope resolution
11021 operator, object, function, and enumerator names are ignored.
11023 (Note that cp_parser_class_or_namespace_name only calls this
11024 function if the token after the name is the scope resolution
11026 namespace_decl = cp_parser_lookup_name (parser, identifier,
11028 /*is_template=*/false,
11029 /*is_namespace=*/true,
11030 /*check_dependency=*/true,
11031 /*ambiguous_decls=*/NULL);
11032 /* If it's not a namespace, issue an error. */
11033 if (namespace_decl == error_mark_node
11034 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11036 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11037 error ("%qD is not a namespace-name", identifier);
11038 cp_parser_error (parser, "expected namespace-name");
11039 namespace_decl = error_mark_node;
11042 return namespace_decl;
11045 /* Parse a namespace-definition.
11047 namespace-definition:
11048 named-namespace-definition
11049 unnamed-namespace-definition
11051 named-namespace-definition:
11052 original-namespace-definition
11053 extension-namespace-definition
11055 original-namespace-definition:
11056 namespace identifier { namespace-body }
11058 extension-namespace-definition:
11059 namespace original-namespace-name { namespace-body }
11061 unnamed-namespace-definition:
11062 namespace { namespace-body } */
11065 cp_parser_namespace_definition (cp_parser* parser)
11067 tree identifier, attribs;
11069 /* Look for the `namespace' keyword. */
11070 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11072 /* Get the name of the namespace. We do not attempt to distinguish
11073 between an original-namespace-definition and an
11074 extension-namespace-definition at this point. The semantic
11075 analysis routines are responsible for that. */
11076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11077 identifier = cp_parser_identifier (parser);
11079 identifier = NULL_TREE;
11081 /* Parse any specified attributes. */
11082 attribs = cp_parser_attributes_opt (parser);
11084 /* Look for the `{' to start the namespace. */
11085 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11086 /* Start the namespace. */
11087 push_namespace_with_attribs (identifier, attribs);
11088 /* Parse the body of the namespace. */
11089 cp_parser_namespace_body (parser);
11090 /* Finish the namespace. */
11092 /* Look for the final `}'. */
11093 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11096 /* Parse a namespace-body.
11099 declaration-seq [opt] */
11102 cp_parser_namespace_body (cp_parser* parser)
11104 cp_parser_declaration_seq_opt (parser);
11107 /* Parse a namespace-alias-definition.
11109 namespace-alias-definition:
11110 namespace identifier = qualified-namespace-specifier ; */
11113 cp_parser_namespace_alias_definition (cp_parser* parser)
11116 tree namespace_specifier;
11118 /* Look for the `namespace' keyword. */
11119 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11120 /* Look for the identifier. */
11121 identifier = cp_parser_identifier (parser);
11122 if (identifier == error_mark_node)
11124 /* Look for the `=' token. */
11125 cp_parser_require (parser, CPP_EQ, "`='");
11126 /* Look for the qualified-namespace-specifier. */
11127 namespace_specifier
11128 = cp_parser_qualified_namespace_specifier (parser);
11129 /* Look for the `;' token. */
11130 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11132 /* Register the alias in the symbol table. */
11133 do_namespace_alias (identifier, namespace_specifier);
11136 /* Parse a qualified-namespace-specifier.
11138 qualified-namespace-specifier:
11139 :: [opt] nested-name-specifier [opt] namespace-name
11141 Returns a NAMESPACE_DECL corresponding to the specified
11145 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11147 /* Look for the optional `::'. */
11148 cp_parser_global_scope_opt (parser,
11149 /*current_scope_valid_p=*/false);
11151 /* Look for the optional nested-name-specifier. */
11152 cp_parser_nested_name_specifier_opt (parser,
11153 /*typename_keyword_p=*/false,
11154 /*check_dependency_p=*/true,
11156 /*is_declaration=*/true);
11158 return cp_parser_namespace_name (parser);
11161 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11162 access declaration.
11165 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11166 using :: unqualified-id ;
11168 access-declaration:
11174 cp_parser_using_declaration (cp_parser* parser,
11175 bool access_declaration_p)
11178 bool typename_p = false;
11179 bool global_scope_p;
11184 if (access_declaration_p)
11185 cp_parser_parse_tentatively (parser);
11188 /* Look for the `using' keyword. */
11189 cp_parser_require_keyword (parser, RID_USING, "`using'");
11191 /* Peek at the next token. */
11192 token = cp_lexer_peek_token (parser->lexer);
11193 /* See if it's `typename'. */
11194 if (token->keyword == RID_TYPENAME)
11196 /* Remember that we've seen it. */
11198 /* Consume the `typename' token. */
11199 cp_lexer_consume_token (parser->lexer);
11203 /* Look for the optional global scope qualification. */
11205 = (cp_parser_global_scope_opt (parser,
11206 /*current_scope_valid_p=*/false)
11209 /* If we saw `typename', or didn't see `::', then there must be a
11210 nested-name-specifier present. */
11211 if (typename_p || !global_scope_p)
11212 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11213 /*check_dependency_p=*/true,
11215 /*is_declaration=*/true);
11216 /* Otherwise, we could be in either of the two productions. In that
11217 case, treat the nested-name-specifier as optional. */
11219 qscope = cp_parser_nested_name_specifier_opt (parser,
11220 /*typename_keyword_p=*/false,
11221 /*check_dependency_p=*/true,
11223 /*is_declaration=*/true);
11225 qscope = global_namespace;
11227 if (access_declaration_p && cp_parser_error_occurred (parser))
11228 /* Something has already gone wrong; there's no need to parse
11229 further. Since an error has occurred, the return value of
11230 cp_parser_parse_definitely will be false, as required. */
11231 return cp_parser_parse_definitely (parser);
11233 /* Parse the unqualified-id. */
11234 identifier = cp_parser_unqualified_id (parser,
11235 /*template_keyword_p=*/false,
11236 /*check_dependency_p=*/true,
11237 /*declarator_p=*/true,
11238 /*optional_p=*/false);
11240 if (access_declaration_p)
11242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11243 cp_parser_simulate_error (parser);
11244 if (!cp_parser_parse_definitely (parser))
11248 /* The function we call to handle a using-declaration is different
11249 depending on what scope we are in. */
11250 if (qscope == error_mark_node || identifier == error_mark_node)
11252 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11253 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11254 /* [namespace.udecl]
11256 A using declaration shall not name a template-id. */
11257 error ("a template-id may not appear in a using-declaration");
11260 if (at_class_scope_p ())
11262 /* Create the USING_DECL. */
11263 decl = do_class_using_decl (parser->scope, identifier);
11264 /* Add it to the list of members in this class. */
11265 finish_member_declaration (decl);
11269 decl = cp_parser_lookup_name_simple (parser, identifier);
11270 if (decl == error_mark_node)
11271 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11272 else if (!at_namespace_scope_p ())
11273 do_local_using_decl (decl, qscope, identifier);
11275 do_toplevel_using_decl (decl, qscope, identifier);
11279 /* Look for the final `;'. */
11280 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11285 /* Parse a using-directive.
11288 using namespace :: [opt] nested-name-specifier [opt]
11289 namespace-name ; */
11292 cp_parser_using_directive (cp_parser* parser)
11294 tree namespace_decl;
11297 /* Look for the `using' keyword. */
11298 cp_parser_require_keyword (parser, RID_USING, "`using'");
11299 /* And the `namespace' keyword. */
11300 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11301 /* Look for the optional `::' operator. */
11302 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11303 /* And the optional nested-name-specifier. */
11304 cp_parser_nested_name_specifier_opt (parser,
11305 /*typename_keyword_p=*/false,
11306 /*check_dependency_p=*/true,
11308 /*is_declaration=*/true);
11309 /* Get the namespace being used. */
11310 namespace_decl = cp_parser_namespace_name (parser);
11311 /* And any specified attributes. */
11312 attribs = cp_parser_attributes_opt (parser);
11313 /* Update the symbol table. */
11314 parse_using_directive (namespace_decl, attribs);
11315 /* Look for the final `;'. */
11316 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11319 /* Parse an asm-definition.
11322 asm ( string-literal ) ;
11327 asm volatile [opt] ( string-literal ) ;
11328 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11329 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11330 : asm-operand-list [opt] ) ;
11331 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11332 : asm-operand-list [opt]
11333 : asm-operand-list [opt] ) ; */
11336 cp_parser_asm_definition (cp_parser* parser)
11339 tree outputs = NULL_TREE;
11340 tree inputs = NULL_TREE;
11341 tree clobbers = NULL_TREE;
11343 bool volatile_p = false;
11344 bool extended_p = false;
11346 /* Look for the `asm' keyword. */
11347 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11348 /* See if the next token is `volatile'. */
11349 if (cp_parser_allow_gnu_extensions_p (parser)
11350 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11352 /* Remember that we saw the `volatile' keyword. */
11354 /* Consume the token. */
11355 cp_lexer_consume_token (parser->lexer);
11357 /* Look for the opening `('. */
11358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11360 /* Look for the string. */
11361 string = cp_parser_string_literal (parser, false, false);
11362 if (string == error_mark_node)
11364 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11365 /*consume_paren=*/true);
11369 /* If we're allowing GNU extensions, check for the extended assembly
11370 syntax. Unfortunately, the `:' tokens need not be separated by
11371 a space in C, and so, for compatibility, we tolerate that here
11372 too. Doing that means that we have to treat the `::' operator as
11374 if (cp_parser_allow_gnu_extensions_p (parser)
11375 && parser->in_function_body
11376 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11377 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11379 bool inputs_p = false;
11380 bool clobbers_p = false;
11382 /* The extended syntax was used. */
11385 /* Look for outputs. */
11386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11388 /* Consume the `:'. */
11389 cp_lexer_consume_token (parser->lexer);
11390 /* Parse the output-operands. */
11391 if (cp_lexer_next_token_is_not (parser->lexer,
11393 && cp_lexer_next_token_is_not (parser->lexer,
11395 && cp_lexer_next_token_is_not (parser->lexer,
11397 outputs = cp_parser_asm_operand_list (parser);
11399 /* If the next token is `::', there are no outputs, and the
11400 next token is the beginning of the inputs. */
11401 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11402 /* The inputs are coming next. */
11405 /* Look for inputs. */
11407 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11409 /* Consume the `:' or `::'. */
11410 cp_lexer_consume_token (parser->lexer);
11411 /* Parse the output-operands. */
11412 if (cp_lexer_next_token_is_not (parser->lexer,
11414 && cp_lexer_next_token_is_not (parser->lexer,
11416 inputs = cp_parser_asm_operand_list (parser);
11418 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11419 /* The clobbers are coming next. */
11422 /* Look for clobbers. */
11424 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11426 /* Consume the `:' or `::'. */
11427 cp_lexer_consume_token (parser->lexer);
11428 /* Parse the clobbers. */
11429 if (cp_lexer_next_token_is_not (parser->lexer,
11431 clobbers = cp_parser_asm_clobber_list (parser);
11434 /* Look for the closing `)'. */
11435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11436 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11437 /*consume_paren=*/true);
11438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11440 /* Create the ASM_EXPR. */
11441 if (parser->in_function_body)
11443 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11445 /* If the extended syntax was not used, mark the ASM_EXPR. */
11448 tree temp = asm_stmt;
11449 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11450 temp = TREE_OPERAND (temp, 0);
11452 ASM_INPUT_P (temp) = 1;
11456 cgraph_add_asm_node (string);
11459 /* Declarators [gram.dcl.decl] */
11461 /* Parse an init-declarator.
11464 declarator initializer [opt]
11469 declarator asm-specification [opt] attributes [opt] initializer [opt]
11471 function-definition:
11472 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11474 decl-specifier-seq [opt] declarator function-try-block
11478 function-definition:
11479 __extension__ function-definition
11481 The DECL_SPECIFIERS apply to this declarator. Returns a
11482 representation of the entity declared. If MEMBER_P is TRUE, then
11483 this declarator appears in a class scope. The new DECL created by
11484 this declarator is returned.
11486 The CHECKS are access checks that should be performed once we know
11487 what entity is being declared (and, therefore, what classes have
11490 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11491 for a function-definition here as well. If the declarator is a
11492 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11493 be TRUE upon return. By that point, the function-definition will
11494 have been completely parsed.
11496 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11500 cp_parser_init_declarator (cp_parser* parser,
11501 cp_decl_specifier_seq *decl_specifiers,
11502 VEC (deferred_access_check,gc)* checks,
11503 bool function_definition_allowed_p,
11505 int declares_class_or_enum,
11506 bool* function_definition_p)
11509 cp_declarator *declarator;
11510 tree prefix_attributes;
11512 tree asm_specification;
11514 tree decl = NULL_TREE;
11516 bool is_initialized;
11517 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11518 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11520 enum cpp_ttype initialization_kind;
11521 bool is_parenthesized_init = false;
11522 bool is_non_constant_init;
11523 int ctor_dtor_or_conv_p;
11525 tree pushed_scope = NULL;
11527 /* Gather the attributes that were provided with the
11528 decl-specifiers. */
11529 prefix_attributes = decl_specifiers->attributes;
11531 /* Assume that this is not the declarator for a function
11533 if (function_definition_p)
11534 *function_definition_p = false;
11536 /* Defer access checks while parsing the declarator; we cannot know
11537 what names are accessible until we know what is being
11539 resume_deferring_access_checks ();
11541 /* Parse the declarator. */
11543 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11544 &ctor_dtor_or_conv_p,
11545 /*parenthesized_p=*/NULL,
11546 /*member_p=*/false);
11547 /* Gather up the deferred checks. */
11548 stop_deferring_access_checks ();
11550 /* If the DECLARATOR was erroneous, there's no need to go
11552 if (declarator == cp_error_declarator)
11553 return error_mark_node;
11555 /* Check that the number of template-parameter-lists is OK. */
11556 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11557 return error_mark_node;
11559 if (declares_class_or_enum & 2)
11560 cp_parser_check_for_definition_in_return_type (declarator,
11561 decl_specifiers->type);
11563 /* Figure out what scope the entity declared by the DECLARATOR is
11564 located in. `grokdeclarator' sometimes changes the scope, so
11565 we compute it now. */
11566 scope = get_scope_of_declarator (declarator);
11568 /* If we're allowing GNU extensions, look for an asm-specification
11570 if (cp_parser_allow_gnu_extensions_p (parser))
11572 /* Look for an asm-specification. */
11573 asm_specification = cp_parser_asm_specification_opt (parser);
11574 /* And attributes. */
11575 attributes = cp_parser_attributes_opt (parser);
11579 asm_specification = NULL_TREE;
11580 attributes = NULL_TREE;
11583 /* Peek at the next token. */
11584 token = cp_lexer_peek_token (parser->lexer);
11585 /* Check to see if the token indicates the start of a
11586 function-definition. */
11587 if (cp_parser_token_starts_function_definition_p (token))
11589 if (!function_definition_allowed_p)
11591 /* If a function-definition should not appear here, issue an
11593 cp_parser_error (parser,
11594 "a function-definition is not allowed here");
11595 return error_mark_node;
11599 /* Neither attributes nor an asm-specification are allowed
11600 on a function-definition. */
11601 if (asm_specification)
11602 error ("an asm-specification is not allowed on a function-definition");
11604 error ("attributes are not allowed on a function-definition");
11605 /* This is a function-definition. */
11606 *function_definition_p = true;
11608 /* Parse the function definition. */
11610 decl = cp_parser_save_member_function_body (parser,
11613 prefix_attributes);
11616 = (cp_parser_function_definition_from_specifiers_and_declarator
11617 (parser, decl_specifiers, prefix_attributes, declarator));
11625 Only in function declarations for constructors, destructors, and
11626 type conversions can the decl-specifier-seq be omitted.
11628 We explicitly postpone this check past the point where we handle
11629 function-definitions because we tolerate function-definitions
11630 that are missing their return types in some modes. */
11631 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11633 cp_parser_error (parser,
11634 "expected constructor, destructor, or type conversion");
11635 return error_mark_node;
11638 /* An `=' or an `(' indicates an initializer. */
11639 if (token->type == CPP_EQ
11640 || token->type == CPP_OPEN_PAREN)
11642 is_initialized = true;
11643 initialization_kind = token->type;
11647 /* If the init-declarator isn't initialized and isn't followed by a
11648 `,' or `;', it's not a valid init-declarator. */
11649 if (token->type != CPP_COMMA
11650 && token->type != CPP_SEMICOLON)
11652 cp_parser_error (parser, "expected initializer");
11653 return error_mark_node;
11655 is_initialized = false;
11656 initialization_kind = CPP_EOF;
11659 /* Because start_decl has side-effects, we should only call it if we
11660 know we're going ahead. By this point, we know that we cannot
11661 possibly be looking at any other construct. */
11662 cp_parser_commit_to_tentative_parse (parser);
11664 /* If the decl specifiers were bad, issue an error now that we're
11665 sure this was intended to be a declarator. Then continue
11666 declaring the variable(s), as int, to try to cut down on further
11668 if (decl_specifiers->any_specifiers_p
11669 && decl_specifiers->type == error_mark_node)
11671 cp_parser_error (parser, "invalid type in declaration");
11672 decl_specifiers->type = integer_type_node;
11675 /* Check to see whether or not this declaration is a friend. */
11676 friend_p = cp_parser_friend_p (decl_specifiers);
11678 /* Enter the newly declared entry in the symbol table. If we're
11679 processing a declaration in a class-specifier, we wait until
11680 after processing the initializer. */
11683 if (parser->in_unbraced_linkage_specification_p)
11684 decl_specifiers->storage_class = sc_extern;
11685 decl = start_decl (declarator, decl_specifiers,
11686 is_initialized, attributes, prefix_attributes,
11690 /* Enter the SCOPE. That way unqualified names appearing in the
11691 initializer will be looked up in SCOPE. */
11692 pushed_scope = push_scope (scope);
11694 /* Perform deferred access control checks, now that we know in which
11695 SCOPE the declared entity resides. */
11696 if (!member_p && decl)
11698 tree saved_current_function_decl = NULL_TREE;
11700 /* If the entity being declared is a function, pretend that we
11701 are in its scope. If it is a `friend', it may have access to
11702 things that would not otherwise be accessible. */
11703 if (TREE_CODE (decl) == FUNCTION_DECL)
11705 saved_current_function_decl = current_function_decl;
11706 current_function_decl = decl;
11709 /* Perform access checks for template parameters. */
11710 cp_parser_perform_template_parameter_access_checks (checks);
11712 /* Perform the access control checks for the declarator and the
11713 the decl-specifiers. */
11714 perform_deferred_access_checks ();
11716 /* Restore the saved value. */
11717 if (TREE_CODE (decl) == FUNCTION_DECL)
11718 current_function_decl = saved_current_function_decl;
11721 /* Parse the initializer. */
11722 initializer = NULL_TREE;
11723 is_parenthesized_init = false;
11724 is_non_constant_init = true;
11725 if (is_initialized)
11727 if (function_declarator_p (declarator))
11729 if (initialization_kind == CPP_EQ)
11730 initializer = cp_parser_pure_specifier (parser);
11733 /* If the declaration was erroneous, we don't really
11734 know what the user intended, so just silently
11735 consume the initializer. */
11736 if (decl != error_mark_node)
11737 error ("initializer provided for function");
11738 cp_parser_skip_to_closing_parenthesis (parser,
11739 /*recovering=*/true,
11740 /*or_comma=*/false,
11741 /*consume_paren=*/true);
11745 initializer = cp_parser_initializer (parser,
11746 &is_parenthesized_init,
11747 &is_non_constant_init);
11750 /* The old parser allows attributes to appear after a parenthesized
11751 initializer. Mark Mitchell proposed removing this functionality
11752 on the GCC mailing lists on 2002-08-13. This parser accepts the
11753 attributes -- but ignores them. */
11754 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11755 if (cp_parser_attributes_opt (parser))
11756 warning (OPT_Wattributes,
11757 "attributes after parenthesized initializer ignored");
11759 /* For an in-class declaration, use `grokfield' to create the
11765 pop_scope (pushed_scope);
11766 pushed_scope = false;
11768 decl = grokfield (declarator, decl_specifiers,
11769 initializer, !is_non_constant_init,
11770 /*asmspec=*/NULL_TREE,
11771 prefix_attributes);
11772 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11773 cp_parser_save_default_args (parser, decl);
11776 /* Finish processing the declaration. But, skip friend
11778 if (!friend_p && decl && decl != error_mark_node)
11780 cp_finish_decl (decl,
11781 initializer, !is_non_constant_init,
11783 /* If the initializer is in parentheses, then this is
11784 a direct-initialization, which means that an
11785 `explicit' constructor is OK. Otherwise, an
11786 `explicit' constructor cannot be used. */
11787 ((is_parenthesized_init || !is_initialized)
11788 ? 0 : LOOKUP_ONLYCONVERTING));
11790 if (!friend_p && pushed_scope)
11791 pop_scope (pushed_scope);
11796 /* APPLE LOCAL begin blocks 6040305 (cc) */
11798 cp_parser_cv_qualifier_or_attribute_seq_opt (cp_parser *parser, tree *attrs_p)
11800 cp_cv_quals quals = TYPE_UNQUALIFIED;
11804 *attrs_p = NULL_TREE;
11807 /* Peek at the next token. */
11808 token = cp_lexer_peek_token (parser->lexer);
11809 /* Handle attributes. */
11810 if (token->keyword == RID_ATTRIBUTE)
11812 /* Parse the attributes. */
11813 *attrs_p = chainon (*attrs_p,
11814 cp_parser_attributes_opt (parser));
11818 q = cp_parser_cv_qualifier_seq_opt (parser);
11819 if (q == TYPE_UNQUALIFIED)
11825 /* APPLE LOCAL end blocks 6040305 (cc) */
11827 /* Parse a declarator.
11831 ptr-operator declarator
11833 abstract-declarator:
11834 ptr-operator abstract-declarator [opt]
11835 direct-abstract-declarator
11840 attributes [opt] direct-declarator
11841 attributes [opt] ptr-operator declarator
11843 abstract-declarator:
11844 attributes [opt] ptr-operator abstract-declarator [opt]
11845 attributes [opt] direct-abstract-declarator
11847 APPLE LOCAL begin blocks 6339747
11849 attributes [opt] ptr-operator block-declarator [opt]
11850 attributes [opt] direct-block-declarator
11851 APPLE LOCAL end blocks 6339747
11853 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11854 detect constructor, destructor or conversion operators. It is set
11855 to -1 if the declarator is a name, and +1 if it is a
11856 function. Otherwise it is set to zero. Usually you just want to
11857 test for >0, but internally the negative value is used.
11859 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11860 a decl-specifier-seq unless it declares a constructor, destructor,
11861 or conversion. It might seem that we could check this condition in
11862 semantic analysis, rather than parsing, but that makes it difficult
11863 to handle something like `f()'. We want to notice that there are
11864 no decl-specifiers, and therefore realize that this is an
11865 expression, not a declaration.)
11867 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11868 the declarator is a direct-declarator of the form "(...)".
11870 MEMBER_P is true iff this declarator is a member-declarator. */
11872 static cp_declarator *
11873 cp_parser_declarator (cp_parser* parser,
11874 cp_parser_declarator_kind dcl_kind,
11875 int* ctor_dtor_or_conv_p,
11876 bool* parenthesized_p,
11880 cp_declarator *declarator;
11881 enum tree_code code;
11882 cp_cv_quals cv_quals;
11884 tree attributes = NULL_TREE;
11886 /* Assume this is not a constructor, destructor, or type-conversion
11888 if (ctor_dtor_or_conv_p)
11889 *ctor_dtor_or_conv_p = 0;
11891 if (cp_parser_allow_gnu_extensions_p (parser))
11892 attributes = cp_parser_attributes_opt (parser);
11894 /* Peek at the next token. */
11895 token = cp_lexer_peek_token (parser->lexer);
11897 /* APPLE LOCAL begin blocks 6040305 (cc) */
11898 if (flag_blocks && token->type == CPP_XOR)
11901 cp_declarator *inner;
11904 cp_lexer_consume_token (parser->lexer);
11906 /* cp_parse_declspecs (parser, quals_attrs, false, false, true); */
11907 quals = cp_parser_cv_qualifier_or_attribute_seq_opt (parser, &attrs);
11909 inner = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
11910 /*ctor_dtor_or_conv_p=*/NULL,
11911 /*parenthesized_p=*/NULL,
11912 /*member_p=*/false);
11913 if (inner == cp_error_declarator)
11915 return make_block_pointer_declarator (attrs, quals, inner);
11917 /* APPLE LOCAL end blocks 6040305 (cc) */
11919 /* Check for the ptr-operator production. */
11920 cp_parser_parse_tentatively (parser);
11921 /* Parse the ptr-operator. */
11922 code = cp_parser_ptr_operator (parser,
11925 /* If that worked, then we have a ptr-operator. */
11926 if (cp_parser_parse_definitely (parser))
11928 /* If a ptr-operator was found, then this declarator was not
11930 if (parenthesized_p)
11931 *parenthesized_p = true;
11932 /* The dependent declarator is optional if we are parsing an
11933 abstract-declarator. */
11934 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11935 cp_parser_parse_tentatively (parser);
11937 /* Parse the dependent declarator. */
11938 declarator = cp_parser_declarator (parser, dcl_kind,
11939 /*ctor_dtor_or_conv_p=*/NULL,
11940 /*parenthesized_p=*/NULL,
11941 /*member_p=*/false);
11943 /* If we are parsing an abstract-declarator, we must handle the
11944 case where the dependent declarator is absent. */
11945 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11946 && !cp_parser_parse_definitely (parser))
11949 /* Build the representation of the ptr-operator. */
11951 declarator = make_ptrmem_declarator (cv_quals,
11954 else if (code == INDIRECT_REF)
11955 declarator = make_pointer_declarator (cv_quals, declarator);
11957 declarator = make_reference_declarator (cv_quals, declarator);
11959 /* Everything else is a direct-declarator. */
11962 if (parenthesized_p)
11963 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11965 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11966 ctor_dtor_or_conv_p,
11970 if (attributes && declarator && declarator != cp_error_declarator)
11971 declarator->attributes = attributes;
11976 /* Parse a direct-declarator or direct-abstract-declarator.
11980 direct-declarator ( parameter-declaration-clause )
11981 cv-qualifier-seq [opt]
11982 exception-specification [opt]
11983 direct-declarator [ constant-expression [opt] ]
11986 direct-abstract-declarator:
11987 direct-abstract-declarator [opt]
11988 ( parameter-declaration-clause )
11989 cv-qualifier-seq [opt]
11990 exception-specification [opt]
11991 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11992 ( abstract-declarator )
11994 APPLE LOCAL begin blocks 6339747
11997 direct-block-declarator:
11998 direct-block-declarator [opt]
11999 ( parameter-declaration-clause ) [opt]
12000 exception-specification [opt]
12001 direct-block-declarator [opt] [ constant-expression [opt] ]
12002 ( block-declarator )
12003 APPLE LOCAL end blocks 6339747
12005 Returns a representation of the declarator. DCL_KIND is
12006 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12007 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12008 we are parsing a direct-declarator. It is
12009 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12010 of ambiguity we prefer an abstract declarator, as per
12011 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12012 cp_parser_declarator. */
12014 static cp_declarator *
12015 cp_parser_direct_declarator (cp_parser* parser,
12016 cp_parser_declarator_kind dcl_kind,
12017 int* ctor_dtor_or_conv_p,
12021 cp_declarator *declarator = NULL;
12022 tree scope = NULL_TREE;
12023 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12024 bool saved_in_declarator_p = parser->in_declarator_p;
12026 tree pushed_scope = NULL_TREE;
12030 /* Peek at the next token. */
12031 token = cp_lexer_peek_token (parser->lexer);
12032 if (token->type == CPP_OPEN_PAREN)
12034 /* This is either a parameter-declaration-clause, or a
12035 parenthesized declarator. When we know we are parsing a
12036 named declarator, it must be a parenthesized declarator
12037 if FIRST is true. For instance, `(int)' is a
12038 parameter-declaration-clause, with an omitted
12039 direct-abstract-declarator. But `((*))', is a
12040 parenthesized abstract declarator. Finally, when T is a
12041 template parameter `(T)' is a
12042 parameter-declaration-clause, and not a parenthesized
12045 We first try and parse a parameter-declaration-clause,
12046 and then try a nested declarator (if FIRST is true).
12048 It is not an error for it not to be a
12049 parameter-declaration-clause, even when FIRST is
12055 The first is the declaration of a function while the
12056 second is a the definition of a variable, including its
12059 Having seen only the parenthesis, we cannot know which of
12060 these two alternatives should be selected. Even more
12061 complex are examples like:
12066 The former is a function-declaration; the latter is a
12067 variable initialization.
12069 Thus again, we try a parameter-declaration-clause, and if
12070 that fails, we back out and return. */
12072 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12074 cp_parameter_declarator *params;
12075 unsigned saved_num_template_parameter_lists;
12077 /* In a member-declarator, the only valid interpretation
12078 of a parenthesis is the start of a
12079 parameter-declaration-clause. (It is invalid to
12080 initialize a static data member with a parenthesized
12081 initializer; only the "=" form of initialization is
12084 cp_parser_parse_tentatively (parser);
12086 /* Consume the `('. */
12087 cp_lexer_consume_token (parser->lexer);
12090 /* If this is going to be an abstract declarator, we're
12091 in a declarator and we can't have default args. */
12092 parser->default_arg_ok_p = false;
12093 parser->in_declarator_p = true;
12096 /* Inside the function parameter list, surrounding
12097 template-parameter-lists do not apply. */
12098 saved_num_template_parameter_lists
12099 = parser->num_template_parameter_lists;
12100 parser->num_template_parameter_lists = 0;
12102 /* Parse the parameter-declaration-clause. */
12103 params = cp_parser_parameter_declaration_clause (parser);
12105 parser->num_template_parameter_lists
12106 = saved_num_template_parameter_lists;
12108 /* If all went well, parse the cv-qualifier-seq and the
12109 exception-specification. */
12110 if (member_p || cp_parser_parse_definitely (parser))
12112 cp_cv_quals cv_quals;
12113 tree exception_specification;
12115 if (ctor_dtor_or_conv_p)
12116 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12118 /* Consume the `)'. */
12119 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12121 /* APPLE LOCAL begin blocks 6339747 */
12122 if (dcl_kind != BLOCKDEF)
12124 /* Parse the cv-qualifier-seq. */
12125 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12128 cv_quals = TYPE_UNQUALIFIED;
12129 /* APPLE LOCAL end blocks 6339747 */
12131 /* And the exception-specification. */
12132 exception_specification
12133 = cp_parser_exception_specification_opt (parser);
12135 /* Create the function-declarator. */
12136 declarator = make_call_declarator (declarator,
12139 exception_specification);
12140 /* Any subsequent parameter lists are to do with
12141 return type, so are not those of the declared
12143 parser->default_arg_ok_p = false;
12145 /* Repeat the main loop. */
12150 /* If this is the first, we can try a parenthesized
12154 bool saved_in_type_id_in_expr_p;
12156 parser->default_arg_ok_p = saved_default_arg_ok_p;
12157 parser->in_declarator_p = saved_in_declarator_p;
12159 /* Consume the `('. */
12160 cp_lexer_consume_token (parser->lexer);
12161 /* Parse the nested declarator. */
12162 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12163 parser->in_type_id_in_expr_p = true;
12165 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12166 /*parenthesized_p=*/NULL,
12168 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12170 /* Expect a `)'. */
12171 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12172 declarator = cp_error_declarator;
12173 if (declarator == cp_error_declarator)
12176 goto handle_declarator;
12178 /* Otherwise, we must be done. */
12182 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12183 && token->type == CPP_OPEN_SQUARE)
12185 /* Parse an array-declarator. */
12188 if (ctor_dtor_or_conv_p)
12189 *ctor_dtor_or_conv_p = 0;
12192 parser->default_arg_ok_p = false;
12193 parser->in_declarator_p = true;
12194 /* Consume the `['. */
12195 cp_lexer_consume_token (parser->lexer);
12196 /* Peek at the next token. */
12197 token = cp_lexer_peek_token (parser->lexer);
12198 /* If the next token is `]', then there is no
12199 constant-expression. */
12200 if (token->type != CPP_CLOSE_SQUARE)
12202 bool non_constant_p;
12205 = cp_parser_constant_expression (parser,
12206 /*allow_non_constant=*/true,
12208 if (!non_constant_p)
12209 bounds = fold_non_dependent_expr (bounds);
12210 /* Normally, the array bound must be an integral constant
12211 expression. However, as an extension, we allow VLAs
12212 in function scopes. */
12213 else if (!parser->in_function_body)
12215 error ("array bound is not an integer constant");
12216 bounds = error_mark_node;
12220 bounds = NULL_TREE;
12221 /* Look for the closing `]'. */
12222 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12224 declarator = cp_error_declarator;
12228 declarator = make_array_declarator (declarator, bounds);
12230 /* APPLE LOCAL begin blocks 6339747 */
12231 else if (first && (dcl_kind == CP_PARSER_DECLARATOR_NAMED
12232 || dcl_kind == CP_PARSER_DECLARATOR_EITHER))
12233 /* APPLE LOCAL end blocks 6339747 */
12235 tree qualifying_scope;
12236 tree unqualified_name;
12237 special_function_kind sfk;
12240 /* Parse a declarator-id */
12241 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12243 cp_parser_parse_tentatively (parser);
12245 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12246 qualifying_scope = parser->scope;
12249 if (!cp_parser_parse_definitely (parser))
12250 unqualified_name = error_mark_node;
12251 else if (unqualified_name
12252 && (qualifying_scope
12253 || (TREE_CODE (unqualified_name)
12254 != IDENTIFIER_NODE)))
12256 cp_parser_error (parser, "expected unqualified-id");
12257 unqualified_name = error_mark_node;
12261 if (!unqualified_name)
12263 if (unqualified_name == error_mark_node)
12265 declarator = cp_error_declarator;
12269 if (qualifying_scope && at_namespace_scope_p ()
12270 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12272 /* In the declaration of a member of a template class
12273 outside of the class itself, the SCOPE will sometimes
12274 be a TYPENAME_TYPE. For example, given:
12276 template <typename T>
12277 int S<T>::R::i = 3;
12279 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12280 this context, we must resolve S<T>::R to an ordinary
12281 type, rather than a typename type.
12283 The reason we normally avoid resolving TYPENAME_TYPEs
12284 is that a specialization of `S' might render
12285 `S<T>::R' not a type. However, if `S' is
12286 specialized, then this `i' will not be used, so there
12287 is no harm in resolving the types here. */
12290 /* Resolve the TYPENAME_TYPE. */
12291 type = resolve_typename_type (qualifying_scope,
12292 /*only_current_p=*/false);
12293 /* If that failed, the declarator is invalid. */
12294 if (type == error_mark_node)
12295 error ("%<%T::%D%> is not a type",
12296 TYPE_CONTEXT (qualifying_scope),
12297 TYPE_IDENTIFIER (qualifying_scope));
12298 qualifying_scope = type;
12302 if (unqualified_name)
12306 if (qualifying_scope
12307 && CLASS_TYPE_P (qualifying_scope))
12308 class_type = qualifying_scope;
12310 class_type = current_class_type;
12312 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12314 tree name_type = TREE_TYPE (unqualified_name);
12315 if (class_type && same_type_p (name_type, class_type))
12317 if (qualifying_scope
12318 && CLASSTYPE_USE_TEMPLATE (name_type))
12320 error ("invalid use of constructor as a template");
12321 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12322 "name the constructor in a qualified name",
12324 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12325 class_type, name_type);
12326 declarator = cp_error_declarator;
12330 unqualified_name = constructor_name (class_type);
12334 /* We do not attempt to print the declarator
12335 here because we do not have enough
12336 information about its original syntactic
12338 cp_parser_error (parser, "invalid declarator");
12339 declarator = cp_error_declarator;
12346 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12347 sfk = sfk_destructor;
12348 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12349 sfk = sfk_conversion;
12350 else if (/* There's no way to declare a constructor
12351 for an anonymous type, even if the type
12352 got a name for linkage purposes. */
12353 !TYPE_WAS_ANONYMOUS (class_type)
12354 && constructor_name_p (unqualified_name,
12357 unqualified_name = constructor_name (class_type);
12358 sfk = sfk_constructor;
12361 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12362 *ctor_dtor_or_conv_p = -1;
12365 declarator = make_id_declarator (qualifying_scope,
12368 declarator->id_loc = token->location;
12370 handle_declarator:;
12371 scope = get_scope_of_declarator (declarator);
12373 /* Any names that appear after the declarator-id for a
12374 member are looked up in the containing scope. */
12375 pushed_scope = push_scope (scope);
12376 parser->in_declarator_p = true;
12377 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12378 || (declarator && declarator->kind == cdk_id))
12379 /* Default args are only allowed on function
12381 parser->default_arg_ok_p = saved_default_arg_ok_p;
12383 parser->default_arg_ok_p = false;
12392 /* For an abstract declarator, we might wind up with nothing at this
12393 point. That's an error; the declarator is not optional. */
12394 /* APPLE LOCAL blocks 6339747 */
12395 if (!declarator && dcl_kind != CP_PARSER_DECLARATOR_BLOCK)
12396 cp_parser_error (parser, "expected declarator");
12398 /* If we entered a scope, we must exit it now. */
12400 pop_scope (pushed_scope);
12402 parser->default_arg_ok_p = saved_default_arg_ok_p;
12403 parser->in_declarator_p = saved_in_declarator_p;
12408 /* Parse a ptr-operator.
12411 * cv-qualifier-seq [opt]
12413 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12418 & cv-qualifier-seq [opt]
12419 APPLE LOCAL blocks 6040305 (cc)
12422 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12423 Returns ADDR_EXPR if a reference was used. In the case of a
12424 pointer-to-member, *TYPE is filled in with the TYPE containing the
12425 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12426 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12427 ERROR_MARK if an error occurred. */
12429 static enum tree_code
12430 cp_parser_ptr_operator (cp_parser* parser,
12432 cp_cv_quals *cv_quals)
12434 enum tree_code code = ERROR_MARK;
12437 /* Assume that it's not a pointer-to-member. */
12439 /* And that there are no cv-qualifiers. */
12440 *cv_quals = TYPE_UNQUALIFIED;
12442 /* Peek at the next token. */
12443 token = cp_lexer_peek_token (parser->lexer);
12444 /* If it's a `*' or `&' we have a pointer or reference. */
12445 if (token->type == CPP_MULT || token->type == CPP_AND)
12447 /* Remember which ptr-operator we were processing. */
12448 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12450 /* Consume the `*' or `&'. */
12451 cp_lexer_consume_token (parser->lexer);
12453 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12454 `&', if we are allowing GNU extensions. (The only qualifier
12455 that can legally appear after `&' is `restrict', but that is
12456 enforced during semantic analysis. */
12457 if (code == INDIRECT_REF
12458 || cp_parser_allow_gnu_extensions_p (parser))
12459 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12463 /* Try the pointer-to-member case. */
12464 cp_parser_parse_tentatively (parser);
12465 /* Look for the optional `::' operator. */
12466 cp_parser_global_scope_opt (parser,
12467 /*current_scope_valid_p=*/false);
12468 /* Look for the nested-name specifier. */
12469 cp_parser_nested_name_specifier (parser,
12470 /*typename_keyword_p=*/false,
12471 /*check_dependency_p=*/true,
12473 /*is_declaration=*/false);
12474 /* If we found it, and the next token is a `*', then we are
12475 indeed looking at a pointer-to-member operator. */
12476 if (!cp_parser_error_occurred (parser)
12477 && cp_parser_require (parser, CPP_MULT, "`*'"))
12479 /* Indicate that the `*' operator was used. */
12480 code = INDIRECT_REF;
12482 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12483 error ("%qD is a namespace", parser->scope);
12486 /* The type of which the member is a member is given by the
12488 *type = parser->scope;
12489 /* The next name will not be qualified. */
12490 parser->scope = NULL_TREE;
12491 parser->qualifying_scope = NULL_TREE;
12492 parser->object_scope = NULL_TREE;
12493 /* Look for the optional cv-qualifier-seq. */
12494 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12497 /* If that didn't work we don't have a ptr-operator. */
12498 if (!cp_parser_parse_definitely (parser))
12499 cp_parser_error (parser, "expected ptr-operator");
12505 /* Parse an (optional) cv-qualifier-seq.
12508 cv-qualifier cv-qualifier-seq [opt]
12519 Returns a bitmask representing the cv-qualifiers. */
12522 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12524 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12529 cp_cv_quals cv_qualifier;
12531 /* Peek at the next token. */
12532 token = cp_lexer_peek_token (parser->lexer);
12533 /* See if it's a cv-qualifier. */
12534 switch (token->keyword)
12537 cv_qualifier = TYPE_QUAL_CONST;
12541 cv_qualifier = TYPE_QUAL_VOLATILE;
12545 cv_qualifier = TYPE_QUAL_RESTRICT;
12549 cv_qualifier = TYPE_UNQUALIFIED;
12556 if (cv_quals & cv_qualifier)
12558 error ("duplicate cv-qualifier");
12559 cp_lexer_purge_token (parser->lexer);
12563 cp_lexer_consume_token (parser->lexer);
12564 cv_quals |= cv_qualifier;
12571 /* Parse a declarator-id.
12575 :: [opt] nested-name-specifier [opt] type-name
12577 In the `id-expression' case, the value returned is as for
12578 cp_parser_id_expression if the id-expression was an unqualified-id.
12579 If the id-expression was a qualified-id, then a SCOPE_REF is
12580 returned. The first operand is the scope (either a NAMESPACE_DECL
12581 or TREE_TYPE), but the second is still just a representation of an
12585 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12588 /* The expression must be an id-expression. Assume that qualified
12589 names are the names of types so that:
12592 int S<T>::R::i = 3;
12594 will work; we must treat `S<T>::R' as the name of a type.
12595 Similarly, assume that qualified names are templates, where
12599 int S<T>::R<T>::i = 3;
12602 id = cp_parser_id_expression (parser,
12603 /*template_keyword_p=*/false,
12604 /*check_dependency_p=*/false,
12605 /*template_p=*/NULL,
12606 /*declarator_p=*/true,
12608 if (id && BASELINK_P (id))
12609 id = BASELINK_FUNCTIONS (id);
12613 /* Parse a type-id.
12616 type-specifier-seq abstract-declarator [opt]
12618 Returns the TYPE specified. */
12621 cp_parser_type_id (cp_parser* parser)
12623 cp_decl_specifier_seq type_specifier_seq;
12624 cp_declarator *abstract_declarator;
12626 /* Parse the type-specifier-seq. */
12627 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12628 &type_specifier_seq);
12629 if (type_specifier_seq.type == error_mark_node)
12630 return error_mark_node;
12632 /* There might or might not be an abstract declarator. */
12633 cp_parser_parse_tentatively (parser);
12634 /* Look for the declarator. */
12635 abstract_declarator
12636 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12637 /*parenthesized_p=*/NULL,
12638 /*member_p=*/false);
12639 /* Check to see if there really was a declarator. */
12640 if (!cp_parser_parse_definitely (parser))
12641 abstract_declarator = NULL;
12643 return groktypename (&type_specifier_seq, abstract_declarator);
12646 /* Parse a type-specifier-seq.
12648 type-specifier-seq:
12649 type-specifier type-specifier-seq [opt]
12653 type-specifier-seq:
12654 attributes type-specifier-seq [opt]
12656 If IS_CONDITION is true, we are at the start of a "condition",
12657 e.g., we've just seen "if (".
12659 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12662 cp_parser_type_specifier_seq (cp_parser* parser,
12664 cp_decl_specifier_seq *type_specifier_seq)
12666 bool seen_type_specifier = false;
12667 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12669 /* Clear the TYPE_SPECIFIER_SEQ. */
12670 clear_decl_specs (type_specifier_seq);
12672 /* Parse the type-specifiers and attributes. */
12675 tree type_specifier;
12676 bool is_cv_qualifier;
12678 /* Check for attributes first. */
12679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12681 type_specifier_seq->attributes =
12682 chainon (type_specifier_seq->attributes,
12683 cp_parser_attributes_opt (parser));
12687 /* Look for the type-specifier. */
12688 type_specifier = cp_parser_type_specifier (parser,
12690 type_specifier_seq,
12691 /*is_declaration=*/false,
12694 if (!type_specifier)
12696 /* If the first type-specifier could not be found, this is not a
12697 type-specifier-seq at all. */
12698 if (!seen_type_specifier)
12700 cp_parser_error (parser, "expected type-specifier");
12701 type_specifier_seq->type = error_mark_node;
12704 /* If subsequent type-specifiers could not be found, the
12705 type-specifier-seq is complete. */
12709 seen_type_specifier = true;
12710 /* The standard says that a condition can be:
12712 type-specifier-seq declarator = assignment-expression
12719 we should treat the "S" as a declarator, not as a
12720 type-specifier. The standard doesn't say that explicitly for
12721 type-specifier-seq, but it does say that for
12722 decl-specifier-seq in an ordinary declaration. Perhaps it
12723 would be clearer just to allow a decl-specifier-seq here, and
12724 then add a semantic restriction that if any decl-specifiers
12725 that are not type-specifiers appear, the program is invalid. */
12726 if (is_condition && !is_cv_qualifier)
12727 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12730 cp_parser_check_decl_spec (type_specifier_seq);
12733 /* Parse a parameter-declaration-clause.
12735 parameter-declaration-clause:
12736 parameter-declaration-list [opt] ... [opt]
12737 parameter-declaration-list , ...
12739 Returns a representation for the parameter declarations. A return
12740 value of NULL indicates a parameter-declaration-clause consisting
12741 only of an ellipsis. */
12743 static cp_parameter_declarator *
12744 cp_parser_parameter_declaration_clause (cp_parser* parser)
12746 cp_parameter_declarator *parameters;
12751 /* Peek at the next token. */
12752 token = cp_lexer_peek_token (parser->lexer);
12753 /* Check for trivial parameter-declaration-clauses. */
12754 if (token->type == CPP_ELLIPSIS)
12756 /* Consume the `...' token. */
12757 cp_lexer_consume_token (parser->lexer);
12760 else if (token->type == CPP_CLOSE_PAREN)
12761 /* There are no parameters. */
12763 #ifndef NO_IMPLICIT_EXTERN_C
12764 if (in_system_header && current_class_type == NULL
12765 && current_lang_name == lang_name_c)
12769 return no_parameters;
12771 /* Check for `(void)', too, which is a special case. */
12772 else if (token->keyword == RID_VOID
12773 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12774 == CPP_CLOSE_PAREN))
12776 /* Consume the `void' token. */
12777 cp_lexer_consume_token (parser->lexer);
12778 /* There are no parameters. */
12779 return no_parameters;
12782 /* Parse the parameter-declaration-list. */
12783 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12784 /* If a parse error occurred while parsing the
12785 parameter-declaration-list, then the entire
12786 parameter-declaration-clause is erroneous. */
12790 /* Peek at the next token. */
12791 token = cp_lexer_peek_token (parser->lexer);
12792 /* If it's a `,', the clause should terminate with an ellipsis. */
12793 if (token->type == CPP_COMMA)
12795 /* Consume the `,'. */
12796 cp_lexer_consume_token (parser->lexer);
12797 /* Expect an ellipsis. */
12799 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12801 /* It might also be `...' if the optional trailing `,' was
12803 else if (token->type == CPP_ELLIPSIS)
12805 /* Consume the `...' token. */
12806 cp_lexer_consume_token (parser->lexer);
12807 /* And remember that we saw it. */
12811 ellipsis_p = false;
12813 /* Finish the parameter list. */
12814 if (parameters && ellipsis_p)
12815 parameters->ellipsis_p = true;
12820 /* Parse a parameter-declaration-list.
12822 parameter-declaration-list:
12823 parameter-declaration
12824 parameter-declaration-list , parameter-declaration
12826 Returns a representation of the parameter-declaration-list, as for
12827 cp_parser_parameter_declaration_clause. However, the
12828 `void_list_node' is never appended to the list. Upon return,
12829 *IS_ERROR will be true iff an error occurred. */
12831 static cp_parameter_declarator *
12832 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12834 cp_parameter_declarator *parameters = NULL;
12835 cp_parameter_declarator **tail = ¶meters;
12836 bool saved_in_unbraced_linkage_specification_p;
12838 /* Assume all will go well. */
12840 /* The special considerations that apply to a function within an
12841 unbraced linkage specifications do not apply to the parameters
12842 to the function. */
12843 saved_in_unbraced_linkage_specification_p
12844 = parser->in_unbraced_linkage_specification_p;
12845 parser->in_unbraced_linkage_specification_p = false;
12847 /* Look for more parameters. */
12850 cp_parameter_declarator *parameter;
12851 bool parenthesized_p;
12852 /* Parse the parameter. */
12854 = cp_parser_parameter_declaration (parser,
12855 /*template_parm_p=*/false,
12858 /* If a parse error occurred parsing the parameter declaration,
12859 then the entire parameter-declaration-list is erroneous. */
12866 /* Add the new parameter to the list. */
12868 tail = ¶meter->next;
12870 /* Peek at the next token. */
12871 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12872 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12873 /* These are for Objective-C++ */
12874 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12875 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12876 /* The parameter-declaration-list is complete. */
12878 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12882 /* Peek at the next token. */
12883 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12884 /* If it's an ellipsis, then the list is complete. */
12885 if (token->type == CPP_ELLIPSIS)
12887 /* Otherwise, there must be more parameters. Consume the
12889 cp_lexer_consume_token (parser->lexer);
12890 /* When parsing something like:
12892 int i(float f, double d)
12894 we can tell after seeing the declaration for "f" that we
12895 are not looking at an initialization of a variable "i",
12896 but rather at the declaration of a function "i".
12898 Due to the fact that the parsing of template arguments
12899 (as specified to a template-id) requires backtracking we
12900 cannot use this technique when inside a template argument
12902 if (!parser->in_template_argument_list_p
12903 && !parser->in_type_id_in_expr_p
12904 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12905 /* However, a parameter-declaration of the form
12906 "foat(f)" (which is a valid declaration of a
12907 parameter "f") can also be interpreted as an
12908 expression (the conversion of "f" to "float"). */
12909 && !parenthesized_p)
12910 cp_parser_commit_to_tentative_parse (parser);
12914 cp_parser_error (parser, "expected %<,%> or %<...%>");
12915 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12916 cp_parser_skip_to_closing_parenthesis (parser,
12917 /*recovering=*/true,
12918 /*or_comma=*/false,
12919 /*consume_paren=*/false);
12924 parser->in_unbraced_linkage_specification_p
12925 = saved_in_unbraced_linkage_specification_p;
12930 /* Parse a parameter declaration.
12932 parameter-declaration:
12933 decl-specifier-seq declarator
12934 decl-specifier-seq declarator = assignment-expression
12935 decl-specifier-seq abstract-declarator [opt]
12936 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12938 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12939 declares a template parameter. (In that case, a non-nested `>'
12940 token encountered during the parsing of the assignment-expression
12941 is not interpreted as a greater-than operator.)
12943 Returns a representation of the parameter, or NULL if an error
12944 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12945 true iff the declarator is of the form "(p)". */
12947 static cp_parameter_declarator *
12948 cp_parser_parameter_declaration (cp_parser *parser,
12949 bool template_parm_p,
12950 bool *parenthesized_p)
12952 int declares_class_or_enum;
12953 bool greater_than_is_operator_p;
12954 cp_decl_specifier_seq decl_specifiers;
12955 cp_declarator *declarator;
12956 tree default_argument;
12958 const char *saved_message;
12960 /* In a template parameter, `>' is not an operator.
12964 When parsing a default template-argument for a non-type
12965 template-parameter, the first non-nested `>' is taken as the end
12966 of the template parameter-list rather than a greater-than
12968 greater_than_is_operator_p = !template_parm_p;
12970 /* Type definitions may not appear in parameter types. */
12971 saved_message = parser->type_definition_forbidden_message;
12972 parser->type_definition_forbidden_message
12973 = "types may not be defined in parameter types";
12975 /* Parse the declaration-specifiers. */
12976 cp_parser_decl_specifier_seq (parser,
12977 CP_PARSER_FLAGS_NONE,
12979 &declares_class_or_enum);
12980 /* If an error occurred, there's no reason to attempt to parse the
12981 rest of the declaration. */
12982 if (cp_parser_error_occurred (parser))
12984 parser->type_definition_forbidden_message = saved_message;
12988 /* Peek at the next token. */
12989 token = cp_lexer_peek_token (parser->lexer);
12990 /* If the next token is a `)', `,', `=', `>', or `...', then there
12991 is no declarator. */
12992 if (token->type == CPP_CLOSE_PAREN
12993 || token->type == CPP_COMMA
12994 || token->type == CPP_EQ
12995 || token->type == CPP_ELLIPSIS
12996 || token->type == CPP_GREATER)
12999 if (parenthesized_p)
13000 *parenthesized_p = false;
13002 /* Otherwise, there should be a declarator. */
13005 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13006 parser->default_arg_ok_p = false;
13008 /* After seeing a decl-specifier-seq, if the next token is not a
13009 "(", there is no possibility that the code is a valid
13010 expression. Therefore, if parsing tentatively, we commit at
13012 if (!parser->in_template_argument_list_p
13013 /* In an expression context, having seen:
13017 we cannot be sure whether we are looking at a
13018 function-type (taking a "char" as a parameter) or a cast
13019 of some object of type "char" to "int". */
13020 && !parser->in_type_id_in_expr_p
13021 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13022 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13023 cp_parser_commit_to_tentative_parse (parser);
13024 /* Parse the declarator. */
13025 declarator = cp_parser_declarator (parser,
13026 CP_PARSER_DECLARATOR_EITHER,
13027 /*ctor_dtor_or_conv_p=*/NULL,
13029 /*member_p=*/false);
13030 parser->default_arg_ok_p = saved_default_arg_ok_p;
13031 /* After the declarator, allow more attributes. */
13032 decl_specifiers.attributes
13033 = chainon (decl_specifiers.attributes,
13034 cp_parser_attributes_opt (parser));
13037 /* The restriction on defining new types applies only to the type
13038 of the parameter, not to the default argument. */
13039 parser->type_definition_forbidden_message = saved_message;
13041 /* If the next token is `=', then process a default argument. */
13042 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13044 bool saved_greater_than_is_operator_p;
13045 /* Consume the `='. */
13046 cp_lexer_consume_token (parser->lexer);
13048 /* If we are defining a class, then the tokens that make up the
13049 default argument must be saved and processed later. */
13050 if (!template_parm_p && at_class_scope_p ()
13051 && TYPE_BEING_DEFINED (current_class_type))
13053 unsigned depth = 0;
13054 cp_token *first_token;
13057 /* Add tokens until we have processed the entire default
13058 argument. We add the range [first_token, token). */
13059 first_token = cp_lexer_peek_token (parser->lexer);
13064 /* Peek at the next token. */
13065 token = cp_lexer_peek_token (parser->lexer);
13066 /* What we do depends on what token we have. */
13067 switch (token->type)
13069 /* In valid code, a default argument must be
13070 immediately followed by a `,' `)', or `...'. */
13072 case CPP_CLOSE_PAREN:
13074 /* If we run into a non-nested `;', `}', or `]',
13075 then the code is invalid -- but the default
13076 argument is certainly over. */
13077 case CPP_SEMICOLON:
13078 case CPP_CLOSE_BRACE:
13079 case CPP_CLOSE_SQUARE:
13082 /* Update DEPTH, if necessary. */
13083 else if (token->type == CPP_CLOSE_PAREN
13084 || token->type == CPP_CLOSE_BRACE
13085 || token->type == CPP_CLOSE_SQUARE)
13089 case CPP_OPEN_PAREN:
13090 case CPP_OPEN_SQUARE:
13091 case CPP_OPEN_BRACE:
13096 /* If we see a non-nested `>', and `>' is not an
13097 operator, then it marks the end of the default
13099 if (!depth && !greater_than_is_operator_p)
13103 /* If we run out of tokens, issue an error message. */
13105 case CPP_PRAGMA_EOL:
13106 error ("file ends in default argument");
13112 /* In these cases, we should look for template-ids.
13113 For example, if the default argument is
13114 `X<int, double>()', we need to do name lookup to
13115 figure out whether or not `X' is a template; if
13116 so, the `,' does not end the default argument.
13118 That is not yet done. */
13125 /* If we've reached the end, stop. */
13129 /* Add the token to the token block. */
13130 token = cp_lexer_consume_token (parser->lexer);
13133 /* Create a DEFAULT_ARG to represented the unparsed default
13135 default_argument = make_node (DEFAULT_ARG);
13136 DEFARG_TOKENS (default_argument)
13137 = cp_token_cache_new (first_token, token);
13138 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13140 /* Outside of a class definition, we can just parse the
13141 assignment-expression. */
13144 bool saved_local_variables_forbidden_p;
13146 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13148 saved_greater_than_is_operator_p
13149 = parser->greater_than_is_operator_p;
13150 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13151 /* Local variable names (and the `this' keyword) may not
13152 appear in a default argument. */
13153 saved_local_variables_forbidden_p
13154 = parser->local_variables_forbidden_p;
13155 parser->local_variables_forbidden_p = true;
13156 /* The default argument expression may cause implicitly
13157 defined member functions to be synthesized, which will
13158 result in garbage collection. We must treat this
13159 situation as if we were within the body of function so as
13160 to avoid collecting live data on the stack. */
13162 /* Parse the assignment-expression. */
13163 if (template_parm_p)
13164 push_deferring_access_checks (dk_no_deferred);
13166 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13167 if (template_parm_p)
13168 pop_deferring_access_checks ();
13169 /* Restore saved state. */
13171 parser->greater_than_is_operator_p
13172 = saved_greater_than_is_operator_p;
13173 parser->local_variables_forbidden_p
13174 = saved_local_variables_forbidden_p;
13176 if (!parser->default_arg_ok_p)
13178 if (!flag_pedantic_errors)
13179 warning (0, "deprecated use of default argument for parameter of non-function");
13182 error ("default arguments are only permitted for function parameters");
13183 default_argument = NULL_TREE;
13188 default_argument = NULL_TREE;
13190 return make_parameter_declarator (&decl_specifiers,
13195 /* Parse a function-body.
13198 compound_statement */
13201 cp_parser_function_body (cp_parser *parser)
13203 /* APPLE LOCAL radar 5982990 */
13204 cp_parser_compound_statement (parser, NULL, false, false);
13207 /* Parse a ctor-initializer-opt followed by a function-body. Return
13208 true if a ctor-initializer was present. */
13211 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13214 bool ctor_initializer_p;
13216 /* Begin the function body. */
13217 body = begin_function_body ();
13218 /* Parse the optional ctor-initializer. */
13219 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13220 /* Parse the function-body. */
13221 cp_parser_function_body (parser);
13222 /* Finish the function body. */
13223 finish_function_body (body);
13225 return ctor_initializer_p;
13228 /* Parse an initializer.
13231 = initializer-clause
13232 ( expression-list )
13234 Returns an expression representing the initializer. If no
13235 initializer is present, NULL_TREE is returned.
13237 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13238 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13239 set to FALSE if there is no initializer present. If there is an
13240 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13241 is set to true; otherwise it is set to false. */
13244 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13245 bool* non_constant_p)
13250 /* Peek at the next token. */
13251 token = cp_lexer_peek_token (parser->lexer);
13253 /* Let our caller know whether or not this initializer was
13255 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13256 /* Assume that the initializer is constant. */
13257 *non_constant_p = false;
13259 if (token->type == CPP_EQ)
13261 /* Consume the `='. */
13262 cp_lexer_consume_token (parser->lexer);
13263 /* Parse the initializer-clause. */
13264 init = cp_parser_initializer_clause (parser, non_constant_p);
13266 else if (token->type == CPP_OPEN_PAREN)
13267 init = cp_parser_parenthesized_expression_list (parser, false,
13272 /* Anything else is an error. */
13273 cp_parser_error (parser, "expected initializer");
13274 init = error_mark_node;
13280 /* Parse an initializer-clause.
13282 initializer-clause:
13283 assignment-expression
13284 { initializer-list , [opt] }
13287 Returns an expression representing the initializer.
13289 If the `assignment-expression' production is used the value
13290 returned is simply a representation for the expression.
13292 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13293 the elements of the initializer-list (or NULL, if the last
13294 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13295 NULL_TREE. There is no way to detect whether or not the optional
13296 trailing `,' was provided. NON_CONSTANT_P is as for
13297 cp_parser_initializer. */
13300 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13304 /* Assume the expression is constant. */
13305 *non_constant_p = false;
13307 /* If it is not a `{', then we are looking at an
13308 assignment-expression. */
13309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13312 = cp_parser_constant_expression (parser,
13313 /*allow_non_constant_p=*/true,
13315 if (!*non_constant_p)
13316 initializer = fold_non_dependent_expr (initializer);
13320 /* Consume the `{' token. */
13321 cp_lexer_consume_token (parser->lexer);
13322 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13323 initializer = make_node (CONSTRUCTOR);
13324 /* If it's not a `}', then there is a non-trivial initializer. */
13325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13327 /* Parse the initializer list. */
13328 CONSTRUCTOR_ELTS (initializer)
13329 = cp_parser_initializer_list (parser, non_constant_p);
13330 /* A trailing `,' token is allowed. */
13331 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13332 cp_lexer_consume_token (parser->lexer);
13334 /* Now, there should be a trailing `}'. */
13335 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13338 return initializer;
13341 /* Parse an initializer-list.
13345 initializer-list , initializer-clause
13350 identifier : initializer-clause
13351 initializer-list, identifier : initializer-clause
13353 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13354 for the initializer. If the INDEX of the elt is non-NULL, it is the
13355 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13356 as for cp_parser_initializer. */
13358 static VEC(constructor_elt,gc) *
13359 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13361 VEC(constructor_elt,gc) *v = NULL;
13363 /* Assume all of the expressions are constant. */
13364 *non_constant_p = false;
13366 /* Parse the rest of the list. */
13372 bool clause_non_constant_p;
13374 /* If the next token is an identifier and the following one is a
13375 colon, we are looking at the GNU designated-initializer
13377 if (cp_parser_allow_gnu_extensions_p (parser)
13378 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13379 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13381 /* Warn the user that they are using an extension. */
13383 pedwarn ("ISO C++ does not allow designated initializers");
13384 /* Consume the identifier. */
13385 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13386 /* Consume the `:'. */
13387 cp_lexer_consume_token (parser->lexer);
13390 identifier = NULL_TREE;
13392 /* Parse the initializer. */
13393 initializer = cp_parser_initializer_clause (parser,
13394 &clause_non_constant_p);
13395 /* If any clause is non-constant, so is the entire initializer. */
13396 if (clause_non_constant_p)
13397 *non_constant_p = true;
13399 /* Add it to the vector. */
13400 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13402 /* If the next token is not a comma, we have reached the end of
13404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13407 /* Peek at the next token. */
13408 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13409 /* If the next token is a `}', then we're still done. An
13410 initializer-clause can have a trailing `,' after the
13411 initializer-list and before the closing `}'. */
13412 if (token->type == CPP_CLOSE_BRACE)
13415 /* Consume the `,' token. */
13416 cp_lexer_consume_token (parser->lexer);
13422 /* Classes [gram.class] */
13424 /* Parse a class-name.
13430 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13431 to indicate that names looked up in dependent types should be
13432 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13433 keyword has been used to indicate that the name that appears next
13434 is a template. TAG_TYPE indicates the explicit tag given before
13435 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13436 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13437 is the class being defined in a class-head.
13439 Returns the TYPE_DECL representing the class. */
13442 cp_parser_class_name (cp_parser *parser,
13443 bool typename_keyword_p,
13444 bool template_keyword_p,
13445 enum tag_types tag_type,
13446 bool check_dependency_p,
13448 bool is_declaration)
13455 /* All class-names start with an identifier. */
13456 token = cp_lexer_peek_token (parser->lexer);
13457 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13459 cp_parser_error (parser, "expected class-name");
13460 return error_mark_node;
13463 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13464 to a template-id, so we save it here. */
13465 scope = parser->scope;
13466 if (scope == error_mark_node)
13467 return error_mark_node;
13469 /* Any name names a type if we're following the `typename' keyword
13470 in a qualified name where the enclosing scope is type-dependent. */
13471 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13472 && dependent_type_p (scope));
13473 /* Handle the common case (an identifier, but not a template-id)
13475 if (token->type == CPP_NAME
13476 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13478 cp_token *identifier_token;
13482 /* Look for the identifier. */
13483 identifier_token = cp_lexer_peek_token (parser->lexer);
13484 ambiguous_p = identifier_token->ambiguous_p;
13485 identifier = cp_parser_identifier (parser);
13486 /* If the next token isn't an identifier, we are certainly not
13487 looking at a class-name. */
13488 if (identifier == error_mark_node)
13489 decl = error_mark_node;
13490 /* If we know this is a type-name, there's no need to look it
13492 else if (typename_p)
13496 tree ambiguous_decls;
13497 /* If we already know that this lookup is ambiguous, then
13498 we've already issued an error message; there's no reason
13502 cp_parser_simulate_error (parser);
13503 return error_mark_node;
13505 /* If the next token is a `::', then the name must be a type
13508 [basic.lookup.qual]
13510 During the lookup for a name preceding the :: scope
13511 resolution operator, object, function, and enumerator
13512 names are ignored. */
13513 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13514 tag_type = typename_type;
13515 /* Look up the name. */
13516 decl = cp_parser_lookup_name (parser, identifier,
13518 /*is_template=*/false,
13519 /*is_namespace=*/false,
13520 check_dependency_p,
13522 if (ambiguous_decls)
13524 error ("reference to %qD is ambiguous", identifier);
13525 print_candidates (ambiguous_decls);
13526 if (cp_parser_parsing_tentatively (parser))
13528 identifier_token->ambiguous_p = true;
13529 cp_parser_simulate_error (parser);
13531 return error_mark_node;
13537 /* Try a template-id. */
13538 decl = cp_parser_template_id (parser, template_keyword_p,
13539 check_dependency_p,
13541 if (decl == error_mark_node)
13542 return error_mark_node;
13545 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13547 /* If this is a typename, create a TYPENAME_TYPE. */
13548 if (typename_p && decl != error_mark_node)
13550 decl = make_typename_type (scope, decl, typename_type,
13551 /*complain=*/tf_error);
13552 if (decl != error_mark_node)
13553 decl = TYPE_NAME (decl);
13556 /* Check to see that it is really the name of a class. */
13557 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13558 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13559 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13560 /* Situations like this:
13562 template <typename T> struct A {
13563 typename T::template X<int>::I i;
13566 are problematic. Is `T::template X<int>' a class-name? The
13567 standard does not seem to be definitive, but there is no other
13568 valid interpretation of the following `::'. Therefore, those
13569 names are considered class-names. */
13571 decl = make_typename_type (scope, decl, tag_type, tf_error);
13572 if (decl != error_mark_node)
13573 decl = TYPE_NAME (decl);
13575 else if (TREE_CODE (decl) != TYPE_DECL
13576 || TREE_TYPE (decl) == error_mark_node
13577 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13578 decl = error_mark_node;
13580 if (decl == error_mark_node)
13581 cp_parser_error (parser, "expected class-name");
13586 /* Parse a class-specifier.
13589 class-head { member-specification [opt] }
13591 Returns the TREE_TYPE representing the class. */
13594 cp_parser_class_specifier (cp_parser* parser)
13598 tree attributes = NULL_TREE;
13599 int has_trailing_semicolon;
13600 bool nested_name_specifier_p;
13601 unsigned saved_num_template_parameter_lists;
13602 bool saved_in_function_body;
13603 tree old_scope = NULL_TREE;
13604 tree scope = NULL_TREE;
13607 push_deferring_access_checks (dk_no_deferred);
13609 /* Parse the class-head. */
13610 type = cp_parser_class_head (parser,
13611 &nested_name_specifier_p,
13614 /* If the class-head was a semantic disaster, skip the entire body
13618 cp_parser_skip_to_end_of_block_or_statement (parser);
13619 pop_deferring_access_checks ();
13620 return error_mark_node;
13623 /* Look for the `{'. */
13624 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13626 pop_deferring_access_checks ();
13627 return error_mark_node;
13630 /* Process the base classes. If they're invalid, skip the
13631 entire class body. */
13632 if (!xref_basetypes (type, bases))
13634 cp_parser_skip_to_closing_brace (parser);
13636 /* Consuming the closing brace yields better error messages
13638 cp_lexer_consume_token (parser->lexer);
13639 pop_deferring_access_checks ();
13640 return error_mark_node;
13643 /* Issue an error message if type-definitions are forbidden here. */
13644 cp_parser_check_type_definition (parser);
13645 /* Remember that we are defining one more class. */
13646 ++parser->num_classes_being_defined;
13647 /* Inside the class, surrounding template-parameter-lists do not
13649 saved_num_template_parameter_lists
13650 = parser->num_template_parameter_lists;
13651 parser->num_template_parameter_lists = 0;
13652 /* We are not in a function body. */
13653 saved_in_function_body = parser->in_function_body;
13654 parser->in_function_body = false;
13656 /* Start the class. */
13657 if (nested_name_specifier_p)
13659 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13660 old_scope = push_inner_scope (scope);
13662 type = begin_class_definition (type, attributes);
13664 if (type == error_mark_node)
13665 /* If the type is erroneous, skip the entire body of the class. */
13666 cp_parser_skip_to_closing_brace (parser);
13668 /* Parse the member-specification. */
13669 cp_parser_member_specification_opt (parser);
13671 /* Look for the trailing `}'. */
13672 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13673 /* We get better error messages by noticing a common problem: a
13674 missing trailing `;'. */
13675 token = cp_lexer_peek_token (parser->lexer);
13676 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13677 /* Look for trailing attributes to apply to this class. */
13678 if (cp_parser_allow_gnu_extensions_p (parser))
13679 attributes = cp_parser_attributes_opt (parser);
13680 if (type != error_mark_node)
13681 type = finish_struct (type, attributes);
13682 if (nested_name_specifier_p)
13683 pop_inner_scope (old_scope, scope);
13684 /* If this class is not itself within the scope of another class,
13685 then we need to parse the bodies of all of the queued function
13686 definitions. Note that the queued functions defined in a class
13687 are not always processed immediately following the
13688 class-specifier for that class. Consider:
13691 struct B { void f() { sizeof (A); } };
13694 If `f' were processed before the processing of `A' were
13695 completed, there would be no way to compute the size of `A'.
13696 Note that the nesting we are interested in here is lexical --
13697 not the semantic nesting given by TYPE_CONTEXT. In particular,
13700 struct A { struct B; };
13701 struct A::B { void f() { } };
13703 there is no need to delay the parsing of `A::B::f'. */
13704 if (--parser->num_classes_being_defined == 0)
13708 tree class_type = NULL_TREE;
13709 tree pushed_scope = NULL_TREE;
13711 /* In a first pass, parse default arguments to the functions.
13712 Then, in a second pass, parse the bodies of the functions.
13713 This two-phased approach handles cases like:
13721 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13722 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13723 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13724 TREE_PURPOSE (parser->unparsed_functions_queues)
13725 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13727 fn = TREE_VALUE (queue_entry);
13728 /* If there are default arguments that have not yet been processed,
13729 take care of them now. */
13730 if (class_type != TREE_PURPOSE (queue_entry))
13733 pop_scope (pushed_scope);
13734 class_type = TREE_PURPOSE (queue_entry);
13735 pushed_scope = push_scope (class_type);
13737 /* Make sure that any template parameters are in scope. */
13738 maybe_begin_member_template_processing (fn);
13739 /* Parse the default argument expressions. */
13740 cp_parser_late_parsing_default_args (parser, fn);
13741 /* Remove any template parameters from the symbol table. */
13742 maybe_end_member_template_processing ();
13745 pop_scope (pushed_scope);
13746 /* Now parse the body of the functions. */
13747 for (TREE_VALUE (parser->unparsed_functions_queues)
13748 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13749 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13750 TREE_VALUE (parser->unparsed_functions_queues)
13751 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13753 /* Figure out which function we need to process. */
13754 fn = TREE_VALUE (queue_entry);
13755 /* Parse the function. */
13756 cp_parser_late_parsing_for_member (parser, fn);
13760 /* Put back any saved access checks. */
13761 pop_deferring_access_checks ();
13763 /* Restore saved state. */
13764 parser->in_function_body = saved_in_function_body;
13765 parser->num_template_parameter_lists
13766 = saved_num_template_parameter_lists;
13771 /* Parse a class-head.
13774 class-key identifier [opt] base-clause [opt]
13775 class-key nested-name-specifier identifier base-clause [opt]
13776 class-key nested-name-specifier [opt] template-id
13780 class-key attributes identifier [opt] base-clause [opt]
13781 class-key attributes nested-name-specifier identifier base-clause [opt]
13782 class-key attributes nested-name-specifier [opt] template-id
13785 Returns the TYPE of the indicated class. Sets
13786 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13787 involving a nested-name-specifier was used, and FALSE otherwise.
13789 Returns error_mark_node if this is not a class-head.
13791 Returns NULL_TREE if the class-head is syntactically valid, but
13792 semantically invalid in a way that means we should skip the entire
13793 body of the class. */
13796 cp_parser_class_head (cp_parser* parser,
13797 bool* nested_name_specifier_p,
13798 tree *attributes_p,
13801 tree nested_name_specifier;
13802 enum tag_types class_key;
13803 tree id = NULL_TREE;
13804 tree type = NULL_TREE;
13806 bool template_id_p = false;
13807 bool qualified_p = false;
13808 bool invalid_nested_name_p = false;
13809 bool invalid_explicit_specialization_p = false;
13810 tree pushed_scope = NULL_TREE;
13811 unsigned num_templates;
13813 /* Assume no nested-name-specifier will be present. */
13814 *nested_name_specifier_p = false;
13815 /* Assume no template parameter lists will be used in defining the
13819 /* Look for the class-key. */
13820 class_key = cp_parser_class_key (parser);
13821 if (class_key == none_type)
13822 return error_mark_node;
13824 /* Parse the attributes. */
13825 attributes = cp_parser_attributes_opt (parser);
13827 /* If the next token is `::', that is invalid -- but sometimes
13828 people do try to write:
13832 Handle this gracefully by accepting the extra qualifier, and then
13833 issuing an error about it later if this really is a
13834 class-head. If it turns out just to be an elaborated type
13835 specifier, remain silent. */
13836 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13837 qualified_p = true;
13839 push_deferring_access_checks (dk_no_check);
13841 /* Determine the name of the class. Begin by looking for an
13842 optional nested-name-specifier. */
13843 nested_name_specifier
13844 = cp_parser_nested_name_specifier_opt (parser,
13845 /*typename_keyword_p=*/false,
13846 /*check_dependency_p=*/false,
13848 /*is_declaration=*/false);
13849 /* If there was a nested-name-specifier, then there *must* be an
13851 if (nested_name_specifier)
13853 /* Although the grammar says `identifier', it really means
13854 `class-name' or `template-name'. You are only allowed to
13855 define a class that has already been declared with this
13858 The proposed resolution for Core Issue 180 says that wherever
13859 you see `class T::X' you should treat `X' as a type-name.
13861 It is OK to define an inaccessible class; for example:
13863 class A { class B; };
13866 We do not know if we will see a class-name, or a
13867 template-name. We look for a class-name first, in case the
13868 class-name is a template-id; if we looked for the
13869 template-name first we would stop after the template-name. */
13870 cp_parser_parse_tentatively (parser);
13871 type = cp_parser_class_name (parser,
13872 /*typename_keyword_p=*/false,
13873 /*template_keyword_p=*/false,
13875 /*check_dependency_p=*/false,
13876 /*class_head_p=*/true,
13877 /*is_declaration=*/false);
13878 /* If that didn't work, ignore the nested-name-specifier. */
13879 if (!cp_parser_parse_definitely (parser))
13881 invalid_nested_name_p = true;
13882 id = cp_parser_identifier (parser);
13883 if (id == error_mark_node)
13886 /* If we could not find a corresponding TYPE, treat this
13887 declaration like an unqualified declaration. */
13888 if (type == error_mark_node)
13889 nested_name_specifier = NULL_TREE;
13890 /* Otherwise, count the number of templates used in TYPE and its
13891 containing scopes. */
13896 for (scope = TREE_TYPE (type);
13897 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13898 scope = (TYPE_P (scope)
13899 ? TYPE_CONTEXT (scope)
13900 : DECL_CONTEXT (scope)))
13902 && CLASS_TYPE_P (scope)
13903 && CLASSTYPE_TEMPLATE_INFO (scope)
13904 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13905 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13909 /* Otherwise, the identifier is optional. */
13912 /* We don't know whether what comes next is a template-id,
13913 an identifier, or nothing at all. */
13914 cp_parser_parse_tentatively (parser);
13915 /* Check for a template-id. */
13916 id = cp_parser_template_id (parser,
13917 /*template_keyword_p=*/false,
13918 /*check_dependency_p=*/true,
13919 /*is_declaration=*/true);
13920 /* If that didn't work, it could still be an identifier. */
13921 if (!cp_parser_parse_definitely (parser))
13923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13924 id = cp_parser_identifier (parser);
13930 template_id_p = true;
13935 pop_deferring_access_checks ();
13938 cp_parser_check_for_invalid_template_id (parser, id);
13940 /* If it's not a `:' or a `{' then we can't really be looking at a
13941 class-head, since a class-head only appears as part of a
13942 class-specifier. We have to detect this situation before calling
13943 xref_tag, since that has irreversible side-effects. */
13944 if (!cp_parser_next_token_starts_class_definition_p (parser))
13946 cp_parser_error (parser, "expected %<{%> or %<:%>");
13947 return error_mark_node;
13950 /* At this point, we're going ahead with the class-specifier, even
13951 if some other problem occurs. */
13952 cp_parser_commit_to_tentative_parse (parser);
13953 /* Issue the error about the overly-qualified name now. */
13955 cp_parser_error (parser,
13956 "global qualification of class name is invalid");
13957 else if (invalid_nested_name_p)
13958 cp_parser_error (parser,
13959 "qualified name does not name a class");
13960 else if (nested_name_specifier)
13964 /* Reject typedef-names in class heads. */
13965 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13967 error ("invalid class name in declaration of %qD", type);
13972 /* Figure out in what scope the declaration is being placed. */
13973 scope = current_scope ();
13974 /* If that scope does not contain the scope in which the
13975 class was originally declared, the program is invalid. */
13976 if (scope && !is_ancestor (scope, nested_name_specifier))
13978 error ("declaration of %qD in %qD which does not enclose %qD",
13979 type, scope, nested_name_specifier);
13985 A declarator-id shall not be qualified exception of the
13986 definition of a ... nested class outside of its class
13987 ... [or] a the definition or explicit instantiation of a
13988 class member of a namespace outside of its namespace. */
13989 if (scope == nested_name_specifier)
13991 pedwarn ("extra qualification ignored");
13992 nested_name_specifier = NULL_TREE;
13996 /* An explicit-specialization must be preceded by "template <>". If
13997 it is not, try to recover gracefully. */
13998 if (at_namespace_scope_p ()
13999 && parser->num_template_parameter_lists == 0
14002 error ("an explicit specialization must be preceded by %<template <>%>");
14003 invalid_explicit_specialization_p = true;
14004 /* Take the same action that would have been taken by
14005 cp_parser_explicit_specialization. */
14006 ++parser->num_template_parameter_lists;
14007 begin_specialization ();
14009 /* There must be no "return" statements between this point and the
14010 end of this function; set "type "to the correct return value and
14011 use "goto done;" to return. */
14012 /* Make sure that the right number of template parameters were
14014 if (!cp_parser_check_template_parameters (parser, num_templates))
14016 /* If something went wrong, there is no point in even trying to
14017 process the class-definition. */
14022 /* Look up the type. */
14025 type = TREE_TYPE (id);
14026 type = maybe_process_partial_specialization (type);
14027 if (nested_name_specifier)
14028 pushed_scope = push_scope (nested_name_specifier);
14030 else if (nested_name_specifier)
14036 template <typename T> struct S { struct T };
14037 template <typename T> struct S<T>::T { };
14039 we will get a TYPENAME_TYPE when processing the definition of
14040 `S::T'. We need to resolve it to the actual type before we
14041 try to define it. */
14042 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14044 class_type = resolve_typename_type (TREE_TYPE (type),
14045 /*only_current_p=*/false);
14046 if (class_type != error_mark_node)
14047 type = TYPE_NAME (class_type);
14050 cp_parser_error (parser, "could not resolve typename type");
14051 type = error_mark_node;
14055 maybe_process_partial_specialization (TREE_TYPE (type));
14056 class_type = current_class_type;
14057 /* Enter the scope indicated by the nested-name-specifier. */
14058 pushed_scope = push_scope (nested_name_specifier);
14059 /* Get the canonical version of this type. */
14060 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14061 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14062 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14064 type = push_template_decl (type);
14065 if (type == error_mark_node)
14072 type = TREE_TYPE (type);
14073 *nested_name_specifier_p = true;
14075 else /* The name is not a nested name. */
14077 /* If the class was unnamed, create a dummy name. */
14079 id = make_anon_name ();
14080 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14081 parser->num_template_parameter_lists);
14084 /* Indicate whether this class was declared as a `class' or as a
14086 if (TREE_CODE (type) == RECORD_TYPE)
14087 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14088 cp_parser_check_class_key (class_key, type);
14090 /* If this type was already complete, and we see another definition,
14091 that's an error. */
14092 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14094 error ("redefinition of %q#T", type);
14095 error ("previous definition of %q+#T", type);
14099 else if (type == error_mark_node)
14102 /* We will have entered the scope containing the class; the names of
14103 base classes should be looked up in that context. For example:
14105 struct A { struct B {}; struct C; };
14106 struct A::C : B {};
14109 *bases = NULL_TREE;
14111 /* Get the list of base-classes, if there is one. */
14112 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14113 *bases = cp_parser_base_clause (parser);
14116 /* Leave the scope given by the nested-name-specifier. We will
14117 enter the class scope itself while processing the members. */
14119 pop_scope (pushed_scope);
14121 if (invalid_explicit_specialization_p)
14123 end_specialization ();
14124 --parser->num_template_parameter_lists;
14126 *attributes_p = attributes;
14130 /* Parse a class-key.
14137 Returns the kind of class-key specified, or none_type to indicate
14140 static enum tag_types
14141 cp_parser_class_key (cp_parser* parser)
14144 enum tag_types tag_type;
14146 /* Look for the class-key. */
14147 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14151 /* Check to see if the TOKEN is a class-key. */
14152 tag_type = cp_parser_token_is_class_key (token);
14154 cp_parser_error (parser, "expected class-key");
14158 /* Parse an (optional) member-specification.
14160 member-specification:
14161 member-declaration member-specification [opt]
14162 access-specifier : member-specification [opt] */
14165 cp_parser_member_specification_opt (cp_parser* parser)
14172 /* Peek at the next token. */
14173 token = cp_lexer_peek_token (parser->lexer);
14174 /* If it's a `}', or EOF then we've seen all the members. */
14175 if (token->type == CPP_CLOSE_BRACE
14176 || token->type == CPP_EOF
14177 || token->type == CPP_PRAGMA_EOL)
14180 /* See if this token is a keyword. */
14181 keyword = token->keyword;
14185 case RID_PROTECTED:
14187 /* Consume the access-specifier. */
14188 cp_lexer_consume_token (parser->lexer);
14189 /* Remember which access-specifier is active. */
14190 current_access_specifier = token->u.value;
14191 /* Look for the `:'. */
14192 cp_parser_require (parser, CPP_COLON, "`:'");
14196 /* Accept #pragmas at class scope. */
14197 if (token->type == CPP_PRAGMA)
14199 cp_parser_pragma (parser, pragma_external);
14203 /* Otherwise, the next construction must be a
14204 member-declaration. */
14205 cp_parser_member_declaration (parser);
14210 /* Parse a member-declaration.
14212 member-declaration:
14213 decl-specifier-seq [opt] member-declarator-list [opt] ;
14214 function-definition ; [opt]
14215 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14217 template-declaration
14219 member-declarator-list:
14221 member-declarator-list , member-declarator
14224 declarator pure-specifier [opt]
14225 declarator constant-initializer [opt]
14226 identifier [opt] : constant-expression
14230 member-declaration:
14231 __extension__ member-declaration
14234 declarator attributes [opt] pure-specifier [opt]
14235 declarator attributes [opt] constant-initializer [opt]
14236 identifier [opt] attributes [opt] : constant-expression */
14239 cp_parser_member_declaration (cp_parser* parser)
14241 cp_decl_specifier_seq decl_specifiers;
14242 tree prefix_attributes;
14244 int declares_class_or_enum;
14247 int saved_pedantic;
14249 /* Check for the `__extension__' keyword. */
14250 if (cp_parser_extension_opt (parser, &saved_pedantic))
14253 cp_parser_member_declaration (parser);
14254 /* Restore the old value of the PEDANTIC flag. */
14255 pedantic = saved_pedantic;
14260 /* Check for a template-declaration. */
14261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14263 /* An explicit specialization here is an error condition, and we
14264 expect the specialization handler to detect and report this. */
14265 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14266 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14267 cp_parser_explicit_specialization (parser);
14269 cp_parser_template_declaration (parser, /*member_p=*/true);
14274 /* Check for a using-declaration. */
14275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14277 /* Parse the using-declaration. */
14278 cp_parser_using_declaration (parser,
14279 /*access_declaration_p=*/false);
14283 /* Check for @defs. */
14284 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14287 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14288 ivar = ivar_chains;
14292 ivar = TREE_CHAIN (member);
14293 TREE_CHAIN (member) = NULL_TREE;
14294 finish_member_declaration (member);
14299 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14302 /* Parse the decl-specifier-seq. */
14303 cp_parser_decl_specifier_seq (parser,
14304 CP_PARSER_FLAGS_OPTIONAL,
14306 &declares_class_or_enum);
14307 prefix_attributes = decl_specifiers.attributes;
14308 decl_specifiers.attributes = NULL_TREE;
14309 /* Check for an invalid type-name. */
14310 if (!decl_specifiers.type
14311 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14313 /* If there is no declarator, then the decl-specifier-seq should
14315 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14317 /* If there was no decl-specifier-seq, and the next token is a
14318 `;', then we have something like:
14324 Each member-declaration shall declare at least one member
14325 name of the class. */
14326 if (!decl_specifiers.any_specifiers_p)
14328 cp_token *token = cp_lexer_peek_token (parser->lexer);
14329 if (pedantic && !token->in_system_header)
14330 pedwarn ("%Hextra %<;%>", &token->location);
14336 /* See if this declaration is a friend. */
14337 friend_p = cp_parser_friend_p (&decl_specifiers);
14338 /* If there were decl-specifiers, check to see if there was
14339 a class-declaration. */
14340 type = check_tag_decl (&decl_specifiers);
14341 /* Nested classes have already been added to the class, but
14342 a `friend' needs to be explicitly registered. */
14345 /* If the `friend' keyword was present, the friend must
14346 be introduced with a class-key. */
14347 if (!declares_class_or_enum)
14348 error ("a class-key must be used when declaring a friend");
14351 template <typename T> struct A {
14352 friend struct A<T>::B;
14355 A<T>::B will be represented by a TYPENAME_TYPE, and
14356 therefore not recognized by check_tag_decl. */
14358 && decl_specifiers.type
14359 && TYPE_P (decl_specifiers.type))
14360 type = decl_specifiers.type;
14361 if (!type || !TYPE_P (type))
14362 error ("friend declaration does not name a class or "
14365 make_friend_class (current_class_type, type,
14366 /*complain=*/true);
14368 /* If there is no TYPE, an error message will already have
14370 else if (!type || type == error_mark_node)
14372 /* An anonymous aggregate has to be handled specially; such
14373 a declaration really declares a data member (with a
14374 particular type), as opposed to a nested class. */
14375 else if (ANON_AGGR_TYPE_P (type))
14377 /* Remove constructors and such from TYPE, now that we
14378 know it is an anonymous aggregate. */
14379 fixup_anonymous_aggr (type);
14380 /* And make the corresponding data member. */
14381 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14382 /* Add it to the class. */
14383 finish_member_declaration (decl);
14386 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14391 /* See if these declarations will be friends. */
14392 friend_p = cp_parser_friend_p (&decl_specifiers);
14394 /* Keep going until we hit the `;' at the end of the
14396 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14398 tree attributes = NULL_TREE;
14399 tree first_attribute;
14401 /* Peek at the next token. */
14402 token = cp_lexer_peek_token (parser->lexer);
14404 /* Check for a bitfield declaration. */
14405 if (token->type == CPP_COLON
14406 || (token->type == CPP_NAME
14407 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14413 /* Get the name of the bitfield. Note that we cannot just
14414 check TOKEN here because it may have been invalidated by
14415 the call to cp_lexer_peek_nth_token above. */
14416 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14417 identifier = cp_parser_identifier (parser);
14419 identifier = NULL_TREE;
14421 /* Consume the `:' token. */
14422 cp_lexer_consume_token (parser->lexer);
14423 /* Get the width of the bitfield. */
14425 = cp_parser_constant_expression (parser,
14426 /*allow_non_constant=*/false,
14429 /* Look for attributes that apply to the bitfield. */
14430 attributes = cp_parser_attributes_opt (parser);
14431 /* Remember which attributes are prefix attributes and
14433 first_attribute = attributes;
14434 /* Combine the attributes. */
14435 attributes = chainon (prefix_attributes, attributes);
14437 /* Create the bitfield declaration. */
14438 decl = grokbitfield (identifier
14439 ? make_id_declarator (NULL_TREE,
14445 /* Apply the attributes. */
14446 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14450 cp_declarator *declarator;
14452 tree asm_specification;
14453 int ctor_dtor_or_conv_p;
14455 /* Parse the declarator. */
14457 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14458 &ctor_dtor_or_conv_p,
14459 /*parenthesized_p=*/NULL,
14460 /*member_p=*/true);
14462 /* If something went wrong parsing the declarator, make sure
14463 that we at least consume some tokens. */
14464 if (declarator == cp_error_declarator)
14466 /* Skip to the end of the statement. */
14467 cp_parser_skip_to_end_of_statement (parser);
14468 /* If the next token is not a semicolon, that is
14469 probably because we just skipped over the body of
14470 a function. So, we consume a semicolon if
14471 present, but do not issue an error message if it
14473 if (cp_lexer_next_token_is (parser->lexer,
14475 cp_lexer_consume_token (parser->lexer);
14479 if (declares_class_or_enum & 2)
14480 cp_parser_check_for_definition_in_return_type
14481 (declarator, decl_specifiers.type);
14483 /* Look for an asm-specification. */
14484 asm_specification = cp_parser_asm_specification_opt (parser);
14485 /* Look for attributes that apply to the declaration. */
14486 attributes = cp_parser_attributes_opt (parser);
14487 /* Remember which attributes are prefix attributes and
14489 first_attribute = attributes;
14490 /* Combine the attributes. */
14491 attributes = chainon (prefix_attributes, attributes);
14493 /* If it's an `=', then we have a constant-initializer or a
14494 pure-specifier. It is not correct to parse the
14495 initializer before registering the member declaration
14496 since the member declaration should be in scope while
14497 its initializer is processed. However, the rest of the
14498 front end does not yet provide an interface that allows
14499 us to handle this correctly. */
14500 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14504 A pure-specifier shall be used only in the declaration of
14505 a virtual function.
14507 A member-declarator can contain a constant-initializer
14508 only if it declares a static member of integral or
14511 Therefore, if the DECLARATOR is for a function, we look
14512 for a pure-specifier; otherwise, we look for a
14513 constant-initializer. When we call `grokfield', it will
14514 perform more stringent semantics checks. */
14515 if (function_declarator_p (declarator))
14516 initializer = cp_parser_pure_specifier (parser);
14518 /* Parse the initializer. */
14519 initializer = cp_parser_constant_initializer (parser);
14521 /* Otherwise, there is no initializer. */
14523 initializer = NULL_TREE;
14525 /* See if we are probably looking at a function
14526 definition. We are certainly not looking at a
14527 member-declarator. Calling `grokfield' has
14528 side-effects, so we must not do it unless we are sure
14529 that we are looking at a member-declarator. */
14530 if (cp_parser_token_starts_function_definition_p
14531 (cp_lexer_peek_token (parser->lexer)))
14533 /* The grammar does not allow a pure-specifier to be
14534 used when a member function is defined. (It is
14535 possible that this fact is an oversight in the
14536 standard, since a pure function may be defined
14537 outside of the class-specifier. */
14539 error ("pure-specifier on function-definition");
14540 decl = cp_parser_save_member_function_body (parser,
14544 /* If the member was not a friend, declare it here. */
14546 finish_member_declaration (decl);
14547 /* Peek at the next token. */
14548 token = cp_lexer_peek_token (parser->lexer);
14549 /* If the next token is a semicolon, consume it. */
14550 if (token->type == CPP_SEMICOLON)
14551 cp_lexer_consume_token (parser->lexer);
14555 /* Create the declaration. */
14556 decl = grokfield (declarator, &decl_specifiers,
14557 initializer, /*init_const_expr_p=*/true,
14562 /* Reset PREFIX_ATTRIBUTES. */
14563 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14564 attributes = TREE_CHAIN (attributes);
14566 TREE_CHAIN (attributes) = NULL_TREE;
14568 /* If there is any qualification still in effect, clear it
14569 now; we will be starting fresh with the next declarator. */
14570 parser->scope = NULL_TREE;
14571 parser->qualifying_scope = NULL_TREE;
14572 parser->object_scope = NULL_TREE;
14573 /* If it's a `,', then there are more declarators. */
14574 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14575 cp_lexer_consume_token (parser->lexer);
14576 /* If the next token isn't a `;', then we have a parse error. */
14577 else if (cp_lexer_next_token_is_not (parser->lexer,
14580 cp_parser_error (parser, "expected %<;%>");
14581 /* Skip tokens until we find a `;'. */
14582 cp_parser_skip_to_end_of_statement (parser);
14589 /* Add DECL to the list of members. */
14591 finish_member_declaration (decl);
14593 if (TREE_CODE (decl) == FUNCTION_DECL)
14594 cp_parser_save_default_args (parser, decl);
14599 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14602 /* Parse a pure-specifier.
14607 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14608 Otherwise, ERROR_MARK_NODE is returned. */
14611 cp_parser_pure_specifier (cp_parser* parser)
14615 /* Look for the `=' token. */
14616 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14617 return error_mark_node;
14618 /* Look for the `0' token. */
14619 token = cp_lexer_consume_token (parser->lexer);
14620 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14621 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14623 cp_parser_error (parser,
14624 "invalid pure specifier (only `= 0' is allowed)");
14625 cp_parser_skip_to_end_of_statement (parser);
14626 return error_mark_node;
14628 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14630 error ("templates may not be %<virtual%>");
14631 return error_mark_node;
14634 return integer_zero_node;
14637 /* Parse a constant-initializer.
14639 constant-initializer:
14640 = constant-expression
14642 Returns a representation of the constant-expression. */
14645 cp_parser_constant_initializer (cp_parser* parser)
14647 /* Look for the `=' token. */
14648 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14649 return error_mark_node;
14651 /* It is invalid to write:
14653 struct S { static const int i = { 7 }; };
14656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14658 cp_parser_error (parser,
14659 "a brace-enclosed initializer is not allowed here");
14660 /* Consume the opening brace. */
14661 cp_lexer_consume_token (parser->lexer);
14662 /* Skip the initializer. */
14663 cp_parser_skip_to_closing_brace (parser);
14664 /* Look for the trailing `}'. */
14665 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14667 return error_mark_node;
14670 return cp_parser_constant_expression (parser,
14671 /*allow_non_constant=*/false,
14675 /* Derived classes [gram.class.derived] */
14677 /* Parse a base-clause.
14680 : base-specifier-list
14682 base-specifier-list:
14684 base-specifier-list , base-specifier
14686 Returns a TREE_LIST representing the base-classes, in the order in
14687 which they were declared. The representation of each node is as
14688 described by cp_parser_base_specifier.
14690 In the case that no bases are specified, this function will return
14691 NULL_TREE, not ERROR_MARK_NODE. */
14694 cp_parser_base_clause (cp_parser* parser)
14696 tree bases = NULL_TREE;
14698 /* Look for the `:' that begins the list. */
14699 cp_parser_require (parser, CPP_COLON, "`:'");
14701 /* Scan the base-specifier-list. */
14707 /* Look for the base-specifier. */
14708 base = cp_parser_base_specifier (parser);
14709 /* Add BASE to the front of the list. */
14710 if (base != error_mark_node)
14712 TREE_CHAIN (base) = bases;
14715 /* Peek at the next token. */
14716 token = cp_lexer_peek_token (parser->lexer);
14717 /* If it's not a comma, then the list is complete. */
14718 if (token->type != CPP_COMMA)
14720 /* Consume the `,'. */
14721 cp_lexer_consume_token (parser->lexer);
14724 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14725 base class had a qualified name. However, the next name that
14726 appears is certainly not qualified. */
14727 parser->scope = NULL_TREE;
14728 parser->qualifying_scope = NULL_TREE;
14729 parser->object_scope = NULL_TREE;
14731 return nreverse (bases);
14734 /* Parse a base-specifier.
14737 :: [opt] nested-name-specifier [opt] class-name
14738 virtual access-specifier [opt] :: [opt] nested-name-specifier
14740 access-specifier virtual [opt] :: [opt] nested-name-specifier
14743 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14744 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14745 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14746 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14749 cp_parser_base_specifier (cp_parser* parser)
14753 bool virtual_p = false;
14754 bool duplicate_virtual_error_issued_p = false;
14755 bool duplicate_access_error_issued_p = false;
14756 bool class_scope_p, template_p;
14757 tree access = access_default_node;
14760 /* Process the optional `virtual' and `access-specifier'. */
14763 /* Peek at the next token. */
14764 token = cp_lexer_peek_token (parser->lexer);
14765 /* Process `virtual'. */
14766 switch (token->keyword)
14769 /* If `virtual' appears more than once, issue an error. */
14770 if (virtual_p && !duplicate_virtual_error_issued_p)
14772 cp_parser_error (parser,
14773 "%<virtual%> specified more than once in base-specified");
14774 duplicate_virtual_error_issued_p = true;
14779 /* Consume the `virtual' token. */
14780 cp_lexer_consume_token (parser->lexer);
14785 case RID_PROTECTED:
14787 /* If more than one access specifier appears, issue an
14789 if (access != access_default_node
14790 && !duplicate_access_error_issued_p)
14792 cp_parser_error (parser,
14793 "more than one access specifier in base-specified");
14794 duplicate_access_error_issued_p = true;
14797 access = ridpointers[(int) token->keyword];
14799 /* Consume the access-specifier. */
14800 cp_lexer_consume_token (parser->lexer);
14809 /* It is not uncommon to see programs mechanically, erroneously, use
14810 the 'typename' keyword to denote (dependent) qualified types
14811 as base classes. */
14812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14814 if (!processing_template_decl)
14815 error ("keyword %<typename%> not allowed outside of templates");
14817 error ("keyword %<typename%> not allowed in this context "
14818 "(the base class is implicitly a type)");
14819 cp_lexer_consume_token (parser->lexer);
14822 /* Look for the optional `::' operator. */
14823 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14824 /* Look for the nested-name-specifier. The simplest way to
14829 The keyword `typename' is not permitted in a base-specifier or
14830 mem-initializer; in these contexts a qualified name that
14831 depends on a template-parameter is implicitly assumed to be a
14834 is to pretend that we have seen the `typename' keyword at this
14836 cp_parser_nested_name_specifier_opt (parser,
14837 /*typename_keyword_p=*/true,
14838 /*check_dependency_p=*/true,
14840 /*is_declaration=*/true);
14841 /* If the base class is given by a qualified name, assume that names
14842 we see are type names or templates, as appropriate. */
14843 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14844 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14846 /* Finally, look for the class-name. */
14847 type = cp_parser_class_name (parser,
14851 /*check_dependency_p=*/true,
14852 /*class_head_p=*/false,
14853 /*is_declaration=*/true);
14855 if (type == error_mark_node)
14856 return error_mark_node;
14858 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14861 /* Exception handling [gram.exception] */
14863 /* Parse an (optional) exception-specification.
14865 exception-specification:
14866 throw ( type-id-list [opt] )
14868 Returns a TREE_LIST representing the exception-specification. The
14869 TREE_VALUE of each node is a type. */
14872 cp_parser_exception_specification_opt (cp_parser* parser)
14877 /* Peek at the next token. */
14878 token = cp_lexer_peek_token (parser->lexer);
14879 /* If it's not `throw', then there's no exception-specification. */
14880 if (!cp_parser_is_keyword (token, RID_THROW))
14883 /* Consume the `throw'. */
14884 cp_lexer_consume_token (parser->lexer);
14886 /* Look for the `('. */
14887 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14889 /* Peek at the next token. */
14890 token = cp_lexer_peek_token (parser->lexer);
14891 /* If it's not a `)', then there is a type-id-list. */
14892 if (token->type != CPP_CLOSE_PAREN)
14894 const char *saved_message;
14896 /* Types may not be defined in an exception-specification. */
14897 saved_message = parser->type_definition_forbidden_message;
14898 parser->type_definition_forbidden_message
14899 = "types may not be defined in an exception-specification";
14900 /* Parse the type-id-list. */
14901 type_id_list = cp_parser_type_id_list (parser);
14902 /* Restore the saved message. */
14903 parser->type_definition_forbidden_message = saved_message;
14906 type_id_list = empty_except_spec;
14908 /* Look for the `)'. */
14909 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14911 return type_id_list;
14914 /* Parse an (optional) type-id-list.
14918 type-id-list , type-id
14920 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14921 in the order that the types were presented. */
14924 cp_parser_type_id_list (cp_parser* parser)
14926 tree types = NULL_TREE;
14933 /* Get the next type-id. */
14934 type = cp_parser_type_id (parser);
14935 /* Add it to the list. */
14936 types = add_exception_specifier (types, type, /*complain=*/1);
14937 /* Peek at the next token. */
14938 token = cp_lexer_peek_token (parser->lexer);
14939 /* If it is not a `,', we are done. */
14940 if (token->type != CPP_COMMA)
14942 /* Consume the `,'. */
14943 cp_lexer_consume_token (parser->lexer);
14946 return nreverse (types);
14949 /* Parse a try-block.
14952 try compound-statement handler-seq */
14955 cp_parser_try_block (cp_parser* parser)
14959 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14960 try_block = begin_try_block ();
14961 /* APPLE LOCAL radar 5982990 */
14962 cp_parser_compound_statement (parser, NULL, true, false);
14963 finish_try_block (try_block);
14964 cp_parser_handler_seq (parser);
14965 finish_handler_sequence (try_block);
14970 /* Parse a function-try-block.
14972 function-try-block:
14973 try ctor-initializer [opt] function-body handler-seq */
14976 cp_parser_function_try_block (cp_parser* parser)
14978 tree compound_stmt;
14980 bool ctor_initializer_p;
14982 /* Look for the `try' keyword. */
14983 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14985 /* Let the rest of the front-end know where we are. */
14986 try_block = begin_function_try_block (&compound_stmt);
14987 /* Parse the function-body. */
14989 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14990 /* We're done with the `try' part. */
14991 finish_function_try_block (try_block);
14992 /* Parse the handlers. */
14993 cp_parser_handler_seq (parser);
14994 /* We're done with the handlers. */
14995 finish_function_handler_sequence (try_block, compound_stmt);
14997 return ctor_initializer_p;
15000 /* Parse a handler-seq.
15003 handler handler-seq [opt] */
15006 cp_parser_handler_seq (cp_parser* parser)
15012 /* Parse the handler. */
15013 cp_parser_handler (parser);
15014 /* Peek at the next token. */
15015 token = cp_lexer_peek_token (parser->lexer);
15016 /* If it's not `catch' then there are no more handlers. */
15017 if (!cp_parser_is_keyword (token, RID_CATCH))
15022 /* Parse a handler.
15025 catch ( exception-declaration ) compound-statement */
15028 cp_parser_handler (cp_parser* parser)
15033 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15034 handler = begin_handler ();
15035 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15036 declaration = cp_parser_exception_declaration (parser);
15037 finish_handler_parms (declaration, handler);
15038 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15039 /* APPLE LOCAL radar 5982990 */
15040 cp_parser_compound_statement (parser, NULL, false, false);
15041 finish_handler (handler);
15044 /* Parse an exception-declaration.
15046 exception-declaration:
15047 type-specifier-seq declarator
15048 type-specifier-seq abstract-declarator
15052 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15053 ellipsis variant is used. */
15056 cp_parser_exception_declaration (cp_parser* parser)
15058 cp_decl_specifier_seq type_specifiers;
15059 cp_declarator *declarator;
15060 const char *saved_message;
15062 /* If it's an ellipsis, it's easy to handle. */
15063 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15065 /* Consume the `...' token. */
15066 cp_lexer_consume_token (parser->lexer);
15070 /* Types may not be defined in exception-declarations. */
15071 saved_message = parser->type_definition_forbidden_message;
15072 parser->type_definition_forbidden_message
15073 = "types may not be defined in exception-declarations";
15075 /* Parse the type-specifier-seq. */
15076 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15078 /* If it's a `)', then there is no declarator. */
15079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15082 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15083 /*ctor_dtor_or_conv_p=*/NULL,
15084 /*parenthesized_p=*/NULL,
15085 /*member_p=*/false);
15087 /* Restore the saved message. */
15088 parser->type_definition_forbidden_message = saved_message;
15090 if (!type_specifiers.any_specifiers_p)
15091 return error_mark_node;
15093 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15096 /* Parse a throw-expression.
15099 throw assignment-expression [opt]
15101 Returns a THROW_EXPR representing the throw-expression. */
15104 cp_parser_throw_expression (cp_parser* parser)
15109 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15110 token = cp_lexer_peek_token (parser->lexer);
15111 /* Figure out whether or not there is an assignment-expression
15112 following the "throw" keyword. */
15113 if (token->type == CPP_COMMA
15114 || token->type == CPP_SEMICOLON
15115 || token->type == CPP_CLOSE_PAREN
15116 || token->type == CPP_CLOSE_SQUARE
15117 || token->type == CPP_CLOSE_BRACE
15118 || token->type == CPP_COLON)
15119 expression = NULL_TREE;
15121 expression = cp_parser_assignment_expression (parser,
15124 return build_throw (expression);
15127 /* GNU Extensions */
15129 /* Parse an (optional) asm-specification.
15132 asm ( string-literal )
15134 If the asm-specification is present, returns a STRING_CST
15135 corresponding to the string-literal. Otherwise, returns
15139 cp_parser_asm_specification_opt (cp_parser* parser)
15142 tree asm_specification;
15144 /* Peek at the next token. */
15145 token = cp_lexer_peek_token (parser->lexer);
15146 /* If the next token isn't the `asm' keyword, then there's no
15147 asm-specification. */
15148 if (!cp_parser_is_keyword (token, RID_ASM))
15151 /* Consume the `asm' token. */
15152 cp_lexer_consume_token (parser->lexer);
15153 /* Look for the `('. */
15154 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15156 /* Look for the string-literal. */
15157 asm_specification = cp_parser_string_literal (parser, false, false);
15159 /* Look for the `)'. */
15160 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15162 return asm_specification;
15165 /* Parse an asm-operand-list.
15169 asm-operand-list , asm-operand
15172 string-literal ( expression )
15173 [ string-literal ] string-literal ( expression )
15175 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15176 each node is the expression. The TREE_PURPOSE is itself a
15177 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15178 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15179 is a STRING_CST for the string literal before the parenthesis. */
15182 cp_parser_asm_operand_list (cp_parser* parser)
15184 tree asm_operands = NULL_TREE;
15188 tree string_literal;
15192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15194 /* Consume the `[' token. */
15195 cp_lexer_consume_token (parser->lexer);
15196 /* Read the operand name. */
15197 name = cp_parser_identifier (parser);
15198 if (name != error_mark_node)
15199 name = build_string (IDENTIFIER_LENGTH (name),
15200 IDENTIFIER_POINTER (name));
15201 /* Look for the closing `]'. */
15202 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15206 /* Look for the string-literal. */
15207 string_literal = cp_parser_string_literal (parser, false, false);
15209 /* Look for the `('. */
15210 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15211 /* Parse the expression. */
15212 expression = cp_parser_expression (parser, /*cast_p=*/false);
15213 /* Look for the `)'. */
15214 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15216 /* Add this operand to the list. */
15217 asm_operands = tree_cons (build_tree_list (name, string_literal),
15220 /* If the next token is not a `,', there are no more
15222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15224 /* Consume the `,'. */
15225 cp_lexer_consume_token (parser->lexer);
15228 return nreverse (asm_operands);
15231 /* Parse an asm-clobber-list.
15235 asm-clobber-list , string-literal
15237 Returns a TREE_LIST, indicating the clobbers in the order that they
15238 appeared. The TREE_VALUE of each node is a STRING_CST. */
15241 cp_parser_asm_clobber_list (cp_parser* parser)
15243 tree clobbers = NULL_TREE;
15247 tree string_literal;
15249 /* Look for the string literal. */
15250 string_literal = cp_parser_string_literal (parser, false, false);
15251 /* Add it to the list. */
15252 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15253 /* If the next token is not a `,', then the list is
15255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15257 /* Consume the `,' token. */
15258 cp_lexer_consume_token (parser->lexer);
15264 /* Parse an (optional) series of attributes.
15267 attributes attribute
15270 __attribute__ (( attribute-list [opt] ))
15272 The return value is as for cp_parser_attribute_list. */
15275 cp_parser_attributes_opt (cp_parser* parser)
15277 tree attributes = NULL_TREE;
15282 tree attribute_list;
15284 /* Peek at the next token. */
15285 token = cp_lexer_peek_token (parser->lexer);
15286 /* If it's not `__attribute__', then we're done. */
15287 if (token->keyword != RID_ATTRIBUTE)
15290 /* Consume the `__attribute__' keyword. */
15291 cp_lexer_consume_token (parser->lexer);
15292 /* Look for the two `(' tokens. */
15293 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15294 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15296 /* Peek at the next token. */
15297 token = cp_lexer_peek_token (parser->lexer);
15298 if (token->type != CPP_CLOSE_PAREN)
15299 /* Parse the attribute-list. */
15300 attribute_list = cp_parser_attribute_list (parser);
15302 /* If the next token is a `)', then there is no attribute
15304 attribute_list = NULL;
15306 /* Look for the two `)' tokens. */
15307 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15308 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15310 /* Add these new attributes to the list. */
15311 attributes = chainon (attributes, attribute_list);
15317 /* Parse an attribute-list.
15321 attribute-list , attribute
15325 identifier ( identifier )
15326 identifier ( identifier , expression-list )
15327 identifier ( expression-list )
15329 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15330 to an attribute. The TREE_PURPOSE of each node is the identifier
15331 indicating which attribute is in use. The TREE_VALUE represents
15332 the arguments, if any. */
15335 cp_parser_attribute_list (cp_parser* parser)
15337 tree attribute_list = NULL_TREE;
15338 bool save_translate_strings_p = parser->translate_strings_p;
15340 parser->translate_strings_p = false;
15347 /* Look for the identifier. We also allow keywords here; for
15348 example `__attribute__ ((const))' is legal. */
15349 token = cp_lexer_peek_token (parser->lexer);
15350 if (token->type == CPP_NAME
15351 || token->type == CPP_KEYWORD)
15353 tree arguments = NULL_TREE;
15355 /* Consume the token. */
15356 token = cp_lexer_consume_token (parser->lexer);
15358 /* Save away the identifier that indicates which attribute
15360 identifier = token->u.value;
15361 attribute = build_tree_list (identifier, NULL_TREE);
15363 /* Peek at the next token. */
15364 token = cp_lexer_peek_token (parser->lexer);
15365 /* If it's an `(', then parse the attribute arguments. */
15366 if (token->type == CPP_OPEN_PAREN)
15368 arguments = cp_parser_parenthesized_expression_list
15369 (parser, true, /*cast_p=*/false,
15370 /*non_constant_p=*/NULL);
15371 /* Save the arguments away. */
15372 TREE_VALUE (attribute) = arguments;
15375 if (arguments != error_mark_node)
15377 /* Add this attribute to the list. */
15378 TREE_CHAIN (attribute) = attribute_list;
15379 attribute_list = attribute;
15382 token = cp_lexer_peek_token (parser->lexer);
15384 /* Now, look for more attributes. If the next token isn't a
15385 `,', we're done. */
15386 if (token->type != CPP_COMMA)
15389 /* Consume the comma and keep going. */
15390 cp_lexer_consume_token (parser->lexer);
15392 parser->translate_strings_p = save_translate_strings_p;
15394 /* We built up the list in reverse order. */
15395 return nreverse (attribute_list);
15398 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15399 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15400 current value of the PEDANTIC flag, regardless of whether or not
15401 the `__extension__' keyword is present. The caller is responsible
15402 for restoring the value of the PEDANTIC flag. */
15405 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15407 /* Save the old value of the PEDANTIC flag. */
15408 *saved_pedantic = pedantic;
15410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15412 /* Consume the `__extension__' token. */
15413 cp_lexer_consume_token (parser->lexer);
15414 /* We're not being pedantic while the `__extension__' keyword is
15424 /* Parse a label declaration.
15427 __label__ label-declarator-seq ;
15429 label-declarator-seq:
15430 identifier , label-declarator-seq
15434 cp_parser_label_declaration (cp_parser* parser)
15436 /* Look for the `__label__' keyword. */
15437 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15443 /* Look for an identifier. */
15444 identifier = cp_parser_identifier (parser);
15445 /* If we failed, stop. */
15446 if (identifier == error_mark_node)
15448 /* Declare it as a label. */
15449 finish_label_decl (identifier);
15450 /* If the next token is a `;', stop. */
15451 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15453 /* Look for the `,' separating the label declarations. */
15454 cp_parser_require (parser, CPP_COMMA, "`,'");
15457 /* Look for the final `;'. */
15458 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15461 /* Support Functions */
15463 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15464 NAME should have one of the representations used for an
15465 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15466 is returned. If PARSER->SCOPE is a dependent type, then a
15467 SCOPE_REF is returned.
15469 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15470 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15471 was formed. Abstractly, such entities should not be passed to this
15472 function, because they do not need to be looked up, but it is
15473 simpler to check for this special case here, rather than at the
15476 In cases not explicitly covered above, this function returns a
15477 DECL, OVERLOAD, or baselink representing the result of the lookup.
15478 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15481 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15482 (e.g., "struct") that was used. In that case bindings that do not
15483 refer to types are ignored.
15485 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15488 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15491 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15494 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15495 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15496 NULL_TREE otherwise. */
15499 cp_parser_lookup_name (cp_parser *parser, tree name,
15500 enum tag_types tag_type,
15503 bool check_dependency,
15504 tree *ambiguous_decls)
15508 tree object_type = parser->context->object_type;
15510 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15511 flags |= LOOKUP_COMPLAIN;
15513 /* Assume that the lookup will be unambiguous. */
15514 if (ambiguous_decls)
15515 *ambiguous_decls = NULL_TREE;
15517 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15518 no longer valid. Note that if we are parsing tentatively, and
15519 the parse fails, OBJECT_TYPE will be automatically restored. */
15520 parser->context->object_type = NULL_TREE;
15522 if (name == error_mark_node)
15523 return error_mark_node;
15525 /* A template-id has already been resolved; there is no lookup to
15527 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15529 if (BASELINK_P (name))
15531 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15532 == TEMPLATE_ID_EXPR);
15536 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15537 it should already have been checked to make sure that the name
15538 used matches the type being destroyed. */
15539 if (TREE_CODE (name) == BIT_NOT_EXPR)
15543 /* Figure out to which type this destructor applies. */
15545 type = parser->scope;
15546 else if (object_type)
15547 type = object_type;
15549 type = current_class_type;
15550 /* If that's not a class type, there is no destructor. */
15551 if (!type || !CLASS_TYPE_P (type))
15552 return error_mark_node;
15553 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15554 lazily_declare_fn (sfk_destructor, type);
15555 if (!CLASSTYPE_DESTRUCTORS (type))
15556 return error_mark_node;
15557 /* If it was a class type, return the destructor. */
15558 return CLASSTYPE_DESTRUCTORS (type);
15561 /* By this point, the NAME should be an ordinary identifier. If
15562 the id-expression was a qualified name, the qualifying scope is
15563 stored in PARSER->SCOPE at this point. */
15564 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15566 /* Perform the lookup. */
15571 if (parser->scope == error_mark_node)
15572 return error_mark_node;
15574 /* If the SCOPE is dependent, the lookup must be deferred until
15575 the template is instantiated -- unless we are explicitly
15576 looking up names in uninstantiated templates. Even then, we
15577 cannot look up the name if the scope is not a class type; it
15578 might, for example, be a template type parameter. */
15579 dependent_p = (TYPE_P (parser->scope)
15580 && !(parser->in_declarator_p
15581 && currently_open_class (parser->scope))
15582 && dependent_type_p (parser->scope));
15583 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15590 /* The resolution to Core Issue 180 says that `struct
15591 A::B' should be considered a type-name, even if `A'
15593 type = make_typename_type (parser->scope, name, tag_type,
15594 /*complain=*/tf_error);
15595 decl = TYPE_NAME (type);
15597 else if (is_template
15598 && (cp_parser_next_token_ends_template_argument_p (parser)
15599 || cp_lexer_next_token_is (parser->lexer,
15601 decl = make_unbound_class_template (parser->scope,
15603 /*complain=*/tf_error);
15605 decl = build_qualified_name (/*type=*/NULL_TREE,
15606 parser->scope, name,
15611 tree pushed_scope = NULL_TREE;
15613 /* If PARSER->SCOPE is a dependent type, then it must be a
15614 class type, and we must not be checking dependencies;
15615 otherwise, we would have processed this lookup above. So
15616 that PARSER->SCOPE is not considered a dependent base by
15617 lookup_member, we must enter the scope here. */
15619 pushed_scope = push_scope (parser->scope);
15620 /* If the PARSER->SCOPE is a template specialization, it
15621 may be instantiated during name lookup. In that case,
15622 errors may be issued. Even if we rollback the current
15623 tentative parse, those errors are valid. */
15624 decl = lookup_qualified_name (parser->scope, name,
15625 tag_type != none_type,
15626 /*complain=*/true);
15628 pop_scope (pushed_scope);
15630 parser->qualifying_scope = parser->scope;
15631 parser->object_scope = NULL_TREE;
15633 else if (object_type)
15635 tree object_decl = NULL_TREE;
15636 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15637 OBJECT_TYPE is not a class. */
15638 if (CLASS_TYPE_P (object_type))
15639 /* If the OBJECT_TYPE is a template specialization, it may
15640 be instantiated during name lookup. In that case, errors
15641 may be issued. Even if we rollback the current tentative
15642 parse, those errors are valid. */
15643 object_decl = lookup_member (object_type,
15646 tag_type != none_type);
15647 /* Look it up in the enclosing context, too. */
15648 decl = lookup_name_real (name, tag_type != none_type,
15650 /*block_p=*/true, is_namespace, flags);
15651 parser->object_scope = object_type;
15652 parser->qualifying_scope = NULL_TREE;
15654 decl = object_decl;
15658 decl = lookup_name_real (name, tag_type != none_type,
15660 /*block_p=*/true, is_namespace, flags);
15661 parser->qualifying_scope = NULL_TREE;
15662 parser->object_scope = NULL_TREE;
15665 /* If the lookup failed, let our caller know. */
15666 if (!decl || decl == error_mark_node)
15667 return error_mark_node;
15669 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15670 if (TREE_CODE (decl) == TREE_LIST)
15672 if (ambiguous_decls)
15673 *ambiguous_decls = decl;
15674 /* The error message we have to print is too complicated for
15675 cp_parser_error, so we incorporate its actions directly. */
15676 if (!cp_parser_simulate_error (parser))
15678 error ("reference to %qD is ambiguous", name);
15679 print_candidates (decl);
15681 return error_mark_node;
15684 gcc_assert (DECL_P (decl)
15685 || TREE_CODE (decl) == OVERLOAD
15686 || TREE_CODE (decl) == SCOPE_REF
15687 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15688 || BASELINK_P (decl));
15690 /* If we have resolved the name of a member declaration, check to
15691 see if the declaration is accessible. When the name resolves to
15692 set of overloaded functions, accessibility is checked when
15693 overload resolution is done.
15695 During an explicit instantiation, access is not checked at all,
15696 as per [temp.explicit]. */
15698 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15703 /* Like cp_parser_lookup_name, but for use in the typical case where
15704 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15705 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15708 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15710 return cp_parser_lookup_name (parser, name,
15712 /*is_template=*/false,
15713 /*is_namespace=*/false,
15714 /*check_dependency=*/true,
15715 /*ambiguous_decls=*/NULL);
15718 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15719 the current context, return the TYPE_DECL. If TAG_NAME_P is
15720 true, the DECL indicates the class being defined in a class-head,
15721 or declared in an elaborated-type-specifier.
15723 Otherwise, return DECL. */
15726 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15728 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15729 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15732 template <typename T> struct B;
15735 template <typename T> struct A::B {};
15737 Similarly, in an elaborated-type-specifier:
15739 namespace N { struct X{}; }
15742 template <typename T> friend struct N::X;
15745 However, if the DECL refers to a class type, and we are in
15746 the scope of the class, then the name lookup automatically
15747 finds the TYPE_DECL created by build_self_reference rather
15748 than a TEMPLATE_DECL. For example, in:
15750 template <class T> struct S {
15754 there is no need to handle such case. */
15756 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15757 return DECL_TEMPLATE_RESULT (decl);
15762 /* If too many, or too few, template-parameter lists apply to the
15763 declarator, issue an error message. Returns TRUE if all went well,
15764 and FALSE otherwise. */
15767 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15768 cp_declarator *declarator)
15770 unsigned num_templates;
15772 /* We haven't seen any classes that involve template parameters yet. */
15775 switch (declarator->kind)
15778 if (declarator->u.id.qualifying_scope)
15783 scope = declarator->u.id.qualifying_scope;
15784 member = declarator->u.id.unqualified_name;
15786 while (scope && CLASS_TYPE_P (scope))
15788 /* You're supposed to have one `template <...>'
15789 for every template class, but you don't need one
15790 for a full specialization. For example:
15792 template <class T> struct S{};
15793 template <> struct S<int> { void f(); };
15794 void S<int>::f () {}
15796 is correct; there shouldn't be a `template <>' for
15797 the definition of `S<int>::f'. */
15798 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15799 /* If SCOPE does not have template information of any
15800 kind, then it is not a template, nor is it nested
15801 within a template. */
15803 if (explicit_class_specialization_p (scope))
15805 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15808 scope = TYPE_CONTEXT (scope);
15811 else if (TREE_CODE (declarator->u.id.unqualified_name)
15812 == TEMPLATE_ID_EXPR)
15813 /* If the DECLARATOR has the form `X<y>' then it uses one
15814 additional level of template parameters. */
15817 return cp_parser_check_template_parameters (parser,
15823 case cdk_reference:
15825 /* APPLE LOCAL blocks 6040305 */
15826 case cdk_block_pointer:
15827 return (cp_parser_check_declarator_template_parameters
15828 (parser, declarator->declarator));
15834 gcc_unreachable ();
15839 /* NUM_TEMPLATES were used in the current declaration. If that is
15840 invalid, return FALSE and issue an error messages. Otherwise,
15844 cp_parser_check_template_parameters (cp_parser* parser,
15845 unsigned num_templates)
15847 /* If there are more template classes than parameter lists, we have
15850 template <class T> void S<T>::R<T>::f (); */
15851 if (parser->num_template_parameter_lists < num_templates)
15853 error ("too few template-parameter-lists");
15856 /* If there are the same number of template classes and parameter
15857 lists, that's OK. */
15858 if (parser->num_template_parameter_lists == num_templates)
15860 /* If there are more, but only one more, then we are referring to a
15861 member template. That's OK too. */
15862 if (parser->num_template_parameter_lists == num_templates + 1)
15864 /* Otherwise, there are too many template parameter lists. We have
15867 template <class T> template <class U> void S::f(); */
15868 error ("too many template-parameter-lists");
15872 /* Parse an optional `::' token indicating that the following name is
15873 from the global namespace. If so, PARSER->SCOPE is set to the
15874 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15875 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15876 Returns the new value of PARSER->SCOPE, if the `::' token is
15877 present, and NULL_TREE otherwise. */
15880 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15884 /* Peek at the next token. */
15885 token = cp_lexer_peek_token (parser->lexer);
15886 /* If we're looking at a `::' token then we're starting from the
15887 global namespace, not our current location. */
15888 if (token->type == CPP_SCOPE)
15890 /* Consume the `::' token. */
15891 cp_lexer_consume_token (parser->lexer);
15892 /* Set the SCOPE so that we know where to start the lookup. */
15893 parser->scope = global_namespace;
15894 parser->qualifying_scope = global_namespace;
15895 parser->object_scope = NULL_TREE;
15897 return parser->scope;
15899 else if (!current_scope_valid_p)
15901 parser->scope = NULL_TREE;
15902 parser->qualifying_scope = NULL_TREE;
15903 parser->object_scope = NULL_TREE;
15909 /* Returns TRUE if the upcoming token sequence is the start of a
15910 constructor declarator. If FRIEND_P is true, the declarator is
15911 preceded by the `friend' specifier. */
15914 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15916 bool constructor_p;
15917 tree type_decl = NULL_TREE;
15918 bool nested_name_p;
15919 cp_token *next_token;
15921 /* The common case is that this is not a constructor declarator, so
15922 try to avoid doing lots of work if at all possible. It's not
15923 valid declare a constructor at function scope. */
15924 if (parser->in_function_body)
15926 /* And only certain tokens can begin a constructor declarator. */
15927 next_token = cp_lexer_peek_token (parser->lexer);
15928 if (next_token->type != CPP_NAME
15929 && next_token->type != CPP_SCOPE
15930 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15931 && next_token->type != CPP_TEMPLATE_ID)
15934 /* Parse tentatively; we are going to roll back all of the tokens
15936 cp_parser_parse_tentatively (parser);
15937 /* Assume that we are looking at a constructor declarator. */
15938 constructor_p = true;
15940 /* Look for the optional `::' operator. */
15941 cp_parser_global_scope_opt (parser,
15942 /*current_scope_valid_p=*/false);
15943 /* Look for the nested-name-specifier. */
15945 = (cp_parser_nested_name_specifier_opt (parser,
15946 /*typename_keyword_p=*/false,
15947 /*check_dependency_p=*/false,
15949 /*is_declaration=*/false)
15951 /* Outside of a class-specifier, there must be a
15952 nested-name-specifier. */
15953 if (!nested_name_p &&
15954 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15956 constructor_p = false;
15957 /* If we still think that this might be a constructor-declarator,
15958 look for a class-name. */
15963 template <typename T> struct S { S(); };
15964 template <typename T> S<T>::S ();
15966 we must recognize that the nested `S' names a class.
15969 template <typename T> S<T>::S<T> ();
15971 we must recognize that the nested `S' names a template. */
15972 type_decl = cp_parser_class_name (parser,
15973 /*typename_keyword_p=*/false,
15974 /*template_keyword_p=*/false,
15976 /*check_dependency_p=*/false,
15977 /*class_head_p=*/false,
15978 /*is_declaration=*/false);
15979 /* If there was no class-name, then this is not a constructor. */
15980 constructor_p = !cp_parser_error_occurred (parser);
15983 /* If we're still considering a constructor, we have to see a `(',
15984 to begin the parameter-declaration-clause, followed by either a
15985 `)', an `...', or a decl-specifier. We need to check for a
15986 type-specifier to avoid being fooled into thinking that:
15990 is a constructor. (It is actually a function named `f' that
15991 takes one parameter (of type `int') and returns a value of type
15994 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15997 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15998 /* A parameter declaration begins with a decl-specifier,
15999 which is either the "attribute" keyword, a storage class
16000 specifier, or (usually) a type-specifier. */
16001 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16004 tree pushed_scope = NULL_TREE;
16005 unsigned saved_num_template_parameter_lists;
16007 /* Names appearing in the type-specifier should be looked up
16008 in the scope of the class. */
16009 if (current_class_type)
16013 type = TREE_TYPE (type_decl);
16014 if (TREE_CODE (type) == TYPENAME_TYPE)
16016 type = resolve_typename_type (type,
16017 /*only_current_p=*/false);
16018 if (type == error_mark_node)
16020 cp_parser_abort_tentative_parse (parser);
16024 pushed_scope = push_scope (type);
16027 /* Inside the constructor parameter list, surrounding
16028 template-parameter-lists do not apply. */
16029 saved_num_template_parameter_lists
16030 = parser->num_template_parameter_lists;
16031 parser->num_template_parameter_lists = 0;
16033 /* Look for the type-specifier. */
16034 cp_parser_type_specifier (parser,
16035 CP_PARSER_FLAGS_NONE,
16036 /*decl_specs=*/NULL,
16037 /*is_declarator=*/true,
16038 /*declares_class_or_enum=*/NULL,
16039 /*is_cv_qualifier=*/NULL);
16041 parser->num_template_parameter_lists
16042 = saved_num_template_parameter_lists;
16044 /* Leave the scope of the class. */
16046 pop_scope (pushed_scope);
16048 constructor_p = !cp_parser_error_occurred (parser);
16052 constructor_p = false;
16053 /* We did not really want to consume any tokens. */
16054 cp_parser_abort_tentative_parse (parser);
16056 return constructor_p;
16059 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16060 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16061 they must be performed once we are in the scope of the function.
16063 Returns the function defined. */
16066 cp_parser_function_definition_from_specifiers_and_declarator
16067 (cp_parser* parser,
16068 cp_decl_specifier_seq *decl_specifiers,
16070 const cp_declarator *declarator)
16075 /* Begin the function-definition. */
16076 success_p = start_function (decl_specifiers, declarator, attributes);
16078 /* The things we're about to see are not directly qualified by any
16079 template headers we've seen thus far. */
16080 reset_specialization ();
16082 /* If there were names looked up in the decl-specifier-seq that we
16083 did not check, check them now. We must wait until we are in the
16084 scope of the function to perform the checks, since the function
16085 might be a friend. */
16086 perform_deferred_access_checks ();
16090 /* Skip the entire function. */
16091 cp_parser_skip_to_end_of_block_or_statement (parser);
16092 fn = error_mark_node;
16094 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16096 /* Seen already, skip it. An error message has already been output. */
16097 cp_parser_skip_to_end_of_block_or_statement (parser);
16098 fn = current_function_decl;
16099 current_function_decl = NULL_TREE;
16100 /* If this is a function from a class, pop the nested class. */
16101 if (current_class_name)
16102 pop_nested_class ();
16105 fn = cp_parser_function_definition_after_declarator (parser,
16106 /*inline_p=*/false);
16111 /* Parse the part of a function-definition that follows the
16112 declarator. INLINE_P is TRUE iff this function is an inline
16113 function defined with a class-specifier.
16115 Returns the function defined. */
16118 cp_parser_function_definition_after_declarator (cp_parser* parser,
16122 bool ctor_initializer_p = false;
16123 bool saved_in_unbraced_linkage_specification_p;
16124 bool saved_in_function_body;
16125 unsigned saved_num_template_parameter_lists;
16127 saved_in_function_body = parser->in_function_body;
16128 parser->in_function_body = true;
16129 /* If the next token is `return', then the code may be trying to
16130 make use of the "named return value" extension that G++ used to
16132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16134 /* Consume the `return' keyword. */
16135 cp_lexer_consume_token (parser->lexer);
16136 /* Look for the identifier that indicates what value is to be
16138 cp_parser_identifier (parser);
16139 /* Issue an error message. */
16140 error ("named return values are no longer supported");
16141 /* Skip tokens until we reach the start of the function body. */
16144 cp_token *token = cp_lexer_peek_token (parser->lexer);
16145 if (token->type == CPP_OPEN_BRACE
16146 || token->type == CPP_EOF
16147 || token->type == CPP_PRAGMA_EOL)
16149 cp_lexer_consume_token (parser->lexer);
16152 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16153 anything declared inside `f'. */
16154 saved_in_unbraced_linkage_specification_p
16155 = parser->in_unbraced_linkage_specification_p;
16156 parser->in_unbraced_linkage_specification_p = false;
16157 /* Inside the function, surrounding template-parameter-lists do not
16159 saved_num_template_parameter_lists
16160 = parser->num_template_parameter_lists;
16161 parser->num_template_parameter_lists = 0;
16162 /* If the next token is `try', then we are looking at a
16163 function-try-block. */
16164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16165 ctor_initializer_p = cp_parser_function_try_block (parser);
16166 /* A function-try-block includes the function-body, so we only do
16167 this next part if we're not processing a function-try-block. */
16170 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16172 /* Finish the function. */
16173 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16174 (inline_p ? 2 : 0));
16175 /* Generate code for it, if necessary. */
16176 expand_or_defer_fn (fn);
16177 /* Restore the saved values. */
16178 parser->in_unbraced_linkage_specification_p
16179 = saved_in_unbraced_linkage_specification_p;
16180 parser->num_template_parameter_lists
16181 = saved_num_template_parameter_lists;
16182 parser->in_function_body = saved_in_function_body;
16187 /* Parse a template-declaration, assuming that the `export' (and
16188 `extern') keywords, if present, has already been scanned. MEMBER_P
16189 is as for cp_parser_template_declaration. */
16192 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16194 tree decl = NULL_TREE;
16195 VEC (deferred_access_check,gc) *checks;
16196 tree parameter_list;
16197 bool friend_p = false;
16198 bool need_lang_pop;
16200 /* Look for the `template' keyword. */
16201 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16205 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16207 if (at_class_scope_p () && current_function_decl)
16209 /* 14.5.2.2 [temp.mem]
16211 A local class shall not have member templates. */
16212 error ("invalid declaration of member template in local class");
16213 cp_parser_skip_to_end_of_block_or_statement (parser);
16218 A template ... shall not have C linkage. */
16219 if (current_lang_name == lang_name_c)
16221 error ("template with C linkage");
16222 /* Give it C++ linkage to avoid confusing other parts of the
16224 push_lang_context (lang_name_cplusplus);
16225 need_lang_pop = true;
16228 need_lang_pop = false;
16230 /* We cannot perform access checks on the template parameter
16231 declarations until we know what is being declared, just as we
16232 cannot check the decl-specifier list. */
16233 push_deferring_access_checks (dk_deferred);
16235 /* If the next token is `>', then we have an invalid
16236 specialization. Rather than complain about an invalid template
16237 parameter, issue an error message here. */
16238 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16240 cp_parser_error (parser, "invalid explicit specialization");
16241 begin_specialization ();
16242 parameter_list = NULL_TREE;
16245 /* Parse the template parameters. */
16246 parameter_list = cp_parser_template_parameter_list (parser);
16248 /* Get the deferred access checks from the parameter list. These
16249 will be checked once we know what is being declared, as for a
16250 member template the checks must be performed in the scope of the
16251 class containing the member. */
16252 checks = get_deferred_access_checks ();
16254 /* Look for the `>'. */
16255 cp_parser_skip_to_end_of_template_parameter_list (parser);
16256 /* We just processed one more parameter list. */
16257 ++parser->num_template_parameter_lists;
16258 /* If the next token is `template', there are more template
16260 if (cp_lexer_next_token_is_keyword (parser->lexer,
16262 cp_parser_template_declaration_after_export (parser, member_p);
16265 /* There are no access checks when parsing a template, as we do not
16266 know if a specialization will be a friend. */
16267 push_deferring_access_checks (dk_no_check);
16268 decl = cp_parser_single_declaration (parser,
16272 pop_deferring_access_checks ();
16274 /* If this is a member template declaration, let the front
16276 if (member_p && !friend_p && decl)
16278 if (TREE_CODE (decl) == TYPE_DECL)
16279 cp_parser_check_access_in_redeclaration (decl);
16281 decl = finish_member_template_decl (decl);
16283 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16284 make_friend_class (current_class_type, TREE_TYPE (decl),
16285 /*complain=*/true);
16287 /* We are done with the current parameter list. */
16288 --parser->num_template_parameter_lists;
16290 pop_deferring_access_checks ();
16293 finish_template_decl (parameter_list);
16295 /* Register member declarations. */
16296 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16297 finish_member_declaration (decl);
16298 /* For the erroneous case of a template with C linkage, we pushed an
16299 implicit C++ linkage scope; exit that scope now. */
16301 pop_lang_context ();
16302 /* If DECL is a function template, we must return to parse it later.
16303 (Even though there is no definition, there might be default
16304 arguments that need handling.) */
16305 if (member_p && decl
16306 && (TREE_CODE (decl) == FUNCTION_DECL
16307 || DECL_FUNCTION_TEMPLATE_P (decl)))
16308 TREE_VALUE (parser->unparsed_functions_queues)
16309 = tree_cons (NULL_TREE, decl,
16310 TREE_VALUE (parser->unparsed_functions_queues));
16313 /* Perform the deferred access checks from a template-parameter-list.
16314 CHECKS is a TREE_LIST of access checks, as returned by
16315 get_deferred_access_checks. */
16318 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16320 ++processing_template_parmlist;
16321 perform_access_checks (checks);
16322 --processing_template_parmlist;
16325 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16326 `function-definition' sequence. MEMBER_P is true, this declaration
16327 appears in a class scope.
16329 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16330 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16333 cp_parser_single_declaration (cp_parser* parser,
16334 VEC (deferred_access_check,gc)* checks,
16338 int declares_class_or_enum;
16339 tree decl = NULL_TREE;
16340 cp_decl_specifier_seq decl_specifiers;
16341 bool function_definition_p = false;
16343 /* This function is only used when processing a template
16345 gcc_assert (innermost_scope_kind () == sk_template_parms
16346 || innermost_scope_kind () == sk_template_spec);
16348 /* Defer access checks until we know what is being declared. */
16349 push_deferring_access_checks (dk_deferred);
16351 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16353 cp_parser_decl_specifier_seq (parser,
16354 CP_PARSER_FLAGS_OPTIONAL,
16356 &declares_class_or_enum);
16358 *friend_p = cp_parser_friend_p (&decl_specifiers);
16360 /* There are no template typedefs. */
16361 if (decl_specifiers.specs[(int) ds_typedef])
16363 error ("template declaration of %qs", "typedef");
16364 decl = error_mark_node;
16367 /* Gather up the access checks that occurred the
16368 decl-specifier-seq. */
16369 stop_deferring_access_checks ();
16371 /* Check for the declaration of a template class. */
16372 if (declares_class_or_enum)
16374 if (cp_parser_declares_only_class_p (parser))
16376 decl = shadow_tag (&decl_specifiers);
16381 friend template <typename T> struct A<T>::B;
16384 A<T>::B will be represented by a TYPENAME_TYPE, and
16385 therefore not recognized by shadow_tag. */
16386 if (friend_p && *friend_p
16388 && decl_specifiers.type
16389 && TYPE_P (decl_specifiers.type))
16390 decl = decl_specifiers.type;
16392 if (decl && decl != error_mark_node)
16393 decl = TYPE_NAME (decl);
16395 decl = error_mark_node;
16397 /* Perform access checks for template parameters. */
16398 cp_parser_perform_template_parameter_access_checks (checks);
16401 /* If it's not a template class, try for a template function. If
16402 the next token is a `;', then this declaration does not declare
16403 anything. But, if there were errors in the decl-specifiers, then
16404 the error might well have come from an attempted class-specifier.
16405 In that case, there's no need to warn about a missing declarator. */
16407 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16408 || decl_specifiers.type != error_mark_node))
16409 decl = cp_parser_init_declarator (parser,
16412 /*function_definition_allowed_p=*/true,
16414 declares_class_or_enum,
16415 &function_definition_p);
16417 pop_deferring_access_checks ();
16419 /* Clear any current qualification; whatever comes next is the start
16420 of something new. */
16421 parser->scope = NULL_TREE;
16422 parser->qualifying_scope = NULL_TREE;
16423 parser->object_scope = NULL_TREE;
16424 /* Look for a trailing `;' after the declaration. */
16425 if (!function_definition_p
16426 && (decl == error_mark_node
16427 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16428 cp_parser_skip_to_end_of_block_or_statement (parser);
16433 /* Parse a cast-expression that is not the operand of a unary "&". */
16436 cp_parser_simple_cast_expression (cp_parser *parser)
16438 return cp_parser_cast_expression (parser, /*address_p=*/false,
16442 /* Parse a functional cast to TYPE. Returns an expression
16443 representing the cast. */
16446 cp_parser_functional_cast (cp_parser* parser, tree type)
16448 tree expression_list;
16452 = cp_parser_parenthesized_expression_list (parser, false,
16454 /*non_constant_p=*/NULL);
16456 cast = build_functional_cast (type, expression_list);
16457 /* [expr.const]/1: In an integral constant expression "only type
16458 conversions to integral or enumeration type can be used". */
16459 if (TREE_CODE (type) == TYPE_DECL)
16460 type = TREE_TYPE (type);
16461 if (cast != error_mark_node
16462 && !cast_valid_in_integral_constant_expression_p (type)
16463 && (cp_parser_non_integral_constant_expression
16464 (parser, "a call to a constructor")))
16465 return error_mark_node;
16469 /* Save the tokens that make up the body of a member function defined
16470 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16471 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16472 specifiers applied to the declaration. Returns the FUNCTION_DECL
16473 for the member function. */
16476 cp_parser_save_member_function_body (cp_parser* parser,
16477 cp_decl_specifier_seq *decl_specifiers,
16478 cp_declarator *declarator,
16485 /* Create the function-declaration. */
16486 fn = start_method (decl_specifiers, declarator, attributes);
16487 /* If something went badly wrong, bail out now. */
16488 if (fn == error_mark_node)
16490 /* If there's a function-body, skip it. */
16491 if (cp_parser_token_starts_function_definition_p
16492 (cp_lexer_peek_token (parser->lexer)))
16493 cp_parser_skip_to_end_of_block_or_statement (parser);
16494 return error_mark_node;
16497 /* Remember it, if there default args to post process. */
16498 cp_parser_save_default_args (parser, fn);
16500 /* Save away the tokens that make up the body of the
16502 first = parser->lexer->next_token;
16503 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16504 /* Handle function try blocks. */
16505 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16506 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16507 last = parser->lexer->next_token;
16509 /* Save away the inline definition; we will process it when the
16510 class is complete. */
16511 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16512 DECL_PENDING_INLINE_P (fn) = 1;
16514 /* We need to know that this was defined in the class, so that
16515 friend templates are handled correctly. */
16516 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16518 /* We're done with the inline definition. */
16519 finish_method (fn);
16521 /* Add FN to the queue of functions to be parsed later. */
16522 TREE_VALUE (parser->unparsed_functions_queues)
16523 = tree_cons (NULL_TREE, fn,
16524 TREE_VALUE (parser->unparsed_functions_queues));
16529 /* Parse a template-argument-list, as well as the trailing ">" (but
16530 not the opening ">"). See cp_parser_template_argument_list for the
16534 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16538 tree saved_qualifying_scope;
16539 tree saved_object_scope;
16540 bool saved_greater_than_is_operator_p;
16541 bool saved_skip_evaluation;
16545 When parsing a template-id, the first non-nested `>' is taken as
16546 the end of the template-argument-list rather than a greater-than
16548 saved_greater_than_is_operator_p
16549 = parser->greater_than_is_operator_p;
16550 parser->greater_than_is_operator_p = false;
16551 /* Parsing the argument list may modify SCOPE, so we save it
16553 saved_scope = parser->scope;
16554 saved_qualifying_scope = parser->qualifying_scope;
16555 saved_object_scope = parser->object_scope;
16556 /* We need to evaluate the template arguments, even though this
16557 template-id may be nested within a "sizeof". */
16558 saved_skip_evaluation = skip_evaluation;
16559 skip_evaluation = false;
16560 /* Parse the template-argument-list itself. */
16561 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16562 arguments = NULL_TREE;
16564 arguments = cp_parser_template_argument_list (parser);
16565 /* Look for the `>' that ends the template-argument-list. If we find
16566 a '>>' instead, it's probably just a typo. */
16567 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16569 if (!saved_greater_than_is_operator_p)
16571 /* If we're in a nested template argument list, the '>>' has
16572 to be a typo for '> >'. We emit the error message, but we
16573 continue parsing and we push a '>' as next token, so that
16574 the argument list will be parsed correctly. Note that the
16575 global source location is still on the token before the
16576 '>>', so we need to say explicitly where we want it. */
16577 cp_token *token = cp_lexer_peek_token (parser->lexer);
16578 error ("%H%<>>%> should be %<> >%> "
16579 "within a nested template argument list",
16582 /* ??? Proper recovery should terminate two levels of
16583 template argument list here. */
16584 token->type = CPP_GREATER;
16588 /* If this is not a nested template argument list, the '>>'
16589 is a typo for '>'. Emit an error message and continue.
16590 Same deal about the token location, but here we can get it
16591 right by consuming the '>>' before issuing the diagnostic. */
16592 cp_lexer_consume_token (parser->lexer);
16593 error ("spurious %<>>%>, use %<>%> to terminate "
16594 "a template argument list");
16598 cp_parser_skip_to_end_of_template_parameter_list (parser);
16599 /* The `>' token might be a greater-than operator again now. */
16600 parser->greater_than_is_operator_p
16601 = saved_greater_than_is_operator_p;
16602 /* Restore the SAVED_SCOPE. */
16603 parser->scope = saved_scope;
16604 parser->qualifying_scope = saved_qualifying_scope;
16605 parser->object_scope = saved_object_scope;
16606 skip_evaluation = saved_skip_evaluation;
16611 /* MEMBER_FUNCTION is a member function, or a friend. If default
16612 arguments, or the body of the function have not yet been parsed,
16616 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16618 /* If this member is a template, get the underlying
16620 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16621 member_function = DECL_TEMPLATE_RESULT (member_function);
16623 /* There should not be any class definitions in progress at this
16624 point; the bodies of members are only parsed outside of all class
16626 gcc_assert (parser->num_classes_being_defined == 0);
16627 /* While we're parsing the member functions we might encounter more
16628 classes. We want to handle them right away, but we don't want
16629 them getting mixed up with functions that are currently in the
16631 parser->unparsed_functions_queues
16632 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16634 /* Make sure that any template parameters are in scope. */
16635 maybe_begin_member_template_processing (member_function);
16637 /* If the body of the function has not yet been parsed, parse it
16639 if (DECL_PENDING_INLINE_P (member_function))
16641 tree function_scope;
16642 cp_token_cache *tokens;
16644 /* The function is no longer pending; we are processing it. */
16645 tokens = DECL_PENDING_INLINE_INFO (member_function);
16646 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16647 DECL_PENDING_INLINE_P (member_function) = 0;
16649 /* If this is a local class, enter the scope of the containing
16651 function_scope = current_function_decl;
16652 if (function_scope)
16653 push_function_context_to (function_scope);
16656 /* Push the body of the function onto the lexer stack. */
16657 cp_parser_push_lexer_for_tokens (parser, tokens);
16659 /* Let the front end know that we going to be defining this
16661 start_preparsed_function (member_function, NULL_TREE,
16662 SF_PRE_PARSED | SF_INCLASS_INLINE);
16664 /* Don't do access checking if it is a templated function. */
16665 if (processing_template_decl)
16666 push_deferring_access_checks (dk_no_check);
16668 /* Now, parse the body of the function. */
16669 cp_parser_function_definition_after_declarator (parser,
16670 /*inline_p=*/true);
16672 if (processing_template_decl)
16673 pop_deferring_access_checks ();
16675 /* Leave the scope of the containing function. */
16676 if (function_scope)
16677 pop_function_context_from (function_scope);
16678 cp_parser_pop_lexer (parser);
16681 /* Remove any template parameters from the symbol table. */
16682 maybe_end_member_template_processing ();
16684 /* Restore the queue. */
16685 parser->unparsed_functions_queues
16686 = TREE_CHAIN (parser->unparsed_functions_queues);
16689 /* If DECL contains any default args, remember it on the unparsed
16690 functions queue. */
16693 cp_parser_save_default_args (cp_parser* parser, tree decl)
16697 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16699 probe = TREE_CHAIN (probe))
16700 if (TREE_PURPOSE (probe))
16702 TREE_PURPOSE (parser->unparsed_functions_queues)
16703 = tree_cons (current_class_type, decl,
16704 TREE_PURPOSE (parser->unparsed_functions_queues));
16709 /* FN is a FUNCTION_DECL which may contains a parameter with an
16710 unparsed DEFAULT_ARG. Parse the default args now. This function
16711 assumes that the current scope is the scope in which the default
16712 argument should be processed. */
16715 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16717 bool saved_local_variables_forbidden_p;
16720 /* While we're parsing the default args, we might (due to the
16721 statement expression extension) encounter more classes. We want
16722 to handle them right away, but we don't want them getting mixed
16723 up with default args that are currently in the queue. */
16724 parser->unparsed_functions_queues
16725 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16727 /* Local variable names (and the `this' keyword) may not appear
16728 in a default argument. */
16729 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16730 parser->local_variables_forbidden_p = true;
16732 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16734 parm = TREE_CHAIN (parm))
16736 cp_token_cache *tokens;
16737 tree default_arg = TREE_PURPOSE (parm);
16739 VEC(tree,gc) *insts;
16746 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16747 /* This can happen for a friend declaration for a function
16748 already declared with default arguments. */
16751 /* Push the saved tokens for the default argument onto the parser's
16753 tokens = DEFARG_TOKENS (default_arg);
16754 cp_parser_push_lexer_for_tokens (parser, tokens);
16756 /* Parse the assignment-expression. */
16757 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16759 if (!processing_template_decl)
16760 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16762 TREE_PURPOSE (parm) = parsed_arg;
16764 /* Update any instantiations we've already created. */
16765 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16766 VEC_iterate (tree, insts, ix, copy); ix++)
16767 TREE_PURPOSE (copy) = parsed_arg;
16769 /* If the token stream has not been completely used up, then
16770 there was extra junk after the end of the default
16772 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16773 cp_parser_error (parser, "expected %<,%>");
16775 /* Revert to the main lexer. */
16776 cp_parser_pop_lexer (parser);
16779 /* Make sure no default arg is missing. */
16780 check_default_args (fn);
16782 /* Restore the state of local_variables_forbidden_p. */
16783 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16785 /* Restore the queue. */
16786 parser->unparsed_functions_queues
16787 = TREE_CHAIN (parser->unparsed_functions_queues);
16790 /* Parse the operand of `sizeof' (or a similar operator). Returns
16791 either a TYPE or an expression, depending on the form of the
16792 input. The KEYWORD indicates which kind of expression we have
16796 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16798 static const char *format;
16799 tree expr = NULL_TREE;
16800 const char *saved_message;
16801 bool saved_integral_constant_expression_p;
16802 bool saved_non_integral_constant_expression_p;
16804 /* Initialize FORMAT the first time we get here. */
16806 format = "types may not be defined in '%s' expressions";
16808 /* Types cannot be defined in a `sizeof' expression. Save away the
16810 saved_message = parser->type_definition_forbidden_message;
16811 /* And create the new one. */
16812 parser->type_definition_forbidden_message
16813 = XNEWVEC (const char, strlen (format)
16814 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16816 sprintf ((char *) parser->type_definition_forbidden_message,
16817 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16819 /* The restrictions on constant-expressions do not apply inside
16820 sizeof expressions. */
16821 saved_integral_constant_expression_p
16822 = parser->integral_constant_expression_p;
16823 saved_non_integral_constant_expression_p
16824 = parser->non_integral_constant_expression_p;
16825 parser->integral_constant_expression_p = false;
16827 /* Do not actually evaluate the expression. */
16829 /* If it's a `(', then we might be looking at the type-id
16831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16834 bool saved_in_type_id_in_expr_p;
16836 /* We can't be sure yet whether we're looking at a type-id or an
16838 cp_parser_parse_tentatively (parser);
16839 /* Consume the `('. */
16840 cp_lexer_consume_token (parser->lexer);
16841 /* Parse the type-id. */
16842 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16843 parser->in_type_id_in_expr_p = true;
16844 type = cp_parser_type_id (parser);
16845 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16846 /* Now, look for the trailing `)'. */
16847 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16848 /* If all went well, then we're done. */
16849 if (cp_parser_parse_definitely (parser))
16851 cp_decl_specifier_seq decl_specs;
16853 /* Build a trivial decl-specifier-seq. */
16854 clear_decl_specs (&decl_specs);
16855 decl_specs.type = type;
16857 /* Call grokdeclarator to figure out what type this is. */
16858 expr = grokdeclarator (NULL,
16862 /*attrlist=*/NULL);
16866 /* If the type-id production did not work out, then we must be
16867 looking at the unary-expression production. */
16869 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16871 /* Go back to evaluating expressions. */
16874 /* Free the message we created. */
16875 free ((char *) parser->type_definition_forbidden_message);
16876 /* And restore the old one. */
16877 parser->type_definition_forbidden_message = saved_message;
16878 parser->integral_constant_expression_p
16879 = saved_integral_constant_expression_p;
16880 parser->non_integral_constant_expression_p
16881 = saved_non_integral_constant_expression_p;
16886 /* If the current declaration has no declarator, return true. */
16889 cp_parser_declares_only_class_p (cp_parser *parser)
16891 /* If the next token is a `;' or a `,' then there is no
16893 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16894 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16897 /* Update the DECL_SPECS to reflect the storage class indicated by
16901 cp_parser_set_storage_class (cp_parser *parser,
16902 cp_decl_specifier_seq *decl_specs,
16905 cp_storage_class storage_class;
16907 if (parser->in_unbraced_linkage_specification_p)
16909 error ("invalid use of %qD in linkage specification",
16910 ridpointers[keyword]);
16913 else if (decl_specs->storage_class != sc_none)
16915 decl_specs->conflicting_specifiers_p = true;
16919 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16920 && decl_specs->specs[(int) ds_thread])
16922 error ("%<__thread%> before %qD", ridpointers[keyword]);
16923 decl_specs->specs[(int) ds_thread] = 0;
16929 storage_class = sc_auto;
16932 storage_class = sc_register;
16935 storage_class = sc_static;
16938 storage_class = sc_extern;
16941 storage_class = sc_mutable;
16944 gcc_unreachable ();
16946 decl_specs->storage_class = storage_class;
16948 /* A storage class specifier cannot be applied alongside a typedef
16949 specifier. If there is a typedef specifier present then set
16950 conflicting_specifiers_p which will trigger an error later
16951 on in grokdeclarator. */
16952 if (decl_specs->specs[(int)ds_typedef])
16953 decl_specs->conflicting_specifiers_p = true;
16956 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16957 is true, the type is a user-defined type; otherwise it is a
16958 built-in type specified by a keyword. */
16961 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16963 bool user_defined_p)
16965 decl_specs->any_specifiers_p = true;
16967 /* If the user tries to redeclare bool or wchar_t (with, for
16968 example, in "typedef int wchar_t;") we remember that this is what
16969 happened. In system headers, we ignore these declarations so
16970 that G++ can work with system headers that are not C++-safe. */
16971 if (decl_specs->specs[(int) ds_typedef]
16973 && (type_spec == boolean_type_node
16974 || type_spec == wchar_type_node)
16975 && (decl_specs->type
16976 || decl_specs->specs[(int) ds_long]
16977 || decl_specs->specs[(int) ds_short]
16978 || decl_specs->specs[(int) ds_unsigned]
16979 || decl_specs->specs[(int) ds_signed]))
16981 decl_specs->redefined_builtin_type = type_spec;
16982 if (!decl_specs->type)
16984 decl_specs->type = type_spec;
16985 decl_specs->user_defined_type_p = false;
16988 else if (decl_specs->type)
16989 decl_specs->multiple_types_p = true;
16992 decl_specs->type = type_spec;
16993 decl_specs->user_defined_type_p = user_defined_p;
16994 decl_specs->redefined_builtin_type = NULL_TREE;
16998 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16999 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17002 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17004 return decl_specifiers->specs[(int) ds_friend] != 0;
17007 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17008 issue an error message indicating that TOKEN_DESC was expected.
17010 Returns the token consumed, if the token had the appropriate type.
17011 Otherwise, returns NULL. */
17014 cp_parser_require (cp_parser* parser,
17015 enum cpp_ttype type,
17016 const char* token_desc)
17018 if (cp_lexer_next_token_is (parser->lexer, type))
17019 return cp_lexer_consume_token (parser->lexer);
17022 /* Output the MESSAGE -- unless we're parsing tentatively. */
17023 if (!cp_parser_simulate_error (parser))
17025 char *message = concat ("expected ", token_desc, NULL);
17026 cp_parser_error (parser, message);
17033 /* An error message is produced if the next token is not '>'.
17034 All further tokens are skipped until the desired token is
17035 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17038 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17040 /* Current level of '< ... >'. */
17041 unsigned level = 0;
17042 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17043 unsigned nesting_depth = 0;
17045 /* Are we ready, yet? If not, issue error message. */
17046 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17049 /* Skip tokens until the desired token is found. */
17052 /* Peek at the next token. */
17053 switch (cp_lexer_peek_token (parser->lexer)->type)
17056 if (!nesting_depth)
17061 if (!nesting_depth && level-- == 0)
17063 /* We've reached the token we want, consume it and stop. */
17064 cp_lexer_consume_token (parser->lexer);
17069 case CPP_OPEN_PAREN:
17070 case CPP_OPEN_SQUARE:
17074 case CPP_CLOSE_PAREN:
17075 case CPP_CLOSE_SQUARE:
17076 if (nesting_depth-- == 0)
17081 case CPP_PRAGMA_EOL:
17082 case CPP_SEMICOLON:
17083 case CPP_OPEN_BRACE:
17084 case CPP_CLOSE_BRACE:
17085 /* The '>' was probably forgotten, don't look further. */
17092 /* Consume this token. */
17093 cp_lexer_consume_token (parser->lexer);
17097 /* If the next token is the indicated keyword, consume it. Otherwise,
17098 issue an error message indicating that TOKEN_DESC was expected.
17100 Returns the token consumed, if the token had the appropriate type.
17101 Otherwise, returns NULL. */
17104 cp_parser_require_keyword (cp_parser* parser,
17106 const char* token_desc)
17108 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17110 if (token && token->keyword != keyword)
17112 dyn_string_t error_msg;
17114 /* Format the error message. */
17115 error_msg = dyn_string_new (0);
17116 dyn_string_append_cstr (error_msg, "expected ");
17117 dyn_string_append_cstr (error_msg, token_desc);
17118 cp_parser_error (parser, error_msg->s);
17119 dyn_string_delete (error_msg);
17126 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17127 function-definition. */
17130 cp_parser_token_starts_function_definition_p (cp_token* token)
17132 return (/* An ordinary function-body begins with an `{'. */
17133 token->type == CPP_OPEN_BRACE
17134 /* A ctor-initializer begins with a `:'. */
17135 || token->type == CPP_COLON
17136 /* A function-try-block begins with `try'. */
17137 || token->keyword == RID_TRY
17138 /* The named return value extension begins with `return'. */
17139 || token->keyword == RID_RETURN);
17142 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17146 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17150 token = cp_lexer_peek_token (parser->lexer);
17151 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17154 /* Returns TRUE iff the next token is the "," or ">" ending a
17155 template-argument. */
17158 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17162 token = cp_lexer_peek_token (parser->lexer);
17163 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
17166 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17167 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17170 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17175 token = cp_lexer_peek_nth_token (parser->lexer, n);
17176 if (token->type == CPP_LESS)
17178 /* Check for the sequence `<::' in the original code. It would be lexed as
17179 `[:', where `[' is a digraph, and there is no whitespace before
17181 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17184 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17185 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17191 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17192 or none_type otherwise. */
17194 static enum tag_types
17195 cp_parser_token_is_class_key (cp_token* token)
17197 switch (token->keyword)
17202 return record_type;
17211 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17214 cp_parser_check_class_key (enum tag_types class_key, tree type)
17216 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17217 pedwarn ("%qs tag used in naming %q#T",
17218 class_key == union_type ? "union"
17219 : class_key == record_type ? "struct" : "class",
17223 /* Issue an error message if DECL is redeclared with different
17224 access than its original declaration [class.access.spec/3].
17225 This applies to nested classes and nested class templates.
17229 cp_parser_check_access_in_redeclaration (tree decl)
17231 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17234 if ((TREE_PRIVATE (decl)
17235 != (current_access_specifier == access_private_node))
17236 || (TREE_PROTECTED (decl)
17237 != (current_access_specifier == access_protected_node)))
17238 error ("%qD redeclared with different access", decl);
17241 /* Look for the `template' keyword, as a syntactic disambiguator.
17242 Return TRUE iff it is present, in which case it will be
17246 cp_parser_optional_template_keyword (cp_parser *parser)
17248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17250 /* The `template' keyword can only be used within templates;
17251 outside templates the parser can always figure out what is a
17252 template and what is not. */
17253 if (!processing_template_decl)
17255 error ("%<template%> (as a disambiguator) is only allowed "
17256 "within templates");
17257 /* If this part of the token stream is rescanned, the same
17258 error message would be generated. So, we purge the token
17259 from the stream. */
17260 cp_lexer_purge_token (parser->lexer);
17265 /* Consume the `template' keyword. */
17266 cp_lexer_consume_token (parser->lexer);
17274 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17275 set PARSER->SCOPE, and perform other related actions. */
17278 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17281 struct tree_check *check_value;
17282 deferred_access_check *chk;
17283 VEC (deferred_access_check,gc) *checks;
17285 /* Get the stored value. */
17286 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17287 /* Perform any access checks that were deferred. */
17288 checks = check_value->checks;
17292 VEC_iterate (deferred_access_check, checks, i, chk) ;
17295 perform_or_defer_access_check (chk->binfo,
17300 /* Set the scope from the stored value. */
17301 parser->scope = check_value->value;
17302 parser->qualifying_scope = check_value->qualifying_scope;
17303 parser->object_scope = NULL_TREE;
17306 /* Consume tokens up through a non-nested END token. */
17309 cp_parser_cache_group (cp_parser *parser,
17310 enum cpp_ttype end,
17317 /* Abort a parenthesized expression if we encounter a brace. */
17318 if ((end == CPP_CLOSE_PAREN || depth == 0)
17319 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17321 /* If we've reached the end of the file, stop. */
17322 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17323 || (end != CPP_PRAGMA_EOL
17324 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17326 /* Consume the next token. */
17327 token = cp_lexer_consume_token (parser->lexer);
17328 /* See if it starts a new group. */
17329 if (token->type == CPP_OPEN_BRACE)
17331 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17335 else if (token->type == CPP_OPEN_PAREN)
17336 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17337 else if (token->type == CPP_PRAGMA)
17338 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17339 else if (token->type == end)
17344 /* Begin parsing tentatively. We always save tokens while parsing
17345 tentatively so that if the tentative parsing fails we can restore the
17349 cp_parser_parse_tentatively (cp_parser* parser)
17351 /* Enter a new parsing context. */
17352 parser->context = cp_parser_context_new (parser->context);
17353 /* Begin saving tokens. */
17354 cp_lexer_save_tokens (parser->lexer);
17355 /* In order to avoid repetitive access control error messages,
17356 access checks are queued up until we are no longer parsing
17358 push_deferring_access_checks (dk_deferred);
17361 /* Commit to the currently active tentative parse. */
17364 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17366 cp_parser_context *context;
17369 /* Mark all of the levels as committed. */
17370 lexer = parser->lexer;
17371 for (context = parser->context; context->next; context = context->next)
17373 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17375 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17376 while (!cp_lexer_saving_tokens (lexer))
17377 lexer = lexer->next;
17378 cp_lexer_commit_tokens (lexer);
17382 /* Abort the currently active tentative parse. All consumed tokens
17383 will be rolled back, and no diagnostics will be issued. */
17386 cp_parser_abort_tentative_parse (cp_parser* parser)
17388 cp_parser_simulate_error (parser);
17389 /* Now, pretend that we want to see if the construct was
17390 successfully parsed. */
17391 cp_parser_parse_definitely (parser);
17394 /* Stop parsing tentatively. If a parse error has occurred, restore the
17395 token stream. Otherwise, commit to the tokens we have consumed.
17396 Returns true if no error occurred; false otherwise. */
17399 cp_parser_parse_definitely (cp_parser* parser)
17401 bool error_occurred;
17402 cp_parser_context *context;
17404 /* Remember whether or not an error occurred, since we are about to
17405 destroy that information. */
17406 error_occurred = cp_parser_error_occurred (parser);
17407 /* Remove the topmost context from the stack. */
17408 context = parser->context;
17409 parser->context = context->next;
17410 /* If no parse errors occurred, commit to the tentative parse. */
17411 if (!error_occurred)
17413 /* Commit to the tokens read tentatively, unless that was
17415 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17416 cp_lexer_commit_tokens (parser->lexer);
17418 pop_to_parent_deferring_access_checks ();
17420 /* Otherwise, if errors occurred, roll back our state so that things
17421 are just as they were before we began the tentative parse. */
17424 cp_lexer_rollback_tokens (parser->lexer);
17425 pop_deferring_access_checks ();
17427 /* Add the context to the front of the free list. */
17428 context->next = cp_parser_context_free_list;
17429 cp_parser_context_free_list = context;
17431 return !error_occurred;
17434 /* Returns true if we are parsing tentatively and are not committed to
17435 this tentative parse. */
17438 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17440 return (cp_parser_parsing_tentatively (parser)
17441 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17444 /* Returns nonzero iff an error has occurred during the most recent
17445 tentative parse. */
17448 cp_parser_error_occurred (cp_parser* parser)
17450 return (cp_parser_parsing_tentatively (parser)
17451 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17454 /* Returns nonzero if GNU extensions are allowed. */
17457 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17459 return parser->allow_gnu_extensions_p;
17462 /* Objective-C++ Productions */
17465 /* Parse an Objective-C expression, which feeds into a primary-expression
17469 objc-message-expression
17470 objc-string-literal
17471 objc-encode-expression
17472 objc-protocol-expression
17473 objc-selector-expression
17475 Returns a tree representation of the expression. */
17478 cp_parser_objc_expression (cp_parser* parser)
17480 /* Try to figure out what kind of declaration is present. */
17481 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17485 case CPP_OPEN_SQUARE:
17486 return cp_parser_objc_message_expression (parser);
17488 case CPP_OBJC_STRING:
17489 kwd = cp_lexer_consume_token (parser->lexer);
17490 return objc_build_string_object (kwd->u.value);
17493 switch (kwd->keyword)
17495 case RID_AT_ENCODE:
17496 return cp_parser_objc_encode_expression (parser);
17498 case RID_AT_PROTOCOL:
17499 return cp_parser_objc_protocol_expression (parser);
17501 case RID_AT_SELECTOR:
17502 return cp_parser_objc_selector_expression (parser);
17508 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17509 cp_parser_skip_to_end_of_block_or_statement (parser);
17512 return error_mark_node;
17515 /* Parse an Objective-C message expression.
17517 objc-message-expression:
17518 [ objc-message-receiver objc-message-args ]
17520 Returns a representation of an Objective-C message. */
17523 cp_parser_objc_message_expression (cp_parser* parser)
17525 tree receiver, messageargs;
17527 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17528 receiver = cp_parser_objc_message_receiver (parser);
17529 messageargs = cp_parser_objc_message_args (parser);
17530 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17532 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17535 /* APPLE LOCAL begin radar 5277239 */
17536 /* Parse an Objective-C dot-syntax class expression.
17538 objc-message-expression:
17539 class-name '.' class-method-name
17541 Returns an objc_property_reference expression. */
17544 cp_parser_objc_reference_expression (cp_parser* parser, tree type_decl)
17546 tree receiver, component;
17547 receiver = objc_get_class_reference (TREE_TYPE (type_decl));
17548 cp_lexer_consume_token (parser->lexer); /* Eact '.' */
17549 component = cp_parser_objc_message_args (parser);
17550 return objc_build_property_reference_expr (receiver, TREE_PURPOSE (component));
17552 /* APPLE LOCAL end radar 5277239 */
17554 /* Parse an objc-message-receiver.
17556 objc-message-receiver:
17558 simple-type-specifier
17560 Returns a representation of the type or expression. */
17563 cp_parser_objc_message_receiver (cp_parser* parser)
17567 /* An Objective-C message receiver may be either (1) a type
17568 or (2) an expression. */
17569 cp_parser_parse_tentatively (parser);
17570 rcv = cp_parser_expression (parser, false);
17572 if (cp_parser_parse_definitely (parser))
17575 rcv = cp_parser_simple_type_specifier (parser,
17576 /*decl_specs=*/NULL,
17577 CP_PARSER_FLAGS_NONE);
17579 return objc_get_class_reference (rcv);
17582 /* Parse the arguments and selectors comprising an Objective-C message.
17587 objc-selector-args , objc-comma-args
17589 objc-selector-args:
17590 objc-selector [opt] : assignment-expression
17591 objc-selector-args objc-selector [opt] : assignment-expression
17594 assignment-expression
17595 objc-comma-args , assignment-expression
17597 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17598 selector arguments and TREE_VALUE containing a list of comma
17602 cp_parser_objc_message_args (cp_parser* parser)
17604 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17605 bool maybe_unary_selector_p = true;
17606 cp_token *token = cp_lexer_peek_token (parser->lexer);
17608 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17610 tree selector = NULL_TREE, arg;
17612 if (token->type != CPP_COLON)
17613 selector = cp_parser_objc_selector (parser);
17615 /* Detect if we have a unary selector. */
17616 if (maybe_unary_selector_p
17617 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17618 return build_tree_list (selector, NULL_TREE);
17620 maybe_unary_selector_p = false;
17621 cp_parser_require (parser, CPP_COLON, "`:'");
17622 arg = cp_parser_assignment_expression (parser, false);
17625 = chainon (sel_args,
17626 build_tree_list (selector, arg));
17628 token = cp_lexer_peek_token (parser->lexer);
17631 /* Handle non-selector arguments, if any. */
17632 while (token->type == CPP_COMMA)
17636 cp_lexer_consume_token (parser->lexer);
17637 arg = cp_parser_assignment_expression (parser, false);
17640 = chainon (addl_args,
17641 build_tree_list (NULL_TREE, arg));
17643 token = cp_lexer_peek_token (parser->lexer);
17646 return build_tree_list (sel_args, addl_args);
17649 /* Parse an Objective-C encode expression.
17651 objc-encode-expression:
17652 @encode objc-typename
17654 Returns an encoded representation of the type argument. */
17657 cp_parser_objc_encode_expression (cp_parser* parser)
17661 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17662 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17663 type = complete_type (cp_parser_type_id (parser));
17664 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17668 error ("%<@encode%> must specify a type as an argument");
17669 return error_mark_node;
17672 return objc_build_encode_expr (type);
17675 /* Parse an Objective-C @defs expression. */
17678 cp_parser_objc_defs_expression (cp_parser *parser)
17682 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17683 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17684 name = cp_parser_identifier (parser);
17685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17687 return objc_get_class_ivars (name);
17690 /* Parse an Objective-C protocol expression.
17692 objc-protocol-expression:
17693 @protocol ( identifier )
17695 Returns a representation of the protocol expression. */
17698 cp_parser_objc_protocol_expression (cp_parser* parser)
17702 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17703 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17704 proto = cp_parser_identifier (parser);
17705 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17707 return objc_build_protocol_expr (proto);
17710 /* Parse an Objective-C selector expression.
17712 objc-selector-expression:
17713 @selector ( objc-method-signature )
17715 objc-method-signature:
17721 objc-selector-seq objc-selector :
17723 Returns a representation of the method selector. */
17726 cp_parser_objc_selector_expression (cp_parser* parser)
17728 tree sel_seq = NULL_TREE;
17729 bool maybe_unary_selector_p = true;
17732 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17733 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17734 token = cp_lexer_peek_token (parser->lexer);
17736 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17737 || token->type == CPP_SCOPE)
17739 tree selector = NULL_TREE;
17741 if (token->type != CPP_COLON
17742 || token->type == CPP_SCOPE)
17743 selector = cp_parser_objc_selector (parser);
17745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17746 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17748 /* Detect if we have a unary selector. */
17749 if (maybe_unary_selector_p)
17751 sel_seq = selector;
17752 goto finish_selector;
17756 cp_parser_error (parser, "expected %<:%>");
17759 maybe_unary_selector_p = false;
17760 token = cp_lexer_consume_token (parser->lexer);
17762 if (token->type == CPP_SCOPE)
17765 = chainon (sel_seq,
17766 build_tree_list (selector, NULL_TREE));
17768 = chainon (sel_seq,
17769 build_tree_list (NULL_TREE, NULL_TREE));
17773 = chainon (sel_seq,
17774 build_tree_list (selector, NULL_TREE));
17776 token = cp_lexer_peek_token (parser->lexer);
17780 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17782 return objc_build_selector_expr (sel_seq);
17785 /* Parse a list of identifiers.
17787 objc-identifier-list:
17789 objc-identifier-list , identifier
17791 Returns a TREE_LIST of identifier nodes. */
17794 cp_parser_objc_identifier_list (cp_parser* parser)
17796 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17797 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17799 while (sep->type == CPP_COMMA)
17801 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17802 list = chainon (list,
17803 build_tree_list (NULL_TREE,
17804 cp_parser_identifier (parser)));
17805 sep = cp_lexer_peek_token (parser->lexer);
17811 /* Parse an Objective-C alias declaration.
17813 objc-alias-declaration:
17814 @compatibility_alias identifier identifier ;
17816 This function registers the alias mapping with the Objective-C front-end.
17817 It returns nothing. */
17820 cp_parser_objc_alias_declaration (cp_parser* parser)
17824 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17825 alias = cp_parser_identifier (parser);
17826 orig = cp_parser_identifier (parser);
17827 objc_declare_alias (alias, orig);
17828 cp_parser_consume_semicolon_at_end_of_statement (parser);
17831 /* Parse an Objective-C class forward-declaration.
17833 objc-class-declaration:
17834 @class objc-identifier-list ;
17836 The function registers the forward declarations with the Objective-C
17837 front-end. It returns nothing. */
17840 cp_parser_objc_class_declaration (cp_parser* parser)
17842 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17843 objc_declare_class (cp_parser_objc_identifier_list (parser));
17844 cp_parser_consume_semicolon_at_end_of_statement (parser);
17847 /* Parse a list of Objective-C protocol references.
17849 objc-protocol-refs-opt:
17850 objc-protocol-refs [opt]
17852 objc-protocol-refs:
17853 < objc-identifier-list >
17855 Returns a TREE_LIST of identifiers, if any. */
17858 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17860 tree protorefs = NULL_TREE;
17862 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17864 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17865 protorefs = cp_parser_objc_identifier_list (parser);
17866 cp_parser_require (parser, CPP_GREATER, "`>'");
17872 /* Parse a Objective-C visibility specification. */
17875 cp_parser_objc_visibility_spec (cp_parser* parser)
17877 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17879 switch (vis->keyword)
17881 case RID_AT_PRIVATE:
17882 objc_set_visibility (2);
17884 case RID_AT_PROTECTED:
17885 objc_set_visibility (0);
17887 case RID_AT_PUBLIC:
17888 objc_set_visibility (1);
17894 /* Eat '@private'/'@protected'/'@public'. */
17895 cp_lexer_consume_token (parser->lexer);
17898 /* Parse an Objective-C method type. */
17901 cp_parser_objc_method_type (cp_parser* parser)
17903 objc_set_method_type
17904 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17909 /* Parse an Objective-C protocol qualifier. */
17912 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17914 tree quals = NULL_TREE, node;
17915 cp_token *token = cp_lexer_peek_token (parser->lexer);
17917 node = token->u.value;
17919 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17920 && (node == ridpointers [(int) RID_IN]
17921 || node == ridpointers [(int) RID_OUT]
17922 || node == ridpointers [(int) RID_INOUT]
17923 || node == ridpointers [(int) RID_BYCOPY]
17924 || node == ridpointers [(int) RID_BYREF]
17925 || node == ridpointers [(int) RID_ONEWAY]))
17927 quals = tree_cons (NULL_TREE, node, quals);
17928 cp_lexer_consume_token (parser->lexer);
17929 token = cp_lexer_peek_token (parser->lexer);
17930 node = token->u.value;
17936 /* Parse an Objective-C typename. */
17939 cp_parser_objc_typename (cp_parser* parser)
17941 tree typename = NULL_TREE;
17943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17945 tree proto_quals, cp_type = NULL_TREE;
17947 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17948 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17950 /* An ObjC type name may consist of just protocol qualifiers, in which
17951 case the type shall default to 'id'. */
17952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17953 cp_type = cp_parser_type_id (parser);
17955 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17956 typename = build_tree_list (proto_quals, cp_type);
17962 /* Check to see if TYPE refers to an Objective-C selector name. */
17965 cp_parser_objc_selector_p (enum cpp_ttype type)
17967 return (type == CPP_NAME || type == CPP_KEYWORD
17968 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17969 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17970 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17971 || type == CPP_XOR || type == CPP_XOR_EQ);
17974 /* Parse an Objective-C selector. */
17977 cp_parser_objc_selector (cp_parser* parser)
17979 cp_token *token = cp_lexer_consume_token (parser->lexer);
17981 if (!cp_parser_objc_selector_p (token->type))
17983 error ("invalid Objective-C++ selector name");
17984 return error_mark_node;
17987 /* C++ operator names are allowed to appear in ObjC selectors. */
17988 switch (token->type)
17990 case CPP_AND_AND: return get_identifier ("and");
17991 case CPP_AND_EQ: return get_identifier ("and_eq");
17992 case CPP_AND: return get_identifier ("bitand");
17993 case CPP_OR: return get_identifier ("bitor");
17994 case CPP_COMPL: return get_identifier ("compl");
17995 case CPP_NOT: return get_identifier ("not");
17996 case CPP_NOT_EQ: return get_identifier ("not_eq");
17997 case CPP_OR_OR: return get_identifier ("or");
17998 case CPP_OR_EQ: return get_identifier ("or_eq");
17999 case CPP_XOR: return get_identifier ("xor");
18000 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18001 default: return token->u.value;
18005 /* APPLE LOCAL begin radar 3803157 - objc attribute */
18007 cp_parser_objc_maybe_attributes (cp_parser* parser, tree* attributes)
18009 cp_token *token = cp_lexer_peek_token (parser->lexer);
18010 if (*attributes != NULL_TREE)
18012 error ("method attributes must be specified at the end only");
18013 *attributes = NULL_TREE;
18015 if (token->keyword == RID_ATTRIBUTE)
18016 *attributes = cp_parser_attributes_opt (parser);
18019 /* Parse an Objective-C params list. */
18022 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
18023 /* APPLE LOCAL end radar 3803157 - objc attribute */
18025 tree params = NULL_TREE;
18026 bool maybe_unary_selector_p = true;
18027 cp_token *token = cp_lexer_peek_token (parser->lexer);
18029 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18031 tree selector = NULL_TREE, typename, identifier;
18032 /* APPLE LOCAL radar 4157812 */
18033 tree attr = NULL_TREE;
18035 if (token->type != CPP_COLON)
18036 selector = cp_parser_objc_selector (parser);
18038 /* Detect if we have a unary selector. */
18039 if (maybe_unary_selector_p
18040 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18041 /* APPLE LOCAL begin radar 3803157 - objc attribute */
18043 cp_parser_objc_maybe_attributes (parser, attributes);
18044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18047 /* APPLE LOCAL end radar 3803157 - objc attribute */
18049 maybe_unary_selector_p = false;
18050 cp_parser_require (parser, CPP_COLON, "`:'");
18051 typename = cp_parser_objc_typename (parser);
18052 /* APPLE LOCAL radar 4157812 */
18053 cp_parser_objc_maybe_attributes (parser, &attr);
18054 identifier = cp_parser_identifier (parser);
18055 /* APPLE LOCAL radar 3803157 - objc attribute */
18056 cp_parser_objc_maybe_attributes (parser, attributes);
18060 objc_build_keyword_decl (selector,
18062 /* APPLE LOCAL radar 4157812 */
18063 identifier, attr));
18065 token = cp_lexer_peek_token (parser->lexer);
18068 /* APPLE LOCAL begin radar 4290840 */
18069 if (params == NULL_TREE)
18071 cp_parser_error (parser, "objective-c++ method declaration is expected");
18072 return error_mark_node;
18074 /* APPLE LOCAL end radar 4290840 */
18079 /* Parse the non-keyword Objective-C params. */
18082 /* APPLE LOCAL radar 3803157 - objc attribute */
18083 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, tree* attributes)
18085 tree params = make_node (TREE_LIST);
18086 cp_token *token = cp_lexer_peek_token (parser->lexer);
18087 *ellipsisp = false; /* Initially, assume no ellipsis. */
18089 while (token->type == CPP_COMMA)
18091 cp_parameter_declarator *parmdecl;
18094 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18095 token = cp_lexer_peek_token (parser->lexer);
18097 if (token->type == CPP_ELLIPSIS)
18099 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18101 /* APPLE LOCAL radar 3803157 - objc attribute */
18102 cp_parser_objc_maybe_attributes (parser, attributes);
18106 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18107 parm = grokdeclarator (parmdecl->declarator,
18108 &parmdecl->decl_specifiers,
18109 PARM, /*initialized=*/0,
18110 /*attrlist=*/NULL);
18112 chainon (params, build_tree_list (NULL_TREE, parm));
18113 token = cp_lexer_peek_token (parser->lexer);
18119 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18122 cp_parser_objc_interstitial_code (cp_parser* parser)
18124 cp_token *token = cp_lexer_peek_token (parser->lexer);
18126 /* If the next token is `extern' and the following token is a string
18127 literal, then we have a linkage specification. */
18128 if (token->keyword == RID_EXTERN
18129 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18130 cp_parser_linkage_specification (parser);
18131 /* Handle #pragma, if any. */
18132 else if (token->type == CPP_PRAGMA)
18133 cp_parser_pragma (parser, pragma_external);
18134 /* Allow stray semicolons. */
18135 else if (token->type == CPP_SEMICOLON)
18136 cp_lexer_consume_token (parser->lexer);
18137 /* Finally, try to parse a block-declaration, or a function-definition. */
18139 cp_parser_block_declaration (parser, /*statement_p=*/false);
18142 /* Parse a method signature. */
18145 /* APPLE LOCAL radar 3803157 - objc attribute */
18146 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
18148 tree rettype, kwdparms, optparms;
18149 bool ellipsis = false;
18151 cp_parser_objc_method_type (parser);
18152 rettype = cp_parser_objc_typename (parser);
18153 /* APPLE LOCAL begin radar 3803157 - objc attribute */
18154 *attributes = NULL_TREE;
18155 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
18156 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
18157 /* APPLE LOCAL end radar 3803157 - objc attribute */
18159 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18162 /* Pars an Objective-C method prototype list. */
18165 cp_parser_objc_method_prototype_list (cp_parser* parser)
18167 cp_token *token = cp_lexer_peek_token (parser->lexer);
18169 /* APPLE LOCAL 4093475 */
18170 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
18172 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18174 /* APPLE LOCAL begin radar 3803157 - objc attribute */
18175 tree attributes, sig;
18176 sig = cp_parser_objc_method_signature (parser, &attributes);
18177 objc_add_method_declaration (sig, attributes);
18178 /* APPLE LOCAL end radar 3803157 - objc attribute */
18179 cp_parser_consume_semicolon_at_end_of_statement (parser);
18181 /* APPLE LOCAL begin C* interface */
18182 else if (token->keyword == RID_AT_PROPERTY)
18183 objc_cp_parser_at_property (parser);
18184 /* APPLE LOCAL end C* interface */
18186 /* Allow for interspersed non-ObjC++ code. */
18187 cp_parser_objc_interstitial_code (parser);
18189 token = cp_lexer_peek_token (parser->lexer);
18192 /* APPLE LOCAL 4093475 */
18193 cp_parser_require_keyword (parser, RID_AT_END, "`@end'");
18194 objc_finish_interface ();
18197 /* Parse an Objective-C method definition list. */
18200 cp_parser_objc_method_definition_list (cp_parser* parser)
18202 cp_token *token = cp_lexer_peek_token (parser->lexer);
18204 /* APPLE LOCAL 4093475 */
18205 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
18209 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18211 /* APPLE LOCAL radar 4290840 */
18213 /* APPLE LOCAL begin radar 3803157 - objc attribute */
18214 tree sig, attribute;
18215 push_deferring_access_checks (dk_deferred);
18216 sig = cp_parser_objc_method_signature (parser, &attribute);
18217 objc_start_method_definition (sig, attribute);
18218 /* APPLE LOCAL end radar 3803157 - objc attribute */
18220 /* For historical reasons, we accept an optional semicolon. */
18221 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18222 cp_lexer_consume_token (parser->lexer);
18224 /* APPLE LOCAL begin radar 4290840 */
18225 /* Check for all possibilities of illegal lookahead tokens. */
18226 ptk = cp_lexer_peek_token (parser->lexer);
18227 /* APPLE LOCAL radar 6271728 */
18228 if (ptk->type == CPP_OPEN_BRACE)
18230 perform_deferred_access_checks ();
18231 stop_deferring_access_checks ();
18232 meth = cp_parser_function_definition_after_declarator (parser,
18234 pop_deferring_access_checks ();
18235 objc_finish_method_definition (meth);
18237 /* APPLE LOCAL begin radar 6271728 */
18239 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
18240 /* APPLE LOCAL end radar 6271728 */
18241 /* APPLE LOCAL end radar 4290840 */
18243 /* APPLE LOCAL begin C* interface */
18244 else if (token->keyword == RID_AT_PROPERTY)
18245 objc_cp_parser_at_property (parser);
18246 /* APPLE LOCAL end C* interface */
18248 /* Allow for interspersed non-ObjC++ code. */
18249 cp_parser_objc_interstitial_code (parser);
18251 token = cp_lexer_peek_token (parser->lexer);
18254 /* APPLE LOCAL 4093475 */
18255 cp_parser_require_keyword (parser, RID_AT_END, "`@end'");
18256 objc_finish_implementation ();
18259 /* Parse Objective-C ivars. */
18262 cp_parser_objc_class_ivars (cp_parser* parser)
18264 cp_token *token = cp_lexer_peek_token (parser->lexer);
18266 if (token->type != CPP_OPEN_BRACE)
18267 return; /* No ivars specified. */
18269 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18270 token = cp_lexer_peek_token (parser->lexer);
18272 while (token->type != CPP_CLOSE_BRACE)
18274 cp_decl_specifier_seq declspecs;
18275 int decl_class_or_enum_p;
18276 tree prefix_attributes;
18278 cp_parser_objc_visibility_spec (parser);
18280 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18283 cp_parser_decl_specifier_seq (parser,
18284 CP_PARSER_FLAGS_OPTIONAL,
18286 &decl_class_or_enum_p);
18287 prefix_attributes = declspecs.attributes;
18288 declspecs.attributes = NULL_TREE;
18290 /* Keep going until we hit the `;' at the end of the
18292 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18294 tree width = NULL_TREE, attributes, first_attribute, decl;
18295 cp_declarator *declarator = NULL;
18296 int ctor_dtor_or_conv_p;
18298 /* Check for a (possibly unnamed) bitfield declaration. */
18299 token = cp_lexer_peek_token (parser->lexer);
18300 if (token->type == CPP_COLON)
18303 if (token->type == CPP_NAME
18304 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18307 /* Get the name of the bitfield. */
18308 declarator = make_id_declarator (NULL_TREE,
18309 cp_parser_identifier (parser),
18313 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18314 /* Get the width of the bitfield. */
18316 = cp_parser_constant_expression (parser,
18317 /*allow_non_constant=*/false,
18322 /* Parse the declarator. */
18324 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18325 &ctor_dtor_or_conv_p,
18326 /*parenthesized_p=*/NULL,
18327 /*member_p=*/false);
18330 /* Look for attributes that apply to the ivar. */
18331 attributes = cp_parser_attributes_opt (parser);
18332 /* Remember which attributes are prefix attributes and
18334 first_attribute = attributes;
18335 /* Combine the attributes. */
18336 attributes = chainon (prefix_attributes, attributes);
18340 /* Create the bitfield declaration. */
18341 decl = grokbitfield (declarator, &declspecs, width);
18342 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18345 decl = grokfield (declarator, &declspecs,
18346 NULL_TREE, /*init_const_expr_p=*/false,
18347 NULL_TREE, attributes);
18349 /* Add the instance variable. */
18350 objc_add_instance_variable (decl);
18352 /* Reset PREFIX_ATTRIBUTES. */
18353 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18354 attributes = TREE_CHAIN (attributes);
18356 TREE_CHAIN (attributes) = NULL_TREE;
18358 token = cp_lexer_peek_token (parser->lexer);
18360 if (token->type == CPP_COMMA)
18362 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18368 cp_parser_consume_semicolon_at_end_of_statement (parser);
18369 token = cp_lexer_peek_token (parser->lexer);
18372 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18373 /* For historical reasons, we accept an optional semicolon. */
18374 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18375 cp_lexer_consume_token (parser->lexer);
18378 /* Parse an Objective-C protocol declaration. */
18381 /* APPLE LOCAL radar 4947311 */
18382 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
18384 tree proto, protorefs;
18387 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18390 error ("identifier expected after %<@protocol%>");
18394 /* See if we have a forward declaration or a definition. */
18395 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18397 /* Try a forward declaration first. */
18398 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18400 /* APPLE LOCAL radar 4947311 */
18401 objc_declare_protocols (cp_parser_objc_identifier_list (parser), attributes);
18403 cp_parser_consume_semicolon_at_end_of_statement (parser);
18406 /* Ok, we got a full-fledged definition (or at least should). */
18409 proto = cp_parser_identifier (parser);
18410 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18411 /* APPLE LOCAL radar 4947311 */
18412 objc_start_protocol (proto, protorefs, attributes);
18413 cp_parser_objc_method_prototype_list (parser);
18417 /* Parse an Objective-C superclass or category. */
18419 /* APPLE LOCAL begin radar 4965989 */
18421 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18422 tree *categ, bool *is_category)
18424 cp_token *next = cp_lexer_peek_token (parser->lexer);
18426 *super = *categ = NULL_TREE;
18427 *is_category = false;
18428 if (next->type == CPP_COLON)
18430 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18431 *super = cp_parser_identifier (parser);
18433 else if (next->type == CPP_OPEN_PAREN)
18435 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18436 /* APPLE LOCAL begin radar 4965989 */
18437 next = cp_lexer_peek_token (parser->lexer);
18438 *categ = (next->type == CPP_CLOSE_PAREN) ? NULL_TREE : cp_parser_identifier (parser);
18439 *is_category = true;
18440 /* APPLE LOCAL end radar 4965989 */
18441 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18444 /* APPLE LOCAL end radar 4965989 */
18446 /* Parse an Objective-C class interface. */
18449 /* APPLE LOCAL radar 4947311 */
18450 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
18452 tree name, super, categ, protos;
18453 /* APPLE LOCAL radar 4965989 */
18455 /* APPLE LOCAL radar 4947311 */
18456 /* Code for radar 4548636 removed. */
18457 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18458 name = cp_parser_identifier (parser);
18459 /* APPLE LOCAL radar 4965989 */
18460 cp_parser_objc_superclass_or_category (parser, &super, &categ, &is_categ);
18461 protos = cp_parser_objc_protocol_refs_opt (parser);
18463 /* We have either a class or a category on our hands. */
18464 /* APPLE LOCAL radar 4965989 */
18466 /* APPLE LOCAL begin radar 4548636 */
18469 error ("attributes may not be specified on a category");
18470 objc_start_category_interface (name, categ, protos);
18472 /* APPLE LOCAL end radar 4548636 */
18475 /* APPLE LOCAL radar 4548636 */
18476 objc_start_class_interface (name, super, protos, attributes);
18477 /* Handle instance variable declarations, if any. */
18478 cp_parser_objc_class_ivars (parser);
18479 objc_continue_interface ();
18482 cp_parser_objc_method_prototype_list (parser);
18485 /* Parse an Objective-C class implementation. */
18488 cp_parser_objc_class_implementation (cp_parser* parser)
18490 tree name, super, categ;
18491 /* APPLE LOCAL radar 4965989 */
18493 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18494 name = cp_parser_identifier (parser);
18495 /* APPLE LOCAL radar 4965989 */
18496 cp_parser_objc_superclass_or_category (parser, &super, &categ, &is_categ);
18498 /* We have either a class or a category on our hands. */
18499 /* APPLE LOCAL begin radar 4965989 */
18502 if (categ == NULL_TREE)
18504 error ("cannot implement anonymous category");
18507 objc_start_category_implementation (name, categ);
18509 /* APPLE LOCAL end radar 4965989 */
18512 objc_start_class_implementation (name, super);
18513 /* Handle instance variable declarations, if any. */
18514 cp_parser_objc_class_ivars (parser);
18515 objc_continue_implementation ();
18518 cp_parser_objc_method_definition_list (parser);
18521 /* Consume the @end token and finish off the implementation. */
18524 cp_parser_objc_end_implementation (cp_parser* parser)
18526 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18527 objc_finish_implementation ();
18530 /* Parse an Objective-C declaration. */
18533 cp_parser_objc_declaration (cp_parser* parser)
18535 /* Try to figure out what kind of declaration is present. */
18536 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18538 switch (kwd->keyword)
18541 cp_parser_objc_alias_declaration (parser);
18544 cp_parser_objc_class_declaration (parser);
18546 case RID_AT_PROTOCOL:
18547 /* APPLE LOCAL radar 4947311 */
18548 cp_parser_objc_protocol_declaration (parser, NULL_TREE);
18550 /* APPLE LOCAL begin radar 4548636 - radar 4947311 */
18551 case RID_ATTRIBUTE:
18553 tree attributes = NULL_TREE;
18554 cp_parser_objc_maybe_attributes (parser, &attributes);
18555 if (cp_lexer_peek_token (parser->lexer)->keyword == RID_AT_INTERFACE)
18556 cp_parser_objc_class_interface (parser, attributes);
18557 else if (cp_lexer_peek_token (parser->lexer)->keyword == RID_AT_PROTOCOL)
18558 cp_parser_objc_protocol_declaration (parser, attributes);
18561 /* APPLE LOCAL end radar 4548636 - radar 4947311 */
18562 case RID_AT_INTERFACE:
18563 /* APPLE LOCAL radar 4947311 */
18564 cp_parser_objc_class_interface (parser, NULL_TREE);
18566 case RID_AT_IMPLEMENTATION:
18567 cp_parser_objc_class_implementation (parser);
18570 cp_parser_objc_end_implementation (parser);
18573 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18574 cp_parser_skip_to_end_of_block_or_statement (parser);
18578 /* Parse an Objective-C try-catch-finally statement.
18580 objc-try-catch-finally-stmt:
18581 @try compound-statement objc-catch-clause-seq [opt]
18582 objc-finally-clause [opt]
18584 objc-catch-clause-seq:
18585 objc-catch-clause objc-catch-clause-seq [opt]
18588 @catch ( exception-declaration ) compound-statement
18590 objc-finally-clause
18591 @finally compound-statement
18593 Returns NULL_TREE. */
18596 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18597 location_t location;
18600 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18601 location = cp_lexer_peek_token (parser->lexer)->location;
18602 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18603 node, lest it get absorbed into the surrounding block. */
18604 stmt = push_stmt_list ();
18605 /* APPLE LOCAL radar 5982990 */
18606 cp_parser_compound_statement (parser, NULL, false, false);
18607 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18609 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18611 cp_parameter_declarator *parmdecl;
18614 cp_lexer_consume_token (parser->lexer);
18615 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18616 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18617 objc_begin_catch_clause (parm);
18618 /* APPLE LOCAL radar 5982990 */
18619 cp_parser_compound_statement (parser, NULL, false, false);
18620 objc_finish_catch_clause ();
18623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18625 cp_lexer_consume_token (parser->lexer);
18626 location = cp_lexer_peek_token (parser->lexer)->location;
18627 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18628 node, lest it get absorbed into the surrounding block. */
18629 stmt = push_stmt_list ();
18630 /* APPLE LOCAL radar 5982990 */
18631 cp_parser_compound_statement (parser, NULL, false, false);
18632 objc_build_finally_clause (location, pop_stmt_list (stmt));
18635 return objc_finish_try_stmt ();
18638 /* Parse an Objective-C synchronized statement.
18640 objc-synchronized-stmt:
18641 @synchronized ( expression ) compound-statement
18643 Returns NULL_TREE. */
18646 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18647 location_t location;
18650 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18652 location = cp_lexer_peek_token (parser->lexer)->location;
18653 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18654 lock = cp_parser_expression (parser, false);
18655 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18657 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18658 node, lest it get absorbed into the surrounding block. */
18659 stmt = push_stmt_list ();
18660 /* APPLE LOCAL radar 5982990 */
18661 cp_parser_compound_statement (parser, NULL, false, flag_objc_sjlj_exceptions);
18663 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18666 /* Parse an Objective-C throw statement.
18669 @throw assignment-expression [opt] ;
18671 Returns a constructed '@throw' statement. */
18674 cp_parser_objc_throw_statement (cp_parser *parser) {
18675 tree expr = NULL_TREE;
18677 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18680 expr = cp_parser_assignment_expression (parser, false);
18682 cp_parser_consume_semicolon_at_end_of_statement (parser);
18684 return objc_build_throw_stmt (expr);
18687 /* Parse an Objective-C statement. */
18690 cp_parser_objc_statement (cp_parser * parser) {
18691 /* Try to figure out what kind of declaration is present. */
18692 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18694 switch (kwd->keyword)
18697 return cp_parser_objc_try_catch_finally_statement (parser);
18698 case RID_AT_SYNCHRONIZED:
18699 return cp_parser_objc_synchronized_statement (parser);
18701 return cp_parser_objc_throw_statement (parser);
18703 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18704 cp_parser_skip_to_end_of_block_or_statement (parser);
18707 return error_mark_node;
18710 /* APPLE LOCAL begin C* language */
18711 /* Routine closes up the C*'s foreach statement.
18715 objc_finish_foreach_stmt (tree for_stmt)
18717 if (flag_new_for_scope > 0)
18719 tree scope = TREE_CHAIN (for_stmt);
18720 TREE_CHAIN (for_stmt) = NULL;
18721 add_stmt (do_poplevel (scope));
18728 Synthesizer routine for C*'s feareach statement.
18731 for ( type elem in collection) { stmts; }
18736 __objcFastEnumerationState enumState = { 0 };
18739 unsigned long limit = [collection countByEnumeratingWithState:&enumState objects:items count:16];
18741 unsigned long startMutations = *enumState.mutationsPtr;
18743 unsigned long counter = 0;
18745 if (startMutations != *enumState.mutationsPtr) objc_enumerationMutation(collection);
18746 elem = enumState.itemsPtr[counter++];
18748 } while (counter < limit);
18749 } while (limit = [collection countByEnumeratingWithState:&enumState objects:items count:16]);
18752 elem = nil; radar 4854605, 5128402
18757 objc_foreach_stmt (cp_parser* parser, tree statement)
18759 unsigned char in_statement;
18760 tree enumerationMutation_call_exp;
18761 tree countByEnumeratingWithState;
18764 tree enumState_decl, items_decl;
18765 tree limit_decl, limit_decl_assign_expr;
18766 tree outer_if_stmt, inner_if_stmt, if_condition, startMutations_decl;
18767 tree outer_do_stmt, inner_do_stmt, do_condition;
18769 tree_stmt_iterator i = tsi_start (TREE_CHAIN (statement));
18770 tree t = tsi_stmt (i);
18771 /* APPLE LOCAL radar 5130983 */
18772 tree elem_decl = TREE_CODE (t) == DECL_EXPR ? DECL_EXPR_DECL (t) : t;
18774 receiver = cp_parser_condition (parser);
18775 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18777 /* APPLE LOCAL begin radar 4507230 */
18778 if (!objc_type_valid_for_messaging (TREE_TYPE (elem_decl)))
18780 error ("selector element does not have a valid object type");
18784 if (!objc_type_valid_for_messaging (TREE_TYPE (receiver)))
18786 error ("expression does not have a valid object type");
18789 /* APPLE LOCAL end radar 4507230 */
18791 enumerationMutation_call_exp = objc_build_foreach_components (receiver, &enumState_decl,
18792 &items_decl, &limit_decl,
18793 &startMutations_decl, &counter_decl,
18794 &countByEnumeratingWithState);
18796 /* __objcFastEnumerationState enumState = { 0 }; */
18797 exp = build_stmt (DECL_EXPR, enumState_decl);
18798 bind = build3 (BIND_EXPR, void_type_node, enumState_decl, exp, NULL);
18799 TREE_SIDE_EFFECTS (bind) = 1;
18802 /* id items[16]; */
18803 bind = build3 (BIND_EXPR, void_type_node, items_decl, NULL, NULL);
18804 TREE_SIDE_EFFECTS (bind) = 1;
18807 /* Generate this statement and add it to the list. */
18808 /* limit = [collection countByEnumeratingWithState:&enumState objects:items count:16] */
18809 limit_decl_assign_expr = build2 (MODIFY_EXPR, TREE_TYPE (limit_decl), limit_decl,
18810 countByEnumeratingWithState);
18811 bind = build3 (BIND_EXPR, void_type_node, limit_decl, NULL, NULL);
18812 TREE_SIDE_EFFECTS (bind) = 1;
18816 outer_if_stmt = begin_if_stmt ();
18817 /* APPLE LOCAL radar 4547045 */
18818 if_condition = build_binary_op (NE_EXPR, limit_decl_assign_expr,
18819 fold_convert (TREE_TYPE (limit_decl), integer_zero_node),
18822 finish_if_stmt_cond (if_condition, outer_if_stmt);
18824 /* unsigned long startMutations = *enumState.mutationsPtr; */
18825 exp = objc_build_component_ref (enumState_decl, get_identifier("mutationsPtr"));
18826 exp = build_indirect_ref (exp, "unary *");
18827 exp = build2 (MODIFY_EXPR, void_type_node, startMutations_decl, exp);
18828 bind = build3 (BIND_EXPR, void_type_node, startMutations_decl, exp, NULL);
18829 TREE_SIDE_EFFECTS (bind) = 1;
18833 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
18834 outer_do_stmt = begin_do_stmt (NULL_TREE);
18836 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
18837 /* Body of the outer do-while loop */
18838 /* unsigned int counter = 0; */
18839 exp = build2 (MODIFY_EXPR, void_type_node, counter_decl,
18840 fold_convert (TREE_TYPE (counter_decl), integer_zero_node));
18841 bind = build3 (BIND_EXPR, void_type_node, counter_decl, exp, NULL);
18842 TREE_SIDE_EFFECTS (bind) = 1;
18846 /* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
18847 inner_do_stmt = begin_do_stmt (NULL_TREE);
18849 /* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
18850 /* Body of the inner do-while loop */
18852 /* if (startMutations != *enumState.mutationsPtr) objc_enumerationMutation (collection); */
18853 inner_if_stmt = begin_if_stmt ();
18854 exp = objc_build_component_ref (enumState_decl, get_identifier("mutationsPtr"));
18855 exp = build_indirect_ref (exp, "unary *");
18856 if_condition = build_binary_op (NE_EXPR, startMutations_decl, exp, 1);
18857 finish_if_stmt_cond (if_condition, inner_if_stmt);
18859 add_stmt (enumerationMutation_call_exp);
18860 finish_then_clause (inner_if_stmt);
18861 finish_if_stmt (inner_if_stmt);
18863 /* elem = enumState.itemsPtr [counter]; */
18864 exp = objc_build_component_ref (enumState_decl, get_identifier("itemsPtr"));
18865 exp = build_array_ref (exp, counter_decl);
18866 add_stmt (build2 (MODIFY_EXPR, void_type_node, elem_decl, exp));
18867 /* APPLE LOCAL radar 4538105 */
18868 TREE_USED (elem_decl) = 1;
18871 exp = build2 (PLUS_EXPR, TREE_TYPE (counter_decl), counter_decl,
18872 build_int_cst (NULL_TREE, 1));
18873 add_stmt (build2 (MODIFY_EXPR, void_type_node, counter_decl, exp));
18875 /* ADD << stmts >> from the foreach loop. */
18876 /* Parse the body of the for-statement. */
18877 in_statement = parser->in_statement;
18878 parser->in_statement = IN_ITERATION_STMT;
18879 cp_parser_already_scoped_statement (parser);
18880 parser->in_statement = in_statement;
18882 finish_do_body (inner_do_stmt);
18884 /* } while (counter < limit ); */
18885 do_condition = build_binary_op (LT_EXPR, counter_decl, limit_decl, 1);
18886 finish_do_stmt (do_condition, inner_do_stmt);
18887 DO_FOREACH (inner_do_stmt) = integer_zero_node;
18888 /* APPLE LOCAL radar 4667060 */
18889 DO_FOREACH (outer_do_stmt) = elem_decl;
18891 finish_do_body (outer_do_stmt);
18893 /* } while (limit = [collection countByEnumeratingWithState:&enumState objects:items count:16]); */
18895 exp = unshare_expr (limit_decl_assign_expr);
18896 do_condition = build_binary_op (NE_EXPR, exp,
18897 fold_convert (TREE_TYPE (limit_decl), integer_zero_node),
18899 finish_do_stmt (do_condition, outer_do_stmt);
18902 finish_then_clause (outer_if_stmt);
18905 /* APPLE LOCAL begin radar 4854605 - radar 5128402 */
18906 begin_else_clause (outer_if_stmt);
18907 add_stmt (build2 (MODIFY_EXPR, void_type_node, elem_decl,
18908 fold_convert (TREE_TYPE (elem_decl), integer_zero_node)));
18909 finish_else_clause (outer_if_stmt);
18910 /* APPLE LOCAL end radar 4854605 - radar 5128402 */
18912 finish_if_stmt (outer_if_stmt);
18914 objc_finish_foreach_stmt (statement);
18916 /* APPLE LOCAL end C* language */
18917 /* APPLE LOCAL begin blocks 6040305 (ce) */
18918 #define I_SYMBOL_BINDING(t) IDENTIFIER_BINDING(t)
18920 tree build_component_ref (tree e, tree member);
18922 build_component_ref (tree e, tree member)
18924 if (!DECL_P (member))
18925 member = lookup_member (TREE_TYPE (e), member, 0, 0);
18926 if (processing_template_decl)
18927 return build3 (COMPONENT_REF, TREE_TYPE (member), e, DECL_NAME (member), NULL_TREE);
18928 return build_class_member_access_expr (e, member,
18932 /* APPLE LOCAL begin radar 6214617 */
18934 cp_block_requires_copying (tree exp)
18936 return (block_requires_copying (exp)
18937 || TYPE_HAS_CONSTRUCTOR (TREE_TYPE (exp))
18938 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)));
18940 /* APPLE LOCAL end radar 6214617 */
18942 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
18943 /** build_descriptor_block_decl -
18944 This routine builds a static block_descriptior variable of type:
18945 struct __block_descriptor; and initializes it to:
18946 {0, sizeof(struct literal_block_n),
18947 copy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
18948 destroy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
18952 build_descriptor_block_decl (tree block_struct_type, struct block_sema_info *block_impl)
18954 extern tree create_tmp_var_raw (tree, const char *);
18955 static int desc_unique_count;
18958 tree decl, constructor;
18960 VEC(constructor_elt,gc) *impl_v = NULL;
18961 tree descriptor_type =
18962 TREE_TYPE (build_block_descriptor_type (block_impl->BlockHasCopyDispose));
18964 sprintf (name, "__block_descriptor_tmp_%d", ++desc_unique_count);
18965 decl = create_tmp_var_raw (descriptor_type, name);
18966 DECL_CONTEXT (decl) = NULL_TREE;
18968 /* Initialize "reserved" field to 0 for now. */
18969 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (long_unsigned_type_node, 0));
18971 /* Initialize "Size" field. */
18972 size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (block_struct_type));
18973 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (long_unsigned_type_node, size));
18975 if (block_impl->BlockHasCopyDispose)
18977 /* Initialize "CopyFuncPtr" and "DestroyFuncPtr" fields. */
18978 /* Helpers were previously generated completeley as a nested
18979 function (and context was required for code gen.) But they are not,
18980 so context must be set to NULL so initialization logic does not complain. */
18981 DECL_CONTEXT (block_impl->copy_helper_func_decl) = NULL_TREE;
18982 helper_addr = build_fold_addr_expr (block_impl->copy_helper_func_decl);
18983 helper_addr = convert (ptr_type_node, helper_addr);
18984 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, helper_addr);
18986 DECL_CONTEXT (block_impl->destroy_helper_func_decl) = NULL_TREE;
18987 helper_addr = build_fold_addr_expr (block_impl->destroy_helper_func_decl);
18988 helper_addr = convert (ptr_type_node, helper_addr);
18989 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, helper_addr);
18991 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18992 constructor = make_node (CONSTRUCTOR);
18993 CONSTRUCTOR_ELTS (constructor) = impl_v;
18994 TREE_PUBLIC (decl) = 0;
18995 TREE_STATIC (decl) = 1;
18996 cp_finish_decl (decl, constructor, 0, 0, LOOKUP_ONLYCONVERTING);
19000 /* APPLE LOCAL begin radar 6300081 */
19001 /* This function builds a "generic" block struct type, to be passed
19002 into the debug information for blocks pointers, to allow gdb to
19003 find the actual function pointer for the block. Any time the Blocks
19004 structure layout changes, this may also need to change.
19006 Currently a block pointer is a pointer to a __block_literal_n struct,
19007 the third field of which is a pointer to a __block_descriptor struct,
19008 whose third field is the function pointer. There are other fields as
19009 well, but these are the ones gdb needs to know about to find the
19010 function pointer. Therefore a generic block struct currently looks
19013 struct __block_literal_generic
19019 struct __block_descriptor
19021 unsigned long int reserved;
19022 unsigned long int Size;
19026 IF AT ANY TIME THE STRUCTURE OF A __BLOCK_LITERAL_N CHANGES, THIS
19027 MUST BE CHANGED ALSO!!
19032 /* APPLE LOCAL radar 6353006 */
19033 c_build_generic_block_struct_type (void)
19035 tree fields = NULL_TREE;
19037 tree block_struct_type;
19039 push_to_top_level ();
19040 block_struct_type = xref_tag (record_type,
19041 get_identifier ("__block_literal_generic"),
19042 ts_current, false);
19043 xref_basetypes (block_struct_type, NULL_TREE);
19044 CLASSTYPE_DECLARED_CLASS (block_struct_type) = 0;
19045 pushclass (block_struct_type);
19047 field = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node);
19048 TREE_CHAIN (field) = fields;
19051 field = build_decl (FIELD_DECL, get_identifier ("__flags"),
19052 integer_type_node);
19053 TREE_CHAIN (field) = fields;
19056 field = build_decl (FIELD_DECL, get_identifier ("__reserved"),
19057 integer_type_node);
19058 TREE_CHAIN (field) = fields;
19061 field = build_decl (FIELD_DECL, get_identifier ("__FuncPtr"),
19063 TREE_CHAIN (field) = fields;
19066 field = build_decl (FIELD_DECL, get_identifier ("__descriptor"),
19067 build_block_descriptor_type (false));
19068 TREE_CHAIN (field) = fields;
19071 TYPE_FIELDS (block_struct_type) = fields;
19072 TYPE_NAME (block_struct_type) = build_decl (TYPE_DECL,
19073 get_identifier ("__block_literal_generic"),
19074 block_struct_type);
19075 TYPE_STUB_DECL (block_struct_type) = TYPE_NAME (block_struct_type);
19076 TYPE_BLOCK_IMPL_STRUCT (block_struct_type) = 1;
19077 finish_struct (block_struct_type, NULL_TREE);
19078 pop_from_top_level ();
19080 return block_struct_type;
19082 /* APPLE LOCAL end radar 6300081 */
19084 /** build_block_struct_type -
19085 struct __block_literal_n {
19086 void *__isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
19091 struct __block_descriptor {
19092 unsigned long int reserved; // NULL
19093 unsigned long int Size; // sizeof(struct __block_literal_n)
19095 // optional helper functions
19096 void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19097 void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19100 // imported variables
19101 int x; // ref variable list ...
19102 int *y; // byref variable list
19106 build_block_struct_type (struct block_sema_info * block_impl)
19108 tree fields = NULL_TREE, field, chain;
19110 static int unique_count;
19111 tree block_struct_type;
19113 /* Check and see if this block is required to have a Copy/Dispose
19114 helper function. If yes, set BlockHasCopyDispose to TRUE. */
19115 for (chain = block_impl->block_ref_decl_list; chain;
19116 chain = TREE_CHAIN (chain))
19117 /* APPLE LOCAL begin radar 6214617 */
19118 if (cp_block_requires_copying (TREE_VALUE (chain)))
19120 tree type = TREE_TYPE (TREE_VALUE (chain));
19121 block_impl->BlockHasCopyDispose = TRUE;
19122 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
19124 block_impl->BlockImportsCxxObjects = TRUE;
19127 /* APPLE LOCAL end radar 6214617 */
19130 /* Further check to see that we have __block variables which require
19131 Copy/Dispose helpers. */
19132 for (chain = block_impl->block_byref_decl_list; chain;
19133 chain = TREE_CHAIN (chain))
19134 if (COPYABLE_BYREF_LOCAL_VAR (TREE_VALUE (chain)))
19136 block_impl->BlockHasCopyDispose = TRUE;
19140 sprintf(buffer, "__block_literal_%d", ++unique_count);
19141 push_to_top_level ();
19142 /* APPLE LOCAL begin radar 6243400 */
19143 block_struct_type = xref_tag (record_type, get_identifier (buffer), ts_current, false);
19144 xref_basetypes (block_struct_type, NULL_TREE);
19145 CLASSTYPE_DECLARED_CLASS (block_struct_type) = 0;
19146 pushclass (block_struct_type);
19147 /* APPLE LOCAL end radar 6243400 */
19148 /* void * __isa; */
19149 field = build_decl (FIELD_DECL, get_identifier ("__isa"), ptr_type_node);
19150 TREE_CHAIN (field) = fields;
19154 field = build_decl (FIELD_DECL, get_identifier ("__flags"), integer_type_node);
19155 TREE_CHAIN (field) = fields;
19158 /* int __reserved. */
19159 field = build_decl (FIELD_DECL, get_identifier ("__reserved"), integer_type_node);
19160 TREE_CHAIN (field) = fields;
19163 /* void *__FuncPtr. */
19164 field = build_decl (FIELD_DECL, get_identifier ("__FuncPtr"),
19166 TREE_CHAIN (field) = fields;
19169 /* struct __block_descriptor *__descriptor */
19170 field = build_decl (FIELD_DECL, get_identifier ("__descriptor"),
19171 build_block_descriptor_type (block_impl->BlockHasCopyDispose));
19172 TREE_CHAIN (field) = fields;
19175 if (block_impl->BlockHasCopyDispose)
19177 /* If inner block of a nested block has BlockHasCopyDispose, so
19178 does its outer block. */
19179 if (block_impl->prev_block_info)
19180 block_impl->prev_block_info->BlockHasCopyDispose = TRUE;
19183 /* int x; // ref variable list ... */
19184 for (chain = block_impl->block_ref_decl_list; chain; chain = TREE_CHAIN (chain))
19186 tree p = TREE_VALUE (chain);
19187 /* Note! const-ness of copied in variable must not be carried over to the
19188 type of the synthesized struct field. It prevents to assign to this
19189 field when copy constructor is synthesized. */
19190 field = build_decl (FIELD_DECL, DECL_NAME (p),
19191 c_build_qualified_type (TREE_TYPE (p),
19192 TYPE_UNQUALIFIED));
19193 TREE_CHAIN (field) = fields;
19197 /* int *y; // byref variable list */
19198 for (chain = block_impl->block_byref_decl_list; chain; chain = TREE_CHAIN (chain))
19200 tree p = TREE_VALUE (chain);
19201 field = build_decl (FIELD_DECL, DECL_NAME (p),
19203 TREE_CHAIN (field) = fields;
19207 /* APPLE LOCAL begin radar 6243400 */
19208 TYPE_FIELDS (block_struct_type) = fields;
19209 TYPE_NAME (block_struct_type) =
19210 build_decl (TYPE_DECL, get_identifier (buffer), block_struct_type);
19211 TYPE_STUB_DECL (block_struct_type) = TYPE_NAME (block_struct_type);
19212 finish_struct (block_struct_type, NULL_TREE);
19213 pop_from_top_level ();
19214 /* APPLE LOCAL end radar 6243400 */
19215 return block_struct_type;
19219 build_block_struct_initlist - builds the initializer list:
19220 { &_NSConcreteStackBlock or &_NSConcreteGlobalBlock // __isa,
19221 BLOCK_USE_STRET | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL // __flags,
19223 &helper_1, // __FuncPtr,
19224 &static_descriptor_variable // __descriptor,
19225 x, // user variables.
19230 /* APPLE LOCAL begin radar 6169527 */
19231 /* This routine is entirely rewritten as we now have to deal with full-blown
19232 c++ classes with fields which may require construction. */
19233 static VEC(constructor_elt,gc) *
19234 build_block_struct_initlist (tree block_struct_type,
19235 struct block_sema_info *block_impl)
19237 tree expr, chain, helper_addr;
19238 /* APPLE LOCAL radar 7735196 */
19239 unsigned flags = 0;
19240 static tree NSConcreteStackBlock_decl = NULL_TREE;
19241 static tree NSConcreteGlobalBlock_decl = NULL_TREE;
19242 VEC(constructor_elt,gc) *impl_v = NULL;
19243 tree descriptor_block_decl = build_descriptor_block_decl (block_struct_type, block_impl);
19245 if (block_impl->BlockHasCopyDispose)
19246 /* Note! setting of this flag merely indicates to the runtime that
19247 we have destroy_helper_block/copy_helper_block helper
19249 flags |= BLOCK_HAS_COPY_DISPOSE;
19250 /* APPLE LOCAL begin radar 6214617 */
19251 /* Set BLOCK_HAS_CXX_OBJ if block is importing a cxx object. */
19252 if (block_impl->BlockImportsCxxObjects)
19253 flags |= BLOCK_HAS_CXX_OBJ;
19254 /* APPLE LOCAL end radar 6214617 */
19255 /* APPLE LOCAL begin radar 7735196 */
19256 if (block_impl->return_type && aggregate_value_p(block_impl->return_type, 0))
19257 flags |= BLOCK_USE_STRET;
19258 /* APPLE LOCAL end 7735196 */
19259 /* APPLE LOCAL begin radar 6230297 */
19260 if (!current_function_decl ||
19261 (block_impl->block_ref_decl_list == NULL_TREE &&
19262 block_impl->block_byref_decl_list == NULL_TREE))
19263 /* APPLE LOCAL end radar 6230297 */
19265 /* This is a global block. */
19266 /* Find an existing declaration for _NSConcreteGlobalBlock or declare
19267 extern void *_NSConcreteGlobalBlock; */
19268 if (NSConcreteGlobalBlock_decl == NULL_TREE)
19270 tree name_id = get_identifier("_NSConcreteGlobalBlock");
19271 NSConcreteGlobalBlock_decl = lookup_name (name_id);
19272 if (!NSConcreteGlobalBlock_decl)
19274 NSConcreteGlobalBlock_decl = build_decl (VAR_DECL, name_id, ptr_type_node);
19275 DECL_EXTERNAL (NSConcreteGlobalBlock_decl) = 1;
19276 TREE_PUBLIC (NSConcreteGlobalBlock_decl) = 1;
19277 pushdecl_top_level (NSConcreteGlobalBlock_decl);
19278 rest_of_decl_compilation (NSConcreteGlobalBlock_decl, 0, 0);
19281 /* APPLE LOCAL begin radar 6457359 */
19282 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE,
19283 convert (ptr_type_node,
19284 build_fold_addr_expr (NSConcreteGlobalBlock_decl)));
19285 /* APPLE LOCAL end radar 6457359 */
19286 flags |= BLOCK_IS_GLOBAL;
19290 /* Find an existing declaration for _NSConcreteStackBlock or declare
19291 extern void *_NSConcreteStackBlock; */
19292 if (NSConcreteStackBlock_decl == NULL_TREE)
19294 tree name_id = get_identifier("_NSConcreteStackBlock");
19295 NSConcreteStackBlock_decl = lookup_name (name_id);
19296 if (!NSConcreteStackBlock_decl)
19298 NSConcreteStackBlock_decl = build_decl (VAR_DECL, name_id, ptr_type_node);
19299 DECL_EXTERNAL (NSConcreteStackBlock_decl) = 1;
19300 TREE_PUBLIC (NSConcreteStackBlock_decl) = 1;
19301 pushdecl_top_level (NSConcreteStackBlock_decl);
19302 rest_of_decl_compilation (NSConcreteStackBlock_decl, 0, 0);
19305 /* APPLE LOCAL begin radar 6457359 */
19306 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE,
19307 convert (ptr_type_node,
19308 build_fold_addr_expr (NSConcreteStackBlock_decl)));
19309 /* APPLE LOCAL end radar 6457359 */
19313 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (integer_type_node, flags));
19315 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, build_int_cst (integer_type_node, 0));
19317 helper_addr = build_fold_addr_expr (block_impl->helper_func_decl);
19318 helper_addr = convert (ptr_type_node, helper_addr);
19319 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, helper_addr);
19321 /* &static_descriptor_variable initializer */
19322 expr = build_fold_addr_expr (descriptor_block_decl);
19323 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, expr);
19325 for (chain = block_impl->block_original_ref_decl_list; chain;
19326 chain = TREE_CHAIN (chain))
19328 tree y = TREE_VALUE (chain);
19330 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, y);
19332 for (chain = block_impl->block_byref_decl_list; chain;
19333 chain = TREE_CHAIN (chain))
19335 tree y = lookup_name (DECL_NAME (TREE_VALUE (chain)));
19336 tree forwarding_expr;
19339 if (COPYABLE_BYREF_LOCAL_VAR (y))
19341 /* For variables declared __block, either the original one
19342 at the point of declaration or the imported version (which is
19343 initialized in the helper function's prologue) is used to
19344 initilize the byref variable field in the temporary. */
19345 if (TREE_CODE (TREE_TYPE (y)) != RECORD_TYPE)
19346 y = build_indirect_ref (y, "unary *");
19347 /* We will be using the __block_struct_variable.__forwarding as the
19349 forwarding_expr = build_component_ref (y, get_identifier ("__forwarding"));
19352 /* Global variable is always assumed passed by its address. */
19353 forwarding_expr = build_fold_addr_expr (y);
19355 CONSTRUCTOR_APPEND_ELT(impl_v, NULL_TREE, forwarding_expr);
19359 /* APPLE LOCAL end radar 6169527 */
19360 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
19363 build_block_literal_tmp - This routine:
19365 1) builds block type:
19366 struct __block_literal_n {
19367 void *__isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
19372 struct __block_descriptor {
19373 unsigned long int reserved; // NULL
19374 unsigned long int Size; // sizeof(struct Block_literal_1)
19376 // optional helper functions
19377 void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19378 void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE
19381 // imported variables
19382 int x; // ref variable list ...
19383 int *y; // byref variable list
19386 2) build function prototype:
19387 double helper_1(struct block_1 *ii, int z);
19389 3) build the temporary initialization:
19390 struct block_1 I = {
19391 { &_NSConcreteStackBlock or &_NSConcreteGlobalBlock // isa,
19392 BLOCK_HAS_CXX_OBJ | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL // flags,
19397 sizeof(struct block_1),
19398 copy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
19399 destroy_helper_block_1, // only if block BLOCK_HAS_COPY_DISPOSE
19405 It return the temporary.
19407 /* APPLE LOCAL begin radar 6169527 */
19409 build_block_literal_tmp (const char *name,
19410 struct block_sema_info * block_impl)
19412 extern tree create_tmp_var_raw (tree, const char *);
19413 tree block_holder_tmp_decl;
19415 tree block_struct_type = TREE_TYPE (block_impl->block_arg_ptr_type);
19416 /* APPLE LOCAL begin radar 6230297 */
19417 bool staticBlockTmp = (block_impl->block_ref_decl_list == NULL_TREE &&
19418 block_impl->block_byref_decl_list == NULL_TREE);
19420 block_holder_tmp_decl = create_tmp_var_raw (block_struct_type, name);
19421 /* Context will not be known until when the literal is synthesized.
19422 This is more so in the case of nested block literal blocks. */
19423 maybe_push_decl (block_holder_tmp_decl);
19424 DECL_CONTEXT (block_holder_tmp_decl) = staticBlockTmp ? NULL_TREE
19425 : current_function_decl;
19426 if (staticBlockTmp)
19427 DECL_CONTEXT (block_impl->helper_func_decl) = NULL_TREE;
19428 /* APPLE LOCAL end radar 6230297 */
19429 DECL_ARTIFICIAL (block_holder_tmp_decl) = 1;
19431 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19432 constructor = make_node (CONSTRUCTOR);
19434 CONSTRUCTOR_ELTS (constructor) = build_block_struct_initlist (block_struct_type,
19436 /* Temporary representing a global block is made global static. */
19437 /* APPLE LOCAL radar 6230297 */
19438 if (staticBlockTmp || global_bindings_p ()) {
19439 TREE_PUBLIC (block_holder_tmp_decl) = 0;
19440 TREE_STATIC (block_holder_tmp_decl) = 1;
19442 cp_finish_decl (block_holder_tmp_decl, constructor, 0, 0, LOOKUP_ONLYCONVERTING);
19443 return block_holder_tmp_decl;
19445 /* APPLE LOCAL end radar 6169527 */
19448 clean_and_exit (tree block)
19450 pop_function_context ();
19451 pop_lang_context ();
19452 if (current_function_decl)
19453 free (finish_block (block));
19454 return error_mark_node;
19457 /** synth_copy_helper_block_func - This function synthesizes
19458 void copy_helper_block (struct block* _dest, struct block *_src) function.
19462 synth_copy_helper_block_func (struct block_sema_info * block_impl)
19465 tree dst_arg, src_arg;
19466 /* struct c_arg_info * arg_info; */
19467 /* Set up: (struct block* _dest, struct block *_src) parameters. */
19468 dst_arg = build_decl (PARM_DECL, get_identifier ("_dst"),
19469 block_impl->block_arg_ptr_type);
19470 DECL_CONTEXT (dst_arg) = cur_block->copy_helper_func_decl;
19471 TREE_USED (dst_arg) = 1;
19472 DECL_ARG_TYPE (dst_arg) = block_impl->block_arg_ptr_type;
19473 src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
19474 block_impl->block_arg_ptr_type);
19475 DECL_CONTEXT (src_arg) = cur_block->copy_helper_func_decl;
19476 TREE_USED (src_arg) = 1;
19477 DECL_ARG_TYPE (src_arg) = block_impl->block_arg_ptr_type;
19478 /* arg_info = xcalloc (1, sizeof (struct c_arg_info)); */
19479 TREE_CHAIN (dst_arg) = src_arg;
19481 pushdecl (cur_block->copy_helper_func_decl);
19482 /* arg_info->parms = dst_arg; */
19483 /* arg_info->types = tree_cons (NULL_TREE, block_impl->block_arg_ptr_type,
19484 tree_cons (NULL_TREE,
19485 block_impl->block_arg_ptr_type,
19487 DECL_ARGUMENTS (cur_block->copy_helper_func_decl) = dst_arg;
19488 /* function header synthesis. */
19489 push_function_context ();
19490 /* start_block_helper_function (cur_block->copy_helper_func_decl, true); */
19491 /* store_parm_decls (arg_info); */
19492 start_preparsed_function (cur_block->copy_helper_func_decl,
19493 /*attrs*/NULL_TREE,
19496 /* Body of the function. */
19497 stmt = begin_compound_stmt (BCS_FN_BODY);
19498 for (chain = block_impl->block_ref_decl_list; chain;
19499 chain = TREE_CHAIN (chain))
19500 /* APPLE LOCAL radar 6214617 */
19501 if (cp_block_requires_copying (TREE_VALUE (chain)))
19503 /* APPLE LOCAL begin radar 6175959 */
19505 tree p = TREE_VALUE (chain);
19506 tree dst_block_component, src_block_component;
19507 dst_block_component = build_component_ref (build_indirect_ref (dst_arg, "->"),
19509 src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19512 if (TREE_CODE (TREE_TYPE (p)) == BLOCK_POINTER_TYPE)
19513 /* _Block_object_assign(&_dest->myImportedBlock, _src->myImportedClosure, BLOCK_FIELD_IS_BLOCK) */
19514 flag = BLOCK_FIELD_IS_BLOCK;
19515 /* APPLE LOCAL begin radar 6214617 */
19516 else if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (p))
19517 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
19519 tree call_exp = build_aggr_init (dst_block_component, src_block_component,
19520 LOOKUP_ONLYCONVERTING);
19521 add_stmt (call_exp);
19523 /* APPLE LOCAL end radar 6214617 */
19525 /* _Block_object_assign(&_dest->myImportedBlock, _src->myImportedClosure, BLOCK_FIELD_IS_OBJECT) */
19526 flag = BLOCK_FIELD_IS_OBJECT;
19530 dst_block_component = build_fold_addr_expr (dst_block_component);
19531 call_exp = build_block_object_assign_call_exp (dst_block_component, src_block_component, flag);
19532 add_stmt (call_exp);
19534 /* APPLE LOCAL end radar 6175959 */
19537 /* For each __block declared variable used in |...| Must generate call to:
19538 _Block_object_assign(&_dest->myImportedBlock, _src->myImportedBlock, BLOCK_FIELD_IS_BYREF [|BLOCK_FIELD_IS_WEAK])
19540 for (chain = block_impl->block_byref_decl_list; chain;
19541 chain = TREE_CHAIN (chain))
19542 if (COPYABLE_BYREF_LOCAL_VAR (TREE_VALUE (chain)))
19544 int flag = BLOCK_FIELD_IS_BYREF;
19546 tree p = TREE_VALUE (chain);
19547 tree dst_block_component, src_block_component;
19548 dst_block_component = build_component_ref (build_indirect_ref (dst_arg, "->"),
19550 src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19553 /* _Block_object_assign(&_dest->myImportedClosure, _src->myImportedClosure, BLOCK_FIELD_IS_BYREF [|BLOCK_FIELD_IS_WEAK]) */
19554 if (COPYABLE_WEAK_BLOCK (p))
19555 flag |= BLOCK_FIELD_IS_WEAK;
19557 dst_block_component = build_fold_addr_expr (dst_block_component);
19558 call_exp = build_block_object_assign_call_exp (dst_block_component, src_block_component, flag);
19559 add_stmt (call_exp);
19562 finish_compound_stmt (stmt);
19563 /* APPLE LOCAL radar 6169580 */
19564 finish_function (4);
19565 /* Hum, would be nice if someone else did this for us. */
19566 if (global_bindings_p ())
19567 cgraph_finalize_function (block_impl->copy_helper_func_decl, false);
19568 pop_function_context ();
19569 /* free (arg_info); */
19573 synth_destroy_helper_block_func (struct block_sema_info * block_impl)
19577 /* struct c_arg_info * arg_info; */
19578 /* Set up: (struct block *_src) parameter. */
19579 src_arg = build_decl (PARM_DECL, get_identifier ("_src"),
19580 block_impl->block_arg_ptr_type);
19581 DECL_CONTEXT (src_arg) = cur_block->destroy_helper_func_decl;
19582 TREE_USED (src_arg) = 1;
19583 DECL_ARG_TYPE (src_arg) = block_impl->block_arg_ptr_type;
19584 /* arg_info = xcalloc (1, sizeof (struct c_arg_info)); */
19586 pushdecl (cur_block->destroy_helper_func_decl);
19587 /* arg_info->parms = src_arg; */
19588 /* arg_info->types = tree_cons (NULL_TREE, block_impl->block_arg_ptr_type,
19590 DECL_ARGUMENTS (cur_block->destroy_helper_func_decl) = src_arg;
19592 /* function header synthesis. */
19593 push_function_context ();
19594 /* start_block_helper_function (cur_block->destroy_helper_func_decl, true); */
19595 /* store_parm_decls_from (arg_info); */
19596 start_preparsed_function (cur_block->destroy_helper_func_decl,
19597 /*attrs*/NULL_TREE,
19600 /* Body of the function. */
19601 stmt = begin_compound_stmt (BCS_FN_BODY);
19602 for (chain = block_impl->block_ref_decl_list; chain;
19603 chain = TREE_CHAIN (chain))
19604 /* APPLE LOCAL begin radar 6214617 */
19605 if (block_requires_copying (TREE_VALUE (chain))
19606 || (TREE_CODE (TREE_TYPE (TREE_VALUE (chain))) == RECORD_TYPE
19607 && CLASSTYPE_DESTRUCTORS (TREE_TYPE (TREE_VALUE (chain)))))
19608 /* APPLE LOCAL end radar 6214617 */
19612 tree p = TREE_VALUE (chain);
19613 tree src_block_component;
19614 src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19617 if (TREE_CODE (TREE_TYPE (p)) == BLOCK_POINTER_TYPE)
19618 /* _Block_object_dispose(_src->imported_object_0, BLOCK_FIELD_IS_BLOCK); */
19619 flag = BLOCK_FIELD_IS_BLOCK;
19620 /* APPLE LOCAL begin radar 6214617 */
19621 else if (TREE_CODE (TREE_TYPE (p)) == RECORD_TYPE
19622 && CLASSTYPE_DESTRUCTORS (TREE_TYPE (p)))
19624 tree call_exp = cxx_maybe_build_cleanup (src_block_component);
19625 gcc_assert (call_exp);
19626 add_stmt (call_exp);
19628 /* APPLE LOCAL end radar 6214617 */
19630 /* _Block_object_dispose(_src->imported_object_0, BLOCK_FIELD_IS_OBJECT); */
19631 flag = BLOCK_FIELD_IS_OBJECT;
19634 rel_exp = build_block_object_dispose_call_exp (src_block_component, flag);
19635 add_stmt (rel_exp);
19639 /* For each __block declared variable used in |...| Must generate call to:
19640 _Block_object_dispose(_src->myImportedClosure, BLOCK_FIELD_IS_BYREF[|BLOCK_FIELD_IS_WEAK])
19642 for (chain = block_impl->block_byref_decl_list; chain;
19643 chain = TREE_CHAIN (chain))
19644 if (COPYABLE_BYREF_LOCAL_VAR (TREE_VALUE (chain)))
19647 int flag = BLOCK_FIELD_IS_BYREF;
19648 tree p = TREE_VALUE (chain);
19649 tree src_block_component;
19651 src_block_component = build_component_ref (build_indirect_ref (src_arg, "->"),
19653 if (COPYABLE_WEAK_BLOCK (p))
19654 flag |= BLOCK_FIELD_IS_WEAK;
19655 /* _Block_object_dispose(_src->myImportedClosure, BLOCK_FIELD_IS_BYREF[|BLOCK_FIELD_IS_WEAK]) */
19656 call_exp = build_block_object_dispose_call_exp (src_block_component, flag);
19657 add_stmt (call_exp);
19660 finish_compound_stmt (stmt);
19661 /* APPLE LOCAL radar 6169580 */
19662 finish_function (4);
19663 /* Hum, would be nice if someone else did this for us. */
19664 if (global_bindings_p ())
19665 cgraph_finalize_function (block_impl->destroy_helper_func_decl, false);
19666 pop_function_context ();
19669 /* Parse a block-id.
19674 type-specifier-seq block-declarator
19676 Returns the DECL specified or implied. */
19679 cp_parser_block_id (cp_parser* parser)
19681 cp_decl_specifier_seq type_specifier_seq;
19682 cp_declarator *declarator;
19684 /* Parse the type-specifier-seq. */
19685 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19686 &type_specifier_seq);
19687 if (type_specifier_seq.type == error_mark_node)
19688 return error_mark_node;
19690 /* Look for the block-declarator. */
19692 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_BLOCK, NULL,
19693 /*parenthesized_p=*/NULL,
19694 /*member_p=*/false);
19696 return grokblockdecl (&type_specifier_seq, declarator);
19699 /* Parse a block-literal-expr.
19703 block-literal-expr:
19704 ^ parameter-declation-clause exception-specification [opt] compound-statement
19705 ^ block-id compound-statement
19707 It synthesizes the helper function for later generation and builds
19708 the necessary data to represent the block literal where it is
19711 cp_parser_block_literal_expr (cp_parser* parser)
19714 static int global_unique_count;
19715 int unique_count = ++global_unique_count;
19716 tree block_helper_function_decl;
19717 tree expr, type, arglist = NULL_TREE, ftype;
19718 tree self_arg, stmt;
19719 /* struct c_arg_info *args = NULL; */
19720 cp_parameter_declarator *args = NULL;
19721 tree arg_type = void_list_node;
19722 struct block_sema_info *block_impl;
19726 tree helper_function_type;
19728 /* APPLE LOCAL radar 6185344 */
19729 tree declared_block_return_type = NULL_TREE;
19730 /* APPLE LOCAL radar 6237713 */
19731 tree attributes = NULL_TREE;
19732 /* APPLE LOCAL radar 6169580 */
19733 int context_is_nonstatic_method;
19734 tree raises = NULL_TREE;
19736 cp_lexer_consume_token (parser->lexer); /* eat '^' */
19738 /* APPLE LOCAL begin radar 6237713 */
19739 if (cp_lexer_peek_token (parser->lexer)->keyword == RID_ATTRIBUTE)
19740 attributes = cp_parser_attributes_opt (parser);
19741 /* APPLE LOCAL end radar 6237713 */
19743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19745 /* Parse the optional argument list */
19746 cp_lexer_consume_token (parser->lexer);
19747 /* Open the scope to collect parameter decls */
19748 /* push_scope (); */
19749 /* args = c_parser_parms_declarator (parser, true, NULL_TREE); */
19750 /* Parse the parameter-declaration-clause. */
19751 args = cp_parser_parameter_declaration_clause (parser);
19752 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19753 arg_type = grokparms (args, &arglist);
19754 /* Check for args as it might be NULL due to error. */
19757 return error_mark_node;
19759 raises = cp_parser_exception_specification_opt (parser);
19761 /* APPLE LOCAL begin radar 6185344 */
19762 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19764 /* Parse user declared return type. */
19767 /* APPLE LOCAL begin radar 6237713 */
19770 warning (0, "attributes before block type are ignored");
19771 attributes = NULL_TREE;
19773 /* APPLE LOCAL end radar 6237713 */
19775 decl = cp_parser_block_id (parser);
19777 if (decl && decl != error_mark_node)
19779 arg_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
19780 arglist = DECL_ARGUMENTS (decl);
19781 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
19782 declared_block_return_type = TREE_TYPE (TREE_TYPE (decl));
19785 /* APPLE LOCAL end radar 6185344 */
19787 block = begin_block ();
19788 /* APPLE LOCAL begin radar 6169580 */
19789 context_is_nonstatic_method = (current_function_decl
19790 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl));
19791 /* APPLE LOCAL end radar 6169580 */
19793 /* cur_block->arg_info = NULL; */
19794 /* APPLE LOCAL begin radar 6185344 */
19795 if (declared_block_return_type)
19797 cur_block->return_type = TYPE_MAIN_VARIANT (declared_block_return_type);
19798 cur_block->block_has_return_type = true;
19801 cur_block->return_type = NULL_TREE;
19802 /* APPLE LOCAL end radar 6185344 */
19804 /* Must also build hidden parameter .block_descriptor added to the helper
19805 function, even though we do not know its type yet. */
19806 /* APPLE LOCAL radar 6404979 */
19807 self_arg = build_artificial_parm (get_identifier (".block_descriptor"), ptr_type_node);
19809 /* TREE_CHAIN (self_arg) = cur_block->arg_info->parms; */
19810 TREE_CHAIN (self_arg) = arglist;
19811 arg_type = tree_cons (NULL_TREE, ptr_type_node, arg_type);
19812 arglist = self_arg;
19814 /* APPLE LOCAL begin radar 6185344 */
19815 /* Build the declaration of the helper function (if we do not know its result
19816 type yet, assume it is 'void'. If user provided it, use it).
19817 Treat this as a nested function and use nested function infrastructure for
19820 push_lang_context (lang_name_c);
19822 ftype = build_function_type ((!cur_block->block_has_return_type
19823 ? void_type_node : cur_block->return_type),
19825 /* APPLE LOCAL end radar 6185344 */
19827 ftype = build_exception_variant (ftype, raises);
19828 /* APPLE LOCAL radar 6160536 */
19829 block_helper_function_decl = build_helper_func_decl (build_block_helper_name (unique_count),
19831 DECL_CONTEXT (block_helper_function_decl) = current_function_decl;
19832 cur_block->helper_func_decl = block_helper_function_decl;
19834 DECL_ARGUMENTS (block_helper_function_decl) = arglist;
19836 push_function_context ();
19837 /* start_block_helper_function (cur_block->helper_func_decl, false); */
19838 /* Enter parameter list to the scope of the helper function. */
19839 /* store_parm_decls_from (cur_block->arg_info); */
19840 start_preparsed_function (cur_block->helper_func_decl,
19841 /*attrs*/NULL_TREE,
19843 /* APPLE LOCAL begin radar 6237713 */
19844 if (cp_lexer_peek_token (parser->lexer)->keyword == RID_ATTRIBUTE)
19845 attributes = cp_parser_attributes_opt (parser);
19846 /* APPLE LOCAL radar 6246527 */
19847 any_recognized_block_attribute (attributes);
19848 decl_attributes (&cur_block->helper_func_decl, attributes, 0);
19849 /* APPLE LOCAL end radar 6237713 */
19851 /* Start parsing body or expression part of the block literal. */
19853 unsigned save = parser->in_statement;
19854 /* Indicate no valid break/continue context. We'll notice and
19855 emit the proper error message in c_finish_bc_stmt. */
19856 parser->in_statement = 0;
19857 stmt = begin_compound_stmt (BCS_FN_BODY);
19858 /* Set block's scope to the scope of the helper function's main body.
19859 This is primarily used when nested blocks are declared. */
19860 cur_block->cp_the_scope = current_binding_level;
19861 /* APPLE LOCAL begin radar 6169580 */
19862 if (context_is_nonstatic_method)
19864 tree this_decl = lookup_name (this_identifier);
19865 gcc_assert (this_decl);
19866 build_block_ref_decl (this_identifier, this_decl);
19868 /* APPLE LOCAL end radar 6169580 */
19869 cp_parser_compound_statement (parser, NULL, false, false);
19870 parser->in_statement = save;
19873 cur_block->block_arg_ptr_type =
19874 build_pointer_type (build_block_struct_type (cur_block));
19876 restype = !cur_block->return_type ? void_type_node
19877 : cur_block->return_type;
19878 if (restype == error_mark_node)
19879 return clean_and_exit (block);
19881 /* Now that we know type of the hidden .block_descriptor argument, fix its type. */
19882 TREE_TYPE (self_arg) = cur_block->block_arg_ptr_type;
19883 DECL_ARG_TYPE (self_arg) = cur_block->block_arg_ptr_type;
19885 /* The DECL_RESULT should already have the correct type by now. */
19886 gcc_assert (TREE_TYPE (DECL_RESULT (current_function_decl))
19889 cur_block->block_body = stmt;
19890 block_build_prologue (cur_block);
19892 finish_compound_stmt (stmt);
19893 /* add_stmt (fnbody); */
19895 /* We are done parsing of the block body. Return type of block is now known.
19896 We also know all we need to know about the helper function. So, fix its
19898 /* We moved this here because for global blocks, helper function body is
19899 not nested and is gimplified in call to finish_function() and return type
19900 of the function must be correct. */
19901 ftype = build_function_type (restype, TREE_CHAIN (arg_type));
19903 ftype = build_exception_variant (ftype, raises);
19904 /* Declare helper function; as in:
19905 double helper_1(struct block_1 *ii, int z); */
19906 typelist = TYPE_ARG_TYPES (ftype);
19907 /* (struct block_1 *ii, int z, ...) */
19908 typelist = tree_cons (NULL_TREE, cur_block->block_arg_ptr_type,
19910 helper_function_type = build_function_type (TREE_TYPE (ftype), typelist);
19912 helper_function_type = build_exception_variant (helper_function_type, raises);
19913 TREE_TYPE (cur_block->helper_func_decl) = helper_function_type;
19914 finish_function (4);
19915 pop_function_context ();
19916 /* Hum, would be nice if someone else did this for us. */
19917 if (global_bindings_p ())
19918 cgraph_finalize_function (cur_block->helper_func_decl, false);
19919 pop_lang_context ();
19921 /* Build the declaration for copy_helper_block and destroy_helper_block
19922 helper functions for later use. */
19924 if (cur_block->BlockHasCopyDispose)
19928 push_lang_context (lang_name_c);
19929 /* void copy_helper_block (struct block*, struct block *); */
19930 s_ftype = build_function_type (void_type_node,
19931 tree_cons (NULL_TREE, cur_block->block_arg_ptr_type,
19932 tree_cons (NULL_TREE,
19933 cur_block->block_arg_ptr_type,
19935 sprintf (name, "__copy_helper_block_%d", unique_count);
19936 cur_block->copy_helper_func_decl =
19937 build_helper_func_decl (get_identifier (name), s_ftype);
19938 DECL_CONTEXT (cur_block->copy_helper_func_decl) = current_function_decl;
19939 synth_copy_helper_block_func (cur_block);
19941 /* void destroy_helper_block (struct block*); */
19942 s_ftype = build_function_type (void_type_node,
19943 tree_cons (NULL_TREE,
19944 cur_block->block_arg_ptr_type, void_list_node));
19945 sprintf (name, "__destroy_helper_block_%d", unique_count);
19946 cur_block->destroy_helper_func_decl =
19947 build_helper_func_decl (get_identifier (name), s_ftype);
19948 DECL_CONTEXT (cur_block->destroy_helper_func_decl) = current_function_decl;
19949 synth_destroy_helper_block_func (cur_block);
19950 pop_lang_context ();
19953 block_impl = finish_block (block);
19955 /* Build unqiue name of the temporary used in code gen. */
19956 sprintf (name, "__block_holder_tmp_%d", unique_count);
19957 tmp = build_block_literal_tmp (name, block_impl);
19958 tmp = build_fold_addr_expr (tmp);
19959 type = build_block_pointer_type (ftype);
19960 expr = convert (type, convert (ptr_type_node, tmp));
19964 /* APPLE LOCAL end blocks 6040305 (ce) */
19966 /* APPLE LOCAL begin blocks 6040305 (ch) */
19967 /* build_byref_local_var_access - converts EXPR to:
19968 EXPR.__forwarding-><decl-name>.
19971 build_byref_local_var_access (tree expr, tree decl_name)
19973 tree exp = build_component_ref (expr, get_identifier ("__forwarding"));
19974 exp = build_indirect_ref (exp, "unary *");
19975 exp = build_component_ref (exp, decl_name);
19979 #define BINDING_VALUE(b) ((b)->value)
19982 build_block_byref_decl - This routine inserts a variable declared as a
19983 'byref' variable using the |...| syntax in helper function's outer-most scope.
19986 build_block_byref_decl (tree name, tree decl, tree exp)
19988 tree ptr_type, byref_decl;
19989 /* APPLE LOCAL begin radar 6225809 */
19990 if (cur_block->prev_block_info) {
19991 /* Traverse enclosing blocks. Insert a __block variable in
19992 each enclosing block which has no declaration of this
19993 variable. This is to ensure that the current (inner) block
19994 gets the __block version of the variable; */
19995 struct block_sema_info *cb = cur_block->prev_block_info;
19997 struct cxx_binding *b = I_SYMBOL_BINDING (name);
19999 gcc_assert (BINDING_VALUE (b));
20000 gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20001 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20002 /* Find the first declaration not in current block. */
20003 while (b && BINDING_VALUE (b)
20004 && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20005 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20006 && DECL_CONTEXT (BINDING_VALUE (b)) == cur_block->helper_func_decl)
20008 /* FIXME: This can't happen?! */
20010 /* b = b->previous; */
20014 gcc_assert (BINDING_VALUE (b));
20015 gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20016 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20018 /* Is the next declaration not in the enclosing block? */
20019 if (b && BINDING_VALUE (b)
20020 && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20021 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20022 && DECL_CONTEXT (BINDING_VALUE (b)) != cb->helper_func_decl)
20024 /* No declaration of variable seen in the block. Must insert one. */
20025 /* FIXME: does this push enough? scope? */
20026 struct cp_binding_level *save_scope = current_binding_level;
20027 struct block_sema_info *save_current_block = cur_block;
20028 tree save_current_function_decl = current_function_decl;
20029 current_binding_level = cb->cp_the_scope;
20031 current_function_decl = cb->helper_func_decl;
20032 decl = build_block_byref_decl (name, decl, exp);
20033 cur_block = save_current_block;
20034 current_binding_level = save_scope;
20035 current_function_decl = save_current_function_decl;
20037 cb = cb->prev_block_info;
20040 /* APPLE LOCAL end radar 6225809 */
20042 /* If it is already a byref declaration, do not add the pointer type
20043 because such declarations already have the pointer type
20044 added. This happens when we have two nested byref declarations in
20046 ptr_type = (TREE_CODE (decl) == VAR_DECL && BLOCK_DECL_BYREF (decl))
20047 ? TREE_TYPE (decl) : build_pointer_type (TREE_TYPE (decl));
20048 byref_decl = build_decl (VAR_DECL, name, ptr_type);
20049 DECL_CONTEXT (byref_decl) = current_function_decl;
20050 BLOCK_DECL_BYREF (byref_decl) = 1;
20052 if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
20054 COPYABLE_BYREF_LOCAL_VAR (byref_decl) = 1;
20055 COPYABLE_BYREF_LOCAL_NONPOD (byref_decl) = COPYABLE_BYREF_LOCAL_NONPOD (decl);
20056 /* APPLE LOCAL radar 5847976 */
20057 COPYABLE_WEAK_BLOCK (byref_decl) = COPYABLE_WEAK_BLOCK (decl);
20060 /* Current scope must be that of the main function body. */
20061 /* FIXME gcc_assert (current_scope->function_body);*/
20062 pushdecl (byref_decl);
20063 mark_used (byref_decl);
20064 /* APPLE LOCAL begin radar 6083129 - byref escapes (cp) */
20065 /* FIXME: finish this off, ensure the decl is scoped appropriately
20066 for when we want the cleanup to run. */
20067 /* APPLE LOCAL end radar 6083129 - byref escapes (cp) */
20068 cur_block->block_byref_decl_list =
20069 tree_cons (NULL_TREE, byref_decl, cur_block->block_byref_decl_list);
20070 /* APPLE LOCAL radar 5847213 */
20071 /* build of block_original_byref_decl_list us removed. */
20072 /* APPLE LOCAL begin radar 6144664 */
20073 DECL_SOURCE_LOCATION (byref_decl)
20074 = DECL_SOURCE_LOCATION (cur_block->helper_func_decl);
20075 /* APPLE LOCAL end radar 6144664 */
20080 build_block_ref_decl - This routine inserts a copied-in variable (a variable
20081 referenced in the block but whose scope is outside the block) in helper
20082 function's outer-most scope. It also sets its type to 'const' as such
20083 variables are read-only.
20086 build_block_ref_decl (tree name, tree decl)
20088 /* FIXME - Broken, should be found via objc runtime testcases. */
20089 /* FIXME - Don't use DECL_CONTEXT on any helpers */
20091 /* APPLE LOCAL radar 6212722 */
20093 /* 'decl' was previously declared as __block. Simply, copy the value
20094 embedded in the above variable. */
20095 if (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))
20096 decl = build_byref_local_var_access (decl, DECL_NAME (decl));
20098 if (cur_block->prev_block_info) {
20099 /* Traverse enclosing blocks. Insert a copied-in variable in
20100 each enclosing block which has no declaration of this
20101 variable. This is to ensure that the current (inner) block
20102 has the 'frozen' value of the copied-in variable; which means
20103 the value of the copied in variable is at the point of the
20104 block declaration and *not* when the inner block is
20106 struct block_sema_info *cb = cur_block->prev_block_info;
20108 struct cxx_binding *b = I_SYMBOL_BINDING (name);
20110 gcc_assert (BINDING_VALUE (b));
20111 gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20112 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20113 /* Find the first declaration not in current block. */
20114 while (b && BINDING_VALUE (b)
20115 && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20116 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20117 && DECL_CONTEXT (BINDING_VALUE (b)) == cur_block->helper_func_decl)
20119 /* FIXME: This can't happen?! */
20121 /* b = b->previous; */
20125 gcc_assert (BINDING_VALUE (b));
20126 gcc_assert (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20127 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL);
20129 /* Is the next declaration not in the enclosing block? */
20130 if (b && BINDING_VALUE (b)
20131 && (TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20132 || TREE_CODE (BINDING_VALUE (b)) == PARM_DECL)
20133 && DECL_CONTEXT (BINDING_VALUE (b)) != cb->helper_func_decl)
20135 /* No declaration of variable seen in the block. Must
20136 insert one, so it 'freezes' the variable in this
20138 /* FIXME: does this push enough? scope? */
20139 struct cp_binding_level *save_scope = current_binding_level;
20140 struct block_sema_info *save_current_block = cur_block;
20141 tree save_current_function_decl = current_function_decl;
20142 current_binding_level = cb->cp_the_scope;
20144 current_function_decl = cb->helper_func_decl;
20145 decl = build_block_ref_decl (name, decl);
20146 cur_block = save_current_block;
20147 current_binding_level = save_scope;
20148 current_function_decl = save_current_function_decl;
20150 cb = cb->prev_block_info;
20154 /* APPLE LOCAL begin radar 6212722 */
20156 type = TREE_TYPE (exp);
20157 if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == FUNCTION_TYPE) {
20158 exp = decay_conversion (exp);
20159 type = TREE_TYPE (exp);
20161 ref_decl = build_decl (VAR_DECL, name,
20162 build_qualified_type (type, TYPE_QUAL_CONST));
20163 /* APPLE LOCAL end radar 6212722 */
20164 /* APPLE LOCAL begin radar 6144664 */
20165 DECL_SOURCE_LOCATION (ref_decl) = DECL_SOURCE_LOCATION
20166 (cur_block->helper_func_decl);
20167 /* APPLE LOCAL end radar 6144664 */
20168 DECL_CONTEXT (ref_decl) = current_function_decl;
20169 DECL_INITIAL (ref_decl) = error_mark_node;
20170 c_apply_type_quals_to_decl (TYPE_QUAL_CONST, ref_decl);
20171 BLOCK_DECL_COPIED (ref_decl) = 1;
20173 /* Find the scope for function body (outer-most scope) and insert
20174 this variable in that scope. This is to avoid duplicate
20175 declaration of the save variable. */
20177 struct cp_binding_level *b = current_binding_level;
20178 while (b->level_chain->kind != sk_function_parms)
20179 b = b->level_chain;
20180 pushdecl_with_scope (ref_decl, b, /*is_friend=*/false);
20181 /* APPLE LOCAL radar 6169527 */
20182 add_decl_expr (ref_decl);
20184 cur_block->block_ref_decl_list =
20185 tree_cons (NULL_TREE, ref_decl, cur_block->block_ref_decl_list);
20186 cur_block->block_original_ref_decl_list =
20187 /* APPLE LOCAL radar 6212722 */
20188 tree_cons (NULL_TREE, exp, cur_block->block_original_ref_decl_list);
20192 /* APPLE LOCAL begin radar 5847213 - radar 6329245 */
20193 static GTY (()) tree descriptor_ptr_type;
20194 static GTY (()) tree descriptor_ptr_type_with_copydispose;
20195 /** build_block_descriptor_type - This routine builds following internal type:
20196 struct __block_descriptor {
20197 unsigned long int reserved; // NULL
20198 unsigned long int Size; // sizeof(struct Block_literal_1)
20200 // optional helper functions
20201 void *CopyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
20202 void *DestroyFuncPtr; // When BLOCK_HAS_COPY_DISPOSE is set (withCopyDispose true)
20203 } *descriptor_ptr_type;
20205 Objects of this type will always be static. This is one main component of abi change.
20208 build_block_descriptor_type (bool withCopyDispose)
20210 tree field_decl_chain = NULL_TREE, field_decl;
20213 if (withCopyDispose && descriptor_ptr_type_with_copydispose)
20214 return descriptor_ptr_type_with_copydispose;
20215 if (!withCopyDispose && descriptor_ptr_type)
20216 return descriptor_ptr_type;
20218 main_type = make_aggr_type (RECORD_TYPE);
20219 xref_basetypes (main_type, NULL_TREE);
20221 /* unsigned long int reserved; */
20222 field_decl = build_decl (FIELD_DECL, get_identifier ("reserved"), long_unsigned_type_node);
20223 TREE_CHAIN (field_decl) = field_decl_chain;
20224 field_decl_chain = field_decl;
20226 /* unsigned long int Size; */
20227 field_decl = build_decl (FIELD_DECL, get_identifier ("Size"), long_unsigned_type_node);
20228 TREE_CHAIN (field_decl) = field_decl_chain;
20229 field_decl_chain = field_decl;
20231 if (withCopyDispose)
20233 /* void *CopyFuncPtr; */
20234 field_decl = build_decl (FIELD_DECL, get_identifier ("CopyFuncPtr"), ptr_type_node);
20235 TREE_CHAIN (field_decl) = field_decl_chain;
20236 field_decl_chain = field_decl;
20237 /* void *DestroyFuncPtr; */
20238 field_decl = build_decl (FIELD_DECL, get_identifier ("DestroyFuncPtr"), ptr_type_node);
20239 TREE_CHAIN (field_decl) = field_decl_chain;
20240 field_decl_chain = field_decl;
20243 /* Mark this struct as being a block struct rather than a 'normal'
20245 TYPE_BLOCK_IMPL_STRUCT (main_type) = 1;
20246 if (withCopyDispose)
20247 finish_builtin_struct (main_type, "__block_descriptor_withcopydispose", field_decl_chain, NULL_TREE);
20249 finish_builtin_struct (main_type, "__block_descriptor", field_decl_chain, NULL_TREE);
20250 CLASSTYPE_AS_BASE (main_type) = main_type;
20252 main_type = build_pointer_type (main_type);
20253 if (withCopyDispose)
20254 descriptor_ptr_type_with_copydispose = main_type;
20256 descriptor_ptr_type = main_type;
20259 /* APPLE LOCAL end radar 5847213 - radar 6329245 */
20262 make_block_pointer_declarator (tree attributes,
20264 cp_declarator *target)
20266 struct cp_declarator *itarget = target;
20267 struct cp_declarator *ret = make_declarator (cdk_block_pointer);
20269 /* APPLE LOCAL radar 5847213 */
20273 ret->attributes = attributes;
20274 ret->declarator = itarget;
20275 ret->u.block_pointer.qualifiers = quals;
20279 /* This routine returns 'true' if 'name' has a declaration inside the
20280 current block, 'false' otherwise. If 'name' has no declaration in
20281 the current block, it returns in DECL the user declaration for
20282 'name' found in the enclosing scope. Note that if it is declared
20283 in current declaration, it can be either a user declaration or a
20284 byref/copied-in declaration added in current block's scope by the
20287 lookup_name_in_block (tree name, tree *decl)
20289 /* FIXME - Broken, should be found via objc runtime testcases. */
20290 /* FIXME - Don't use DECL_CONTEXT on any helpers */
20291 cxx_binding *b = I_SYMBOL_BINDING (name);
20292 if (b && b->declared_in_block
20293 && DECL_CONTEXT (BINDING_VALUE (b)) == current_function_decl)
20296 /* Check for variables only, as we may have parameters, such as
20298 /* Note that if a copied-in variable (BLOCK_DECL_COPIED) in the
20299 enclosing block is found, it must be returned as this is
20300 where the variable in current (nested block) will have to get
20303 && TREE_CODE (BINDING_VALUE (b)) == VAR_DECL
20304 && (BLOCK_DECL_BYREF (BINDING_VALUE (b))))
20307 *decl = BINDING_VALUE (b);
20312 build_helper_func_decl - This routine builds a FUNCTION_DECL for
20313 a block helper function.
20316 build_helper_func_decl (tree ident, tree type)
20318 tree func_decl = build_decl (FUNCTION_DECL, ident, type);
20319 DECL_EXTERNAL (func_decl) = 0;
20320 TREE_PUBLIC (func_decl) = 0;
20321 TREE_USED (func_decl) = 1;
20322 TREE_NOTHROW (func_decl) = 0;
20323 /* APPLE LOCAL radar 6172148 */
20324 BLOCK_SYNTHESIZED_FUNC (func_decl) = 1;
20325 retrofit_lang_decl (func_decl);
20326 if (current_function_decl)
20327 DECL_NO_STATIC_CHAIN (current_function_decl) = 0;
20332 declare_block_prologue_local_vars - utility routine to do the actual
20333 declaration and initialization for each referecned block variable.
20335 /* APPLE LOCAL begin radar 6169527 */
20336 /* This routine is mostly rewritten for c++ because initialization of variables
20337 may involve copy construction. */
20339 declare_block_prologue_local_vars (tree self_parm, tree component,
20342 tree decl, block_component;
20343 tree_stmt_iterator i;
20344 tree initialization_stmt;
20345 /* APPLE LOCAL radar 6163705 */
20346 int save_line = LOCATION_LINE (input_location);
20349 block_component = build_component_ref (build_indirect_ref (self_parm, "->"),
20350 DECL_NAME (component));
20351 gcc_assert (block_component);
20352 /* APPLE LOCAL radar 6163705 */
20353 LOCATION_LINE (input_location) = DECL_SOURCE_LINE (decl) - 1;
20354 DECL_EXTERNAL (decl) = 0;
20355 TREE_STATIC (decl) = 0;
20356 TREE_USED (decl) = 1;
20357 DECL_CONTEXT (decl) = current_function_decl;
20358 DECL_ARTIFICIAL (decl) = 1;
20359 initialization_stmt = push_stmt_list();
20360 cp_finish_decl (decl, block_component, 0, 0, LOOKUP_ONLYCONVERTING);
20361 initialization_stmt = pop_stmt_list (initialization_stmt);
20362 /* APPLE LOCAL radar 6163705 */
20363 LOCATION_LINE (input_location) = save_line;
20364 /* Prepend a initialization_stmt statement to the statement list. */
20365 i = tsi_start (stmt);
20366 tsi_link_before (&i, initialization_stmt, TSI_SAME_STMT);
20370 declare_block_prologue_local_byref_vars - utility routine to do the actual
20371 declaration and initialization for each __block referenced block variable.
20374 declare_block_prologue_local_byref_vars (tree self_parm, tree component,
20377 tree decl, block_component;
20378 tree_stmt_iterator i;
20382 block_component = build_component_ref (build_indirect_ref (self_parm, "->"),
20383 DECL_NAME (component));
20384 gcc_assert (block_component);
20385 DECL_EXTERNAL (decl) = 0;
20386 TREE_STATIC (decl) = 0;
20387 TREE_USED (decl) = 1;
20388 DECL_CONTEXT (decl) = current_function_decl;
20389 DECL_ARTIFICIAL (decl) = 1;
20390 DECL_INITIAL (decl) = block_component;
20391 /* Prepend a DECL_EXPR statement to the statement list. */
20392 i = tsi_start (stmt);
20393 decl_stmt = build_stmt (DECL_EXPR, decl);
20394 SET_EXPR_LOCATION (decl_stmt, DECL_SOURCE_LOCATION (decl));
20395 /* APPLE LOCAL begin radar 6163705, Blocks prologues */
20396 /* Give the prologue statements a line number of one before the beginning of
20397 the function, to make them easily identifiable later. */
20398 EXPR_LINENO (decl_stmt) = DECL_SOURCE_LINE (decl) - 1;
20399 /* APPLE LOCAL end radar 6163705, Blocks prologues */
20400 decl_stmt = build3 (BIND_EXPR, void_type_node, decl, decl_stmt, NULL);
20401 TREE_SIDE_EFFECTS (decl_stmt) = 1;
20403 tsi_link_before (&i, decl_stmt, TSI_SAME_STMT);
20405 /* APPLE LOCAL end radar 6169527 */
20408 block_build_prologue
20409 - This routine builds the declarations for the
20410 variables referenced in the block; as in:
20411 int *y = .block_descriptor->y;
20412 int x = .block_descriptor->x;
20414 The decl_expr declaration for each initialization is enterred at the
20415 beginning of the helper function's statement-list which is passed
20416 in block_impl->block_body.
20419 block_build_prologue (struct block_sema_info *block_impl)
20422 tree self_parm = lookup_name (get_identifier (".block_descriptor"));
20423 gcc_assert (self_parm);
20425 for (chain = block_impl->block_ref_decl_list; chain;
20426 chain = TREE_CHAIN (chain))
20427 declare_block_prologue_local_vars (self_parm, TREE_VALUE (chain),
20428 block_impl->block_body);
20429 /* APPLE LOCAL begin radar 6169527 */
20430 for (chain = block_impl->block_byref_decl_list; chain;
20431 chain = TREE_CHAIN (chain))
20432 declare_block_prologue_local_byref_vars (self_parm, TREE_VALUE (chain),
20433 block_impl->block_body);
20434 /* APPLE LOCAL end radar 6169527 */
20436 /* APPLE LOCAL end blocks 6040305 (ch) */
20438 /* OpenMP 2.5 parsing routines. */
20440 /* All OpenMP clauses. OpenMP 2.5. */
20441 typedef enum pragma_omp_clause {
20442 PRAGMA_OMP_CLAUSE_NONE = 0,
20444 PRAGMA_OMP_CLAUSE_COPYIN,
20445 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
20446 PRAGMA_OMP_CLAUSE_DEFAULT,
20447 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
20448 PRAGMA_OMP_CLAUSE_IF,
20449 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
20450 PRAGMA_OMP_CLAUSE_NOWAIT,
20451 PRAGMA_OMP_CLAUSE_NUM_THREADS,
20452 PRAGMA_OMP_CLAUSE_ORDERED,
20453 PRAGMA_OMP_CLAUSE_PRIVATE,
20454 PRAGMA_OMP_CLAUSE_REDUCTION,
20455 PRAGMA_OMP_CLAUSE_SCHEDULE,
20456 PRAGMA_OMP_CLAUSE_SHARED
20457 } pragma_omp_clause;
20459 /* Returns name of the next clause.
20460 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20461 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20462 returned and the token is consumed. */
20464 static pragma_omp_clause
20465 cp_parser_omp_clause_name (cp_parser *parser)
20467 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20470 result = PRAGMA_OMP_CLAUSE_IF;
20471 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20472 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20473 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20474 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20475 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20478 const char *p = IDENTIFIER_POINTER (id);
20483 if (!strcmp ("copyin", p))
20484 result = PRAGMA_OMP_CLAUSE_COPYIN;
20485 else if (!strcmp ("copyprivate", p))
20486 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20489 if (!strcmp ("firstprivate", p))
20490 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20493 if (!strcmp ("lastprivate", p))
20494 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20497 if (!strcmp ("nowait", p))
20498 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20499 else if (!strcmp ("num_threads", p))
20500 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20503 if (!strcmp ("ordered", p))
20504 result = PRAGMA_OMP_CLAUSE_ORDERED;
20507 if (!strcmp ("reduction", p))
20508 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20511 if (!strcmp ("schedule", p))
20512 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20513 else if (!strcmp ("shared", p))
20514 result = PRAGMA_OMP_CLAUSE_SHARED;
20519 if (result != PRAGMA_OMP_CLAUSE_NONE)
20520 cp_lexer_consume_token (parser->lexer);
20525 /* Validate that a clause of the given type does not already exist. */
20528 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
20532 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20533 if (OMP_CLAUSE_CODE (c) == code)
20535 error ("too many %qs clauses", name);
20543 variable-list , identifier
20545 In addition, we match a closing parenthesis. An opening parenthesis
20546 will have been consumed by the caller.
20548 If KIND is nonzero, create the appropriate node and install the decl
20549 in OMP_CLAUSE_DECL and add the node to the head of the list.
20551 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20552 return the list created. */
20555 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20562 name = cp_parser_id_expression (parser, /*template_p=*/false,
20563 /*check_dependency_p=*/true,
20564 /*template_p=*/NULL,
20565 /*declarator_p=*/false,
20566 /*optional_p=*/false);
20567 if (name == error_mark_node)
20570 decl = cp_parser_lookup_name_simple (parser, name);
20571 if (decl == error_mark_node)
20572 cp_parser_name_lookup_error (parser, name, decl, NULL);
20573 else if (kind != 0)
20575 tree u = build_omp_clause (kind);
20576 OMP_CLAUSE_DECL (u) = decl;
20577 OMP_CLAUSE_CHAIN (u) = list;
20581 list = tree_cons (decl, NULL_TREE, list);
20584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20586 cp_lexer_consume_token (parser->lexer);
20589 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20593 /* Try to resync to an unnested comma. Copied from
20594 cp_parser_parenthesized_expression_list. */
20596 ending = cp_parser_skip_to_closing_parenthesis (parser,
20597 /*recovering=*/true,
20599 /*consume_paren=*/true);
20607 /* Similarly, but expect leading and trailing parenthesis. This is a very
20608 common case for omp clauses. */
20611 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20613 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20614 return cp_parser_omp_var_list_no_open (parser, kind, list);
20619 default ( shared | none ) */
20622 cp_parser_omp_clause_default (cp_parser *parser, tree list)
20624 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20627 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20631 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20632 const char *p = IDENTIFIER_POINTER (id);
20637 if (strcmp ("none", p) != 0)
20639 kind = OMP_CLAUSE_DEFAULT_NONE;
20643 if (strcmp ("shared", p) != 0)
20645 kind = OMP_CLAUSE_DEFAULT_SHARED;
20652 cp_lexer_consume_token (parser->lexer);
20657 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20662 /*or_comma=*/false,
20663 /*consume_paren=*/true);
20665 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20668 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
20669 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20670 OMP_CLAUSE_CHAIN (c) = list;
20671 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20677 if ( expression ) */
20680 cp_parser_omp_clause_if (cp_parser *parser, tree list)
20684 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20687 t = cp_parser_condition (parser);
20689 if (t == error_mark_node
20690 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20692 /*or_comma=*/false,
20693 /*consume_paren=*/true);
20695 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
20697 c = build_omp_clause (OMP_CLAUSE_IF);
20698 OMP_CLAUSE_IF_EXPR (c) = t;
20699 OMP_CLAUSE_CHAIN (c) = list;
20708 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
20712 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
20714 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20715 OMP_CLAUSE_CHAIN (c) = list;
20720 num_threads ( expression ) */
20723 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
20727 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20730 t = cp_parser_expression (parser, false);
20732 if (t == error_mark_node
20733 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20735 /*or_comma=*/false,
20736 /*consume_paren=*/true);
20738 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
20740 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20741 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20742 OMP_CLAUSE_CHAIN (c) = list;
20751 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
20755 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
20757 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20758 OMP_CLAUSE_CHAIN (c) = list;
20763 reduction ( reduction-operator : variable-list )
20765 reduction-operator:
20766 One of: + * - & ^ | && || */
20769 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20771 enum tree_code code;
20774 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20777 switch (cp_lexer_peek_token (parser->lexer)->type)
20789 code = BIT_AND_EXPR;
20792 code = BIT_XOR_EXPR;
20795 code = BIT_IOR_EXPR;
20798 code = TRUTH_ANDIF_EXPR;
20801 code = TRUTH_ORIF_EXPR;
20804 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
20806 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20807 /*or_comma=*/false,
20808 /*consume_paren=*/true);
20811 cp_lexer_consume_token (parser->lexer);
20813 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
20816 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20817 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20818 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20824 schedule ( schedule-kind )
20825 schedule ( schedule-kind , expression )
20828 static | dynamic | guided | runtime */
20831 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
20835 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
20838 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20840 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20842 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20843 const char *p = IDENTIFIER_POINTER (id);
20848 if (strcmp ("dynamic", p) != 0)
20850 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20854 if (strcmp ("guided", p) != 0)
20856 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20860 if (strcmp ("runtime", p) != 0)
20862 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20869 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20870 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20873 cp_lexer_consume_token (parser->lexer);
20875 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20877 cp_lexer_consume_token (parser->lexer);
20879 t = cp_parser_assignment_expression (parser, false);
20881 if (t == error_mark_node)
20883 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20884 error ("schedule %<runtime%> does not take "
20885 "a %<chunk_size%> parameter");
20887 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20889 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20892 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
20895 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
20896 OMP_CLAUSE_CHAIN (c) = list;
20900 cp_parser_error (parser, "invalid schedule kind");
20902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20903 /*or_comma=*/false,
20904 /*consume_paren=*/true);
20908 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20909 is a bitmask in MASK. Return the list of clauses found; the result
20910 of clause default goes in *pdefault. */
20913 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20914 const char *where, cp_token *pragma_tok)
20916 tree clauses = NULL;
20918 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20920 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
20921 const char *c_name;
20922 tree prev = clauses;
20926 case PRAGMA_OMP_CLAUSE_COPYIN:
20927 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20930 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20931 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20933 c_name = "copyprivate";
20935 case PRAGMA_OMP_CLAUSE_DEFAULT:
20936 clauses = cp_parser_omp_clause_default (parser, clauses);
20937 c_name = "default";
20939 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20940 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20942 c_name = "firstprivate";
20944 case PRAGMA_OMP_CLAUSE_IF:
20945 clauses = cp_parser_omp_clause_if (parser, clauses);
20948 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20949 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20951 c_name = "lastprivate";
20953 case PRAGMA_OMP_CLAUSE_NOWAIT:
20954 clauses = cp_parser_omp_clause_nowait (parser, clauses);
20957 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20958 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
20959 c_name = "num_threads";
20961 case PRAGMA_OMP_CLAUSE_ORDERED:
20962 clauses = cp_parser_omp_clause_ordered (parser, clauses);
20963 c_name = "ordered";
20965 case PRAGMA_OMP_CLAUSE_PRIVATE:
20966 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20968 c_name = "private";
20970 case PRAGMA_OMP_CLAUSE_REDUCTION:
20971 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20972 c_name = "reduction";
20974 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20975 clauses = cp_parser_omp_clause_schedule (parser, clauses);
20976 c_name = "schedule";
20978 case PRAGMA_OMP_CLAUSE_SHARED:
20979 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20984 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20988 if (((mask >> c_kind) & 1) == 0)
20990 /* Remove the invalid clause(s) from the list to avoid
20991 confusing the rest of the compiler. */
20993 error ("%qs is not valid for %qs", c_name, where);
20997 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20998 return finish_omp_clauses (clauses);
21005 In practice, we're also interested in adding the statement to an
21006 outer node. So it is convenient if we work around the fact that
21007 cp_parser_statement calls add_stmt. */
21010 cp_parser_begin_omp_structured_block (cp_parser *parser)
21012 unsigned save = parser->in_statement;
21014 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21015 This preserves the "not within loop or switch" style error messages
21016 for nonsense cases like
21022 if (parser->in_statement)
21023 parser->in_statement = IN_OMP_BLOCK;
21029 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21031 parser->in_statement = save;
21035 cp_parser_omp_structured_block (cp_parser *parser)
21037 tree stmt = begin_omp_structured_block ();
21038 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21040 cp_parser_statement (parser, NULL_TREE, false, NULL);
21042 cp_parser_end_omp_structured_block (parser, save);
21043 return finish_omp_structured_block (stmt);
21047 # pragma omp atomic new-line
21051 x binop= expr | x++ | ++x | x-- | --x
21053 +, *, -, /, &, ^, |, <<, >>
21055 where x is an lvalue expression with scalar type. */
21058 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21061 enum tree_code code;
21063 cp_parser_require_pragma_eol (parser, pragma_tok);
21065 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21067 switch (TREE_CODE (lhs))
21072 case PREINCREMENT_EXPR:
21073 case POSTINCREMENT_EXPR:
21074 lhs = TREE_OPERAND (lhs, 0);
21076 rhs = integer_one_node;
21079 case PREDECREMENT_EXPR:
21080 case POSTDECREMENT_EXPR:
21081 lhs = TREE_OPERAND (lhs, 0);
21083 rhs = integer_one_node;
21087 switch (cp_lexer_peek_token (parser->lexer)->type)
21093 code = TRUNC_DIV_EXPR;
21101 case CPP_LSHIFT_EQ:
21102 code = LSHIFT_EXPR;
21104 case CPP_RSHIFT_EQ:
21105 code = RSHIFT_EXPR;
21108 code = BIT_AND_EXPR;
21111 code = BIT_IOR_EXPR;
21114 code = BIT_XOR_EXPR;
21117 cp_parser_error (parser,
21118 "invalid operator for %<#pragma omp atomic%>");
21121 cp_lexer_consume_token (parser->lexer);
21123 rhs = cp_parser_expression (parser, false);
21124 if (rhs == error_mark_node)
21128 finish_omp_atomic (code, lhs, rhs);
21129 cp_parser_consume_semicolon_at_end_of_statement (parser);
21133 cp_parser_skip_to_end_of_block_or_statement (parser);
21138 # pragma omp barrier new-line */
21141 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21143 cp_parser_require_pragma_eol (parser, pragma_tok);
21144 finish_omp_barrier ();
21148 # pragma omp critical [(name)] new-line
21149 structured-block */
21152 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21154 tree stmt, name = NULL;
21156 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21158 cp_lexer_consume_token (parser->lexer);
21160 name = cp_parser_identifier (parser);
21162 if (name == error_mark_node
21163 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
21164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21165 /*or_comma=*/false,
21166 /*consume_paren=*/true);
21167 if (name == error_mark_node)
21170 cp_parser_require_pragma_eol (parser, pragma_tok);
21172 stmt = cp_parser_omp_structured_block (parser);
21173 return c_finish_omp_critical (stmt, name);
21178 # pragma omp flush flush-vars[opt] new-line
21181 ( variable-list ) */
21184 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21186 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21187 (void) cp_parser_omp_var_list (parser, 0, NULL);
21188 cp_parser_require_pragma_eol (parser, pragma_tok);
21190 finish_omp_flush ();
21193 /* Parse the restricted form of the for statment allowed by OpenMP. */
21196 cp_parser_omp_for_loop (cp_parser *parser)
21198 tree init, cond, incr, body, decl, pre_body;
21201 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21203 cp_parser_error (parser, "for statement expected");
21206 loc = cp_lexer_consume_token (parser->lexer)->location;
21207 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
21210 init = decl = NULL;
21211 pre_body = push_stmt_list ();
21212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21214 cp_decl_specifier_seq type_specifiers;
21216 /* First, try to parse as an initialized declaration. See
21217 cp_parser_condition, from whence the bulk of this is copied. */
21219 cp_parser_parse_tentatively (parser);
21220 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21222 if (!cp_parser_error_occurred (parser))
21224 tree asm_specification, attributes;
21225 cp_declarator *declarator;
21227 declarator = cp_parser_declarator (parser,
21228 CP_PARSER_DECLARATOR_NAMED,
21229 /*ctor_dtor_or_conv_p=*/NULL,
21230 /*parenthesized_p=*/NULL,
21231 /*member_p=*/false);
21232 attributes = cp_parser_attributes_opt (parser);
21233 asm_specification = cp_parser_asm_specification_opt (parser);
21235 cp_parser_require (parser, CPP_EQ, "`='");
21236 if (cp_parser_parse_definitely (parser))
21240 decl = start_decl (declarator, &type_specifiers,
21241 /*initialized_p=*/false, attributes,
21242 /*prefix_attributes=*/NULL_TREE,
21245 init = cp_parser_assignment_expression (parser, false);
21247 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
21248 asm_specification, LOOKUP_ONLYCONVERTING);
21251 pop_scope (pushed_scope);
21255 cp_parser_abort_tentative_parse (parser);
21257 /* If parsing as an initialized declaration failed, try again as
21258 a simple expression. */
21260 init = cp_parser_expression (parser, false);
21262 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
21263 pre_body = pop_stmt_list (pre_body);
21266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21267 cond = cp_parser_condition (parser);
21268 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
21271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21272 incr = cp_parser_expression (parser, false);
21274 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
21275 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21276 /*or_comma=*/false,
21277 /*consume_paren=*/true);
21279 /* Note that we saved the original contents of this flag when we entered
21280 the structured block, and so we don't need to re-save it here. */
21281 parser->in_statement = IN_OMP_FOR;
21283 /* Note that the grammar doesn't call for a structured block here,
21284 though the loop as a whole is a structured block. */
21285 body = push_stmt_list ();
21286 cp_parser_statement (parser, NULL_TREE, false, NULL);
21287 body = pop_stmt_list (body);
21289 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
21293 #pragma omp for for-clause[optseq] new-line
21296 #define OMP_FOR_CLAUSE_MASK \
21297 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21298 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21299 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21300 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21301 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21302 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21303 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21306 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21308 tree clauses, sb, ret;
21311 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21312 "#pragma omp for", pragma_tok);
21314 sb = begin_omp_structured_block ();
21315 save = cp_parser_begin_omp_structured_block (parser);
21317 ret = cp_parser_omp_for_loop (parser);
21319 OMP_FOR_CLAUSES (ret) = clauses;
21321 cp_parser_end_omp_structured_block (parser, save);
21322 add_stmt (finish_omp_structured_block (sb));
21328 # pragma omp master new-line
21329 structured-block */
21332 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21334 cp_parser_require_pragma_eol (parser, pragma_tok);
21335 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21339 # pragma omp ordered new-line
21340 structured-block */
21343 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21345 cp_parser_require_pragma_eol (parser, pragma_tok);
21346 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21352 { section-sequence }
21355 section-directive[opt] structured-block
21356 section-sequence section-directive structured-block */
21359 cp_parser_omp_sections_scope (cp_parser *parser)
21361 tree stmt, substmt;
21362 bool error_suppress = false;
21365 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
21368 stmt = push_stmt_list ();
21370 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21374 substmt = begin_omp_structured_block ();
21375 save = cp_parser_begin_omp_structured_block (parser);
21379 cp_parser_statement (parser, NULL_TREE, false, NULL);
21381 tok = cp_lexer_peek_token (parser->lexer);
21382 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21384 if (tok->type == CPP_CLOSE_BRACE)
21386 if (tok->type == CPP_EOF)
21390 cp_parser_end_omp_structured_block (parser, save);
21391 substmt = finish_omp_structured_block (substmt);
21392 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21393 add_stmt (substmt);
21398 tok = cp_lexer_peek_token (parser->lexer);
21399 if (tok->type == CPP_CLOSE_BRACE)
21401 if (tok->type == CPP_EOF)
21404 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21406 cp_lexer_consume_token (parser->lexer);
21407 cp_parser_require_pragma_eol (parser, tok);
21408 error_suppress = false;
21410 else if (!error_suppress)
21412 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21413 error_suppress = true;
21416 substmt = cp_parser_omp_structured_block (parser);
21417 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21418 add_stmt (substmt);
21420 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21422 substmt = pop_stmt_list (stmt);
21424 stmt = make_node (OMP_SECTIONS);
21425 TREE_TYPE (stmt) = void_type_node;
21426 OMP_SECTIONS_BODY (stmt) = substmt;
21433 # pragma omp sections sections-clause[optseq] newline
21436 #define OMP_SECTIONS_CLAUSE_MASK \
21437 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21438 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21439 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21440 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21441 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21444 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21448 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21449 "#pragma omp sections", pragma_tok);
21451 ret = cp_parser_omp_sections_scope (parser);
21453 OMP_SECTIONS_CLAUSES (ret) = clauses;
21459 # pragma parallel parallel-clause new-line
21460 # pragma parallel for parallel-for-clause new-line
21461 # pragma parallel sections parallel-sections-clause new-line */
21463 #define OMP_PARALLEL_CLAUSE_MASK \
21464 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21465 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21466 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21467 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21468 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21469 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21470 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21471 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21474 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21476 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21477 const char *p_name = "#pragma omp parallel";
21478 tree stmt, clauses, par_clause, ws_clause, block;
21479 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21484 cp_lexer_consume_token (parser->lexer);
21485 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21486 p_name = "#pragma omp parallel for";
21487 mask |= OMP_FOR_CLAUSE_MASK;
21488 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21490 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21492 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21493 const char *p = IDENTIFIER_POINTER (id);
21494 if (strcmp (p, "sections") == 0)
21496 cp_lexer_consume_token (parser->lexer);
21497 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21498 p_name = "#pragma omp parallel sections";
21499 mask |= OMP_SECTIONS_CLAUSE_MASK;
21500 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21504 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21505 block = begin_omp_parallel ();
21506 save = cp_parser_begin_omp_structured_block (parser);
21510 case PRAGMA_OMP_PARALLEL:
21511 cp_parser_already_scoped_statement (parser);
21512 par_clause = clauses;
21515 case PRAGMA_OMP_PARALLEL_FOR:
21516 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21517 stmt = cp_parser_omp_for_loop (parser);
21519 OMP_FOR_CLAUSES (stmt) = ws_clause;
21522 case PRAGMA_OMP_PARALLEL_SECTIONS:
21523 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21524 stmt = cp_parser_omp_sections_scope (parser);
21526 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21530 gcc_unreachable ();
21533 cp_parser_end_omp_structured_block (parser, save);
21534 stmt = finish_omp_parallel (par_clause, block);
21535 if (p_kind != PRAGMA_OMP_PARALLEL)
21536 OMP_PARALLEL_COMBINED (stmt) = 1;
21541 # pragma omp single single-clause[optseq] new-line
21542 structured-block */
21544 #define OMP_SINGLE_CLAUSE_MASK \
21545 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21546 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21547 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21548 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21551 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21553 tree stmt = make_node (OMP_SINGLE);
21554 TREE_TYPE (stmt) = void_type_node;
21556 OMP_SINGLE_CLAUSES (stmt)
21557 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21558 "#pragma omp single", pragma_tok);
21559 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21561 return add_stmt (stmt);
21565 # pragma omp threadprivate (variable-list) */
21568 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21572 vars = cp_parser_omp_var_list (parser, 0, NULL);
21573 cp_parser_require_pragma_eol (parser, pragma_tok);
21575 if (!targetm.have_tls)
21576 sorry ("threadprivate variables not supported in this target");
21578 finish_omp_threadprivate (vars);
21581 /* Main entry point to OpenMP statement pragmas. */
21584 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21588 switch (pragma_tok->pragma_kind)
21590 case PRAGMA_OMP_ATOMIC:
21591 cp_parser_omp_atomic (parser, pragma_tok);
21593 case PRAGMA_OMP_CRITICAL:
21594 stmt = cp_parser_omp_critical (parser, pragma_tok);
21596 case PRAGMA_OMP_FOR:
21597 stmt = cp_parser_omp_for (parser, pragma_tok);
21599 case PRAGMA_OMP_MASTER:
21600 stmt = cp_parser_omp_master (parser, pragma_tok);
21602 case PRAGMA_OMP_ORDERED:
21603 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21605 case PRAGMA_OMP_PARALLEL:
21606 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21608 case PRAGMA_OMP_SECTIONS:
21609 stmt = cp_parser_omp_sections (parser, pragma_tok);
21611 case PRAGMA_OMP_SINGLE:
21612 stmt = cp_parser_omp_single (parser, pragma_tok);
21615 gcc_unreachable ();
21619 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21624 static GTY (()) cp_parser *the_parser;
21627 /* Special handling for the first token or line in the file. The first
21628 thing in the file might be #pragma GCC pch_preprocess, which loads a
21629 PCH file, which is a GC collection point. So we need to handle this
21630 first pragma without benefit of an existing lexer structure.
21632 Always returns one token to the caller in *FIRST_TOKEN. This is
21633 either the true first token of the file, or the first token after
21634 the initial pragma. */
21637 cp_parser_initial_pragma (cp_token *first_token)
21641 cp_lexer_get_preprocessor_token (NULL, first_token);
21642 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21645 cp_lexer_get_preprocessor_token (NULL, first_token);
21646 if (first_token->type == CPP_STRING)
21648 name = first_token->u.value;
21650 cp_lexer_get_preprocessor_token (NULL, first_token);
21651 if (first_token->type != CPP_PRAGMA_EOL)
21652 error ("junk at end of %<#pragma GCC pch_preprocess%>");
21655 error ("expected string literal");
21657 /* Skip to the end of the pragma. */
21658 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21659 cp_lexer_get_preprocessor_token (NULL, first_token);
21661 /* Now actually load the PCH file. */
21663 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21665 /* Read one more token to return to our caller. We have to do this
21666 after reading the PCH file in, since its pointers have to be
21668 cp_lexer_get_preprocessor_token (NULL, first_token);
21671 /* Normal parsing of a pragma token. Here we can (and must) use the
21675 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21677 cp_token *pragma_tok;
21680 pragma_tok = cp_lexer_consume_token (parser->lexer);
21681 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21682 parser->lexer->in_pragma = true;
21684 id = pragma_tok->pragma_kind;
21687 case PRAGMA_GCC_PCH_PREPROCESS:
21688 error ("%<#pragma GCC pch_preprocess%> must be first");
21691 case PRAGMA_OMP_BARRIER:
21694 case pragma_compound:
21695 cp_parser_omp_barrier (parser, pragma_tok);
21698 error ("%<#pragma omp barrier%> may only be "
21699 "used in compound statements");
21706 case PRAGMA_OMP_FLUSH:
21709 case pragma_compound:
21710 cp_parser_omp_flush (parser, pragma_tok);
21713 error ("%<#pragma omp flush%> may only be "
21714 "used in compound statements");
21721 case PRAGMA_OMP_THREADPRIVATE:
21722 cp_parser_omp_threadprivate (parser, pragma_tok);
21725 case PRAGMA_OMP_ATOMIC:
21726 case PRAGMA_OMP_CRITICAL:
21727 case PRAGMA_OMP_FOR:
21728 case PRAGMA_OMP_MASTER:
21729 case PRAGMA_OMP_ORDERED:
21730 case PRAGMA_OMP_PARALLEL:
21731 case PRAGMA_OMP_SECTIONS:
21732 case PRAGMA_OMP_SINGLE:
21733 if (context == pragma_external)
21735 cp_parser_omp_construct (parser, pragma_tok);
21738 case PRAGMA_OMP_SECTION:
21739 error ("%<#pragma omp section%> may only be used in "
21740 "%<#pragma omp sections%> construct");
21744 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21745 c_invoke_pragma_handler (id);
21749 cp_parser_error (parser, "expected declaration specifiers");
21753 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21757 /* The interface the pragma parsers have to the lexer. */
21760 pragma_lex (tree *value)
21763 enum cpp_ttype ret;
21765 tok = cp_lexer_peek_token (the_parser->lexer);
21768 *value = tok->u.value;
21770 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21772 else if (ret == CPP_STRING)
21773 *value = cp_parser_string_literal (the_parser, false, false);
21776 cp_lexer_consume_token (the_parser->lexer);
21777 if (ret == CPP_KEYWORD)
21785 /* External interface. */
21787 /* Parse one entire translation unit. */
21790 c_parse_file (void)
21792 bool error_occurred;
21793 static bool already_called = false;
21795 if (already_called)
21797 sorry ("inter-module optimizations not implemented for C++");
21800 already_called = true;
21802 the_parser = cp_parser_new ();
21803 push_deferring_access_checks (flag_access_control
21804 ? dk_no_deferred : dk_no_check);
21805 error_occurred = cp_parser_translation_unit (the_parser);
21809 /* This variable must be provided by every front end. */
21813 #include "gt-cp-parser.h"