2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
158 static cp_lexer *cp_lexer_new_main
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
610 if (token == lexer->last_token)
616 if (token->type != CPP_PURGED)
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
683 if (tok == lexer->last_token)
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
956 declarator->parameter_pack_p = false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
979 declarator->parameter_pack_p = false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1004 declarator->parameter_pack_p = false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator *target,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification,
1019 tree late_return_type)
1021 cp_declarator *declarator;
1023 declarator = make_declarator (cdk_function);
1024 declarator->declarator = target;
1025 declarator->u.function.parameters = parms;
1026 declarator->u.function.qualifiers = cv_qualifiers;
1027 declarator->u.function.exception_specification = exception_specification;
1028 declarator->u.function.late_return_type = late_return_type;
1031 declarator->parameter_pack_p = target->parameter_pack_p;
1032 target->parameter_pack_p = false;
1035 declarator->parameter_pack_p = false;
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041 defined by ELEMENT. */
1044 make_array_declarator (cp_declarator *element, tree bounds)
1046 cp_declarator *declarator;
1048 declarator = make_declarator (cdk_array);
1049 declarator->declarator = element;
1050 declarator->u.array.bounds = bounds;
1053 declarator->parameter_pack_p = element->parameter_pack_p;
1054 element->parameter_pack_p = false;
1057 declarator->parameter_pack_p = false;
1062 /* Determine whether the declarator we've seen so far can be a
1063 parameter pack, when followed by an ellipsis. */
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1067 /* Search for a declarator name, or any other declarator that goes
1068 after the point where the ellipsis could appear in a parameter
1069 pack. If we find any of these, then this declarator can not be
1070 made into a parameter pack. */
1072 while (declarator && !found)
1074 switch ((int)declarator->kind)
1085 declarator = declarator->declarator;
1093 cp_parameter_declarator *no_parameters;
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096 DECLARATOR and DEFAULT_ARGUMENT. */
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100 cp_declarator *declarator,
1101 tree default_argument)
1103 cp_parameter_declarator *parameter;
1105 parameter = ((cp_parameter_declarator *)
1106 alloc_declarator (sizeof (cp_parameter_declarator)));
1107 parameter->next = NULL;
1108 if (decl_specifiers)
1109 parameter->decl_specifiers = *decl_specifiers;
1111 clear_decl_specs (¶meter->decl_specifiers);
1112 parameter->declarator = declarator;
1113 parameter->default_argument = default_argument;
1114 parameter->ellipsis_p = false;
1119 /* Returns true iff DECLARATOR is a declaration for a function. */
1122 function_declarator_p (const cp_declarator *declarator)
1126 if (declarator->kind == cdk_function
1127 && declarator->declarator->kind == cdk_id)
1129 if (declarator->kind == cdk_id
1130 || declarator->kind == cdk_error)
1132 declarator = declarator->declarator;
1142 A cp_parser parses the token stream as specified by the C++
1143 grammar. Its job is purely parsing, not semantic analysis. For
1144 example, the parser breaks the token stream into declarators,
1145 expressions, statements, and other similar syntactic constructs.
1146 It does not check that the types of the expressions on either side
1147 of an assignment-statement are compatible, or that a function is
1148 not declared with a parameter of type `void'.
1150 The parser invokes routines elsewhere in the compiler to perform
1151 semantic analysis and to build up the abstract syntax tree for the
1154 The parser (and the template instantiation code, which is, in a
1155 way, a close relative of parsing) are the only parts of the
1156 compiler that should be calling push_scope and pop_scope, or
1157 related functions. The parser (and template instantiation code)
1158 keeps track of what scope is presently active; everything else
1159 should simply honor that. (The code that generates static
1160 initializers may also need to set the scope, in order to check
1161 access control correctly when emitting the initializers.)
1166 The parser is of the standard recursive-descent variety. Upcoming
1167 tokens in the token stream are examined in order to determine which
1168 production to use when parsing a non-terminal. Some C++ constructs
1169 require arbitrary look ahead to disambiguate. For example, it is
1170 impossible, in the general case, to tell whether a statement is an
1171 expression or declaration without scanning the entire statement.
1172 Therefore, the parser is capable of "parsing tentatively." When the
1173 parser is not sure what construct comes next, it enters this mode.
1174 Then, while we attempt to parse the construct, the parser queues up
1175 error messages, rather than issuing them immediately, and saves the
1176 tokens it consumes. If the construct is parsed successfully, the
1177 parser "commits", i.e., it issues any queued error messages and
1178 the tokens that were being preserved are permanently discarded.
1179 If, however, the construct is not parsed successfully, the parser
1180 rolls back its state completely so that it can resume parsing using
1181 a different alternative.
1186 The performance of the parser could probably be improved substantially.
1187 We could often eliminate the need to parse tentatively by looking ahead
1188 a little bit. In some places, this approach might not entirely eliminate
1189 the need to parse tentatively, but it might still speed up the average
1192 /* Flags that are passed to some parsing functions. These values can
1193 be bitwise-ored together. */
1195 typedef enum cp_parser_flags
1198 CP_PARSER_FLAGS_NONE = 0x0,
1199 /* The construct is optional. If it is not present, then no error
1200 should be issued. */
1201 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202 /* When parsing a type-specifier, do not allow user-defined types. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1206 /* The different kinds of declarators we want to parse. */
1208 typedef enum cp_parser_declarator_kind
1210 /* We want an abstract declarator. */
1211 CP_PARSER_DECLARATOR_ABSTRACT,
1212 /* We want a named declarator. */
1213 CP_PARSER_DECLARATOR_NAMED,
1214 /* We don't mind, but the name must be an unqualified-id. */
1215 CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1218 /* The precedence values used to parse binary expressions. The minimum value
1219 of PREC must be 1, because zero is reserved to quickly discriminate
1220 binary operators from other tokens. */
1225 PREC_LOGICAL_OR_EXPRESSION,
1226 PREC_LOGICAL_AND_EXPRESSION,
1227 PREC_INCLUSIVE_OR_EXPRESSION,
1228 PREC_EXCLUSIVE_OR_EXPRESSION,
1229 PREC_AND_EXPRESSION,
1230 PREC_EQUALITY_EXPRESSION,
1231 PREC_RELATIONAL_EXPRESSION,
1232 PREC_SHIFT_EXPRESSION,
1233 PREC_ADDITIVE_EXPRESSION,
1234 PREC_MULTIPLICATIVE_EXPRESSION,
1236 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240 precedence value. */
1242 typedef struct cp_parser_binary_operations_map_node
1244 /* The token type. */
1245 enum cpp_ttype token_type;
1246 /* The corresponding tree code. */
1247 enum tree_code tree_type;
1248 /* The precedence of this operator. */
1249 enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1252 /* The status of a tentative parse. */
1254 typedef enum cp_parser_status_kind
1256 /* No errors have occurred. */
1257 CP_PARSER_STATUS_KIND_NO_ERROR,
1258 /* An error has occurred. */
1259 CP_PARSER_STATUS_KIND_ERROR,
1260 /* We are committed to this tentative parse, whether or not an error
1262 CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1265 typedef struct cp_parser_expression_stack_entry
1267 /* Left hand side of the binary operation we are currently
1270 /* Original tree code for left hand side, if it was a binary
1271 expression itself (used for -Wparentheses). */
1272 enum tree_code lhs_type;
1273 /* Tree code for the binary operation we are parsing. */
1274 enum tree_code tree_type;
1275 /* Precedence of the binary operation we are parsing. */
1277 } cp_parser_expression_stack_entry;
1279 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1280 entries because precedence levels on the stack are monotonically
1282 typedef struct cp_parser_expression_stack_entry
1283 cp_parser_expression_stack[NUM_PREC_VALUES];
1285 /* Context that is saved and restored when parsing tentatively. */
1286 typedef struct cp_parser_context GTY (())
1288 /* If this is a tentative parsing context, the status of the
1290 enum cp_parser_status_kind status;
1291 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1292 that are looked up in this context must be looked up both in the
1293 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294 the context of the containing expression. */
1297 /* The next parsing context in the stack. */
1298 struct cp_parser_context *next;
1299 } cp_parser_context;
1303 /* Constructors and destructors. */
1305 static cp_parser_context *cp_parser_context_new
1306 (cp_parser_context *);
1308 /* Class variables. */
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313 Transformed into an associative array (binops_by_token) by
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1320 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1327 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1330 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1338 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1340 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1342 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1344 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1346 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1349 /* The same as binops, but initialized by cp_parser_new so that
1350 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1354 /* Constructors and destructors. */
1356 /* Construct a new context. The context below this one on the stack
1357 is given by NEXT. */
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1362 cp_parser_context *context;
1364 /* Allocate the storage. */
1365 if (cp_parser_context_free_list != NULL)
1367 /* Pull the first entry from the free list. */
1368 context = cp_parser_context_free_list;
1369 cp_parser_context_free_list = context->next;
1370 memset (context, 0, sizeof (*context));
1373 context = GGC_CNEW (cp_parser_context);
1375 /* No errors have occurred yet in this context. */
1376 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377 /* If this is not the bottommost context, copy information that we
1378 need from the previous context. */
1381 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382 expression, then we are parsing one in this context, too. */
1383 context->object_type = next->object_type;
1384 /* Thread the stack. */
1385 context->next = next;
1391 /* The cp_parser structure represents the C++ parser. */
1393 typedef struct cp_parser GTY(())
1395 /* The lexer from which we are obtaining tokens. */
1398 /* The scope in which names should be looked up. If NULL_TREE, then
1399 we look up names in the scope that is currently open in the
1400 source program. If non-NULL, this is either a TYPE or
1401 NAMESPACE_DECL for the scope in which we should look. It can
1402 also be ERROR_MARK, when we've parsed a bogus scope.
1404 This value is not cleared automatically after a name is looked
1405 up, so we must be careful to clear it before starting a new look
1406 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1407 will look up `Z' in the scope of `X', rather than the current
1408 scope.) Unfortunately, it is difficult to tell when name lookup
1409 is complete, because we sometimes peek at a token, look it up,
1410 and then decide not to consume it. */
1413 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414 last lookup took place. OBJECT_SCOPE is used if an expression
1415 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416 respectively. QUALIFYING_SCOPE is used for an expression of the
1417 form "X::Y"; it refers to X. */
1419 tree qualifying_scope;
1421 /* A stack of parsing contexts. All but the bottom entry on the
1422 stack will be tentative contexts.
1424 We parse tentatively in order to determine which construct is in
1425 use in some situations. For example, in order to determine
1426 whether a statement is an expression-statement or a
1427 declaration-statement we parse it tentatively as a
1428 declaration-statement. If that fails, we then reparse the same
1429 token stream as an expression-statement. */
1430 cp_parser_context *context;
1432 /* True if we are parsing GNU C++. If this flag is not set, then
1433 GNU extensions are not recognized. */
1434 bool allow_gnu_extensions_p;
1436 /* TRUE if the `>' token should be interpreted as the greater-than
1437 operator. FALSE if it is the end of a template-id or
1438 template-parameter-list. In C++0x mode, this flag also applies to
1439 `>>' tokens, which are viewed as two consecutive `>' tokens when
1440 this flag is FALSE. */
1441 bool greater_than_is_operator_p;
1443 /* TRUE if default arguments are allowed within a parameter list
1444 that starts at this point. FALSE if only a gnu extension makes
1445 them permissible. */
1446 bool default_arg_ok_p;
1448 /* TRUE if we are parsing an integral constant-expression. See
1449 [expr.const] for a precise definition. */
1450 bool integral_constant_expression_p;
1452 /* TRUE if we are parsing an integral constant-expression -- but a
1453 non-constant expression should be permitted as well. This flag
1454 is used when parsing an array bound so that GNU variable-length
1455 arrays are tolerated. */
1456 bool allow_non_integral_constant_expression_p;
1458 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459 been seen that makes the expression non-constant. */
1460 bool non_integral_constant_expression_p;
1462 /* TRUE if local variable names and `this' are forbidden in the
1464 bool local_variables_forbidden_p;
1466 /* TRUE if the declaration we are parsing is part of a
1467 linkage-specification of the form `extern string-literal
1469 bool in_unbraced_linkage_specification_p;
1471 /* TRUE if we are presently parsing a declarator, after the
1472 direct-declarator. */
1473 bool in_declarator_p;
1475 /* TRUE if we are presently parsing a template-argument-list. */
1476 bool in_template_argument_list_p;
1478 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479 to IN_OMP_BLOCK if parsing OpenMP structured block and
1480 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1481 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482 iteration-statement, OpenMP block or loop within that switch. */
1483 #define IN_SWITCH_STMT 1
1484 #define IN_ITERATION_STMT 2
1485 #define IN_OMP_BLOCK 4
1486 #define IN_OMP_FOR 8
1487 #define IN_IF_STMT 16
1488 unsigned char in_statement;
1490 /* TRUE if we are presently parsing the body of a switch statement.
1491 Note that this doesn't quite overlap with in_statement above.
1492 The difference relates to giving the right sets of error messages:
1493 "case not in switch" vs "break statement used with OpenMP...". */
1494 bool in_switch_statement_p;
1496 /* TRUE if we are parsing a type-id in an expression context. In
1497 such a situation, both "type (expr)" and "type (type)" are valid
1499 bool in_type_id_in_expr_p;
1501 /* TRUE if we are currently in a header file where declarations are
1502 implicitly extern "C". */
1503 bool implicit_extern_c;
1505 /* TRUE if strings in expressions should be translated to the execution
1507 bool translate_strings_p;
1509 /* TRUE if we are presently parsing the body of a function, but not
1511 bool in_function_body;
1513 /* If non-NULL, then we are parsing a construct where new type
1514 definitions are not permitted. The string stored here will be
1515 issued as an error message if a type is defined. */
1516 const char *type_definition_forbidden_message;
1518 /* A list of lists. The outer list is a stack, used for member
1519 functions of local classes. At each level there are two sub-list,
1520 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522 TREE_VALUE's. The functions are chained in reverse declaration
1525 The TREE_PURPOSE sublist contains those functions with default
1526 arguments that need post processing, and the TREE_VALUE sublist
1527 contains those functions with definitions that need post
1530 These lists can only be processed once the outermost class being
1531 defined is complete. */
1532 tree unparsed_functions_queues;
1534 /* The number of classes whose definitions are currently in
1536 unsigned num_classes_being_defined;
1538 /* The number of template parameter lists that apply directly to the
1539 current declaration. */
1540 unsigned num_template_parameter_lists;
1545 /* Constructors and destructors. */
1547 static cp_parser *cp_parser_new
1550 /* Routines to parse various constructs.
1552 Those that return `tree' will return the error_mark_node (rather
1553 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554 Sometimes, they will return an ordinary node if error-recovery was
1555 attempted, even though a parse error occurred. So, to check
1556 whether or not a parse error occurred, you should always use
1557 cp_parser_error_occurred. If the construct is optional (indicated
1558 either by an `_opt' in the name of the function that does the
1559 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560 the construct is not present. */
1562 /* Lexical conventions [gram.lex] */
1564 static tree cp_parser_identifier
1566 static tree cp_parser_string_literal
1567 (cp_parser *, bool, bool);
1569 /* Basic concepts [gram.basic] */
1571 static bool cp_parser_translation_unit
1574 /* Expressions [gram.expr] */
1576 static tree cp_parser_primary_expression
1577 (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579 (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587 (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589 (cp_parser *, bool, bool, bool);
1590 static tree cp_parser_postfix_open_square_expression
1591 (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595 (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597 (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599 (cp_parser *, bool, bool);
1600 static enum tree_code cp_parser_unary_operator
1602 static tree cp_parser_new_expression
1604 static tree cp_parser_new_placement
1606 static tree cp_parser_new_type_id
1607 (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1610 static cp_declarator *cp_parser_direct_new_declarator
1612 static tree cp_parser_new_initializer
1614 static tree cp_parser_delete_expression
1616 static tree cp_parser_cast_expression
1617 (cp_parser *, bool, bool);
1618 static tree cp_parser_binary_expression
1619 (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621 (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623 (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1626 static tree cp_parser_expression
1627 (cp_parser *, bool);
1628 static tree cp_parser_constant_expression
1629 (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1633 /* Statements [gram.stmt.stmt] */
1635 static void cp_parser_statement
1636 (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1639 static tree cp_parser_expression_statement
1640 (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642 (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644 (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646 (cp_parser *, bool *);
1647 static tree cp_parser_condition
1649 static tree cp_parser_iteration_statement
1651 static void cp_parser_for_init_statement
1653 static tree cp_parser_jump_statement
1655 static void cp_parser_declaration_statement
1658 static tree cp_parser_implicitly_scoped_statement
1659 (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1663 /* Declarations [gram.dcl.dcl] */
1665 static void cp_parser_declaration_seq_opt
1667 static void cp_parser_declaration
1669 static void cp_parser_block_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1677 static tree cp_parser_function_specifier_opt
1678 (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1682 static tree cp_parser_simple_type_specifier
1683 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1686 static tree cp_parser_nonclass_name
1687 (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689 (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1692 static void cp_parser_enumerator_list
1693 (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695 (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1698 static void cp_parser_namespace_definition
1700 static void cp_parser_namespace_body
1702 static tree cp_parser_qualified_namespace_specifier
1704 static void cp_parser_namespace_alias_definition
1706 static bool cp_parser_using_declaration
1707 (cp_parser *, bool);
1708 static void cp_parser_using_directive
1710 static void cp_parser_asm_definition
1712 static void cp_parser_linkage_specification
1714 static void cp_parser_static_assert
1715 (cp_parser *, bool);
1716 static tree cp_parser_decltype
1719 /* Declarators [gram.dcl.decl] */
1721 static tree cp_parser_init_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728 (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1731 static tree cp_parser_late_return_type_opt
1733 static tree cp_parser_declarator_id
1734 (cp_parser *, bool);
1735 static tree cp_parser_type_id
1737 static void cp_parser_type_specifier_seq
1738 (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1741 static tree cp_parser_parameter_declaration_list
1742 (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744 (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument
1746 (cp_parser *, bool);
1747 static void cp_parser_function_body
1749 static tree cp_parser_initializer
1750 (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752 (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754 (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756 (cp_parser *, bool *);
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1761 /* Classes [gram.class] */
1763 static tree cp_parser_class_name
1764 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1767 static tree cp_parser_class_head
1768 (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1771 static void cp_parser_member_specification_opt
1773 static void cp_parser_member_declaration
1775 static tree cp_parser_pure_specifier
1777 static tree cp_parser_constant_initializer
1780 /* Derived classes [gram.class.derived] */
1782 static tree cp_parser_base_clause
1784 static tree cp_parser_base_specifier
1787 /* Special member functions [gram.special] */
1789 static tree cp_parser_conversion_function_id
1791 static tree cp_parser_conversion_type_id
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1795 static bool cp_parser_ctor_initializer_opt
1797 static void cp_parser_mem_initializer_list
1799 static tree cp_parser_mem_initializer
1801 static tree cp_parser_mem_initializer_id
1804 /* Overloading [gram.over] */
1806 static tree cp_parser_operator_function_id
1808 static tree cp_parser_operator
1811 /* Templates [gram.temp] */
1813 static void cp_parser_template_declaration
1814 (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1817 static tree cp_parser_template_parameter
1818 (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820 (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822 (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824 (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1827 static tree cp_parser_template_argument
1829 static void cp_parser_explicit_instantiation
1831 static void cp_parser_explicit_specialization
1834 /* Exception handling [gram.exception] */
1836 static tree cp_parser_try_block
1838 static bool cp_parser_function_try_block
1840 static void cp_parser_handler_seq
1842 static void cp_parser_handler
1844 static tree cp_parser_exception_declaration
1846 static tree cp_parser_throw_expression
1848 static tree cp_parser_exception_specification_opt
1850 static tree cp_parser_type_id_list
1853 /* GNU Extensions */
1855 static tree cp_parser_asm_specification_opt
1857 static tree cp_parser_asm_operand_list
1859 static tree cp_parser_asm_clobber_list
1861 static tree cp_parser_attributes_opt
1863 static tree cp_parser_attribute_list
1865 static bool cp_parser_extension_opt
1866 (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872 (cp_parser *, enum pragma_context);
1874 /* Objective-C++ Productions */
1876 static tree cp_parser_objc_message_receiver
1878 static tree cp_parser_objc_message_args
1880 static tree cp_parser_objc_message_expression
1882 static tree cp_parser_objc_encode_expression
1884 static tree cp_parser_objc_defs_expression
1886 static tree cp_parser_objc_protocol_expression
1888 static tree cp_parser_objc_selector_expression
1890 static tree cp_parser_objc_expression
1892 static bool cp_parser_objc_selector_p
1894 static tree cp_parser_objc_selector
1896 static tree cp_parser_objc_protocol_refs_opt
1898 static void cp_parser_objc_declaration
1900 static tree cp_parser_objc_statement
1903 /* Utility Routines */
1905 static tree cp_parser_lookup_name
1906 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908 (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1911 static bool cp_parser_check_declarator_template_parameters
1912 (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914 (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1917 static tree cp_parser_global_scope_opt
1918 (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920 (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924 (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926 (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928 (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932 (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1937 static void cp_parser_save_default_args
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942 (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944 (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946 (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1949 static void cp_parser_set_storage_class
1950 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952 (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954 (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956 (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958 (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1961 static bool cp_parser_next_token_starts_class_definition_p
1963 static bool cp_parser_next_token_ends_template_argument_p
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966 (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1969 static void cp_parser_check_class_key
1970 (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972 (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1975 static void cp_parser_pre_parsed_nested_name_specifier
1977 static bool cp_parser_cache_group
1978 (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1981 static void cp_parser_commit_to_tentative_parse
1983 static void cp_parser_abort_tentative_parse
1985 static bool cp_parser_parse_definitely
1987 static inline bool cp_parser_parsing_tentatively
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1991 static void cp_parser_error
1992 (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994 (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1997 static bool cp_parser_check_type_definition
1999 static void cp_parser_check_for_definition_in_return_type
2000 (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002 (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004 (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006 (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009 static int cp_parser_skip_to_closing_parenthesis
2010 (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2015 static void cp_parser_skip_to_end_of_block_or_statement
2017 static bool cp_parser_skip_to_closing_brace
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2021 static void cp_parser_skip_to_pragma_eol
2022 (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2025 static bool cp_parser_allow_gnu_extensions_p
2027 static bool cp_parser_is_string_literal
2029 static bool cp_parser_is_keyword
2030 (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032 (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2036 /* Returns nonzero if we are parsing tentatively. */
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2041 return parser->context->next != NULL;
2044 /* Returns nonzero if TOKEN is a string literal. */
2047 cp_parser_is_string_literal (cp_token* token)
2049 return (token->type == CPP_STRING ||
2050 token->type == CPP_STRING16 ||
2051 token->type == CPP_STRING32 ||
2052 token->type == CPP_WSTRING);
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2060 return token->keyword == keyword;
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064 FILE:LINE: MESSAGE before TOKEN
2065 where TOKEN is the next token in the input stream. MESSAGE
2066 (specified by the caller) is usually of the form "expected
2070 cp_parser_error (cp_parser* parser, const char* message)
2072 if (!cp_parser_simulate_error (parser))
2074 cp_token *token = cp_lexer_peek_token (parser->lexer);
2075 /* This diagnostic makes more sense if it is tagged to the line
2076 of the token we just peeked at. */
2077 cp_lexer_set_source_position_from_token (token);
2079 if (token->type == CPP_PRAGMA)
2081 error ("%H%<#pragma%> is not allowed here", &token->location);
2082 cp_parser_skip_to_pragma_eol (parser, token);
2086 c_parse_error (message,
2087 /* Because c_parser_error does not understand
2088 CPP_KEYWORD, keywords are treated like
2090 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2095 /* Issue an error about name-lookup failing. NAME is the
2096 IDENTIFIER_NODE DECL is the result of
2097 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2098 the thing that we hoped to find. */
2101 cp_parser_name_lookup_error (cp_parser* parser,
2104 const char* desired,
2105 location_t location)
2107 /* If name lookup completely failed, tell the user that NAME was not
2109 if (decl == error_mark_node)
2111 if (parser->scope && parser->scope != global_namespace)
2112 error ("%H%<%E::%E%> has not been declared",
2113 &location, parser->scope, name);
2114 else if (parser->scope == global_namespace)
2115 error ("%H%<::%E%> has not been declared", &location, name);
2116 else if (parser->object_scope
2117 && !CLASS_TYPE_P (parser->object_scope))
2118 error ("%Hrequest for member %qE in non-class type %qT",
2119 &location, name, parser->object_scope);
2120 else if (parser->object_scope)
2121 error ("%H%<%T::%E%> has not been declared",
2122 &location, parser->object_scope, name);
2124 error ("%H%qE has not been declared", &location, name);
2126 else if (parser->scope && parser->scope != global_namespace)
2127 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128 else if (parser->scope == global_namespace)
2129 error ("%H%<::%E%> %s", &location, name, desired);
2131 error ("%H%qE %s", &location, name, desired);
2134 /* If we are parsing tentatively, remember that an error has occurred
2135 during this tentative parse. Returns true if the error was
2136 simulated; false if a message should be issued by the caller. */
2139 cp_parser_simulate_error (cp_parser* parser)
2141 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2143 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2149 /* Check for repeated decl-specifiers. */
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153 location_t location)
2157 for (ds = ds_first; ds != ds_last; ++ds)
2159 unsigned count = decl_specs->specs[(int)ds];
2162 /* The "long" specifier is a special case because of "long long". */
2166 error ("%H%<long long long%> is too long for GCC", &location);
2167 else if (pedantic && !in_system_header && warn_long_long
2168 && cxx_dialect == cxx98)
2169 pedwarn (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2174 static const char *const decl_spec_names[] = {
2190 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2200 cp_parser_check_type_definition (cp_parser* parser)
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2232 && declarator->kind == cdk_function)
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2251 cp_token_position start = 0;
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2287 if (!parser->allow_non_integral_constant_expression_p)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2330 else if (!parser->scope)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2389 cp_parser_commit_to_tentative_parse (parser);
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2423 cp_parser_abort_tentative_parse (parser);
2426 if (!cp_parser_parse_definitely (parser))
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2460 switch (token->type)
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2468 /* This matches the processing in skip_to_end_of_statement. */
2473 case CPP_OPEN_BRACE:
2476 case CPP_CLOSE_BRACE:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2486 case CPP_OPEN_PAREN:
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2495 cp_lexer_consume_token (parser->lexer);
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2516 unsigned nesting_depth = 0;
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2522 switch (token->type)
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2530 /* If the next token is a `;', we have reached the end of the
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2557 if (--nesting_depth == 0)
2559 cp_lexer_consume_token (parser->lexer);
2563 case CPP_OPEN_BRACE:
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2586 /* If there is additional (erroneous) input, skip to the end of
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2601 int nesting_depth = 0;
2603 while (nesting_depth >= 0)
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2607 switch (token->type)
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2615 /* Stop if this is an unnested ';'. */
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2628 case CPP_OPEN_BRACE:
2637 /* Consume the token. */
2638 cp_lexer_consume_token (parser->lexer);
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643 token, or there are no more tokens. Return true in the first case,
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2649 unsigned nesting_depth = 0;
2653 cp_token *token = cp_lexer_peek_token (parser->lexer);
2655 switch (token->type)
2658 case CPP_PRAGMA_EOL:
2659 /* If we've run out of tokens, stop. */
2662 case CPP_CLOSE_BRACE:
2663 /* If the next token is a non-nested `}', then we have reached
2664 the end of the current block. */
2665 if (nesting_depth-- == 0)
2669 case CPP_OPEN_BRACE:
2670 /* If it the next token is a `{', then we are entering a new
2671 block. Consume the entire block. */
2679 /* Consume the token. */
2680 cp_lexer_consume_token (parser->lexer);
2684 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2685 parameter is the PRAGMA token, allowing us to purge the entire pragma
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2693 parser->lexer->in_pragma = false;
2696 token = cp_lexer_consume_token (parser->lexer);
2697 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2699 /* Ensure that the pragma is not parsed again. */
2700 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2703 /* Require pragma end of line, resyncing with it as necessary. The
2704 arguments are as for cp_parser_skip_to_pragma_eol. */
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2709 parser->lexer->in_pragma = false;
2710 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715 an unresolved identifier node, we can provide a superior diagnostic
2716 using cp_parser_diagnose_invalid_type_name. */
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720 tree id, location_t id_location)
2723 if (TREE_CODE (id) == IDENTIFIER_NODE)
2725 result = make_typename_type (scope, id, typename_type,
2726 /*complain=*/tf_none);
2727 if (result == error_mark_node)
2728 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2731 return make_typename_type (scope, id, typename_type, tf_error);
2734 /* This is a wrapper around the
2735 make_{pointer,ptrmem,reference}_declarator functions that decides
2736 which one to call based on the CODE and CLASS_TYPE arguments. The
2737 CODE argument should be one of the values returned by
2738 cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741 cp_cv_quals cv_qualifiers,
2742 cp_declarator *target)
2744 if (code == ERROR_MARK)
2745 return cp_error_declarator;
2747 if (code == INDIRECT_REF)
2748 if (class_type == NULL_TREE)
2749 return make_pointer_declarator (cv_qualifiers, target);
2751 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, false);
2754 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, true);
2759 /* Create a new C++ parser. */
2762 cp_parser_new (void)
2768 /* cp_lexer_new_main is called before calling ggc_alloc because
2769 cp_lexer_new_main might load a PCH file. */
2770 lexer = cp_lexer_new_main ();
2772 /* Initialize the binops_by_token so that we can get the tree
2773 directly from the token. */
2774 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775 binops_by_token[binops[i].token_type] = binops[i];
2777 parser = GGC_CNEW (cp_parser);
2778 parser->lexer = lexer;
2779 parser->context = cp_parser_context_new (NULL);
2781 /* For now, we always accept GNU extensions. */
2782 parser->allow_gnu_extensions_p = 1;
2784 /* The `>' token is a greater-than operator, not the end of a
2786 parser->greater_than_is_operator_p = true;
2788 parser->default_arg_ok_p = true;
2790 /* We are not parsing a constant-expression. */
2791 parser->integral_constant_expression_p = false;
2792 parser->allow_non_integral_constant_expression_p = false;
2793 parser->non_integral_constant_expression_p = false;
2795 /* Local variable names are not forbidden. */
2796 parser->local_variables_forbidden_p = false;
2798 /* We are not processing an `extern "C"' declaration. */
2799 parser->in_unbraced_linkage_specification_p = false;
2801 /* We are not processing a declarator. */
2802 parser->in_declarator_p = false;
2804 /* We are not processing a template-argument-list. */
2805 parser->in_template_argument_list_p = false;
2807 /* We are not in an iteration statement. */
2808 parser->in_statement = 0;
2810 /* We are not in a switch statement. */
2811 parser->in_switch_statement_p = false;
2813 /* We are not parsing a type-id inside an expression. */
2814 parser->in_type_id_in_expr_p = false;
2816 /* Declarations aren't implicitly extern "C". */
2817 parser->implicit_extern_c = false;
2819 /* String literals should be translated to the execution character set. */
2820 parser->translate_strings_p = true;
2822 /* We are not parsing a function body. */
2823 parser->in_function_body = false;
2825 /* The unparsed function queue is empty. */
2826 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2828 /* There are no classes being defined. */
2829 parser->num_classes_being_defined = 0;
2831 /* No template parameters apply. */
2832 parser->num_template_parameter_lists = 0;
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838 and push it onto the parser's lexer stack. This is used for delayed
2839 parsing of in-class method bodies and default arguments, and should
2840 not be confused with tentative parsing. */
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2844 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845 lexer->next = parser->lexer;
2846 parser->lexer = lexer;
2848 /* Move the current source position to that of the first token in the
2850 cp_lexer_set_source_position_from_token (lexer->next_token);
2853 /* Pop the top lexer off the parser stack. This is never used for the
2854 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2856 cp_parser_pop_lexer (cp_parser *parser)
2858 cp_lexer *lexer = parser->lexer;
2859 parser->lexer = lexer->next;
2860 cp_lexer_destroy (lexer);
2862 /* Put the current source position back where it was before this
2863 lexer was pushed. */
2864 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2867 /* Lexical conventions [gram.lex] */
2869 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2873 cp_parser_identifier (cp_parser* parser)
2877 /* Look for the identifier. */
2878 token = cp_parser_require (parser, CPP_NAME, "identifier");
2879 /* Return the value. */
2880 return token ? token->u.value : error_mark_node;
2883 /* Parse a sequence of adjacent string constants. Returns a
2884 TREE_STRING representing the combined, nul-terminated string
2885 constant. If TRANSLATE is true, translate the string to the
2886 execution character set. If WIDE_OK is true, a wide string is
2889 C++98 [lex.string] says that if a narrow string literal token is
2890 adjacent to a wide string literal token, the behavior is undefined.
2891 However, C99 6.4.5p4 says that this results in a wide string literal.
2892 We follow C99 here, for consistency with the C front end.
2894 This code is largely lifted from lex_string() in c-lex.c.
2896 FUTURE: ObjC++ will need to handle @-strings here. */
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2902 struct obstack str_ob;
2903 cpp_string str, istr, *strs;
2905 enum cpp_ttype type;
2907 tok = cp_lexer_peek_token (parser->lexer);
2908 if (!cp_parser_is_string_literal (tok))
2910 cp_parser_error (parser, "expected string-literal");
2911 return error_mark_node;
2916 /* Try to avoid the overhead of creating and destroying an obstack
2917 for the common case of just one string. */
2918 if (!cp_parser_is_string_literal
2919 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2921 cp_lexer_consume_token (parser->lexer);
2923 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924 str.len = TREE_STRING_LENGTH (tok->u.value);
2931 gcc_obstack_init (&str_ob);
2936 cp_lexer_consume_token (parser->lexer);
2938 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939 str.len = TREE_STRING_LENGTH (tok->u.value);
2941 if (type != tok->type)
2943 if (type == CPP_STRING)
2945 else if (tok->type != CPP_STRING)
2946 error ("%Hunsupported non-standard concatenation "
2947 "of string literals", &tok->location);
2950 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2952 tok = cp_lexer_peek_token (parser->lexer);
2954 while (cp_parser_is_string_literal (tok));
2956 strs = (cpp_string *) obstack_finish (&str_ob);
2959 if (type != CPP_STRING && !wide_ok)
2961 cp_parser_error (parser, "a wide string is invalid in this context");
2965 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966 (parse_in, strs, count, &istr, type))
2968 value = build_string (istr.len, (const char *)istr.text);
2969 free (CONST_CAST (unsigned char *, istr.text));
2975 TREE_TYPE (value) = char_array_type_node;
2978 TREE_TYPE (value) = char16_array_type_node;
2981 TREE_TYPE (value) = char32_array_type_node;
2984 TREE_TYPE (value) = wchar_array_type_node;
2988 value = fix_string_type (value);
2991 /* cpp_interpret_string has issued an error. */
2992 value = error_mark_node;
2995 obstack_free (&str_ob, 0);
3001 /* Basic concepts [gram.basic] */
3003 /* Parse a translation-unit.
3006 declaration-seq [opt]
3008 Returns TRUE if all went well. */
3011 cp_parser_translation_unit (cp_parser* parser)
3013 /* The address of the first non-permanent object on the declarator
3015 static void *declarator_obstack_base;
3019 /* Create the declarator obstack, if necessary. */
3020 if (!cp_error_declarator)
3022 gcc_obstack_init (&declarator_obstack);
3023 /* Create the error declarator. */
3024 cp_error_declarator = make_declarator (cdk_error);
3025 /* Create the empty parameter list. */
3026 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027 /* Remember where the base of the declarator obstack lies. */
3028 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3031 cp_parser_declaration_seq_opt (parser);
3033 /* If there are no tokens left then all went well. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3036 /* Get rid of the token array; we don't need it any more. */
3037 cp_lexer_destroy (parser->lexer);
3038 parser->lexer = NULL;
3040 /* This file might have been a context that's implicitly extern
3041 "C". If so, pop the lang context. (Only relevant for PCH.) */
3042 if (parser->implicit_extern_c)
3044 pop_lang_context ();
3045 parser->implicit_extern_c = false;
3049 finish_translation_unit ();
3055 cp_parser_error (parser, "expected declaration");
3059 /* Make sure the declarator obstack was fully cleaned up. */
3060 gcc_assert (obstack_next_free (&declarator_obstack)
3061 == declarator_obstack_base);
3063 /* All went well. */
3067 /* Expressions [gram.expr] */
3069 /* Parse a primary-expression.
3080 ( compound-statement )
3081 __builtin_va_arg ( assignment-expression , type-id )
3082 __builtin_offsetof ( type-id , offsetof-expression )
3085 __has_nothrow_assign ( type-id )
3086 __has_nothrow_constructor ( type-id )
3087 __has_nothrow_copy ( type-id )
3088 __has_trivial_assign ( type-id )
3089 __has_trivial_constructor ( type-id )
3090 __has_trivial_copy ( type-id )
3091 __has_trivial_destructor ( type-id )
3092 __has_virtual_destructor ( type-id )
3093 __is_abstract ( type-id )
3094 __is_base_of ( type-id , type-id )
3095 __is_class ( type-id )
3096 __is_convertible_to ( type-id , type-id )
3097 __is_empty ( type-id )
3098 __is_enum ( type-id )
3099 __is_pod ( type-id )
3100 __is_polymorphic ( type-id )
3101 __is_union ( type-id )
3103 Objective-C++ Extension:
3111 ADDRESS_P is true iff this expression was immediately preceded by
3112 "&" and therefore might denote a pointer-to-member. CAST_P is true
3113 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3114 true iff this expression is a template argument.
3116 Returns a representation of the expression. Upon return, *IDK
3117 indicates what kind of id-expression (if any) was present. */
3120 cp_parser_primary_expression (cp_parser *parser,
3123 bool template_arg_p,
3126 cp_token *token = NULL;
3128 /* Assume the primary expression is not an id-expression. */
3129 *idk = CP_ID_KIND_NONE;
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133 switch (token->type)
3146 token = cp_lexer_consume_token (parser->lexer);
3147 /* Floating-point literals are only allowed in an integral
3148 constant expression if they are cast to an integral or
3149 enumeration type. */
3150 if (TREE_CODE (token->u.value) == REAL_CST
3151 && parser->integral_constant_expression_p
3154 /* CAST_P will be set even in invalid code like "int(2.7 +
3155 ...)". Therefore, we have to check that the next token
3156 is sure to end the cast. */
3159 cp_token *next_token;
3161 next_token = cp_lexer_peek_token (parser->lexer);
3162 if (/* The comma at the end of an
3163 enumerator-definition. */
3164 next_token->type != CPP_COMMA
3165 /* The curly brace at the end of an enum-specifier. */
3166 && next_token->type != CPP_CLOSE_BRACE
3167 /* The end of a statement. */
3168 && next_token->type != CPP_SEMICOLON
3169 /* The end of the cast-expression. */
3170 && next_token->type != CPP_CLOSE_PAREN
3171 /* The end of an array bound. */
3172 && next_token->type != CPP_CLOSE_SQUARE
3173 /* The closing ">" in a template-argument-list. */
3174 && (next_token->type != CPP_GREATER
3175 || parser->greater_than_is_operator_p)
3176 /* C++0x only: A ">>" treated like two ">" tokens,
3177 in a template-argument-list. */
3178 && (next_token->type != CPP_RSHIFT
3179 || (cxx_dialect == cxx98)
3180 || parser->greater_than_is_operator_p))
3184 /* If we are within a cast, then the constraint that the
3185 cast is to an integral or enumeration type will be
3186 checked at that point. If we are not within a cast, then
3187 this code is invalid. */
3189 cp_parser_non_integral_constant_expression
3190 (parser, "floating-point literal");
3192 return token->u.value;
3198 /* ??? Should wide strings be allowed when parser->translate_strings_p
3199 is false (i.e. in attributes)? If not, we can kill the third
3200 argument to cp_parser_string_literal. */
3201 return cp_parser_string_literal (parser,
3202 parser->translate_strings_p,
3205 case CPP_OPEN_PAREN:
3208 bool saved_greater_than_is_operator_p;
3210 /* Consume the `('. */
3211 cp_lexer_consume_token (parser->lexer);
3212 /* Within a parenthesized expression, a `>' token is always
3213 the greater-than operator. */
3214 saved_greater_than_is_operator_p
3215 = parser->greater_than_is_operator_p;
3216 parser->greater_than_is_operator_p = true;
3217 /* If we see `( { ' then we are looking at the beginning of
3218 a GNU statement-expression. */
3219 if (cp_parser_allow_gnu_extensions_p (parser)
3220 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3222 /* Statement-expressions are not allowed by the standard. */
3223 pedwarn (token->location, OPT_pedantic,
3224 "ISO C++ forbids braced-groups within expressions");
3226 /* And they're not allowed outside of a function-body; you
3227 cannot, for example, write:
3229 int i = ({ int j = 3; j + 1; });
3231 at class or namespace scope. */
3232 if (!parser->in_function_body
3233 || parser->in_template_argument_list_p)
3235 error ("%Hstatement-expressions are not allowed outside "
3236 "functions nor in template-argument lists",
3238 cp_parser_skip_to_end_of_block_or_statement (parser);
3239 expr = error_mark_node;
3243 /* Start the statement-expression. */
3244 expr = begin_stmt_expr ();
3245 /* Parse the compound-statement. */
3246 cp_parser_compound_statement (parser, expr, false);
3248 expr = finish_stmt_expr (expr, false);
3253 /* Parse the parenthesized expression. */
3254 expr = cp_parser_expression (parser, cast_p);
3255 /* Let the front end know that this expression was
3256 enclosed in parentheses. This matters in case, for
3257 example, the expression is of the form `A::B', since
3258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3260 finish_parenthesized_expr (expr);
3262 /* The `>' token might be the end of a template-id or
3263 template-parameter-list now. */
3264 parser->greater_than_is_operator_p
3265 = saved_greater_than_is_operator_p;
3266 /* Consume the `)'. */
3267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268 cp_parser_skip_to_end_of_statement (parser);
3274 switch (token->keyword)
3276 /* These two are the boolean literals. */
3278 cp_lexer_consume_token (parser->lexer);
3279 return boolean_true_node;
3281 cp_lexer_consume_token (parser->lexer);
3282 return boolean_false_node;
3284 /* The `__null' literal. */
3286 cp_lexer_consume_token (parser->lexer);
3289 /* Recognize the `this' keyword. */
3291 cp_lexer_consume_token (parser->lexer);
3292 if (parser->local_variables_forbidden_p)
3294 error ("%H%<this%> may not be used in this context",
3296 return error_mark_node;
3298 /* Pointers cannot appear in constant-expressions. */
3299 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300 return error_mark_node;
3301 return finish_this_expr ();
3303 /* The `operator' keyword can be the beginning of an
3308 case RID_FUNCTION_NAME:
3309 case RID_PRETTY_FUNCTION_NAME:
3310 case RID_C99_FUNCTION_NAME:
3314 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3315 __func__ are the names of variables -- but they are
3316 treated specially. Therefore, they are handled here,
3317 rather than relying on the generic id-expression logic
3318 below. Grammatically, these names are id-expressions.
3320 Consume the token. */
3321 token = cp_lexer_consume_token (parser->lexer);
3323 switch (token->keyword)
3325 case RID_FUNCTION_NAME:
3326 name = "%<__FUNCTION__%>";
3328 case RID_PRETTY_FUNCTION_NAME:
3329 name = "%<__PRETTY_FUNCTION__%>";
3331 case RID_C99_FUNCTION_NAME:
3332 name = "%<__func__%>";
3338 if (cp_parser_non_integral_constant_expression (parser, name))
3339 return error_mark_node;
3341 /* Look up the name. */
3342 return finish_fname (token->u.value);
3350 /* The `__builtin_va_arg' construct is used to handle
3351 `va_arg'. Consume the `__builtin_va_arg' token. */
3352 cp_lexer_consume_token (parser->lexer);
3353 /* Look for the opening `('. */
3354 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3355 /* Now, parse the assignment-expression. */
3356 expression = cp_parser_assignment_expression (parser,
3358 /* Look for the `,'. */
3359 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3360 /* Parse the type-id. */
3361 type = cp_parser_type_id (parser);
3362 /* Look for the closing `)'. */
3363 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3364 /* Using `va_arg' in a constant-expression is not
3366 if (cp_parser_non_integral_constant_expression (parser,
3368 return error_mark_node;
3369 return build_x_va_arg (expression, type);
3373 return cp_parser_builtin_offsetof (parser);
3375 case RID_HAS_NOTHROW_ASSIGN:
3376 case RID_HAS_NOTHROW_CONSTRUCTOR:
3377 case RID_HAS_NOTHROW_COPY:
3378 case RID_HAS_TRIVIAL_ASSIGN:
3379 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3380 case RID_HAS_TRIVIAL_COPY:
3381 case RID_HAS_TRIVIAL_DESTRUCTOR:
3382 case RID_HAS_VIRTUAL_DESTRUCTOR:
3383 case RID_IS_ABSTRACT:
3384 case RID_IS_BASE_OF:
3386 case RID_IS_CONVERTIBLE_TO:
3390 case RID_IS_POLYMORPHIC:
3392 return cp_parser_trait_expr (parser, token->keyword);
3394 /* Objective-C++ expressions. */
3396 case RID_AT_PROTOCOL:
3397 case RID_AT_SELECTOR:
3398 return cp_parser_objc_expression (parser);
3401 cp_parser_error (parser, "expected primary-expression");
3402 return error_mark_node;
3405 /* An id-expression can start with either an identifier, a
3406 `::' as the beginning of a qualified-id, or the "operator"
3410 case CPP_TEMPLATE_ID:
3411 case CPP_NESTED_NAME_SPECIFIER:
3415 const char *error_msg;
3418 cp_token *id_expr_token;
3421 /* Parse the id-expression. */
3423 = cp_parser_id_expression (parser,
3424 /*template_keyword_p=*/false,
3425 /*check_dependency_p=*/true,
3427 /*declarator_p=*/false,
3428 /*optional_p=*/false);
3429 if (id_expression == error_mark_node)
3430 return error_mark_node;
3431 id_expr_token = token;
3432 token = cp_lexer_peek_token (parser->lexer);
3433 done = (token->type != CPP_OPEN_SQUARE
3434 && token->type != CPP_OPEN_PAREN
3435 && token->type != CPP_DOT
3436 && token->type != CPP_DEREF
3437 && token->type != CPP_PLUS_PLUS
3438 && token->type != CPP_MINUS_MINUS);
3439 /* If we have a template-id, then no further lookup is
3440 required. If the template-id was for a template-class, we
3441 will sometimes have a TYPE_DECL at this point. */
3442 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3443 || TREE_CODE (id_expression) == TYPE_DECL)
3444 decl = id_expression;
3445 /* Look up the name. */
3448 tree ambiguous_decls;
3450 decl = cp_parser_lookup_name (parser, id_expression,
3453 /*is_namespace=*/false,
3454 /*check_dependency=*/true,
3456 id_expr_token->location);
3457 /* If the lookup was ambiguous, an error will already have
3459 if (ambiguous_decls)
3460 return error_mark_node;
3462 /* In Objective-C++, an instance variable (ivar) may be preferred
3463 to whatever cp_parser_lookup_name() found. */
3464 decl = objc_lookup_ivar (decl, id_expression);
3466 /* If name lookup gives us a SCOPE_REF, then the
3467 qualifying scope was dependent. */
3468 if (TREE_CODE (decl) == SCOPE_REF)
3470 /* At this point, we do not know if DECL is a valid
3471 integral constant expression. We assume that it is
3472 in fact such an expression, so that code like:
3474 template <int N> struct A {
3478 is accepted. At template-instantiation time, we
3479 will check that B<N>::i is actually a constant. */
3482 /* Check to see if DECL is a local variable in a context
3483 where that is forbidden. */
3484 if (parser->local_variables_forbidden_p
3485 && local_variable_p (decl))
3487 /* It might be that we only found DECL because we are
3488 trying to be generous with pre-ISO scoping rules.
3489 For example, consider:
3493 for (int i = 0; i < 10; ++i) {}
3494 extern void f(int j = i);
3497 Here, name look up will originally find the out
3498 of scope `i'. We need to issue a warning message,
3499 but then use the global `i'. */
3500 decl = check_for_out_of_scope_variable (decl);
3501 if (local_variable_p (decl))
3503 error ("%Hlocal variable %qD may not appear in this context",
3504 &id_expr_token->location, decl);
3505 return error_mark_node;
3510 decl = (finish_id_expression
3511 (id_expression, decl, parser->scope,
3513 parser->integral_constant_expression_p,
3514 parser->allow_non_integral_constant_expression_p,
3515 &parser->non_integral_constant_expression_p,
3516 template_p, done, address_p,
3519 id_expr_token->location));
3521 cp_parser_error (parser, error_msg);
3525 /* Anything else is an error. */
3527 /* ...unless we have an Objective-C++ message or string literal,
3529 if (c_dialect_objc ()
3530 && (token->type == CPP_OPEN_SQUARE
3531 || token->type == CPP_OBJC_STRING))
3532 return cp_parser_objc_expression (parser);
3534 cp_parser_error (parser, "expected primary-expression");
3535 return error_mark_node;
3539 /* Parse an id-expression.
3546 :: [opt] nested-name-specifier template [opt] unqualified-id
3548 :: operator-function-id
3551 Return a representation of the unqualified portion of the
3552 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3553 a `::' or nested-name-specifier.
3555 Often, if the id-expression was a qualified-id, the caller will
3556 want to make a SCOPE_REF to represent the qualified-id. This
3557 function does not do this in order to avoid wastefully creating
3558 SCOPE_REFs when they are not required.
3560 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3563 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3564 uninstantiated templates.
3566 If *TEMPLATE_P is non-NULL, it is set to true iff the
3567 `template' keyword is used to explicitly indicate that the entity
3568 named is a template.
3570 If DECLARATOR_P is true, the id-expression is appearing as part of
3571 a declarator, rather than as part of an expression. */
3574 cp_parser_id_expression (cp_parser *parser,
3575 bool template_keyword_p,
3576 bool check_dependency_p,
3581 bool global_scope_p;
3582 bool nested_name_specifier_p;
3584 /* Assume the `template' keyword was not used. */
3586 *template_p = template_keyword_p;
3588 /* Look for the optional `::' operator. */
3590 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3592 /* Look for the optional nested-name-specifier. */
3593 nested_name_specifier_p
3594 = (cp_parser_nested_name_specifier_opt (parser,
3595 /*typename_keyword_p=*/false,
3600 /* If there is a nested-name-specifier, then we are looking at
3601 the first qualified-id production. */
3602 if (nested_name_specifier_p)
3605 tree saved_object_scope;
3606 tree saved_qualifying_scope;
3607 tree unqualified_id;
3610 /* See if the next token is the `template' keyword. */
3612 template_p = &is_template;
3613 *template_p = cp_parser_optional_template_keyword (parser);
3614 /* Name lookup we do during the processing of the
3615 unqualified-id might obliterate SCOPE. */
3616 saved_scope = parser->scope;
3617 saved_object_scope = parser->object_scope;
3618 saved_qualifying_scope = parser->qualifying_scope;
3619 /* Process the final unqualified-id. */
3620 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3623 /*optional_p=*/false);
3624 /* Restore the SAVED_SCOPE for our caller. */
3625 parser->scope = saved_scope;
3626 parser->object_scope = saved_object_scope;
3627 parser->qualifying_scope = saved_qualifying_scope;
3629 return unqualified_id;
3631 /* Otherwise, if we are in global scope, then we are looking at one
3632 of the other qualified-id productions. */
3633 else if (global_scope_p)
3638 /* Peek at the next token. */
3639 token = cp_lexer_peek_token (parser->lexer);
3641 /* If it's an identifier, and the next token is not a "<", then
3642 we can avoid the template-id case. This is an optimization
3643 for this common case. */
3644 if (token->type == CPP_NAME
3645 && !cp_parser_nth_token_starts_template_argument_list_p
3647 return cp_parser_identifier (parser);
3649 cp_parser_parse_tentatively (parser);
3650 /* Try a template-id. */
3651 id = cp_parser_template_id (parser,
3652 /*template_keyword_p=*/false,
3653 /*check_dependency_p=*/true,
3655 /* If that worked, we're done. */
3656 if (cp_parser_parse_definitely (parser))
3659 /* Peek at the next token. (Changes in the token buffer may
3660 have invalidated the pointer obtained above.) */
3661 token = cp_lexer_peek_token (parser->lexer);
3663 switch (token->type)
3666 return cp_parser_identifier (parser);
3669 if (token->keyword == RID_OPERATOR)
3670 return cp_parser_operator_function_id (parser);
3674 cp_parser_error (parser, "expected id-expression");
3675 return error_mark_node;
3679 return cp_parser_unqualified_id (parser, template_keyword_p,
3680 /*check_dependency_p=*/true,
3685 /* Parse an unqualified-id.
3689 operator-function-id
3690 conversion-function-id
3694 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3695 keyword, in a construct like `A::template ...'.
3697 Returns a representation of unqualified-id. For the `identifier'
3698 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3699 production a BIT_NOT_EXPR is returned; the operand of the
3700 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3701 other productions, see the documentation accompanying the
3702 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3703 names are looked up in uninstantiated templates. If DECLARATOR_P
3704 is true, the unqualified-id is appearing as part of a declarator,
3705 rather than as part of an expression. */
3708 cp_parser_unqualified_id (cp_parser* parser,
3709 bool template_keyword_p,
3710 bool check_dependency_p,
3716 /* Peek at the next token. */
3717 token = cp_lexer_peek_token (parser->lexer);
3719 switch (token->type)
3725 /* We don't know yet whether or not this will be a
3727 cp_parser_parse_tentatively (parser);
3728 /* Try a template-id. */
3729 id = cp_parser_template_id (parser, template_keyword_p,
3732 /* If it worked, we're done. */
3733 if (cp_parser_parse_definitely (parser))
3735 /* Otherwise, it's an ordinary identifier. */
3736 return cp_parser_identifier (parser);
3739 case CPP_TEMPLATE_ID:
3740 return cp_parser_template_id (parser, template_keyword_p,
3747 tree qualifying_scope;
3752 /* Consume the `~' token. */
3753 cp_lexer_consume_token (parser->lexer);
3754 /* Parse the class-name. The standard, as written, seems to
3757 template <typename T> struct S { ~S (); };
3758 template <typename T> S<T>::~S() {}
3760 is invalid, since `~' must be followed by a class-name, but
3761 `S<T>' is dependent, and so not known to be a class.
3762 That's not right; we need to look in uninstantiated
3763 templates. A further complication arises from:
3765 template <typename T> void f(T t) {
3769 Here, it is not possible to look up `T' in the scope of `T'
3770 itself. We must look in both the current scope, and the
3771 scope of the containing complete expression.
3773 Yet another issue is:
3782 The standard does not seem to say that the `S' in `~S'
3783 should refer to the type `S' and not the data member
3786 /* DR 244 says that we look up the name after the "~" in the
3787 same scope as we looked up the qualifying name. That idea
3788 isn't fully worked out; it's more complicated than that. */
3789 scope = parser->scope;
3790 object_scope = parser->object_scope;
3791 qualifying_scope = parser->qualifying_scope;
3793 /* Check for invalid scopes. */
3794 if (scope == error_mark_node)
3796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3797 cp_lexer_consume_token (parser->lexer);
3798 return error_mark_node;
3800 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3803 error ("%Hscope %qT before %<~%> is not a class-name",
3804 &token->location, scope);
3805 cp_parser_simulate_error (parser);
3806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3807 cp_lexer_consume_token (parser->lexer);
3808 return error_mark_node;
3810 gcc_assert (!scope || TYPE_P (scope));
3812 /* If the name is of the form "X::~X" it's OK. */
3813 token = cp_lexer_peek_token (parser->lexer);
3815 && token->type == CPP_NAME
3816 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3818 && constructor_name_p (token->u.value, scope))
3820 cp_lexer_consume_token (parser->lexer);
3821 return build_nt (BIT_NOT_EXPR, scope);
3824 /* If there was an explicit qualification (S::~T), first look
3825 in the scope given by the qualification (i.e., S). */
3827 type_decl = NULL_TREE;
3830 cp_parser_parse_tentatively (parser);
3831 type_decl = cp_parser_class_name (parser,
3832 /*typename_keyword_p=*/false,
3833 /*template_keyword_p=*/false,
3835 /*check_dependency=*/false,
3836 /*class_head_p=*/false,
3838 if (cp_parser_parse_definitely (parser))
3841 /* In "N::S::~S", look in "N" as well. */
3842 if (!done && scope && qualifying_scope)
3844 cp_parser_parse_tentatively (parser);
3845 parser->scope = qualifying_scope;
3846 parser->object_scope = NULL_TREE;
3847 parser->qualifying_scope = NULL_TREE;
3849 = cp_parser_class_name (parser,
3850 /*typename_keyword_p=*/false,
3851 /*template_keyword_p=*/false,
3853 /*check_dependency=*/false,
3854 /*class_head_p=*/false,
3856 if (cp_parser_parse_definitely (parser))
3859 /* In "p->S::~T", look in the scope given by "*p" as well. */
3860 else if (!done && object_scope)
3862 cp_parser_parse_tentatively (parser);
3863 parser->scope = object_scope;
3864 parser->object_scope = NULL_TREE;
3865 parser->qualifying_scope = NULL_TREE;
3867 = cp_parser_class_name (parser,
3868 /*typename_keyword_p=*/false,
3869 /*template_keyword_p=*/false,
3871 /*check_dependency=*/false,
3872 /*class_head_p=*/false,
3874 if (cp_parser_parse_definitely (parser))
3877 /* Look in the surrounding context. */
3880 parser->scope = NULL_TREE;
3881 parser->object_scope = NULL_TREE;
3882 parser->qualifying_scope = NULL_TREE;
3884 = cp_parser_class_name (parser,
3885 /*typename_keyword_p=*/false,
3886 /*template_keyword_p=*/false,
3888 /*check_dependency=*/false,
3889 /*class_head_p=*/false,
3892 /* If an error occurred, assume that the name of the
3893 destructor is the same as the name of the qualifying
3894 class. That allows us to keep parsing after running
3895 into ill-formed destructor names. */
3896 if (type_decl == error_mark_node && scope)
3897 return build_nt (BIT_NOT_EXPR, scope);
3898 else if (type_decl == error_mark_node)
3899 return error_mark_node;
3901 /* Check that destructor name and scope match. */
3902 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3904 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3905 error ("%Hdeclaration of %<~%T%> as member of %qT",
3906 &token->location, type_decl, scope);
3907 cp_parser_simulate_error (parser);
3908 return error_mark_node;
3913 A typedef-name that names a class shall not be used as the
3914 identifier in the declarator for a destructor declaration. */
3916 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3917 && !DECL_SELF_REFERENCE_P (type_decl)
3918 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3919 error ("%Htypedef-name %qD used as destructor declarator",
3920 &token->location, type_decl);
3922 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3926 if (token->keyword == RID_OPERATOR)
3930 /* This could be a template-id, so we try that first. */
3931 cp_parser_parse_tentatively (parser);
3932 /* Try a template-id. */
3933 id = cp_parser_template_id (parser, template_keyword_p,
3934 /*check_dependency_p=*/true,
3936 /* If that worked, we're done. */
3937 if (cp_parser_parse_definitely (parser))
3939 /* We still don't know whether we're looking at an
3940 operator-function-id or a conversion-function-id. */
3941 cp_parser_parse_tentatively (parser);
3942 /* Try an operator-function-id. */
3943 id = cp_parser_operator_function_id (parser);
3944 /* If that didn't work, try a conversion-function-id. */
3945 if (!cp_parser_parse_definitely (parser))
3946 id = cp_parser_conversion_function_id (parser);
3955 cp_parser_error (parser, "expected unqualified-id");
3956 return error_mark_node;
3960 /* Parse an (optional) nested-name-specifier.
3962 nested-name-specifier: [C++98]
3963 class-or-namespace-name :: nested-name-specifier [opt]
3964 class-or-namespace-name :: template nested-name-specifier [opt]
3966 nested-name-specifier: [C++0x]
3969 nested-name-specifier identifier ::
3970 nested-name-specifier template [opt] simple-template-id ::
3972 PARSER->SCOPE should be set appropriately before this function is
3973 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3974 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3977 Sets PARSER->SCOPE to the class (TYPE) or namespace
3978 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3979 it unchanged if there is no nested-name-specifier. Returns the new
3980 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3982 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3983 part of a declaration and/or decl-specifier. */
3986 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3987 bool typename_keyword_p,
3988 bool check_dependency_p,
3990 bool is_declaration)
3992 bool success = false;
3993 cp_token_position start = 0;
3996 /* Remember where the nested-name-specifier starts. */
3997 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3999 start = cp_lexer_token_position (parser->lexer, false);
4000 push_deferring_access_checks (dk_deferred);
4007 tree saved_qualifying_scope;
4008 bool template_keyword_p;
4010 /* Spot cases that cannot be the beginning of a
4011 nested-name-specifier. */
4012 token = cp_lexer_peek_token (parser->lexer);
4014 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4015 the already parsed nested-name-specifier. */
4016 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4018 /* Grab the nested-name-specifier and continue the loop. */
4019 cp_parser_pre_parsed_nested_name_specifier (parser);
4020 /* If we originally encountered this nested-name-specifier
4021 with IS_DECLARATION set to false, we will not have
4022 resolved TYPENAME_TYPEs, so we must do so here. */
4024 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4026 new_scope = resolve_typename_type (parser->scope,
4027 /*only_current_p=*/false);
4028 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4029 parser->scope = new_scope;
4035 /* Spot cases that cannot be the beginning of a
4036 nested-name-specifier. On the second and subsequent times
4037 through the loop, we look for the `template' keyword. */
4038 if (success && token->keyword == RID_TEMPLATE)
4040 /* A template-id can start a nested-name-specifier. */
4041 else if (token->type == CPP_TEMPLATE_ID)
4045 /* If the next token is not an identifier, then it is
4046 definitely not a type-name or namespace-name. */
4047 if (token->type != CPP_NAME)
4049 /* If the following token is neither a `<' (to begin a
4050 template-id), nor a `::', then we are not looking at a
4051 nested-name-specifier. */
4052 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4053 if (token->type != CPP_SCOPE
4054 && !cp_parser_nth_token_starts_template_argument_list_p
4059 /* The nested-name-specifier is optional, so we parse
4061 cp_parser_parse_tentatively (parser);
4063 /* Look for the optional `template' keyword, if this isn't the
4064 first time through the loop. */
4066 template_keyword_p = cp_parser_optional_template_keyword (parser);
4068 template_keyword_p = false;
4070 /* Save the old scope since the name lookup we are about to do
4071 might destroy it. */
4072 old_scope = parser->scope;
4073 saved_qualifying_scope = parser->qualifying_scope;
4074 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4075 look up names in "X<T>::I" in order to determine that "Y" is
4076 a template. So, if we have a typename at this point, we make
4077 an effort to look through it. */
4079 && !typename_keyword_p
4081 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4082 parser->scope = resolve_typename_type (parser->scope,
4083 /*only_current_p=*/false);
4084 /* Parse the qualifying entity. */
4086 = cp_parser_qualifying_entity (parser,
4092 /* Look for the `::' token. */
4093 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4095 /* If we found what we wanted, we keep going; otherwise, we're
4097 if (!cp_parser_parse_definitely (parser))
4099 bool error_p = false;
4101 /* Restore the OLD_SCOPE since it was valid before the
4102 failed attempt at finding the last
4103 class-or-namespace-name. */
4104 parser->scope = old_scope;
4105 parser->qualifying_scope = saved_qualifying_scope;
4106 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4108 /* If the next token is an identifier, and the one after
4109 that is a `::', then any valid interpretation would have
4110 found a class-or-namespace-name. */
4111 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4112 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4114 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4117 token = cp_lexer_consume_token (parser->lexer);
4120 if (!token->ambiguous_p)
4123 tree ambiguous_decls;
4125 decl = cp_parser_lookup_name (parser, token->u.value,
4127 /*is_template=*/false,
4128 /*is_namespace=*/false,
4129 /*check_dependency=*/true,
4132 if (TREE_CODE (decl) == TEMPLATE_DECL)
4133 error ("%H%qD used without template parameters",
4134 &token->location, decl);
4135 else if (ambiguous_decls)
4137 error ("%Hreference to %qD is ambiguous",
4138 &token->location, token->u.value);
4139 print_candidates (ambiguous_decls);
4140 decl = error_mark_node;
4144 const char* msg = "is not a class or namespace";
4145 if (cxx_dialect != cxx98)
4146 msg = "is not a class, namespace, or enumeration";
4147 cp_parser_name_lookup_error
4148 (parser, token->u.value, decl, msg,
4152 parser->scope = error_mark_node;
4154 /* Treat this as a successful nested-name-specifier
4159 If the name found is not a class-name (clause
4160 _class_) or namespace-name (_namespace.def_), the
4161 program is ill-formed. */
4164 cp_lexer_consume_token (parser->lexer);
4168 /* We've found one valid nested-name-specifier. */
4170 /* Name lookup always gives us a DECL. */
4171 if (TREE_CODE (new_scope) == TYPE_DECL)
4172 new_scope = TREE_TYPE (new_scope);
4173 /* Uses of "template" must be followed by actual templates. */
4174 if (template_keyword_p
4175 && !(CLASS_TYPE_P (new_scope)
4176 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4177 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4178 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4179 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4180 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4181 == TEMPLATE_ID_EXPR)))
4182 permerror (input_location, TYPE_P (new_scope)
4183 ? "%qT is not a template"
4184 : "%qD is not a template",
4186 /* If it is a class scope, try to complete it; we are about to
4187 be looking up names inside the class. */
4188 if (TYPE_P (new_scope)
4189 /* Since checking types for dependency can be expensive,
4190 avoid doing it if the type is already complete. */
4191 && !COMPLETE_TYPE_P (new_scope)
4192 /* Do not try to complete dependent types. */
4193 && !dependent_type_p (new_scope))
4195 new_scope = complete_type (new_scope);
4196 /* If it is a typedef to current class, use the current
4197 class instead, as the typedef won't have any names inside
4199 if (!COMPLETE_TYPE_P (new_scope)
4200 && currently_open_class (new_scope))
4201 new_scope = TYPE_MAIN_VARIANT (new_scope);
4203 /* Make sure we look in the right scope the next time through
4205 parser->scope = new_scope;
4208 /* If parsing tentatively, replace the sequence of tokens that makes
4209 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4210 token. That way, should we re-parse the token stream, we will
4211 not have to repeat the effort required to do the parse, nor will
4212 we issue duplicate error messages. */
4213 if (success && start)
4217 token = cp_lexer_token_at (parser->lexer, start);
4218 /* Reset the contents of the START token. */
4219 token->type = CPP_NESTED_NAME_SPECIFIER;
4220 /* Retrieve any deferred checks. Do not pop this access checks yet
4221 so the memory will not be reclaimed during token replacing below. */
4222 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4223 token->u.tree_check_value->value = parser->scope;
4224 token->u.tree_check_value->checks = get_deferred_access_checks ();
4225 token->u.tree_check_value->qualifying_scope =
4226 parser->qualifying_scope;
4227 token->keyword = RID_MAX;
4229 /* Purge all subsequent tokens. */
4230 cp_lexer_purge_tokens_after (parser->lexer, start);
4234 pop_to_parent_deferring_access_checks ();
4236 return success ? parser->scope : NULL_TREE;
4239 /* Parse a nested-name-specifier. See
4240 cp_parser_nested_name_specifier_opt for details. This function
4241 behaves identically, except that it will an issue an error if no
4242 nested-name-specifier is present. */
4245 cp_parser_nested_name_specifier (cp_parser *parser,
4246 bool typename_keyword_p,
4247 bool check_dependency_p,
4249 bool is_declaration)
4253 /* Look for the nested-name-specifier. */
4254 scope = cp_parser_nested_name_specifier_opt (parser,
4259 /* If it was not present, issue an error message. */
4262 cp_parser_error (parser, "expected nested-name-specifier");
4263 parser->scope = NULL_TREE;
4269 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4270 this is either a class-name or a namespace-name (which corresponds
4271 to the class-or-namespace-name production in the grammar). For
4272 C++0x, it can also be a type-name that refers to an enumeration
4275 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4276 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4277 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4278 TYPE_P is TRUE iff the next name should be taken as a class-name,
4279 even the same name is declared to be another entity in the same
4282 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4283 specified by the class-or-namespace-name. If neither is found the
4284 ERROR_MARK_NODE is returned. */
4287 cp_parser_qualifying_entity (cp_parser *parser,
4288 bool typename_keyword_p,
4289 bool template_keyword_p,
4290 bool check_dependency_p,
4292 bool is_declaration)
4295 tree saved_qualifying_scope;
4296 tree saved_object_scope;
4299 bool successful_parse_p;
4301 /* Before we try to parse the class-name, we must save away the
4302 current PARSER->SCOPE since cp_parser_class_name will destroy
4304 saved_scope = parser->scope;
4305 saved_qualifying_scope = parser->qualifying_scope;
4306 saved_object_scope = parser->object_scope;
4307 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4308 there is no need to look for a namespace-name. */
4309 only_class_p = template_keyword_p
4310 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4312 cp_parser_parse_tentatively (parser);
4313 scope = cp_parser_class_name (parser,
4316 type_p ? class_type : none_type,
4318 /*class_head_p=*/false,
4320 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4321 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4323 && cxx_dialect != cxx98
4324 && !successful_parse_p)
4326 /* Restore the saved scope. */
4327 parser->scope = saved_scope;
4328 parser->qualifying_scope = saved_qualifying_scope;
4329 parser->object_scope = saved_object_scope;
4331 /* Parse tentatively. */
4332 cp_parser_parse_tentatively (parser);
4334 /* Parse a typedef-name or enum-name. */
4335 scope = cp_parser_nonclass_name (parser);
4336 successful_parse_p = cp_parser_parse_definitely (parser);
4338 /* If that didn't work, try for a namespace-name. */
4339 if (!only_class_p && !successful_parse_p)
4341 /* Restore the saved scope. */
4342 parser->scope = saved_scope;
4343 parser->qualifying_scope = saved_qualifying_scope;
4344 parser->object_scope = saved_object_scope;
4345 /* If we are not looking at an identifier followed by the scope
4346 resolution operator, then this is not part of a
4347 nested-name-specifier. (Note that this function is only used
4348 to parse the components of a nested-name-specifier.) */
4349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4350 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4351 return error_mark_node;
4352 scope = cp_parser_namespace_name (parser);
4358 /* Parse a postfix-expression.
4362 postfix-expression [ expression ]
4363 postfix-expression ( expression-list [opt] )
4364 simple-type-specifier ( expression-list [opt] )
4365 typename :: [opt] nested-name-specifier identifier
4366 ( expression-list [opt] )
4367 typename :: [opt] nested-name-specifier template [opt] template-id
4368 ( expression-list [opt] )
4369 postfix-expression . template [opt] id-expression
4370 postfix-expression -> template [opt] id-expression
4371 postfix-expression . pseudo-destructor-name
4372 postfix-expression -> pseudo-destructor-name
4373 postfix-expression ++
4374 postfix-expression --
4375 dynamic_cast < type-id > ( expression )
4376 static_cast < type-id > ( expression )
4377 reinterpret_cast < type-id > ( expression )
4378 const_cast < type-id > ( expression )
4379 typeid ( expression )
4385 ( type-id ) { initializer-list , [opt] }
4387 This extension is a GNU version of the C99 compound-literal
4388 construct. (The C99 grammar uses `type-name' instead of `type-id',
4389 but they are essentially the same concept.)
4391 If ADDRESS_P is true, the postfix expression is the operand of the
4392 `&' operator. CAST_P is true if this expression is the target of a
4395 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4396 class member access expressions [expr.ref].
4398 Returns a representation of the expression. */
4401 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4402 bool member_access_only_p)
4406 cp_id_kind idk = CP_ID_KIND_NONE;
4407 tree postfix_expression = NULL_TREE;
4408 bool is_member_access = false;
4410 /* Peek at the next token. */
4411 token = cp_lexer_peek_token (parser->lexer);
4412 /* Some of the productions are determined by keywords. */
4413 keyword = token->keyword;
4423 const char *saved_message;
4425 /* All of these can be handled in the same way from the point
4426 of view of parsing. Begin by consuming the token
4427 identifying the cast. */
4428 cp_lexer_consume_token (parser->lexer);
4430 /* New types cannot be defined in the cast. */
4431 saved_message = parser->type_definition_forbidden_message;
4432 parser->type_definition_forbidden_message
4433 = "types may not be defined in casts";
4435 /* Look for the opening `<'. */
4436 cp_parser_require (parser, CPP_LESS, "%<<%>");
4437 /* Parse the type to which we are casting. */
4438 type = cp_parser_type_id (parser);
4439 /* Look for the closing `>'. */
4440 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4441 /* Restore the old message. */
4442 parser->type_definition_forbidden_message = saved_message;
4444 /* And the expression which is being cast. */
4445 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4446 expression = cp_parser_expression (parser, /*cast_p=*/true);
4447 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4449 /* Only type conversions to integral or enumeration types
4450 can be used in constant-expressions. */
4451 if (!cast_valid_in_integral_constant_expression_p (type)
4452 && (cp_parser_non_integral_constant_expression
4454 "a cast to a type other than an integral or "
4455 "enumeration type")))
4456 return error_mark_node;
4462 = build_dynamic_cast (type, expression, tf_warning_or_error);
4466 = build_static_cast (type, expression, tf_warning_or_error);
4470 = build_reinterpret_cast (type, expression,
4471 tf_warning_or_error);
4475 = build_const_cast (type, expression, tf_warning_or_error);
4486 const char *saved_message;
4487 bool saved_in_type_id_in_expr_p;
4489 /* Consume the `typeid' token. */
4490 cp_lexer_consume_token (parser->lexer);
4491 /* Look for the `(' token. */
4492 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4493 /* Types cannot be defined in a `typeid' expression. */
4494 saved_message = parser->type_definition_forbidden_message;
4495 parser->type_definition_forbidden_message
4496 = "types may not be defined in a %<typeid%> expression";
4497 /* We can't be sure yet whether we're looking at a type-id or an
4499 cp_parser_parse_tentatively (parser);
4500 /* Try a type-id first. */
4501 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4502 parser->in_type_id_in_expr_p = true;
4503 type = cp_parser_type_id (parser);
4504 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4505 /* Look for the `)' token. Otherwise, we can't be sure that
4506 we're not looking at an expression: consider `typeid (int
4507 (3))', for example. */
4508 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4509 /* If all went well, simply lookup the type-id. */
4510 if (cp_parser_parse_definitely (parser))
4511 postfix_expression = get_typeid (type);
4512 /* Otherwise, fall back to the expression variant. */
4517 /* Look for an expression. */
4518 expression = cp_parser_expression (parser, /*cast_p=*/false);
4519 /* Compute its typeid. */
4520 postfix_expression = build_typeid (expression);
4521 /* Look for the `)' token. */
4522 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4524 /* Restore the saved message. */
4525 parser->type_definition_forbidden_message = saved_message;
4526 /* `typeid' may not appear in an integral constant expression. */
4527 if (cp_parser_non_integral_constant_expression(parser,
4528 "%<typeid%> operator"))
4529 return error_mark_node;
4536 /* The syntax permitted here is the same permitted for an
4537 elaborated-type-specifier. */
4538 type = cp_parser_elaborated_type_specifier (parser,
4539 /*is_friend=*/false,
4540 /*is_declaration=*/false);
4541 postfix_expression = cp_parser_functional_cast (parser, type);
4549 /* If the next thing is a simple-type-specifier, we may be
4550 looking at a functional cast. We could also be looking at
4551 an id-expression. So, we try the functional cast, and if
4552 that doesn't work we fall back to the primary-expression. */
4553 cp_parser_parse_tentatively (parser);
4554 /* Look for the simple-type-specifier. */
4555 type = cp_parser_simple_type_specifier (parser,
4556 /*decl_specs=*/NULL,
4557 CP_PARSER_FLAGS_NONE);
4558 /* Parse the cast itself. */
4559 if (!cp_parser_error_occurred (parser))
4561 = cp_parser_functional_cast (parser, type);
4562 /* If that worked, we're done. */
4563 if (cp_parser_parse_definitely (parser))
4566 /* If the functional-cast didn't work out, try a
4567 compound-literal. */
4568 if (cp_parser_allow_gnu_extensions_p (parser)
4569 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4571 VEC(constructor_elt,gc) *initializer_list = NULL;
4572 bool saved_in_type_id_in_expr_p;
4574 cp_parser_parse_tentatively (parser);
4575 /* Consume the `('. */
4576 cp_lexer_consume_token (parser->lexer);
4577 /* Parse the type. */
4578 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4579 parser->in_type_id_in_expr_p = true;
4580 type = cp_parser_type_id (parser);
4581 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4582 /* Look for the `)'. */
4583 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4584 /* Look for the `{'. */
4585 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4586 /* If things aren't going well, there's no need to
4588 if (!cp_parser_error_occurred (parser))
4590 bool non_constant_p;
4591 /* Parse the initializer-list. */
4593 = cp_parser_initializer_list (parser, &non_constant_p);
4594 /* Allow a trailing `,'. */
4595 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4596 cp_lexer_consume_token (parser->lexer);
4597 /* Look for the final `}'. */
4598 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4600 /* If that worked, we're definitely looking at a
4601 compound-literal expression. */
4602 if (cp_parser_parse_definitely (parser))
4604 /* Warn the user that a compound literal is not
4605 allowed in standard C++. */
4606 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4607 /* For simplicity, we disallow compound literals in
4608 constant-expressions. We could
4609 allow compound literals of integer type, whose
4610 initializer was a constant, in constant
4611 expressions. Permitting that usage, as a further
4612 extension, would not change the meaning of any
4613 currently accepted programs. (Of course, as
4614 compound literals are not part of ISO C++, the
4615 standard has nothing to say.) */
4616 if (cp_parser_non_integral_constant_expression
4617 (parser, "non-constant compound literals"))
4619 postfix_expression = error_mark_node;
4622 /* Form the representation of the compound-literal. */
4624 = (finish_compound_literal
4625 (type, build_constructor (init_list_type_node,
4626 initializer_list)));
4631 /* It must be a primary-expression. */
4633 = cp_parser_primary_expression (parser, address_p, cast_p,
4634 /*template_arg_p=*/false,
4640 /* Keep looping until the postfix-expression is complete. */
4643 if (idk == CP_ID_KIND_UNQUALIFIED
4644 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4645 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4646 /* It is not a Koenig lookup function call. */
4648 = unqualified_name_lookup_error (postfix_expression);
4650 /* Peek at the next token. */
4651 token = cp_lexer_peek_token (parser->lexer);
4653 switch (token->type)
4655 case CPP_OPEN_SQUARE:
4657 = cp_parser_postfix_open_square_expression (parser,
4660 idk = CP_ID_KIND_NONE;
4661 is_member_access = false;
4664 case CPP_OPEN_PAREN:
4665 /* postfix-expression ( expression-list [opt] ) */
4668 bool is_builtin_constant_p;
4669 bool saved_integral_constant_expression_p = false;
4670 bool saved_non_integral_constant_expression_p = false;
4673 is_member_access = false;
4675 is_builtin_constant_p
4676 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4677 if (is_builtin_constant_p)
4679 /* The whole point of __builtin_constant_p is to allow
4680 non-constant expressions to appear as arguments. */
4681 saved_integral_constant_expression_p
4682 = parser->integral_constant_expression_p;
4683 saved_non_integral_constant_expression_p
4684 = parser->non_integral_constant_expression_p;
4685 parser->integral_constant_expression_p = false;
4687 args = (cp_parser_parenthesized_expression_list
4688 (parser, /*is_attribute_list=*/false,
4689 /*cast_p=*/false, /*allow_expansion_p=*/true,
4690 /*non_constant_p=*/NULL));
4691 if (is_builtin_constant_p)
4693 parser->integral_constant_expression_p
4694 = saved_integral_constant_expression_p;
4695 parser->non_integral_constant_expression_p
4696 = saved_non_integral_constant_expression_p;
4699 if (args == error_mark_node)
4701 postfix_expression = error_mark_node;
4705 /* Function calls are not permitted in
4706 constant-expressions. */
4707 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4708 && cp_parser_non_integral_constant_expression (parser,
4711 postfix_expression = error_mark_node;
4716 if (idk == CP_ID_KIND_UNQUALIFIED)
4718 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4724 = perform_koenig_lookup (postfix_expression, args);
4728 = unqualified_fn_lookup_error (postfix_expression);
4730 /* We do not perform argument-dependent lookup if
4731 normal lookup finds a non-function, in accordance
4732 with the expected resolution of DR 218. */
4733 else if (args && is_overloaded_fn (postfix_expression))
4735 tree fn = get_first_fn (postfix_expression);
4737 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4738 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4740 /* Only do argument dependent lookup if regular
4741 lookup does not find a set of member functions.
4742 [basic.lookup.koenig]/2a */
4743 if (!DECL_FUNCTION_MEMBER_P (fn))
4747 = perform_koenig_lookup (postfix_expression, args);
4752 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4754 tree instance = TREE_OPERAND (postfix_expression, 0);
4755 tree fn = TREE_OPERAND (postfix_expression, 1);
4757 if (processing_template_decl
4758 && (type_dependent_expression_p (instance)
4759 || (!BASELINK_P (fn)
4760 && TREE_CODE (fn) != FIELD_DECL)
4761 || type_dependent_expression_p (fn)
4762 || any_type_dependent_arguments_p (args)))
4765 = build_nt_call_list (postfix_expression, args);
4769 if (BASELINK_P (fn))
4771 = (build_new_method_call
4772 (instance, fn, args, NULL_TREE,
4773 (idk == CP_ID_KIND_QUALIFIED
4774 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4776 tf_warning_or_error));
4779 = finish_call_expr (postfix_expression, args,
4780 /*disallow_virtual=*/false,
4782 tf_warning_or_error);
4784 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4785 || TREE_CODE (postfix_expression) == MEMBER_REF
4786 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4787 postfix_expression = (build_offset_ref_call_from_tree
4788 (postfix_expression, args));
4789 else if (idk == CP_ID_KIND_QUALIFIED)
4790 /* A call to a static class member, or a namespace-scope
4793 = finish_call_expr (postfix_expression, args,
4794 /*disallow_virtual=*/true,
4796 tf_warning_or_error);
4798 /* All other function calls. */
4800 = finish_call_expr (postfix_expression, args,
4801 /*disallow_virtual=*/false,
4803 tf_warning_or_error);
4805 if (warn_disallowed_functions)
4806 warn_if_disallowed_function_p (postfix_expression);
4808 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4809 idk = CP_ID_KIND_NONE;
4815 /* postfix-expression . template [opt] id-expression
4816 postfix-expression . pseudo-destructor-name
4817 postfix-expression -> template [opt] id-expression
4818 postfix-expression -> pseudo-destructor-name */
4820 /* Consume the `.' or `->' operator. */
4821 cp_lexer_consume_token (parser->lexer);
4824 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4829 is_member_access = true;
4833 /* postfix-expression ++ */
4834 /* Consume the `++' token. */
4835 cp_lexer_consume_token (parser->lexer);
4836 /* Generate a representation for the complete expression. */
4838 = finish_increment_expr (postfix_expression,
4839 POSTINCREMENT_EXPR);
4840 /* Increments may not appear in constant-expressions. */
4841 if (cp_parser_non_integral_constant_expression (parser,
4843 postfix_expression = error_mark_node;
4844 idk = CP_ID_KIND_NONE;
4845 is_member_access = false;
4848 case CPP_MINUS_MINUS:
4849 /* postfix-expression -- */
4850 /* Consume the `--' token. */
4851 cp_lexer_consume_token (parser->lexer);
4852 /* Generate a representation for the complete expression. */
4854 = finish_increment_expr (postfix_expression,
4855 POSTDECREMENT_EXPR);
4856 /* Decrements may not appear in constant-expressions. */
4857 if (cp_parser_non_integral_constant_expression (parser,
4859 postfix_expression = error_mark_node;
4860 idk = CP_ID_KIND_NONE;
4861 is_member_access = false;
4865 if (member_access_only_p)
4866 return is_member_access? postfix_expression : error_mark_node;
4868 return postfix_expression;
4872 /* We should never get here. */
4874 return error_mark_node;
4877 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4878 by cp_parser_builtin_offsetof. We're looking for
4880 postfix-expression [ expression ]
4882 FOR_OFFSETOF is set if we're being called in that context, which
4883 changes how we deal with integer constant expressions. */
4886 cp_parser_postfix_open_square_expression (cp_parser *parser,
4887 tree postfix_expression,
4892 /* Consume the `[' token. */
4893 cp_lexer_consume_token (parser->lexer);
4895 /* Parse the index expression. */
4896 /* ??? For offsetof, there is a question of what to allow here. If
4897 offsetof is not being used in an integral constant expression context,
4898 then we *could* get the right answer by computing the value at runtime.
4899 If we are in an integral constant expression context, then we might
4900 could accept any constant expression; hard to say without analysis.
4901 Rather than open the barn door too wide right away, allow only integer
4902 constant expressions here. */
4904 index = cp_parser_constant_expression (parser, false, NULL);
4906 index = cp_parser_expression (parser, /*cast_p=*/false);
4908 /* Look for the closing `]'. */
4909 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4911 /* Build the ARRAY_REF. */
4912 postfix_expression = grok_array_decl (postfix_expression, index);
4914 /* When not doing offsetof, array references are not permitted in
4915 constant-expressions. */
4917 && (cp_parser_non_integral_constant_expression
4918 (parser, "an array reference")))
4919 postfix_expression = error_mark_node;
4921 return postfix_expression;
4924 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4925 by cp_parser_builtin_offsetof. We're looking for
4927 postfix-expression . template [opt] id-expression
4928 postfix-expression . pseudo-destructor-name
4929 postfix-expression -> template [opt] id-expression
4930 postfix-expression -> pseudo-destructor-name
4932 FOR_OFFSETOF is set if we're being called in that context. That sorta
4933 limits what of the above we'll actually accept, but nevermind.
4934 TOKEN_TYPE is the "." or "->" token, which will already have been
4935 removed from the stream. */
4938 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4939 enum cpp_ttype token_type,
4940 tree postfix_expression,
4941 bool for_offsetof, cp_id_kind *idk,
4942 location_t location)
4946 bool pseudo_destructor_p;
4947 tree scope = NULL_TREE;
4949 /* If this is a `->' operator, dereference the pointer. */
4950 if (token_type == CPP_DEREF)
4951 postfix_expression = build_x_arrow (postfix_expression);
4952 /* Check to see whether or not the expression is type-dependent. */
4953 dependent_p = type_dependent_expression_p (postfix_expression);
4954 /* The identifier following the `->' or `.' is not qualified. */
4955 parser->scope = NULL_TREE;
4956 parser->qualifying_scope = NULL_TREE;
4957 parser->object_scope = NULL_TREE;
4958 *idk = CP_ID_KIND_NONE;
4959 /* Enter the scope corresponding to the type of the object
4960 given by the POSTFIX_EXPRESSION. */
4961 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4963 scope = TREE_TYPE (postfix_expression);
4964 /* According to the standard, no expression should ever have
4965 reference type. Unfortunately, we do not currently match
4966 the standard in this respect in that our internal representation
4967 of an expression may have reference type even when the standard
4968 says it does not. Therefore, we have to manually obtain the
4969 underlying type here. */
4970 scope = non_reference (scope);
4971 /* The type of the POSTFIX_EXPRESSION must be complete. */
4972 if (scope == unknown_type_node)
4974 error ("%H%qE does not have class type", &location, postfix_expression);
4978 scope = complete_type_or_else (scope, NULL_TREE);
4979 /* Let the name lookup machinery know that we are processing a
4980 class member access expression. */
4981 parser->context->object_type = scope;
4982 /* If something went wrong, we want to be able to discern that case,
4983 as opposed to the case where there was no SCOPE due to the type
4984 of expression being dependent. */
4986 scope = error_mark_node;
4987 /* If the SCOPE was erroneous, make the various semantic analysis
4988 functions exit quickly -- and without issuing additional error
4990 if (scope == error_mark_node)
4991 postfix_expression = error_mark_node;
4994 /* Assume this expression is not a pseudo-destructor access. */
4995 pseudo_destructor_p = false;
4997 /* If the SCOPE is a scalar type, then, if this is a valid program,
4998 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4999 is type dependent, it can be pseudo-destructor-name or something else.
5000 Try to parse it as pseudo-destructor-name first. */
5001 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5006 cp_parser_parse_tentatively (parser);
5007 /* Parse the pseudo-destructor-name. */
5009 cp_parser_pseudo_destructor_name (parser, &s, &type);
5011 && (cp_parser_error_occurred (parser)
5012 || TREE_CODE (type) != TYPE_DECL
5013 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5014 cp_parser_abort_tentative_parse (parser);
5015 else if (cp_parser_parse_definitely (parser))
5017 pseudo_destructor_p = true;
5019 = finish_pseudo_destructor_expr (postfix_expression,
5020 s, TREE_TYPE (type));
5024 if (!pseudo_destructor_p)
5026 /* If the SCOPE is not a scalar type, we are looking at an
5027 ordinary class member access expression, rather than a
5028 pseudo-destructor-name. */
5030 cp_token *token = cp_lexer_peek_token (parser->lexer);
5031 /* Parse the id-expression. */
5032 name = (cp_parser_id_expression
5034 cp_parser_optional_template_keyword (parser),
5035 /*check_dependency_p=*/true,
5037 /*declarator_p=*/false,
5038 /*optional_p=*/false));
5039 /* In general, build a SCOPE_REF if the member name is qualified.
5040 However, if the name was not dependent and has already been
5041 resolved; there is no need to build the SCOPE_REF. For example;
5043 struct X { void f(); };
5044 template <typename T> void f(T* t) { t->X::f(); }
5046 Even though "t" is dependent, "X::f" is not and has been resolved
5047 to a BASELINK; there is no need to include scope information. */
5049 /* But we do need to remember that there was an explicit scope for
5050 virtual function calls. */
5052 *idk = CP_ID_KIND_QUALIFIED;
5054 /* If the name is a template-id that names a type, we will get a
5055 TYPE_DECL here. That is invalid code. */
5056 if (TREE_CODE (name) == TYPE_DECL)
5058 error ("%Hinvalid use of %qD", &token->location, name);
5059 postfix_expression = error_mark_node;
5063 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5065 name = build_qualified_name (/*type=*/NULL_TREE,
5069 parser->scope = NULL_TREE;
5070 parser->qualifying_scope = NULL_TREE;
5071 parser->object_scope = NULL_TREE;
5073 if (scope && name && BASELINK_P (name))
5074 adjust_result_of_qualified_name_lookup
5075 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5077 = finish_class_member_access_expr (postfix_expression, name,
5079 tf_warning_or_error);
5083 /* We no longer need to look up names in the scope of the object on
5084 the left-hand side of the `.' or `->' operator. */
5085 parser->context->object_type = NULL_TREE;
5087 /* Outside of offsetof, these operators may not appear in
5088 constant-expressions. */
5090 && (cp_parser_non_integral_constant_expression
5091 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5092 postfix_expression = error_mark_node;
5094 return postfix_expression;
5097 /* Parse a parenthesized expression-list.
5100 assignment-expression
5101 expression-list, assignment-expression
5106 identifier, expression-list
5108 CAST_P is true if this expression is the target of a cast.
5110 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5113 Returns a TREE_LIST. The TREE_VALUE of each node is a
5114 representation of an assignment-expression. Note that a TREE_LIST
5115 is returned even if there is only a single expression in the list.
5116 error_mark_node is returned if the ( and or ) are
5117 missing. NULL_TREE is returned on no expressions. The parentheses
5118 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5119 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5120 indicates whether or not all of the expressions in the list were
5124 cp_parser_parenthesized_expression_list (cp_parser* parser,
5125 bool is_attribute_list,
5127 bool allow_expansion_p,
5128 bool *non_constant_p)
5130 tree expression_list = NULL_TREE;
5131 bool fold_expr_p = is_attribute_list;
5132 tree identifier = NULL_TREE;
5133 bool saved_greater_than_is_operator_p;
5135 /* Assume all the expressions will be constant. */
5137 *non_constant_p = false;
5139 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5140 return error_mark_node;
5142 /* Within a parenthesized expression, a `>' token is always
5143 the greater-than operator. */
5144 saved_greater_than_is_operator_p
5145 = parser->greater_than_is_operator_p;
5146 parser->greater_than_is_operator_p = true;
5148 /* Consume expressions until there are no more. */
5149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5154 /* At the beginning of attribute lists, check to see if the
5155 next token is an identifier. */
5156 if (is_attribute_list
5157 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5161 /* Consume the identifier. */
5162 token = cp_lexer_consume_token (parser->lexer);
5163 /* Save the identifier. */
5164 identifier = token->u.value;
5168 bool expr_non_constant_p;
5170 /* Parse the next assignment-expression. */
5171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5173 /* A braced-init-list. */
5174 maybe_warn_cpp0x ("extended initializer lists");
5175 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5176 if (non_constant_p && expr_non_constant_p)
5177 *non_constant_p = true;
5179 else if (non_constant_p)
5181 expr = (cp_parser_constant_expression
5182 (parser, /*allow_non_constant_p=*/true,
5183 &expr_non_constant_p));
5184 if (expr_non_constant_p)
5185 *non_constant_p = true;
5188 expr = cp_parser_assignment_expression (parser, cast_p);
5191 expr = fold_non_dependent_expr (expr);
5193 /* If we have an ellipsis, then this is an expression
5195 if (allow_expansion_p
5196 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5198 /* Consume the `...'. */
5199 cp_lexer_consume_token (parser->lexer);
5201 /* Build the argument pack. */
5202 expr = make_pack_expansion (expr);
5205 /* Add it to the list. We add error_mark_node
5206 expressions to the list, so that we can still tell if
5207 the correct form for a parenthesized expression-list
5208 is found. That gives better errors. */
5209 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5211 if (expr == error_mark_node)
5215 /* After the first item, attribute lists look the same as
5216 expression lists. */
5217 is_attribute_list = false;
5220 /* If the next token isn't a `,', then we are done. */
5221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5224 /* Otherwise, consume the `,' and keep going. */
5225 cp_lexer_consume_token (parser->lexer);
5228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5233 /* We try and resync to an unnested comma, as that will give the
5234 user better diagnostics. */
5235 ending = cp_parser_skip_to_closing_parenthesis (parser,
5236 /*recovering=*/true,
5238 /*consume_paren=*/true);
5243 parser->greater_than_is_operator_p
5244 = saved_greater_than_is_operator_p;
5245 return error_mark_node;
5249 parser->greater_than_is_operator_p
5250 = saved_greater_than_is_operator_p;
5252 /* We built up the list in reverse order so we must reverse it now. */
5253 expression_list = nreverse (expression_list);
5255 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5257 return expression_list;
5260 /* Parse a pseudo-destructor-name.
5262 pseudo-destructor-name:
5263 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5264 :: [opt] nested-name-specifier template template-id :: ~ type-name
5265 :: [opt] nested-name-specifier [opt] ~ type-name
5267 If either of the first two productions is used, sets *SCOPE to the
5268 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5269 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5270 or ERROR_MARK_NODE if the parse fails. */
5273 cp_parser_pseudo_destructor_name (cp_parser* parser,
5277 bool nested_name_specifier_p;
5279 /* Assume that things will not work out. */
5280 *type = error_mark_node;
5282 /* Look for the optional `::' operator. */
5283 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5284 /* Look for the optional nested-name-specifier. */
5285 nested_name_specifier_p
5286 = (cp_parser_nested_name_specifier_opt (parser,
5287 /*typename_keyword_p=*/false,
5288 /*check_dependency_p=*/true,
5290 /*is_declaration=*/false)
5292 /* Now, if we saw a nested-name-specifier, we might be doing the
5293 second production. */
5294 if (nested_name_specifier_p
5295 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5297 /* Consume the `template' keyword. */
5298 cp_lexer_consume_token (parser->lexer);
5299 /* Parse the template-id. */
5300 cp_parser_template_id (parser,
5301 /*template_keyword_p=*/true,
5302 /*check_dependency_p=*/false,
5303 /*is_declaration=*/true);
5304 /* Look for the `::' token. */
5305 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5307 /* If the next token is not a `~', then there might be some
5308 additional qualification. */
5309 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5311 /* At this point, we're looking for "type-name :: ~". The type-name
5312 must not be a class-name, since this is a pseudo-destructor. So,
5313 it must be either an enum-name, or a typedef-name -- both of which
5314 are just identifiers. So, we peek ahead to check that the "::"
5315 and "~" tokens are present; if they are not, then we can avoid
5316 calling type_name. */
5317 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5318 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5319 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5321 cp_parser_error (parser, "non-scalar type");
5325 /* Look for the type-name. */
5326 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5327 if (*scope == error_mark_node)
5330 /* Look for the `::' token. */
5331 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5336 /* Look for the `~'. */
5337 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5338 /* Look for the type-name again. We are not responsible for
5339 checking that it matches the first type-name. */
5340 *type = cp_parser_nonclass_name (parser);
5343 /* Parse a unary-expression.
5349 unary-operator cast-expression
5350 sizeof unary-expression
5358 __extension__ cast-expression
5359 __alignof__ unary-expression
5360 __alignof__ ( type-id )
5361 __real__ cast-expression
5362 __imag__ cast-expression
5365 ADDRESS_P is true iff the unary-expression is appearing as the
5366 operand of the `&' operator. CAST_P is true if this expression is
5367 the target of a cast.
5369 Returns a representation of the expression. */
5372 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5375 enum tree_code unary_operator;
5377 /* Peek at the next token. */
5378 token = cp_lexer_peek_token (parser->lexer);
5379 /* Some keywords give away the kind of expression. */
5380 if (token->type == CPP_KEYWORD)
5382 enum rid keyword = token->keyword;
5392 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5393 /* Consume the token. */
5394 cp_lexer_consume_token (parser->lexer);
5395 /* Parse the operand. */
5396 operand = cp_parser_sizeof_operand (parser, keyword);
5398 if (TYPE_P (operand))
5399 return cxx_sizeof_or_alignof_type (operand, op, true);
5401 return cxx_sizeof_or_alignof_expr (operand, op, true);
5405 return cp_parser_new_expression (parser);
5408 return cp_parser_delete_expression (parser);
5412 /* The saved value of the PEDANTIC flag. */
5416 /* Save away the PEDANTIC flag. */
5417 cp_parser_extension_opt (parser, &saved_pedantic);
5418 /* Parse the cast-expression. */
5419 expr = cp_parser_simple_cast_expression (parser);
5420 /* Restore the PEDANTIC flag. */
5421 pedantic = saved_pedantic;
5431 /* Consume the `__real__' or `__imag__' token. */
5432 cp_lexer_consume_token (parser->lexer);
5433 /* Parse the cast-expression. */
5434 expression = cp_parser_simple_cast_expression (parser);
5435 /* Create the complete representation. */
5436 return build_x_unary_op ((keyword == RID_REALPART
5437 ? REALPART_EXPR : IMAGPART_EXPR),
5439 tf_warning_or_error);
5448 /* Look for the `:: new' and `:: delete', which also signal the
5449 beginning of a new-expression, or delete-expression,
5450 respectively. If the next token is `::', then it might be one of
5452 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5456 /* See if the token after the `::' is one of the keywords in
5457 which we're interested. */
5458 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5459 /* If it's `new', we have a new-expression. */
5460 if (keyword == RID_NEW)
5461 return cp_parser_new_expression (parser);
5462 /* Similarly, for `delete'. */
5463 else if (keyword == RID_DELETE)
5464 return cp_parser_delete_expression (parser);
5467 /* Look for a unary operator. */
5468 unary_operator = cp_parser_unary_operator (token);
5469 /* The `++' and `--' operators can be handled similarly, even though
5470 they are not technically unary-operators in the grammar. */
5471 if (unary_operator == ERROR_MARK)
5473 if (token->type == CPP_PLUS_PLUS)
5474 unary_operator = PREINCREMENT_EXPR;
5475 else if (token->type == CPP_MINUS_MINUS)
5476 unary_operator = PREDECREMENT_EXPR;
5477 /* Handle the GNU address-of-label extension. */
5478 else if (cp_parser_allow_gnu_extensions_p (parser)
5479 && token->type == CPP_AND_AND)
5483 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5485 /* Consume the '&&' token. */
5486 cp_lexer_consume_token (parser->lexer);
5487 /* Look for the identifier. */
5488 identifier = cp_parser_identifier (parser);
5489 /* Create an expression representing the address. */
5490 expression = finish_label_address_expr (identifier, loc);
5491 if (cp_parser_non_integral_constant_expression (parser,
5492 "the address of a label"))
5493 expression = error_mark_node;
5497 if (unary_operator != ERROR_MARK)
5499 tree cast_expression;
5500 tree expression = error_mark_node;
5501 const char *non_constant_p = NULL;
5503 /* Consume the operator token. */
5504 token = cp_lexer_consume_token (parser->lexer);
5505 /* Parse the cast-expression. */
5507 = cp_parser_cast_expression (parser,
5508 unary_operator == ADDR_EXPR,
5510 /* Now, build an appropriate representation. */
5511 switch (unary_operator)
5514 non_constant_p = "%<*%>";
5515 expression = build_x_indirect_ref (cast_expression, "unary *",
5516 tf_warning_or_error);
5520 non_constant_p = "%<&%>";
5523 expression = build_x_unary_op (unary_operator, cast_expression,
5524 tf_warning_or_error);
5527 case PREINCREMENT_EXPR:
5528 case PREDECREMENT_EXPR:
5529 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5530 ? "%<++%>" : "%<--%>");
5532 case UNARY_PLUS_EXPR:
5534 case TRUTH_NOT_EXPR:
5535 expression = finish_unary_op_expr (unary_operator, cast_expression);
5543 && cp_parser_non_integral_constant_expression (parser,
5545 expression = error_mark_node;
5550 return cp_parser_postfix_expression (parser, address_p, cast_p,
5551 /*member_access_only_p=*/false);
5554 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5555 unary-operator, the corresponding tree code is returned. */
5557 static enum tree_code
5558 cp_parser_unary_operator (cp_token* token)
5560 switch (token->type)
5563 return INDIRECT_REF;
5569 return UNARY_PLUS_EXPR;
5575 return TRUTH_NOT_EXPR;
5578 return BIT_NOT_EXPR;
5585 /* Parse a new-expression.
5588 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5589 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5591 Returns a representation of the expression. */
5594 cp_parser_new_expression (cp_parser* parser)
5596 bool global_scope_p;
5602 /* Look for the optional `::' operator. */
5604 = (cp_parser_global_scope_opt (parser,
5605 /*current_scope_valid_p=*/false)
5607 /* Look for the `new' operator. */
5608 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5609 /* There's no easy way to tell a new-placement from the
5610 `( type-id )' construct. */
5611 cp_parser_parse_tentatively (parser);
5612 /* Look for a new-placement. */
5613 placement = cp_parser_new_placement (parser);
5614 /* If that didn't work out, there's no new-placement. */
5615 if (!cp_parser_parse_definitely (parser))
5616 placement = NULL_TREE;
5618 /* If the next token is a `(', then we have a parenthesized
5620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5623 /* Consume the `('. */
5624 cp_lexer_consume_token (parser->lexer);
5625 /* Parse the type-id. */
5626 type = cp_parser_type_id (parser);
5627 /* Look for the closing `)'. */
5628 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5629 token = cp_lexer_peek_token (parser->lexer);
5630 /* There should not be a direct-new-declarator in this production,
5631 but GCC used to allowed this, so we check and emit a sensible error
5632 message for this case. */
5633 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5635 error ("%Harray bound forbidden after parenthesized type-id",
5637 inform (token->location,
5638 "try removing the parentheses around the type-id");
5639 cp_parser_direct_new_declarator (parser);
5643 /* Otherwise, there must be a new-type-id. */
5645 type = cp_parser_new_type_id (parser, &nelts);
5647 /* If the next token is a `(' or '{', then we have a new-initializer. */
5648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5649 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5650 initializer = cp_parser_new_initializer (parser);
5652 initializer = NULL_TREE;
5654 /* A new-expression may not appear in an integral constant
5656 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5657 return error_mark_node;
5659 /* Create a representation of the new-expression. */
5660 return build_new (placement, type, nelts, initializer, global_scope_p,
5661 tf_warning_or_error);
5664 /* Parse a new-placement.
5669 Returns the same representation as for an expression-list. */
5672 cp_parser_new_placement (cp_parser* parser)
5674 tree expression_list;
5676 /* Parse the expression-list. */
5677 expression_list = (cp_parser_parenthesized_expression_list
5678 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5679 /*non_constant_p=*/NULL));
5681 return expression_list;
5684 /* Parse a new-type-id.
5687 type-specifier-seq new-declarator [opt]
5689 Returns the TYPE allocated. If the new-type-id indicates an array
5690 type, *NELTS is set to the number of elements in the last array
5691 bound; the TYPE will not include the last array bound. */
5694 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5696 cp_decl_specifier_seq type_specifier_seq;
5697 cp_declarator *new_declarator;
5698 cp_declarator *declarator;
5699 cp_declarator *outer_declarator;
5700 const char *saved_message;
5703 /* The type-specifier sequence must not contain type definitions.
5704 (It cannot contain declarations of new types either, but if they
5705 are not definitions we will catch that because they are not
5707 saved_message = parser->type_definition_forbidden_message;
5708 parser->type_definition_forbidden_message
5709 = "types may not be defined in a new-type-id";
5710 /* Parse the type-specifier-seq. */
5711 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5712 &type_specifier_seq);
5713 /* Restore the old message. */
5714 parser->type_definition_forbidden_message = saved_message;
5715 /* Parse the new-declarator. */
5716 new_declarator = cp_parser_new_declarator_opt (parser);
5718 /* Determine the number of elements in the last array dimension, if
5721 /* Skip down to the last array dimension. */
5722 declarator = new_declarator;
5723 outer_declarator = NULL;
5724 while (declarator && (declarator->kind == cdk_pointer
5725 || declarator->kind == cdk_ptrmem))
5727 outer_declarator = declarator;
5728 declarator = declarator->declarator;
5731 && declarator->kind == cdk_array
5732 && declarator->declarator
5733 && declarator->declarator->kind == cdk_array)
5735 outer_declarator = declarator;
5736 declarator = declarator->declarator;
5739 if (declarator && declarator->kind == cdk_array)
5741 *nelts = declarator->u.array.bounds;
5742 if (*nelts == error_mark_node)
5743 *nelts = integer_one_node;
5745 if (outer_declarator)
5746 outer_declarator->declarator = declarator->declarator;
5748 new_declarator = NULL;
5751 type = groktypename (&type_specifier_seq, new_declarator);
5755 /* Parse an (optional) new-declarator.
5758 ptr-operator new-declarator [opt]
5759 direct-new-declarator
5761 Returns the declarator. */
5763 static cp_declarator *
5764 cp_parser_new_declarator_opt (cp_parser* parser)
5766 enum tree_code code;
5768 cp_cv_quals cv_quals;
5770 /* We don't know if there's a ptr-operator next, or not. */
5771 cp_parser_parse_tentatively (parser);
5772 /* Look for a ptr-operator. */
5773 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5774 /* If that worked, look for more new-declarators. */
5775 if (cp_parser_parse_definitely (parser))
5777 cp_declarator *declarator;
5779 /* Parse another optional declarator. */
5780 declarator = cp_parser_new_declarator_opt (parser);
5782 return cp_parser_make_indirect_declarator
5783 (code, type, cv_quals, declarator);
5786 /* If the next token is a `[', there is a direct-new-declarator. */
5787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5788 return cp_parser_direct_new_declarator (parser);
5793 /* Parse a direct-new-declarator.
5795 direct-new-declarator:
5797 direct-new-declarator [constant-expression]
5801 static cp_declarator *
5802 cp_parser_direct_new_declarator (cp_parser* parser)
5804 cp_declarator *declarator = NULL;
5810 /* Look for the opening `['. */
5811 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5812 /* The first expression is not required to be constant. */
5815 cp_token *token = cp_lexer_peek_token (parser->lexer);
5816 expression = cp_parser_expression (parser, /*cast_p=*/false);
5817 /* The standard requires that the expression have integral
5818 type. DR 74 adds enumeration types. We believe that the
5819 real intent is that these expressions be handled like the
5820 expression in a `switch' condition, which also allows
5821 classes with a single conversion to integral or
5822 enumeration type. */
5823 if (!processing_template_decl)
5826 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5831 error ("%Hexpression in new-declarator must have integral "
5832 "or enumeration type", &token->location);
5833 expression = error_mark_node;
5837 /* But all the other expressions must be. */
5840 = cp_parser_constant_expression (parser,
5841 /*allow_non_constant=*/false,
5843 /* Look for the closing `]'. */
5844 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5846 /* Add this bound to the declarator. */
5847 declarator = make_array_declarator (declarator, expression);
5849 /* If the next token is not a `[', then there are no more
5851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5858 /* Parse a new-initializer.
5861 ( expression-list [opt] )
5864 Returns a representation of the expression-list. If there is no
5865 expression-list, VOID_ZERO_NODE is returned. */
5868 cp_parser_new_initializer (cp_parser* parser)
5870 tree expression_list;
5872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5874 bool expr_non_constant_p;
5875 maybe_warn_cpp0x ("extended initializer lists");
5876 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5877 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5878 expression_list = build_tree_list (NULL_TREE, expression_list);
5881 expression_list = (cp_parser_parenthesized_expression_list
5882 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5883 /*non_constant_p=*/NULL));
5884 if (!expression_list)
5885 expression_list = void_zero_node;
5887 return expression_list;
5890 /* Parse a delete-expression.
5893 :: [opt] delete cast-expression
5894 :: [opt] delete [ ] cast-expression
5896 Returns a representation of the expression. */
5899 cp_parser_delete_expression (cp_parser* parser)
5901 bool global_scope_p;
5905 /* Look for the optional `::' operator. */
5907 = (cp_parser_global_scope_opt (parser,
5908 /*current_scope_valid_p=*/false)
5910 /* Look for the `delete' keyword. */
5911 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5912 /* See if the array syntax is in use. */
5913 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5915 /* Consume the `[' token. */
5916 cp_lexer_consume_token (parser->lexer);
5917 /* Look for the `]' token. */
5918 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5919 /* Remember that this is the `[]' construct. */
5925 /* Parse the cast-expression. */
5926 expression = cp_parser_simple_cast_expression (parser);
5928 /* A delete-expression may not appear in an integral constant
5930 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5931 return error_mark_node;
5933 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5936 /* Returns true if TOKEN may start a cast-expression and false
5940 cp_parser_token_starts_cast_expression (cp_token *token)
5942 switch (token->type)
5948 case CPP_CLOSE_SQUARE:
5949 case CPP_CLOSE_PAREN:
5950 case CPP_CLOSE_BRACE:
5954 case CPP_DEREF_STAR:
5962 case CPP_GREATER_EQ:
5982 /* '[' may start a primary-expression in obj-c++. */
5983 case CPP_OPEN_SQUARE:
5984 return c_dialect_objc ();
5991 /* Parse a cast-expression.
5995 ( type-id ) cast-expression
5997 ADDRESS_P is true iff the unary-expression is appearing as the
5998 operand of the `&' operator. CAST_P is true if this expression is
5999 the target of a cast.
6001 Returns a representation of the expression. */
6004 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
6006 /* If it's a `(', then we might be looking at a cast. */
6007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6009 tree type = NULL_TREE;
6010 tree expr = NULL_TREE;
6011 bool compound_literal_p;
6012 const char *saved_message;
6014 /* There's no way to know yet whether or not this is a cast.
6015 For example, `(int (3))' is a unary-expression, while `(int)
6016 3' is a cast. So, we resort to parsing tentatively. */
6017 cp_parser_parse_tentatively (parser);
6018 /* Types may not be defined in a cast. */
6019 saved_message = parser->type_definition_forbidden_message;
6020 parser->type_definition_forbidden_message
6021 = "types may not be defined in casts";
6022 /* Consume the `('. */
6023 cp_lexer_consume_token (parser->lexer);
6024 /* A very tricky bit is that `(struct S) { 3 }' is a
6025 compound-literal (which we permit in C++ as an extension).
6026 But, that construct is not a cast-expression -- it is a
6027 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6028 is legal; if the compound-literal were a cast-expression,
6029 you'd need an extra set of parentheses.) But, if we parse
6030 the type-id, and it happens to be a class-specifier, then we
6031 will commit to the parse at that point, because we cannot
6032 undo the action that is done when creating a new class. So,
6033 then we cannot back up and do a postfix-expression.
6035 Therefore, we scan ahead to the closing `)', and check to see
6036 if the token after the `)' is a `{'. If so, we are not
6037 looking at a cast-expression.
6039 Save tokens so that we can put them back. */
6040 cp_lexer_save_tokens (parser->lexer);
6041 /* Skip tokens until the next token is a closing parenthesis.
6042 If we find the closing `)', and the next token is a `{', then
6043 we are looking at a compound-literal. */
6045 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6046 /*consume_paren=*/true)
6047 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6048 /* Roll back the tokens we skipped. */
6049 cp_lexer_rollback_tokens (parser->lexer);
6050 /* If we were looking at a compound-literal, simulate an error
6051 so that the call to cp_parser_parse_definitely below will
6053 if (compound_literal_p)
6054 cp_parser_simulate_error (parser);
6057 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6058 parser->in_type_id_in_expr_p = true;
6059 /* Look for the type-id. */
6060 type = cp_parser_type_id (parser);
6061 /* Look for the closing `)'. */
6062 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6066 /* Restore the saved message. */
6067 parser->type_definition_forbidden_message = saved_message;
6069 /* At this point this can only be either a cast or a
6070 parenthesized ctor such as `(T ())' that looks like a cast to
6071 function returning T. */
6072 if (!cp_parser_error_occurred (parser)
6073 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6076 cp_parser_parse_definitely (parser);
6077 expr = cp_parser_cast_expression (parser,
6078 /*address_p=*/false,
6081 /* Warn about old-style casts, if so requested. */
6082 if (warn_old_style_cast
6083 && !in_system_header
6084 && !VOID_TYPE_P (type)
6085 && current_lang_name != lang_name_c)
6086 warning (OPT_Wold_style_cast, "use of old-style cast");
6088 /* Only type conversions to integral or enumeration types
6089 can be used in constant-expressions. */
6090 if (!cast_valid_in_integral_constant_expression_p (type)
6091 && (cp_parser_non_integral_constant_expression
6093 "a cast to a type other than an integral or "
6094 "enumeration type")))
6095 return error_mark_node;
6097 /* Perform the cast. */
6098 expr = build_c_cast (type, expr);
6102 cp_parser_abort_tentative_parse (parser);
6105 /* If we get here, then it's not a cast, so it must be a
6106 unary-expression. */
6107 return cp_parser_unary_expression (parser, address_p, cast_p);
6110 /* Parse a binary expression of the general form:
6114 pm-expression .* cast-expression
6115 pm-expression ->* cast-expression
6117 multiplicative-expression:
6119 multiplicative-expression * pm-expression
6120 multiplicative-expression / pm-expression
6121 multiplicative-expression % pm-expression
6123 additive-expression:
6124 multiplicative-expression
6125 additive-expression + multiplicative-expression
6126 additive-expression - multiplicative-expression
6130 shift-expression << additive-expression
6131 shift-expression >> additive-expression
6133 relational-expression:
6135 relational-expression < shift-expression
6136 relational-expression > shift-expression
6137 relational-expression <= shift-expression
6138 relational-expression >= shift-expression
6142 relational-expression:
6143 relational-expression <? shift-expression
6144 relational-expression >? shift-expression
6146 equality-expression:
6147 relational-expression
6148 equality-expression == relational-expression
6149 equality-expression != relational-expression
6153 and-expression & equality-expression
6155 exclusive-or-expression:
6157 exclusive-or-expression ^ and-expression
6159 inclusive-or-expression:
6160 exclusive-or-expression
6161 inclusive-or-expression | exclusive-or-expression
6163 logical-and-expression:
6164 inclusive-or-expression
6165 logical-and-expression && inclusive-or-expression
6167 logical-or-expression:
6168 logical-and-expression
6169 logical-or-expression || logical-and-expression
6171 All these are implemented with a single function like:
6174 simple-cast-expression
6175 binary-expression <token> binary-expression
6177 CAST_P is true if this expression is the target of a cast.
6179 The binops_by_token map is used to get the tree codes for each <token> type.
6180 binary-expressions are associated according to a precedence table. */
6182 #define TOKEN_PRECEDENCE(token) \
6183 (((token->type == CPP_GREATER \
6184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6185 && !parser->greater_than_is_operator_p) \
6186 ? PREC_NOT_OPERATOR \
6187 : binops_by_token[token->type].prec)
6190 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6191 enum cp_parser_prec prec)
6193 cp_parser_expression_stack stack;
6194 cp_parser_expression_stack_entry *sp = &stack[0];
6197 enum tree_code tree_type, lhs_type, rhs_type;
6198 enum cp_parser_prec new_prec, lookahead_prec;
6201 /* Parse the first expression. */
6202 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6203 lhs_type = ERROR_MARK;
6207 /* Get an operator token. */
6208 token = cp_lexer_peek_token (parser->lexer);
6210 if (warn_cxx0x_compat
6211 && token->type == CPP_RSHIFT
6212 && !parser->greater_than_is_operator_p)
6214 warning (OPT_Wc__0x_compat,
6215 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6217 warning (OPT_Wc__0x_compat,
6218 "suggest parentheses around %<>>%> expression");
6221 new_prec = TOKEN_PRECEDENCE (token);
6223 /* Popping an entry off the stack means we completed a subexpression:
6224 - either we found a token which is not an operator (`>' where it is not
6225 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6226 will happen repeatedly;
6227 - or, we found an operator which has lower priority. This is the case
6228 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6230 if (new_prec <= prec)
6239 tree_type = binops_by_token[token->type].tree_type;
6241 /* We used the operator token. */
6242 cp_lexer_consume_token (parser->lexer);
6244 /* Extract another operand. It may be the RHS of this expression
6245 or the LHS of a new, higher priority expression. */
6246 rhs = cp_parser_simple_cast_expression (parser);
6247 rhs_type = ERROR_MARK;
6249 /* Get another operator token. Look up its precedence to avoid
6250 building a useless (immediately popped) stack entry for common
6251 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6252 token = cp_lexer_peek_token (parser->lexer);
6253 lookahead_prec = TOKEN_PRECEDENCE (token);
6254 if (lookahead_prec > new_prec)
6256 /* ... and prepare to parse the RHS of the new, higher priority
6257 expression. Since precedence levels on the stack are
6258 monotonically increasing, we do not have to care about
6261 sp->tree_type = tree_type;
6263 sp->lhs_type = lhs_type;
6266 lhs_type = rhs_type;
6268 new_prec = lookahead_prec;
6272 /* If the stack is not empty, we have parsed into LHS the right side
6273 (`4' in the example above) of an expression we had suspended.
6274 We can use the information on the stack to recover the LHS (`3')
6275 from the stack together with the tree code (`MULT_EXPR'), and
6276 the precedence of the higher level subexpression
6277 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6278 which will be used to actually build the additive expression. */
6281 tree_type = sp->tree_type;
6283 rhs_type = lhs_type;
6285 lhs_type = sp->lhs_type;
6288 overloaded_p = false;
6289 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6290 ERROR_MARK for everything that is not a binary expression.
6291 This makes warn_about_parentheses miss some warnings that
6292 involve unary operators. For unary expressions we should
6293 pass the correct tree_code unless the unary expression was
6294 surrounded by parentheses.
6296 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6297 &overloaded_p, tf_warning_or_error);
6298 lhs_type = tree_type;
6300 /* If the binary operator required the use of an overloaded operator,
6301 then this expression cannot be an integral constant-expression.
6302 An overloaded operator can be used even if both operands are
6303 otherwise permissible in an integral constant-expression if at
6304 least one of the operands is of enumeration type. */
6307 && (cp_parser_non_integral_constant_expression
6308 (parser, "calls to overloaded operators")))
6309 return error_mark_node;
6316 /* Parse the `? expression : assignment-expression' part of a
6317 conditional-expression. The LOGICAL_OR_EXPR is the
6318 logical-or-expression that started the conditional-expression.
6319 Returns a representation of the entire conditional-expression.
6321 This routine is used by cp_parser_assignment_expression.
6323 ? expression : assignment-expression
6327 ? : assignment-expression */
6330 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6333 tree assignment_expr;
6335 /* Consume the `?' token. */
6336 cp_lexer_consume_token (parser->lexer);
6337 if (cp_parser_allow_gnu_extensions_p (parser)
6338 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6339 /* Implicit true clause. */
6342 /* Parse the expression. */
6343 expr = cp_parser_expression (parser, /*cast_p=*/false);
6345 /* The next token should be a `:'. */
6346 cp_parser_require (parser, CPP_COLON, "%<:%>");
6347 /* Parse the assignment-expression. */
6348 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6350 /* Build the conditional-expression. */
6351 return build_x_conditional_expr (logical_or_expr,
6354 tf_warning_or_error);
6357 /* Parse an assignment-expression.
6359 assignment-expression:
6360 conditional-expression
6361 logical-or-expression assignment-operator assignment_expression
6364 CAST_P is true if this expression is the target of a cast.
6366 Returns a representation for the expression. */
6369 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6373 /* If the next token is the `throw' keyword, then we're looking at
6374 a throw-expression. */
6375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6376 expr = cp_parser_throw_expression (parser);
6377 /* Otherwise, it must be that we are looking at a
6378 logical-or-expression. */
6381 /* Parse the binary expressions (logical-or-expression). */
6382 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6383 /* If the next token is a `?' then we're actually looking at a
6384 conditional-expression. */
6385 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6386 return cp_parser_question_colon_clause (parser, expr);
6389 enum tree_code assignment_operator;
6391 /* If it's an assignment-operator, we're using the second
6394 = cp_parser_assignment_operator_opt (parser);
6395 if (assignment_operator != ERROR_MARK)
6397 bool non_constant_p;
6399 /* Parse the right-hand side of the assignment. */
6400 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6402 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6403 maybe_warn_cpp0x ("extended initializer lists");
6405 /* An assignment may not appear in a
6406 constant-expression. */
6407 if (cp_parser_non_integral_constant_expression (parser,
6409 return error_mark_node;
6410 /* Build the assignment expression. */
6411 expr = build_x_modify_expr (expr,
6412 assignment_operator,
6414 tf_warning_or_error);
6422 /* Parse an (optional) assignment-operator.
6424 assignment-operator: one of
6425 = *= /= %= += -= >>= <<= &= ^= |=
6429 assignment-operator: one of
6432 If the next token is an assignment operator, the corresponding tree
6433 code is returned, and the token is consumed. For example, for
6434 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6435 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6436 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6437 operator, ERROR_MARK is returned. */
6439 static enum tree_code
6440 cp_parser_assignment_operator_opt (cp_parser* parser)
6445 /* Peek at the next token. */
6446 token = cp_lexer_peek_token (parser->lexer);
6448 switch (token->type)
6459 op = TRUNC_DIV_EXPR;
6463 op = TRUNC_MOD_EXPR;
6495 /* Nothing else is an assignment operator. */
6499 /* If it was an assignment operator, consume it. */
6500 if (op != ERROR_MARK)
6501 cp_lexer_consume_token (parser->lexer);
6506 /* Parse an expression.
6509 assignment-expression
6510 expression , assignment-expression
6512 CAST_P is true if this expression is the target of a cast.
6514 Returns a representation of the expression. */
6517 cp_parser_expression (cp_parser* parser, bool cast_p)
6519 tree expression = NULL_TREE;
6523 tree assignment_expression;
6525 /* Parse the next assignment-expression. */
6526 assignment_expression
6527 = cp_parser_assignment_expression (parser, cast_p);
6528 /* If this is the first assignment-expression, we can just
6531 expression = assignment_expression;
6533 expression = build_x_compound_expr (expression,
6534 assignment_expression,
6535 tf_warning_or_error);
6536 /* If the next token is not a comma, then we are done with the
6538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6540 /* Consume the `,'. */
6541 cp_lexer_consume_token (parser->lexer);
6542 /* A comma operator cannot appear in a constant-expression. */
6543 if (cp_parser_non_integral_constant_expression (parser,
6544 "a comma operator"))
6545 expression = error_mark_node;
6551 /* Parse a constant-expression.
6553 constant-expression:
6554 conditional-expression
6556 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6557 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6558 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6559 is false, NON_CONSTANT_P should be NULL. */
6562 cp_parser_constant_expression (cp_parser* parser,
6563 bool allow_non_constant_p,
6564 bool *non_constant_p)
6566 bool saved_integral_constant_expression_p;
6567 bool saved_allow_non_integral_constant_expression_p;
6568 bool saved_non_integral_constant_expression_p;
6571 /* It might seem that we could simply parse the
6572 conditional-expression, and then check to see if it were
6573 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6574 one that the compiler can figure out is constant, possibly after
6575 doing some simplifications or optimizations. The standard has a
6576 precise definition of constant-expression, and we must honor
6577 that, even though it is somewhat more restrictive.
6583 is not a legal declaration, because `(2, 3)' is not a
6584 constant-expression. The `,' operator is forbidden in a
6585 constant-expression. However, GCC's constant-folding machinery
6586 will fold this operation to an INTEGER_CST for `3'. */
6588 /* Save the old settings. */
6589 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6590 saved_allow_non_integral_constant_expression_p
6591 = parser->allow_non_integral_constant_expression_p;
6592 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6593 /* We are now parsing a constant-expression. */
6594 parser->integral_constant_expression_p = true;
6595 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6596 parser->non_integral_constant_expression_p = false;
6597 /* Although the grammar says "conditional-expression", we parse an
6598 "assignment-expression", which also permits "throw-expression"
6599 and the use of assignment operators. In the case that
6600 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6601 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6602 actually essential that we look for an assignment-expression.
6603 For example, cp_parser_initializer_clauses uses this function to
6604 determine whether a particular assignment-expression is in fact
6606 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6607 /* Restore the old settings. */
6608 parser->integral_constant_expression_p
6609 = saved_integral_constant_expression_p;
6610 parser->allow_non_integral_constant_expression_p
6611 = saved_allow_non_integral_constant_expression_p;
6612 if (allow_non_constant_p)
6613 *non_constant_p = parser->non_integral_constant_expression_p;
6614 else if (parser->non_integral_constant_expression_p)
6615 expression = error_mark_node;
6616 parser->non_integral_constant_expression_p
6617 = saved_non_integral_constant_expression_p;
6622 /* Parse __builtin_offsetof.
6624 offsetof-expression:
6625 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6627 offsetof-member-designator:
6629 | offsetof-member-designator "." id-expression
6630 | offsetof-member-designator "[" expression "]"
6631 | offsetof-member-designator "->" id-expression */
6634 cp_parser_builtin_offsetof (cp_parser *parser)
6636 int save_ice_p, save_non_ice_p;
6641 /* We're about to accept non-integral-constant things, but will
6642 definitely yield an integral constant expression. Save and
6643 restore these values around our local parsing. */
6644 save_ice_p = parser->integral_constant_expression_p;
6645 save_non_ice_p = parser->non_integral_constant_expression_p;
6647 /* Consume the "__builtin_offsetof" token. */
6648 cp_lexer_consume_token (parser->lexer);
6649 /* Consume the opening `('. */
6650 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6651 /* Parse the type-id. */
6652 type = cp_parser_type_id (parser);
6653 /* Look for the `,'. */
6654 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6655 token = cp_lexer_peek_token (parser->lexer);
6657 /* Build the (type *)null that begins the traditional offsetof macro. */
6658 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6659 tf_warning_or_error);
6661 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6662 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6663 true, &dummy, token->location);
6666 token = cp_lexer_peek_token (parser->lexer);
6667 switch (token->type)
6669 case CPP_OPEN_SQUARE:
6670 /* offsetof-member-designator "[" expression "]" */
6671 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6675 /* offsetof-member-designator "->" identifier */
6676 expr = grok_array_decl (expr, integer_zero_node);
6680 /* offsetof-member-designator "." identifier */
6681 cp_lexer_consume_token (parser->lexer);
6682 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6687 case CPP_CLOSE_PAREN:
6688 /* Consume the ")" token. */
6689 cp_lexer_consume_token (parser->lexer);
6693 /* Error. We know the following require will fail, but
6694 that gives the proper error message. */
6695 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6696 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6697 expr = error_mark_node;
6703 /* If we're processing a template, we can't finish the semantics yet.
6704 Otherwise we can fold the entire expression now. */
6705 if (processing_template_decl)
6706 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6708 expr = finish_offsetof (expr);
6711 parser->integral_constant_expression_p = save_ice_p;
6712 parser->non_integral_constant_expression_p = save_non_ice_p;
6717 /* Parse a trait expression. */
6720 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6723 tree type1, type2 = NULL_TREE;
6724 bool binary = false;
6725 cp_decl_specifier_seq decl_specs;
6729 case RID_HAS_NOTHROW_ASSIGN:
6730 kind = CPTK_HAS_NOTHROW_ASSIGN;
6732 case RID_HAS_NOTHROW_CONSTRUCTOR:
6733 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6735 case RID_HAS_NOTHROW_COPY:
6736 kind = CPTK_HAS_NOTHROW_COPY;
6738 case RID_HAS_TRIVIAL_ASSIGN:
6739 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6741 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6742 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6744 case RID_HAS_TRIVIAL_COPY:
6745 kind = CPTK_HAS_TRIVIAL_COPY;
6747 case RID_HAS_TRIVIAL_DESTRUCTOR:
6748 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6750 case RID_HAS_VIRTUAL_DESTRUCTOR:
6751 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6753 case RID_IS_ABSTRACT:
6754 kind = CPTK_IS_ABSTRACT;
6756 case RID_IS_BASE_OF:
6757 kind = CPTK_IS_BASE_OF;
6761 kind = CPTK_IS_CLASS;
6763 case RID_IS_CONVERTIBLE_TO:
6764 kind = CPTK_IS_CONVERTIBLE_TO;
6768 kind = CPTK_IS_EMPTY;
6771 kind = CPTK_IS_ENUM;
6776 case RID_IS_POLYMORPHIC:
6777 kind = CPTK_IS_POLYMORPHIC;
6780 kind = CPTK_IS_UNION;
6786 /* Consume the token. */
6787 cp_lexer_consume_token (parser->lexer);
6789 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6791 type1 = cp_parser_type_id (parser);
6793 if (type1 == error_mark_node)
6794 return error_mark_node;
6796 /* Build a trivial decl-specifier-seq. */
6797 clear_decl_specs (&decl_specs);
6798 decl_specs.type = type1;
6800 /* Call grokdeclarator to figure out what type this is. */
6801 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6802 /*initialized=*/0, /*attrlist=*/NULL);
6806 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6808 type2 = cp_parser_type_id (parser);
6810 if (type2 == error_mark_node)
6811 return error_mark_node;
6813 /* Build a trivial decl-specifier-seq. */
6814 clear_decl_specs (&decl_specs);
6815 decl_specs.type = type2;
6817 /* Call grokdeclarator to figure out what type this is. */
6818 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6819 /*initialized=*/0, /*attrlist=*/NULL);
6822 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6824 /* Complete the trait expression, which may mean either processing
6825 the trait expr now or saving it for template instantiation. */
6826 return finish_trait_expr (kind, type1, type2);
6829 /* Statements [gram.stmt.stmt] */
6831 /* Parse a statement.
6835 expression-statement
6840 declaration-statement
6843 IN_COMPOUND is true when the statement is nested inside a
6844 cp_parser_compound_statement; this matters for certain pragmas.
6846 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6847 is a (possibly labeled) if statement which is not enclosed in braces
6848 and has an else clause. This is used to implement -Wparentheses. */
6851 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6852 bool in_compound, bool *if_p)
6856 location_t statement_location;
6861 /* There is no statement yet. */
6862 statement = NULL_TREE;
6863 /* Peek at the next token. */
6864 token = cp_lexer_peek_token (parser->lexer);
6865 /* Remember the location of the first token in the statement. */
6866 statement_location = token->location;
6867 /* If this is a keyword, then that will often determine what kind of
6868 statement we have. */
6869 if (token->type == CPP_KEYWORD)
6871 enum rid keyword = token->keyword;
6877 /* Looks like a labeled-statement with a case label.
6878 Parse the label, and then use tail recursion to parse
6880 cp_parser_label_for_labeled_statement (parser);
6885 statement = cp_parser_selection_statement (parser, if_p);
6891 statement = cp_parser_iteration_statement (parser);
6898 statement = cp_parser_jump_statement (parser);
6901 /* Objective-C++ exception-handling constructs. */
6904 case RID_AT_FINALLY:
6905 case RID_AT_SYNCHRONIZED:
6907 statement = cp_parser_objc_statement (parser);
6911 statement = cp_parser_try_block (parser);
6915 /* This must be a namespace alias definition. */
6916 cp_parser_declaration_statement (parser);
6920 /* It might be a keyword like `int' that can start a
6921 declaration-statement. */
6925 else if (token->type == CPP_NAME)
6927 /* If the next token is a `:', then we are looking at a
6928 labeled-statement. */
6929 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6930 if (token->type == CPP_COLON)
6932 /* Looks like a labeled-statement with an ordinary label.
6933 Parse the label, and then use tail recursion to parse
6935 cp_parser_label_for_labeled_statement (parser);
6939 /* Anything that starts with a `{' must be a compound-statement. */
6940 else if (token->type == CPP_OPEN_BRACE)
6941 statement = cp_parser_compound_statement (parser, NULL, false);
6942 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6943 a statement all its own. */
6944 else if (token->type == CPP_PRAGMA)
6946 /* Only certain OpenMP pragmas are attached to statements, and thus
6947 are considered statements themselves. All others are not. In
6948 the context of a compound, accept the pragma as a "statement" and
6949 return so that we can check for a close brace. Otherwise we
6950 require a real statement and must go back and read one. */
6952 cp_parser_pragma (parser, pragma_compound);
6953 else if (!cp_parser_pragma (parser, pragma_stmt))
6957 else if (token->type == CPP_EOF)
6959 cp_parser_error (parser, "expected statement");
6963 /* Everything else must be a declaration-statement or an
6964 expression-statement. Try for the declaration-statement
6965 first, unless we are looking at a `;', in which case we know that
6966 we have an expression-statement. */
6969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6971 cp_parser_parse_tentatively (parser);
6972 /* Try to parse the declaration-statement. */
6973 cp_parser_declaration_statement (parser);
6974 /* If that worked, we're done. */
6975 if (cp_parser_parse_definitely (parser))
6978 /* Look for an expression-statement instead. */
6979 statement = cp_parser_expression_statement (parser, in_statement_expr);
6982 /* Set the line number for the statement. */
6983 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6984 SET_EXPR_LOCATION (statement, statement_location);
6987 /* Parse the label for a labeled-statement, i.e.
6990 case constant-expression :
6994 case constant-expression ... constant-expression : statement
6996 When a label is parsed without errors, the label is added to the
6997 parse tree by the finish_* functions, so this function doesn't
6998 have to return the label. */
7001 cp_parser_label_for_labeled_statement (cp_parser* parser)
7005 /* The next token should be an identifier. */
7006 token = cp_lexer_peek_token (parser->lexer);
7007 if (token->type != CPP_NAME
7008 && token->type != CPP_KEYWORD)
7010 cp_parser_error (parser, "expected labeled-statement");
7014 switch (token->keyword)
7021 /* Consume the `case' token. */
7022 cp_lexer_consume_token (parser->lexer);
7023 /* Parse the constant-expression. */
7024 expr = cp_parser_constant_expression (parser,
7025 /*allow_non_constant_p=*/false,
7028 ellipsis = cp_lexer_peek_token (parser->lexer);
7029 if (ellipsis->type == CPP_ELLIPSIS)
7031 /* Consume the `...' token. */
7032 cp_lexer_consume_token (parser->lexer);
7034 cp_parser_constant_expression (parser,
7035 /*allow_non_constant_p=*/false,
7037 /* We don't need to emit warnings here, as the common code
7038 will do this for us. */
7041 expr_hi = NULL_TREE;
7043 if (parser->in_switch_statement_p)
7044 finish_case_label (expr, expr_hi);
7046 error ("%Hcase label %qE not within a switch statement",
7047 &token->location, expr);
7052 /* Consume the `default' token. */
7053 cp_lexer_consume_token (parser->lexer);
7055 if (parser->in_switch_statement_p)
7056 finish_case_label (NULL_TREE, NULL_TREE);
7058 error ("%Hcase label not within a switch statement", &token->location);
7062 /* Anything else must be an ordinary label. */
7063 finish_label_stmt (cp_parser_identifier (parser));
7067 /* Require the `:' token. */
7068 cp_parser_require (parser, CPP_COLON, "%<:%>");
7071 /* Parse an expression-statement.
7073 expression-statement:
7076 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7077 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7078 indicates whether this expression-statement is part of an
7079 expression statement. */
7082 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7084 tree statement = NULL_TREE;
7086 /* If the next token is a ';', then there is no expression
7088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7089 statement = cp_parser_expression (parser, /*cast_p=*/false);
7091 /* Consume the final `;'. */
7092 cp_parser_consume_semicolon_at_end_of_statement (parser);
7094 if (in_statement_expr
7095 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7096 /* This is the final expression statement of a statement
7098 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7100 statement = finish_expr_stmt (statement);
7107 /* Parse a compound-statement.
7110 { statement-seq [opt] }
7115 { label-declaration-seq [opt] statement-seq [opt] }
7117 label-declaration-seq:
7119 label-declaration-seq label-declaration
7121 Returns a tree representing the statement. */
7124 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7129 /* Consume the `{'. */
7130 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7131 return error_mark_node;
7132 /* Begin the compound-statement. */
7133 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7134 /* If the next keyword is `__label__' we have a label declaration. */
7135 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7136 cp_parser_label_declaration (parser);
7137 /* Parse an (optional) statement-seq. */
7138 cp_parser_statement_seq_opt (parser, in_statement_expr);
7139 /* Finish the compound-statement. */
7140 finish_compound_stmt (compound_stmt);
7141 /* Consume the `}'. */
7142 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7144 return compound_stmt;
7147 /* Parse an (optional) statement-seq.
7151 statement-seq [opt] statement */
7154 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7156 /* Scan statements until there aren't any more. */
7159 cp_token *token = cp_lexer_peek_token (parser->lexer);
7161 /* If we're looking at a `}', then we've run out of statements. */
7162 if (token->type == CPP_CLOSE_BRACE
7163 || token->type == CPP_EOF
7164 || token->type == CPP_PRAGMA_EOL)
7167 /* If we are in a compound statement and find 'else' then
7168 something went wrong. */
7169 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7171 if (parser->in_statement & IN_IF_STMT)
7175 token = cp_lexer_consume_token (parser->lexer);
7176 error ("%H%<else%> without a previous %<if%>", &token->location);
7180 /* Parse the statement. */
7181 cp_parser_statement (parser, in_statement_expr, true, NULL);
7185 /* Parse a selection-statement.
7187 selection-statement:
7188 if ( condition ) statement
7189 if ( condition ) statement else statement
7190 switch ( condition ) statement
7192 Returns the new IF_STMT or SWITCH_STMT.
7194 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7195 is a (possibly labeled) if statement which is not enclosed in
7196 braces and has an else clause. This is used to implement
7200 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7208 /* Peek at the next token. */
7209 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7211 /* See what kind of keyword it is. */
7212 keyword = token->keyword;
7221 /* Look for the `('. */
7222 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7224 cp_parser_skip_to_end_of_statement (parser);
7225 return error_mark_node;
7228 /* Begin the selection-statement. */
7229 if (keyword == RID_IF)
7230 statement = begin_if_stmt ();
7232 statement = begin_switch_stmt ();
7234 /* Parse the condition. */
7235 condition = cp_parser_condition (parser);
7236 /* Look for the `)'. */
7237 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7238 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7239 /*consume_paren=*/true);
7241 if (keyword == RID_IF)
7244 unsigned char in_statement;
7246 /* Add the condition. */
7247 finish_if_stmt_cond (condition, statement);
7249 /* Parse the then-clause. */
7250 in_statement = parser->in_statement;
7251 parser->in_statement |= IN_IF_STMT;
7252 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7255 add_stmt (build_empty_stmt ());
7256 cp_lexer_consume_token (parser->lexer);
7257 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7258 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7259 "empty body in an %<if%> statement");
7263 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7264 parser->in_statement = in_statement;
7266 finish_then_clause (statement);
7268 /* If the next token is `else', parse the else-clause. */
7269 if (cp_lexer_next_token_is_keyword (parser->lexer,
7272 /* Consume the `else' keyword. */
7273 cp_lexer_consume_token (parser->lexer);
7274 begin_else_clause (statement);
7275 /* Parse the else-clause. */
7276 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7278 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7279 OPT_Wempty_body, "suggest braces around "
7280 "empty body in an %<else%> statement");
7281 add_stmt (build_empty_stmt ());
7282 cp_lexer_consume_token (parser->lexer);
7285 cp_parser_implicitly_scoped_statement (parser, NULL);
7287 finish_else_clause (statement);
7289 /* If we are currently parsing a then-clause, then
7290 IF_P will not be NULL. We set it to true to
7291 indicate that this if statement has an else clause.
7292 This may trigger the Wparentheses warning below
7293 when we get back up to the parent if statement. */
7299 /* This if statement does not have an else clause. If
7300 NESTED_IF is true, then the then-clause is an if
7301 statement which does have an else clause. We warn
7302 about the potential ambiguity. */
7304 warning (OPT_Wparentheses,
7305 ("%Hsuggest explicit braces "
7306 "to avoid ambiguous %<else%>"),
7307 EXPR_LOCUS (statement));
7310 /* Now we're all done with the if-statement. */
7311 finish_if_stmt (statement);
7315 bool in_switch_statement_p;
7316 unsigned char in_statement;
7318 /* Add the condition. */
7319 finish_switch_cond (condition, statement);
7321 /* Parse the body of the switch-statement. */
7322 in_switch_statement_p = parser->in_switch_statement_p;
7323 in_statement = parser->in_statement;
7324 parser->in_switch_statement_p = true;
7325 parser->in_statement |= IN_SWITCH_STMT;
7326 cp_parser_implicitly_scoped_statement (parser, NULL);
7327 parser->in_switch_statement_p = in_switch_statement_p;
7328 parser->in_statement = in_statement;
7330 /* Now we're all done with the switch-statement. */
7331 finish_switch_stmt (statement);
7339 cp_parser_error (parser, "expected selection-statement");
7340 return error_mark_node;
7344 /* Parse a condition.
7348 type-specifier-seq declarator = initializer-clause
7349 type-specifier-seq declarator braced-init-list
7354 type-specifier-seq declarator asm-specification [opt]
7355 attributes [opt] = assignment-expression
7357 Returns the expression that should be tested. */
7360 cp_parser_condition (cp_parser* parser)
7362 cp_decl_specifier_seq type_specifiers;
7363 const char *saved_message;
7365 /* Try the declaration first. */
7366 cp_parser_parse_tentatively (parser);
7367 /* New types are not allowed in the type-specifier-seq for a
7369 saved_message = parser->type_definition_forbidden_message;
7370 parser->type_definition_forbidden_message
7371 = "types may not be defined in conditions";
7372 /* Parse the type-specifier-seq. */
7373 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7375 /* Restore the saved message. */
7376 parser->type_definition_forbidden_message = saved_message;
7377 /* If all is well, we might be looking at a declaration. */
7378 if (!cp_parser_error_occurred (parser))
7381 tree asm_specification;
7383 cp_declarator *declarator;
7384 tree initializer = NULL_TREE;
7386 /* Parse the declarator. */
7387 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7388 /*ctor_dtor_or_conv_p=*/NULL,
7389 /*parenthesized_p=*/NULL,
7390 /*member_p=*/false);
7391 /* Parse the attributes. */
7392 attributes = cp_parser_attributes_opt (parser);
7393 /* Parse the asm-specification. */
7394 asm_specification = cp_parser_asm_specification_opt (parser);
7395 /* If the next token is not an `=' or '{', then we might still be
7396 looking at an expression. For example:
7400 looks like a decl-specifier-seq and a declarator -- but then
7401 there is no `=', so this is an expression. */
7402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7403 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7404 cp_parser_simulate_error (parser);
7406 /* If we did see an `=' or '{', then we are looking at a declaration
7408 if (cp_parser_parse_definitely (parser))
7411 bool non_constant_p;
7412 bool flags = LOOKUP_ONLYCONVERTING;
7414 /* Create the declaration. */
7415 decl = start_decl (declarator, &type_specifiers,
7416 /*initialized_p=*/true,
7417 attributes, /*prefix_attributes=*/NULL_TREE,
7420 /* Parse the initializer. */
7421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7423 initializer = cp_parser_braced_list (parser, &non_constant_p);
7424 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7429 /* Consume the `='. */
7430 cp_parser_require (parser, CPP_EQ, "%<=%>");
7431 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7433 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7434 maybe_warn_cpp0x ("extended initializer lists");
7436 if (!non_constant_p)
7437 initializer = fold_non_dependent_expr (initializer);
7439 /* Process the initializer. */
7440 cp_finish_decl (decl,
7441 initializer, !non_constant_p,
7446 pop_scope (pushed_scope);
7448 return convert_from_reference (decl);
7451 /* If we didn't even get past the declarator successfully, we are
7452 definitely not looking at a declaration. */
7454 cp_parser_abort_tentative_parse (parser);
7456 /* Otherwise, we are looking at an expression. */
7457 return cp_parser_expression (parser, /*cast_p=*/false);
7460 /* Parse an iteration-statement.
7462 iteration-statement:
7463 while ( condition ) statement
7464 do statement while ( expression ) ;
7465 for ( for-init-statement condition [opt] ; expression [opt] )
7468 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7471 cp_parser_iteration_statement (cp_parser* parser)
7476 unsigned char in_statement;
7478 /* Peek at the next token. */
7479 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7481 return error_mark_node;
7483 /* Remember whether or not we are already within an iteration
7485 in_statement = parser->in_statement;
7487 /* See what kind of keyword it is. */
7488 keyword = token->keyword;
7495 /* Begin the while-statement. */
7496 statement = begin_while_stmt ();
7497 /* Look for the `('. */
7498 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7499 /* Parse the condition. */
7500 condition = cp_parser_condition (parser);
7501 finish_while_stmt_cond (condition, statement);
7502 /* Look for the `)'. */
7503 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7504 /* Parse the dependent statement. */
7505 parser->in_statement = IN_ITERATION_STMT;
7506 cp_parser_already_scoped_statement (parser);
7507 parser->in_statement = in_statement;
7508 /* We're done with the while-statement. */
7509 finish_while_stmt (statement);
7517 /* Begin the do-statement. */
7518 statement = begin_do_stmt ();
7519 /* Parse the body of the do-statement. */
7520 parser->in_statement = IN_ITERATION_STMT;
7521 cp_parser_implicitly_scoped_statement (parser, NULL);
7522 parser->in_statement = in_statement;
7523 finish_do_body (statement);
7524 /* Look for the `while' keyword. */
7525 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7526 /* Look for the `('. */
7527 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7528 /* Parse the expression. */
7529 expression = cp_parser_expression (parser, /*cast_p=*/false);
7530 /* We're done with the do-statement. */
7531 finish_do_stmt (expression, statement);
7532 /* Look for the `)'. */
7533 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7534 /* Look for the `;'. */
7535 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7541 tree condition = NULL_TREE;
7542 tree expression = NULL_TREE;
7544 /* Begin the for-statement. */
7545 statement = begin_for_stmt ();
7546 /* Look for the `('. */
7547 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7548 /* Parse the initialization. */
7549 cp_parser_for_init_statement (parser);
7550 finish_for_init_stmt (statement);
7552 /* If there's a condition, process it. */
7553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7554 condition = cp_parser_condition (parser);
7555 finish_for_cond (condition, statement);
7556 /* Look for the `;'. */
7557 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7559 /* If there's an expression, process it. */
7560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7561 expression = cp_parser_expression (parser, /*cast_p=*/false);
7562 finish_for_expr (expression, statement);
7563 /* Look for the `)'. */
7564 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7566 /* Parse the body of the for-statement. */
7567 parser->in_statement = IN_ITERATION_STMT;
7568 cp_parser_already_scoped_statement (parser);
7569 parser->in_statement = in_statement;
7571 /* We're done with the for-statement. */
7572 finish_for_stmt (statement);
7577 cp_parser_error (parser, "expected iteration-statement");
7578 statement = error_mark_node;
7585 /* Parse a for-init-statement.
7588 expression-statement
7589 simple-declaration */
7592 cp_parser_for_init_statement (cp_parser* parser)
7594 /* If the next token is a `;', then we have an empty
7595 expression-statement. Grammatically, this is also a
7596 simple-declaration, but an invalid one, because it does not
7597 declare anything. Therefore, if we did not handle this case
7598 specially, we would issue an error message about an invalid
7600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7602 /* We're going to speculatively look for a declaration, falling back
7603 to an expression, if necessary. */
7604 cp_parser_parse_tentatively (parser);
7605 /* Parse the declaration. */
7606 cp_parser_simple_declaration (parser,
7607 /*function_definition_allowed_p=*/false);
7608 /* If the tentative parse failed, then we shall need to look for an
7609 expression-statement. */
7610 if (cp_parser_parse_definitely (parser))
7614 cp_parser_expression_statement (parser, false);
7617 /* Parse a jump-statement.
7622 return expression [opt] ;
7623 return braced-init-list ;
7631 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7634 cp_parser_jump_statement (cp_parser* parser)
7636 tree statement = error_mark_node;
7639 unsigned char in_statement;
7641 /* Peek at the next token. */
7642 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7644 return error_mark_node;
7646 /* See what kind of keyword it is. */
7647 keyword = token->keyword;
7651 in_statement = parser->in_statement & ~IN_IF_STMT;
7652 switch (in_statement)
7655 error ("%Hbreak statement not within loop or switch", &token->location);
7658 gcc_assert ((in_statement & IN_SWITCH_STMT)
7659 || in_statement == IN_ITERATION_STMT);
7660 statement = finish_break_stmt ();
7663 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7666 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7669 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7673 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7676 error ("%Hcontinue statement not within a loop", &token->location);
7678 case IN_ITERATION_STMT:
7680 statement = finish_continue_stmt ();
7683 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7688 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7694 bool expr_non_constant_p;
7696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7698 maybe_warn_cpp0x ("extended initializer lists");
7699 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7701 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7702 expr = cp_parser_expression (parser, /*cast_p=*/false);
7704 /* If the next token is a `;', then there is no
7707 /* Build the return-statement. */
7708 statement = finish_return_stmt (expr);
7709 /* Look for the final `;'. */
7710 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7715 /* Create the goto-statement. */
7716 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7718 /* Issue a warning about this use of a GNU extension. */
7719 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7720 /* Consume the '*' token. */
7721 cp_lexer_consume_token (parser->lexer);
7722 /* Parse the dependent expression. */
7723 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7726 finish_goto_stmt (cp_parser_identifier (parser));
7727 /* Look for the final `;'. */
7728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7732 cp_parser_error (parser, "expected jump-statement");
7739 /* Parse a declaration-statement.
7741 declaration-statement:
7742 block-declaration */
7745 cp_parser_declaration_statement (cp_parser* parser)
7749 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7750 p = obstack_alloc (&declarator_obstack, 0);
7752 /* Parse the block-declaration. */
7753 cp_parser_block_declaration (parser, /*statement_p=*/true);
7755 /* Free any declarators allocated. */
7756 obstack_free (&declarator_obstack, p);
7758 /* Finish off the statement. */
7762 /* Some dependent statements (like `if (cond) statement'), are
7763 implicitly in their own scope. In other words, if the statement is
7764 a single statement (as opposed to a compound-statement), it is
7765 none-the-less treated as if it were enclosed in braces. Any
7766 declarations appearing in the dependent statement are out of scope
7767 after control passes that point. This function parses a statement,
7768 but ensures that is in its own scope, even if it is not a
7771 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7772 is a (possibly labeled) if statement which is not enclosed in
7773 braces and has an else clause. This is used to implement
7776 Returns the new statement. */
7779 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7786 /* Mark if () ; with a special NOP_EXPR. */
7787 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7789 cp_lexer_consume_token (parser->lexer);
7790 statement = add_stmt (build_empty_stmt ());
7792 /* if a compound is opened, we simply parse the statement directly. */
7793 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7794 statement = cp_parser_compound_statement (parser, NULL, false);
7795 /* If the token is not a `{', then we must take special action. */
7798 /* Create a compound-statement. */
7799 statement = begin_compound_stmt (0);
7800 /* Parse the dependent-statement. */
7801 cp_parser_statement (parser, NULL_TREE, false, if_p);
7802 /* Finish the dummy compound-statement. */
7803 finish_compound_stmt (statement);
7806 /* Return the statement. */
7810 /* For some dependent statements (like `while (cond) statement'), we
7811 have already created a scope. Therefore, even if the dependent
7812 statement is a compound-statement, we do not want to create another
7816 cp_parser_already_scoped_statement (cp_parser* parser)
7818 /* If the token is a `{', then we must take special action. */
7819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7820 cp_parser_statement (parser, NULL_TREE, false, NULL);
7823 /* Avoid calling cp_parser_compound_statement, so that we
7824 don't create a new scope. Do everything else by hand. */
7825 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7826 cp_parser_statement_seq_opt (parser, NULL_TREE);
7827 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7831 /* Declarations [gram.dcl.dcl] */
7833 /* Parse an optional declaration-sequence.
7837 declaration-seq declaration */
7840 cp_parser_declaration_seq_opt (cp_parser* parser)
7846 token = cp_lexer_peek_token (parser->lexer);
7848 if (token->type == CPP_CLOSE_BRACE
7849 || token->type == CPP_EOF
7850 || token->type == CPP_PRAGMA_EOL)
7853 if (token->type == CPP_SEMICOLON)
7855 /* A declaration consisting of a single semicolon is
7856 invalid. Allow it unless we're being pedantic. */
7857 cp_lexer_consume_token (parser->lexer);
7858 if (!in_system_header)
7859 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7863 /* If we're entering or exiting a region that's implicitly
7864 extern "C", modify the lang context appropriately. */
7865 if (!parser->implicit_extern_c && token->implicit_extern_c)
7867 push_lang_context (lang_name_c);
7868 parser->implicit_extern_c = true;
7870 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7872 pop_lang_context ();
7873 parser->implicit_extern_c = false;
7876 if (token->type == CPP_PRAGMA)
7878 /* A top-level declaration can consist solely of a #pragma.
7879 A nested declaration cannot, so this is done here and not
7880 in cp_parser_declaration. (A #pragma at block scope is
7881 handled in cp_parser_statement.) */
7882 cp_parser_pragma (parser, pragma_external);
7886 /* Parse the declaration itself. */
7887 cp_parser_declaration (parser);
7891 /* Parse a declaration.
7896 template-declaration
7897 explicit-instantiation
7898 explicit-specialization
7899 linkage-specification
7900 namespace-definition
7905 __extension__ declaration */
7908 cp_parser_declaration (cp_parser* parser)
7915 /* Check for the `__extension__' keyword. */
7916 if (cp_parser_extension_opt (parser, &saved_pedantic))
7918 /* Parse the qualified declaration. */
7919 cp_parser_declaration (parser);
7920 /* Restore the PEDANTIC flag. */
7921 pedantic = saved_pedantic;
7926 /* Try to figure out what kind of declaration is present. */
7927 token1 = *cp_lexer_peek_token (parser->lexer);
7929 if (token1.type != CPP_EOF)
7930 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7933 token2.type = CPP_EOF;
7934 token2.keyword = RID_MAX;
7937 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7938 p = obstack_alloc (&declarator_obstack, 0);
7940 /* If the next token is `extern' and the following token is a string
7941 literal, then we have a linkage specification. */
7942 if (token1.keyword == RID_EXTERN
7943 && cp_parser_is_string_literal (&token2))
7944 cp_parser_linkage_specification (parser);
7945 /* If the next token is `template', then we have either a template
7946 declaration, an explicit instantiation, or an explicit
7948 else if (token1.keyword == RID_TEMPLATE)
7950 /* `template <>' indicates a template specialization. */
7951 if (token2.type == CPP_LESS
7952 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7953 cp_parser_explicit_specialization (parser);
7954 /* `template <' indicates a template declaration. */
7955 else if (token2.type == CPP_LESS)
7956 cp_parser_template_declaration (parser, /*member_p=*/false);
7957 /* Anything else must be an explicit instantiation. */
7959 cp_parser_explicit_instantiation (parser);
7961 /* If the next token is `export', then we have a template
7963 else if (token1.keyword == RID_EXPORT)
7964 cp_parser_template_declaration (parser, /*member_p=*/false);
7965 /* If the next token is `extern', 'static' or 'inline' and the one
7966 after that is `template', we have a GNU extended explicit
7967 instantiation directive. */
7968 else if (cp_parser_allow_gnu_extensions_p (parser)
7969 && (token1.keyword == RID_EXTERN
7970 || token1.keyword == RID_STATIC
7971 || token1.keyword == RID_INLINE)
7972 && token2.keyword == RID_TEMPLATE)
7973 cp_parser_explicit_instantiation (parser);
7974 /* If the next token is `namespace', check for a named or unnamed
7975 namespace definition. */
7976 else if (token1.keyword == RID_NAMESPACE
7977 && (/* A named namespace definition. */
7978 (token2.type == CPP_NAME
7979 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7981 /* An unnamed namespace definition. */
7982 || token2.type == CPP_OPEN_BRACE
7983 || token2.keyword == RID_ATTRIBUTE))
7984 cp_parser_namespace_definition (parser);
7985 /* An inline (associated) namespace definition. */
7986 else if (token1.keyword == RID_INLINE
7987 && token2.keyword == RID_NAMESPACE)
7988 cp_parser_namespace_definition (parser);
7989 /* Objective-C++ declaration/definition. */
7990 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7991 cp_parser_objc_declaration (parser);
7992 /* We must have either a block declaration or a function
7995 /* Try to parse a block-declaration, or a function-definition. */
7996 cp_parser_block_declaration (parser, /*statement_p=*/false);
7998 /* Free any declarators allocated. */
7999 obstack_free (&declarator_obstack, p);
8002 /* Parse a block-declaration.
8007 namespace-alias-definition
8014 __extension__ block-declaration
8019 static_assert-declaration
8021 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8022 part of a declaration-statement. */
8025 cp_parser_block_declaration (cp_parser *parser,
8031 /* Check for the `__extension__' keyword. */
8032 if (cp_parser_extension_opt (parser, &saved_pedantic))
8034 /* Parse the qualified declaration. */
8035 cp_parser_block_declaration (parser, statement_p);
8036 /* Restore the PEDANTIC flag. */
8037 pedantic = saved_pedantic;
8042 /* Peek at the next token to figure out which kind of declaration is
8044 token1 = cp_lexer_peek_token (parser->lexer);
8046 /* If the next keyword is `asm', we have an asm-definition. */
8047 if (token1->keyword == RID_ASM)
8050 cp_parser_commit_to_tentative_parse (parser);
8051 cp_parser_asm_definition (parser);
8053 /* If the next keyword is `namespace', we have a
8054 namespace-alias-definition. */
8055 else if (token1->keyword == RID_NAMESPACE)
8056 cp_parser_namespace_alias_definition (parser);
8057 /* If the next keyword is `using', we have either a
8058 using-declaration or a using-directive. */
8059 else if (token1->keyword == RID_USING)
8064 cp_parser_commit_to_tentative_parse (parser);
8065 /* If the token after `using' is `namespace', then we have a
8067 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8068 if (token2->keyword == RID_NAMESPACE)
8069 cp_parser_using_directive (parser);
8070 /* Otherwise, it's a using-declaration. */
8072 cp_parser_using_declaration (parser,
8073 /*access_declaration_p=*/false);
8075 /* If the next keyword is `__label__' we have a misplaced label
8077 else if (token1->keyword == RID_LABEL)
8079 cp_lexer_consume_token (parser->lexer);
8080 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8081 cp_parser_skip_to_end_of_statement (parser);
8082 /* If the next token is now a `;', consume it. */
8083 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8084 cp_lexer_consume_token (parser->lexer);
8086 /* If the next token is `static_assert' we have a static assertion. */
8087 else if (token1->keyword == RID_STATIC_ASSERT)
8088 cp_parser_static_assert (parser, /*member_p=*/false);
8089 /* Anything else must be a simple-declaration. */
8091 cp_parser_simple_declaration (parser, !statement_p);
8094 /* Parse a simple-declaration.
8097 decl-specifier-seq [opt] init-declarator-list [opt] ;
8099 init-declarator-list:
8101 init-declarator-list , init-declarator
8103 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8104 function-definition as a simple-declaration. */
8107 cp_parser_simple_declaration (cp_parser* parser,
8108 bool function_definition_allowed_p)
8110 cp_decl_specifier_seq decl_specifiers;
8111 int declares_class_or_enum;
8112 bool saw_declarator;
8114 /* Defer access checks until we know what is being declared; the
8115 checks for names appearing in the decl-specifier-seq should be
8116 done as if we were in the scope of the thing being declared. */
8117 push_deferring_access_checks (dk_deferred);
8119 /* Parse the decl-specifier-seq. We have to keep track of whether
8120 or not the decl-specifier-seq declares a named class or
8121 enumeration type, since that is the only case in which the
8122 init-declarator-list is allowed to be empty.
8126 In a simple-declaration, the optional init-declarator-list can be
8127 omitted only when declaring a class or enumeration, that is when
8128 the decl-specifier-seq contains either a class-specifier, an
8129 elaborated-type-specifier, or an enum-specifier. */
8130 cp_parser_decl_specifier_seq (parser,
8131 CP_PARSER_FLAGS_OPTIONAL,
8133 &declares_class_or_enum);
8134 /* We no longer need to defer access checks. */
8135 stop_deferring_access_checks ();
8137 /* In a block scope, a valid declaration must always have a
8138 decl-specifier-seq. By not trying to parse declarators, we can
8139 resolve the declaration/expression ambiguity more quickly. */
8140 if (!function_definition_allowed_p
8141 && !decl_specifiers.any_specifiers_p)
8143 cp_parser_error (parser, "expected declaration");
8147 /* If the next two tokens are both identifiers, the code is
8148 erroneous. The usual cause of this situation is code like:
8152 where "T" should name a type -- but does not. */
8153 if (!decl_specifiers.type
8154 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8156 /* If parsing tentatively, we should commit; we really are
8157 looking at a declaration. */
8158 cp_parser_commit_to_tentative_parse (parser);
8163 /* If we have seen at least one decl-specifier, and the next token
8164 is not a parenthesis, then we must be looking at a declaration.
8165 (After "int (" we might be looking at a functional cast.) */
8166 if (decl_specifiers.any_specifiers_p
8167 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8168 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8169 && !cp_parser_error_occurred (parser))
8170 cp_parser_commit_to_tentative_parse (parser);
8172 /* Keep going until we hit the `;' at the end of the simple
8174 saw_declarator = false;
8175 while (cp_lexer_next_token_is_not (parser->lexer,
8179 bool function_definition_p;
8184 /* If we are processing next declarator, coma is expected */
8185 token = cp_lexer_peek_token (parser->lexer);
8186 gcc_assert (token->type == CPP_COMMA);
8187 cp_lexer_consume_token (parser->lexer);
8190 saw_declarator = true;
8192 /* Parse the init-declarator. */
8193 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8195 function_definition_allowed_p,
8197 declares_class_or_enum,
8198 &function_definition_p);
8199 /* If an error occurred while parsing tentatively, exit quickly.
8200 (That usually happens when in the body of a function; each
8201 statement is treated as a declaration-statement until proven
8203 if (cp_parser_error_occurred (parser))
8205 /* Handle function definitions specially. */
8206 if (function_definition_p)
8208 /* If the next token is a `,', then we are probably
8209 processing something like:
8213 which is erroneous. */
8214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8216 cp_token *token = cp_lexer_peek_token (parser->lexer);
8217 error ("%Hmixing declarations and function-definitions is forbidden",
8220 /* Otherwise, we're done with the list of declarators. */
8223 pop_deferring_access_checks ();
8227 /* The next token should be either a `,' or a `;'. */
8228 token = cp_lexer_peek_token (parser->lexer);
8229 /* If it's a `,', there are more declarators to come. */
8230 if (token->type == CPP_COMMA)
8231 /* will be consumed next time around */;
8232 /* If it's a `;', we are done. */
8233 else if (token->type == CPP_SEMICOLON)
8235 /* Anything else is an error. */
8238 /* If we have already issued an error message we don't need
8239 to issue another one. */
8240 if (decl != error_mark_node
8241 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8242 cp_parser_error (parser, "expected %<,%> or %<;%>");
8243 /* Skip tokens until we reach the end of the statement. */
8244 cp_parser_skip_to_end_of_statement (parser);
8245 /* If the next token is now a `;', consume it. */
8246 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8247 cp_lexer_consume_token (parser->lexer);
8250 /* After the first time around, a function-definition is not
8251 allowed -- even if it was OK at first. For example:
8256 function_definition_allowed_p = false;
8259 /* Issue an error message if no declarators are present, and the
8260 decl-specifier-seq does not itself declare a class or
8262 if (!saw_declarator)
8264 if (cp_parser_declares_only_class_p (parser))
8265 shadow_tag (&decl_specifiers);
8266 /* Perform any deferred access checks. */
8267 perform_deferred_access_checks ();
8270 /* Consume the `;'. */
8271 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8274 pop_deferring_access_checks ();
8277 /* Parse a decl-specifier-seq.
8280 decl-specifier-seq [opt] decl-specifier
8283 storage-class-specifier
8294 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8296 The parser flags FLAGS is used to control type-specifier parsing.
8298 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8301 1: one of the decl-specifiers is an elaborated-type-specifier
8302 (i.e., a type declaration)
8303 2: one of the decl-specifiers is an enum-specifier or a
8304 class-specifier (i.e., a type definition)
8309 cp_parser_decl_specifier_seq (cp_parser* parser,
8310 cp_parser_flags flags,
8311 cp_decl_specifier_seq *decl_specs,
8312 int* declares_class_or_enum)
8314 bool constructor_possible_p = !parser->in_declarator_p;
8315 cp_token *start_token = NULL;
8317 /* Clear DECL_SPECS. */
8318 clear_decl_specs (decl_specs);
8320 /* Assume no class or enumeration type is declared. */
8321 *declares_class_or_enum = 0;
8323 /* Keep reading specifiers until there are no more to read. */
8327 bool found_decl_spec;
8330 /* Peek at the next token. */
8331 token = cp_lexer_peek_token (parser->lexer);
8333 /* Save the first token of the decl spec list for error
8336 start_token = token;
8337 /* Handle attributes. */
8338 if (token->keyword == RID_ATTRIBUTE)
8340 /* Parse the attributes. */
8341 decl_specs->attributes
8342 = chainon (decl_specs->attributes,
8343 cp_parser_attributes_opt (parser));
8346 /* Assume we will find a decl-specifier keyword. */
8347 found_decl_spec = true;
8348 /* If the next token is an appropriate keyword, we can simply
8349 add it to the list. */
8350 switch (token->keyword)
8355 if (!at_class_scope_p ())
8357 error ("%H%<friend%> used outside of class", &token->location);
8358 cp_lexer_purge_token (parser->lexer);
8362 ++decl_specs->specs[(int) ds_friend];
8363 /* Consume the token. */
8364 cp_lexer_consume_token (parser->lexer);
8368 /* function-specifier:
8375 cp_parser_function_specifier_opt (parser, decl_specs);
8381 ++decl_specs->specs[(int) ds_typedef];
8382 /* Consume the token. */
8383 cp_lexer_consume_token (parser->lexer);
8384 /* A constructor declarator cannot appear in a typedef. */
8385 constructor_possible_p = false;
8386 /* The "typedef" keyword can only occur in a declaration; we
8387 may as well commit at this point. */
8388 cp_parser_commit_to_tentative_parse (parser);
8390 if (decl_specs->storage_class != sc_none)
8391 decl_specs->conflicting_specifiers_p = true;
8394 /* storage-class-specifier:
8404 if (cxx_dialect == cxx98)
8406 /* Consume the token. */
8407 cp_lexer_consume_token (parser->lexer);
8409 /* Complain about `auto' as a storage specifier, if
8410 we're complaining about C++0x compatibility. */
8413 "%H%<auto%> will change meaning in C++0x; please remove it",
8416 /* Set the storage class anyway. */
8417 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8421 /* C++0x auto type-specifier. */
8422 found_decl_spec = false;
8429 /* Consume the token. */
8430 cp_lexer_consume_token (parser->lexer);
8431 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8435 /* Consume the token. */
8436 cp_lexer_consume_token (parser->lexer);
8437 ++decl_specs->specs[(int) ds_thread];
8441 /* We did not yet find a decl-specifier yet. */
8442 found_decl_spec = false;
8446 /* Constructors are a special case. The `S' in `S()' is not a
8447 decl-specifier; it is the beginning of the declarator. */
8450 && constructor_possible_p
8451 && (cp_parser_constructor_declarator_p
8452 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8454 /* If we don't have a DECL_SPEC yet, then we must be looking at
8455 a type-specifier. */
8456 if (!found_decl_spec && !constructor_p)
8458 int decl_spec_declares_class_or_enum;
8459 bool is_cv_qualifier;
8463 = cp_parser_type_specifier (parser, flags,
8465 /*is_declaration=*/true,
8466 &decl_spec_declares_class_or_enum,
8468 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8470 /* If this type-specifier referenced a user-defined type
8471 (a typedef, class-name, etc.), then we can't allow any
8472 more such type-specifiers henceforth.
8476 The longest sequence of decl-specifiers that could
8477 possibly be a type name is taken as the
8478 decl-specifier-seq of a declaration. The sequence shall
8479 be self-consistent as described below.
8483 As a general rule, at most one type-specifier is allowed
8484 in the complete decl-specifier-seq of a declaration. The
8485 only exceptions are the following:
8487 -- const or volatile can be combined with any other
8490 -- signed or unsigned can be combined with char, long,
8498 void g (const int Pc);
8500 Here, Pc is *not* part of the decl-specifier seq; it's
8501 the declarator. Therefore, once we see a type-specifier
8502 (other than a cv-qualifier), we forbid any additional
8503 user-defined types. We *do* still allow things like `int
8504 int' to be considered a decl-specifier-seq, and issue the
8505 error message later. */
8506 if (type_spec && !is_cv_qualifier)
8507 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8508 /* A constructor declarator cannot follow a type-specifier. */
8511 constructor_possible_p = false;
8512 found_decl_spec = true;
8516 /* If we still do not have a DECL_SPEC, then there are no more
8518 if (!found_decl_spec)
8521 decl_specs->any_specifiers_p = true;
8522 /* After we see one decl-specifier, further decl-specifiers are
8524 flags |= CP_PARSER_FLAGS_OPTIONAL;
8527 cp_parser_check_decl_spec (decl_specs, start_token->location);
8529 /* Don't allow a friend specifier with a class definition. */
8530 if (decl_specs->specs[(int) ds_friend] != 0
8531 && (*declares_class_or_enum & 2))
8532 error ("%Hclass definition may not be declared a friend",
8533 &start_token->location);
8536 /* Parse an (optional) storage-class-specifier.
8538 storage-class-specifier:
8547 storage-class-specifier:
8550 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8553 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8555 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8558 if (cxx_dialect != cxx98)
8560 /* Fall through for C++98. */
8567 /* Consume the token. */
8568 return cp_lexer_consume_token (parser->lexer)->u.value;
8575 /* Parse an (optional) function-specifier.
8582 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8583 Updates DECL_SPECS, if it is non-NULL. */
8586 cp_parser_function_specifier_opt (cp_parser* parser,
8587 cp_decl_specifier_seq *decl_specs)
8589 cp_token *token = cp_lexer_peek_token (parser->lexer);
8590 switch (token->keyword)
8594 ++decl_specs->specs[(int) ds_inline];
8598 /* 14.5.2.3 [temp.mem]
8600 A member function template shall not be virtual. */
8601 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8602 error ("%Htemplates may not be %<virtual%>", &token->location);
8603 else if (decl_specs)
8604 ++decl_specs->specs[(int) ds_virtual];
8609 ++decl_specs->specs[(int) ds_explicit];
8616 /* Consume the token. */
8617 return cp_lexer_consume_token (parser->lexer)->u.value;
8620 /* Parse a linkage-specification.
8622 linkage-specification:
8623 extern string-literal { declaration-seq [opt] }
8624 extern string-literal declaration */
8627 cp_parser_linkage_specification (cp_parser* parser)
8631 /* Look for the `extern' keyword. */
8632 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8634 /* Look for the string-literal. */
8635 linkage = cp_parser_string_literal (parser, false, false);
8637 /* Transform the literal into an identifier. If the literal is a
8638 wide-character string, or contains embedded NULs, then we can't
8639 handle it as the user wants. */
8640 if (strlen (TREE_STRING_POINTER (linkage))
8641 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8643 cp_parser_error (parser, "invalid linkage-specification");
8644 /* Assume C++ linkage. */
8645 linkage = lang_name_cplusplus;
8648 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8650 /* We're now using the new linkage. */
8651 push_lang_context (linkage);
8653 /* If the next token is a `{', then we're using the first
8655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8657 /* Consume the `{' token. */
8658 cp_lexer_consume_token (parser->lexer);
8659 /* Parse the declarations. */
8660 cp_parser_declaration_seq_opt (parser);
8661 /* Look for the closing `}'. */
8662 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8664 /* Otherwise, there's just one declaration. */
8667 bool saved_in_unbraced_linkage_specification_p;
8669 saved_in_unbraced_linkage_specification_p
8670 = parser->in_unbraced_linkage_specification_p;
8671 parser->in_unbraced_linkage_specification_p = true;
8672 cp_parser_declaration (parser);
8673 parser->in_unbraced_linkage_specification_p
8674 = saved_in_unbraced_linkage_specification_p;
8677 /* We're done with the linkage-specification. */
8678 pop_lang_context ();
8681 /* Parse a static_assert-declaration.
8683 static_assert-declaration:
8684 static_assert ( constant-expression , string-literal ) ;
8686 If MEMBER_P, this static_assert is a class member. */
8689 cp_parser_static_assert(cp_parser *parser, bool member_p)
8694 location_t saved_loc;
8696 /* Peek at the `static_assert' token so we can keep track of exactly
8697 where the static assertion started. */
8698 token = cp_lexer_peek_token (parser->lexer);
8699 saved_loc = token->location;
8701 /* Look for the `static_assert' keyword. */
8702 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8703 "%<static_assert%>"))
8706 /* We know we are in a static assertion; commit to any tentative
8708 if (cp_parser_parsing_tentatively (parser))
8709 cp_parser_commit_to_tentative_parse (parser);
8711 /* Parse the `(' starting the static assertion condition. */
8712 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8714 /* Parse the constant-expression. */
8716 cp_parser_constant_expression (parser,
8717 /*allow_non_constant_p=*/false,
8718 /*non_constant_p=*/NULL);
8720 /* Parse the separating `,'. */
8721 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8723 /* Parse the string-literal message. */
8724 message = cp_parser_string_literal (parser,
8725 /*translate=*/false,
8728 /* A `)' completes the static assertion. */
8729 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8730 cp_parser_skip_to_closing_parenthesis (parser,
8731 /*recovering=*/true,
8733 /*consume_paren=*/true);
8735 /* A semicolon terminates the declaration. */
8736 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8738 /* Complete the static assertion, which may mean either processing
8739 the static assert now or saving it for template instantiation. */
8740 finish_static_assert (condition, message, saved_loc, member_p);
8743 /* Parse a `decltype' type. Returns the type.
8745 simple-type-specifier:
8746 decltype ( expression ) */
8749 cp_parser_decltype (cp_parser *parser)
8752 bool id_expression_or_member_access_p = false;
8753 const char *saved_message;
8754 bool saved_integral_constant_expression_p;
8755 bool saved_non_integral_constant_expression_p;
8756 cp_token *id_expr_start_token;
8758 /* Look for the `decltype' token. */
8759 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8760 return error_mark_node;
8762 /* Types cannot be defined in a `decltype' expression. Save away the
8764 saved_message = parser->type_definition_forbidden_message;
8766 /* And create the new one. */
8767 parser->type_definition_forbidden_message
8768 = "types may not be defined in %<decltype%> expressions";
8770 /* The restrictions on constant-expressions do not apply inside
8771 decltype expressions. */
8772 saved_integral_constant_expression_p
8773 = parser->integral_constant_expression_p;
8774 saved_non_integral_constant_expression_p
8775 = parser->non_integral_constant_expression_p;
8776 parser->integral_constant_expression_p = false;
8778 /* Do not actually evaluate the expression. */
8781 /* Parse the opening `('. */
8782 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8783 return error_mark_node;
8785 /* First, try parsing an id-expression. */
8786 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8787 cp_parser_parse_tentatively (parser);
8788 expr = cp_parser_id_expression (parser,
8789 /*template_keyword_p=*/false,
8790 /*check_dependency_p=*/true,
8791 /*template_p=*/NULL,
8792 /*declarator_p=*/false,
8793 /*optional_p=*/false);
8795 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8797 bool non_integral_constant_expression_p = false;
8798 tree id_expression = expr;
8800 const char *error_msg;
8802 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8803 /* Lookup the name we got back from the id-expression. */
8804 expr = cp_parser_lookup_name (parser, expr,
8806 /*is_template=*/false,
8807 /*is_namespace=*/false,
8808 /*check_dependency=*/true,
8809 /*ambiguous_decls=*/NULL,
8810 id_expr_start_token->location);
8813 && expr != error_mark_node
8814 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8815 && TREE_CODE (expr) != TYPE_DECL
8816 && (TREE_CODE (expr) != BIT_NOT_EXPR
8817 || !TYPE_P (TREE_OPERAND (expr, 0)))
8818 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8820 /* Complete lookup of the id-expression. */
8821 expr = (finish_id_expression
8822 (id_expression, expr, parser->scope, &idk,
8823 /*integral_constant_expression_p=*/false,
8824 /*allow_non_integral_constant_expression_p=*/true,
8825 &non_integral_constant_expression_p,
8826 /*template_p=*/false,
8828 /*address_p=*/false,
8829 /*template_arg_p=*/false,
8831 id_expr_start_token->location));
8833 if (expr == error_mark_node)
8834 /* We found an id-expression, but it was something that we
8835 should not have found. This is an error, not something
8836 we can recover from, so note that we found an
8837 id-expression and we'll recover as gracefully as
8839 id_expression_or_member_access_p = true;
8843 && expr != error_mark_node
8844 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8845 /* We have an id-expression. */
8846 id_expression_or_member_access_p = true;
8849 if (!id_expression_or_member_access_p)
8851 /* Abort the id-expression parse. */
8852 cp_parser_abort_tentative_parse (parser);
8854 /* Parsing tentatively, again. */
8855 cp_parser_parse_tentatively (parser);
8857 /* Parse a class member access. */
8858 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8860 /*member_access_only_p=*/true);
8863 && expr != error_mark_node
8864 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8865 /* We have an id-expression. */
8866 id_expression_or_member_access_p = true;
8869 if (id_expression_or_member_access_p)
8870 /* We have parsed the complete id-expression or member access. */
8871 cp_parser_parse_definitely (parser);
8874 /* Abort our attempt to parse an id-expression or member access
8876 cp_parser_abort_tentative_parse (parser);
8878 /* Parse a full expression. */
8879 expr = cp_parser_expression (parser, /*cast_p=*/false);
8882 /* Go back to evaluating expressions. */
8885 /* Restore the old message and the integral constant expression
8887 parser->type_definition_forbidden_message = saved_message;
8888 parser->integral_constant_expression_p
8889 = saved_integral_constant_expression_p;
8890 parser->non_integral_constant_expression_p
8891 = saved_non_integral_constant_expression_p;
8893 if (expr == error_mark_node)
8895 /* Skip everything up to the closing `)'. */
8896 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8897 /*consume_paren=*/true);
8898 return error_mark_node;
8901 /* Parse to the closing `)'. */
8902 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8904 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8905 /*consume_paren=*/true);
8906 return error_mark_node;
8909 return finish_decltype_type (expr, id_expression_or_member_access_p);
8912 /* Special member functions [gram.special] */
8914 /* Parse a conversion-function-id.
8916 conversion-function-id:
8917 operator conversion-type-id
8919 Returns an IDENTIFIER_NODE representing the operator. */
8922 cp_parser_conversion_function_id (cp_parser* parser)
8926 tree saved_qualifying_scope;
8927 tree saved_object_scope;
8928 tree pushed_scope = NULL_TREE;
8930 /* Look for the `operator' token. */
8931 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8932 return error_mark_node;
8933 /* When we parse the conversion-type-id, the current scope will be
8934 reset. However, we need that information in able to look up the
8935 conversion function later, so we save it here. */
8936 saved_scope = parser->scope;
8937 saved_qualifying_scope = parser->qualifying_scope;
8938 saved_object_scope = parser->object_scope;
8939 /* We must enter the scope of the class so that the names of
8940 entities declared within the class are available in the
8941 conversion-type-id. For example, consider:
8948 S::operator I() { ... }
8950 In order to see that `I' is a type-name in the definition, we
8951 must be in the scope of `S'. */
8953 pushed_scope = push_scope (saved_scope);
8954 /* Parse the conversion-type-id. */
8955 type = cp_parser_conversion_type_id (parser);
8956 /* Leave the scope of the class, if any. */
8958 pop_scope (pushed_scope);
8959 /* Restore the saved scope. */
8960 parser->scope = saved_scope;
8961 parser->qualifying_scope = saved_qualifying_scope;
8962 parser->object_scope = saved_object_scope;
8963 /* If the TYPE is invalid, indicate failure. */
8964 if (type == error_mark_node)
8965 return error_mark_node;
8966 return mangle_conv_op_name_for_type (type);
8969 /* Parse a conversion-type-id:
8972 type-specifier-seq conversion-declarator [opt]
8974 Returns the TYPE specified. */
8977 cp_parser_conversion_type_id (cp_parser* parser)
8980 cp_decl_specifier_seq type_specifiers;
8981 cp_declarator *declarator;
8982 tree type_specified;
8984 /* Parse the attributes. */
8985 attributes = cp_parser_attributes_opt (parser);
8986 /* Parse the type-specifiers. */
8987 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8989 /* If that didn't work, stop. */
8990 if (type_specifiers.type == error_mark_node)
8991 return error_mark_node;
8992 /* Parse the conversion-declarator. */
8993 declarator = cp_parser_conversion_declarator_opt (parser);
8995 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8996 /*initialized=*/0, &attributes);
8998 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9000 /* Don't give this error when parsing tentatively. This happens to
9001 work because we always parse this definitively once. */
9002 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9003 && type_uses_auto (type_specified))
9005 error ("invalid use of %<auto%> in conversion operator");
9006 return error_mark_node;
9009 return type_specified;
9012 /* Parse an (optional) conversion-declarator.
9014 conversion-declarator:
9015 ptr-operator conversion-declarator [opt]
9019 static cp_declarator *
9020 cp_parser_conversion_declarator_opt (cp_parser* parser)
9022 enum tree_code code;
9024 cp_cv_quals cv_quals;
9026 /* We don't know if there's a ptr-operator next, or not. */
9027 cp_parser_parse_tentatively (parser);
9028 /* Try the ptr-operator. */
9029 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9030 /* If it worked, look for more conversion-declarators. */
9031 if (cp_parser_parse_definitely (parser))
9033 cp_declarator *declarator;
9035 /* Parse another optional declarator. */
9036 declarator = cp_parser_conversion_declarator_opt (parser);
9038 return cp_parser_make_indirect_declarator
9039 (code, class_type, cv_quals, declarator);
9045 /* Parse an (optional) ctor-initializer.
9048 : mem-initializer-list
9050 Returns TRUE iff the ctor-initializer was actually present. */
9053 cp_parser_ctor_initializer_opt (cp_parser* parser)
9055 /* If the next token is not a `:', then there is no
9056 ctor-initializer. */
9057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9059 /* Do default initialization of any bases and members. */
9060 if (DECL_CONSTRUCTOR_P (current_function_decl))
9061 finish_mem_initializers (NULL_TREE);
9066 /* Consume the `:' token. */
9067 cp_lexer_consume_token (parser->lexer);
9068 /* And the mem-initializer-list. */
9069 cp_parser_mem_initializer_list (parser);
9074 /* Parse a mem-initializer-list.
9076 mem-initializer-list:
9077 mem-initializer ... [opt]
9078 mem-initializer ... [opt] , mem-initializer-list */
9081 cp_parser_mem_initializer_list (cp_parser* parser)
9083 tree mem_initializer_list = NULL_TREE;
9084 cp_token *token = cp_lexer_peek_token (parser->lexer);
9086 /* Let the semantic analysis code know that we are starting the
9087 mem-initializer-list. */
9088 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9089 error ("%Honly constructors take base initializers",
9092 /* Loop through the list. */
9095 tree mem_initializer;
9097 token = cp_lexer_peek_token (parser->lexer);
9098 /* Parse the mem-initializer. */
9099 mem_initializer = cp_parser_mem_initializer (parser);
9100 /* If the next token is a `...', we're expanding member initializers. */
9101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9103 /* Consume the `...'. */
9104 cp_lexer_consume_token (parser->lexer);
9106 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9107 can be expanded but members cannot. */
9108 if (mem_initializer != error_mark_node
9109 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9111 error ("%Hcannot expand initializer for member %<%D%>",
9112 &token->location, TREE_PURPOSE (mem_initializer));
9113 mem_initializer = error_mark_node;
9116 /* Construct the pack expansion type. */
9117 if (mem_initializer != error_mark_node)
9118 mem_initializer = make_pack_expansion (mem_initializer);
9120 /* Add it to the list, unless it was erroneous. */
9121 if (mem_initializer != error_mark_node)
9123 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9124 mem_initializer_list = mem_initializer;
9126 /* If the next token is not a `,', we're done. */
9127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9129 /* Consume the `,' token. */
9130 cp_lexer_consume_token (parser->lexer);
9133 /* Perform semantic analysis. */
9134 if (DECL_CONSTRUCTOR_P (current_function_decl))
9135 finish_mem_initializers (mem_initializer_list);
9138 /* Parse a mem-initializer.
9141 mem-initializer-id ( expression-list [opt] )
9142 mem-initializer-id braced-init-list
9147 ( expression-list [opt] )
9149 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9150 class) or FIELD_DECL (for a non-static data member) to initialize;
9151 the TREE_VALUE is the expression-list. An empty initialization
9152 list is represented by void_list_node. */
9155 cp_parser_mem_initializer (cp_parser* parser)
9157 tree mem_initializer_id;
9158 tree expression_list;
9160 cp_token *token = cp_lexer_peek_token (parser->lexer);
9162 /* Find out what is being initialized. */
9163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9165 permerror (token->location,
9166 "anachronistic old-style base class initializer");
9167 mem_initializer_id = NULL_TREE;
9170 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9171 member = expand_member_init (mem_initializer_id);
9172 if (member && !DECL_P (member))
9173 in_base_initializer = 1;
9175 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9177 bool expr_non_constant_p;
9178 maybe_warn_cpp0x ("extended initializer lists");
9179 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9180 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9181 expression_list = build_tree_list (NULL_TREE, expression_list);
9185 = cp_parser_parenthesized_expression_list (parser, false,
9187 /*allow_expansion_p=*/true,
9188 /*non_constant_p=*/NULL);
9189 if (expression_list == error_mark_node)
9190 return error_mark_node;
9191 if (!expression_list)
9192 expression_list = void_type_node;
9194 in_base_initializer = 0;
9196 return member ? build_tree_list (member, expression_list) : error_mark_node;
9199 /* Parse a mem-initializer-id.
9202 :: [opt] nested-name-specifier [opt] class-name
9205 Returns a TYPE indicating the class to be initializer for the first
9206 production. Returns an IDENTIFIER_NODE indicating the data member
9207 to be initialized for the second production. */
9210 cp_parser_mem_initializer_id (cp_parser* parser)
9212 bool global_scope_p;
9213 bool nested_name_specifier_p;
9214 bool template_p = false;
9217 cp_token *token = cp_lexer_peek_token (parser->lexer);
9219 /* `typename' is not allowed in this context ([temp.res]). */
9220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9222 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9223 "member initializer is implicitly a type)",
9225 cp_lexer_consume_token (parser->lexer);
9227 /* Look for the optional `::' operator. */
9229 = (cp_parser_global_scope_opt (parser,
9230 /*current_scope_valid_p=*/false)
9232 /* Look for the optional nested-name-specifier. The simplest way to
9237 The keyword `typename' is not permitted in a base-specifier or
9238 mem-initializer; in these contexts a qualified name that
9239 depends on a template-parameter is implicitly assumed to be a
9242 is to assume that we have seen the `typename' keyword at this
9244 nested_name_specifier_p
9245 = (cp_parser_nested_name_specifier_opt (parser,
9246 /*typename_keyword_p=*/true,
9247 /*check_dependency_p=*/true,
9249 /*is_declaration=*/true)
9251 if (nested_name_specifier_p)
9252 template_p = cp_parser_optional_template_keyword (parser);
9253 /* If there is a `::' operator or a nested-name-specifier, then we
9254 are definitely looking for a class-name. */
9255 if (global_scope_p || nested_name_specifier_p)
9256 return cp_parser_class_name (parser,
9257 /*typename_keyword_p=*/true,
9258 /*template_keyword_p=*/template_p,
9260 /*check_dependency_p=*/true,
9261 /*class_head_p=*/false,
9262 /*is_declaration=*/true);
9263 /* Otherwise, we could also be looking for an ordinary identifier. */
9264 cp_parser_parse_tentatively (parser);
9265 /* Try a class-name. */
9266 id = cp_parser_class_name (parser,
9267 /*typename_keyword_p=*/true,
9268 /*template_keyword_p=*/false,
9270 /*check_dependency_p=*/true,
9271 /*class_head_p=*/false,
9272 /*is_declaration=*/true);
9273 /* If we found one, we're done. */
9274 if (cp_parser_parse_definitely (parser))
9276 /* Otherwise, look for an ordinary identifier. */
9277 return cp_parser_identifier (parser);
9280 /* Overloading [gram.over] */
9282 /* Parse an operator-function-id.
9284 operator-function-id:
9287 Returns an IDENTIFIER_NODE for the operator which is a
9288 human-readable spelling of the identifier, e.g., `operator +'. */
9291 cp_parser_operator_function_id (cp_parser* parser)
9293 /* Look for the `operator' keyword. */
9294 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9295 return error_mark_node;
9296 /* And then the name of the operator itself. */
9297 return cp_parser_operator (parser);
9300 /* Parse an operator.
9303 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9304 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9305 || ++ -- , ->* -> () []
9312 Returns an IDENTIFIER_NODE for the operator which is a
9313 human-readable spelling of the identifier, e.g., `operator +'. */
9316 cp_parser_operator (cp_parser* parser)
9318 tree id = NULL_TREE;
9321 /* Peek at the next token. */
9322 token = cp_lexer_peek_token (parser->lexer);
9323 /* Figure out which operator we have. */
9324 switch (token->type)
9330 /* The keyword should be either `new' or `delete'. */
9331 if (token->keyword == RID_NEW)
9333 else if (token->keyword == RID_DELETE)
9338 /* Consume the `new' or `delete' token. */
9339 cp_lexer_consume_token (parser->lexer);
9341 /* Peek at the next token. */
9342 token = cp_lexer_peek_token (parser->lexer);
9343 /* If it's a `[' token then this is the array variant of the
9345 if (token->type == CPP_OPEN_SQUARE)
9347 /* Consume the `[' token. */
9348 cp_lexer_consume_token (parser->lexer);
9349 /* Look for the `]' token. */
9350 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9351 id = ansi_opname (op == NEW_EXPR
9352 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9354 /* Otherwise, we have the non-array variant. */
9356 id = ansi_opname (op);
9362 id = ansi_opname (PLUS_EXPR);
9366 id = ansi_opname (MINUS_EXPR);
9370 id = ansi_opname (MULT_EXPR);
9374 id = ansi_opname (TRUNC_DIV_EXPR);
9378 id = ansi_opname (TRUNC_MOD_EXPR);
9382 id = ansi_opname (BIT_XOR_EXPR);
9386 id = ansi_opname (BIT_AND_EXPR);
9390 id = ansi_opname (BIT_IOR_EXPR);
9394 id = ansi_opname (BIT_NOT_EXPR);
9398 id = ansi_opname (TRUTH_NOT_EXPR);
9402 id = ansi_assopname (NOP_EXPR);
9406 id = ansi_opname (LT_EXPR);
9410 id = ansi_opname (GT_EXPR);
9414 id = ansi_assopname (PLUS_EXPR);
9418 id = ansi_assopname (MINUS_EXPR);
9422 id = ansi_assopname (MULT_EXPR);
9426 id = ansi_assopname (TRUNC_DIV_EXPR);
9430 id = ansi_assopname (TRUNC_MOD_EXPR);
9434 id = ansi_assopname (BIT_XOR_EXPR);
9438 id = ansi_assopname (BIT_AND_EXPR);
9442 id = ansi_assopname (BIT_IOR_EXPR);
9446 id = ansi_opname (LSHIFT_EXPR);
9450 id = ansi_opname (RSHIFT_EXPR);
9454 id = ansi_assopname (LSHIFT_EXPR);
9458 id = ansi_assopname (RSHIFT_EXPR);
9462 id = ansi_opname (EQ_EXPR);
9466 id = ansi_opname (NE_EXPR);
9470 id = ansi_opname (LE_EXPR);
9473 case CPP_GREATER_EQ:
9474 id = ansi_opname (GE_EXPR);
9478 id = ansi_opname (TRUTH_ANDIF_EXPR);
9482 id = ansi_opname (TRUTH_ORIF_EXPR);
9486 id = ansi_opname (POSTINCREMENT_EXPR);
9489 case CPP_MINUS_MINUS:
9490 id = ansi_opname (PREDECREMENT_EXPR);
9494 id = ansi_opname (COMPOUND_EXPR);
9497 case CPP_DEREF_STAR:
9498 id = ansi_opname (MEMBER_REF);
9502 id = ansi_opname (COMPONENT_REF);
9505 case CPP_OPEN_PAREN:
9506 /* Consume the `('. */
9507 cp_lexer_consume_token (parser->lexer);
9508 /* Look for the matching `)'. */
9509 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9510 return ansi_opname (CALL_EXPR);
9512 case CPP_OPEN_SQUARE:
9513 /* Consume the `['. */
9514 cp_lexer_consume_token (parser->lexer);
9515 /* Look for the matching `]'. */
9516 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9517 return ansi_opname (ARRAY_REF);
9520 /* Anything else is an error. */
9524 /* If we have selected an identifier, we need to consume the
9527 cp_lexer_consume_token (parser->lexer);
9528 /* Otherwise, no valid operator name was present. */
9531 cp_parser_error (parser, "expected operator");
9532 id = error_mark_node;
9538 /* Parse a template-declaration.
9540 template-declaration:
9541 export [opt] template < template-parameter-list > declaration
9543 If MEMBER_P is TRUE, this template-declaration occurs within a
9546 The grammar rule given by the standard isn't correct. What
9549 template-declaration:
9550 export [opt] template-parameter-list-seq
9551 decl-specifier-seq [opt] init-declarator [opt] ;
9552 export [opt] template-parameter-list-seq
9555 template-parameter-list-seq:
9556 template-parameter-list-seq [opt]
9557 template < template-parameter-list > */
9560 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9562 /* Check for `export'. */
9563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9565 /* Consume the `export' token. */
9566 cp_lexer_consume_token (parser->lexer);
9567 /* Warn that we do not support `export'. */
9568 warning (0, "keyword %<export%> not implemented, and will be ignored");
9571 cp_parser_template_declaration_after_export (parser, member_p);
9574 /* Parse a template-parameter-list.
9576 template-parameter-list:
9578 template-parameter-list , template-parameter
9580 Returns a TREE_LIST. Each node represents a template parameter.
9581 The nodes are connected via their TREE_CHAINs. */
9584 cp_parser_template_parameter_list (cp_parser* parser)
9586 tree parameter_list = NULL_TREE;
9588 begin_template_parm_list ();
9593 bool is_parameter_pack;
9595 /* Parse the template-parameter. */
9596 parameter = cp_parser_template_parameter (parser,
9598 &is_parameter_pack);
9599 /* Add it to the list. */
9600 if (parameter != error_mark_node)
9601 parameter_list = process_template_parm (parameter_list,
9607 tree err_parm = build_tree_list (parameter, parameter);
9608 TREE_VALUE (err_parm) = error_mark_node;
9609 parameter_list = chainon (parameter_list, err_parm);
9612 /* If the next token is not a `,', we're done. */
9613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9615 /* Otherwise, consume the `,' token. */
9616 cp_lexer_consume_token (parser->lexer);
9619 return end_template_parm_list (parameter_list);
9622 /* Parse a template-parameter.
9626 parameter-declaration
9628 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9629 the parameter. The TREE_PURPOSE is the default value, if any.
9630 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9631 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9632 set to true iff this parameter is a parameter pack. */
9635 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9636 bool *is_parameter_pack)
9639 cp_parameter_declarator *parameter_declarator;
9640 cp_declarator *id_declarator;
9643 /* Assume it is a type parameter or a template parameter. */
9644 *is_non_type = false;
9645 /* Assume it not a parameter pack. */
9646 *is_parameter_pack = false;
9647 /* Peek at the next token. */
9648 token = cp_lexer_peek_token (parser->lexer);
9649 /* If it is `class' or `template', we have a type-parameter. */
9650 if (token->keyword == RID_TEMPLATE)
9651 return cp_parser_type_parameter (parser, is_parameter_pack);
9652 /* If it is `class' or `typename' we do not know yet whether it is a
9653 type parameter or a non-type parameter. Consider:
9655 template <typename T, typename T::X X> ...
9659 template <class C, class D*> ...
9661 Here, the first parameter is a type parameter, and the second is
9662 a non-type parameter. We can tell by looking at the token after
9663 the identifier -- if it is a `,', `=', or `>' then we have a type
9665 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9667 /* Peek at the token after `class' or `typename'. */
9668 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9669 /* If it's an ellipsis, we have a template type parameter
9671 if (token->type == CPP_ELLIPSIS)
9672 return cp_parser_type_parameter (parser, is_parameter_pack);
9673 /* If it's an identifier, skip it. */
9674 if (token->type == CPP_NAME)
9675 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9676 /* Now, see if the token looks like the end of a template
9678 if (token->type == CPP_COMMA
9679 || token->type == CPP_EQ
9680 || token->type == CPP_GREATER)
9681 return cp_parser_type_parameter (parser, is_parameter_pack);
9684 /* Otherwise, it is a non-type parameter.
9688 When parsing a default template-argument for a non-type
9689 template-parameter, the first non-nested `>' is taken as the end
9690 of the template parameter-list rather than a greater-than
9692 *is_non_type = true;
9693 parameter_declarator
9694 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9695 /*parenthesized_p=*/NULL);
9697 /* If the parameter declaration is marked as a parameter pack, set
9698 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9699 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9701 if (parameter_declarator
9702 && parameter_declarator->declarator
9703 && parameter_declarator->declarator->parameter_pack_p)
9705 *is_parameter_pack = true;
9706 parameter_declarator->declarator->parameter_pack_p = false;
9709 /* If the next token is an ellipsis, and we don't already have it
9710 marked as a parameter pack, then we have a parameter pack (that
9711 has no declarator). */
9712 if (!*is_parameter_pack
9713 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9714 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9716 /* Consume the `...'. */
9717 cp_lexer_consume_token (parser->lexer);
9718 maybe_warn_variadic_templates ();
9720 *is_parameter_pack = true;
9722 /* We might end up with a pack expansion as the type of the non-type
9723 template parameter, in which case this is a non-type template
9725 else if (parameter_declarator
9726 && parameter_declarator->decl_specifiers.type
9727 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9729 *is_parameter_pack = true;
9730 parameter_declarator->decl_specifiers.type =
9731 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9734 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9736 /* Parameter packs cannot have default arguments. However, a
9737 user may try to do so, so we'll parse them and give an
9738 appropriate diagnostic here. */
9740 /* Consume the `='. */
9741 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9742 cp_lexer_consume_token (parser->lexer);
9744 /* Find the name of the parameter pack. */
9745 id_declarator = parameter_declarator->declarator;
9746 while (id_declarator && id_declarator->kind != cdk_id)
9747 id_declarator = id_declarator->declarator;
9749 if (id_declarator && id_declarator->kind == cdk_id)
9750 error ("%Htemplate parameter pack %qD cannot have a default argument",
9751 &start_token->location, id_declarator->u.id.unqualified_name);
9753 error ("%Htemplate parameter pack cannot have a default argument",
9754 &start_token->location);
9756 /* Parse the default argument, but throw away the result. */
9757 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9760 parm = grokdeclarator (parameter_declarator->declarator,
9761 ¶meter_declarator->decl_specifiers,
9762 PARM, /*initialized=*/0,
9764 if (parm == error_mark_node)
9765 return error_mark_node;
9767 return build_tree_list (parameter_declarator->default_argument, parm);
9770 /* Parse a type-parameter.
9773 class identifier [opt]
9774 class identifier [opt] = type-id
9775 typename identifier [opt]
9776 typename identifier [opt] = type-id
9777 template < template-parameter-list > class identifier [opt]
9778 template < template-parameter-list > class identifier [opt]
9781 GNU Extension (variadic templates):
9784 class ... identifier [opt]
9785 typename ... identifier [opt]
9787 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9788 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9789 the declaration of the parameter.
9791 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9794 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9799 /* Look for a keyword to tell us what kind of parameter this is. */
9800 token = cp_parser_require (parser, CPP_KEYWORD,
9801 "%<class%>, %<typename%>, or %<template%>");
9803 return error_mark_node;
9805 switch (token->keyword)
9811 tree default_argument;
9813 /* If the next token is an ellipsis, we have a template
9815 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9817 /* Consume the `...' token. */
9818 cp_lexer_consume_token (parser->lexer);
9819 maybe_warn_variadic_templates ();
9821 *is_parameter_pack = true;
9824 /* If the next token is an identifier, then it names the
9826 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9827 identifier = cp_parser_identifier (parser);
9829 identifier = NULL_TREE;
9831 /* Create the parameter. */
9832 parameter = finish_template_type_parm (class_type_node, identifier);
9834 /* If the next token is an `=', we have a default argument. */
9835 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9837 /* Consume the `=' token. */
9838 cp_lexer_consume_token (parser->lexer);
9839 /* Parse the default-argument. */
9840 push_deferring_access_checks (dk_no_deferred);
9841 default_argument = cp_parser_type_id (parser);
9843 /* Template parameter packs cannot have default
9845 if (*is_parameter_pack)
9848 error ("%Htemplate parameter pack %qD cannot have a "
9849 "default argument", &token->location, identifier);
9851 error ("%Htemplate parameter packs cannot have "
9852 "default arguments", &token->location);
9853 default_argument = NULL_TREE;
9855 pop_deferring_access_checks ();
9858 default_argument = NULL_TREE;
9860 /* Create the combined representation of the parameter and the
9861 default argument. */
9862 parameter = build_tree_list (default_argument, parameter);
9868 tree parameter_list;
9870 tree default_argument;
9872 /* Look for the `<'. */
9873 cp_parser_require (parser, CPP_LESS, "%<<%>");
9874 /* Parse the template-parameter-list. */
9875 parameter_list = cp_parser_template_parameter_list (parser);
9876 /* Look for the `>'. */
9877 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9878 /* Look for the `class' keyword. */
9879 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9880 /* If the next token is an ellipsis, we have a template
9882 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9884 /* Consume the `...' token. */
9885 cp_lexer_consume_token (parser->lexer);
9886 maybe_warn_variadic_templates ();
9888 *is_parameter_pack = true;
9890 /* If the next token is an `=', then there is a
9891 default-argument. If the next token is a `>', we are at
9892 the end of the parameter-list. If the next token is a `,',
9893 then we are at the end of this parameter. */
9894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9895 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9896 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9898 identifier = cp_parser_identifier (parser);
9899 /* Treat invalid names as if the parameter were nameless. */
9900 if (identifier == error_mark_node)
9901 identifier = NULL_TREE;
9904 identifier = NULL_TREE;
9906 /* Create the template parameter. */
9907 parameter = finish_template_template_parm (class_type_node,
9910 /* If the next token is an `=', then there is a
9911 default-argument. */
9912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9916 /* Consume the `='. */
9917 cp_lexer_consume_token (parser->lexer);
9918 /* Parse the id-expression. */
9919 push_deferring_access_checks (dk_no_deferred);
9920 /* save token before parsing the id-expression, for error
9922 token = cp_lexer_peek_token (parser->lexer);
9924 = cp_parser_id_expression (parser,
9925 /*template_keyword_p=*/false,
9926 /*check_dependency_p=*/true,
9927 /*template_p=*/&is_template,
9928 /*declarator_p=*/false,
9929 /*optional_p=*/false);
9930 if (TREE_CODE (default_argument) == TYPE_DECL)
9931 /* If the id-expression was a template-id that refers to
9932 a template-class, we already have the declaration here,
9933 so no further lookup is needed. */
9936 /* Look up the name. */
9938 = cp_parser_lookup_name (parser, default_argument,
9940 /*is_template=*/is_template,
9941 /*is_namespace=*/false,
9942 /*check_dependency=*/true,
9943 /*ambiguous_decls=*/NULL,
9945 /* See if the default argument is valid. */
9947 = check_template_template_default_arg (default_argument);
9949 /* Template parameter packs cannot have default
9951 if (*is_parameter_pack)
9954 error ("%Htemplate parameter pack %qD cannot "
9955 "have a default argument",
9956 &token->location, identifier);
9958 error ("%Htemplate parameter packs cannot "
9959 "have default arguments",
9961 default_argument = NULL_TREE;
9963 pop_deferring_access_checks ();
9966 default_argument = NULL_TREE;
9968 /* Create the combined representation of the parameter and the
9969 default argument. */
9970 parameter = build_tree_list (default_argument, parameter);
9982 /* Parse a template-id.
9985 template-name < template-argument-list [opt] >
9987 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9988 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9989 returned. Otherwise, if the template-name names a function, or set
9990 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9991 names a class, returns a TYPE_DECL for the specialization.
9993 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9994 uninstantiated templates. */
9997 cp_parser_template_id (cp_parser *parser,
9998 bool template_keyword_p,
9999 bool check_dependency_p,
10000 bool is_declaration)
10006 cp_token_position start_of_id = 0;
10007 deferred_access_check *chk;
10008 VEC (deferred_access_check,gc) *access_check;
10009 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10010 bool is_identifier;
10012 /* If the next token corresponds to a template-id, there is no need
10014 next_token = cp_lexer_peek_token (parser->lexer);
10015 if (next_token->type == CPP_TEMPLATE_ID)
10017 struct tree_check *check_value;
10019 /* Get the stored value. */
10020 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10021 /* Perform any access checks that were deferred. */
10022 access_check = check_value->checks;
10026 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10029 perform_or_defer_access_check (chk->binfo,
10034 /* Return the stored value. */
10035 return check_value->value;
10038 /* Avoid performing name lookup if there is no possibility of
10039 finding a template-id. */
10040 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10041 || (next_token->type == CPP_NAME
10042 && !cp_parser_nth_token_starts_template_argument_list_p
10045 cp_parser_error (parser, "expected template-id");
10046 return error_mark_node;
10049 /* Remember where the template-id starts. */
10050 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10051 start_of_id = cp_lexer_token_position (parser->lexer, false);
10053 push_deferring_access_checks (dk_deferred);
10055 /* Parse the template-name. */
10056 is_identifier = false;
10057 token = cp_lexer_peek_token (parser->lexer);
10058 templ = cp_parser_template_name (parser, template_keyword_p,
10059 check_dependency_p,
10062 if (templ == error_mark_node || is_identifier)
10064 pop_deferring_access_checks ();
10068 /* If we find the sequence `[:' after a template-name, it's probably
10069 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10070 parse correctly the argument list. */
10071 next_token = cp_lexer_peek_token (parser->lexer);
10072 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10073 if (next_token->type == CPP_OPEN_SQUARE
10074 && next_token->flags & DIGRAPH
10075 && next_token_2->type == CPP_COLON
10076 && !(next_token_2->flags & PREV_WHITE))
10078 cp_parser_parse_tentatively (parser);
10079 /* Change `:' into `::'. */
10080 next_token_2->type = CPP_SCOPE;
10081 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10083 cp_lexer_consume_token (parser->lexer);
10085 /* Parse the arguments. */
10086 arguments = cp_parser_enclosed_template_argument_list (parser);
10087 if (!cp_parser_parse_definitely (parser))
10089 /* If we couldn't parse an argument list, then we revert our changes
10090 and return simply an error. Maybe this is not a template-id
10092 next_token_2->type = CPP_COLON;
10093 cp_parser_error (parser, "expected %<<%>");
10094 pop_deferring_access_checks ();
10095 return error_mark_node;
10097 /* Otherwise, emit an error about the invalid digraph, but continue
10098 parsing because we got our argument list. */
10099 if (permerror (next_token->location,
10100 "%<<::%> cannot begin a template-argument list"))
10102 static bool hint = false;
10103 inform (next_token->location,
10104 "%<<:%> is an alternate spelling for %<[%>."
10105 " Insert whitespace between %<<%> and %<::%>");
10106 if (!hint && !flag_permissive)
10108 inform (next_token->location, "(if you use %<-fpermissive%>"
10109 " G++ will accept your code)");
10116 /* Look for the `<' that starts the template-argument-list. */
10117 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10119 pop_deferring_access_checks ();
10120 return error_mark_node;
10122 /* Parse the arguments. */
10123 arguments = cp_parser_enclosed_template_argument_list (parser);
10126 /* Build a representation of the specialization. */
10127 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10128 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10129 else if (DECL_CLASS_TEMPLATE_P (templ)
10130 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10132 bool entering_scope;
10133 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10134 template (rather than some instantiation thereof) only if
10135 is not nested within some other construct. For example, in
10136 "template <typename T> void f(T) { A<T>::", A<T> is just an
10137 instantiation of A. */
10138 entering_scope = (template_parm_scope_p ()
10139 && cp_lexer_next_token_is (parser->lexer,
10142 = finish_template_type (templ, arguments, entering_scope);
10146 /* If it's not a class-template or a template-template, it should be
10147 a function-template. */
10148 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10149 || TREE_CODE (templ) == OVERLOAD
10150 || BASELINK_P (templ)));
10152 template_id = lookup_template_function (templ, arguments);
10155 /* If parsing tentatively, replace the sequence of tokens that makes
10156 up the template-id with a CPP_TEMPLATE_ID token. That way,
10157 should we re-parse the token stream, we will not have to repeat
10158 the effort required to do the parse, nor will we issue duplicate
10159 error messages about problems during instantiation of the
10163 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10165 /* Reset the contents of the START_OF_ID token. */
10166 token->type = CPP_TEMPLATE_ID;
10167 /* Retrieve any deferred checks. Do not pop this access checks yet
10168 so the memory will not be reclaimed during token replacing below. */
10169 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10170 token->u.tree_check_value->value = template_id;
10171 token->u.tree_check_value->checks = get_deferred_access_checks ();
10172 token->keyword = RID_MAX;
10174 /* Purge all subsequent tokens. */
10175 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10177 /* ??? Can we actually assume that, if template_id ==
10178 error_mark_node, we will have issued a diagnostic to the
10179 user, as opposed to simply marking the tentative parse as
10181 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10182 error ("%Hparse error in template argument list",
10186 pop_deferring_access_checks ();
10187 return template_id;
10190 /* Parse a template-name.
10195 The standard should actually say:
10199 operator-function-id
10201 A defect report has been filed about this issue.
10203 A conversion-function-id cannot be a template name because they cannot
10204 be part of a template-id. In fact, looking at this code:
10206 a.operator K<int>()
10208 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10209 It is impossible to call a templated conversion-function-id with an
10210 explicit argument list, since the only allowed template parameter is
10211 the type to which it is converting.
10213 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10214 `template' keyword, in a construction like:
10218 In that case `f' is taken to be a template-name, even though there
10219 is no way of knowing for sure.
10221 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10222 name refers to a set of overloaded functions, at least one of which
10223 is a template, or an IDENTIFIER_NODE with the name of the template,
10224 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10225 names are looked up inside uninstantiated templates. */
10228 cp_parser_template_name (cp_parser* parser,
10229 bool template_keyword_p,
10230 bool check_dependency_p,
10231 bool is_declaration,
10232 bool *is_identifier)
10237 cp_token *token = cp_lexer_peek_token (parser->lexer);
10239 /* If the next token is `operator', then we have either an
10240 operator-function-id or a conversion-function-id. */
10241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10243 /* We don't know whether we're looking at an
10244 operator-function-id or a conversion-function-id. */
10245 cp_parser_parse_tentatively (parser);
10246 /* Try an operator-function-id. */
10247 identifier = cp_parser_operator_function_id (parser);
10248 /* If that didn't work, try a conversion-function-id. */
10249 if (!cp_parser_parse_definitely (parser))
10251 cp_parser_error (parser, "expected template-name");
10252 return error_mark_node;
10255 /* Look for the identifier. */
10257 identifier = cp_parser_identifier (parser);
10259 /* If we didn't find an identifier, we don't have a template-id. */
10260 if (identifier == error_mark_node)
10261 return error_mark_node;
10263 /* If the name immediately followed the `template' keyword, then it
10264 is a template-name. However, if the next token is not `<', then
10265 we do not treat it as a template-name, since it is not being used
10266 as part of a template-id. This enables us to handle constructs
10269 template <typename T> struct S { S(); };
10270 template <typename T> S<T>::S();
10272 correctly. We would treat `S' as a template -- if it were `S<T>'
10273 -- but we do not if there is no `<'. */
10275 if (processing_template_decl
10276 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10278 /* In a declaration, in a dependent context, we pretend that the
10279 "template" keyword was present in order to improve error
10280 recovery. For example, given:
10282 template <typename T> void f(T::X<int>);
10284 we want to treat "X<int>" as a template-id. */
10286 && !template_keyword_p
10287 && parser->scope && TYPE_P (parser->scope)
10288 && check_dependency_p
10289 && dependent_type_p (parser->scope)
10290 /* Do not do this for dtors (or ctors), since they never
10291 need the template keyword before their name. */
10292 && !constructor_name_p (identifier, parser->scope))
10294 cp_token_position start = 0;
10296 /* Explain what went wrong. */
10297 error ("%Hnon-template %qD used as template",
10298 &token->location, identifier);
10299 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10300 parser->scope, identifier);
10301 /* If parsing tentatively, find the location of the "<" token. */
10302 if (cp_parser_simulate_error (parser))
10303 start = cp_lexer_token_position (parser->lexer, true);
10304 /* Parse the template arguments so that we can issue error
10305 messages about them. */
10306 cp_lexer_consume_token (parser->lexer);
10307 cp_parser_enclosed_template_argument_list (parser);
10308 /* Skip tokens until we find a good place from which to
10309 continue parsing. */
10310 cp_parser_skip_to_closing_parenthesis (parser,
10311 /*recovering=*/true,
10313 /*consume_paren=*/false);
10314 /* If parsing tentatively, permanently remove the
10315 template argument list. That will prevent duplicate
10316 error messages from being issued about the missing
10317 "template" keyword. */
10319 cp_lexer_purge_tokens_after (parser->lexer, start);
10321 *is_identifier = true;
10325 /* If the "template" keyword is present, then there is generally
10326 no point in doing name-lookup, so we just return IDENTIFIER.
10327 But, if the qualifying scope is non-dependent then we can
10328 (and must) do name-lookup normally. */
10329 if (template_keyword_p
10331 || (TYPE_P (parser->scope)
10332 && dependent_type_p (parser->scope))))
10336 /* Look up the name. */
10337 decl = cp_parser_lookup_name (parser, identifier,
10339 /*is_template=*/false,
10340 /*is_namespace=*/false,
10341 check_dependency_p,
10342 /*ambiguous_decls=*/NULL,
10344 decl = maybe_get_template_decl_from_type_decl (decl);
10346 /* If DECL is a template, then the name was a template-name. */
10347 if (TREE_CODE (decl) == TEMPLATE_DECL)
10351 tree fn = NULL_TREE;
10353 /* The standard does not explicitly indicate whether a name that
10354 names a set of overloaded declarations, some of which are
10355 templates, is a template-name. However, such a name should
10356 be a template-name; otherwise, there is no way to form a
10357 template-id for the overloaded templates. */
10358 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10359 if (TREE_CODE (fns) == OVERLOAD)
10360 for (fn = fns; fn; fn = OVL_NEXT (fn))
10361 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10366 /* The name does not name a template. */
10367 cp_parser_error (parser, "expected template-name");
10368 return error_mark_node;
10372 /* If DECL is dependent, and refers to a function, then just return
10373 its name; we will look it up again during template instantiation. */
10374 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10376 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10377 if (TYPE_P (scope) && dependent_type_p (scope))
10384 /* Parse a template-argument-list.
10386 template-argument-list:
10387 template-argument ... [opt]
10388 template-argument-list , template-argument ... [opt]
10390 Returns a TREE_VEC containing the arguments. */
10393 cp_parser_template_argument_list (cp_parser* parser)
10395 tree fixed_args[10];
10396 unsigned n_args = 0;
10397 unsigned alloced = 10;
10398 tree *arg_ary = fixed_args;
10400 bool saved_in_template_argument_list_p;
10402 bool saved_non_ice_p;
10404 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10405 parser->in_template_argument_list_p = true;
10406 /* Even if the template-id appears in an integral
10407 constant-expression, the contents of the argument list do
10409 saved_ice_p = parser->integral_constant_expression_p;
10410 parser->integral_constant_expression_p = false;
10411 saved_non_ice_p = parser->non_integral_constant_expression_p;
10412 parser->non_integral_constant_expression_p = false;
10413 /* Parse the arguments. */
10419 /* Consume the comma. */
10420 cp_lexer_consume_token (parser->lexer);
10422 /* Parse the template-argument. */
10423 argument = cp_parser_template_argument (parser);
10425 /* If the next token is an ellipsis, we're expanding a template
10427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10429 /* Consume the `...' token. */
10430 cp_lexer_consume_token (parser->lexer);
10432 /* Make the argument into a TYPE_PACK_EXPANSION or
10433 EXPR_PACK_EXPANSION. */
10434 argument = make_pack_expansion (argument);
10437 if (n_args == alloced)
10441 if (arg_ary == fixed_args)
10443 arg_ary = XNEWVEC (tree, alloced);
10444 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10447 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10449 arg_ary[n_args++] = argument;
10451 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10453 vec = make_tree_vec (n_args);
10456 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10458 if (arg_ary != fixed_args)
10460 parser->non_integral_constant_expression_p = saved_non_ice_p;
10461 parser->integral_constant_expression_p = saved_ice_p;
10462 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10466 /* Parse a template-argument.
10469 assignment-expression
10473 The representation is that of an assignment-expression, type-id, or
10474 id-expression -- except that the qualified id-expression is
10475 evaluated, so that the value returned is either a DECL or an
10478 Although the standard says "assignment-expression", it forbids
10479 throw-expressions or assignments in the template argument.
10480 Therefore, we use "conditional-expression" instead. */
10483 cp_parser_template_argument (cp_parser* parser)
10488 bool maybe_type_id = false;
10489 cp_token *token = NULL, *argument_start_token = NULL;
10492 /* There's really no way to know what we're looking at, so we just
10493 try each alternative in order.
10497 In a template-argument, an ambiguity between a type-id and an
10498 expression is resolved to a type-id, regardless of the form of
10499 the corresponding template-parameter.
10501 Therefore, we try a type-id first. */
10502 cp_parser_parse_tentatively (parser);
10503 argument = cp_parser_type_id (parser);
10504 /* If there was no error parsing the type-id but the next token is a
10505 '>>', our behavior depends on which dialect of C++ we're
10506 parsing. In C++98, we probably found a typo for '> >'. But there
10507 are type-id which are also valid expressions. For instance:
10509 struct X { int operator >> (int); };
10510 template <int V> struct Foo {};
10513 Here 'X()' is a valid type-id of a function type, but the user just
10514 wanted to write the expression "X() >> 5". Thus, we remember that we
10515 found a valid type-id, but we still try to parse the argument as an
10516 expression to see what happens.
10518 In C++0x, the '>>' will be considered two separate '>'
10520 if (!cp_parser_error_occurred (parser)
10521 && cxx_dialect == cxx98
10522 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10524 maybe_type_id = true;
10525 cp_parser_abort_tentative_parse (parser);
10529 /* If the next token isn't a `,' or a `>', then this argument wasn't
10530 really finished. This means that the argument is not a valid
10532 if (!cp_parser_next_token_ends_template_argument_p (parser))
10533 cp_parser_error (parser, "expected template-argument");
10534 /* If that worked, we're done. */
10535 if (cp_parser_parse_definitely (parser))
10538 /* We're still not sure what the argument will be. */
10539 cp_parser_parse_tentatively (parser);
10540 /* Try a template. */
10541 argument_start_token = cp_lexer_peek_token (parser->lexer);
10542 argument = cp_parser_id_expression (parser,
10543 /*template_keyword_p=*/false,
10544 /*check_dependency_p=*/true,
10546 /*declarator_p=*/false,
10547 /*optional_p=*/false);
10548 /* If the next token isn't a `,' or a `>', then this argument wasn't
10549 really finished. */
10550 if (!cp_parser_next_token_ends_template_argument_p (parser))
10551 cp_parser_error (parser, "expected template-argument");
10552 if (!cp_parser_error_occurred (parser))
10554 /* Figure out what is being referred to. If the id-expression
10555 was for a class template specialization, then we will have a
10556 TYPE_DECL at this point. There is no need to do name lookup
10557 at this point in that case. */
10558 if (TREE_CODE (argument) != TYPE_DECL)
10559 argument = cp_parser_lookup_name (parser, argument,
10561 /*is_template=*/template_p,
10562 /*is_namespace=*/false,
10563 /*check_dependency=*/true,
10564 /*ambiguous_decls=*/NULL,
10565 argument_start_token->location);
10566 if (TREE_CODE (argument) != TEMPLATE_DECL
10567 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10568 cp_parser_error (parser, "expected template-name");
10570 if (cp_parser_parse_definitely (parser))
10572 /* It must be a non-type argument. There permitted cases are given
10573 in [temp.arg.nontype]:
10575 -- an integral constant-expression of integral or enumeration
10578 -- the name of a non-type template-parameter; or
10580 -- the name of an object or function with external linkage...
10582 -- the address of an object or function with external linkage...
10584 -- a pointer to member... */
10585 /* Look for a non-type template parameter. */
10586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10588 cp_parser_parse_tentatively (parser);
10589 argument = cp_parser_primary_expression (parser,
10590 /*address_p=*/false,
10592 /*template_arg_p=*/true,
10594 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10595 || !cp_parser_next_token_ends_template_argument_p (parser))
10596 cp_parser_simulate_error (parser);
10597 if (cp_parser_parse_definitely (parser))
10601 /* If the next token is "&", the argument must be the address of an
10602 object or function with external linkage. */
10603 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10605 cp_lexer_consume_token (parser->lexer);
10606 /* See if we might have an id-expression. */
10607 token = cp_lexer_peek_token (parser->lexer);
10608 if (token->type == CPP_NAME
10609 || token->keyword == RID_OPERATOR
10610 || token->type == CPP_SCOPE
10611 || token->type == CPP_TEMPLATE_ID
10612 || token->type == CPP_NESTED_NAME_SPECIFIER)
10614 cp_parser_parse_tentatively (parser);
10615 argument = cp_parser_primary_expression (parser,
10618 /*template_arg_p=*/true,
10620 if (cp_parser_error_occurred (parser)
10621 || !cp_parser_next_token_ends_template_argument_p (parser))
10622 cp_parser_abort_tentative_parse (parser);
10625 if (TREE_CODE (argument) == INDIRECT_REF)
10627 gcc_assert (REFERENCE_REF_P (argument));
10628 argument = TREE_OPERAND (argument, 0);
10631 if (TREE_CODE (argument) == VAR_DECL)
10633 /* A variable without external linkage might still be a
10634 valid constant-expression, so no error is issued here
10635 if the external-linkage check fails. */
10636 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10637 cp_parser_simulate_error (parser);
10639 else if (is_overloaded_fn (argument))
10640 /* All overloaded functions are allowed; if the external
10641 linkage test does not pass, an error will be issued
10645 && (TREE_CODE (argument) == OFFSET_REF
10646 || TREE_CODE (argument) == SCOPE_REF))
10647 /* A pointer-to-member. */
10649 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10652 cp_parser_simulate_error (parser);
10654 if (cp_parser_parse_definitely (parser))
10657 argument = build_x_unary_op (ADDR_EXPR, argument,
10658 tf_warning_or_error);
10663 /* If the argument started with "&", there are no other valid
10664 alternatives at this point. */
10667 cp_parser_error (parser, "invalid non-type template argument");
10668 return error_mark_node;
10671 /* If the argument wasn't successfully parsed as a type-id followed
10672 by '>>', the argument can only be a constant expression now.
10673 Otherwise, we try parsing the constant-expression tentatively,
10674 because the argument could really be a type-id. */
10676 cp_parser_parse_tentatively (parser);
10677 argument = cp_parser_constant_expression (parser,
10678 /*allow_non_constant_p=*/false,
10679 /*non_constant_p=*/NULL);
10680 argument = fold_non_dependent_expr (argument);
10681 if (!maybe_type_id)
10683 if (!cp_parser_next_token_ends_template_argument_p (parser))
10684 cp_parser_error (parser, "expected template-argument");
10685 if (cp_parser_parse_definitely (parser))
10687 /* We did our best to parse the argument as a non type-id, but that
10688 was the only alternative that matched (albeit with a '>' after
10689 it). We can assume it's just a typo from the user, and a
10690 diagnostic will then be issued. */
10691 return cp_parser_type_id (parser);
10694 /* Parse an explicit-instantiation.
10696 explicit-instantiation:
10697 template declaration
10699 Although the standard says `declaration', what it really means is:
10701 explicit-instantiation:
10702 template decl-specifier-seq [opt] declarator [opt] ;
10704 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10705 supposed to be allowed. A defect report has been filed about this
10710 explicit-instantiation:
10711 storage-class-specifier template
10712 decl-specifier-seq [opt] declarator [opt] ;
10713 function-specifier template
10714 decl-specifier-seq [opt] declarator [opt] ; */
10717 cp_parser_explicit_instantiation (cp_parser* parser)
10719 int declares_class_or_enum;
10720 cp_decl_specifier_seq decl_specifiers;
10721 tree extension_specifier = NULL_TREE;
10724 /* Look for an (optional) storage-class-specifier or
10725 function-specifier. */
10726 if (cp_parser_allow_gnu_extensions_p (parser))
10728 extension_specifier
10729 = cp_parser_storage_class_specifier_opt (parser);
10730 if (!extension_specifier)
10731 extension_specifier
10732 = cp_parser_function_specifier_opt (parser,
10733 /*decl_specs=*/NULL);
10736 /* Look for the `template' keyword. */
10737 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10738 /* Let the front end know that we are processing an explicit
10740 begin_explicit_instantiation ();
10741 /* [temp.explicit] says that we are supposed to ignore access
10742 control while processing explicit instantiation directives. */
10743 push_deferring_access_checks (dk_no_check);
10744 /* Parse a decl-specifier-seq. */
10745 token = cp_lexer_peek_token (parser->lexer);
10746 cp_parser_decl_specifier_seq (parser,
10747 CP_PARSER_FLAGS_OPTIONAL,
10749 &declares_class_or_enum);
10750 /* If there was exactly one decl-specifier, and it declared a class,
10751 and there's no declarator, then we have an explicit type
10753 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10757 type = check_tag_decl (&decl_specifiers);
10758 /* Turn access control back on for names used during
10759 template instantiation. */
10760 pop_deferring_access_checks ();
10762 do_type_instantiation (type, extension_specifier,
10763 /*complain=*/tf_error);
10767 cp_declarator *declarator;
10770 /* Parse the declarator. */
10772 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10773 /*ctor_dtor_or_conv_p=*/NULL,
10774 /*parenthesized_p=*/NULL,
10775 /*member_p=*/false);
10776 if (declares_class_or_enum & 2)
10777 cp_parser_check_for_definition_in_return_type (declarator,
10778 decl_specifiers.type,
10779 decl_specifiers.type_location);
10780 if (declarator != cp_error_declarator)
10782 decl = grokdeclarator (declarator, &decl_specifiers,
10783 NORMAL, 0, &decl_specifiers.attributes);
10784 /* Turn access control back on for names used during
10785 template instantiation. */
10786 pop_deferring_access_checks ();
10787 /* Do the explicit instantiation. */
10788 do_decl_instantiation (decl, extension_specifier);
10792 pop_deferring_access_checks ();
10793 /* Skip the body of the explicit instantiation. */
10794 cp_parser_skip_to_end_of_statement (parser);
10797 /* We're done with the instantiation. */
10798 end_explicit_instantiation ();
10800 cp_parser_consume_semicolon_at_end_of_statement (parser);
10803 /* Parse an explicit-specialization.
10805 explicit-specialization:
10806 template < > declaration
10808 Although the standard says `declaration', what it really means is:
10810 explicit-specialization:
10811 template <> decl-specifier [opt] init-declarator [opt] ;
10812 template <> function-definition
10813 template <> explicit-specialization
10814 template <> template-declaration */
10817 cp_parser_explicit_specialization (cp_parser* parser)
10819 bool need_lang_pop;
10820 cp_token *token = cp_lexer_peek_token (parser->lexer);
10822 /* Look for the `template' keyword. */
10823 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10824 /* Look for the `<'. */
10825 cp_parser_require (parser, CPP_LESS, "%<<%>");
10826 /* Look for the `>'. */
10827 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10828 /* We have processed another parameter list. */
10829 ++parser->num_template_parameter_lists;
10832 A template ... explicit specialization ... shall not have C
10834 if (current_lang_name == lang_name_c)
10836 error ("%Htemplate specialization with C linkage", &token->location);
10837 /* Give it C++ linkage to avoid confusing other parts of the
10839 push_lang_context (lang_name_cplusplus);
10840 need_lang_pop = true;
10843 need_lang_pop = false;
10844 /* Let the front end know that we are beginning a specialization. */
10845 if (!begin_specialization ())
10847 end_specialization ();
10848 cp_parser_skip_to_end_of_block_or_statement (parser);
10852 /* If the next keyword is `template', we need to figure out whether
10853 or not we're looking a template-declaration. */
10854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10856 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10857 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10858 cp_parser_template_declaration_after_export (parser,
10859 /*member_p=*/false);
10861 cp_parser_explicit_specialization (parser);
10864 /* Parse the dependent declaration. */
10865 cp_parser_single_declaration (parser,
10867 /*member_p=*/false,
10868 /*explicit_specialization_p=*/true,
10869 /*friend_p=*/NULL);
10870 /* We're done with the specialization. */
10871 end_specialization ();
10872 /* For the erroneous case of a template with C linkage, we pushed an
10873 implicit C++ linkage scope; exit that scope now. */
10875 pop_lang_context ();
10876 /* We're done with this parameter list. */
10877 --parser->num_template_parameter_lists;
10880 /* Parse a type-specifier.
10883 simple-type-specifier
10886 elaborated-type-specifier
10894 Returns a representation of the type-specifier. For a
10895 class-specifier, enum-specifier, or elaborated-type-specifier, a
10896 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10898 The parser flags FLAGS is used to control type-specifier parsing.
10900 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10901 in a decl-specifier-seq.
10903 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10904 class-specifier, enum-specifier, or elaborated-type-specifier, then
10905 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10906 if a type is declared; 2 if it is defined. Otherwise, it is set to
10909 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10910 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10911 is set to FALSE. */
10914 cp_parser_type_specifier (cp_parser* parser,
10915 cp_parser_flags flags,
10916 cp_decl_specifier_seq *decl_specs,
10917 bool is_declaration,
10918 int* declares_class_or_enum,
10919 bool* is_cv_qualifier)
10921 tree type_spec = NULL_TREE;
10924 cp_decl_spec ds = ds_last;
10926 /* Assume this type-specifier does not declare a new type. */
10927 if (declares_class_or_enum)
10928 *declares_class_or_enum = 0;
10929 /* And that it does not specify a cv-qualifier. */
10930 if (is_cv_qualifier)
10931 *is_cv_qualifier = false;
10932 /* Peek at the next token. */
10933 token = cp_lexer_peek_token (parser->lexer);
10935 /* If we're looking at a keyword, we can use that to guide the
10936 production we choose. */
10937 keyword = token->keyword;
10941 /* Look for the enum-specifier. */
10942 type_spec = cp_parser_enum_specifier (parser);
10943 /* If that worked, we're done. */
10946 if (declares_class_or_enum)
10947 *declares_class_or_enum = 2;
10949 cp_parser_set_decl_spec_type (decl_specs,
10952 /*user_defined_p=*/true);
10956 goto elaborated_type_specifier;
10958 /* Any of these indicate either a class-specifier, or an
10959 elaborated-type-specifier. */
10963 /* Parse tentatively so that we can back up if we don't find a
10964 class-specifier. */
10965 cp_parser_parse_tentatively (parser);
10966 /* Look for the class-specifier. */
10967 type_spec = cp_parser_class_specifier (parser);
10968 /* If that worked, we're done. */
10969 if (cp_parser_parse_definitely (parser))
10971 if (declares_class_or_enum)
10972 *declares_class_or_enum = 2;
10974 cp_parser_set_decl_spec_type (decl_specs,
10977 /*user_defined_p=*/true);
10981 /* Fall through. */
10982 elaborated_type_specifier:
10983 /* We're declaring (not defining) a class or enum. */
10984 if (declares_class_or_enum)
10985 *declares_class_or_enum = 1;
10987 /* Fall through. */
10989 /* Look for an elaborated-type-specifier. */
10991 = (cp_parser_elaborated_type_specifier
10993 decl_specs && decl_specs->specs[(int) ds_friend],
10996 cp_parser_set_decl_spec_type (decl_specs,
10999 /*user_defined_p=*/true);
11004 if (is_cv_qualifier)
11005 *is_cv_qualifier = true;
11010 if (is_cv_qualifier)
11011 *is_cv_qualifier = true;
11016 if (is_cv_qualifier)
11017 *is_cv_qualifier = true;
11021 /* The `__complex__' keyword is a GNU extension. */
11029 /* Handle simple keywords. */
11034 ++decl_specs->specs[(int)ds];
11035 decl_specs->any_specifiers_p = true;
11037 return cp_lexer_consume_token (parser->lexer)->u.value;
11040 /* If we do not already have a type-specifier, assume we are looking
11041 at a simple-type-specifier. */
11042 type_spec = cp_parser_simple_type_specifier (parser,
11046 /* If we didn't find a type-specifier, and a type-specifier was not
11047 optional in this context, issue an error message. */
11048 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11050 cp_parser_error (parser, "expected type specifier");
11051 return error_mark_node;
11057 /* Parse a simple-type-specifier.
11059 simple-type-specifier:
11060 :: [opt] nested-name-specifier [opt] type-name
11061 :: [opt] nested-name-specifier template template-id
11076 simple-type-specifier:
11078 decltype ( expression )
11084 simple-type-specifier:
11085 __typeof__ unary-expression
11086 __typeof__ ( type-id )
11088 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11089 appropriately updated. */
11092 cp_parser_simple_type_specifier (cp_parser* parser,
11093 cp_decl_specifier_seq *decl_specs,
11094 cp_parser_flags flags)
11096 tree type = NULL_TREE;
11099 /* Peek at the next token. */
11100 token = cp_lexer_peek_token (parser->lexer);
11102 /* If we're looking at a keyword, things are easy. */
11103 switch (token->keyword)
11107 decl_specs->explicit_char_p = true;
11108 type = char_type_node;
11111 type = char16_type_node;
11114 type = char32_type_node;
11117 type = wchar_type_node;
11120 type = boolean_type_node;
11124 ++decl_specs->specs[(int) ds_short];
11125 type = short_integer_type_node;
11129 decl_specs->explicit_int_p = true;
11130 type = integer_type_node;
11134 ++decl_specs->specs[(int) ds_long];
11135 type = long_integer_type_node;
11139 ++decl_specs->specs[(int) ds_signed];
11140 type = integer_type_node;
11144 ++decl_specs->specs[(int) ds_unsigned];
11145 type = unsigned_type_node;
11148 type = float_type_node;
11151 type = double_type_node;
11154 type = void_type_node;
11158 maybe_warn_cpp0x ("C++0x auto");
11159 type = make_auto ();
11163 /* Parse the `decltype' type. */
11164 type = cp_parser_decltype (parser);
11167 cp_parser_set_decl_spec_type (decl_specs, type,
11169 /*user_defined_p=*/true);
11174 /* Consume the `typeof' token. */
11175 cp_lexer_consume_token (parser->lexer);
11176 /* Parse the operand to `typeof'. */
11177 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11178 /* If it is not already a TYPE, take its type. */
11179 if (!TYPE_P (type))
11180 type = finish_typeof (type);
11183 cp_parser_set_decl_spec_type (decl_specs, type,
11185 /*user_defined_p=*/true);
11193 /* If the type-specifier was for a built-in type, we're done. */
11198 /* Record the type. */
11200 && (token->keyword != RID_SIGNED
11201 && token->keyword != RID_UNSIGNED
11202 && token->keyword != RID_SHORT
11203 && token->keyword != RID_LONG))
11204 cp_parser_set_decl_spec_type (decl_specs,
11207 /*user_defined=*/false);
11209 decl_specs->any_specifiers_p = true;
11211 /* Consume the token. */
11212 id = cp_lexer_consume_token (parser->lexer)->u.value;
11214 /* There is no valid C++ program where a non-template type is
11215 followed by a "<". That usually indicates that the user thought
11216 that the type was a template. */
11217 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11219 return TYPE_NAME (type);
11222 /* The type-specifier must be a user-defined type. */
11223 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11228 /* Don't gobble tokens or issue error messages if this is an
11229 optional type-specifier. */
11230 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11231 cp_parser_parse_tentatively (parser);
11233 /* Look for the optional `::' operator. */
11235 = (cp_parser_global_scope_opt (parser,
11236 /*current_scope_valid_p=*/false)
11238 /* Look for the nested-name specifier. */
11240 = (cp_parser_nested_name_specifier_opt (parser,
11241 /*typename_keyword_p=*/false,
11242 /*check_dependency_p=*/true,
11244 /*is_declaration=*/false)
11246 token = cp_lexer_peek_token (parser->lexer);
11247 /* If we have seen a nested-name-specifier, and the next token
11248 is `template', then we are using the template-id production. */
11250 && cp_parser_optional_template_keyword (parser))
11252 /* Look for the template-id. */
11253 type = cp_parser_template_id (parser,
11254 /*template_keyword_p=*/true,
11255 /*check_dependency_p=*/true,
11256 /*is_declaration=*/false);
11257 /* If the template-id did not name a type, we are out of
11259 if (TREE_CODE (type) != TYPE_DECL)
11261 cp_parser_error (parser, "expected template-id for type");
11265 /* Otherwise, look for a type-name. */
11267 type = cp_parser_type_name (parser);
11268 /* Keep track of all name-lookups performed in class scopes. */
11272 && TREE_CODE (type) == TYPE_DECL
11273 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11274 maybe_note_name_used_in_class (DECL_NAME (type), type);
11275 /* If it didn't work out, we don't have a TYPE. */
11276 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11277 && !cp_parser_parse_definitely (parser))
11279 if (type && decl_specs)
11280 cp_parser_set_decl_spec_type (decl_specs, type,
11282 /*user_defined=*/true);
11285 /* If we didn't get a type-name, issue an error message. */
11286 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11288 cp_parser_error (parser, "expected type-name");
11289 return error_mark_node;
11292 /* There is no valid C++ program where a non-template type is
11293 followed by a "<". That usually indicates that the user thought
11294 that the type was a template. */
11295 if (type && type != error_mark_node)
11297 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11298 If it is, then the '<'...'>' enclose protocol names rather than
11299 template arguments, and so everything is fine. */
11300 if (c_dialect_objc ()
11301 && (objc_is_id (type) || objc_is_class_name (type)))
11303 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11304 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11306 /* Clobber the "unqualified" type previously entered into
11307 DECL_SPECS with the new, improved protocol-qualified version. */
11309 decl_specs->type = qual_type;
11314 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11321 /* Parse a type-name.
11334 Returns a TYPE_DECL for the type. */
11337 cp_parser_type_name (cp_parser* parser)
11341 /* We can't know yet whether it is a class-name or not. */
11342 cp_parser_parse_tentatively (parser);
11343 /* Try a class-name. */
11344 type_decl = cp_parser_class_name (parser,
11345 /*typename_keyword_p=*/false,
11346 /*template_keyword_p=*/false,
11348 /*check_dependency_p=*/true,
11349 /*class_head_p=*/false,
11350 /*is_declaration=*/false);
11351 /* If it's not a class-name, keep looking. */
11352 if (!cp_parser_parse_definitely (parser))
11354 /* It must be a typedef-name or an enum-name. */
11355 return cp_parser_nonclass_name (parser);
11361 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11369 Returns a TYPE_DECL for the type. */
11372 cp_parser_nonclass_name (cp_parser* parser)
11377 cp_token *token = cp_lexer_peek_token (parser->lexer);
11378 identifier = cp_parser_identifier (parser);
11379 if (identifier == error_mark_node)
11380 return error_mark_node;
11382 /* Look up the type-name. */
11383 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11385 if (TREE_CODE (type_decl) != TYPE_DECL
11386 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11388 /* See if this is an Objective-C type. */
11389 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11390 tree type = objc_get_protocol_qualified_type (identifier, protos);
11392 type_decl = TYPE_NAME (type);
11395 /* Issue an error if we did not find a type-name. */
11396 if (TREE_CODE (type_decl) != TYPE_DECL)
11398 if (!cp_parser_simulate_error (parser))
11399 cp_parser_name_lookup_error (parser, identifier, type_decl,
11400 "is not a type", token->location);
11401 return error_mark_node;
11403 /* Remember that the name was used in the definition of the
11404 current class so that we can check later to see if the
11405 meaning would have been different after the class was
11406 entirely defined. */
11407 else if (type_decl != error_mark_node
11409 maybe_note_name_used_in_class (identifier, type_decl);
11414 /* Parse an elaborated-type-specifier. Note that the grammar given
11415 here incorporates the resolution to DR68.
11417 elaborated-type-specifier:
11418 class-key :: [opt] nested-name-specifier [opt] identifier
11419 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11420 enum-key :: [opt] nested-name-specifier [opt] identifier
11421 typename :: [opt] nested-name-specifier identifier
11422 typename :: [opt] nested-name-specifier template [opt]
11427 elaborated-type-specifier:
11428 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11429 class-key attributes :: [opt] nested-name-specifier [opt]
11430 template [opt] template-id
11431 enum attributes :: [opt] nested-name-specifier [opt] identifier
11433 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11434 declared `friend'. If IS_DECLARATION is TRUE, then this
11435 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11436 something is being declared.
11438 Returns the TYPE specified. */
11441 cp_parser_elaborated_type_specifier (cp_parser* parser,
11443 bool is_declaration)
11445 enum tag_types tag_type;
11447 tree type = NULL_TREE;
11448 tree attributes = NULL_TREE;
11449 cp_token *token = NULL;
11451 /* See if we're looking at the `enum' keyword. */
11452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11454 /* Consume the `enum' token. */
11455 cp_lexer_consume_token (parser->lexer);
11456 /* Remember that it's an enumeration type. */
11457 tag_type = enum_type;
11458 /* Parse the optional `struct' or `class' key (for C++0x scoped
11460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11461 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11463 if (cxx_dialect == cxx98)
11464 maybe_warn_cpp0x ("scoped enums");
11466 /* Consume the `struct' or `class'. */
11467 cp_lexer_consume_token (parser->lexer);
11469 /* Parse the attributes. */
11470 attributes = cp_parser_attributes_opt (parser);
11472 /* Or, it might be `typename'. */
11473 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11476 /* Consume the `typename' token. */
11477 cp_lexer_consume_token (parser->lexer);
11478 /* Remember that it's a `typename' type. */
11479 tag_type = typename_type;
11480 /* The `typename' keyword is only allowed in templates. */
11481 if (!processing_template_decl)
11482 permerror (input_location, "using %<typename%> outside of template");
11484 /* Otherwise it must be a class-key. */
11487 tag_type = cp_parser_class_key (parser);
11488 if (tag_type == none_type)
11489 return error_mark_node;
11490 /* Parse the attributes. */
11491 attributes = cp_parser_attributes_opt (parser);
11494 /* Look for the `::' operator. */
11495 cp_parser_global_scope_opt (parser,
11496 /*current_scope_valid_p=*/false);
11497 /* Look for the nested-name-specifier. */
11498 if (tag_type == typename_type)
11500 if (!cp_parser_nested_name_specifier (parser,
11501 /*typename_keyword_p=*/true,
11502 /*check_dependency_p=*/true,
11505 return error_mark_node;
11508 /* Even though `typename' is not present, the proposed resolution
11509 to Core Issue 180 says that in `class A<T>::B', `B' should be
11510 considered a type-name, even if `A<T>' is dependent. */
11511 cp_parser_nested_name_specifier_opt (parser,
11512 /*typename_keyword_p=*/true,
11513 /*check_dependency_p=*/true,
11516 /* For everything but enumeration types, consider a template-id.
11517 For an enumeration type, consider only a plain identifier. */
11518 if (tag_type != enum_type)
11520 bool template_p = false;
11523 /* Allow the `template' keyword. */
11524 template_p = cp_parser_optional_template_keyword (parser);
11525 /* If we didn't see `template', we don't know if there's a
11526 template-id or not. */
11528 cp_parser_parse_tentatively (parser);
11529 /* Parse the template-id. */
11530 token = cp_lexer_peek_token (parser->lexer);
11531 decl = cp_parser_template_id (parser, template_p,
11532 /*check_dependency_p=*/true,
11534 /* If we didn't find a template-id, look for an ordinary
11536 if (!template_p && !cp_parser_parse_definitely (parser))
11538 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11539 in effect, then we must assume that, upon instantiation, the
11540 template will correspond to a class. */
11541 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11542 && tag_type == typename_type)
11543 type = make_typename_type (parser->scope, decl,
11545 /*complain=*/tf_error);
11547 type = TREE_TYPE (decl);
11552 token = cp_lexer_peek_token (parser->lexer);
11553 identifier = cp_parser_identifier (parser);
11555 if (identifier == error_mark_node)
11557 parser->scope = NULL_TREE;
11558 return error_mark_node;
11561 /* For a `typename', we needn't call xref_tag. */
11562 if (tag_type == typename_type
11563 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11564 return cp_parser_make_typename_type (parser, parser->scope,
11567 /* Look up a qualified name in the usual way. */
11571 tree ambiguous_decls;
11573 decl = cp_parser_lookup_name (parser, identifier,
11575 /*is_template=*/false,
11576 /*is_namespace=*/false,
11577 /*check_dependency=*/true,
11581 /* If the lookup was ambiguous, an error will already have been
11583 if (ambiguous_decls)
11584 return error_mark_node;
11586 /* If we are parsing friend declaration, DECL may be a
11587 TEMPLATE_DECL tree node here. However, we need to check
11588 whether this TEMPLATE_DECL results in valid code. Consider
11589 the following example:
11592 template <class T> class C {};
11595 template <class T> friend class N::C; // #1, valid code
11597 template <class T> class Y {
11598 friend class N::C; // #2, invalid code
11601 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11602 name lookup of `N::C'. We see that friend declaration must
11603 be template for the code to be valid. Note that
11604 processing_template_decl does not work here since it is
11605 always 1 for the above two cases. */
11607 decl = (cp_parser_maybe_treat_template_as_class
11608 (decl, /*tag_name_p=*/is_friend
11609 && parser->num_template_parameter_lists));
11611 if (TREE_CODE (decl) != TYPE_DECL)
11613 cp_parser_diagnose_invalid_type_name (parser,
11617 return error_mark_node;
11620 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11622 bool allow_template = (parser->num_template_parameter_lists
11623 || DECL_SELF_REFERENCE_P (decl));
11624 type = check_elaborated_type_specifier (tag_type, decl,
11627 if (type == error_mark_node)
11628 return error_mark_node;
11631 /* Forward declarations of nested types, such as
11636 are invalid unless all components preceding the final '::'
11637 are complete. If all enclosing types are complete, these
11638 declarations become merely pointless.
11640 Invalid forward declarations of nested types are errors
11641 caught elsewhere in parsing. Those that are pointless arrive
11644 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11645 && !is_friend && !processing_explicit_instantiation)
11646 warning (0, "declaration %qD does not declare anything", decl);
11648 type = TREE_TYPE (decl);
11652 /* An elaborated-type-specifier sometimes introduces a new type and
11653 sometimes names an existing type. Normally, the rule is that it
11654 introduces a new type only if there is not an existing type of
11655 the same name already in scope. For example, given:
11658 void f() { struct S s; }
11660 the `struct S' in the body of `f' is the same `struct S' as in
11661 the global scope; the existing definition is used. However, if
11662 there were no global declaration, this would introduce a new
11663 local class named `S'.
11665 An exception to this rule applies to the following code:
11667 namespace N { struct S; }
11669 Here, the elaborated-type-specifier names a new type
11670 unconditionally; even if there is already an `S' in the
11671 containing scope this declaration names a new type.
11672 This exception only applies if the elaborated-type-specifier
11673 forms the complete declaration:
11677 A declaration consisting solely of `class-key identifier ;' is
11678 either a redeclaration of the name in the current scope or a
11679 forward declaration of the identifier as a class name. It
11680 introduces the name into the current scope.
11682 We are in this situation precisely when the next token is a `;'.
11684 An exception to the exception is that a `friend' declaration does
11685 *not* name a new type; i.e., given:
11687 struct S { friend struct T; };
11689 `T' is not a new type in the scope of `S'.
11691 Also, `new struct S' or `sizeof (struct S)' never results in the
11692 definition of a new type; a new type can only be declared in a
11693 declaration context. */
11699 /* Friends have special name lookup rules. */
11700 ts = ts_within_enclosing_non_class;
11701 else if (is_declaration
11702 && cp_lexer_next_token_is (parser->lexer,
11704 /* This is a `class-key identifier ;' */
11710 (parser->num_template_parameter_lists
11711 && (cp_parser_next_token_starts_class_definition_p (parser)
11712 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11713 /* An unqualified name was used to reference this type, so
11714 there were no qualifying templates. */
11715 if (!cp_parser_check_template_parameters (parser,
11716 /*num_templates=*/0,
11718 return error_mark_node;
11719 type = xref_tag (tag_type, identifier, ts, template_p);
11723 if (type == error_mark_node)
11724 return error_mark_node;
11726 /* Allow attributes on forward declarations of classes. */
11729 if (TREE_CODE (type) == TYPENAME_TYPE)
11730 warning (OPT_Wattributes,
11731 "attributes ignored on uninstantiated type");
11732 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11733 && ! processing_explicit_instantiation)
11734 warning (OPT_Wattributes,
11735 "attributes ignored on template instantiation");
11736 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11737 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11739 warning (OPT_Wattributes,
11740 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11743 if (tag_type != enum_type)
11744 cp_parser_check_class_key (tag_type, type);
11746 /* A "<" cannot follow an elaborated type specifier. If that
11747 happens, the user was probably trying to form a template-id. */
11748 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11753 /* Parse an enum-specifier.
11756 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11761 enum struct [C++0x]
11764 : type-specifier-seq
11767 enum-key attributes[opt] identifier [opt] enum-base [opt]
11768 { enumerator-list [opt] }attributes[opt]
11770 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11771 if the token stream isn't an enum-specifier after all. */
11774 cp_parser_enum_specifier (cp_parser* parser)
11779 bool scoped_enum_p = false;
11780 bool has_underlying_type = false;
11781 tree underlying_type = NULL_TREE;
11783 /* Parse tentatively so that we can back up if we don't find a
11785 cp_parser_parse_tentatively (parser);
11787 /* Caller guarantees that the current token is 'enum', an identifier
11788 possibly follows, and the token after that is an opening brace.
11789 If we don't have an identifier, fabricate an anonymous name for
11790 the enumeration being defined. */
11791 cp_lexer_consume_token (parser->lexer);
11793 /* Parse the "class" or "struct", which indicates a scoped
11794 enumeration type in C++0x. */
11795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11796 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11798 if (cxx_dialect == cxx98)
11799 maybe_warn_cpp0x ("scoped enums");
11801 /* Consume the `struct' or `class' token. */
11802 cp_lexer_consume_token (parser->lexer);
11804 scoped_enum_p = true;
11807 attributes = cp_parser_attributes_opt (parser);
11809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11810 identifier = cp_parser_identifier (parser);
11812 identifier = make_anon_name ();
11814 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11815 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11817 cp_decl_specifier_seq type_specifiers;
11819 /* At this point this is surely not elaborated type specifier. */
11820 if (!cp_parser_parse_definitely (parser))
11823 if (cxx_dialect == cxx98)
11824 maybe_warn_cpp0x ("scoped enums");
11826 /* Consume the `:'. */
11827 cp_lexer_consume_token (parser->lexer);
11829 has_underlying_type = true;
11831 /* Parse the type-specifier-seq. */
11832 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11835 /* If that didn't work, stop. */
11836 if (type_specifiers.type != error_mark_node)
11838 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11839 /*initialized=*/0, NULL);
11840 if (underlying_type == error_mark_node)
11841 underlying_type = NULL_TREE;
11845 /* Look for the `{' but don't consume it yet. */
11846 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11848 cp_parser_error (parser, "expected %<{%>");
11849 if (has_underlying_type)
11853 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11856 /* Issue an error message if type-definitions are forbidden here. */
11857 if (!cp_parser_check_type_definition (parser))
11858 type = error_mark_node;
11860 /* Create the new type. We do this before consuming the opening
11861 brace so the enum will be recorded as being on the line of its
11862 tag (or the 'enum' keyword, if there is no tag). */
11863 type = start_enum (identifier, underlying_type, scoped_enum_p);
11865 /* Consume the opening brace. */
11866 cp_lexer_consume_token (parser->lexer);
11868 if (type == error_mark_node)
11870 cp_parser_skip_to_end_of_block_or_statement (parser);
11871 return error_mark_node;
11874 /* If the next token is not '}', then there are some enumerators. */
11875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11876 cp_parser_enumerator_list (parser, type);
11878 /* Consume the final '}'. */
11879 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11881 /* Look for trailing attributes to apply to this enumeration, and
11882 apply them if appropriate. */
11883 if (cp_parser_allow_gnu_extensions_p (parser))
11885 tree trailing_attr = cp_parser_attributes_opt (parser);
11886 cplus_decl_attributes (&type,
11888 (int) ATTR_FLAG_TYPE_IN_PLACE);
11891 /* Finish up the enumeration. */
11892 finish_enum (type);
11897 /* Parse an enumerator-list. The enumerators all have the indicated
11901 enumerator-definition
11902 enumerator-list , enumerator-definition */
11905 cp_parser_enumerator_list (cp_parser* parser, tree type)
11909 /* Parse an enumerator-definition. */
11910 cp_parser_enumerator_definition (parser, type);
11912 /* If the next token is not a ',', we've reached the end of
11914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11916 /* Otherwise, consume the `,' and keep going. */
11917 cp_lexer_consume_token (parser->lexer);
11918 /* If the next token is a `}', there is a trailing comma. */
11919 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11921 if (!in_system_header)
11922 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11928 /* Parse an enumerator-definition. The enumerator has the indicated
11931 enumerator-definition:
11933 enumerator = constant-expression
11939 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11944 /* Look for the identifier. */
11945 identifier = cp_parser_identifier (parser);
11946 if (identifier == error_mark_node)
11949 /* If the next token is an '=', then there is an explicit value. */
11950 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11952 /* Consume the `=' token. */
11953 cp_lexer_consume_token (parser->lexer);
11954 /* Parse the value. */
11955 value = cp_parser_constant_expression (parser,
11956 /*allow_non_constant_p=*/false,
11962 /* Create the enumerator. */
11963 build_enumerator (identifier, value, type);
11966 /* Parse a namespace-name.
11969 original-namespace-name
11972 Returns the NAMESPACE_DECL for the namespace. */
11975 cp_parser_namespace_name (cp_parser* parser)
11978 tree namespace_decl;
11980 cp_token *token = cp_lexer_peek_token (parser->lexer);
11982 /* Get the name of the namespace. */
11983 identifier = cp_parser_identifier (parser);
11984 if (identifier == error_mark_node)
11985 return error_mark_node;
11987 /* Look up the identifier in the currently active scope. Look only
11988 for namespaces, due to:
11990 [basic.lookup.udir]
11992 When looking up a namespace-name in a using-directive or alias
11993 definition, only namespace names are considered.
11997 [basic.lookup.qual]
11999 During the lookup of a name preceding the :: scope resolution
12000 operator, object, function, and enumerator names are ignored.
12002 (Note that cp_parser_qualifying_entity only calls this
12003 function if the token after the name is the scope resolution
12005 namespace_decl = cp_parser_lookup_name (parser, identifier,
12007 /*is_template=*/false,
12008 /*is_namespace=*/true,
12009 /*check_dependency=*/true,
12010 /*ambiguous_decls=*/NULL,
12012 /* If it's not a namespace, issue an error. */
12013 if (namespace_decl == error_mark_node
12014 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12016 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12017 error ("%H%qD is not a namespace-name", &token->location, identifier);
12018 cp_parser_error (parser, "expected namespace-name");
12019 namespace_decl = error_mark_node;
12022 return namespace_decl;
12025 /* Parse a namespace-definition.
12027 namespace-definition:
12028 named-namespace-definition
12029 unnamed-namespace-definition
12031 named-namespace-definition:
12032 original-namespace-definition
12033 extension-namespace-definition
12035 original-namespace-definition:
12036 namespace identifier { namespace-body }
12038 extension-namespace-definition:
12039 namespace original-namespace-name { namespace-body }
12041 unnamed-namespace-definition:
12042 namespace { namespace-body } */
12045 cp_parser_namespace_definition (cp_parser* parser)
12047 tree identifier, attribs;
12048 bool has_visibility;
12051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12054 cp_lexer_consume_token (parser->lexer);
12059 /* Look for the `namespace' keyword. */
12060 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12062 /* Get the name of the namespace. We do not attempt to distinguish
12063 between an original-namespace-definition and an
12064 extension-namespace-definition at this point. The semantic
12065 analysis routines are responsible for that. */
12066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12067 identifier = cp_parser_identifier (parser);
12069 identifier = NULL_TREE;
12071 /* Parse any specified attributes. */
12072 attribs = cp_parser_attributes_opt (parser);
12074 /* Look for the `{' to start the namespace. */
12075 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12076 /* Start the namespace. */
12077 push_namespace (identifier);
12079 /* "inline namespace" is equivalent to a stub namespace definition
12080 followed by a strong using directive. */
12083 tree name_space = current_namespace;
12084 /* Set up namespace association. */
12085 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12086 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12087 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12088 /* Import the contents of the inline namespace. */
12090 do_using_directive (name_space);
12091 push_namespace (identifier);
12094 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12096 /* Parse the body of the namespace. */
12097 cp_parser_namespace_body (parser);
12099 #ifdef HANDLE_PRAGMA_VISIBILITY
12100 if (has_visibility)
12104 /* Finish the namespace. */
12106 /* Look for the final `}'. */
12107 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12110 /* Parse a namespace-body.
12113 declaration-seq [opt] */
12116 cp_parser_namespace_body (cp_parser* parser)
12118 cp_parser_declaration_seq_opt (parser);
12121 /* Parse a namespace-alias-definition.
12123 namespace-alias-definition:
12124 namespace identifier = qualified-namespace-specifier ; */
12127 cp_parser_namespace_alias_definition (cp_parser* parser)
12130 tree namespace_specifier;
12132 cp_token *token = cp_lexer_peek_token (parser->lexer);
12134 /* Look for the `namespace' keyword. */
12135 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12136 /* Look for the identifier. */
12137 identifier = cp_parser_identifier (parser);
12138 if (identifier == error_mark_node)
12140 /* Look for the `=' token. */
12141 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12142 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12144 error ("%H%<namespace%> definition is not allowed here", &token->location);
12145 /* Skip the definition. */
12146 cp_lexer_consume_token (parser->lexer);
12147 if (cp_parser_skip_to_closing_brace (parser))
12148 cp_lexer_consume_token (parser->lexer);
12151 cp_parser_require (parser, CPP_EQ, "%<=%>");
12152 /* Look for the qualified-namespace-specifier. */
12153 namespace_specifier
12154 = cp_parser_qualified_namespace_specifier (parser);
12155 /* Look for the `;' token. */
12156 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12158 /* Register the alias in the symbol table. */
12159 do_namespace_alias (identifier, namespace_specifier);
12162 /* Parse a qualified-namespace-specifier.
12164 qualified-namespace-specifier:
12165 :: [opt] nested-name-specifier [opt] namespace-name
12167 Returns a NAMESPACE_DECL corresponding to the specified
12171 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12173 /* Look for the optional `::'. */
12174 cp_parser_global_scope_opt (parser,
12175 /*current_scope_valid_p=*/false);
12177 /* Look for the optional nested-name-specifier. */
12178 cp_parser_nested_name_specifier_opt (parser,
12179 /*typename_keyword_p=*/false,
12180 /*check_dependency_p=*/true,
12182 /*is_declaration=*/true);
12184 return cp_parser_namespace_name (parser);
12187 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12188 access declaration.
12191 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12192 using :: unqualified-id ;
12194 access-declaration:
12200 cp_parser_using_declaration (cp_parser* parser,
12201 bool access_declaration_p)
12204 bool typename_p = false;
12205 bool global_scope_p;
12210 if (access_declaration_p)
12211 cp_parser_parse_tentatively (parser);
12214 /* Look for the `using' keyword. */
12215 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12217 /* Peek at the next token. */
12218 token = cp_lexer_peek_token (parser->lexer);
12219 /* See if it's `typename'. */
12220 if (token->keyword == RID_TYPENAME)
12222 /* Remember that we've seen it. */
12224 /* Consume the `typename' token. */
12225 cp_lexer_consume_token (parser->lexer);
12229 /* Look for the optional global scope qualification. */
12231 = (cp_parser_global_scope_opt (parser,
12232 /*current_scope_valid_p=*/false)
12235 /* If we saw `typename', or didn't see `::', then there must be a
12236 nested-name-specifier present. */
12237 if (typename_p || !global_scope_p)
12238 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12239 /*check_dependency_p=*/true,
12241 /*is_declaration=*/true);
12242 /* Otherwise, we could be in either of the two productions. In that
12243 case, treat the nested-name-specifier as optional. */
12245 qscope = cp_parser_nested_name_specifier_opt (parser,
12246 /*typename_keyword_p=*/false,
12247 /*check_dependency_p=*/true,
12249 /*is_declaration=*/true);
12251 qscope = global_namespace;
12253 if (access_declaration_p && cp_parser_error_occurred (parser))
12254 /* Something has already gone wrong; there's no need to parse
12255 further. Since an error has occurred, the return value of
12256 cp_parser_parse_definitely will be false, as required. */
12257 return cp_parser_parse_definitely (parser);
12259 token = cp_lexer_peek_token (parser->lexer);
12260 /* Parse the unqualified-id. */
12261 identifier = cp_parser_unqualified_id (parser,
12262 /*template_keyword_p=*/false,
12263 /*check_dependency_p=*/true,
12264 /*declarator_p=*/true,
12265 /*optional_p=*/false);
12267 if (access_declaration_p)
12269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12270 cp_parser_simulate_error (parser);
12271 if (!cp_parser_parse_definitely (parser))
12275 /* The function we call to handle a using-declaration is different
12276 depending on what scope we are in. */
12277 if (qscope == error_mark_node || identifier == error_mark_node)
12279 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12280 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12281 /* [namespace.udecl]
12283 A using declaration shall not name a template-id. */
12284 error ("%Ha template-id may not appear in a using-declaration",
12288 if (at_class_scope_p ())
12290 /* Create the USING_DECL. */
12291 decl = do_class_using_decl (parser->scope, identifier);
12293 if (check_for_bare_parameter_packs (decl))
12296 /* Add it to the list of members in this class. */
12297 finish_member_declaration (decl);
12301 decl = cp_parser_lookup_name_simple (parser,
12304 if (decl == error_mark_node)
12305 cp_parser_name_lookup_error (parser, identifier,
12308 else if (check_for_bare_parameter_packs (decl))
12310 else if (!at_namespace_scope_p ())
12311 do_local_using_decl (decl, qscope, identifier);
12313 do_toplevel_using_decl (decl, qscope, identifier);
12317 /* Look for the final `;'. */
12318 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12323 /* Parse a using-directive.
12326 using namespace :: [opt] nested-name-specifier [opt]
12327 namespace-name ; */
12330 cp_parser_using_directive (cp_parser* parser)
12332 tree namespace_decl;
12335 /* Look for the `using' keyword. */
12336 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12337 /* And the `namespace' keyword. */
12338 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12339 /* Look for the optional `::' operator. */
12340 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12341 /* And the optional nested-name-specifier. */
12342 cp_parser_nested_name_specifier_opt (parser,
12343 /*typename_keyword_p=*/false,
12344 /*check_dependency_p=*/true,
12346 /*is_declaration=*/true);
12347 /* Get the namespace being used. */
12348 namespace_decl = cp_parser_namespace_name (parser);
12349 /* And any specified attributes. */
12350 attribs = cp_parser_attributes_opt (parser);
12351 /* Update the symbol table. */
12352 parse_using_directive (namespace_decl, attribs);
12353 /* Look for the final `;'. */
12354 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12357 /* Parse an asm-definition.
12360 asm ( string-literal ) ;
12365 asm volatile [opt] ( string-literal ) ;
12366 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12368 : asm-operand-list [opt] ) ;
12369 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12370 : asm-operand-list [opt]
12371 : asm-operand-list [opt] ) ; */
12374 cp_parser_asm_definition (cp_parser* parser)
12377 tree outputs = NULL_TREE;
12378 tree inputs = NULL_TREE;
12379 tree clobbers = NULL_TREE;
12381 bool volatile_p = false;
12382 bool extended_p = false;
12383 bool invalid_inputs_p = false;
12384 bool invalid_outputs_p = false;
12386 /* Look for the `asm' keyword. */
12387 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12388 /* See if the next token is `volatile'. */
12389 if (cp_parser_allow_gnu_extensions_p (parser)
12390 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12392 /* Remember that we saw the `volatile' keyword. */
12394 /* Consume the token. */
12395 cp_lexer_consume_token (parser->lexer);
12397 /* Look for the opening `('. */
12398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12400 /* Look for the string. */
12401 string = cp_parser_string_literal (parser, false, false);
12402 if (string == error_mark_node)
12404 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12405 /*consume_paren=*/true);
12409 /* If we're allowing GNU extensions, check for the extended assembly
12410 syntax. Unfortunately, the `:' tokens need not be separated by
12411 a space in C, and so, for compatibility, we tolerate that here
12412 too. Doing that means that we have to treat the `::' operator as
12414 if (cp_parser_allow_gnu_extensions_p (parser)
12415 && parser->in_function_body
12416 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12417 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12419 bool inputs_p = false;
12420 bool clobbers_p = false;
12422 /* The extended syntax was used. */
12425 /* Look for outputs. */
12426 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12428 /* Consume the `:'. */
12429 cp_lexer_consume_token (parser->lexer);
12430 /* Parse the output-operands. */
12431 if (cp_lexer_next_token_is_not (parser->lexer,
12433 && cp_lexer_next_token_is_not (parser->lexer,
12435 && cp_lexer_next_token_is_not (parser->lexer,
12437 outputs = cp_parser_asm_operand_list (parser);
12439 if (outputs == error_mark_node)
12440 invalid_outputs_p = true;
12442 /* If the next token is `::', there are no outputs, and the
12443 next token is the beginning of the inputs. */
12444 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12445 /* The inputs are coming next. */
12448 /* Look for inputs. */
12450 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12452 /* Consume the `:' or `::'. */
12453 cp_lexer_consume_token (parser->lexer);
12454 /* Parse the output-operands. */
12455 if (cp_lexer_next_token_is_not (parser->lexer,
12457 && cp_lexer_next_token_is_not (parser->lexer,
12459 inputs = cp_parser_asm_operand_list (parser);
12461 if (inputs == error_mark_node)
12462 invalid_inputs_p = true;
12464 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12465 /* The clobbers are coming next. */
12468 /* Look for clobbers. */
12470 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12472 /* Consume the `:' or `::'. */
12473 cp_lexer_consume_token (parser->lexer);
12474 /* Parse the clobbers. */
12475 if (cp_lexer_next_token_is_not (parser->lexer,
12477 clobbers = cp_parser_asm_clobber_list (parser);
12480 /* Look for the closing `)'. */
12481 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12482 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12483 /*consume_paren=*/true);
12484 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12486 if (!invalid_inputs_p && !invalid_outputs_p)
12488 /* Create the ASM_EXPR. */
12489 if (parser->in_function_body)
12491 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12493 /* If the extended syntax was not used, mark the ASM_EXPR. */
12496 tree temp = asm_stmt;
12497 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12498 temp = TREE_OPERAND (temp, 0);
12500 ASM_INPUT_P (temp) = 1;
12504 cgraph_add_asm_node (string);
12508 /* Declarators [gram.dcl.decl] */
12510 /* Parse an init-declarator.
12513 declarator initializer [opt]
12518 declarator asm-specification [opt] attributes [opt] initializer [opt]
12520 function-definition:
12521 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12523 decl-specifier-seq [opt] declarator function-try-block
12527 function-definition:
12528 __extension__ function-definition
12530 The DECL_SPECIFIERS apply to this declarator. Returns a
12531 representation of the entity declared. If MEMBER_P is TRUE, then
12532 this declarator appears in a class scope. The new DECL created by
12533 this declarator is returned.
12535 The CHECKS are access checks that should be performed once we know
12536 what entity is being declared (and, therefore, what classes have
12539 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12540 for a function-definition here as well. If the declarator is a
12541 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12542 be TRUE upon return. By that point, the function-definition will
12543 have been completely parsed.
12545 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12549 cp_parser_init_declarator (cp_parser* parser,
12550 cp_decl_specifier_seq *decl_specifiers,
12551 VEC (deferred_access_check,gc)* checks,
12552 bool function_definition_allowed_p,
12554 int declares_class_or_enum,
12555 bool* function_definition_p)
12557 cp_token *token = NULL, *asm_spec_start_token = NULL,
12558 *attributes_start_token = NULL;
12559 cp_declarator *declarator;
12560 tree prefix_attributes;
12562 tree asm_specification;
12564 tree decl = NULL_TREE;
12566 int is_initialized;
12567 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12568 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12570 enum cpp_ttype initialization_kind;
12571 bool is_direct_init = false;
12572 bool is_non_constant_init;
12573 int ctor_dtor_or_conv_p;
12575 tree pushed_scope = NULL;
12577 /* Gather the attributes that were provided with the
12578 decl-specifiers. */
12579 prefix_attributes = decl_specifiers->attributes;
12581 /* Assume that this is not the declarator for a function
12583 if (function_definition_p)
12584 *function_definition_p = false;
12586 /* Defer access checks while parsing the declarator; we cannot know
12587 what names are accessible until we know what is being
12589 resume_deferring_access_checks ();
12591 /* Parse the declarator. */
12592 token = cp_lexer_peek_token (parser->lexer);
12594 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12595 &ctor_dtor_or_conv_p,
12596 /*parenthesized_p=*/NULL,
12597 /*member_p=*/false);
12598 /* Gather up the deferred checks. */
12599 stop_deferring_access_checks ();
12601 /* If the DECLARATOR was erroneous, there's no need to go
12603 if (declarator == cp_error_declarator)
12604 return error_mark_node;
12606 /* Check that the number of template-parameter-lists is OK. */
12607 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12609 return error_mark_node;
12611 if (declares_class_or_enum & 2)
12612 cp_parser_check_for_definition_in_return_type (declarator,
12613 decl_specifiers->type,
12614 decl_specifiers->type_location);
12616 /* Figure out what scope the entity declared by the DECLARATOR is
12617 located in. `grokdeclarator' sometimes changes the scope, so
12618 we compute it now. */
12619 scope = get_scope_of_declarator (declarator);
12621 /* If we're allowing GNU extensions, look for an asm-specification
12623 if (cp_parser_allow_gnu_extensions_p (parser))
12625 /* Look for an asm-specification. */
12626 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12627 asm_specification = cp_parser_asm_specification_opt (parser);
12628 /* And attributes. */
12629 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12630 attributes = cp_parser_attributes_opt (parser);
12634 asm_specification = NULL_TREE;
12635 attributes = NULL_TREE;
12638 /* Peek at the next token. */
12639 token = cp_lexer_peek_token (parser->lexer);
12640 /* Check to see if the token indicates the start of a
12641 function-definition. */
12642 if (function_declarator_p (declarator)
12643 && cp_parser_token_starts_function_definition_p (token))
12645 if (!function_definition_allowed_p)
12647 /* If a function-definition should not appear here, issue an
12649 cp_parser_error (parser,
12650 "a function-definition is not allowed here");
12651 return error_mark_node;
12655 location_t func_brace_location
12656 = cp_lexer_peek_token (parser->lexer)->location;
12658 /* Neither attributes nor an asm-specification are allowed
12659 on a function-definition. */
12660 if (asm_specification)
12661 error ("%Han asm-specification is not allowed "
12662 "on a function-definition",
12663 &asm_spec_start_token->location);
12665 error ("%Hattributes are not allowed on a function-definition",
12666 &attributes_start_token->location);
12667 /* This is a function-definition. */
12668 *function_definition_p = true;
12670 /* Parse the function definition. */
12672 decl = cp_parser_save_member_function_body (parser,
12675 prefix_attributes);
12678 = (cp_parser_function_definition_from_specifiers_and_declarator
12679 (parser, decl_specifiers, prefix_attributes, declarator));
12681 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12683 /* This is where the prologue starts... */
12684 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12685 = func_brace_location;
12694 Only in function declarations for constructors, destructors, and
12695 type conversions can the decl-specifier-seq be omitted.
12697 We explicitly postpone this check past the point where we handle
12698 function-definitions because we tolerate function-definitions
12699 that are missing their return types in some modes. */
12700 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12702 cp_parser_error (parser,
12703 "expected constructor, destructor, or type conversion");
12704 return error_mark_node;
12707 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12708 if (token->type == CPP_EQ
12709 || token->type == CPP_OPEN_PAREN
12710 || token->type == CPP_OPEN_BRACE)
12712 is_initialized = SD_INITIALIZED;
12713 initialization_kind = token->type;
12715 if (token->type == CPP_EQ
12716 && function_declarator_p (declarator))
12718 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12719 if (t2->keyword == RID_DEFAULT)
12720 is_initialized = SD_DEFAULTED;
12721 else if (t2->keyword == RID_DELETE)
12722 is_initialized = SD_DELETED;
12727 /* If the init-declarator isn't initialized and isn't followed by a
12728 `,' or `;', it's not a valid init-declarator. */
12729 if (token->type != CPP_COMMA
12730 && token->type != CPP_SEMICOLON)
12732 cp_parser_error (parser, "expected initializer");
12733 return error_mark_node;
12735 is_initialized = SD_UNINITIALIZED;
12736 initialization_kind = CPP_EOF;
12739 /* Because start_decl has side-effects, we should only call it if we
12740 know we're going ahead. By this point, we know that we cannot
12741 possibly be looking at any other construct. */
12742 cp_parser_commit_to_tentative_parse (parser);
12744 /* If the decl specifiers were bad, issue an error now that we're
12745 sure this was intended to be a declarator. Then continue
12746 declaring the variable(s), as int, to try to cut down on further
12748 if (decl_specifiers->any_specifiers_p
12749 && decl_specifiers->type == error_mark_node)
12751 cp_parser_error (parser, "invalid type in declaration");
12752 decl_specifiers->type = integer_type_node;
12755 /* Check to see whether or not this declaration is a friend. */
12756 friend_p = cp_parser_friend_p (decl_specifiers);
12758 /* Enter the newly declared entry in the symbol table. If we're
12759 processing a declaration in a class-specifier, we wait until
12760 after processing the initializer. */
12763 if (parser->in_unbraced_linkage_specification_p)
12764 decl_specifiers->storage_class = sc_extern;
12765 decl = start_decl (declarator, decl_specifiers,
12766 is_initialized, attributes, prefix_attributes,
12770 /* Enter the SCOPE. That way unqualified names appearing in the
12771 initializer will be looked up in SCOPE. */
12772 pushed_scope = push_scope (scope);
12774 /* Perform deferred access control checks, now that we know in which
12775 SCOPE the declared entity resides. */
12776 if (!member_p && decl)
12778 tree saved_current_function_decl = NULL_TREE;
12780 /* If the entity being declared is a function, pretend that we
12781 are in its scope. If it is a `friend', it may have access to
12782 things that would not otherwise be accessible. */
12783 if (TREE_CODE (decl) == FUNCTION_DECL)
12785 saved_current_function_decl = current_function_decl;
12786 current_function_decl = decl;
12789 /* Perform access checks for template parameters. */
12790 cp_parser_perform_template_parameter_access_checks (checks);
12792 /* Perform the access control checks for the declarator and the
12793 decl-specifiers. */
12794 perform_deferred_access_checks ();
12796 /* Restore the saved value. */
12797 if (TREE_CODE (decl) == FUNCTION_DECL)
12798 current_function_decl = saved_current_function_decl;
12801 /* Parse the initializer. */
12802 initializer = NULL_TREE;
12803 is_direct_init = false;
12804 is_non_constant_init = true;
12805 if (is_initialized)
12807 if (function_declarator_p (declarator))
12809 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12810 if (initialization_kind == CPP_EQ)
12811 initializer = cp_parser_pure_specifier (parser);
12814 /* If the declaration was erroneous, we don't really
12815 know what the user intended, so just silently
12816 consume the initializer. */
12817 if (decl != error_mark_node)
12818 error ("%Hinitializer provided for function",
12819 &initializer_start_token->location);
12820 cp_parser_skip_to_closing_parenthesis (parser,
12821 /*recovering=*/true,
12822 /*or_comma=*/false,
12823 /*consume_paren=*/true);
12827 initializer = cp_parser_initializer (parser,
12829 &is_non_constant_init);
12832 /* The old parser allows attributes to appear after a parenthesized
12833 initializer. Mark Mitchell proposed removing this functionality
12834 on the GCC mailing lists on 2002-08-13. This parser accepts the
12835 attributes -- but ignores them. */
12836 if (cp_parser_allow_gnu_extensions_p (parser)
12837 && initialization_kind == CPP_OPEN_PAREN)
12838 if (cp_parser_attributes_opt (parser))
12839 warning (OPT_Wattributes,
12840 "attributes after parenthesized initializer ignored");
12842 /* For an in-class declaration, use `grokfield' to create the
12848 pop_scope (pushed_scope);
12849 pushed_scope = false;
12851 decl = grokfield (declarator, decl_specifiers,
12852 initializer, !is_non_constant_init,
12853 /*asmspec=*/NULL_TREE,
12854 prefix_attributes);
12855 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12856 cp_parser_save_default_args (parser, decl);
12859 /* Finish processing the declaration. But, skip friend
12861 if (!friend_p && decl && decl != error_mark_node)
12863 cp_finish_decl (decl,
12864 initializer, !is_non_constant_init,
12866 /* If the initializer is in parentheses, then this is
12867 a direct-initialization, which means that an
12868 `explicit' constructor is OK. Otherwise, an
12869 `explicit' constructor cannot be used. */
12870 ((is_direct_init || !is_initialized)
12871 ? 0 : LOOKUP_ONLYCONVERTING));
12873 else if ((cxx_dialect != cxx98) && friend_p
12874 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12875 /* Core issue #226 (C++0x only): A default template-argument
12876 shall not be specified in a friend class template
12878 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12879 /*is_partial=*/0, /*is_friend_decl=*/1);
12881 if (!friend_p && pushed_scope)
12882 pop_scope (pushed_scope);
12887 /* Parse a declarator.
12891 ptr-operator declarator
12893 abstract-declarator:
12894 ptr-operator abstract-declarator [opt]
12895 direct-abstract-declarator
12900 attributes [opt] direct-declarator
12901 attributes [opt] ptr-operator declarator
12903 abstract-declarator:
12904 attributes [opt] ptr-operator abstract-declarator [opt]
12905 attributes [opt] direct-abstract-declarator
12907 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12908 detect constructor, destructor or conversion operators. It is set
12909 to -1 if the declarator is a name, and +1 if it is a
12910 function. Otherwise it is set to zero. Usually you just want to
12911 test for >0, but internally the negative value is used.
12913 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12914 a decl-specifier-seq unless it declares a constructor, destructor,
12915 or conversion. It might seem that we could check this condition in
12916 semantic analysis, rather than parsing, but that makes it difficult
12917 to handle something like `f()'. We want to notice that there are
12918 no decl-specifiers, and therefore realize that this is an
12919 expression, not a declaration.)
12921 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12922 the declarator is a direct-declarator of the form "(...)".
12924 MEMBER_P is true iff this declarator is a member-declarator. */
12926 static cp_declarator *
12927 cp_parser_declarator (cp_parser* parser,
12928 cp_parser_declarator_kind dcl_kind,
12929 int* ctor_dtor_or_conv_p,
12930 bool* parenthesized_p,
12934 cp_declarator *declarator;
12935 enum tree_code code;
12936 cp_cv_quals cv_quals;
12938 tree attributes = NULL_TREE;
12940 /* Assume this is not a constructor, destructor, or type-conversion
12942 if (ctor_dtor_or_conv_p)
12943 *ctor_dtor_or_conv_p = 0;
12945 if (cp_parser_allow_gnu_extensions_p (parser))
12946 attributes = cp_parser_attributes_opt (parser);
12948 /* Peek at the next token. */
12949 token = cp_lexer_peek_token (parser->lexer);
12951 /* Check for the ptr-operator production. */
12952 cp_parser_parse_tentatively (parser);
12953 /* Parse the ptr-operator. */
12954 code = cp_parser_ptr_operator (parser,
12957 /* If that worked, then we have a ptr-operator. */
12958 if (cp_parser_parse_definitely (parser))
12960 /* If a ptr-operator was found, then this declarator was not
12962 if (parenthesized_p)
12963 *parenthesized_p = true;
12964 /* The dependent declarator is optional if we are parsing an
12965 abstract-declarator. */
12966 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12967 cp_parser_parse_tentatively (parser);
12969 /* Parse the dependent declarator. */
12970 declarator = cp_parser_declarator (parser, dcl_kind,
12971 /*ctor_dtor_or_conv_p=*/NULL,
12972 /*parenthesized_p=*/NULL,
12973 /*member_p=*/false);
12975 /* If we are parsing an abstract-declarator, we must handle the
12976 case where the dependent declarator is absent. */
12977 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12978 && !cp_parser_parse_definitely (parser))
12981 declarator = cp_parser_make_indirect_declarator
12982 (code, class_type, cv_quals, declarator);
12984 /* Everything else is a direct-declarator. */
12987 if (parenthesized_p)
12988 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12990 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12991 ctor_dtor_or_conv_p,
12995 if (attributes && declarator && declarator != cp_error_declarator)
12996 declarator->attributes = attributes;
13001 /* Parse a direct-declarator or direct-abstract-declarator.
13005 direct-declarator ( parameter-declaration-clause )
13006 cv-qualifier-seq [opt]
13007 exception-specification [opt]
13008 direct-declarator [ constant-expression [opt] ]
13011 direct-abstract-declarator:
13012 direct-abstract-declarator [opt]
13013 ( parameter-declaration-clause )
13014 cv-qualifier-seq [opt]
13015 exception-specification [opt]
13016 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13017 ( abstract-declarator )
13019 Returns a representation of the declarator. DCL_KIND is
13020 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13021 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13022 we are parsing a direct-declarator. It is
13023 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13024 of ambiguity we prefer an abstract declarator, as per
13025 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13026 cp_parser_declarator. */
13028 static cp_declarator *
13029 cp_parser_direct_declarator (cp_parser* parser,
13030 cp_parser_declarator_kind dcl_kind,
13031 int* ctor_dtor_or_conv_p,
13035 cp_declarator *declarator = NULL;
13036 tree scope = NULL_TREE;
13037 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13038 bool saved_in_declarator_p = parser->in_declarator_p;
13040 tree pushed_scope = NULL_TREE;
13044 /* Peek at the next token. */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 if (token->type == CPP_OPEN_PAREN)
13048 /* This is either a parameter-declaration-clause, or a
13049 parenthesized declarator. When we know we are parsing a
13050 named declarator, it must be a parenthesized declarator
13051 if FIRST is true. For instance, `(int)' is a
13052 parameter-declaration-clause, with an omitted
13053 direct-abstract-declarator. But `((*))', is a
13054 parenthesized abstract declarator. Finally, when T is a
13055 template parameter `(T)' is a
13056 parameter-declaration-clause, and not a parenthesized
13059 We first try and parse a parameter-declaration-clause,
13060 and then try a nested declarator (if FIRST is true).
13062 It is not an error for it not to be a
13063 parameter-declaration-clause, even when FIRST is
13069 The first is the declaration of a function while the
13070 second is the definition of a variable, including its
13073 Having seen only the parenthesis, we cannot know which of
13074 these two alternatives should be selected. Even more
13075 complex are examples like:
13080 The former is a function-declaration; the latter is a
13081 variable initialization.
13083 Thus again, we try a parameter-declaration-clause, and if
13084 that fails, we back out and return. */
13086 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13089 unsigned saved_num_template_parameter_lists;
13090 bool is_declarator = false;
13093 /* In a member-declarator, the only valid interpretation
13094 of a parenthesis is the start of a
13095 parameter-declaration-clause. (It is invalid to
13096 initialize a static data member with a parenthesized
13097 initializer; only the "=" form of initialization is
13100 cp_parser_parse_tentatively (parser);
13102 /* Consume the `('. */
13103 cp_lexer_consume_token (parser->lexer);
13106 /* If this is going to be an abstract declarator, we're
13107 in a declarator and we can't have default args. */
13108 parser->default_arg_ok_p = false;
13109 parser->in_declarator_p = true;
13112 /* Inside the function parameter list, surrounding
13113 template-parameter-lists do not apply. */
13114 saved_num_template_parameter_lists
13115 = parser->num_template_parameter_lists;
13116 parser->num_template_parameter_lists = 0;
13118 begin_scope (sk_function_parms, NULL_TREE);
13120 /* Parse the parameter-declaration-clause. */
13121 params = cp_parser_parameter_declaration_clause (parser);
13123 parser->num_template_parameter_lists
13124 = saved_num_template_parameter_lists;
13126 /* If all went well, parse the cv-qualifier-seq and the
13127 exception-specification. */
13128 if (member_p || cp_parser_parse_definitely (parser))
13130 cp_cv_quals cv_quals;
13131 tree exception_specification;
13134 is_declarator = true;
13136 if (ctor_dtor_or_conv_p)
13137 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13139 /* Consume the `)'. */
13140 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13142 /* Parse the cv-qualifier-seq. */
13143 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13144 /* And the exception-specification. */
13145 exception_specification
13146 = cp_parser_exception_specification_opt (parser);
13149 = cp_parser_late_return_type_opt (parser);
13151 /* Create the function-declarator. */
13152 declarator = make_call_declarator (declarator,
13155 exception_specification,
13157 /* Any subsequent parameter lists are to do with
13158 return type, so are not those of the declared
13160 parser->default_arg_ok_p = false;
13163 /* Remove the function parms from scope. */
13164 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13165 pop_binding (DECL_NAME (t), t);
13169 /* Repeat the main loop. */
13173 /* If this is the first, we can try a parenthesized
13177 bool saved_in_type_id_in_expr_p;
13179 parser->default_arg_ok_p = saved_default_arg_ok_p;
13180 parser->in_declarator_p = saved_in_declarator_p;
13182 /* Consume the `('. */
13183 cp_lexer_consume_token (parser->lexer);
13184 /* Parse the nested declarator. */
13185 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13186 parser->in_type_id_in_expr_p = true;
13188 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13189 /*parenthesized_p=*/NULL,
13191 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13193 /* Expect a `)'. */
13194 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13195 declarator = cp_error_declarator;
13196 if (declarator == cp_error_declarator)
13199 goto handle_declarator;
13201 /* Otherwise, we must be done. */
13205 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13206 && token->type == CPP_OPEN_SQUARE)
13208 /* Parse an array-declarator. */
13211 if (ctor_dtor_or_conv_p)
13212 *ctor_dtor_or_conv_p = 0;
13215 parser->default_arg_ok_p = false;
13216 parser->in_declarator_p = true;
13217 /* Consume the `['. */
13218 cp_lexer_consume_token (parser->lexer);
13219 /* Peek at the next token. */
13220 token = cp_lexer_peek_token (parser->lexer);
13221 /* If the next token is `]', then there is no
13222 constant-expression. */
13223 if (token->type != CPP_CLOSE_SQUARE)
13225 bool non_constant_p;
13228 = cp_parser_constant_expression (parser,
13229 /*allow_non_constant=*/true,
13231 if (!non_constant_p)
13232 bounds = fold_non_dependent_expr (bounds);
13233 /* Normally, the array bound must be an integral constant
13234 expression. However, as an extension, we allow VLAs
13235 in function scopes. */
13236 else if (!parser->in_function_body)
13238 error ("%Harray bound is not an integer constant",
13240 bounds = error_mark_node;
13244 bounds = NULL_TREE;
13245 /* Look for the closing `]'. */
13246 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13248 declarator = cp_error_declarator;
13252 declarator = make_array_declarator (declarator, bounds);
13254 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13256 tree qualifying_scope;
13257 tree unqualified_name;
13258 special_function_kind sfk;
13260 bool pack_expansion_p = false;
13261 cp_token *declarator_id_start_token;
13263 /* Parse a declarator-id */
13264 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13267 cp_parser_parse_tentatively (parser);
13269 /* If we see an ellipsis, we should be looking at a
13271 if (token->type == CPP_ELLIPSIS)
13273 /* Consume the `...' */
13274 cp_lexer_consume_token (parser->lexer);
13276 pack_expansion_p = true;
13280 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13282 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13283 qualifying_scope = parser->scope;
13288 if (!unqualified_name && pack_expansion_p)
13290 /* Check whether an error occurred. */
13291 okay = !cp_parser_error_occurred (parser);
13293 /* We already consumed the ellipsis to mark a
13294 parameter pack, but we have no way to report it,
13295 so abort the tentative parse. We will be exiting
13296 immediately anyway. */
13297 cp_parser_abort_tentative_parse (parser);
13300 okay = cp_parser_parse_definitely (parser);
13303 unqualified_name = error_mark_node;
13304 else if (unqualified_name
13305 && (qualifying_scope
13306 || (TREE_CODE (unqualified_name)
13307 != IDENTIFIER_NODE)))
13309 cp_parser_error (parser, "expected unqualified-id");
13310 unqualified_name = error_mark_node;
13314 if (!unqualified_name)
13316 if (unqualified_name == error_mark_node)
13318 declarator = cp_error_declarator;
13319 pack_expansion_p = false;
13320 declarator->parameter_pack_p = false;
13324 if (qualifying_scope && at_namespace_scope_p ()
13325 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13327 /* In the declaration of a member of a template class
13328 outside of the class itself, the SCOPE will sometimes
13329 be a TYPENAME_TYPE. For example, given:
13331 template <typename T>
13332 int S<T>::R::i = 3;
13334 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13335 this context, we must resolve S<T>::R to an ordinary
13336 type, rather than a typename type.
13338 The reason we normally avoid resolving TYPENAME_TYPEs
13339 is that a specialization of `S' might render
13340 `S<T>::R' not a type. However, if `S' is
13341 specialized, then this `i' will not be used, so there
13342 is no harm in resolving the types here. */
13345 /* Resolve the TYPENAME_TYPE. */
13346 type = resolve_typename_type (qualifying_scope,
13347 /*only_current_p=*/false);
13348 /* If that failed, the declarator is invalid. */
13349 if (TREE_CODE (type) == TYPENAME_TYPE)
13350 error ("%H%<%T::%E%> is not a type",
13351 &declarator_id_start_token->location,
13352 TYPE_CONTEXT (qualifying_scope),
13353 TYPE_IDENTIFIER (qualifying_scope));
13354 qualifying_scope = type;
13359 if (unqualified_name)
13363 if (qualifying_scope
13364 && CLASS_TYPE_P (qualifying_scope))
13365 class_type = qualifying_scope;
13367 class_type = current_class_type;
13369 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13371 tree name_type = TREE_TYPE (unqualified_name);
13372 if (class_type && same_type_p (name_type, class_type))
13374 if (qualifying_scope
13375 && CLASSTYPE_USE_TEMPLATE (name_type))
13377 error ("%Hinvalid use of constructor as a template",
13378 &declarator_id_start_token->location);
13379 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13380 "name the constructor in a qualified name",
13382 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13383 class_type, name_type);
13384 declarator = cp_error_declarator;
13388 unqualified_name = constructor_name (class_type);
13392 /* We do not attempt to print the declarator
13393 here because we do not have enough
13394 information about its original syntactic
13396 cp_parser_error (parser, "invalid declarator");
13397 declarator = cp_error_declarator;
13404 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13405 sfk = sfk_destructor;
13406 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13407 sfk = sfk_conversion;
13408 else if (/* There's no way to declare a constructor
13409 for an anonymous type, even if the type
13410 got a name for linkage purposes. */
13411 !TYPE_WAS_ANONYMOUS (class_type)
13412 && constructor_name_p (unqualified_name,
13415 unqualified_name = constructor_name (class_type);
13416 sfk = sfk_constructor;
13419 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13420 *ctor_dtor_or_conv_p = -1;
13423 declarator = make_id_declarator (qualifying_scope,
13426 declarator->id_loc = token->location;
13427 declarator->parameter_pack_p = pack_expansion_p;
13429 if (pack_expansion_p)
13430 maybe_warn_variadic_templates ();
13432 handle_declarator:;
13433 scope = get_scope_of_declarator (declarator);
13435 /* Any names that appear after the declarator-id for a
13436 member are looked up in the containing scope. */
13437 pushed_scope = push_scope (scope);
13438 parser->in_declarator_p = true;
13439 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13440 || (declarator && declarator->kind == cdk_id))
13441 /* Default args are only allowed on function
13443 parser->default_arg_ok_p = saved_default_arg_ok_p;
13445 parser->default_arg_ok_p = false;
13454 /* For an abstract declarator, we might wind up with nothing at this
13455 point. That's an error; the declarator is not optional. */
13457 cp_parser_error (parser, "expected declarator");
13459 /* If we entered a scope, we must exit it now. */
13461 pop_scope (pushed_scope);
13463 parser->default_arg_ok_p = saved_default_arg_ok_p;
13464 parser->in_declarator_p = saved_in_declarator_p;
13469 /* Parse a ptr-operator.
13472 * cv-qualifier-seq [opt]
13474 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13479 & cv-qualifier-seq [opt]
13481 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13482 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13483 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13484 filled in with the TYPE containing the member. *CV_QUALS is
13485 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13486 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13487 Note that the tree codes returned by this function have nothing
13488 to do with the types of trees that will be eventually be created
13489 to represent the pointer or reference type being parsed. They are
13490 just constants with suggestive names. */
13491 static enum tree_code
13492 cp_parser_ptr_operator (cp_parser* parser,
13494 cp_cv_quals *cv_quals)
13496 enum tree_code code = ERROR_MARK;
13499 /* Assume that it's not a pointer-to-member. */
13501 /* And that there are no cv-qualifiers. */
13502 *cv_quals = TYPE_UNQUALIFIED;
13504 /* Peek at the next token. */
13505 token = cp_lexer_peek_token (parser->lexer);
13507 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13508 if (token->type == CPP_MULT)
13509 code = INDIRECT_REF;
13510 else if (token->type == CPP_AND)
13512 else if ((cxx_dialect != cxx98) &&
13513 token->type == CPP_AND_AND) /* C++0x only */
13514 code = NON_LVALUE_EXPR;
13516 if (code != ERROR_MARK)
13518 /* Consume the `*', `&' or `&&'. */
13519 cp_lexer_consume_token (parser->lexer);
13521 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13522 `&', if we are allowing GNU extensions. (The only qualifier
13523 that can legally appear after `&' is `restrict', but that is
13524 enforced during semantic analysis. */
13525 if (code == INDIRECT_REF
13526 || cp_parser_allow_gnu_extensions_p (parser))
13527 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13531 /* Try the pointer-to-member case. */
13532 cp_parser_parse_tentatively (parser);
13533 /* Look for the optional `::' operator. */
13534 cp_parser_global_scope_opt (parser,
13535 /*current_scope_valid_p=*/false);
13536 /* Look for the nested-name specifier. */
13537 token = cp_lexer_peek_token (parser->lexer);
13538 cp_parser_nested_name_specifier (parser,
13539 /*typename_keyword_p=*/false,
13540 /*check_dependency_p=*/true,
13542 /*is_declaration=*/false);
13543 /* If we found it, and the next token is a `*', then we are
13544 indeed looking at a pointer-to-member operator. */
13545 if (!cp_parser_error_occurred (parser)
13546 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13548 /* Indicate that the `*' operator was used. */
13549 code = INDIRECT_REF;
13551 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13552 error ("%H%qD is a namespace", &token->location, parser->scope);
13555 /* The type of which the member is a member is given by the
13557 *type = parser->scope;
13558 /* The next name will not be qualified. */
13559 parser->scope = NULL_TREE;
13560 parser->qualifying_scope = NULL_TREE;
13561 parser->object_scope = NULL_TREE;
13562 /* Look for the optional cv-qualifier-seq. */
13563 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13566 /* If that didn't work we don't have a ptr-operator. */
13567 if (!cp_parser_parse_definitely (parser))
13568 cp_parser_error (parser, "expected ptr-operator");
13574 /* Parse an (optional) cv-qualifier-seq.
13577 cv-qualifier cv-qualifier-seq [opt]
13588 Returns a bitmask representing the cv-qualifiers. */
13591 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13593 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13598 cp_cv_quals cv_qualifier;
13600 /* Peek at the next token. */
13601 token = cp_lexer_peek_token (parser->lexer);
13602 /* See if it's a cv-qualifier. */
13603 switch (token->keyword)
13606 cv_qualifier = TYPE_QUAL_CONST;
13610 cv_qualifier = TYPE_QUAL_VOLATILE;
13614 cv_qualifier = TYPE_QUAL_RESTRICT;
13618 cv_qualifier = TYPE_UNQUALIFIED;
13625 if (cv_quals & cv_qualifier)
13627 error ("%Hduplicate cv-qualifier", &token->location);
13628 cp_lexer_purge_token (parser->lexer);
13632 cp_lexer_consume_token (parser->lexer);
13633 cv_quals |= cv_qualifier;
13640 /* Parse a late-specified return type, if any. This is not a separate
13641 non-terminal, but part of a function declarator, which looks like
13645 Returns the type indicated by the type-id. */
13648 cp_parser_late_return_type_opt (cp_parser* parser)
13652 /* Peek at the next token. */
13653 token = cp_lexer_peek_token (parser->lexer);
13654 /* A late-specified return type is indicated by an initial '->'. */
13655 if (token->type != CPP_DEREF)
13658 /* Consume the ->. */
13659 cp_lexer_consume_token (parser->lexer);
13661 return cp_parser_type_id (parser);
13664 /* Parse a declarator-id.
13668 :: [opt] nested-name-specifier [opt] type-name
13670 In the `id-expression' case, the value returned is as for
13671 cp_parser_id_expression if the id-expression was an unqualified-id.
13672 If the id-expression was a qualified-id, then a SCOPE_REF is
13673 returned. The first operand is the scope (either a NAMESPACE_DECL
13674 or TREE_TYPE), but the second is still just a representation of an
13678 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13681 /* The expression must be an id-expression. Assume that qualified
13682 names are the names of types so that:
13685 int S<T>::R::i = 3;
13687 will work; we must treat `S<T>::R' as the name of a type.
13688 Similarly, assume that qualified names are templates, where
13692 int S<T>::R<T>::i = 3;
13695 id = cp_parser_id_expression (parser,
13696 /*template_keyword_p=*/false,
13697 /*check_dependency_p=*/false,
13698 /*template_p=*/NULL,
13699 /*declarator_p=*/true,
13701 if (id && BASELINK_P (id))
13702 id = BASELINK_FUNCTIONS (id);
13706 /* Parse a type-id.
13709 type-specifier-seq abstract-declarator [opt]
13711 Returns the TYPE specified. */
13714 cp_parser_type_id (cp_parser* parser)
13716 cp_decl_specifier_seq type_specifier_seq;
13717 cp_declarator *abstract_declarator;
13719 /* Parse the type-specifier-seq. */
13720 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13721 &type_specifier_seq);
13722 if (type_specifier_seq.type == error_mark_node)
13723 return error_mark_node;
13725 /* There might or might not be an abstract declarator. */
13726 cp_parser_parse_tentatively (parser);
13727 /* Look for the declarator. */
13728 abstract_declarator
13729 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13730 /*parenthesized_p=*/NULL,
13731 /*member_p=*/false);
13732 /* Check to see if there really was a declarator. */
13733 if (!cp_parser_parse_definitely (parser))
13734 abstract_declarator = NULL;
13736 if (type_specifier_seq.type
13737 && type_uses_auto (type_specifier_seq.type))
13739 error ("invalid use of %<auto%>");
13740 return error_mark_node;
13743 return groktypename (&type_specifier_seq, abstract_declarator);
13746 /* Parse a type-specifier-seq.
13748 type-specifier-seq:
13749 type-specifier type-specifier-seq [opt]
13753 type-specifier-seq:
13754 attributes type-specifier-seq [opt]
13756 If IS_CONDITION is true, we are at the start of a "condition",
13757 e.g., we've just seen "if (".
13759 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13762 cp_parser_type_specifier_seq (cp_parser* parser,
13764 cp_decl_specifier_seq *type_specifier_seq)
13766 bool seen_type_specifier = false;
13767 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13768 cp_token *start_token = NULL;
13770 /* Clear the TYPE_SPECIFIER_SEQ. */
13771 clear_decl_specs (type_specifier_seq);
13773 /* Parse the type-specifiers and attributes. */
13776 tree type_specifier;
13777 bool is_cv_qualifier;
13779 /* Check for attributes first. */
13780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13782 type_specifier_seq->attributes =
13783 chainon (type_specifier_seq->attributes,
13784 cp_parser_attributes_opt (parser));
13788 /* record the token of the beginning of the type specifier seq,
13789 for error reporting purposes*/
13791 start_token = cp_lexer_peek_token (parser->lexer);
13793 /* Look for the type-specifier. */
13794 type_specifier = cp_parser_type_specifier (parser,
13796 type_specifier_seq,
13797 /*is_declaration=*/false,
13800 if (!type_specifier)
13802 /* If the first type-specifier could not be found, this is not a
13803 type-specifier-seq at all. */
13804 if (!seen_type_specifier)
13806 cp_parser_error (parser, "expected type-specifier");
13807 type_specifier_seq->type = error_mark_node;
13810 /* If subsequent type-specifiers could not be found, the
13811 type-specifier-seq is complete. */
13815 seen_type_specifier = true;
13816 /* The standard says that a condition can be:
13818 type-specifier-seq declarator = assignment-expression
13825 we should treat the "S" as a declarator, not as a
13826 type-specifier. The standard doesn't say that explicitly for
13827 type-specifier-seq, but it does say that for
13828 decl-specifier-seq in an ordinary declaration. Perhaps it
13829 would be clearer just to allow a decl-specifier-seq here, and
13830 then add a semantic restriction that if any decl-specifiers
13831 that are not type-specifiers appear, the program is invalid. */
13832 if (is_condition && !is_cv_qualifier)
13833 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13836 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13839 /* Parse a parameter-declaration-clause.
13841 parameter-declaration-clause:
13842 parameter-declaration-list [opt] ... [opt]
13843 parameter-declaration-list , ...
13845 Returns a representation for the parameter declarations. A return
13846 value of NULL indicates a parameter-declaration-clause consisting
13847 only of an ellipsis. */
13850 cp_parser_parameter_declaration_clause (cp_parser* parser)
13857 /* Peek at the next token. */
13858 token = cp_lexer_peek_token (parser->lexer);
13859 /* Check for trivial parameter-declaration-clauses. */
13860 if (token->type == CPP_ELLIPSIS)
13862 /* Consume the `...' token. */
13863 cp_lexer_consume_token (parser->lexer);
13866 else if (token->type == CPP_CLOSE_PAREN)
13867 /* There are no parameters. */
13869 #ifndef NO_IMPLICIT_EXTERN_C
13870 if (in_system_header && current_class_type == NULL
13871 && current_lang_name == lang_name_c)
13875 return void_list_node;
13877 /* Check for `(void)', too, which is a special case. */
13878 else if (token->keyword == RID_VOID
13879 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13880 == CPP_CLOSE_PAREN))
13882 /* Consume the `void' token. */
13883 cp_lexer_consume_token (parser->lexer);
13884 /* There are no parameters. */
13885 return void_list_node;
13888 /* Parse the parameter-declaration-list. */
13889 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13890 /* If a parse error occurred while parsing the
13891 parameter-declaration-list, then the entire
13892 parameter-declaration-clause is erroneous. */
13896 /* Peek at the next token. */
13897 token = cp_lexer_peek_token (parser->lexer);
13898 /* If it's a `,', the clause should terminate with an ellipsis. */
13899 if (token->type == CPP_COMMA)
13901 /* Consume the `,'. */
13902 cp_lexer_consume_token (parser->lexer);
13903 /* Expect an ellipsis. */
13905 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13907 /* It might also be `...' if the optional trailing `,' was
13909 else if (token->type == CPP_ELLIPSIS)
13911 /* Consume the `...' token. */
13912 cp_lexer_consume_token (parser->lexer);
13913 /* And remember that we saw it. */
13917 ellipsis_p = false;
13919 /* Finish the parameter list. */
13921 parameters = chainon (parameters, void_list_node);
13926 /* Parse a parameter-declaration-list.
13928 parameter-declaration-list:
13929 parameter-declaration
13930 parameter-declaration-list , parameter-declaration
13932 Returns a representation of the parameter-declaration-list, as for
13933 cp_parser_parameter_declaration_clause. However, the
13934 `void_list_node' is never appended to the list. Upon return,
13935 *IS_ERROR will be true iff an error occurred. */
13938 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13940 tree parameters = NULL_TREE;
13941 tree *tail = ¶meters;
13942 bool saved_in_unbraced_linkage_specification_p;
13944 /* Assume all will go well. */
13946 /* The special considerations that apply to a function within an
13947 unbraced linkage specifications do not apply to the parameters
13948 to the function. */
13949 saved_in_unbraced_linkage_specification_p
13950 = parser->in_unbraced_linkage_specification_p;
13951 parser->in_unbraced_linkage_specification_p = false;
13953 /* Look for more parameters. */
13956 cp_parameter_declarator *parameter;
13957 tree decl = error_mark_node;
13958 bool parenthesized_p;
13959 /* Parse the parameter. */
13961 = cp_parser_parameter_declaration (parser,
13962 /*template_parm_p=*/false,
13965 /* We don't know yet if the enclosing context is deprecated, so wait
13966 and warn in grokparms if appropriate. */
13967 deprecated_state = DEPRECATED_SUPPRESS;
13970 decl = grokdeclarator (parameter->declarator,
13971 ¶meter->decl_specifiers,
13973 parameter->default_argument != NULL_TREE,
13974 ¶meter->decl_specifiers.attributes);
13976 deprecated_state = DEPRECATED_NORMAL;
13978 /* If a parse error occurred parsing the parameter declaration,
13979 then the entire parameter-declaration-list is erroneous. */
13980 if (decl == error_mark_node)
13983 parameters = error_mark_node;
13987 if (parameter->decl_specifiers.attributes)
13988 cplus_decl_attributes (&decl,
13989 parameter->decl_specifiers.attributes,
13991 if (DECL_NAME (decl))
13992 decl = pushdecl (decl);
13994 /* Add the new parameter to the list. */
13995 *tail = build_tree_list (parameter->default_argument, decl);
13996 tail = &TREE_CHAIN (*tail);
13998 /* Peek at the next token. */
13999 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14000 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14001 /* These are for Objective-C++ */
14002 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14003 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14004 /* The parameter-declaration-list is complete. */
14006 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14010 /* Peek at the next token. */
14011 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14012 /* If it's an ellipsis, then the list is complete. */
14013 if (token->type == CPP_ELLIPSIS)
14015 /* Otherwise, there must be more parameters. Consume the
14017 cp_lexer_consume_token (parser->lexer);
14018 /* When parsing something like:
14020 int i(float f, double d)
14022 we can tell after seeing the declaration for "f" that we
14023 are not looking at an initialization of a variable "i",
14024 but rather at the declaration of a function "i".
14026 Due to the fact that the parsing of template arguments
14027 (as specified to a template-id) requires backtracking we
14028 cannot use this technique when inside a template argument
14030 if (!parser->in_template_argument_list_p
14031 && !parser->in_type_id_in_expr_p
14032 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14033 /* However, a parameter-declaration of the form
14034 "foat(f)" (which is a valid declaration of a
14035 parameter "f") can also be interpreted as an
14036 expression (the conversion of "f" to "float"). */
14037 && !parenthesized_p)
14038 cp_parser_commit_to_tentative_parse (parser);
14042 cp_parser_error (parser, "expected %<,%> or %<...%>");
14043 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14044 cp_parser_skip_to_closing_parenthesis (parser,
14045 /*recovering=*/true,
14046 /*or_comma=*/false,
14047 /*consume_paren=*/false);
14052 parser->in_unbraced_linkage_specification_p
14053 = saved_in_unbraced_linkage_specification_p;
14058 /* Parse a parameter declaration.
14060 parameter-declaration:
14061 decl-specifier-seq ... [opt] declarator
14062 decl-specifier-seq declarator = assignment-expression
14063 decl-specifier-seq ... [opt] abstract-declarator [opt]
14064 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14066 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14067 declares a template parameter. (In that case, a non-nested `>'
14068 token encountered during the parsing of the assignment-expression
14069 is not interpreted as a greater-than operator.)
14071 Returns a representation of the parameter, or NULL if an error
14072 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14073 true iff the declarator is of the form "(p)". */
14075 static cp_parameter_declarator *
14076 cp_parser_parameter_declaration (cp_parser *parser,
14077 bool template_parm_p,
14078 bool *parenthesized_p)
14080 int declares_class_or_enum;
14081 bool greater_than_is_operator_p;
14082 cp_decl_specifier_seq decl_specifiers;
14083 cp_declarator *declarator;
14084 tree default_argument;
14085 cp_token *token = NULL, *declarator_token_start = NULL;
14086 const char *saved_message;
14088 /* In a template parameter, `>' is not an operator.
14092 When parsing a default template-argument for a non-type
14093 template-parameter, the first non-nested `>' is taken as the end
14094 of the template parameter-list rather than a greater-than
14096 greater_than_is_operator_p = !template_parm_p;
14098 /* Type definitions may not appear in parameter types. */
14099 saved_message = parser->type_definition_forbidden_message;
14100 parser->type_definition_forbidden_message
14101 = "types may not be defined in parameter types";
14103 /* Parse the declaration-specifiers. */
14104 cp_parser_decl_specifier_seq (parser,
14105 CP_PARSER_FLAGS_NONE,
14107 &declares_class_or_enum);
14108 /* If an error occurred, there's no reason to attempt to parse the
14109 rest of the declaration. */
14110 if (cp_parser_error_occurred (parser))
14112 parser->type_definition_forbidden_message = saved_message;
14116 /* Peek at the next token. */
14117 token = cp_lexer_peek_token (parser->lexer);
14119 /* If the next token is a `)', `,', `=', `>', or `...', then there
14120 is no declarator. However, when variadic templates are enabled,
14121 there may be a declarator following `...'. */
14122 if (token->type == CPP_CLOSE_PAREN
14123 || token->type == CPP_COMMA
14124 || token->type == CPP_EQ
14125 || token->type == CPP_GREATER)
14128 if (parenthesized_p)
14129 *parenthesized_p = false;
14131 /* Otherwise, there should be a declarator. */
14134 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14135 parser->default_arg_ok_p = false;
14137 /* After seeing a decl-specifier-seq, if the next token is not a
14138 "(", there is no possibility that the code is a valid
14139 expression. Therefore, if parsing tentatively, we commit at
14141 if (!parser->in_template_argument_list_p
14142 /* In an expression context, having seen:
14146 we cannot be sure whether we are looking at a
14147 function-type (taking a "char" as a parameter) or a cast
14148 of some object of type "char" to "int". */
14149 && !parser->in_type_id_in_expr_p
14150 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14151 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14152 cp_parser_commit_to_tentative_parse (parser);
14153 /* Parse the declarator. */
14154 declarator_token_start = token;
14155 declarator = cp_parser_declarator (parser,
14156 CP_PARSER_DECLARATOR_EITHER,
14157 /*ctor_dtor_or_conv_p=*/NULL,
14159 /*member_p=*/false);
14160 parser->default_arg_ok_p = saved_default_arg_ok_p;
14161 /* After the declarator, allow more attributes. */
14162 decl_specifiers.attributes
14163 = chainon (decl_specifiers.attributes,
14164 cp_parser_attributes_opt (parser));
14167 /* If the next token is an ellipsis, and we have not seen a
14168 declarator name, and the type of the declarator contains parameter
14169 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14170 a parameter pack expansion expression. Otherwise, leave the
14171 ellipsis for a C-style variadic function. */
14172 token = cp_lexer_peek_token (parser->lexer);
14173 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14175 tree type = decl_specifiers.type;
14177 if (type && DECL_P (type))
14178 type = TREE_TYPE (type);
14181 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14182 && declarator_can_be_parameter_pack (declarator)
14183 && (!declarator || !declarator->parameter_pack_p)
14184 && uses_parameter_packs (type))
14186 /* Consume the `...'. */
14187 cp_lexer_consume_token (parser->lexer);
14188 maybe_warn_variadic_templates ();
14190 /* Build a pack expansion type */
14192 declarator->parameter_pack_p = true;
14194 decl_specifiers.type = make_pack_expansion (type);
14198 /* The restriction on defining new types applies only to the type
14199 of the parameter, not to the default argument. */
14200 parser->type_definition_forbidden_message = saved_message;
14202 /* If the next token is `=', then process a default argument. */
14203 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14205 /* Consume the `='. */
14206 cp_lexer_consume_token (parser->lexer);
14208 /* If we are defining a class, then the tokens that make up the
14209 default argument must be saved and processed later. */
14210 if (!template_parm_p && at_class_scope_p ()
14211 && TYPE_BEING_DEFINED (current_class_type))
14213 unsigned depth = 0;
14214 int maybe_template_id = 0;
14215 cp_token *first_token;
14218 /* Add tokens until we have processed the entire default
14219 argument. We add the range [first_token, token). */
14220 first_token = cp_lexer_peek_token (parser->lexer);
14225 /* Peek at the next token. */
14226 token = cp_lexer_peek_token (parser->lexer);
14227 /* What we do depends on what token we have. */
14228 switch (token->type)
14230 /* In valid code, a default argument must be
14231 immediately followed by a `,' `)', or `...'. */
14233 if (depth == 0 && maybe_template_id)
14235 /* If we've seen a '<', we might be in a
14236 template-argument-list. Until Core issue 325 is
14237 resolved, we don't know how this situation ought
14238 to be handled, so try to DTRT. We check whether
14239 what comes after the comma is a valid parameter
14240 declaration list. If it is, then the comma ends
14241 the default argument; otherwise the default
14242 argument continues. */
14243 bool error = false;
14245 /* Set ITALP so cp_parser_parameter_declaration_list
14246 doesn't decide to commit to this parse. */
14247 bool saved_italp = parser->in_template_argument_list_p;
14248 parser->in_template_argument_list_p = true;
14250 cp_parser_parse_tentatively (parser);
14251 cp_lexer_consume_token (parser->lexer);
14252 cp_parser_parameter_declaration_list (parser, &error);
14253 if (!cp_parser_error_occurred (parser) && !error)
14255 cp_parser_abort_tentative_parse (parser);
14257 parser->in_template_argument_list_p = saved_italp;
14260 case CPP_CLOSE_PAREN:
14262 /* If we run into a non-nested `;', `}', or `]',
14263 then the code is invalid -- but the default
14264 argument is certainly over. */
14265 case CPP_SEMICOLON:
14266 case CPP_CLOSE_BRACE:
14267 case CPP_CLOSE_SQUARE:
14270 /* Update DEPTH, if necessary. */
14271 else if (token->type == CPP_CLOSE_PAREN
14272 || token->type == CPP_CLOSE_BRACE
14273 || token->type == CPP_CLOSE_SQUARE)
14277 case CPP_OPEN_PAREN:
14278 case CPP_OPEN_SQUARE:
14279 case CPP_OPEN_BRACE:
14285 /* This might be the comparison operator, or it might
14286 start a template argument list. */
14287 ++maybe_template_id;
14291 if (cxx_dialect == cxx98)
14293 /* Fall through for C++0x, which treats the `>>'
14294 operator like two `>' tokens in certain
14300 /* This might be an operator, or it might close a
14301 template argument list. But if a previous '<'
14302 started a template argument list, this will have
14303 closed it, so we can't be in one anymore. */
14304 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14305 if (maybe_template_id < 0)
14306 maybe_template_id = 0;
14310 /* If we run out of tokens, issue an error message. */
14312 case CPP_PRAGMA_EOL:
14313 error ("%Hfile ends in default argument", &token->location);
14319 /* In these cases, we should look for template-ids.
14320 For example, if the default argument is
14321 `X<int, double>()', we need to do name lookup to
14322 figure out whether or not `X' is a template; if
14323 so, the `,' does not end the default argument.
14325 That is not yet done. */
14332 /* If we've reached the end, stop. */
14336 /* Add the token to the token block. */
14337 token = cp_lexer_consume_token (parser->lexer);
14340 /* Create a DEFAULT_ARG to represent the unparsed default
14342 default_argument = make_node (DEFAULT_ARG);
14343 DEFARG_TOKENS (default_argument)
14344 = cp_token_cache_new (first_token, token);
14345 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14347 /* Outside of a class definition, we can just parse the
14348 assignment-expression. */
14351 token = cp_lexer_peek_token (parser->lexer);
14353 = cp_parser_default_argument (parser, template_parm_p);
14356 if (!parser->default_arg_ok_p)
14358 if (flag_permissive)
14359 warning (0, "deprecated use of default argument for parameter of non-function");
14362 error ("%Hdefault arguments are only "
14363 "permitted for function parameters",
14365 default_argument = NULL_TREE;
14368 else if ((declarator && declarator->parameter_pack_p)
14369 || (decl_specifiers.type
14370 && PACK_EXPANSION_P (decl_specifiers.type)))
14372 const char* kind = template_parm_p? "template " : "";
14374 /* Find the name of the parameter pack. */
14375 cp_declarator *id_declarator = declarator;
14376 while (id_declarator && id_declarator->kind != cdk_id)
14377 id_declarator = id_declarator->declarator;
14379 if (id_declarator && id_declarator->kind == cdk_id)
14380 error ("%H%sparameter pack %qD cannot have a default argument",
14381 &declarator_token_start->location,
14382 kind, id_declarator->u.id.unqualified_name);
14384 error ("%H%sparameter pack cannot have a default argument",
14385 &declarator_token_start->location, kind);
14387 default_argument = NULL_TREE;
14391 default_argument = NULL_TREE;
14393 return make_parameter_declarator (&decl_specifiers,
14398 /* Parse a default argument and return it.
14400 TEMPLATE_PARM_P is true if this is a default argument for a
14401 non-type template parameter. */
14403 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14405 tree default_argument = NULL_TREE;
14406 bool saved_greater_than_is_operator_p;
14407 bool saved_local_variables_forbidden_p;
14409 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14411 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14412 parser->greater_than_is_operator_p = !template_parm_p;
14413 /* Local variable names (and the `this' keyword) may not
14414 appear in a default argument. */
14415 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14416 parser->local_variables_forbidden_p = true;
14417 /* The default argument expression may cause implicitly
14418 defined member functions to be synthesized, which will
14419 result in garbage collection. We must treat this
14420 situation as if we were within the body of function so as
14421 to avoid collecting live data on the stack. */
14423 /* Parse the assignment-expression. */
14424 if (template_parm_p)
14425 push_deferring_access_checks (dk_no_deferred);
14427 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14428 if (template_parm_p)
14429 pop_deferring_access_checks ();
14430 /* Restore saved state. */
14432 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14433 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14435 return default_argument;
14438 /* Parse a function-body.
14441 compound_statement */
14444 cp_parser_function_body (cp_parser *parser)
14446 cp_parser_compound_statement (parser, NULL, false);
14449 /* Parse a ctor-initializer-opt followed by a function-body. Return
14450 true if a ctor-initializer was present. */
14453 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14456 bool ctor_initializer_p;
14458 /* Begin the function body. */
14459 body = begin_function_body ();
14460 /* Parse the optional ctor-initializer. */
14461 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14462 /* Parse the function-body. */
14463 cp_parser_function_body (parser);
14464 /* Finish the function body. */
14465 finish_function_body (body);
14467 return ctor_initializer_p;
14470 /* Parse an initializer.
14473 = initializer-clause
14474 ( expression-list )
14476 Returns an expression representing the initializer. If no
14477 initializer is present, NULL_TREE is returned.
14479 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14480 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14481 set to TRUE if there is no initializer present. If there is an
14482 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14483 is set to true; otherwise it is set to false. */
14486 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14487 bool* non_constant_p)
14492 /* Peek at the next token. */
14493 token = cp_lexer_peek_token (parser->lexer);
14495 /* Let our caller know whether or not this initializer was
14497 *is_direct_init = (token->type != CPP_EQ);
14498 /* Assume that the initializer is constant. */
14499 *non_constant_p = false;
14501 if (token->type == CPP_EQ)
14503 /* Consume the `='. */
14504 cp_lexer_consume_token (parser->lexer);
14505 /* Parse the initializer-clause. */
14506 init = cp_parser_initializer_clause (parser, non_constant_p);
14508 else if (token->type == CPP_OPEN_PAREN)
14509 init = cp_parser_parenthesized_expression_list (parser, false,
14511 /*allow_expansion_p=*/true,
14513 else if (token->type == CPP_OPEN_BRACE)
14515 maybe_warn_cpp0x ("extended initializer lists");
14516 init = cp_parser_braced_list (parser, non_constant_p);
14517 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14521 /* Anything else is an error. */
14522 cp_parser_error (parser, "expected initializer");
14523 init = error_mark_node;
14529 /* Parse an initializer-clause.
14531 initializer-clause:
14532 assignment-expression
14535 Returns an expression representing the initializer.
14537 If the `assignment-expression' production is used the value
14538 returned is simply a representation for the expression.
14540 Otherwise, calls cp_parser_braced_list. */
14543 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14547 /* Assume the expression is constant. */
14548 *non_constant_p = false;
14550 /* If it is not a `{', then we are looking at an
14551 assignment-expression. */
14552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14555 = cp_parser_constant_expression (parser,
14556 /*allow_non_constant_p=*/true,
14558 if (!*non_constant_p)
14559 initializer = fold_non_dependent_expr (initializer);
14562 initializer = cp_parser_braced_list (parser, non_constant_p);
14564 return initializer;
14567 /* Parse a brace-enclosed initializer list.
14570 { initializer-list , [opt] }
14573 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14574 the elements of the initializer-list (or NULL, if the last
14575 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14576 NULL_TREE. There is no way to detect whether or not the optional
14577 trailing `,' was provided. NON_CONSTANT_P is as for
14578 cp_parser_initializer. */
14581 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14585 /* Consume the `{' token. */
14586 cp_lexer_consume_token (parser->lexer);
14587 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14588 initializer = make_node (CONSTRUCTOR);
14589 /* If it's not a `}', then there is a non-trivial initializer. */
14590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14592 /* Parse the initializer list. */
14593 CONSTRUCTOR_ELTS (initializer)
14594 = cp_parser_initializer_list (parser, non_constant_p);
14595 /* A trailing `,' token is allowed. */
14596 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14597 cp_lexer_consume_token (parser->lexer);
14599 /* Now, there should be a trailing `}'. */
14600 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14601 TREE_TYPE (initializer) = init_list_type_node;
14602 return initializer;
14605 /* Parse an initializer-list.
14608 initializer-clause ... [opt]
14609 initializer-list , initializer-clause ... [opt]
14614 identifier : initializer-clause
14615 initializer-list, identifier : initializer-clause
14617 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14618 for the initializer. If the INDEX of the elt is non-NULL, it is the
14619 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14620 as for cp_parser_initializer. */
14622 static VEC(constructor_elt,gc) *
14623 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14625 VEC(constructor_elt,gc) *v = NULL;
14627 /* Assume all of the expressions are constant. */
14628 *non_constant_p = false;
14630 /* Parse the rest of the list. */
14636 bool clause_non_constant_p;
14638 /* If the next token is an identifier and the following one is a
14639 colon, we are looking at the GNU designated-initializer
14641 if (cp_parser_allow_gnu_extensions_p (parser)
14642 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14643 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14645 /* Warn the user that they are using an extension. */
14646 pedwarn (input_location, OPT_pedantic,
14647 "ISO C++ does not allow designated initializers");
14648 /* Consume the identifier. */
14649 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14650 /* Consume the `:'. */
14651 cp_lexer_consume_token (parser->lexer);
14654 identifier = NULL_TREE;
14656 /* Parse the initializer. */
14657 initializer = cp_parser_initializer_clause (parser,
14658 &clause_non_constant_p);
14659 /* If any clause is non-constant, so is the entire initializer. */
14660 if (clause_non_constant_p)
14661 *non_constant_p = true;
14663 /* If we have an ellipsis, this is an initializer pack
14665 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14667 /* Consume the `...'. */
14668 cp_lexer_consume_token (parser->lexer);
14670 /* Turn the initializer into an initializer expansion. */
14671 initializer = make_pack_expansion (initializer);
14674 /* Add it to the vector. */
14675 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14677 /* If the next token is not a comma, we have reached the end of
14679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14682 /* Peek at the next token. */
14683 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14684 /* If the next token is a `}', then we're still done. An
14685 initializer-clause can have a trailing `,' after the
14686 initializer-list and before the closing `}'. */
14687 if (token->type == CPP_CLOSE_BRACE)
14690 /* Consume the `,' token. */
14691 cp_lexer_consume_token (parser->lexer);
14697 /* Classes [gram.class] */
14699 /* Parse a class-name.
14705 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14706 to indicate that names looked up in dependent types should be
14707 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14708 keyword has been used to indicate that the name that appears next
14709 is a template. TAG_TYPE indicates the explicit tag given before
14710 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14711 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14712 is the class being defined in a class-head.
14714 Returns the TYPE_DECL representing the class. */
14717 cp_parser_class_name (cp_parser *parser,
14718 bool typename_keyword_p,
14719 bool template_keyword_p,
14720 enum tag_types tag_type,
14721 bool check_dependency_p,
14723 bool is_declaration)
14729 tree identifier = NULL_TREE;
14731 /* All class-names start with an identifier. */
14732 token = cp_lexer_peek_token (parser->lexer);
14733 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14735 cp_parser_error (parser, "expected class-name");
14736 return error_mark_node;
14739 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14740 to a template-id, so we save it here. */
14741 scope = parser->scope;
14742 if (scope == error_mark_node)
14743 return error_mark_node;
14745 /* Any name names a type if we're following the `typename' keyword
14746 in a qualified name where the enclosing scope is type-dependent. */
14747 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14748 && dependent_type_p (scope));
14749 /* Handle the common case (an identifier, but not a template-id)
14751 if (token->type == CPP_NAME
14752 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14754 cp_token *identifier_token;
14757 /* Look for the identifier. */
14758 identifier_token = cp_lexer_peek_token (parser->lexer);
14759 ambiguous_p = identifier_token->ambiguous_p;
14760 identifier = cp_parser_identifier (parser);
14761 /* If the next token isn't an identifier, we are certainly not
14762 looking at a class-name. */
14763 if (identifier == error_mark_node)
14764 decl = error_mark_node;
14765 /* If we know this is a type-name, there's no need to look it
14767 else if (typename_p)
14771 tree ambiguous_decls;
14772 /* If we already know that this lookup is ambiguous, then
14773 we've already issued an error message; there's no reason
14777 cp_parser_simulate_error (parser);
14778 return error_mark_node;
14780 /* If the next token is a `::', then the name must be a type
14783 [basic.lookup.qual]
14785 During the lookup for a name preceding the :: scope
14786 resolution operator, object, function, and enumerator
14787 names are ignored. */
14788 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14789 tag_type = typename_type;
14790 /* Look up the name. */
14791 decl = cp_parser_lookup_name (parser, identifier,
14793 /*is_template=*/false,
14794 /*is_namespace=*/false,
14795 check_dependency_p,
14797 identifier_token->location);
14798 if (ambiguous_decls)
14800 error ("%Hreference to %qD is ambiguous",
14801 &identifier_token->location, identifier);
14802 print_candidates (ambiguous_decls);
14803 if (cp_parser_parsing_tentatively (parser))
14805 identifier_token->ambiguous_p = true;
14806 cp_parser_simulate_error (parser);
14808 return error_mark_node;
14814 /* Try a template-id. */
14815 decl = cp_parser_template_id (parser, template_keyword_p,
14816 check_dependency_p,
14818 if (decl == error_mark_node)
14819 return error_mark_node;
14822 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14824 /* If this is a typename, create a TYPENAME_TYPE. */
14825 if (typename_p && decl != error_mark_node)
14827 decl = make_typename_type (scope, decl, typename_type,
14828 /*complain=*/tf_error);
14829 if (decl != error_mark_node)
14830 decl = TYPE_NAME (decl);
14833 /* Check to see that it is really the name of a class. */
14834 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14835 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14836 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14837 /* Situations like this:
14839 template <typename T> struct A {
14840 typename T::template X<int>::I i;
14843 are problematic. Is `T::template X<int>' a class-name? The
14844 standard does not seem to be definitive, but there is no other
14845 valid interpretation of the following `::'. Therefore, those
14846 names are considered class-names. */
14848 decl = make_typename_type (scope, decl, tag_type, tf_error);
14849 if (decl != error_mark_node)
14850 decl = TYPE_NAME (decl);
14852 else if (TREE_CODE (decl) != TYPE_DECL
14853 || TREE_TYPE (decl) == error_mark_node
14854 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14855 decl = error_mark_node;
14857 if (decl == error_mark_node)
14858 cp_parser_error (parser, "expected class-name");
14859 else if (identifier && !parser->scope)
14860 maybe_note_name_used_in_class (identifier, decl);
14865 /* Parse a class-specifier.
14868 class-head { member-specification [opt] }
14870 Returns the TREE_TYPE representing the class. */
14873 cp_parser_class_specifier (cp_parser* parser)
14877 tree attributes = NULL_TREE;
14878 int has_trailing_semicolon;
14879 bool nested_name_specifier_p;
14880 unsigned saved_num_template_parameter_lists;
14881 bool saved_in_function_body;
14882 bool saved_in_unbraced_linkage_specification_p;
14883 tree old_scope = NULL_TREE;
14884 tree scope = NULL_TREE;
14887 push_deferring_access_checks (dk_no_deferred);
14889 /* Parse the class-head. */
14890 type = cp_parser_class_head (parser,
14891 &nested_name_specifier_p,
14894 /* If the class-head was a semantic disaster, skip the entire body
14898 cp_parser_skip_to_end_of_block_or_statement (parser);
14899 pop_deferring_access_checks ();
14900 return error_mark_node;
14903 /* Look for the `{'. */
14904 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14906 pop_deferring_access_checks ();
14907 return error_mark_node;
14910 /* Process the base classes. If they're invalid, skip the
14911 entire class body. */
14912 if (!xref_basetypes (type, bases))
14914 /* Consuming the closing brace yields better error messages
14916 if (cp_parser_skip_to_closing_brace (parser))
14917 cp_lexer_consume_token (parser->lexer);
14918 pop_deferring_access_checks ();
14919 return error_mark_node;
14922 /* Issue an error message if type-definitions are forbidden here. */
14923 cp_parser_check_type_definition (parser);
14924 /* Remember that we are defining one more class. */
14925 ++parser->num_classes_being_defined;
14926 /* Inside the class, surrounding template-parameter-lists do not
14928 saved_num_template_parameter_lists
14929 = parser->num_template_parameter_lists;
14930 parser->num_template_parameter_lists = 0;
14931 /* We are not in a function body. */
14932 saved_in_function_body = parser->in_function_body;
14933 parser->in_function_body = false;
14934 /* We are not immediately inside an extern "lang" block. */
14935 saved_in_unbraced_linkage_specification_p
14936 = parser->in_unbraced_linkage_specification_p;
14937 parser->in_unbraced_linkage_specification_p = false;
14939 /* Start the class. */
14940 if (nested_name_specifier_p)
14942 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14943 old_scope = push_inner_scope (scope);
14945 type = begin_class_definition (type, attributes);
14947 if (type == error_mark_node)
14948 /* If the type is erroneous, skip the entire body of the class. */
14949 cp_parser_skip_to_closing_brace (parser);
14951 /* Parse the member-specification. */
14952 cp_parser_member_specification_opt (parser);
14954 /* Look for the trailing `}'. */
14955 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14956 /* We get better error messages by noticing a common problem: a
14957 missing trailing `;'. */
14958 token = cp_lexer_peek_token (parser->lexer);
14959 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14960 /* Look for trailing attributes to apply to this class. */
14961 if (cp_parser_allow_gnu_extensions_p (parser))
14962 attributes = cp_parser_attributes_opt (parser);
14963 if (type != error_mark_node)
14964 type = finish_struct (type, attributes);
14965 if (nested_name_specifier_p)
14966 pop_inner_scope (old_scope, scope);
14967 /* If this class is not itself within the scope of another class,
14968 then we need to parse the bodies of all of the queued function
14969 definitions. Note that the queued functions defined in a class
14970 are not always processed immediately following the
14971 class-specifier for that class. Consider:
14974 struct B { void f() { sizeof (A); } };
14977 If `f' were processed before the processing of `A' were
14978 completed, there would be no way to compute the size of `A'.
14979 Note that the nesting we are interested in here is lexical --
14980 not the semantic nesting given by TYPE_CONTEXT. In particular,
14983 struct A { struct B; };
14984 struct A::B { void f() { } };
14986 there is no need to delay the parsing of `A::B::f'. */
14987 if (--parser->num_classes_being_defined == 0)
14991 tree class_type = NULL_TREE;
14992 tree pushed_scope = NULL_TREE;
14994 /* In a first pass, parse default arguments to the functions.
14995 Then, in a second pass, parse the bodies of the functions.
14996 This two-phased approach handles cases like:
15004 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15005 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15006 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15007 TREE_PURPOSE (parser->unparsed_functions_queues)
15008 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15010 fn = TREE_VALUE (queue_entry);
15011 /* If there are default arguments that have not yet been processed,
15012 take care of them now. */
15013 if (class_type != TREE_PURPOSE (queue_entry))
15016 pop_scope (pushed_scope);
15017 class_type = TREE_PURPOSE (queue_entry);
15018 pushed_scope = push_scope (class_type);
15020 /* Make sure that any template parameters are in scope. */
15021 maybe_begin_member_template_processing (fn);
15022 /* Parse the default argument expressions. */
15023 cp_parser_late_parsing_default_args (parser, fn);
15024 /* Remove any template parameters from the symbol table. */
15025 maybe_end_member_template_processing ();
15028 pop_scope (pushed_scope);
15029 /* Now parse the body of the functions. */
15030 for (TREE_VALUE (parser->unparsed_functions_queues)
15031 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15032 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15033 TREE_VALUE (parser->unparsed_functions_queues)
15034 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15036 /* Figure out which function we need to process. */
15037 fn = TREE_VALUE (queue_entry);
15038 /* Parse the function. */
15039 cp_parser_late_parsing_for_member (parser, fn);
15043 /* Put back any saved access checks. */
15044 pop_deferring_access_checks ();
15046 /* Restore saved state. */
15047 parser->in_function_body = saved_in_function_body;
15048 parser->num_template_parameter_lists
15049 = saved_num_template_parameter_lists;
15050 parser->in_unbraced_linkage_specification_p
15051 = saved_in_unbraced_linkage_specification_p;
15056 /* Parse a class-head.
15059 class-key identifier [opt] base-clause [opt]
15060 class-key nested-name-specifier identifier base-clause [opt]
15061 class-key nested-name-specifier [opt] template-id
15065 class-key attributes identifier [opt] base-clause [opt]
15066 class-key attributes nested-name-specifier identifier base-clause [opt]
15067 class-key attributes nested-name-specifier [opt] template-id
15070 Upon return BASES is initialized to the list of base classes (or
15071 NULL, if there are none) in the same form returned by
15072 cp_parser_base_clause.
15074 Returns the TYPE of the indicated class. Sets
15075 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15076 involving a nested-name-specifier was used, and FALSE otherwise.
15078 Returns error_mark_node if this is not a class-head.
15080 Returns NULL_TREE if the class-head is syntactically valid, but
15081 semantically invalid in a way that means we should skip the entire
15082 body of the class. */
15085 cp_parser_class_head (cp_parser* parser,
15086 bool* nested_name_specifier_p,
15087 tree *attributes_p,
15090 tree nested_name_specifier;
15091 enum tag_types class_key;
15092 tree id = NULL_TREE;
15093 tree type = NULL_TREE;
15095 bool template_id_p = false;
15096 bool qualified_p = false;
15097 bool invalid_nested_name_p = false;
15098 bool invalid_explicit_specialization_p = false;
15099 tree pushed_scope = NULL_TREE;
15100 unsigned num_templates;
15101 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15102 /* Assume no nested-name-specifier will be present. */
15103 *nested_name_specifier_p = false;
15104 /* Assume no template parameter lists will be used in defining the
15108 *bases = NULL_TREE;
15110 /* Look for the class-key. */
15111 class_key = cp_parser_class_key (parser);
15112 if (class_key == none_type)
15113 return error_mark_node;
15115 /* Parse the attributes. */
15116 attributes = cp_parser_attributes_opt (parser);
15118 /* If the next token is `::', that is invalid -- but sometimes
15119 people do try to write:
15123 Handle this gracefully by accepting the extra qualifier, and then
15124 issuing an error about it later if this really is a
15125 class-head. If it turns out just to be an elaborated type
15126 specifier, remain silent. */
15127 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15128 qualified_p = true;
15130 push_deferring_access_checks (dk_no_check);
15132 /* Determine the name of the class. Begin by looking for an
15133 optional nested-name-specifier. */
15134 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15135 nested_name_specifier
15136 = cp_parser_nested_name_specifier_opt (parser,
15137 /*typename_keyword_p=*/false,
15138 /*check_dependency_p=*/false,
15140 /*is_declaration=*/false);
15141 /* If there was a nested-name-specifier, then there *must* be an
15143 if (nested_name_specifier)
15145 type_start_token = cp_lexer_peek_token (parser->lexer);
15146 /* Although the grammar says `identifier', it really means
15147 `class-name' or `template-name'. You are only allowed to
15148 define a class that has already been declared with this
15151 The proposed resolution for Core Issue 180 says that wherever
15152 you see `class T::X' you should treat `X' as a type-name.
15154 It is OK to define an inaccessible class; for example:
15156 class A { class B; };
15159 We do not know if we will see a class-name, or a
15160 template-name. We look for a class-name first, in case the
15161 class-name is a template-id; if we looked for the
15162 template-name first we would stop after the template-name. */
15163 cp_parser_parse_tentatively (parser);
15164 type = cp_parser_class_name (parser,
15165 /*typename_keyword_p=*/false,
15166 /*template_keyword_p=*/false,
15168 /*check_dependency_p=*/false,
15169 /*class_head_p=*/true,
15170 /*is_declaration=*/false);
15171 /* If that didn't work, ignore the nested-name-specifier. */
15172 if (!cp_parser_parse_definitely (parser))
15174 invalid_nested_name_p = true;
15175 type_start_token = cp_lexer_peek_token (parser->lexer);
15176 id = cp_parser_identifier (parser);
15177 if (id == error_mark_node)
15180 /* If we could not find a corresponding TYPE, treat this
15181 declaration like an unqualified declaration. */
15182 if (type == error_mark_node)
15183 nested_name_specifier = NULL_TREE;
15184 /* Otherwise, count the number of templates used in TYPE and its
15185 containing scopes. */
15190 for (scope = TREE_TYPE (type);
15191 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15192 scope = (TYPE_P (scope)
15193 ? TYPE_CONTEXT (scope)
15194 : DECL_CONTEXT (scope)))
15196 && CLASS_TYPE_P (scope)
15197 && CLASSTYPE_TEMPLATE_INFO (scope)
15198 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15199 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15203 /* Otherwise, the identifier is optional. */
15206 /* We don't know whether what comes next is a template-id,
15207 an identifier, or nothing at all. */
15208 cp_parser_parse_tentatively (parser);
15209 /* Check for a template-id. */
15210 type_start_token = cp_lexer_peek_token (parser->lexer);
15211 id = cp_parser_template_id (parser,
15212 /*template_keyword_p=*/false,
15213 /*check_dependency_p=*/true,
15214 /*is_declaration=*/true);
15215 /* If that didn't work, it could still be an identifier. */
15216 if (!cp_parser_parse_definitely (parser))
15218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15220 type_start_token = cp_lexer_peek_token (parser->lexer);
15221 id = cp_parser_identifier (parser);
15228 template_id_p = true;
15233 pop_deferring_access_checks ();
15236 cp_parser_check_for_invalid_template_id (parser, id,
15237 type_start_token->location);
15239 /* If it's not a `:' or a `{' then we can't really be looking at a
15240 class-head, since a class-head only appears as part of a
15241 class-specifier. We have to detect this situation before calling
15242 xref_tag, since that has irreversible side-effects. */
15243 if (!cp_parser_next_token_starts_class_definition_p (parser))
15245 cp_parser_error (parser, "expected %<{%> or %<:%>");
15246 return error_mark_node;
15249 /* At this point, we're going ahead with the class-specifier, even
15250 if some other problem occurs. */
15251 cp_parser_commit_to_tentative_parse (parser);
15252 /* Issue the error about the overly-qualified name now. */
15255 cp_parser_error (parser,
15256 "global qualification of class name is invalid");
15257 return error_mark_node;
15259 else if (invalid_nested_name_p)
15261 cp_parser_error (parser,
15262 "qualified name does not name a class");
15263 return error_mark_node;
15265 else if (nested_name_specifier)
15269 /* Reject typedef-names in class heads. */
15270 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15272 error ("%Hinvalid class name in declaration of %qD",
15273 &type_start_token->location, type);
15278 /* Figure out in what scope the declaration is being placed. */
15279 scope = current_scope ();
15280 /* If that scope does not contain the scope in which the
15281 class was originally declared, the program is invalid. */
15282 if (scope && !is_ancestor (scope, nested_name_specifier))
15284 if (at_namespace_scope_p ())
15285 error ("%Hdeclaration of %qD in namespace %qD which does not "
15287 &type_start_token->location,
15288 type, scope, nested_name_specifier);
15290 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15291 &type_start_token->location,
15292 type, scope, nested_name_specifier);
15298 A declarator-id shall not be qualified except for the
15299 definition of a ... nested class outside of its class
15300 ... [or] the definition or explicit instantiation of a
15301 class member of a namespace outside of its namespace. */
15302 if (scope == nested_name_specifier)
15304 permerror (input_location, "%Hextra qualification not allowed",
15305 &nested_name_specifier_token_start->location);
15306 nested_name_specifier = NULL_TREE;
15310 /* An explicit-specialization must be preceded by "template <>". If
15311 it is not, try to recover gracefully. */
15312 if (at_namespace_scope_p ()
15313 && parser->num_template_parameter_lists == 0
15316 error ("%Han explicit specialization must be preceded by %<template <>%>",
15317 &type_start_token->location);
15318 invalid_explicit_specialization_p = true;
15319 /* Take the same action that would have been taken by
15320 cp_parser_explicit_specialization. */
15321 ++parser->num_template_parameter_lists;
15322 begin_specialization ();
15324 /* There must be no "return" statements between this point and the
15325 end of this function; set "type "to the correct return value and
15326 use "goto done;" to return. */
15327 /* Make sure that the right number of template parameters were
15329 if (!cp_parser_check_template_parameters (parser, num_templates,
15330 type_start_token->location))
15332 /* If something went wrong, there is no point in even trying to
15333 process the class-definition. */
15338 /* Look up the type. */
15341 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15342 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15343 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15345 error ("%Hfunction template %qD redeclared as a class template",
15346 &type_start_token->location, id);
15347 type = error_mark_node;
15351 type = TREE_TYPE (id);
15352 type = maybe_process_partial_specialization (type);
15354 if (nested_name_specifier)
15355 pushed_scope = push_scope (nested_name_specifier);
15357 else if (nested_name_specifier)
15363 template <typename T> struct S { struct T };
15364 template <typename T> struct S<T>::T { };
15366 we will get a TYPENAME_TYPE when processing the definition of
15367 `S::T'. We need to resolve it to the actual type before we
15368 try to define it. */
15369 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15371 class_type = resolve_typename_type (TREE_TYPE (type),
15372 /*only_current_p=*/false);
15373 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15374 type = TYPE_NAME (class_type);
15377 cp_parser_error (parser, "could not resolve typename type");
15378 type = error_mark_node;
15382 if (maybe_process_partial_specialization (TREE_TYPE (type))
15383 == error_mark_node)
15389 class_type = current_class_type;
15390 /* Enter the scope indicated by the nested-name-specifier. */
15391 pushed_scope = push_scope (nested_name_specifier);
15392 /* Get the canonical version of this type. */
15393 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15394 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15395 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15397 type = push_template_decl (type);
15398 if (type == error_mark_node)
15405 type = TREE_TYPE (type);
15406 *nested_name_specifier_p = true;
15408 else /* The name is not a nested name. */
15410 /* If the class was unnamed, create a dummy name. */
15412 id = make_anon_name ();
15413 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15414 parser->num_template_parameter_lists);
15417 /* Indicate whether this class was declared as a `class' or as a
15419 if (TREE_CODE (type) == RECORD_TYPE)
15420 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15421 cp_parser_check_class_key (class_key, type);
15423 /* If this type was already complete, and we see another definition,
15424 that's an error. */
15425 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15427 error ("%Hredefinition of %q#T",
15428 &type_start_token->location, type);
15429 error ("%Hprevious definition of %q+#T",
15430 &type_start_token->location, type);
15434 else if (type == error_mark_node)
15437 /* We will have entered the scope containing the class; the names of
15438 base classes should be looked up in that context. For example:
15440 struct A { struct B {}; struct C; };
15441 struct A::C : B {};
15445 /* Get the list of base-classes, if there is one. */
15446 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15447 *bases = cp_parser_base_clause (parser);
15450 /* Leave the scope given by the nested-name-specifier. We will
15451 enter the class scope itself while processing the members. */
15453 pop_scope (pushed_scope);
15455 if (invalid_explicit_specialization_p)
15457 end_specialization ();
15458 --parser->num_template_parameter_lists;
15460 *attributes_p = attributes;
15464 /* Parse a class-key.
15471 Returns the kind of class-key specified, or none_type to indicate
15474 static enum tag_types
15475 cp_parser_class_key (cp_parser* parser)
15478 enum tag_types tag_type;
15480 /* Look for the class-key. */
15481 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15485 /* Check to see if the TOKEN is a class-key. */
15486 tag_type = cp_parser_token_is_class_key (token);
15488 cp_parser_error (parser, "expected class-key");
15492 /* Parse an (optional) member-specification.
15494 member-specification:
15495 member-declaration member-specification [opt]
15496 access-specifier : member-specification [opt] */
15499 cp_parser_member_specification_opt (cp_parser* parser)
15506 /* Peek at the next token. */
15507 token = cp_lexer_peek_token (parser->lexer);
15508 /* If it's a `}', or EOF then we've seen all the members. */
15509 if (token->type == CPP_CLOSE_BRACE
15510 || token->type == CPP_EOF
15511 || token->type == CPP_PRAGMA_EOL)
15514 /* See if this token is a keyword. */
15515 keyword = token->keyword;
15519 case RID_PROTECTED:
15521 /* Consume the access-specifier. */
15522 cp_lexer_consume_token (parser->lexer);
15523 /* Remember which access-specifier is active. */
15524 current_access_specifier = token->u.value;
15525 /* Look for the `:'. */
15526 cp_parser_require (parser, CPP_COLON, "%<:%>");
15530 /* Accept #pragmas at class scope. */
15531 if (token->type == CPP_PRAGMA)
15533 cp_parser_pragma (parser, pragma_external);
15537 /* Otherwise, the next construction must be a
15538 member-declaration. */
15539 cp_parser_member_declaration (parser);
15544 /* Parse a member-declaration.
15546 member-declaration:
15547 decl-specifier-seq [opt] member-declarator-list [opt] ;
15548 function-definition ; [opt]
15549 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15551 template-declaration
15553 member-declarator-list:
15555 member-declarator-list , member-declarator
15558 declarator pure-specifier [opt]
15559 declarator constant-initializer [opt]
15560 identifier [opt] : constant-expression
15564 member-declaration:
15565 __extension__ member-declaration
15568 declarator attributes [opt] pure-specifier [opt]
15569 declarator attributes [opt] constant-initializer [opt]
15570 identifier [opt] attributes [opt] : constant-expression
15574 member-declaration:
15575 static_assert-declaration */
15578 cp_parser_member_declaration (cp_parser* parser)
15580 cp_decl_specifier_seq decl_specifiers;
15581 tree prefix_attributes;
15583 int declares_class_or_enum;
15585 cp_token *token = NULL;
15586 cp_token *decl_spec_token_start = NULL;
15587 cp_token *initializer_token_start = NULL;
15588 int saved_pedantic;
15590 /* Check for the `__extension__' keyword. */
15591 if (cp_parser_extension_opt (parser, &saved_pedantic))
15594 cp_parser_member_declaration (parser);
15595 /* Restore the old value of the PEDANTIC flag. */
15596 pedantic = saved_pedantic;
15601 /* Check for a template-declaration. */
15602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15604 /* An explicit specialization here is an error condition, and we
15605 expect the specialization handler to detect and report this. */
15606 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15608 cp_parser_explicit_specialization (parser);
15610 cp_parser_template_declaration (parser, /*member_p=*/true);
15615 /* Check for a using-declaration. */
15616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15618 /* Parse the using-declaration. */
15619 cp_parser_using_declaration (parser,
15620 /*access_declaration_p=*/false);
15624 /* Check for @defs. */
15625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15628 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15629 ivar = ivar_chains;
15633 ivar = TREE_CHAIN (member);
15634 TREE_CHAIN (member) = NULL_TREE;
15635 finish_member_declaration (member);
15640 /* If the next token is `static_assert' we have a static assertion. */
15641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15643 cp_parser_static_assert (parser, /*member_p=*/true);
15647 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15650 /* Parse the decl-specifier-seq. */
15651 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15652 cp_parser_decl_specifier_seq (parser,
15653 CP_PARSER_FLAGS_OPTIONAL,
15655 &declares_class_or_enum);
15656 prefix_attributes = decl_specifiers.attributes;
15657 decl_specifiers.attributes = NULL_TREE;
15658 /* Check for an invalid type-name. */
15659 if (!decl_specifiers.type
15660 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15662 /* If there is no declarator, then the decl-specifier-seq should
15664 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15666 /* If there was no decl-specifier-seq, and the next token is a
15667 `;', then we have something like:
15673 Each member-declaration shall declare at least one member
15674 name of the class. */
15675 if (!decl_specifiers.any_specifiers_p)
15677 cp_token *token = cp_lexer_peek_token (parser->lexer);
15678 if (!in_system_header_at (token->location))
15679 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15685 /* See if this declaration is a friend. */
15686 friend_p = cp_parser_friend_p (&decl_specifiers);
15687 /* If there were decl-specifiers, check to see if there was
15688 a class-declaration. */
15689 type = check_tag_decl (&decl_specifiers);
15690 /* Nested classes have already been added to the class, but
15691 a `friend' needs to be explicitly registered. */
15694 /* If the `friend' keyword was present, the friend must
15695 be introduced with a class-key. */
15696 if (!declares_class_or_enum)
15697 error ("%Ha class-key must be used when declaring a friend",
15698 &decl_spec_token_start->location);
15701 template <typename T> struct A {
15702 friend struct A<T>::B;
15705 A<T>::B will be represented by a TYPENAME_TYPE, and
15706 therefore not recognized by check_tag_decl. */
15708 && decl_specifiers.type
15709 && TYPE_P (decl_specifiers.type))
15710 type = decl_specifiers.type;
15711 if (!type || !TYPE_P (type))
15712 error ("%Hfriend declaration does not name a class or "
15713 "function", &decl_spec_token_start->location);
15715 make_friend_class (current_class_type, type,
15716 /*complain=*/true);
15718 /* If there is no TYPE, an error message will already have
15720 else if (!type || type == error_mark_node)
15722 /* An anonymous aggregate has to be handled specially; such
15723 a declaration really declares a data member (with a
15724 particular type), as opposed to a nested class. */
15725 else if (ANON_AGGR_TYPE_P (type))
15727 /* Remove constructors and such from TYPE, now that we
15728 know it is an anonymous aggregate. */
15729 fixup_anonymous_aggr (type);
15730 /* And make the corresponding data member. */
15731 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15732 /* Add it to the class. */
15733 finish_member_declaration (decl);
15736 cp_parser_check_access_in_redeclaration
15738 decl_spec_token_start->location);
15743 /* See if these declarations will be friends. */
15744 friend_p = cp_parser_friend_p (&decl_specifiers);
15746 /* Keep going until we hit the `;' at the end of the
15748 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15750 tree attributes = NULL_TREE;
15751 tree first_attribute;
15753 /* Peek at the next token. */
15754 token = cp_lexer_peek_token (parser->lexer);
15756 /* Check for a bitfield declaration. */
15757 if (token->type == CPP_COLON
15758 || (token->type == CPP_NAME
15759 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15765 /* Get the name of the bitfield. Note that we cannot just
15766 check TOKEN here because it may have been invalidated by
15767 the call to cp_lexer_peek_nth_token above. */
15768 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15769 identifier = cp_parser_identifier (parser);
15771 identifier = NULL_TREE;
15773 /* Consume the `:' token. */
15774 cp_lexer_consume_token (parser->lexer);
15775 /* Get the width of the bitfield. */
15777 = cp_parser_constant_expression (parser,
15778 /*allow_non_constant=*/false,
15781 /* Look for attributes that apply to the bitfield. */
15782 attributes = cp_parser_attributes_opt (parser);
15783 /* Remember which attributes are prefix attributes and
15785 first_attribute = attributes;
15786 /* Combine the attributes. */
15787 attributes = chainon (prefix_attributes, attributes);
15789 /* Create the bitfield declaration. */
15790 decl = grokbitfield (identifier
15791 ? make_id_declarator (NULL_TREE,
15801 cp_declarator *declarator;
15803 tree asm_specification;
15804 int ctor_dtor_or_conv_p;
15806 /* Parse the declarator. */
15808 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15809 &ctor_dtor_or_conv_p,
15810 /*parenthesized_p=*/NULL,
15811 /*member_p=*/true);
15813 /* If something went wrong parsing the declarator, make sure
15814 that we at least consume some tokens. */
15815 if (declarator == cp_error_declarator)
15817 /* Skip to the end of the statement. */
15818 cp_parser_skip_to_end_of_statement (parser);
15819 /* If the next token is not a semicolon, that is
15820 probably because we just skipped over the body of
15821 a function. So, we consume a semicolon if
15822 present, but do not issue an error message if it
15824 if (cp_lexer_next_token_is (parser->lexer,
15826 cp_lexer_consume_token (parser->lexer);
15830 if (declares_class_or_enum & 2)
15831 cp_parser_check_for_definition_in_return_type
15832 (declarator, decl_specifiers.type,
15833 decl_specifiers.type_location);
15835 /* Look for an asm-specification. */
15836 asm_specification = cp_parser_asm_specification_opt (parser);
15837 /* Look for attributes that apply to the declaration. */
15838 attributes = cp_parser_attributes_opt (parser);
15839 /* Remember which attributes are prefix attributes and
15841 first_attribute = attributes;
15842 /* Combine the attributes. */
15843 attributes = chainon (prefix_attributes, attributes);
15845 /* If it's an `=', then we have a constant-initializer or a
15846 pure-specifier. It is not correct to parse the
15847 initializer before registering the member declaration
15848 since the member declaration should be in scope while
15849 its initializer is processed. However, the rest of the
15850 front end does not yet provide an interface that allows
15851 us to handle this correctly. */
15852 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15856 A pure-specifier shall be used only in the declaration of
15857 a virtual function.
15859 A member-declarator can contain a constant-initializer
15860 only if it declares a static member of integral or
15863 Therefore, if the DECLARATOR is for a function, we look
15864 for a pure-specifier; otherwise, we look for a
15865 constant-initializer. When we call `grokfield', it will
15866 perform more stringent semantics checks. */
15867 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15868 if (function_declarator_p (declarator))
15869 initializer = cp_parser_pure_specifier (parser);
15871 /* Parse the initializer. */
15872 initializer = cp_parser_constant_initializer (parser);
15874 /* Otherwise, there is no initializer. */
15876 initializer = NULL_TREE;
15878 /* See if we are probably looking at a function
15879 definition. We are certainly not looking at a
15880 member-declarator. Calling `grokfield' has
15881 side-effects, so we must not do it unless we are sure
15882 that we are looking at a member-declarator. */
15883 if (cp_parser_token_starts_function_definition_p
15884 (cp_lexer_peek_token (parser->lexer)))
15886 /* The grammar does not allow a pure-specifier to be
15887 used when a member function is defined. (It is
15888 possible that this fact is an oversight in the
15889 standard, since a pure function may be defined
15890 outside of the class-specifier. */
15892 error ("%Hpure-specifier on function-definition",
15893 &initializer_token_start->location);
15894 decl = cp_parser_save_member_function_body (parser,
15898 /* If the member was not a friend, declare it here. */
15900 finish_member_declaration (decl);
15901 /* Peek at the next token. */
15902 token = cp_lexer_peek_token (parser->lexer);
15903 /* If the next token is a semicolon, consume it. */
15904 if (token->type == CPP_SEMICOLON)
15905 cp_lexer_consume_token (parser->lexer);
15909 if (declarator->kind == cdk_function)
15910 declarator->id_loc = token->location;
15911 /* Create the declaration. */
15912 decl = grokfield (declarator, &decl_specifiers,
15913 initializer, /*init_const_expr_p=*/true,
15918 /* Reset PREFIX_ATTRIBUTES. */
15919 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15920 attributes = TREE_CHAIN (attributes);
15922 TREE_CHAIN (attributes) = NULL_TREE;
15924 /* If there is any qualification still in effect, clear it
15925 now; we will be starting fresh with the next declarator. */
15926 parser->scope = NULL_TREE;
15927 parser->qualifying_scope = NULL_TREE;
15928 parser->object_scope = NULL_TREE;
15929 /* If it's a `,', then there are more declarators. */
15930 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15931 cp_lexer_consume_token (parser->lexer);
15932 /* If the next token isn't a `;', then we have a parse error. */
15933 else if (cp_lexer_next_token_is_not (parser->lexer,
15936 cp_parser_error (parser, "expected %<;%>");
15937 /* Skip tokens until we find a `;'. */
15938 cp_parser_skip_to_end_of_statement (parser);
15945 /* Add DECL to the list of members. */
15947 finish_member_declaration (decl);
15949 if (TREE_CODE (decl) == FUNCTION_DECL)
15950 cp_parser_save_default_args (parser, decl);
15955 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15958 /* Parse a pure-specifier.
15963 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15964 Otherwise, ERROR_MARK_NODE is returned. */
15967 cp_parser_pure_specifier (cp_parser* parser)
15971 /* Look for the `=' token. */
15972 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15973 return error_mark_node;
15974 /* Look for the `0' token. */
15975 token = cp_lexer_consume_token (parser->lexer);
15977 /* Accept = default or = delete in c++0x mode. */
15978 if (token->keyword == RID_DEFAULT
15979 || token->keyword == RID_DELETE)
15981 maybe_warn_cpp0x ("defaulted and deleted functions");
15982 return token->u.value;
15985 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15986 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15988 cp_parser_error (parser,
15989 "invalid pure specifier (only %<= 0%> is allowed)");
15990 cp_parser_skip_to_end_of_statement (parser);
15991 return error_mark_node;
15993 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15995 error ("%Htemplates may not be %<virtual%>", &token->location);
15996 return error_mark_node;
15999 return integer_zero_node;
16002 /* Parse a constant-initializer.
16004 constant-initializer:
16005 = constant-expression
16007 Returns a representation of the constant-expression. */
16010 cp_parser_constant_initializer (cp_parser* parser)
16012 /* Look for the `=' token. */
16013 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16014 return error_mark_node;
16016 /* It is invalid to write:
16018 struct S { static const int i = { 7 }; };
16021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16023 cp_parser_error (parser,
16024 "a brace-enclosed initializer is not allowed here");
16025 /* Consume the opening brace. */
16026 cp_lexer_consume_token (parser->lexer);
16027 /* Skip the initializer. */
16028 cp_parser_skip_to_closing_brace (parser);
16029 /* Look for the trailing `}'. */
16030 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16032 return error_mark_node;
16035 return cp_parser_constant_expression (parser,
16036 /*allow_non_constant=*/false,
16040 /* Derived classes [gram.class.derived] */
16042 /* Parse a base-clause.
16045 : base-specifier-list
16047 base-specifier-list:
16048 base-specifier ... [opt]
16049 base-specifier-list , base-specifier ... [opt]
16051 Returns a TREE_LIST representing the base-classes, in the order in
16052 which they were declared. The representation of each node is as
16053 described by cp_parser_base_specifier.
16055 In the case that no bases are specified, this function will return
16056 NULL_TREE, not ERROR_MARK_NODE. */
16059 cp_parser_base_clause (cp_parser* parser)
16061 tree bases = NULL_TREE;
16063 /* Look for the `:' that begins the list. */
16064 cp_parser_require (parser, CPP_COLON, "%<:%>");
16066 /* Scan the base-specifier-list. */
16071 bool pack_expansion_p = false;
16073 /* Look for the base-specifier. */
16074 base = cp_parser_base_specifier (parser);
16075 /* Look for the (optional) ellipsis. */
16076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16078 /* Consume the `...'. */
16079 cp_lexer_consume_token (parser->lexer);
16081 pack_expansion_p = true;
16084 /* Add BASE to the front of the list. */
16085 if (base != error_mark_node)
16087 if (pack_expansion_p)
16088 /* Make this a pack expansion type. */
16089 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16092 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16094 TREE_CHAIN (base) = bases;
16098 /* Peek at the next token. */
16099 token = cp_lexer_peek_token (parser->lexer);
16100 /* If it's not a comma, then the list is complete. */
16101 if (token->type != CPP_COMMA)
16103 /* Consume the `,'. */
16104 cp_lexer_consume_token (parser->lexer);
16107 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16108 base class had a qualified name. However, the next name that
16109 appears is certainly not qualified. */
16110 parser->scope = NULL_TREE;
16111 parser->qualifying_scope = NULL_TREE;
16112 parser->object_scope = NULL_TREE;
16114 return nreverse (bases);
16117 /* Parse a base-specifier.
16120 :: [opt] nested-name-specifier [opt] class-name
16121 virtual access-specifier [opt] :: [opt] nested-name-specifier
16123 access-specifier virtual [opt] :: [opt] nested-name-specifier
16126 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16127 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16128 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16129 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16132 cp_parser_base_specifier (cp_parser* parser)
16136 bool virtual_p = false;
16137 bool duplicate_virtual_error_issued_p = false;
16138 bool duplicate_access_error_issued_p = false;
16139 bool class_scope_p, template_p;
16140 tree access = access_default_node;
16143 /* Process the optional `virtual' and `access-specifier'. */
16146 /* Peek at the next token. */
16147 token = cp_lexer_peek_token (parser->lexer);
16148 /* Process `virtual'. */
16149 switch (token->keyword)
16152 /* If `virtual' appears more than once, issue an error. */
16153 if (virtual_p && !duplicate_virtual_error_issued_p)
16155 cp_parser_error (parser,
16156 "%<virtual%> specified more than once in base-specified");
16157 duplicate_virtual_error_issued_p = true;
16162 /* Consume the `virtual' token. */
16163 cp_lexer_consume_token (parser->lexer);
16168 case RID_PROTECTED:
16170 /* If more than one access specifier appears, issue an
16172 if (access != access_default_node
16173 && !duplicate_access_error_issued_p)
16175 cp_parser_error (parser,
16176 "more than one access specifier in base-specified");
16177 duplicate_access_error_issued_p = true;
16180 access = ridpointers[(int) token->keyword];
16182 /* Consume the access-specifier. */
16183 cp_lexer_consume_token (parser->lexer);
16192 /* It is not uncommon to see programs mechanically, erroneously, use
16193 the 'typename' keyword to denote (dependent) qualified types
16194 as base classes. */
16195 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16197 token = cp_lexer_peek_token (parser->lexer);
16198 if (!processing_template_decl)
16199 error ("%Hkeyword %<typename%> not allowed outside of templates",
16202 error ("%Hkeyword %<typename%> not allowed in this context "
16203 "(the base class is implicitly a type)",
16205 cp_lexer_consume_token (parser->lexer);
16208 /* Look for the optional `::' operator. */
16209 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16210 /* Look for the nested-name-specifier. The simplest way to
16215 The keyword `typename' is not permitted in a base-specifier or
16216 mem-initializer; in these contexts a qualified name that
16217 depends on a template-parameter is implicitly assumed to be a
16220 is to pretend that we have seen the `typename' keyword at this
16222 cp_parser_nested_name_specifier_opt (parser,
16223 /*typename_keyword_p=*/true,
16224 /*check_dependency_p=*/true,
16226 /*is_declaration=*/true);
16227 /* If the base class is given by a qualified name, assume that names
16228 we see are type names or templates, as appropriate. */
16229 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16230 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16232 /* Finally, look for the class-name. */
16233 type = cp_parser_class_name (parser,
16237 /*check_dependency_p=*/true,
16238 /*class_head_p=*/false,
16239 /*is_declaration=*/true);
16241 if (type == error_mark_node)
16242 return error_mark_node;
16244 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16247 /* Exception handling [gram.exception] */
16249 /* Parse an (optional) exception-specification.
16251 exception-specification:
16252 throw ( type-id-list [opt] )
16254 Returns a TREE_LIST representing the exception-specification. The
16255 TREE_VALUE of each node is a type. */
16258 cp_parser_exception_specification_opt (cp_parser* parser)
16263 /* Peek at the next token. */
16264 token = cp_lexer_peek_token (parser->lexer);
16265 /* If it's not `throw', then there's no exception-specification. */
16266 if (!cp_parser_is_keyword (token, RID_THROW))
16269 /* Consume the `throw'. */
16270 cp_lexer_consume_token (parser->lexer);
16272 /* Look for the `('. */
16273 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16275 /* Peek at the next token. */
16276 token = cp_lexer_peek_token (parser->lexer);
16277 /* If it's not a `)', then there is a type-id-list. */
16278 if (token->type != CPP_CLOSE_PAREN)
16280 const char *saved_message;
16282 /* Types may not be defined in an exception-specification. */
16283 saved_message = parser->type_definition_forbidden_message;
16284 parser->type_definition_forbidden_message
16285 = "types may not be defined in an exception-specification";
16286 /* Parse the type-id-list. */
16287 type_id_list = cp_parser_type_id_list (parser);
16288 /* Restore the saved message. */
16289 parser->type_definition_forbidden_message = saved_message;
16292 type_id_list = empty_except_spec;
16294 /* Look for the `)'. */
16295 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16297 return type_id_list;
16300 /* Parse an (optional) type-id-list.
16304 type-id-list , type-id ... [opt]
16306 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16307 in the order that the types were presented. */
16310 cp_parser_type_id_list (cp_parser* parser)
16312 tree types = NULL_TREE;
16319 /* Get the next type-id. */
16320 type = cp_parser_type_id (parser);
16321 /* Parse the optional ellipsis. */
16322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16324 /* Consume the `...'. */
16325 cp_lexer_consume_token (parser->lexer);
16327 /* Turn the type into a pack expansion expression. */
16328 type = make_pack_expansion (type);
16330 /* Add it to the list. */
16331 types = add_exception_specifier (types, type, /*complain=*/1);
16332 /* Peek at the next token. */
16333 token = cp_lexer_peek_token (parser->lexer);
16334 /* If it is not a `,', we are done. */
16335 if (token->type != CPP_COMMA)
16337 /* Consume the `,'. */
16338 cp_lexer_consume_token (parser->lexer);
16341 return nreverse (types);
16344 /* Parse a try-block.
16347 try compound-statement handler-seq */
16350 cp_parser_try_block (cp_parser* parser)
16354 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16355 try_block = begin_try_block ();
16356 cp_parser_compound_statement (parser, NULL, true);
16357 finish_try_block (try_block);
16358 cp_parser_handler_seq (parser);
16359 finish_handler_sequence (try_block);
16364 /* Parse a function-try-block.
16366 function-try-block:
16367 try ctor-initializer [opt] function-body handler-seq */
16370 cp_parser_function_try_block (cp_parser* parser)
16372 tree compound_stmt;
16374 bool ctor_initializer_p;
16376 /* Look for the `try' keyword. */
16377 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16379 /* Let the rest of the front end know where we are. */
16380 try_block = begin_function_try_block (&compound_stmt);
16381 /* Parse the function-body. */
16383 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16384 /* We're done with the `try' part. */
16385 finish_function_try_block (try_block);
16386 /* Parse the handlers. */
16387 cp_parser_handler_seq (parser);
16388 /* We're done with the handlers. */
16389 finish_function_handler_sequence (try_block, compound_stmt);
16391 return ctor_initializer_p;
16394 /* Parse a handler-seq.
16397 handler handler-seq [opt] */
16400 cp_parser_handler_seq (cp_parser* parser)
16406 /* Parse the handler. */
16407 cp_parser_handler (parser);
16408 /* Peek at the next token. */
16409 token = cp_lexer_peek_token (parser->lexer);
16410 /* If it's not `catch' then there are no more handlers. */
16411 if (!cp_parser_is_keyword (token, RID_CATCH))
16416 /* Parse a handler.
16419 catch ( exception-declaration ) compound-statement */
16422 cp_parser_handler (cp_parser* parser)
16427 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16428 handler = begin_handler ();
16429 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16430 declaration = cp_parser_exception_declaration (parser);
16431 finish_handler_parms (declaration, handler);
16432 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16433 cp_parser_compound_statement (parser, NULL, false);
16434 finish_handler (handler);
16437 /* Parse an exception-declaration.
16439 exception-declaration:
16440 type-specifier-seq declarator
16441 type-specifier-seq abstract-declarator
16445 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16446 ellipsis variant is used. */
16449 cp_parser_exception_declaration (cp_parser* parser)
16451 cp_decl_specifier_seq type_specifiers;
16452 cp_declarator *declarator;
16453 const char *saved_message;
16455 /* If it's an ellipsis, it's easy to handle. */
16456 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16458 /* Consume the `...' token. */
16459 cp_lexer_consume_token (parser->lexer);
16463 /* Types may not be defined in exception-declarations. */
16464 saved_message = parser->type_definition_forbidden_message;
16465 parser->type_definition_forbidden_message
16466 = "types may not be defined in exception-declarations";
16468 /* Parse the type-specifier-seq. */
16469 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16471 /* If it's a `)', then there is no declarator. */
16472 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16475 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16476 /*ctor_dtor_or_conv_p=*/NULL,
16477 /*parenthesized_p=*/NULL,
16478 /*member_p=*/false);
16480 /* Restore the saved message. */
16481 parser->type_definition_forbidden_message = saved_message;
16483 if (!type_specifiers.any_specifiers_p)
16484 return error_mark_node;
16486 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16489 /* Parse a throw-expression.
16492 throw assignment-expression [opt]
16494 Returns a THROW_EXPR representing the throw-expression. */
16497 cp_parser_throw_expression (cp_parser* parser)
16502 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16503 token = cp_lexer_peek_token (parser->lexer);
16504 /* Figure out whether or not there is an assignment-expression
16505 following the "throw" keyword. */
16506 if (token->type == CPP_COMMA
16507 || token->type == CPP_SEMICOLON
16508 || token->type == CPP_CLOSE_PAREN
16509 || token->type == CPP_CLOSE_SQUARE
16510 || token->type == CPP_CLOSE_BRACE
16511 || token->type == CPP_COLON)
16512 expression = NULL_TREE;
16514 expression = cp_parser_assignment_expression (parser,
16517 return build_throw (expression);
16520 /* GNU Extensions */
16522 /* Parse an (optional) asm-specification.
16525 asm ( string-literal )
16527 If the asm-specification is present, returns a STRING_CST
16528 corresponding to the string-literal. Otherwise, returns
16532 cp_parser_asm_specification_opt (cp_parser* parser)
16535 tree asm_specification;
16537 /* Peek at the next token. */
16538 token = cp_lexer_peek_token (parser->lexer);
16539 /* If the next token isn't the `asm' keyword, then there's no
16540 asm-specification. */
16541 if (!cp_parser_is_keyword (token, RID_ASM))
16544 /* Consume the `asm' token. */
16545 cp_lexer_consume_token (parser->lexer);
16546 /* Look for the `('. */
16547 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16549 /* Look for the string-literal. */
16550 asm_specification = cp_parser_string_literal (parser, false, false);
16552 /* Look for the `)'. */
16553 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16555 return asm_specification;
16558 /* Parse an asm-operand-list.
16562 asm-operand-list , asm-operand
16565 string-literal ( expression )
16566 [ string-literal ] string-literal ( expression )
16568 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16569 each node is the expression. The TREE_PURPOSE is itself a
16570 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16571 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16572 is a STRING_CST for the string literal before the parenthesis. Returns
16573 ERROR_MARK_NODE if any of the operands are invalid. */
16576 cp_parser_asm_operand_list (cp_parser* parser)
16578 tree asm_operands = NULL_TREE;
16579 bool invalid_operands = false;
16583 tree string_literal;
16587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16589 /* Consume the `[' token. */
16590 cp_lexer_consume_token (parser->lexer);
16591 /* Read the operand name. */
16592 name = cp_parser_identifier (parser);
16593 if (name != error_mark_node)
16594 name = build_string (IDENTIFIER_LENGTH (name),
16595 IDENTIFIER_POINTER (name));
16596 /* Look for the closing `]'. */
16597 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16601 /* Look for the string-literal. */
16602 string_literal = cp_parser_string_literal (parser, false, false);
16604 /* Look for the `('. */
16605 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16606 /* Parse the expression. */
16607 expression = cp_parser_expression (parser, /*cast_p=*/false);
16608 /* Look for the `)'. */
16609 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16611 if (name == error_mark_node
16612 || string_literal == error_mark_node
16613 || expression == error_mark_node)
16614 invalid_operands = true;
16616 /* Add this operand to the list. */
16617 asm_operands = tree_cons (build_tree_list (name, string_literal),
16620 /* If the next token is not a `,', there are no more
16622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16624 /* Consume the `,'. */
16625 cp_lexer_consume_token (parser->lexer);
16628 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16631 /* Parse an asm-clobber-list.
16635 asm-clobber-list , string-literal
16637 Returns a TREE_LIST, indicating the clobbers in the order that they
16638 appeared. The TREE_VALUE of each node is a STRING_CST. */
16641 cp_parser_asm_clobber_list (cp_parser* parser)
16643 tree clobbers = NULL_TREE;
16647 tree string_literal;
16649 /* Look for the string literal. */
16650 string_literal = cp_parser_string_literal (parser, false, false);
16651 /* Add it to the list. */
16652 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16653 /* If the next token is not a `,', then the list is
16655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16657 /* Consume the `,' token. */
16658 cp_lexer_consume_token (parser->lexer);
16664 /* Parse an (optional) series of attributes.
16667 attributes attribute
16670 __attribute__ (( attribute-list [opt] ))
16672 The return value is as for cp_parser_attribute_list. */
16675 cp_parser_attributes_opt (cp_parser* parser)
16677 tree attributes = NULL_TREE;
16682 tree attribute_list;
16684 /* Peek at the next token. */
16685 token = cp_lexer_peek_token (parser->lexer);
16686 /* If it's not `__attribute__', then we're done. */
16687 if (token->keyword != RID_ATTRIBUTE)
16690 /* Consume the `__attribute__' keyword. */
16691 cp_lexer_consume_token (parser->lexer);
16692 /* Look for the two `(' tokens. */
16693 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16694 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16696 /* Peek at the next token. */
16697 token = cp_lexer_peek_token (parser->lexer);
16698 if (token->type != CPP_CLOSE_PAREN)
16699 /* Parse the attribute-list. */
16700 attribute_list = cp_parser_attribute_list (parser);
16702 /* If the next token is a `)', then there is no attribute
16704 attribute_list = NULL;
16706 /* Look for the two `)' tokens. */
16707 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16708 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16710 /* Add these new attributes to the list. */
16711 attributes = chainon (attributes, attribute_list);
16717 /* Parse an attribute-list.
16721 attribute-list , attribute
16725 identifier ( identifier )
16726 identifier ( identifier , expression-list )
16727 identifier ( expression-list )
16729 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16730 to an attribute. The TREE_PURPOSE of each node is the identifier
16731 indicating which attribute is in use. The TREE_VALUE represents
16732 the arguments, if any. */
16735 cp_parser_attribute_list (cp_parser* parser)
16737 tree attribute_list = NULL_TREE;
16738 bool save_translate_strings_p = parser->translate_strings_p;
16740 parser->translate_strings_p = false;
16747 /* Look for the identifier. We also allow keywords here; for
16748 example `__attribute__ ((const))' is legal. */
16749 token = cp_lexer_peek_token (parser->lexer);
16750 if (token->type == CPP_NAME
16751 || token->type == CPP_KEYWORD)
16753 tree arguments = NULL_TREE;
16755 /* Consume the token. */
16756 token = cp_lexer_consume_token (parser->lexer);
16758 /* Save away the identifier that indicates which attribute
16760 identifier = token->u.value;
16761 attribute = build_tree_list (identifier, NULL_TREE);
16763 /* Peek at the next token. */
16764 token = cp_lexer_peek_token (parser->lexer);
16765 /* If it's an `(', then parse the attribute arguments. */
16766 if (token->type == CPP_OPEN_PAREN)
16768 arguments = cp_parser_parenthesized_expression_list
16769 (parser, true, /*cast_p=*/false,
16770 /*allow_expansion_p=*/false,
16771 /*non_constant_p=*/NULL);
16772 /* Save the arguments away. */
16773 TREE_VALUE (attribute) = arguments;
16776 if (arguments != error_mark_node)
16778 /* Add this attribute to the list. */
16779 TREE_CHAIN (attribute) = attribute_list;
16780 attribute_list = attribute;
16783 token = cp_lexer_peek_token (parser->lexer);
16785 /* Now, look for more attributes. If the next token isn't a
16786 `,', we're done. */
16787 if (token->type != CPP_COMMA)
16790 /* Consume the comma and keep going. */
16791 cp_lexer_consume_token (parser->lexer);
16793 parser->translate_strings_p = save_translate_strings_p;
16795 /* We built up the list in reverse order. */
16796 return nreverse (attribute_list);
16799 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16800 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16801 current value of the PEDANTIC flag, regardless of whether or not
16802 the `__extension__' keyword is present. The caller is responsible
16803 for restoring the value of the PEDANTIC flag. */
16806 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16808 /* Save the old value of the PEDANTIC flag. */
16809 *saved_pedantic = pedantic;
16811 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16813 /* Consume the `__extension__' token. */
16814 cp_lexer_consume_token (parser->lexer);
16815 /* We're not being pedantic while the `__extension__' keyword is
16825 /* Parse a label declaration.
16828 __label__ label-declarator-seq ;
16830 label-declarator-seq:
16831 identifier , label-declarator-seq
16835 cp_parser_label_declaration (cp_parser* parser)
16837 /* Look for the `__label__' keyword. */
16838 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16844 /* Look for an identifier. */
16845 identifier = cp_parser_identifier (parser);
16846 /* If we failed, stop. */
16847 if (identifier == error_mark_node)
16849 /* Declare it as a label. */
16850 finish_label_decl (identifier);
16851 /* If the next token is a `;', stop. */
16852 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16854 /* Look for the `,' separating the label declarations. */
16855 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16858 /* Look for the final `;'. */
16859 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16862 /* Support Functions */
16864 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16865 NAME should have one of the representations used for an
16866 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16867 is returned. If PARSER->SCOPE is a dependent type, then a
16868 SCOPE_REF is returned.
16870 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16871 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16872 was formed. Abstractly, such entities should not be passed to this
16873 function, because they do not need to be looked up, but it is
16874 simpler to check for this special case here, rather than at the
16877 In cases not explicitly covered above, this function returns a
16878 DECL, OVERLOAD, or baselink representing the result of the lookup.
16879 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16882 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16883 (e.g., "struct") that was used. In that case bindings that do not
16884 refer to types are ignored.
16886 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16889 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16892 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16895 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16896 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16897 NULL_TREE otherwise. */
16900 cp_parser_lookup_name (cp_parser *parser, tree name,
16901 enum tag_types tag_type,
16904 bool check_dependency,
16905 tree *ambiguous_decls,
16906 location_t name_location)
16910 tree object_type = parser->context->object_type;
16912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16913 flags |= LOOKUP_COMPLAIN;
16915 /* Assume that the lookup will be unambiguous. */
16916 if (ambiguous_decls)
16917 *ambiguous_decls = NULL_TREE;
16919 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16920 no longer valid. Note that if we are parsing tentatively, and
16921 the parse fails, OBJECT_TYPE will be automatically restored. */
16922 parser->context->object_type = NULL_TREE;
16924 if (name == error_mark_node)
16925 return error_mark_node;
16927 /* A template-id has already been resolved; there is no lookup to
16929 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16931 if (BASELINK_P (name))
16933 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16934 == TEMPLATE_ID_EXPR);
16938 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16939 it should already have been checked to make sure that the name
16940 used matches the type being destroyed. */
16941 if (TREE_CODE (name) == BIT_NOT_EXPR)
16945 /* Figure out to which type this destructor applies. */
16947 type = parser->scope;
16948 else if (object_type)
16949 type = object_type;
16951 type = current_class_type;
16952 /* If that's not a class type, there is no destructor. */
16953 if (!type || !CLASS_TYPE_P (type))
16954 return error_mark_node;
16955 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16956 lazily_declare_fn (sfk_destructor, type);
16957 if (!CLASSTYPE_DESTRUCTORS (type))
16958 return error_mark_node;
16959 /* If it was a class type, return the destructor. */
16960 return CLASSTYPE_DESTRUCTORS (type);
16963 /* By this point, the NAME should be an ordinary identifier. If
16964 the id-expression was a qualified name, the qualifying scope is
16965 stored in PARSER->SCOPE at this point. */
16966 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16968 /* Perform the lookup. */
16973 if (parser->scope == error_mark_node)
16974 return error_mark_node;
16976 /* If the SCOPE is dependent, the lookup must be deferred until
16977 the template is instantiated -- unless we are explicitly
16978 looking up names in uninstantiated templates. Even then, we
16979 cannot look up the name if the scope is not a class type; it
16980 might, for example, be a template type parameter. */
16981 dependent_p = (TYPE_P (parser->scope)
16982 && !(parser->in_declarator_p
16983 && currently_open_class (parser->scope))
16984 && dependent_type_p (parser->scope));
16985 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16992 /* The resolution to Core Issue 180 says that `struct
16993 A::B' should be considered a type-name, even if `A'
16995 type = make_typename_type (parser->scope, name, tag_type,
16996 /*complain=*/tf_error);
16997 decl = TYPE_NAME (type);
16999 else if (is_template
17000 && (cp_parser_next_token_ends_template_argument_p (parser)
17001 || cp_lexer_next_token_is (parser->lexer,
17003 decl = make_unbound_class_template (parser->scope,
17005 /*complain=*/tf_error);
17007 decl = build_qualified_name (/*type=*/NULL_TREE,
17008 parser->scope, name,
17013 tree pushed_scope = NULL_TREE;
17015 /* If PARSER->SCOPE is a dependent type, then it must be a
17016 class type, and we must not be checking dependencies;
17017 otherwise, we would have processed this lookup above. So
17018 that PARSER->SCOPE is not considered a dependent base by
17019 lookup_member, we must enter the scope here. */
17021 pushed_scope = push_scope (parser->scope);
17022 /* If the PARSER->SCOPE is a template specialization, it
17023 may be instantiated during name lookup. In that case,
17024 errors may be issued. Even if we rollback the current
17025 tentative parse, those errors are valid. */
17026 decl = lookup_qualified_name (parser->scope, name,
17027 tag_type != none_type,
17028 /*complain=*/true);
17030 /* If we have a single function from a using decl, pull it out. */
17032 && TREE_CODE (decl) == OVERLOAD
17033 && !really_overloaded_fn (decl))
17034 decl = OVL_FUNCTION (decl);
17037 pop_scope (pushed_scope);
17039 parser->qualifying_scope = parser->scope;
17040 parser->object_scope = NULL_TREE;
17042 else if (object_type)
17044 tree object_decl = NULL_TREE;
17045 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17046 OBJECT_TYPE is not a class. */
17047 if (CLASS_TYPE_P (object_type))
17048 /* If the OBJECT_TYPE is a template specialization, it may
17049 be instantiated during name lookup. In that case, errors
17050 may be issued. Even if we rollback the current tentative
17051 parse, those errors are valid. */
17052 object_decl = lookup_member (object_type,
17055 tag_type != none_type);
17056 /* Look it up in the enclosing context, too. */
17057 decl = lookup_name_real (name, tag_type != none_type,
17059 /*block_p=*/true, is_namespace, flags);
17060 parser->object_scope = object_type;
17061 parser->qualifying_scope = NULL_TREE;
17063 decl = object_decl;
17067 decl = lookup_name_real (name, tag_type != none_type,
17069 /*block_p=*/true, is_namespace, flags);
17070 parser->qualifying_scope = NULL_TREE;
17071 parser->object_scope = NULL_TREE;
17074 /* If the lookup failed, let our caller know. */
17075 if (!decl || decl == error_mark_node)
17076 return error_mark_node;
17078 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17079 if (TREE_CODE (decl) == TREE_LIST)
17081 if (ambiguous_decls)
17082 *ambiguous_decls = decl;
17083 /* The error message we have to print is too complicated for
17084 cp_parser_error, so we incorporate its actions directly. */
17085 if (!cp_parser_simulate_error (parser))
17087 error ("%Hreference to %qD is ambiguous",
17088 &name_location, name);
17089 print_candidates (decl);
17091 return error_mark_node;
17094 gcc_assert (DECL_P (decl)
17095 || TREE_CODE (decl) == OVERLOAD
17096 || TREE_CODE (decl) == SCOPE_REF
17097 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17098 || BASELINK_P (decl));
17100 /* If we have resolved the name of a member declaration, check to
17101 see if the declaration is accessible. When the name resolves to
17102 set of overloaded functions, accessibility is checked when
17103 overload resolution is done.
17105 During an explicit instantiation, access is not checked at all,
17106 as per [temp.explicit]. */
17108 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17113 /* Like cp_parser_lookup_name, but for use in the typical case where
17114 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17115 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17118 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17120 return cp_parser_lookup_name (parser, name,
17122 /*is_template=*/false,
17123 /*is_namespace=*/false,
17124 /*check_dependency=*/true,
17125 /*ambiguous_decls=*/NULL,
17129 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17130 the current context, return the TYPE_DECL. If TAG_NAME_P is
17131 true, the DECL indicates the class being defined in a class-head,
17132 or declared in an elaborated-type-specifier.
17134 Otherwise, return DECL. */
17137 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17139 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17140 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17143 template <typename T> struct B;
17146 template <typename T> struct A::B {};
17148 Similarly, in an elaborated-type-specifier:
17150 namespace N { struct X{}; }
17153 template <typename T> friend struct N::X;
17156 However, if the DECL refers to a class type, and we are in
17157 the scope of the class, then the name lookup automatically
17158 finds the TYPE_DECL created by build_self_reference rather
17159 than a TEMPLATE_DECL. For example, in:
17161 template <class T> struct S {
17165 there is no need to handle such case. */
17167 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17168 return DECL_TEMPLATE_RESULT (decl);
17173 /* If too many, or too few, template-parameter lists apply to the
17174 declarator, issue an error message. Returns TRUE if all went well,
17175 and FALSE otherwise. */
17178 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17179 cp_declarator *declarator,
17180 location_t declarator_location)
17182 unsigned num_templates;
17184 /* We haven't seen any classes that involve template parameters yet. */
17187 switch (declarator->kind)
17190 if (declarator->u.id.qualifying_scope)
17195 scope = declarator->u.id.qualifying_scope;
17196 member = declarator->u.id.unqualified_name;
17198 while (scope && CLASS_TYPE_P (scope))
17200 /* You're supposed to have one `template <...>'
17201 for every template class, but you don't need one
17202 for a full specialization. For example:
17204 template <class T> struct S{};
17205 template <> struct S<int> { void f(); };
17206 void S<int>::f () {}
17208 is correct; there shouldn't be a `template <>' for
17209 the definition of `S<int>::f'. */
17210 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17211 /* If SCOPE does not have template information of any
17212 kind, then it is not a template, nor is it nested
17213 within a template. */
17215 if (explicit_class_specialization_p (scope))
17217 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17220 scope = TYPE_CONTEXT (scope);
17223 else if (TREE_CODE (declarator->u.id.unqualified_name)
17224 == TEMPLATE_ID_EXPR)
17225 /* If the DECLARATOR has the form `X<y>' then it uses one
17226 additional level of template parameters. */
17229 return cp_parser_check_template_parameters (parser,
17231 declarator_location);
17236 case cdk_reference:
17238 return (cp_parser_check_declarator_template_parameters
17239 (parser, declarator->declarator, declarator_location));
17245 gcc_unreachable ();
17250 /* NUM_TEMPLATES were used in the current declaration. If that is
17251 invalid, return FALSE and issue an error messages. Otherwise,
17255 cp_parser_check_template_parameters (cp_parser* parser,
17256 unsigned num_templates,
17257 location_t location)
17259 /* If there are more template classes than parameter lists, we have
17262 template <class T> void S<T>::R<T>::f (); */
17263 if (parser->num_template_parameter_lists < num_templates)
17265 error ("%Htoo few template-parameter-lists", &location);
17268 /* If there are the same number of template classes and parameter
17269 lists, that's OK. */
17270 if (parser->num_template_parameter_lists == num_templates)
17272 /* If there are more, but only one more, then we are referring to a
17273 member template. That's OK too. */
17274 if (parser->num_template_parameter_lists == num_templates + 1)
17276 /* Otherwise, there are too many template parameter lists. We have
17279 template <class T> template <class U> void S::f(); */
17280 error ("%Htoo many template-parameter-lists", &location);
17284 /* Parse an optional `::' token indicating that the following name is
17285 from the global namespace. If so, PARSER->SCOPE is set to the
17286 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17287 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17288 Returns the new value of PARSER->SCOPE, if the `::' token is
17289 present, and NULL_TREE otherwise. */
17292 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17296 /* Peek at the next token. */
17297 token = cp_lexer_peek_token (parser->lexer);
17298 /* If we're looking at a `::' token then we're starting from the
17299 global namespace, not our current location. */
17300 if (token->type == CPP_SCOPE)
17302 /* Consume the `::' token. */
17303 cp_lexer_consume_token (parser->lexer);
17304 /* Set the SCOPE so that we know where to start the lookup. */
17305 parser->scope = global_namespace;
17306 parser->qualifying_scope = global_namespace;
17307 parser->object_scope = NULL_TREE;
17309 return parser->scope;
17311 else if (!current_scope_valid_p)
17313 parser->scope = NULL_TREE;
17314 parser->qualifying_scope = NULL_TREE;
17315 parser->object_scope = NULL_TREE;
17321 /* Returns TRUE if the upcoming token sequence is the start of a
17322 constructor declarator. If FRIEND_P is true, the declarator is
17323 preceded by the `friend' specifier. */
17326 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17328 bool constructor_p;
17329 tree type_decl = NULL_TREE;
17330 bool nested_name_p;
17331 cp_token *next_token;
17333 /* The common case is that this is not a constructor declarator, so
17334 try to avoid doing lots of work if at all possible. It's not
17335 valid declare a constructor at function scope. */
17336 if (parser->in_function_body)
17338 /* And only certain tokens can begin a constructor declarator. */
17339 next_token = cp_lexer_peek_token (parser->lexer);
17340 if (next_token->type != CPP_NAME
17341 && next_token->type != CPP_SCOPE
17342 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17343 && next_token->type != CPP_TEMPLATE_ID)
17346 /* Parse tentatively; we are going to roll back all of the tokens
17348 cp_parser_parse_tentatively (parser);
17349 /* Assume that we are looking at a constructor declarator. */
17350 constructor_p = true;
17352 /* Look for the optional `::' operator. */
17353 cp_parser_global_scope_opt (parser,
17354 /*current_scope_valid_p=*/false);
17355 /* Look for the nested-name-specifier. */
17357 = (cp_parser_nested_name_specifier_opt (parser,
17358 /*typename_keyword_p=*/false,
17359 /*check_dependency_p=*/false,
17361 /*is_declaration=*/false)
17363 /* Outside of a class-specifier, there must be a
17364 nested-name-specifier. */
17365 if (!nested_name_p &&
17366 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17368 constructor_p = false;
17369 /* If we still think that this might be a constructor-declarator,
17370 look for a class-name. */
17375 template <typename T> struct S { S(); };
17376 template <typename T> S<T>::S ();
17378 we must recognize that the nested `S' names a class.
17381 template <typename T> S<T>::S<T> ();
17383 we must recognize that the nested `S' names a template. */
17384 type_decl = cp_parser_class_name (parser,
17385 /*typename_keyword_p=*/false,
17386 /*template_keyword_p=*/false,
17388 /*check_dependency_p=*/false,
17389 /*class_head_p=*/false,
17390 /*is_declaration=*/false);
17391 /* If there was no class-name, then this is not a constructor. */
17392 constructor_p = !cp_parser_error_occurred (parser);
17395 /* If we're still considering a constructor, we have to see a `(',
17396 to begin the parameter-declaration-clause, followed by either a
17397 `)', an `...', or a decl-specifier. We need to check for a
17398 type-specifier to avoid being fooled into thinking that:
17402 is a constructor. (It is actually a function named `f' that
17403 takes one parameter (of type `int') and returns a value of type
17406 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17409 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17410 /* A parameter declaration begins with a decl-specifier,
17411 which is either the "attribute" keyword, a storage class
17412 specifier, or (usually) a type-specifier. */
17413 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17416 tree pushed_scope = NULL_TREE;
17417 unsigned saved_num_template_parameter_lists;
17419 /* Names appearing in the type-specifier should be looked up
17420 in the scope of the class. */
17421 if (current_class_type)
17425 type = TREE_TYPE (type_decl);
17426 if (TREE_CODE (type) == TYPENAME_TYPE)
17428 type = resolve_typename_type (type,
17429 /*only_current_p=*/false);
17430 if (TREE_CODE (type) == TYPENAME_TYPE)
17432 cp_parser_abort_tentative_parse (parser);
17436 pushed_scope = push_scope (type);
17439 /* Inside the constructor parameter list, surrounding
17440 template-parameter-lists do not apply. */
17441 saved_num_template_parameter_lists
17442 = parser->num_template_parameter_lists;
17443 parser->num_template_parameter_lists = 0;
17445 /* Look for the type-specifier. */
17446 cp_parser_type_specifier (parser,
17447 CP_PARSER_FLAGS_NONE,
17448 /*decl_specs=*/NULL,
17449 /*is_declarator=*/true,
17450 /*declares_class_or_enum=*/NULL,
17451 /*is_cv_qualifier=*/NULL);
17453 parser->num_template_parameter_lists
17454 = saved_num_template_parameter_lists;
17456 /* Leave the scope of the class. */
17458 pop_scope (pushed_scope);
17460 constructor_p = !cp_parser_error_occurred (parser);
17464 constructor_p = false;
17465 /* We did not really want to consume any tokens. */
17466 cp_parser_abort_tentative_parse (parser);
17468 return constructor_p;
17471 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17472 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17473 they must be performed once we are in the scope of the function.
17475 Returns the function defined. */
17478 cp_parser_function_definition_from_specifiers_and_declarator
17479 (cp_parser* parser,
17480 cp_decl_specifier_seq *decl_specifiers,
17482 const cp_declarator *declarator)
17487 /* Begin the function-definition. */
17488 success_p = start_function (decl_specifiers, declarator, attributes);
17490 /* The things we're about to see are not directly qualified by any
17491 template headers we've seen thus far. */
17492 reset_specialization ();
17494 /* If there were names looked up in the decl-specifier-seq that we
17495 did not check, check them now. We must wait until we are in the
17496 scope of the function to perform the checks, since the function
17497 might be a friend. */
17498 perform_deferred_access_checks ();
17502 /* Skip the entire function. */
17503 cp_parser_skip_to_end_of_block_or_statement (parser);
17504 fn = error_mark_node;
17506 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17508 /* Seen already, skip it. An error message has already been output. */
17509 cp_parser_skip_to_end_of_block_or_statement (parser);
17510 fn = current_function_decl;
17511 current_function_decl = NULL_TREE;
17512 /* If this is a function from a class, pop the nested class. */
17513 if (current_class_name)
17514 pop_nested_class ();
17517 fn = cp_parser_function_definition_after_declarator (parser,
17518 /*inline_p=*/false);
17523 /* Parse the part of a function-definition that follows the
17524 declarator. INLINE_P is TRUE iff this function is an inline
17525 function defined with a class-specifier.
17527 Returns the function defined. */
17530 cp_parser_function_definition_after_declarator (cp_parser* parser,
17534 bool ctor_initializer_p = false;
17535 bool saved_in_unbraced_linkage_specification_p;
17536 bool saved_in_function_body;
17537 unsigned saved_num_template_parameter_lists;
17540 saved_in_function_body = parser->in_function_body;
17541 parser->in_function_body = true;
17542 /* If the next token is `return', then the code may be trying to
17543 make use of the "named return value" extension that G++ used to
17545 token = cp_lexer_peek_token (parser->lexer);
17546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17548 /* Consume the `return' keyword. */
17549 cp_lexer_consume_token (parser->lexer);
17550 /* Look for the identifier that indicates what value is to be
17552 cp_parser_identifier (parser);
17553 /* Issue an error message. */
17554 error ("%Hnamed return values are no longer supported",
17556 /* Skip tokens until we reach the start of the function body. */
17559 cp_token *token = cp_lexer_peek_token (parser->lexer);
17560 if (token->type == CPP_OPEN_BRACE
17561 || token->type == CPP_EOF
17562 || token->type == CPP_PRAGMA_EOL)
17564 cp_lexer_consume_token (parser->lexer);
17567 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17568 anything declared inside `f'. */
17569 saved_in_unbraced_linkage_specification_p
17570 = parser->in_unbraced_linkage_specification_p;
17571 parser->in_unbraced_linkage_specification_p = false;
17572 /* Inside the function, surrounding template-parameter-lists do not
17574 saved_num_template_parameter_lists
17575 = parser->num_template_parameter_lists;
17576 parser->num_template_parameter_lists = 0;
17577 /* If the next token is `try', then we are looking at a
17578 function-try-block. */
17579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17580 ctor_initializer_p = cp_parser_function_try_block (parser);
17581 /* A function-try-block includes the function-body, so we only do
17582 this next part if we're not processing a function-try-block. */
17585 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17587 /* Finish the function. */
17588 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17589 (inline_p ? 2 : 0));
17590 /* Generate code for it, if necessary. */
17591 expand_or_defer_fn (fn);
17592 /* Restore the saved values. */
17593 parser->in_unbraced_linkage_specification_p
17594 = saved_in_unbraced_linkage_specification_p;
17595 parser->num_template_parameter_lists
17596 = saved_num_template_parameter_lists;
17597 parser->in_function_body = saved_in_function_body;
17602 /* Parse a template-declaration, assuming that the `export' (and
17603 `extern') keywords, if present, has already been scanned. MEMBER_P
17604 is as for cp_parser_template_declaration. */
17607 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17609 tree decl = NULL_TREE;
17610 VEC (deferred_access_check,gc) *checks;
17611 tree parameter_list;
17612 bool friend_p = false;
17613 bool need_lang_pop;
17616 /* Look for the `template' keyword. */
17617 token = cp_lexer_peek_token (parser->lexer);
17618 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17622 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17624 if (at_class_scope_p () && current_function_decl)
17626 /* 14.5.2.2 [temp.mem]
17628 A local class shall not have member templates. */
17629 error ("%Hinvalid declaration of member template in local class",
17631 cp_parser_skip_to_end_of_block_or_statement (parser);
17636 A template ... shall not have C linkage. */
17637 if (current_lang_name == lang_name_c)
17639 error ("%Htemplate with C linkage", &token->location);
17640 /* Give it C++ linkage to avoid confusing other parts of the
17642 push_lang_context (lang_name_cplusplus);
17643 need_lang_pop = true;
17646 need_lang_pop = false;
17648 /* We cannot perform access checks on the template parameter
17649 declarations until we know what is being declared, just as we
17650 cannot check the decl-specifier list. */
17651 push_deferring_access_checks (dk_deferred);
17653 /* If the next token is `>', then we have an invalid
17654 specialization. Rather than complain about an invalid template
17655 parameter, issue an error message here. */
17656 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17658 cp_parser_error (parser, "invalid explicit specialization");
17659 begin_specialization ();
17660 parameter_list = NULL_TREE;
17663 /* Parse the template parameters. */
17664 parameter_list = cp_parser_template_parameter_list (parser);
17666 /* Get the deferred access checks from the parameter list. These
17667 will be checked once we know what is being declared, as for a
17668 member template the checks must be performed in the scope of the
17669 class containing the member. */
17670 checks = get_deferred_access_checks ();
17672 /* Look for the `>'. */
17673 cp_parser_skip_to_end_of_template_parameter_list (parser);
17674 /* We just processed one more parameter list. */
17675 ++parser->num_template_parameter_lists;
17676 /* If the next token is `template', there are more template
17678 if (cp_lexer_next_token_is_keyword (parser->lexer,
17680 cp_parser_template_declaration_after_export (parser, member_p);
17683 /* There are no access checks when parsing a template, as we do not
17684 know if a specialization will be a friend. */
17685 push_deferring_access_checks (dk_no_check);
17686 token = cp_lexer_peek_token (parser->lexer);
17687 decl = cp_parser_single_declaration (parser,
17690 /*explicit_specialization_p=*/false,
17692 pop_deferring_access_checks ();
17694 /* If this is a member template declaration, let the front
17696 if (member_p && !friend_p && decl)
17698 if (TREE_CODE (decl) == TYPE_DECL)
17699 cp_parser_check_access_in_redeclaration (decl, token->location);
17701 decl = finish_member_template_decl (decl);
17703 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17704 make_friend_class (current_class_type, TREE_TYPE (decl),
17705 /*complain=*/true);
17707 /* We are done with the current parameter list. */
17708 --parser->num_template_parameter_lists;
17710 pop_deferring_access_checks ();
17713 finish_template_decl (parameter_list);
17715 /* Register member declarations. */
17716 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17717 finish_member_declaration (decl);
17718 /* For the erroneous case of a template with C linkage, we pushed an
17719 implicit C++ linkage scope; exit that scope now. */
17721 pop_lang_context ();
17722 /* If DECL is a function template, we must return to parse it later.
17723 (Even though there is no definition, there might be default
17724 arguments that need handling.) */
17725 if (member_p && decl
17726 && (TREE_CODE (decl) == FUNCTION_DECL
17727 || DECL_FUNCTION_TEMPLATE_P (decl)))
17728 TREE_VALUE (parser->unparsed_functions_queues)
17729 = tree_cons (NULL_TREE, decl,
17730 TREE_VALUE (parser->unparsed_functions_queues));
17733 /* Perform the deferred access checks from a template-parameter-list.
17734 CHECKS is a TREE_LIST of access checks, as returned by
17735 get_deferred_access_checks. */
17738 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17740 ++processing_template_parmlist;
17741 perform_access_checks (checks);
17742 --processing_template_parmlist;
17745 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17746 `function-definition' sequence. MEMBER_P is true, this declaration
17747 appears in a class scope.
17749 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17750 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17753 cp_parser_single_declaration (cp_parser* parser,
17754 VEC (deferred_access_check,gc)* checks,
17756 bool explicit_specialization_p,
17759 int declares_class_or_enum;
17760 tree decl = NULL_TREE;
17761 cp_decl_specifier_seq decl_specifiers;
17762 bool function_definition_p = false;
17763 cp_token *decl_spec_token_start;
17765 /* This function is only used when processing a template
17767 gcc_assert (innermost_scope_kind () == sk_template_parms
17768 || innermost_scope_kind () == sk_template_spec);
17770 /* Defer access checks until we know what is being declared. */
17771 push_deferring_access_checks (dk_deferred);
17773 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17775 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17776 cp_parser_decl_specifier_seq (parser,
17777 CP_PARSER_FLAGS_OPTIONAL,
17779 &declares_class_or_enum);
17781 *friend_p = cp_parser_friend_p (&decl_specifiers);
17783 /* There are no template typedefs. */
17784 if (decl_specifiers.specs[(int) ds_typedef])
17786 error ("%Htemplate declaration of %qs",
17787 &decl_spec_token_start->location, "typedef");
17788 decl = error_mark_node;
17791 /* Gather up the access checks that occurred the
17792 decl-specifier-seq. */
17793 stop_deferring_access_checks ();
17795 /* Check for the declaration of a template class. */
17796 if (declares_class_or_enum)
17798 if (cp_parser_declares_only_class_p (parser))
17800 decl = shadow_tag (&decl_specifiers);
17805 friend template <typename T> struct A<T>::B;
17808 A<T>::B will be represented by a TYPENAME_TYPE, and
17809 therefore not recognized by shadow_tag. */
17810 if (friend_p && *friend_p
17812 && decl_specifiers.type
17813 && TYPE_P (decl_specifiers.type))
17814 decl = decl_specifiers.type;
17816 if (decl && decl != error_mark_node)
17817 decl = TYPE_NAME (decl);
17819 decl = error_mark_node;
17821 /* Perform access checks for template parameters. */
17822 cp_parser_perform_template_parameter_access_checks (checks);
17825 /* If it's not a template class, try for a template function. If
17826 the next token is a `;', then this declaration does not declare
17827 anything. But, if there were errors in the decl-specifiers, then
17828 the error might well have come from an attempted class-specifier.
17829 In that case, there's no need to warn about a missing declarator. */
17831 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17832 || decl_specifiers.type != error_mark_node))
17834 decl = cp_parser_init_declarator (parser,
17837 /*function_definition_allowed_p=*/true,
17839 declares_class_or_enum,
17840 &function_definition_p);
17842 /* 7.1.1-1 [dcl.stc]
17844 A storage-class-specifier shall not be specified in an explicit
17845 specialization... */
17847 && explicit_specialization_p
17848 && decl_specifiers.storage_class != sc_none)
17850 error ("%Hexplicit template specialization cannot have a storage class",
17851 &decl_spec_token_start->location);
17852 decl = error_mark_node;
17856 pop_deferring_access_checks ();
17858 /* Clear any current qualification; whatever comes next is the start
17859 of something new. */
17860 parser->scope = NULL_TREE;
17861 parser->qualifying_scope = NULL_TREE;
17862 parser->object_scope = NULL_TREE;
17863 /* Look for a trailing `;' after the declaration. */
17864 if (!function_definition_p
17865 && (decl == error_mark_node
17866 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17867 cp_parser_skip_to_end_of_block_or_statement (parser);
17872 /* Parse a cast-expression that is not the operand of a unary "&". */
17875 cp_parser_simple_cast_expression (cp_parser *parser)
17877 return cp_parser_cast_expression (parser, /*address_p=*/false,
17881 /* Parse a functional cast to TYPE. Returns an expression
17882 representing the cast. */
17885 cp_parser_functional_cast (cp_parser* parser, tree type)
17887 tree expression_list;
17891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17893 maybe_warn_cpp0x ("extended initializer lists");
17894 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17895 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17896 if (TREE_CODE (type) == TYPE_DECL)
17897 type = TREE_TYPE (type);
17898 return finish_compound_literal (type, expression_list);
17902 = cp_parser_parenthesized_expression_list (parser, false,
17904 /*allow_expansion_p=*/true,
17905 /*non_constant_p=*/NULL);
17907 cast = build_functional_cast (type, expression_list,
17908 tf_warning_or_error);
17909 /* [expr.const]/1: In an integral constant expression "only type
17910 conversions to integral or enumeration type can be used". */
17911 if (TREE_CODE (type) == TYPE_DECL)
17912 type = TREE_TYPE (type);
17913 if (cast != error_mark_node
17914 && !cast_valid_in_integral_constant_expression_p (type)
17915 && (cp_parser_non_integral_constant_expression
17916 (parser, "a call to a constructor")))
17917 return error_mark_node;
17921 /* Save the tokens that make up the body of a member function defined
17922 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17923 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17924 specifiers applied to the declaration. Returns the FUNCTION_DECL
17925 for the member function. */
17928 cp_parser_save_member_function_body (cp_parser* parser,
17929 cp_decl_specifier_seq *decl_specifiers,
17930 cp_declarator *declarator,
17937 /* Create the function-declaration. */
17938 fn = start_method (decl_specifiers, declarator, attributes);
17939 /* If something went badly wrong, bail out now. */
17940 if (fn == error_mark_node)
17942 /* If there's a function-body, skip it. */
17943 if (cp_parser_token_starts_function_definition_p
17944 (cp_lexer_peek_token (parser->lexer)))
17945 cp_parser_skip_to_end_of_block_or_statement (parser);
17946 return error_mark_node;
17949 /* Remember it, if there default args to post process. */
17950 cp_parser_save_default_args (parser, fn);
17952 /* Save away the tokens that make up the body of the
17954 first = parser->lexer->next_token;
17955 /* We can have braced-init-list mem-initializers before the fn body. */
17956 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17958 cp_lexer_consume_token (parser->lexer);
17959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17960 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17962 /* cache_group will stop after an un-nested { } pair, too. */
17963 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17966 /* variadic mem-inits have ... after the ')'. */
17967 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17968 cp_lexer_consume_token (parser->lexer);
17971 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17972 /* Handle function try blocks. */
17973 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17974 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17975 last = parser->lexer->next_token;
17977 /* Save away the inline definition; we will process it when the
17978 class is complete. */
17979 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17980 DECL_PENDING_INLINE_P (fn) = 1;
17982 /* We need to know that this was defined in the class, so that
17983 friend templates are handled correctly. */
17984 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17986 /* We're done with the inline definition. */
17987 finish_method (fn);
17989 /* Add FN to the queue of functions to be parsed later. */
17990 TREE_VALUE (parser->unparsed_functions_queues)
17991 = tree_cons (NULL_TREE, fn,
17992 TREE_VALUE (parser->unparsed_functions_queues));
17997 /* Parse a template-argument-list, as well as the trailing ">" (but
17998 not the opening ">"). See cp_parser_template_argument_list for the
18002 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18006 tree saved_qualifying_scope;
18007 tree saved_object_scope;
18008 bool saved_greater_than_is_operator_p;
18009 bool saved_skip_evaluation;
18013 When parsing a template-id, the first non-nested `>' is taken as
18014 the end of the template-argument-list rather than a greater-than
18016 saved_greater_than_is_operator_p
18017 = parser->greater_than_is_operator_p;
18018 parser->greater_than_is_operator_p = false;
18019 /* Parsing the argument list may modify SCOPE, so we save it
18021 saved_scope = parser->scope;
18022 saved_qualifying_scope = parser->qualifying_scope;
18023 saved_object_scope = parser->object_scope;
18024 /* We need to evaluate the template arguments, even though this
18025 template-id may be nested within a "sizeof". */
18026 saved_skip_evaluation = skip_evaluation;
18027 skip_evaluation = false;
18028 /* Parse the template-argument-list itself. */
18029 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18030 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18031 arguments = NULL_TREE;
18033 arguments = cp_parser_template_argument_list (parser);
18034 /* Look for the `>' that ends the template-argument-list. If we find
18035 a '>>' instead, it's probably just a typo. */
18036 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18038 if (cxx_dialect != cxx98)
18040 /* In C++0x, a `>>' in a template argument list or cast
18041 expression is considered to be two separate `>'
18042 tokens. So, change the current token to a `>', but don't
18043 consume it: it will be consumed later when the outer
18044 template argument list (or cast expression) is parsed.
18045 Note that this replacement of `>' for `>>' is necessary
18046 even if we are parsing tentatively: in the tentative
18047 case, after calling
18048 cp_parser_enclosed_template_argument_list we will always
18049 throw away all of the template arguments and the first
18050 closing `>', either because the template argument list
18051 was erroneous or because we are replacing those tokens
18052 with a CPP_TEMPLATE_ID token. The second `>' (which will
18053 not have been thrown away) is needed either to close an
18054 outer template argument list or to complete a new-style
18056 cp_token *token = cp_lexer_peek_token (parser->lexer);
18057 token->type = CPP_GREATER;
18059 else if (!saved_greater_than_is_operator_p)
18061 /* If we're in a nested template argument list, the '>>' has
18062 to be a typo for '> >'. We emit the error message, but we
18063 continue parsing and we push a '>' as next token, so that
18064 the argument list will be parsed correctly. Note that the
18065 global source location is still on the token before the
18066 '>>', so we need to say explicitly where we want it. */
18067 cp_token *token = cp_lexer_peek_token (parser->lexer);
18068 error ("%H%<>>%> should be %<> >%> "
18069 "within a nested template argument list",
18072 token->type = CPP_GREATER;
18076 /* If this is not a nested template argument list, the '>>'
18077 is a typo for '>'. Emit an error message and continue.
18078 Same deal about the token location, but here we can get it
18079 right by consuming the '>>' before issuing the diagnostic. */
18080 cp_token *token = cp_lexer_consume_token (parser->lexer);
18081 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18082 "a template argument list", &token->location);
18086 cp_parser_skip_to_end_of_template_parameter_list (parser);
18087 /* The `>' token might be a greater-than operator again now. */
18088 parser->greater_than_is_operator_p
18089 = saved_greater_than_is_operator_p;
18090 /* Restore the SAVED_SCOPE. */
18091 parser->scope = saved_scope;
18092 parser->qualifying_scope = saved_qualifying_scope;
18093 parser->object_scope = saved_object_scope;
18094 skip_evaluation = saved_skip_evaluation;
18099 /* MEMBER_FUNCTION is a member function, or a friend. If default
18100 arguments, or the body of the function have not yet been parsed,
18104 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18106 /* If this member is a template, get the underlying
18108 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18109 member_function = DECL_TEMPLATE_RESULT (member_function);
18111 /* There should not be any class definitions in progress at this
18112 point; the bodies of members are only parsed outside of all class
18114 gcc_assert (parser->num_classes_being_defined == 0);
18115 /* While we're parsing the member functions we might encounter more
18116 classes. We want to handle them right away, but we don't want
18117 them getting mixed up with functions that are currently in the
18119 parser->unparsed_functions_queues
18120 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18122 /* Make sure that any template parameters are in scope. */
18123 maybe_begin_member_template_processing (member_function);
18125 /* If the body of the function has not yet been parsed, parse it
18127 if (DECL_PENDING_INLINE_P (member_function))
18129 tree function_scope;
18130 cp_token_cache *tokens;
18132 /* The function is no longer pending; we are processing it. */
18133 tokens = DECL_PENDING_INLINE_INFO (member_function);
18134 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18135 DECL_PENDING_INLINE_P (member_function) = 0;
18137 /* If this is a local class, enter the scope of the containing
18139 function_scope = current_function_decl;
18140 if (function_scope)
18141 push_function_context ();
18143 /* Push the body of the function onto the lexer stack. */
18144 cp_parser_push_lexer_for_tokens (parser, tokens);
18146 /* Let the front end know that we going to be defining this
18148 start_preparsed_function (member_function, NULL_TREE,
18149 SF_PRE_PARSED | SF_INCLASS_INLINE);
18151 /* Don't do access checking if it is a templated function. */
18152 if (processing_template_decl)
18153 push_deferring_access_checks (dk_no_check);
18155 /* Now, parse the body of the function. */
18156 cp_parser_function_definition_after_declarator (parser,
18157 /*inline_p=*/true);
18159 if (processing_template_decl)
18160 pop_deferring_access_checks ();
18162 /* Leave the scope of the containing function. */
18163 if (function_scope)
18164 pop_function_context ();
18165 cp_parser_pop_lexer (parser);
18168 /* Remove any template parameters from the symbol table. */
18169 maybe_end_member_template_processing ();
18171 /* Restore the queue. */
18172 parser->unparsed_functions_queues
18173 = TREE_CHAIN (parser->unparsed_functions_queues);
18176 /* If DECL contains any default args, remember it on the unparsed
18177 functions queue. */
18180 cp_parser_save_default_args (cp_parser* parser, tree decl)
18184 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18186 probe = TREE_CHAIN (probe))
18187 if (TREE_PURPOSE (probe))
18189 TREE_PURPOSE (parser->unparsed_functions_queues)
18190 = tree_cons (current_class_type, decl,
18191 TREE_PURPOSE (parser->unparsed_functions_queues));
18196 /* FN is a FUNCTION_DECL which may contains a parameter with an
18197 unparsed DEFAULT_ARG. Parse the default args now. This function
18198 assumes that the current scope is the scope in which the default
18199 argument should be processed. */
18202 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18204 bool saved_local_variables_forbidden_p;
18207 /* While we're parsing the default args, we might (due to the
18208 statement expression extension) encounter more classes. We want
18209 to handle them right away, but we don't want them getting mixed
18210 up with default args that are currently in the queue. */
18211 parser->unparsed_functions_queues
18212 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18214 /* Local variable names (and the `this' keyword) may not appear
18215 in a default argument. */
18216 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18217 parser->local_variables_forbidden_p = true;
18219 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18221 parm = TREE_CHAIN (parm))
18223 cp_token_cache *tokens;
18224 tree default_arg = TREE_PURPOSE (parm);
18226 VEC(tree,gc) *insts;
18233 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18234 /* This can happen for a friend declaration for a function
18235 already declared with default arguments. */
18238 /* Push the saved tokens for the default argument onto the parser's
18240 tokens = DEFARG_TOKENS (default_arg);
18241 cp_parser_push_lexer_for_tokens (parser, tokens);
18243 /* Parse the assignment-expression. */
18244 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18246 if (!processing_template_decl)
18247 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18249 TREE_PURPOSE (parm) = parsed_arg;
18251 /* Update any instantiations we've already created. */
18252 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18253 VEC_iterate (tree, insts, ix, copy); ix++)
18254 TREE_PURPOSE (copy) = parsed_arg;
18256 /* If the token stream has not been completely used up, then
18257 there was extra junk after the end of the default
18259 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18260 cp_parser_error (parser, "expected %<,%>");
18262 /* Revert to the main lexer. */
18263 cp_parser_pop_lexer (parser);
18266 /* Make sure no default arg is missing. */
18267 check_default_args (fn);
18269 /* Restore the state of local_variables_forbidden_p. */
18270 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18272 /* Restore the queue. */
18273 parser->unparsed_functions_queues
18274 = TREE_CHAIN (parser->unparsed_functions_queues);
18277 /* Parse the operand of `sizeof' (or a similar operator). Returns
18278 either a TYPE or an expression, depending on the form of the
18279 input. The KEYWORD indicates which kind of expression we have
18283 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18285 tree expr = NULL_TREE;
18286 const char *saved_message;
18288 bool saved_integral_constant_expression_p;
18289 bool saved_non_integral_constant_expression_p;
18290 bool pack_expansion_p = false;
18292 /* Types cannot be defined in a `sizeof' expression. Save away the
18294 saved_message = parser->type_definition_forbidden_message;
18295 /* And create the new one. */
18296 tmp = concat ("types may not be defined in %<",
18297 IDENTIFIER_POINTER (ridpointers[keyword]),
18298 "%> expressions", NULL);
18299 parser->type_definition_forbidden_message = tmp;
18301 /* The restrictions on constant-expressions do not apply inside
18302 sizeof expressions. */
18303 saved_integral_constant_expression_p
18304 = parser->integral_constant_expression_p;
18305 saved_non_integral_constant_expression_p
18306 = parser->non_integral_constant_expression_p;
18307 parser->integral_constant_expression_p = false;
18309 /* If it's a `...', then we are computing the length of a parameter
18311 if (keyword == RID_SIZEOF
18312 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18314 /* Consume the `...'. */
18315 cp_lexer_consume_token (parser->lexer);
18316 maybe_warn_variadic_templates ();
18318 /* Note that this is an expansion. */
18319 pack_expansion_p = true;
18322 /* Do not actually evaluate the expression. */
18324 /* If it's a `(', then we might be looking at the type-id
18326 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18329 bool saved_in_type_id_in_expr_p;
18331 /* We can't be sure yet whether we're looking at a type-id or an
18333 cp_parser_parse_tentatively (parser);
18334 /* Consume the `('. */
18335 cp_lexer_consume_token (parser->lexer);
18336 /* Parse the type-id. */
18337 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18338 parser->in_type_id_in_expr_p = true;
18339 type = cp_parser_type_id (parser);
18340 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18341 /* Now, look for the trailing `)'. */
18342 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18343 /* If all went well, then we're done. */
18344 if (cp_parser_parse_definitely (parser))
18346 cp_decl_specifier_seq decl_specs;
18348 /* Build a trivial decl-specifier-seq. */
18349 clear_decl_specs (&decl_specs);
18350 decl_specs.type = type;
18352 /* Call grokdeclarator to figure out what type this is. */
18353 expr = grokdeclarator (NULL,
18357 /*attrlist=*/NULL);
18361 /* If the type-id production did not work out, then we must be
18362 looking at the unary-expression production. */
18364 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18367 if (pack_expansion_p)
18368 /* Build a pack expansion. */
18369 expr = make_pack_expansion (expr);
18371 /* Go back to evaluating expressions. */
18374 /* Free the message we created. */
18376 /* And restore the old one. */
18377 parser->type_definition_forbidden_message = saved_message;
18378 parser->integral_constant_expression_p
18379 = saved_integral_constant_expression_p;
18380 parser->non_integral_constant_expression_p
18381 = saved_non_integral_constant_expression_p;
18386 /* If the current declaration has no declarator, return true. */
18389 cp_parser_declares_only_class_p (cp_parser *parser)
18391 /* If the next token is a `;' or a `,' then there is no
18393 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18394 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18397 /* Update the DECL_SPECS to reflect the storage class indicated by
18401 cp_parser_set_storage_class (cp_parser *parser,
18402 cp_decl_specifier_seq *decl_specs,
18404 location_t location)
18406 cp_storage_class storage_class;
18408 if (parser->in_unbraced_linkage_specification_p)
18410 error ("%Hinvalid use of %qD in linkage specification",
18411 &location, ridpointers[keyword]);
18414 else if (decl_specs->storage_class != sc_none)
18416 decl_specs->conflicting_specifiers_p = true;
18420 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18421 && decl_specs->specs[(int) ds_thread])
18423 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18424 decl_specs->specs[(int) ds_thread] = 0;
18430 storage_class = sc_auto;
18433 storage_class = sc_register;
18436 storage_class = sc_static;
18439 storage_class = sc_extern;
18442 storage_class = sc_mutable;
18445 gcc_unreachable ();
18447 decl_specs->storage_class = storage_class;
18449 /* A storage class specifier cannot be applied alongside a typedef
18450 specifier. If there is a typedef specifier present then set
18451 conflicting_specifiers_p which will trigger an error later
18452 on in grokdeclarator. */
18453 if (decl_specs->specs[(int)ds_typedef])
18454 decl_specs->conflicting_specifiers_p = true;
18457 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18458 is true, the type is a user-defined type; otherwise it is a
18459 built-in type specified by a keyword. */
18462 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18464 location_t location,
18465 bool user_defined_p)
18467 decl_specs->any_specifiers_p = true;
18469 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18470 (with, for example, in "typedef int wchar_t;") we remember that
18471 this is what happened. In system headers, we ignore these
18472 declarations so that G++ can work with system headers that are not
18474 if (decl_specs->specs[(int) ds_typedef]
18476 && (type_spec == boolean_type_node
18477 || type_spec == char16_type_node
18478 || type_spec == char32_type_node
18479 || type_spec == wchar_type_node)
18480 && (decl_specs->type
18481 || decl_specs->specs[(int) ds_long]
18482 || decl_specs->specs[(int) ds_short]
18483 || decl_specs->specs[(int) ds_unsigned]
18484 || decl_specs->specs[(int) ds_signed]))
18486 decl_specs->redefined_builtin_type = type_spec;
18487 if (!decl_specs->type)
18489 decl_specs->type = type_spec;
18490 decl_specs->user_defined_type_p = false;
18491 decl_specs->type_location = location;
18494 else if (decl_specs->type)
18495 decl_specs->multiple_types_p = true;
18498 decl_specs->type = type_spec;
18499 decl_specs->user_defined_type_p = user_defined_p;
18500 decl_specs->redefined_builtin_type = NULL_TREE;
18501 decl_specs->type_location = location;
18505 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18506 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18509 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18511 return decl_specifiers->specs[(int) ds_friend] != 0;
18514 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18515 issue an error message indicating that TOKEN_DESC was expected.
18517 Returns the token consumed, if the token had the appropriate type.
18518 Otherwise, returns NULL. */
18521 cp_parser_require (cp_parser* parser,
18522 enum cpp_ttype type,
18523 const char* token_desc)
18525 if (cp_lexer_next_token_is (parser->lexer, type))
18526 return cp_lexer_consume_token (parser->lexer);
18529 /* Output the MESSAGE -- unless we're parsing tentatively. */
18530 if (!cp_parser_simulate_error (parser))
18532 char *message = concat ("expected ", token_desc, NULL);
18533 cp_parser_error (parser, message);
18540 /* An error message is produced if the next token is not '>'.
18541 All further tokens are skipped until the desired token is
18542 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18545 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18547 /* Current level of '< ... >'. */
18548 unsigned level = 0;
18549 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18550 unsigned nesting_depth = 0;
18552 /* Are we ready, yet? If not, issue error message. */
18553 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18556 /* Skip tokens until the desired token is found. */
18559 /* Peek at the next token. */
18560 switch (cp_lexer_peek_token (parser->lexer)->type)
18563 if (!nesting_depth)
18568 if (cxx_dialect == cxx98)
18569 /* C++0x views the `>>' operator as two `>' tokens, but
18572 else if (!nesting_depth && level-- == 0)
18574 /* We've hit a `>>' where the first `>' closes the
18575 template argument list, and the second `>' is
18576 spurious. Just consume the `>>' and stop; we've
18577 already produced at least one error. */
18578 cp_lexer_consume_token (parser->lexer);
18581 /* Fall through for C++0x, so we handle the second `>' in
18585 if (!nesting_depth && level-- == 0)
18587 /* We've reached the token we want, consume it and stop. */
18588 cp_lexer_consume_token (parser->lexer);
18593 case CPP_OPEN_PAREN:
18594 case CPP_OPEN_SQUARE:
18598 case CPP_CLOSE_PAREN:
18599 case CPP_CLOSE_SQUARE:
18600 if (nesting_depth-- == 0)
18605 case CPP_PRAGMA_EOL:
18606 case CPP_SEMICOLON:
18607 case CPP_OPEN_BRACE:
18608 case CPP_CLOSE_BRACE:
18609 /* The '>' was probably forgotten, don't look further. */
18616 /* Consume this token. */
18617 cp_lexer_consume_token (parser->lexer);
18621 /* If the next token is the indicated keyword, consume it. Otherwise,
18622 issue an error message indicating that TOKEN_DESC was expected.
18624 Returns the token consumed, if the token had the appropriate type.
18625 Otherwise, returns NULL. */
18628 cp_parser_require_keyword (cp_parser* parser,
18630 const char* token_desc)
18632 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18634 if (token && token->keyword != keyword)
18636 dyn_string_t error_msg;
18638 /* Format the error message. */
18639 error_msg = dyn_string_new (0);
18640 dyn_string_append_cstr (error_msg, "expected ");
18641 dyn_string_append_cstr (error_msg, token_desc);
18642 cp_parser_error (parser, error_msg->s);
18643 dyn_string_delete (error_msg);
18650 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18651 function-definition. */
18654 cp_parser_token_starts_function_definition_p (cp_token* token)
18656 return (/* An ordinary function-body begins with an `{'. */
18657 token->type == CPP_OPEN_BRACE
18658 /* A ctor-initializer begins with a `:'. */
18659 || token->type == CPP_COLON
18660 /* A function-try-block begins with `try'. */
18661 || token->keyword == RID_TRY
18662 /* The named return value extension begins with `return'. */
18663 || token->keyword == RID_RETURN);
18666 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18670 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18674 token = cp_lexer_peek_token (parser->lexer);
18675 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18678 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18679 C++0x) ending a template-argument. */
18682 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18686 token = cp_lexer_peek_token (parser->lexer);
18687 return (token->type == CPP_COMMA
18688 || token->type == CPP_GREATER
18689 || token->type == CPP_ELLIPSIS
18690 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18693 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18694 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18697 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18702 token = cp_lexer_peek_nth_token (parser->lexer, n);
18703 if (token->type == CPP_LESS)
18705 /* Check for the sequence `<::' in the original code. It would be lexed as
18706 `[:', where `[' is a digraph, and there is no whitespace before
18708 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18711 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18712 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18718 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18719 or none_type otherwise. */
18721 static enum tag_types
18722 cp_parser_token_is_class_key (cp_token* token)
18724 switch (token->keyword)
18729 return record_type;
18738 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18741 cp_parser_check_class_key (enum tag_types class_key, tree type)
18743 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18744 permerror (input_location, "%qs tag used in naming %q#T",
18745 class_key == union_type ? "union"
18746 : class_key == record_type ? "struct" : "class",
18750 /* Issue an error message if DECL is redeclared with different
18751 access than its original declaration [class.access.spec/3].
18752 This applies to nested classes and nested class templates.
18756 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18758 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18761 if ((TREE_PRIVATE (decl)
18762 != (current_access_specifier == access_private_node))
18763 || (TREE_PROTECTED (decl)
18764 != (current_access_specifier == access_protected_node)))
18765 error ("%H%qD redeclared with different access", &location, decl);
18768 /* Look for the `template' keyword, as a syntactic disambiguator.
18769 Return TRUE iff it is present, in which case it will be
18773 cp_parser_optional_template_keyword (cp_parser *parser)
18775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18777 /* The `template' keyword can only be used within templates;
18778 outside templates the parser can always figure out what is a
18779 template and what is not. */
18780 if (!processing_template_decl)
18782 cp_token *token = cp_lexer_peek_token (parser->lexer);
18783 error ("%H%<template%> (as a disambiguator) is only allowed "
18784 "within templates", &token->location);
18785 /* If this part of the token stream is rescanned, the same
18786 error message would be generated. So, we purge the token
18787 from the stream. */
18788 cp_lexer_purge_token (parser->lexer);
18793 /* Consume the `template' keyword. */
18794 cp_lexer_consume_token (parser->lexer);
18802 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18803 set PARSER->SCOPE, and perform other related actions. */
18806 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18809 struct tree_check *check_value;
18810 deferred_access_check *chk;
18811 VEC (deferred_access_check,gc) *checks;
18813 /* Get the stored value. */
18814 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18815 /* Perform any access checks that were deferred. */
18816 checks = check_value->checks;
18820 VEC_iterate (deferred_access_check, checks, i, chk) ;
18823 perform_or_defer_access_check (chk->binfo,
18828 /* Set the scope from the stored value. */
18829 parser->scope = check_value->value;
18830 parser->qualifying_scope = check_value->qualifying_scope;
18831 parser->object_scope = NULL_TREE;
18834 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18835 encounter the end of a block before what we were looking for. */
18838 cp_parser_cache_group (cp_parser *parser,
18839 enum cpp_ttype end,
18844 cp_token *token = cp_lexer_peek_token (parser->lexer);
18846 /* Abort a parenthesized expression if we encounter a semicolon. */
18847 if ((end == CPP_CLOSE_PAREN || depth == 0)
18848 && token->type == CPP_SEMICOLON)
18850 /* If we've reached the end of the file, stop. */
18851 if (token->type == CPP_EOF
18852 || (end != CPP_PRAGMA_EOL
18853 && token->type == CPP_PRAGMA_EOL))
18855 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18856 /* We've hit the end of an enclosing block, so there's been some
18857 kind of syntax error. */
18860 /* Consume the token. */
18861 cp_lexer_consume_token (parser->lexer);
18862 /* See if it starts a new group. */
18863 if (token->type == CPP_OPEN_BRACE)
18865 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18866 /* In theory this should probably check end == '}', but
18867 cp_parser_save_member_function_body needs it to exit
18868 after either '}' or ')' when called with ')'. */
18872 else if (token->type == CPP_OPEN_PAREN)
18874 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18875 if (depth == 0 && end == CPP_CLOSE_PAREN)
18878 else if (token->type == CPP_PRAGMA)
18879 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18880 else if (token->type == end)
18885 /* Begin parsing tentatively. We always save tokens while parsing
18886 tentatively so that if the tentative parsing fails we can restore the
18890 cp_parser_parse_tentatively (cp_parser* parser)
18892 /* Enter a new parsing context. */
18893 parser->context = cp_parser_context_new (parser->context);
18894 /* Begin saving tokens. */
18895 cp_lexer_save_tokens (parser->lexer);
18896 /* In order to avoid repetitive access control error messages,
18897 access checks are queued up until we are no longer parsing
18899 push_deferring_access_checks (dk_deferred);
18902 /* Commit to the currently active tentative parse. */
18905 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18907 cp_parser_context *context;
18910 /* Mark all of the levels as committed. */
18911 lexer = parser->lexer;
18912 for (context = parser->context; context->next; context = context->next)
18914 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18916 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18917 while (!cp_lexer_saving_tokens (lexer))
18918 lexer = lexer->next;
18919 cp_lexer_commit_tokens (lexer);
18923 /* Abort the currently active tentative parse. All consumed tokens
18924 will be rolled back, and no diagnostics will be issued. */
18927 cp_parser_abort_tentative_parse (cp_parser* parser)
18929 cp_parser_simulate_error (parser);
18930 /* Now, pretend that we want to see if the construct was
18931 successfully parsed. */
18932 cp_parser_parse_definitely (parser);
18935 /* Stop parsing tentatively. If a parse error has occurred, restore the
18936 token stream. Otherwise, commit to the tokens we have consumed.
18937 Returns true if no error occurred; false otherwise. */
18940 cp_parser_parse_definitely (cp_parser* parser)
18942 bool error_occurred;
18943 cp_parser_context *context;
18945 /* Remember whether or not an error occurred, since we are about to
18946 destroy that information. */
18947 error_occurred = cp_parser_error_occurred (parser);
18948 /* Remove the topmost context from the stack. */
18949 context = parser->context;
18950 parser->context = context->next;
18951 /* If no parse errors occurred, commit to the tentative parse. */
18952 if (!error_occurred)
18954 /* Commit to the tokens read tentatively, unless that was
18956 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18957 cp_lexer_commit_tokens (parser->lexer);
18959 pop_to_parent_deferring_access_checks ();
18961 /* Otherwise, if errors occurred, roll back our state so that things
18962 are just as they were before we began the tentative parse. */
18965 cp_lexer_rollback_tokens (parser->lexer);
18966 pop_deferring_access_checks ();
18968 /* Add the context to the front of the free list. */
18969 context->next = cp_parser_context_free_list;
18970 cp_parser_context_free_list = context;
18972 return !error_occurred;
18975 /* Returns true if we are parsing tentatively and are not committed to
18976 this tentative parse. */
18979 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18981 return (cp_parser_parsing_tentatively (parser)
18982 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18985 /* Returns nonzero iff an error has occurred during the most recent
18986 tentative parse. */
18989 cp_parser_error_occurred (cp_parser* parser)
18991 return (cp_parser_parsing_tentatively (parser)
18992 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18995 /* Returns nonzero if GNU extensions are allowed. */
18998 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19000 return parser->allow_gnu_extensions_p;
19003 /* Objective-C++ Productions */
19006 /* Parse an Objective-C expression, which feeds into a primary-expression
19010 objc-message-expression
19011 objc-string-literal
19012 objc-encode-expression
19013 objc-protocol-expression
19014 objc-selector-expression
19016 Returns a tree representation of the expression. */
19019 cp_parser_objc_expression (cp_parser* parser)
19021 /* Try to figure out what kind of declaration is present. */
19022 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19026 case CPP_OPEN_SQUARE:
19027 return cp_parser_objc_message_expression (parser);
19029 case CPP_OBJC_STRING:
19030 kwd = cp_lexer_consume_token (parser->lexer);
19031 return objc_build_string_object (kwd->u.value);
19034 switch (kwd->keyword)
19036 case RID_AT_ENCODE:
19037 return cp_parser_objc_encode_expression (parser);
19039 case RID_AT_PROTOCOL:
19040 return cp_parser_objc_protocol_expression (parser);
19042 case RID_AT_SELECTOR:
19043 return cp_parser_objc_selector_expression (parser);
19049 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19050 &kwd->location, kwd->u.value);
19051 cp_parser_skip_to_end_of_block_or_statement (parser);
19054 return error_mark_node;
19057 /* Parse an Objective-C message expression.
19059 objc-message-expression:
19060 [ objc-message-receiver objc-message-args ]
19062 Returns a representation of an Objective-C message. */
19065 cp_parser_objc_message_expression (cp_parser* parser)
19067 tree receiver, messageargs;
19069 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19070 receiver = cp_parser_objc_message_receiver (parser);
19071 messageargs = cp_parser_objc_message_args (parser);
19072 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19074 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19077 /* Parse an objc-message-receiver.
19079 objc-message-receiver:
19081 simple-type-specifier
19083 Returns a representation of the type or expression. */
19086 cp_parser_objc_message_receiver (cp_parser* parser)
19090 /* An Objective-C message receiver may be either (1) a type
19091 or (2) an expression. */
19092 cp_parser_parse_tentatively (parser);
19093 rcv = cp_parser_expression (parser, false);
19095 if (cp_parser_parse_definitely (parser))
19098 rcv = cp_parser_simple_type_specifier (parser,
19099 /*decl_specs=*/NULL,
19100 CP_PARSER_FLAGS_NONE);
19102 return objc_get_class_reference (rcv);
19105 /* Parse the arguments and selectors comprising an Objective-C message.
19110 objc-selector-args , objc-comma-args
19112 objc-selector-args:
19113 objc-selector [opt] : assignment-expression
19114 objc-selector-args objc-selector [opt] : assignment-expression
19117 assignment-expression
19118 objc-comma-args , assignment-expression
19120 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19121 selector arguments and TREE_VALUE containing a list of comma
19125 cp_parser_objc_message_args (cp_parser* parser)
19127 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19128 bool maybe_unary_selector_p = true;
19129 cp_token *token = cp_lexer_peek_token (parser->lexer);
19131 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19133 tree selector = NULL_TREE, arg;
19135 if (token->type != CPP_COLON)
19136 selector = cp_parser_objc_selector (parser);
19138 /* Detect if we have a unary selector. */
19139 if (maybe_unary_selector_p
19140 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19141 return build_tree_list (selector, NULL_TREE);
19143 maybe_unary_selector_p = false;
19144 cp_parser_require (parser, CPP_COLON, "%<:%>");
19145 arg = cp_parser_assignment_expression (parser, false);
19148 = chainon (sel_args,
19149 build_tree_list (selector, arg));
19151 token = cp_lexer_peek_token (parser->lexer);
19154 /* Handle non-selector arguments, if any. */
19155 while (token->type == CPP_COMMA)
19159 cp_lexer_consume_token (parser->lexer);
19160 arg = cp_parser_assignment_expression (parser, false);
19163 = chainon (addl_args,
19164 build_tree_list (NULL_TREE, arg));
19166 token = cp_lexer_peek_token (parser->lexer);
19169 return build_tree_list (sel_args, addl_args);
19172 /* Parse an Objective-C encode expression.
19174 objc-encode-expression:
19175 @encode objc-typename
19177 Returns an encoded representation of the type argument. */
19180 cp_parser_objc_encode_expression (cp_parser* parser)
19185 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19186 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19187 token = cp_lexer_peek_token (parser->lexer);
19188 type = complete_type (cp_parser_type_id (parser));
19189 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19193 error ("%H%<@encode%> must specify a type as an argument",
19195 return error_mark_node;
19198 return objc_build_encode_expr (type);
19201 /* Parse an Objective-C @defs expression. */
19204 cp_parser_objc_defs_expression (cp_parser *parser)
19208 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19209 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19210 name = cp_parser_identifier (parser);
19211 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19213 return objc_get_class_ivars (name);
19216 /* Parse an Objective-C protocol expression.
19218 objc-protocol-expression:
19219 @protocol ( identifier )
19221 Returns a representation of the protocol expression. */
19224 cp_parser_objc_protocol_expression (cp_parser* parser)
19228 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19229 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19230 proto = cp_parser_identifier (parser);
19231 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19233 return objc_build_protocol_expr (proto);
19236 /* Parse an Objective-C selector expression.
19238 objc-selector-expression:
19239 @selector ( objc-method-signature )
19241 objc-method-signature:
19247 objc-selector-seq objc-selector :
19249 Returns a representation of the method selector. */
19252 cp_parser_objc_selector_expression (cp_parser* parser)
19254 tree sel_seq = NULL_TREE;
19255 bool maybe_unary_selector_p = true;
19258 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19259 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19260 token = cp_lexer_peek_token (parser->lexer);
19262 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19263 || token->type == CPP_SCOPE)
19265 tree selector = NULL_TREE;
19267 if (token->type != CPP_COLON
19268 || token->type == CPP_SCOPE)
19269 selector = cp_parser_objc_selector (parser);
19271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19272 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19274 /* Detect if we have a unary selector. */
19275 if (maybe_unary_selector_p)
19277 sel_seq = selector;
19278 goto finish_selector;
19282 cp_parser_error (parser, "expected %<:%>");
19285 maybe_unary_selector_p = false;
19286 token = cp_lexer_consume_token (parser->lexer);
19288 if (token->type == CPP_SCOPE)
19291 = chainon (sel_seq,
19292 build_tree_list (selector, NULL_TREE));
19294 = chainon (sel_seq,
19295 build_tree_list (NULL_TREE, NULL_TREE));
19299 = chainon (sel_seq,
19300 build_tree_list (selector, NULL_TREE));
19302 token = cp_lexer_peek_token (parser->lexer);
19306 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19308 return objc_build_selector_expr (sel_seq);
19311 /* Parse a list of identifiers.
19313 objc-identifier-list:
19315 objc-identifier-list , identifier
19317 Returns a TREE_LIST of identifier nodes. */
19320 cp_parser_objc_identifier_list (cp_parser* parser)
19322 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19323 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19325 while (sep->type == CPP_COMMA)
19327 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19328 list = chainon (list,
19329 build_tree_list (NULL_TREE,
19330 cp_parser_identifier (parser)));
19331 sep = cp_lexer_peek_token (parser->lexer);
19337 /* Parse an Objective-C alias declaration.
19339 objc-alias-declaration:
19340 @compatibility_alias identifier identifier ;
19342 This function registers the alias mapping with the Objective-C front end.
19343 It returns nothing. */
19346 cp_parser_objc_alias_declaration (cp_parser* parser)
19350 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19351 alias = cp_parser_identifier (parser);
19352 orig = cp_parser_identifier (parser);
19353 objc_declare_alias (alias, orig);
19354 cp_parser_consume_semicolon_at_end_of_statement (parser);
19357 /* Parse an Objective-C class forward-declaration.
19359 objc-class-declaration:
19360 @class objc-identifier-list ;
19362 The function registers the forward declarations with the Objective-C
19363 front end. It returns nothing. */
19366 cp_parser_objc_class_declaration (cp_parser* parser)
19368 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19369 objc_declare_class (cp_parser_objc_identifier_list (parser));
19370 cp_parser_consume_semicolon_at_end_of_statement (parser);
19373 /* Parse a list of Objective-C protocol references.
19375 objc-protocol-refs-opt:
19376 objc-protocol-refs [opt]
19378 objc-protocol-refs:
19379 < objc-identifier-list >
19381 Returns a TREE_LIST of identifiers, if any. */
19384 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19386 tree protorefs = NULL_TREE;
19388 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19390 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19391 protorefs = cp_parser_objc_identifier_list (parser);
19392 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19398 /* Parse a Objective-C visibility specification. */
19401 cp_parser_objc_visibility_spec (cp_parser* parser)
19403 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19405 switch (vis->keyword)
19407 case RID_AT_PRIVATE:
19408 objc_set_visibility (2);
19410 case RID_AT_PROTECTED:
19411 objc_set_visibility (0);
19413 case RID_AT_PUBLIC:
19414 objc_set_visibility (1);
19420 /* Eat '@private'/'@protected'/'@public'. */
19421 cp_lexer_consume_token (parser->lexer);
19424 /* Parse an Objective-C method type. */
19427 cp_parser_objc_method_type (cp_parser* parser)
19429 objc_set_method_type
19430 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19435 /* Parse an Objective-C protocol qualifier. */
19438 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19440 tree quals = NULL_TREE, node;
19441 cp_token *token = cp_lexer_peek_token (parser->lexer);
19443 node = token->u.value;
19445 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19446 && (node == ridpointers [(int) RID_IN]
19447 || node == ridpointers [(int) RID_OUT]
19448 || node == ridpointers [(int) RID_INOUT]
19449 || node == ridpointers [(int) RID_BYCOPY]
19450 || node == ridpointers [(int) RID_BYREF]
19451 || node == ridpointers [(int) RID_ONEWAY]))
19453 quals = tree_cons (NULL_TREE, node, quals);
19454 cp_lexer_consume_token (parser->lexer);
19455 token = cp_lexer_peek_token (parser->lexer);
19456 node = token->u.value;
19462 /* Parse an Objective-C typename. */
19465 cp_parser_objc_typename (cp_parser* parser)
19467 tree type_name = NULL_TREE;
19469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19471 tree proto_quals, cp_type = NULL_TREE;
19473 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19474 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19476 /* An ObjC type name may consist of just protocol qualifiers, in which
19477 case the type shall default to 'id'. */
19478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19479 cp_type = cp_parser_type_id (parser);
19481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19482 type_name = build_tree_list (proto_quals, cp_type);
19488 /* Check to see if TYPE refers to an Objective-C selector name. */
19491 cp_parser_objc_selector_p (enum cpp_ttype type)
19493 return (type == CPP_NAME || type == CPP_KEYWORD
19494 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19495 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19496 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19497 || type == CPP_XOR || type == CPP_XOR_EQ);
19500 /* Parse an Objective-C selector. */
19503 cp_parser_objc_selector (cp_parser* parser)
19505 cp_token *token = cp_lexer_consume_token (parser->lexer);
19507 if (!cp_parser_objc_selector_p (token->type))
19509 error ("%Hinvalid Objective-C++ selector name", &token->location);
19510 return error_mark_node;
19513 /* C++ operator names are allowed to appear in ObjC selectors. */
19514 switch (token->type)
19516 case CPP_AND_AND: return get_identifier ("and");
19517 case CPP_AND_EQ: return get_identifier ("and_eq");
19518 case CPP_AND: return get_identifier ("bitand");
19519 case CPP_OR: return get_identifier ("bitor");
19520 case CPP_COMPL: return get_identifier ("compl");
19521 case CPP_NOT: return get_identifier ("not");
19522 case CPP_NOT_EQ: return get_identifier ("not_eq");
19523 case CPP_OR_OR: return get_identifier ("or");
19524 case CPP_OR_EQ: return get_identifier ("or_eq");
19525 case CPP_XOR: return get_identifier ("xor");
19526 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19527 default: return token->u.value;
19531 /* Parse an Objective-C params list. */
19534 cp_parser_objc_method_keyword_params (cp_parser* parser)
19536 tree params = NULL_TREE;
19537 bool maybe_unary_selector_p = true;
19538 cp_token *token = cp_lexer_peek_token (parser->lexer);
19540 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19542 tree selector = NULL_TREE, type_name, identifier;
19544 if (token->type != CPP_COLON)
19545 selector = cp_parser_objc_selector (parser);
19547 /* Detect if we have a unary selector. */
19548 if (maybe_unary_selector_p
19549 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19552 maybe_unary_selector_p = false;
19553 cp_parser_require (parser, CPP_COLON, "%<:%>");
19554 type_name = cp_parser_objc_typename (parser);
19555 identifier = cp_parser_identifier (parser);
19559 objc_build_keyword_decl (selector,
19563 token = cp_lexer_peek_token (parser->lexer);
19569 /* Parse the non-keyword Objective-C params. */
19572 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19574 tree params = make_node (TREE_LIST);
19575 cp_token *token = cp_lexer_peek_token (parser->lexer);
19576 *ellipsisp = false; /* Initially, assume no ellipsis. */
19578 while (token->type == CPP_COMMA)
19580 cp_parameter_declarator *parmdecl;
19583 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19584 token = cp_lexer_peek_token (parser->lexer);
19586 if (token->type == CPP_ELLIPSIS)
19588 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19593 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19594 parm = grokdeclarator (parmdecl->declarator,
19595 &parmdecl->decl_specifiers,
19596 PARM, /*initialized=*/0,
19597 /*attrlist=*/NULL);
19599 chainon (params, build_tree_list (NULL_TREE, parm));
19600 token = cp_lexer_peek_token (parser->lexer);
19606 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19609 cp_parser_objc_interstitial_code (cp_parser* parser)
19611 cp_token *token = cp_lexer_peek_token (parser->lexer);
19613 /* If the next token is `extern' and the following token is a string
19614 literal, then we have a linkage specification. */
19615 if (token->keyword == RID_EXTERN
19616 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19617 cp_parser_linkage_specification (parser);
19618 /* Handle #pragma, if any. */
19619 else if (token->type == CPP_PRAGMA)
19620 cp_parser_pragma (parser, pragma_external);
19621 /* Allow stray semicolons. */
19622 else if (token->type == CPP_SEMICOLON)
19623 cp_lexer_consume_token (parser->lexer);
19624 /* Finally, try to parse a block-declaration, or a function-definition. */
19626 cp_parser_block_declaration (parser, /*statement_p=*/false);
19629 /* Parse a method signature. */
19632 cp_parser_objc_method_signature (cp_parser* parser)
19634 tree rettype, kwdparms, optparms;
19635 bool ellipsis = false;
19637 cp_parser_objc_method_type (parser);
19638 rettype = cp_parser_objc_typename (parser);
19639 kwdparms = cp_parser_objc_method_keyword_params (parser);
19640 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19642 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19645 /* Pars an Objective-C method prototype list. */
19648 cp_parser_objc_method_prototype_list (cp_parser* parser)
19650 cp_token *token = cp_lexer_peek_token (parser->lexer);
19652 while (token->keyword != RID_AT_END)
19654 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19656 objc_add_method_declaration
19657 (cp_parser_objc_method_signature (parser));
19658 cp_parser_consume_semicolon_at_end_of_statement (parser);
19661 /* Allow for interspersed non-ObjC++ code. */
19662 cp_parser_objc_interstitial_code (parser);
19664 token = cp_lexer_peek_token (parser->lexer);
19667 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19668 objc_finish_interface ();
19671 /* Parse an Objective-C method definition list. */
19674 cp_parser_objc_method_definition_list (cp_parser* parser)
19676 cp_token *token = cp_lexer_peek_token (parser->lexer);
19678 while (token->keyword != RID_AT_END)
19682 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19684 push_deferring_access_checks (dk_deferred);
19685 objc_start_method_definition
19686 (cp_parser_objc_method_signature (parser));
19688 /* For historical reasons, we accept an optional semicolon. */
19689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19690 cp_lexer_consume_token (parser->lexer);
19692 perform_deferred_access_checks ();
19693 stop_deferring_access_checks ();
19694 meth = cp_parser_function_definition_after_declarator (parser,
19696 pop_deferring_access_checks ();
19697 objc_finish_method_definition (meth);
19700 /* Allow for interspersed non-ObjC++ code. */
19701 cp_parser_objc_interstitial_code (parser);
19703 token = cp_lexer_peek_token (parser->lexer);
19706 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19707 objc_finish_implementation ();
19710 /* Parse Objective-C ivars. */
19713 cp_parser_objc_class_ivars (cp_parser* parser)
19715 cp_token *token = cp_lexer_peek_token (parser->lexer);
19717 if (token->type != CPP_OPEN_BRACE)
19718 return; /* No ivars specified. */
19720 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19721 token = cp_lexer_peek_token (parser->lexer);
19723 while (token->type != CPP_CLOSE_BRACE)
19725 cp_decl_specifier_seq declspecs;
19726 int decl_class_or_enum_p;
19727 tree prefix_attributes;
19729 cp_parser_objc_visibility_spec (parser);
19731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19734 cp_parser_decl_specifier_seq (parser,
19735 CP_PARSER_FLAGS_OPTIONAL,
19737 &decl_class_or_enum_p);
19738 prefix_attributes = declspecs.attributes;
19739 declspecs.attributes = NULL_TREE;
19741 /* Keep going until we hit the `;' at the end of the
19743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19745 tree width = NULL_TREE, attributes, first_attribute, decl;
19746 cp_declarator *declarator = NULL;
19747 int ctor_dtor_or_conv_p;
19749 /* Check for a (possibly unnamed) bitfield declaration. */
19750 token = cp_lexer_peek_token (parser->lexer);
19751 if (token->type == CPP_COLON)
19754 if (token->type == CPP_NAME
19755 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19758 /* Get the name of the bitfield. */
19759 declarator = make_id_declarator (NULL_TREE,
19760 cp_parser_identifier (parser),
19764 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19765 /* Get the width of the bitfield. */
19767 = cp_parser_constant_expression (parser,
19768 /*allow_non_constant=*/false,
19773 /* Parse the declarator. */
19775 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19776 &ctor_dtor_or_conv_p,
19777 /*parenthesized_p=*/NULL,
19778 /*member_p=*/false);
19781 /* Look for attributes that apply to the ivar. */
19782 attributes = cp_parser_attributes_opt (parser);
19783 /* Remember which attributes are prefix attributes and
19785 first_attribute = attributes;
19786 /* Combine the attributes. */
19787 attributes = chainon (prefix_attributes, attributes);
19790 /* Create the bitfield declaration. */
19791 decl = grokbitfield (declarator, &declspecs,
19795 decl = grokfield (declarator, &declspecs,
19796 NULL_TREE, /*init_const_expr_p=*/false,
19797 NULL_TREE, attributes);
19799 /* Add the instance variable. */
19800 objc_add_instance_variable (decl);
19802 /* Reset PREFIX_ATTRIBUTES. */
19803 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19804 attributes = TREE_CHAIN (attributes);
19806 TREE_CHAIN (attributes) = NULL_TREE;
19808 token = cp_lexer_peek_token (parser->lexer);
19810 if (token->type == CPP_COMMA)
19812 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19818 cp_parser_consume_semicolon_at_end_of_statement (parser);
19819 token = cp_lexer_peek_token (parser->lexer);
19822 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19823 /* For historical reasons, we accept an optional semicolon. */
19824 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19825 cp_lexer_consume_token (parser->lexer);
19828 /* Parse an Objective-C protocol declaration. */
19831 cp_parser_objc_protocol_declaration (cp_parser* parser)
19833 tree proto, protorefs;
19836 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19839 tok = cp_lexer_peek_token (parser->lexer);
19840 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19844 /* See if we have a forward declaration or a definition. */
19845 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19847 /* Try a forward declaration first. */
19848 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19850 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19852 cp_parser_consume_semicolon_at_end_of_statement (parser);
19855 /* Ok, we got a full-fledged definition (or at least should). */
19858 proto = cp_parser_identifier (parser);
19859 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19860 objc_start_protocol (proto, protorefs);
19861 cp_parser_objc_method_prototype_list (parser);
19865 /* Parse an Objective-C superclass or category. */
19868 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19871 cp_token *next = cp_lexer_peek_token (parser->lexer);
19873 *super = *categ = NULL_TREE;
19874 if (next->type == CPP_COLON)
19876 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19877 *super = cp_parser_identifier (parser);
19879 else if (next->type == CPP_OPEN_PAREN)
19881 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19882 *categ = cp_parser_identifier (parser);
19883 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19887 /* Parse an Objective-C class interface. */
19890 cp_parser_objc_class_interface (cp_parser* parser)
19892 tree name, super, categ, protos;
19894 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19895 name = cp_parser_identifier (parser);
19896 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19897 protos = cp_parser_objc_protocol_refs_opt (parser);
19899 /* We have either a class or a category on our hands. */
19901 objc_start_category_interface (name, categ, protos);
19904 objc_start_class_interface (name, super, protos);
19905 /* Handle instance variable declarations, if any. */
19906 cp_parser_objc_class_ivars (parser);
19907 objc_continue_interface ();
19910 cp_parser_objc_method_prototype_list (parser);
19913 /* Parse an Objective-C class implementation. */
19916 cp_parser_objc_class_implementation (cp_parser* parser)
19918 tree name, super, categ;
19920 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19921 name = cp_parser_identifier (parser);
19922 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19924 /* We have either a class or a category on our hands. */
19926 objc_start_category_implementation (name, categ);
19929 objc_start_class_implementation (name, super);
19930 /* Handle instance variable declarations, if any. */
19931 cp_parser_objc_class_ivars (parser);
19932 objc_continue_implementation ();
19935 cp_parser_objc_method_definition_list (parser);
19938 /* Consume the @end token and finish off the implementation. */
19941 cp_parser_objc_end_implementation (cp_parser* parser)
19943 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19944 objc_finish_implementation ();
19947 /* Parse an Objective-C declaration. */
19950 cp_parser_objc_declaration (cp_parser* parser)
19952 /* Try to figure out what kind of declaration is present. */
19953 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19955 switch (kwd->keyword)
19958 cp_parser_objc_alias_declaration (parser);
19961 cp_parser_objc_class_declaration (parser);
19963 case RID_AT_PROTOCOL:
19964 cp_parser_objc_protocol_declaration (parser);
19966 case RID_AT_INTERFACE:
19967 cp_parser_objc_class_interface (parser);
19969 case RID_AT_IMPLEMENTATION:
19970 cp_parser_objc_class_implementation (parser);
19973 cp_parser_objc_end_implementation (parser);
19976 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19977 &kwd->location, kwd->u.value);
19978 cp_parser_skip_to_end_of_block_or_statement (parser);
19982 /* Parse an Objective-C try-catch-finally statement.
19984 objc-try-catch-finally-stmt:
19985 @try compound-statement objc-catch-clause-seq [opt]
19986 objc-finally-clause [opt]
19988 objc-catch-clause-seq:
19989 objc-catch-clause objc-catch-clause-seq [opt]
19992 @catch ( exception-declaration ) compound-statement
19994 objc-finally-clause
19995 @finally compound-statement
19997 Returns NULL_TREE. */
20000 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20001 location_t location;
20004 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20005 location = cp_lexer_peek_token (parser->lexer)->location;
20006 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20007 node, lest it get absorbed into the surrounding block. */
20008 stmt = push_stmt_list ();
20009 cp_parser_compound_statement (parser, NULL, false);
20010 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20012 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20014 cp_parameter_declarator *parmdecl;
20017 cp_lexer_consume_token (parser->lexer);
20018 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20019 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20020 parm = grokdeclarator (parmdecl->declarator,
20021 &parmdecl->decl_specifiers,
20022 PARM, /*initialized=*/0,
20023 /*attrlist=*/NULL);
20024 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20025 objc_begin_catch_clause (parm);
20026 cp_parser_compound_statement (parser, NULL, false);
20027 objc_finish_catch_clause ();
20030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20032 cp_lexer_consume_token (parser->lexer);
20033 location = cp_lexer_peek_token (parser->lexer)->location;
20034 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20035 node, lest it get absorbed into the surrounding block. */
20036 stmt = push_stmt_list ();
20037 cp_parser_compound_statement (parser, NULL, false);
20038 objc_build_finally_clause (location, pop_stmt_list (stmt));
20041 return objc_finish_try_stmt ();
20044 /* Parse an Objective-C synchronized statement.
20046 objc-synchronized-stmt:
20047 @synchronized ( expression ) compound-statement
20049 Returns NULL_TREE. */
20052 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20053 location_t location;
20056 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20058 location = cp_lexer_peek_token (parser->lexer)->location;
20059 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20060 lock = cp_parser_expression (parser, false);
20061 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20063 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20064 node, lest it get absorbed into the surrounding block. */
20065 stmt = push_stmt_list ();
20066 cp_parser_compound_statement (parser, NULL, false);
20068 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20071 /* Parse an Objective-C throw statement.
20074 @throw assignment-expression [opt] ;
20076 Returns a constructed '@throw' statement. */
20079 cp_parser_objc_throw_statement (cp_parser *parser) {
20080 tree expr = NULL_TREE;
20082 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20085 expr = cp_parser_assignment_expression (parser, false);
20087 cp_parser_consume_semicolon_at_end_of_statement (parser);
20089 return objc_build_throw_stmt (expr);
20092 /* Parse an Objective-C statement. */
20095 cp_parser_objc_statement (cp_parser * parser) {
20096 /* Try to figure out what kind of declaration is present. */
20097 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20099 switch (kwd->keyword)
20102 return cp_parser_objc_try_catch_finally_statement (parser);
20103 case RID_AT_SYNCHRONIZED:
20104 return cp_parser_objc_synchronized_statement (parser);
20106 return cp_parser_objc_throw_statement (parser);
20108 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20109 &kwd->location, kwd->u.value);
20110 cp_parser_skip_to_end_of_block_or_statement (parser);
20113 return error_mark_node;
20116 /* OpenMP 2.5 parsing routines. */
20118 /* Returns name of the next clause.
20119 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20120 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20121 returned and the token is consumed. */
20123 static pragma_omp_clause
20124 cp_parser_omp_clause_name (cp_parser *parser)
20126 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20129 result = PRAGMA_OMP_CLAUSE_IF;
20130 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20131 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20132 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20133 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20134 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20137 const char *p = IDENTIFIER_POINTER (id);
20142 if (!strcmp ("collapse", p))
20143 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20144 else if (!strcmp ("copyin", p))
20145 result = PRAGMA_OMP_CLAUSE_COPYIN;
20146 else if (!strcmp ("copyprivate", p))
20147 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20150 if (!strcmp ("firstprivate", p))
20151 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20154 if (!strcmp ("lastprivate", p))
20155 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20158 if (!strcmp ("nowait", p))
20159 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20160 else if (!strcmp ("num_threads", p))
20161 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20164 if (!strcmp ("ordered", p))
20165 result = PRAGMA_OMP_CLAUSE_ORDERED;
20168 if (!strcmp ("reduction", p))
20169 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20172 if (!strcmp ("schedule", p))
20173 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20174 else if (!strcmp ("shared", p))
20175 result = PRAGMA_OMP_CLAUSE_SHARED;
20178 if (!strcmp ("untied", p))
20179 result = PRAGMA_OMP_CLAUSE_UNTIED;
20184 if (result != PRAGMA_OMP_CLAUSE_NONE)
20185 cp_lexer_consume_token (parser->lexer);
20190 /* Validate that a clause of the given type does not already exist. */
20193 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20194 const char *name, location_t location)
20198 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20199 if (OMP_CLAUSE_CODE (c) == code)
20201 error ("%Htoo many %qs clauses", &location, name);
20209 variable-list , identifier
20211 In addition, we match a closing parenthesis. An opening parenthesis
20212 will have been consumed by the caller.
20214 If KIND is nonzero, create the appropriate node and install the decl
20215 in OMP_CLAUSE_DECL and add the node to the head of the list.
20217 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20218 return the list created. */
20221 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20229 token = cp_lexer_peek_token (parser->lexer);
20230 name = cp_parser_id_expression (parser, /*template_p=*/false,
20231 /*check_dependency_p=*/true,
20232 /*template_p=*/NULL,
20233 /*declarator_p=*/false,
20234 /*optional_p=*/false);
20235 if (name == error_mark_node)
20238 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20239 if (decl == error_mark_node)
20240 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20241 else if (kind != 0)
20243 tree u = build_omp_clause (kind);
20244 OMP_CLAUSE_DECL (u) = decl;
20245 OMP_CLAUSE_CHAIN (u) = list;
20249 list = tree_cons (decl, NULL_TREE, list);
20252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20254 cp_lexer_consume_token (parser->lexer);
20257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20261 /* Try to resync to an unnested comma. Copied from
20262 cp_parser_parenthesized_expression_list. */
20264 ending = cp_parser_skip_to_closing_parenthesis (parser,
20265 /*recovering=*/true,
20267 /*consume_paren=*/true);
20275 /* Similarly, but expect leading and trailing parenthesis. This is a very
20276 common case for omp clauses. */
20279 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20281 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20282 return cp_parser_omp_var_list_no_open (parser, kind, list);
20287 collapse ( constant-expression ) */
20290 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20296 loc = cp_lexer_peek_token (parser->lexer)->location;
20297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20300 num = cp_parser_constant_expression (parser, false, NULL);
20302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20303 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20304 /*or_comma=*/false,
20305 /*consume_paren=*/true);
20307 if (num == error_mark_node)
20309 num = fold_non_dependent_expr (num);
20310 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20311 || !host_integerp (num, 0)
20312 || (n = tree_low_cst (num, 0)) <= 0
20315 error ("%Hcollapse argument needs positive constant integer expression",
20320 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20321 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20322 OMP_CLAUSE_CHAIN (c) = list;
20323 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20329 default ( shared | none ) */
20332 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20334 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20337 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20342 const char *p = IDENTIFIER_POINTER (id);
20347 if (strcmp ("none", p) != 0)
20349 kind = OMP_CLAUSE_DEFAULT_NONE;
20353 if (strcmp ("shared", p) != 0)
20355 kind = OMP_CLAUSE_DEFAULT_SHARED;
20362 cp_lexer_consume_token (parser->lexer);
20367 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20372 /*or_comma=*/false,
20373 /*consume_paren=*/true);
20375 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20378 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20379 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20380 OMP_CLAUSE_CHAIN (c) = list;
20381 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20387 if ( expression ) */
20390 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20397 t = cp_parser_condition (parser);
20399 if (t == error_mark_node
20400 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20402 /*or_comma=*/false,
20403 /*consume_paren=*/true);
20405 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20407 c = build_omp_clause (OMP_CLAUSE_IF);
20408 OMP_CLAUSE_IF_EXPR (c) = t;
20409 OMP_CLAUSE_CHAIN (c) = list;
20418 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20419 tree list, location_t location)
20423 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20425 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20426 OMP_CLAUSE_CHAIN (c) = list;
20431 num_threads ( expression ) */
20434 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20435 location_t location)
20439 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20442 t = cp_parser_expression (parser, false);
20444 if (t == error_mark_node
20445 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20447 /*or_comma=*/false,
20448 /*consume_paren=*/true);
20450 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20451 "num_threads", location);
20453 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20454 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20455 OMP_CLAUSE_CHAIN (c) = list;
20464 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20465 tree list, location_t location)
20469 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20470 "ordered", location);
20472 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20473 OMP_CLAUSE_CHAIN (c) = list;
20478 reduction ( reduction-operator : variable-list )
20480 reduction-operator:
20481 One of: + * - & ^ | && || */
20484 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20486 enum tree_code code;
20489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20492 switch (cp_lexer_peek_token (parser->lexer)->type)
20504 code = BIT_AND_EXPR;
20507 code = BIT_XOR_EXPR;
20510 code = BIT_IOR_EXPR;
20513 code = TRUTH_ANDIF_EXPR;
20516 code = TRUTH_ORIF_EXPR;
20519 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20520 "%<|%>, %<&&%>, or %<||%>");
20522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20523 /*or_comma=*/false,
20524 /*consume_paren=*/true);
20527 cp_lexer_consume_token (parser->lexer);
20529 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20532 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20533 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20534 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20540 schedule ( schedule-kind )
20541 schedule ( schedule-kind , expression )
20544 static | dynamic | guided | runtime | auto */
20547 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20551 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20554 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20559 const char *p = IDENTIFIER_POINTER (id);
20564 if (strcmp ("dynamic", p) != 0)
20566 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20570 if (strcmp ("guided", p) != 0)
20572 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20576 if (strcmp ("runtime", p) != 0)
20578 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20585 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20586 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20588 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20591 cp_lexer_consume_token (parser->lexer);
20593 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20596 cp_lexer_consume_token (parser->lexer);
20598 token = cp_lexer_peek_token (parser->lexer);
20599 t = cp_parser_assignment_expression (parser, false);
20601 if (t == error_mark_node)
20603 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20604 error ("%Hschedule %<runtime%> does not take "
20605 "a %<chunk_size%> parameter", &token->location);
20606 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20607 error ("%Hschedule %<auto%> does not take "
20608 "a %<chunk_size%> parameter", &token->location);
20610 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20615 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20618 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20619 OMP_CLAUSE_CHAIN (c) = list;
20623 cp_parser_error (parser, "invalid schedule kind");
20625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20626 /*or_comma=*/false,
20627 /*consume_paren=*/true);
20635 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20636 tree list, location_t location)
20640 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20642 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20643 OMP_CLAUSE_CHAIN (c) = list;
20647 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20648 is a bitmask in MASK. Return the list of clauses found; the result
20649 of clause default goes in *pdefault. */
20652 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20653 const char *where, cp_token *pragma_tok)
20655 tree clauses = NULL;
20657 cp_token *token = NULL;
20659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20661 pragma_omp_clause c_kind;
20662 const char *c_name;
20663 tree prev = clauses;
20665 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20666 cp_lexer_consume_token (parser->lexer);
20668 token = cp_lexer_peek_token (parser->lexer);
20669 c_kind = cp_parser_omp_clause_name (parser);
20674 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20675 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20677 c_name = "collapse";
20679 case PRAGMA_OMP_CLAUSE_COPYIN:
20680 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20683 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20686 c_name = "copyprivate";
20688 case PRAGMA_OMP_CLAUSE_DEFAULT:
20689 clauses = cp_parser_omp_clause_default (parser, clauses,
20691 c_name = "default";
20693 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20696 c_name = "firstprivate";
20698 case PRAGMA_OMP_CLAUSE_IF:
20699 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20702 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20703 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20705 c_name = "lastprivate";
20707 case PRAGMA_OMP_CLAUSE_NOWAIT:
20708 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20711 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20712 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20714 c_name = "num_threads";
20716 case PRAGMA_OMP_CLAUSE_ORDERED:
20717 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20719 c_name = "ordered";
20721 case PRAGMA_OMP_CLAUSE_PRIVATE:
20722 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20724 c_name = "private";
20726 case PRAGMA_OMP_CLAUSE_REDUCTION:
20727 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20728 c_name = "reduction";
20730 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20731 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20733 c_name = "schedule";
20735 case PRAGMA_OMP_CLAUSE_SHARED:
20736 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20740 case PRAGMA_OMP_CLAUSE_UNTIED:
20741 clauses = cp_parser_omp_clause_untied (parser, clauses,
20746 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20750 if (((mask >> c_kind) & 1) == 0)
20752 /* Remove the invalid clause(s) from the list to avoid
20753 confusing the rest of the compiler. */
20755 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20759 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20760 return finish_omp_clauses (clauses);
20767 In practice, we're also interested in adding the statement to an
20768 outer node. So it is convenient if we work around the fact that
20769 cp_parser_statement calls add_stmt. */
20772 cp_parser_begin_omp_structured_block (cp_parser *parser)
20774 unsigned save = parser->in_statement;
20776 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20777 This preserves the "not within loop or switch" style error messages
20778 for nonsense cases like
20784 if (parser->in_statement)
20785 parser->in_statement = IN_OMP_BLOCK;
20791 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20793 parser->in_statement = save;
20797 cp_parser_omp_structured_block (cp_parser *parser)
20799 tree stmt = begin_omp_structured_block ();
20800 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20802 cp_parser_statement (parser, NULL_TREE, false, NULL);
20804 cp_parser_end_omp_structured_block (parser, save);
20805 return finish_omp_structured_block (stmt);
20809 # pragma omp atomic new-line
20813 x binop= expr | x++ | ++x | x-- | --x
20815 +, *, -, /, &, ^, |, <<, >>
20817 where x is an lvalue expression with scalar type. */
20820 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20823 enum tree_code code;
20825 cp_parser_require_pragma_eol (parser, pragma_tok);
20827 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20829 switch (TREE_CODE (lhs))
20834 case PREINCREMENT_EXPR:
20835 case POSTINCREMENT_EXPR:
20836 lhs = TREE_OPERAND (lhs, 0);
20838 rhs = integer_one_node;
20841 case PREDECREMENT_EXPR:
20842 case POSTDECREMENT_EXPR:
20843 lhs = TREE_OPERAND (lhs, 0);
20845 rhs = integer_one_node;
20849 switch (cp_lexer_peek_token (parser->lexer)->type)
20855 code = TRUNC_DIV_EXPR;
20863 case CPP_LSHIFT_EQ:
20864 code = LSHIFT_EXPR;
20866 case CPP_RSHIFT_EQ:
20867 code = RSHIFT_EXPR;
20870 code = BIT_AND_EXPR;
20873 code = BIT_IOR_EXPR;
20876 code = BIT_XOR_EXPR;
20879 cp_parser_error (parser,
20880 "invalid operator for %<#pragma omp atomic%>");
20883 cp_lexer_consume_token (parser->lexer);
20885 rhs = cp_parser_expression (parser, false);
20886 if (rhs == error_mark_node)
20890 finish_omp_atomic (code, lhs, rhs);
20891 cp_parser_consume_semicolon_at_end_of_statement (parser);
20895 cp_parser_skip_to_end_of_block_or_statement (parser);
20900 # pragma omp barrier new-line */
20903 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20905 cp_parser_require_pragma_eol (parser, pragma_tok);
20906 finish_omp_barrier ();
20910 # pragma omp critical [(name)] new-line
20911 structured-block */
20914 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20916 tree stmt, name = NULL;
20918 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20920 cp_lexer_consume_token (parser->lexer);
20922 name = cp_parser_identifier (parser);
20924 if (name == error_mark_node
20925 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20927 /*or_comma=*/false,
20928 /*consume_paren=*/true);
20929 if (name == error_mark_node)
20932 cp_parser_require_pragma_eol (parser, pragma_tok);
20934 stmt = cp_parser_omp_structured_block (parser);
20935 return c_finish_omp_critical (stmt, name);
20939 # pragma omp flush flush-vars[opt] new-line
20942 ( variable-list ) */
20945 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20948 (void) cp_parser_omp_var_list (parser, 0, NULL);
20949 cp_parser_require_pragma_eol (parser, pragma_tok);
20951 finish_omp_flush ();
20954 /* Helper function, to parse omp for increment expression. */
20957 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20959 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20965 cp_parser_skip_to_end_of_statement (parser);
20966 return error_mark_node;
20969 token = cp_lexer_peek_token (parser->lexer);
20970 op = binops_by_token [token->type].tree_type;
20979 cp_parser_skip_to_end_of_statement (parser);
20980 return error_mark_node;
20983 cp_lexer_consume_token (parser->lexer);
20984 rhs = cp_parser_binary_expression (parser, false,
20985 PREC_RELATIONAL_EXPRESSION);
20986 if (rhs == error_mark_node
20987 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20989 cp_parser_skip_to_end_of_statement (parser);
20990 return error_mark_node;
20993 return build2 (op, boolean_type_node, lhs, rhs);
20996 /* Helper function, to parse omp for increment expression. */
20999 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21001 cp_token *token = cp_lexer_peek_token (parser->lexer);
21007 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21009 op = (token->type == CPP_PLUS_PLUS
21010 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21011 cp_lexer_consume_token (parser->lexer);
21012 lhs = cp_parser_cast_expression (parser, false, false);
21014 return error_mark_node;
21015 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21018 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21020 return error_mark_node;
21022 token = cp_lexer_peek_token (parser->lexer);
21023 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21025 op = (token->type == CPP_PLUS_PLUS
21026 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21027 cp_lexer_consume_token (parser->lexer);
21028 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21031 op = cp_parser_assignment_operator_opt (parser);
21032 if (op == ERROR_MARK)
21033 return error_mark_node;
21035 if (op != NOP_EXPR)
21037 rhs = cp_parser_assignment_expression (parser, false);
21038 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21039 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21042 lhs = cp_parser_binary_expression (parser, false,
21043 PREC_ADDITIVE_EXPRESSION);
21044 token = cp_lexer_peek_token (parser->lexer);
21045 decl_first = lhs == decl;
21048 if (token->type != CPP_PLUS
21049 && token->type != CPP_MINUS)
21050 return error_mark_node;
21054 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21055 cp_lexer_consume_token (parser->lexer);
21056 rhs = cp_parser_binary_expression (parser, false,
21057 PREC_ADDITIVE_EXPRESSION);
21058 token = cp_lexer_peek_token (parser->lexer);
21059 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21061 if (lhs == NULL_TREE)
21063 if (op == PLUS_EXPR)
21066 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21069 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21070 NULL, tf_warning_or_error);
21073 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21077 if (rhs != decl || op == MINUS_EXPR)
21078 return error_mark_node;
21079 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21082 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21084 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21087 /* Parse the restricted form of the for statement allowed by OpenMP. */
21090 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21092 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21093 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21094 tree this_pre_body, cl;
21095 location_t loc_first;
21096 bool collapse_err = false;
21097 int i, collapse = 1, nbraces = 0;
21099 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21100 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21101 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21103 gcc_assert (collapse >= 1);
21105 declv = make_tree_vec (collapse);
21106 initv = make_tree_vec (collapse);
21107 condv = make_tree_vec (collapse);
21108 incrv = make_tree_vec (collapse);
21110 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21112 for (i = 0; i < collapse; i++)
21114 int bracecount = 0;
21115 bool add_private_clause = false;
21118 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21120 cp_parser_error (parser, "for statement expected");
21123 loc = cp_lexer_consume_token (parser->lexer)->location;
21125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21128 init = decl = real_decl = NULL;
21129 this_pre_body = push_stmt_list ();
21130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21132 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21136 integer-type var = lb
21137 random-access-iterator-type var = lb
21138 pointer-type var = lb
21140 cp_decl_specifier_seq type_specifiers;
21142 /* First, try to parse as an initialized declaration. See
21143 cp_parser_condition, from whence the bulk of this is copied. */
21145 cp_parser_parse_tentatively (parser);
21146 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21148 if (cp_parser_parse_definitely (parser))
21150 /* If parsing a type specifier seq succeeded, then this
21151 MUST be a initialized declaration. */
21152 tree asm_specification, attributes;
21153 cp_declarator *declarator;
21155 declarator = cp_parser_declarator (parser,
21156 CP_PARSER_DECLARATOR_NAMED,
21157 /*ctor_dtor_or_conv_p=*/NULL,
21158 /*parenthesized_p=*/NULL,
21159 /*member_p=*/false);
21160 attributes = cp_parser_attributes_opt (parser);
21161 asm_specification = cp_parser_asm_specification_opt (parser);
21163 if (declarator == cp_error_declarator)
21164 cp_parser_skip_to_end_of_statement (parser);
21168 tree pushed_scope, auto_node;
21170 decl = start_decl (declarator, &type_specifiers,
21171 SD_INITIALIZED, attributes,
21172 /*prefix_attributes=*/NULL_TREE,
21175 auto_node = type_uses_auto (TREE_TYPE (decl));
21176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21178 if (cp_lexer_next_token_is (parser->lexer,
21180 error ("parenthesized initialization is not allowed in "
21181 "OpenMP %<for%> loop");
21183 /* Trigger an error. */
21184 cp_parser_require (parser, CPP_EQ, "%<=%>");
21186 init = error_mark_node;
21187 cp_parser_skip_to_end_of_statement (parser);
21189 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21190 || type_dependent_expression_p (decl)
21193 bool is_direct_init, is_non_constant_init;
21195 init = cp_parser_initializer (parser,
21197 &is_non_constant_init);
21199 if (auto_node && describable_type (init))
21202 = do_auto_deduction (TREE_TYPE (decl), init,
21205 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21206 && !type_dependent_expression_p (decl))
21210 cp_finish_decl (decl, init, !is_non_constant_init,
21212 LOOKUP_ONLYCONVERTING);
21213 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21216 = tree_cons (NULL, this_pre_body, for_block);
21220 init = pop_stmt_list (this_pre_body);
21221 this_pre_body = NULL_TREE;
21226 cp_lexer_consume_token (parser->lexer);
21227 init = cp_parser_assignment_expression (parser, false);
21230 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21231 init = error_mark_node;
21233 cp_finish_decl (decl, NULL_TREE,
21234 /*init_const_expr_p=*/false,
21236 LOOKUP_ONLYCONVERTING);
21240 pop_scope (pushed_scope);
21246 /* If parsing a type specifier sequence failed, then
21247 this MUST be a simple expression. */
21248 cp_parser_parse_tentatively (parser);
21249 decl = cp_parser_primary_expression (parser, false, false,
21251 if (!cp_parser_error_occurred (parser)
21254 && CLASS_TYPE_P (TREE_TYPE (decl)))
21258 cp_parser_parse_definitely (parser);
21259 cp_parser_require (parser, CPP_EQ, "%<=%>");
21260 rhs = cp_parser_assignment_expression (parser, false);
21261 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21263 tf_warning_or_error));
21264 add_private_clause = true;
21269 cp_parser_abort_tentative_parse (parser);
21270 init = cp_parser_expression (parser, false);
21273 if (TREE_CODE (init) == MODIFY_EXPR
21274 || TREE_CODE (init) == MODOP_EXPR)
21275 real_decl = TREE_OPERAND (init, 0);
21280 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21283 this_pre_body = pop_stmt_list (this_pre_body);
21287 pre_body = push_stmt_list ();
21289 add_stmt (this_pre_body);
21290 pre_body = pop_stmt_list (pre_body);
21293 pre_body = this_pre_body;
21298 if (par_clauses != NULL && real_decl != NULL_TREE)
21301 for (c = par_clauses; *c ; )
21302 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21303 && OMP_CLAUSE_DECL (*c) == real_decl)
21305 error ("%Hiteration variable %qD should not be firstprivate",
21307 *c = OMP_CLAUSE_CHAIN (*c);
21309 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21310 && OMP_CLAUSE_DECL (*c) == real_decl)
21312 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21313 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21314 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21315 OMP_CLAUSE_DECL (l) = real_decl;
21316 OMP_CLAUSE_CHAIN (l) = clauses;
21317 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21319 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21320 CP_OMP_CLAUSE_INFO (*c) = NULL;
21321 add_private_clause = false;
21325 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21326 && OMP_CLAUSE_DECL (*c) == real_decl)
21327 add_private_clause = false;
21328 c = &OMP_CLAUSE_CHAIN (*c);
21332 if (add_private_clause)
21335 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21337 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21338 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21339 && OMP_CLAUSE_DECL (c) == decl)
21341 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21342 && OMP_CLAUSE_DECL (c) == decl)
21343 error ("%Hiteration variable %qD should not be firstprivate",
21345 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21346 && OMP_CLAUSE_DECL (c) == decl)
21347 error ("%Hiteration variable %qD should not be reduction",
21352 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21353 OMP_CLAUSE_DECL (c) = decl;
21354 c = finish_omp_clauses (c);
21357 OMP_CLAUSE_CHAIN (c) = clauses;
21364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21366 /* If decl is an iterator, preserve LHS and RHS of the relational
21367 expr until finish_omp_for. */
21369 && (type_dependent_expression_p (decl)
21370 || CLASS_TYPE_P (TREE_TYPE (decl))))
21371 cond = cp_parser_omp_for_cond (parser, decl);
21373 cond = cp_parser_condition (parser);
21375 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21380 /* If decl is an iterator, preserve the operator on decl
21381 until finish_omp_for. */
21383 && (type_dependent_expression_p (decl)
21384 || CLASS_TYPE_P (TREE_TYPE (decl))))
21385 incr = cp_parser_omp_for_incr (parser, decl);
21387 incr = cp_parser_expression (parser, false);
21390 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21391 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21392 /*or_comma=*/false,
21393 /*consume_paren=*/true);
21395 TREE_VEC_ELT (declv, i) = decl;
21396 TREE_VEC_ELT (initv, i) = init;
21397 TREE_VEC_ELT (condv, i) = cond;
21398 TREE_VEC_ELT (incrv, i) = incr;
21400 if (i == collapse - 1)
21403 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21404 in between the collapsed for loops to be still considered perfectly
21405 nested. Hopefully the final version clarifies this.
21406 For now handle (multiple) {'s and empty statements. */
21407 cp_parser_parse_tentatively (parser);
21410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21412 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21414 cp_lexer_consume_token (parser->lexer);
21417 else if (bracecount
21418 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21419 cp_lexer_consume_token (parser->lexer);
21422 loc = cp_lexer_peek_token (parser->lexer)->location;
21423 error ("%Hnot enough collapsed for loops", &loc);
21424 collapse_err = true;
21425 cp_parser_abort_tentative_parse (parser);
21434 cp_parser_parse_definitely (parser);
21435 nbraces += bracecount;
21439 /* Note that we saved the original contents of this flag when we entered
21440 the structured block, and so we don't need to re-save it here. */
21441 parser->in_statement = IN_OMP_FOR;
21443 /* Note that the grammar doesn't call for a structured block here,
21444 though the loop as a whole is a structured block. */
21445 body = push_stmt_list ();
21446 cp_parser_statement (parser, NULL_TREE, false, NULL);
21447 body = pop_stmt_list (body);
21449 if (declv == NULL_TREE)
21452 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21453 pre_body, clauses);
21457 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21459 cp_lexer_consume_token (parser->lexer);
21462 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21463 cp_lexer_consume_token (parser->lexer);
21468 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21469 error ("%Hcollapsed loops not perfectly nested", &loc);
21471 collapse_err = true;
21472 cp_parser_statement_seq_opt (parser, NULL);
21473 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21479 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21480 for_block = TREE_CHAIN (for_block);
21487 #pragma omp for for-clause[optseq] new-line
21490 #define OMP_FOR_CLAUSE_MASK \
21491 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21492 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21493 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21494 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21495 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21496 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21497 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21498 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21501 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21503 tree clauses, sb, ret;
21506 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21507 "#pragma omp for", pragma_tok);
21509 sb = begin_omp_structured_block ();
21510 save = cp_parser_begin_omp_structured_block (parser);
21512 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21514 cp_parser_end_omp_structured_block (parser, save);
21515 add_stmt (finish_omp_structured_block (sb));
21521 # pragma omp master new-line
21522 structured-block */
21525 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21527 cp_parser_require_pragma_eol (parser, pragma_tok);
21528 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21532 # pragma omp ordered new-line
21533 structured-block */
21536 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21538 cp_parser_require_pragma_eol (parser, pragma_tok);
21539 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21545 { section-sequence }
21548 section-directive[opt] structured-block
21549 section-sequence section-directive structured-block */
21552 cp_parser_omp_sections_scope (cp_parser *parser)
21554 tree stmt, substmt;
21555 bool error_suppress = false;
21558 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21561 stmt = push_stmt_list ();
21563 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21567 substmt = begin_omp_structured_block ();
21568 save = cp_parser_begin_omp_structured_block (parser);
21572 cp_parser_statement (parser, NULL_TREE, false, NULL);
21574 tok = cp_lexer_peek_token (parser->lexer);
21575 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21577 if (tok->type == CPP_CLOSE_BRACE)
21579 if (tok->type == CPP_EOF)
21583 cp_parser_end_omp_structured_block (parser, save);
21584 substmt = finish_omp_structured_block (substmt);
21585 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21586 add_stmt (substmt);
21591 tok = cp_lexer_peek_token (parser->lexer);
21592 if (tok->type == CPP_CLOSE_BRACE)
21594 if (tok->type == CPP_EOF)
21597 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21599 cp_lexer_consume_token (parser->lexer);
21600 cp_parser_require_pragma_eol (parser, tok);
21601 error_suppress = false;
21603 else if (!error_suppress)
21605 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21606 error_suppress = true;
21609 substmt = cp_parser_omp_structured_block (parser);
21610 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21611 add_stmt (substmt);
21613 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21615 substmt = pop_stmt_list (stmt);
21617 stmt = make_node (OMP_SECTIONS);
21618 TREE_TYPE (stmt) = void_type_node;
21619 OMP_SECTIONS_BODY (stmt) = substmt;
21626 # pragma omp sections sections-clause[optseq] newline
21629 #define OMP_SECTIONS_CLAUSE_MASK \
21630 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21631 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21632 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21633 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21634 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21637 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21641 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21642 "#pragma omp sections", pragma_tok);
21644 ret = cp_parser_omp_sections_scope (parser);
21646 OMP_SECTIONS_CLAUSES (ret) = clauses;
21652 # pragma parallel parallel-clause new-line
21653 # pragma parallel for parallel-for-clause new-line
21654 # pragma parallel sections parallel-sections-clause new-line */
21656 #define OMP_PARALLEL_CLAUSE_MASK \
21657 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21658 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21659 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21660 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21661 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21662 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21663 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21664 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21667 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21669 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21670 const char *p_name = "#pragma omp parallel";
21671 tree stmt, clauses, par_clause, ws_clause, block;
21672 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21677 cp_lexer_consume_token (parser->lexer);
21678 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21679 p_name = "#pragma omp parallel for";
21680 mask |= OMP_FOR_CLAUSE_MASK;
21681 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21683 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21686 const char *p = IDENTIFIER_POINTER (id);
21687 if (strcmp (p, "sections") == 0)
21689 cp_lexer_consume_token (parser->lexer);
21690 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21691 p_name = "#pragma omp parallel sections";
21692 mask |= OMP_SECTIONS_CLAUSE_MASK;
21693 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21697 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21698 block = begin_omp_parallel ();
21699 save = cp_parser_begin_omp_structured_block (parser);
21703 case PRAGMA_OMP_PARALLEL:
21704 cp_parser_statement (parser, NULL_TREE, false, NULL);
21705 par_clause = clauses;
21708 case PRAGMA_OMP_PARALLEL_FOR:
21709 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21710 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21713 case PRAGMA_OMP_PARALLEL_SECTIONS:
21714 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21715 stmt = cp_parser_omp_sections_scope (parser);
21717 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21721 gcc_unreachable ();
21724 cp_parser_end_omp_structured_block (parser, save);
21725 stmt = finish_omp_parallel (par_clause, block);
21726 if (p_kind != PRAGMA_OMP_PARALLEL)
21727 OMP_PARALLEL_COMBINED (stmt) = 1;
21732 # pragma omp single single-clause[optseq] new-line
21733 structured-block */
21735 #define OMP_SINGLE_CLAUSE_MASK \
21736 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21737 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21738 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21739 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21742 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21744 tree stmt = make_node (OMP_SINGLE);
21745 TREE_TYPE (stmt) = void_type_node;
21747 OMP_SINGLE_CLAUSES (stmt)
21748 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21749 "#pragma omp single", pragma_tok);
21750 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21752 return add_stmt (stmt);
21756 # pragma omp task task-clause[optseq] new-line
21757 structured-block */
21759 #define OMP_TASK_CLAUSE_MASK \
21760 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21761 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21762 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21763 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21764 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21765 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21768 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21770 tree clauses, block;
21773 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21774 "#pragma omp task", pragma_tok);
21775 block = begin_omp_task ();
21776 save = cp_parser_begin_omp_structured_block (parser);
21777 cp_parser_statement (parser, NULL_TREE, false, NULL);
21778 cp_parser_end_omp_structured_block (parser, save);
21779 return finish_omp_task (clauses, block);
21783 # pragma omp taskwait new-line */
21786 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21788 cp_parser_require_pragma_eol (parser, pragma_tok);
21789 finish_omp_taskwait ();
21793 # pragma omp threadprivate (variable-list) */
21796 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21800 vars = cp_parser_omp_var_list (parser, 0, NULL);
21801 cp_parser_require_pragma_eol (parser, pragma_tok);
21803 finish_omp_threadprivate (vars);
21806 /* Main entry point to OpenMP statement pragmas. */
21809 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21813 switch (pragma_tok->pragma_kind)
21815 case PRAGMA_OMP_ATOMIC:
21816 cp_parser_omp_atomic (parser, pragma_tok);
21818 case PRAGMA_OMP_CRITICAL:
21819 stmt = cp_parser_omp_critical (parser, pragma_tok);
21821 case PRAGMA_OMP_FOR:
21822 stmt = cp_parser_omp_for (parser, pragma_tok);
21824 case PRAGMA_OMP_MASTER:
21825 stmt = cp_parser_omp_master (parser, pragma_tok);
21827 case PRAGMA_OMP_ORDERED:
21828 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21830 case PRAGMA_OMP_PARALLEL:
21831 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21833 case PRAGMA_OMP_SECTIONS:
21834 stmt = cp_parser_omp_sections (parser, pragma_tok);
21836 case PRAGMA_OMP_SINGLE:
21837 stmt = cp_parser_omp_single (parser, pragma_tok);
21839 case PRAGMA_OMP_TASK:
21840 stmt = cp_parser_omp_task (parser, pragma_tok);
21843 gcc_unreachable ();
21847 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21852 static GTY (()) cp_parser *the_parser;
21855 /* Special handling for the first token or line in the file. The first
21856 thing in the file might be #pragma GCC pch_preprocess, which loads a
21857 PCH file, which is a GC collection point. So we need to handle this
21858 first pragma without benefit of an existing lexer structure.
21860 Always returns one token to the caller in *FIRST_TOKEN. This is
21861 either the true first token of the file, or the first token after
21862 the initial pragma. */
21865 cp_parser_initial_pragma (cp_token *first_token)
21869 cp_lexer_get_preprocessor_token (NULL, first_token);
21870 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21873 cp_lexer_get_preprocessor_token (NULL, first_token);
21874 if (first_token->type == CPP_STRING)
21876 name = first_token->u.value;
21878 cp_lexer_get_preprocessor_token (NULL, first_token);
21879 if (first_token->type != CPP_PRAGMA_EOL)
21880 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21881 &first_token->location);
21884 error ("%Hexpected string literal", &first_token->location);
21886 /* Skip to the end of the pragma. */
21887 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21888 cp_lexer_get_preprocessor_token (NULL, first_token);
21890 /* Now actually load the PCH file. */
21892 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21894 /* Read one more token to return to our caller. We have to do this
21895 after reading the PCH file in, since its pointers have to be
21897 cp_lexer_get_preprocessor_token (NULL, first_token);
21900 /* Normal parsing of a pragma token. Here we can (and must) use the
21904 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21906 cp_token *pragma_tok;
21909 pragma_tok = cp_lexer_consume_token (parser->lexer);
21910 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21911 parser->lexer->in_pragma = true;
21913 id = pragma_tok->pragma_kind;
21916 case PRAGMA_GCC_PCH_PREPROCESS:
21917 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21918 &pragma_tok->location);
21921 case PRAGMA_OMP_BARRIER:
21924 case pragma_compound:
21925 cp_parser_omp_barrier (parser, pragma_tok);
21928 error ("%H%<#pragma omp barrier%> may only be "
21929 "used in compound statements", &pragma_tok->location);
21936 case PRAGMA_OMP_FLUSH:
21939 case pragma_compound:
21940 cp_parser_omp_flush (parser, pragma_tok);
21943 error ("%H%<#pragma omp flush%> may only be "
21944 "used in compound statements", &pragma_tok->location);
21951 case PRAGMA_OMP_TASKWAIT:
21954 case pragma_compound:
21955 cp_parser_omp_taskwait (parser, pragma_tok);
21958 error ("%H%<#pragma omp taskwait%> may only be "
21959 "used in compound statements",
21960 &pragma_tok->location);
21967 case PRAGMA_OMP_THREADPRIVATE:
21968 cp_parser_omp_threadprivate (parser, pragma_tok);
21971 case PRAGMA_OMP_ATOMIC:
21972 case PRAGMA_OMP_CRITICAL:
21973 case PRAGMA_OMP_FOR:
21974 case PRAGMA_OMP_MASTER:
21975 case PRAGMA_OMP_ORDERED:
21976 case PRAGMA_OMP_PARALLEL:
21977 case PRAGMA_OMP_SECTIONS:
21978 case PRAGMA_OMP_SINGLE:
21979 case PRAGMA_OMP_TASK:
21980 if (context == pragma_external)
21982 cp_parser_omp_construct (parser, pragma_tok);
21985 case PRAGMA_OMP_SECTION:
21986 error ("%H%<#pragma omp section%> may only be used in "
21987 "%<#pragma omp sections%> construct", &pragma_tok->location);
21991 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21992 c_invoke_pragma_handler (id);
21996 cp_parser_error (parser, "expected declaration specifiers");
22000 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22004 /* The interface the pragma parsers have to the lexer. */
22007 pragma_lex (tree *value)
22010 enum cpp_ttype ret;
22012 tok = cp_lexer_peek_token (the_parser->lexer);
22015 *value = tok->u.value;
22017 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22019 else if (ret == CPP_STRING)
22020 *value = cp_parser_string_literal (the_parser, false, false);
22023 cp_lexer_consume_token (the_parser->lexer);
22024 if (ret == CPP_KEYWORD)
22032 /* External interface. */
22034 /* Parse one entire translation unit. */
22037 c_parse_file (void)
22039 bool error_occurred;
22040 static bool already_called = false;
22042 if (already_called)
22044 sorry ("inter-module optimizations not implemented for C++");
22047 already_called = true;
22049 the_parser = cp_parser_new ();
22050 push_deferring_access_checks (flag_access_control
22051 ? dk_no_deferred : dk_no_check);
22052 error_occurred = cp_parser_translation_unit (the_parser);
22056 #include "gt-cp-parser.h"